项目中需要快速求解Asin(x) 的近似值,原以为用泰勒展开式会快一些,结果比原生的慢一倍。

Math.ASin
        Time Elapsed:   9ms
        Gen 0:          0
        Gen 1:          0
        Gen 2:          0
Maclaurin.ASin
        Time Elapsed:   17ms
        Gen 0:          4
        Gen 1:          0
        Gen 2:          0

各位,谁有能力改进?

附:

http://www.mathportal.org/formulas/pdf/taylor-series-formulas.pdf

http://pages.pacificcoast.net/~cazelais/260/maclaurin.pdf

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Text;
using Diagnostics; namespace Asin
{
class Program
{
static void Main(string[] args)
{
int count = 100000;
List<double> values = new List<double>(count); Random r = new Random();
for (var i = 0; i <= count; ++i)
{
values .Add(r.NextDouble() * 2 - 1);
} CodeTime.Init();
int? iter = 0;
CodeTime.Timer("Math.ASin", count, () =>
{
var i = iter.Value + 1;
iter = i; Math.Asin(values[i]); }); iter = 0;
CodeTime.Timer("Maclaurin.ASin", count, () =>
{
var i = iter.Value + 1;
iter = i; Maclaurin.Asin(values[i],3); }); while (true)
{
iter = 0;
CodeTime.Timer("Math.ASin", count, () =>
{
var i = iter.Value + 1;
iter = i; Math.Asin(values[i]); }); iter = 0;
CodeTime.Timer("Maclaurin.ASin", count, () =>
{
var i = iter.Value + 1;
iter = i; Maclaurin.Asin(values[i], 3); });
} //var ret = Maclaurin.Asin(0.5, 3);
//var ret2 = Math.Asin(0.5);
//Console.WriteLine(ret);
//Console.WriteLine(ret2);
Console.ReadLine();
}
} class Maclaurin
{
class ASinImpl
{
private List<double> quotieties = new List<double>();
private IEnumerator<double> computeQuotieties = null; public ASinImpl()
{
this.computeQuotieties = ComputeQuotiety();
} public double Calc(double v, int precision = 2)
{
if (quotieties.Count < precision)
{
for (var i = quotieties.Count; i < precision; ++i)
{
computeQuotieties.MoveNext();
quotieties.Add(computeQuotieties.Current);
}
} double ret = 0;
var values = ComputeValues(v);
for (int i = 0; i < precision; ++i)
{
values.MoveNext();
ret += quotieties[i]*values.Current;
} return ret;
} private IEnumerator<double> ComputeValues(double v)
{
double ret = 1;
double q = v*v;
for(int i = 0;;++i)
{ if (i == 0)
{
ret = v;
yield return ret;
}
else
{
ret *= q;
yield return ret;
}
} throw new NotImplementedException();
} private IEnumerator<double> ComputeQuotiety()
{
for (int i = 0;; i++)
{ double up = Factorial(2*i); double down = Math.Pow(Math.Pow(2, i)*Factorial(i), 2)*(2*i + 1); double quotiety = up/down; yield return quotiety; } throw new NotImplementedException();
} private long Factorial(long v )
{
if( v < 0)
throw new ArgumentOutOfRangeException("v"); if (v == 0)
return 1; if (v == 1)
return 1; long ret = 1;
for (int i = 2; i <= v; ++i)
{
ret *= i;
} return ret;
}
} private static ASinImpl asinImpl = new ASinImpl(); /// <summary>
///
/// </summary>
/// <param name="v"></param>
/// <param name="precision"></param>
/// <returns></returns>
public static double Asin(double v, int precision)
{
if (v < -1 || v > 1)
{
throw new ArgumentOutOfRangeException("v");
} return asinImpl.Calc(v, precision);
}
}
}

通过一下优化:基本持平

  class ASinImpl
{
private readonly int _precision;
private double[] _quotieties = null;
private long[] _factorials =null;
public ASinImpl(int precision = 3)
{
_precision = precision;
_quotieties = new double[precision + 1];
_factorials = new long[(precision + 2)*2 + 1];
Factorial();
ComputeQuotiety();
} public double Calc(double v)
{
unchecked
{
double retVal = 0; double vVal = 1;
double q = v * v;
unsafe
{
fixed (double* pq = _quotieties)
{
for (int i = 0; i < _precision; ++i)
{ if (i == 0)
{
vVal = v;
//yield return ret;
retVal += pq[i] * vVal;
}
else
{
vVal *= q;
//yield return ret;
retVal += pq[i] * vVal;
}
} return retVal;
} } } } private void ComputeQuotiety()
{
unchecked
{
int precision = _quotieties.Length;
for (int i = 0; i < precision; i++)
{ double up = _factorials[2*i]; double down = Math.Pow(Math.Pow(2, i)*_factorials[i], 2)*(2*i + 1); double quotiety = up/down; _quotieties[i] = quotiety; }
} } private void Factorial()
{
unchecked
{
int precision = _factorials.Length ;
long ret = 1;
for (long v = 0; v < precision; ++v)
{
if (v == 0)
{
this._factorials[v] = 1;
}
else if (v == 1){
this._factorials[v] = 1;
}
else
{
ret *= v; this._factorials[v] = ret;
} }
}
}
}

实现 Math.Asin 迈克劳林(泰勒)展开式,结果比Math.Asin 慢一倍的更多相关文章

  1. YTU 2452: 麦克劳林用于函数求值

    2452: 麦克劳林用于函数求值 时间限制: 1 Sec  内存限制: 128 MB 提交: 18  解决: 12 题目描述 泰勒公式是一个用函数在某点的信息描述其附近取值的公式.如果函数足够光滑的话 ...

  2. python数学第二天【泰勒展开式】

    1. 泰勒展开式 推论1: 泰勒展开式的应用 推论2: 推论3:

  3. luogu P2000 拯救世界 生成函数_麦克劳林展开_python

    模板题. 将所有的多项式按等比数列求和公式将生成函数压缩,相乘后麦克劳林展开即可. Code: n=int(input()) print((n+1)*(n+2)*(n+3)*(n+4)//24)

  4. bzoj 3028: 食物 生成函数_麦克劳林展开

    不管怎么求似乎都不太好求,我们试试生成函数.这个东西好神奇.生成函数的精华是两个生成函数相乘,对应 $x^{i}$ 前的系数表示取 $i$ 个时的方案数. 有时候,我们会将函数按等比数列求和公式进行压 ...

  5. XGBoost 完整推导过程

    参考: 陈天奇-"XGBoost: A Scalable Tree Boosting System" Paper地址: <https://arxiv.org/abs/1603 ...

  6. 【数学基础篇】---详解极限与微分学与Jensen 不等式

    一.前述 数学基础知识对机器学习还有深度学习的知识点理解尤为重要,本节主要讲解极限等相关知识. 二.极限 1.例子 当 x 趋于 0 的时候,sin(x) 与 tan(x) 都趋于 0. 但是哪一个趋 ...

  7. bzoj3028食物

    http://www.lydsy.com/JudgeOnline/problem.php?id=3028 好吧,这是我第一道生成函数的题目. 先搞出各种食物的生成函数: 汉堡:$1+x^2+x^4+. ...

  8. JavaScript中Math对象的方法介绍

    1.比较最值方法 比较最值有两种方法,max() 和 min() 方法. 1.1 max() 方法,比较一组数值中的最大值,返回最大值. var maxnum = Math.max(12,6,43,5 ...

  9. JavaScript Math和Number对象

    目录 1. Math 对象:数学对象,提供对数据的数学计算.如:获取绝对值.向上取整等.无构造函数,无法被初始化,只提供静态属性和方法. 2. Number 对象 :Js中提供数字的对象.包含整数.浮 ...

随机推荐

  1. PHP 汉字拼音互转

    <?PHP function Pinyin($_String, $_Code='gb2312'){ $_DataKey = "a|ai|an|ang|ao|ba|bai|ban|ban ...

  2. Orcale 数据库客户端PL/SQL中文乱码的问题

    http://jingyan.baidu.com/article/948f59242aa519d80ef5f96e.html

  3. iOS Real Stuff

    Ray Wenderlich     AppCoda(English)   AppCoda(TW) Awesome iOS      Code4App代码库     CocoaChina代码库   o ...

  4. Python for Infomatics 第13章 网页服务四(译)

    这几天因为其他事务,打断了自己的学习计划,今天继续我的翻译,避免又中途而废. 注:文章原文为Dr. Charles Severance 的 <Python for Informatics> ...

  5. 双向链表、双向循环链表的JS实现

    关于链表简介.单链表.单向循环链表.JS中的使用以及扩充方法:  单链表.循环链表的JS实现 关于四种链表的完整封装: https://github.com/zhuwq585/Data-Structu ...

  6. 子代选择器(>)后代选择器(' ')的区别

    子代选择器是指紧接着父级的那个标签,如:container>a指的是紧接着container后面的第一个a(儿子级别的,孙子或者之后的a是不能生效的) 后代选择器是用空格分开的,如:contai ...

  7. oracle 和c3p0 数据库的Time_Wait 过多问题的一个解决方案。

    项目是B/S模式,放在linux服务器上,tomcat和oracle11g在一台服务器上,tomcat读取数据库采用C3P0连接池,一直比较稳定,所以也没有去管.后来把tomcat放在一台win200 ...

  8. [RxJava^Android]项目经验分享 --- RxLifecycle功能实现分析(一)

      最近在研究RxJava自定义操作符的实现原理,发现成型的项目案例较少.突然想起在项目中应用的RxLifecycle是使用自定义操作符,便拿来研究了一下.分析之前,跟大家了解一些相关操作符和RxLi ...

  9. C#调用win32 api程序实例

    1.声明static extern 方法,使用DllImport特性 class MyClass { [DllImport("kernel32", SetLastError = t ...

  10. css 浅析display属性

    继续开始我的css之旅吧.今天我们来说什么啊.构思了两天还是没有什么思路,但是学习的步伐我们不能停止下来.还是按照之前的计划来讲讲display,在讲这个之前我们还是按照老规矩来扯扯蛋,步子不能够迈大 ...