实现 Math.Asin 迈克劳林(泰勒)展开式,结果比Math.Asin 慢一倍
项目中需要快速求解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 慢一倍的更多相关文章
- YTU 2452: 麦克劳林用于函数求值
2452: 麦克劳林用于函数求值 时间限制: 1 Sec 内存限制: 128 MB 提交: 18 解决: 12 题目描述 泰勒公式是一个用函数在某点的信息描述其附近取值的公式.如果函数足够光滑的话 ...
- python数学第二天【泰勒展开式】
1. 泰勒展开式 推论1: 泰勒展开式的应用 推论2: 推论3:
- luogu P2000 拯救世界 生成函数_麦克劳林展开_python
模板题. 将所有的多项式按等比数列求和公式将生成函数压缩,相乘后麦克劳林展开即可. Code: n=int(input()) print((n+1)*(n+2)*(n+3)*(n+4)//24)
- bzoj 3028: 食物 生成函数_麦克劳林展开
不管怎么求似乎都不太好求,我们试试生成函数.这个东西好神奇.生成函数的精华是两个生成函数相乘,对应 $x^{i}$ 前的系数表示取 $i$ 个时的方案数. 有时候,我们会将函数按等比数列求和公式进行压 ...
- XGBoost 完整推导过程
参考: 陈天奇-"XGBoost: A Scalable Tree Boosting System" Paper地址: <https://arxiv.org/abs/1603 ...
- 【数学基础篇】---详解极限与微分学与Jensen 不等式
一.前述 数学基础知识对机器学习还有深度学习的知识点理解尤为重要,本节主要讲解极限等相关知识. 二.极限 1.例子 当 x 趋于 0 的时候,sin(x) 与 tan(x) 都趋于 0. 但是哪一个趋 ...
- bzoj3028食物
http://www.lydsy.com/JudgeOnline/problem.php?id=3028 好吧,这是我第一道生成函数的题目. 先搞出各种食物的生成函数: 汉堡:$1+x^2+x^4+. ...
- JavaScript中Math对象的方法介绍
1.比较最值方法 比较最值有两种方法,max() 和 min() 方法. 1.1 max() 方法,比较一组数值中的最大值,返回最大值. var maxnum = Math.max(12,6,43,5 ...
- JavaScript Math和Number对象
目录 1. Math 对象:数学对象,提供对数据的数学计算.如:获取绝对值.向上取整等.无构造函数,无法被初始化,只提供静态属性和方法. 2. Number 对象 :Js中提供数字的对象.包含整数.浮 ...
随机推荐
- Protocol Buffer搭建及示例
本文来源:http://www.tanhao.me/code/150911.html/ Protocol Buffer(简称Protobuf或PB)是由Google推出的一种数据交换格式,与传统的XM ...
- [leetcode] 小心成环
156. Binary Tree Upside Down Given a binary tree where all the right nodes are either leaf nodes wit ...
- 连接Linux下 XAMPP集成环境中部署的禅道的数据库MariaDB
用mysql数据库工具连接linuxmysql环境,但是会遇到连接失败的问题,如下所示: 这就需要涉及到另外的问题了,需要我们打开mysql的连接授权,具体的操作步骤如下: 1)在xshell里进入m ...
- [spring源码学习]十、IOC源码-conversionService
一.代码示例 1.我们在之前的Person类里新增一个两个属性,分别是客户的兴趣和生日,兴趣爱好有很多,我们使用list进行保存,生日使用日期进行保存 public class Person { pr ...
- jQuery Validate验证框架详解
转自:http://www.cnblogs.com/linjiqin/p/3431835.html jQuery校验官网地址:http://bassistance.de/jquery-plugins/ ...
- 【转】通过Hibernate将数据 存入oracle数据库例子
一. Hibernate介绍 Hibernate是基于对象/关系映射(ORM,Object/Relational Mapping)的一个解决方案.ORM方案的思想是将对象模型表示的对象映射到关系型数据 ...
- linux终端 字符界面 显示乱码
方法一:配置SSH工具 SecureCRT中文版配置 [全局选项]→[默认会话]→[编辑默认设置]→[终端]→[外观]→[字体]→[新宋体 10pt CHINESE_GB2312]→[字符编码 UTF ...
- Cocos2d 利用继承Draw方法制作可显示三维数据(宠物三维等)的三角形显示面板
很久没有写博客了,这段时间比较忙,又是搬家又是做自己的项目,还有太多琐碎的事情缠身,好不容易抽出时间把最近自己做的一些简单例子记录一下. 在我的项目中,我需要一个显示面板来显示游戏中的一个三维数据,例 ...
- CentOS7 编译安装LVS 互为主备 (实测 笔记 Centos 7.0 + ipvsadm 1.27 + keepalived 1.2.15 )
环境: 系统硬件:vmware vsphere (CPU:2*4核,内存2G,双网卡) LVS服务器(两台): 系统:Centos7.0 64位(LVS+keepalived) LvsMaster:1 ...
- mysql 表表连接的问题。
select * from table a, table b where a.aid = b.bid and aid >100 这样连接,如果a有数据,b没有数据, a.aid = b.bid ...