小编grd*_*fgr的帖子

一次计算正弦和余弦

我有一个使用相同参数的正弦和余弦的科学代码(我基本上需要该参数的复数指数).我想知道是否有可能比分别调用正弦和余弦函数更快.

另外我只需要0.1%的精度.那么有什么方法可以找到默认的触发功能并截断功率系列的速度?

我想到的另一件事是,有没有办法执行余数运算,结果总是积极的?在我自己使用的算法中,x=fmod(x,2*pi);但如果x为负数,我需要加2pi(较小的域意味着我可以使用较短的幂级数)

编辑:LUT原来是最好的方法,但我很高兴我了解了其他近似技术.我还建议使用明确的中点近似.这就是我最终做的事情:

const int N = 10000;//about 3e-4 error for 1000//3e-5 for 10 000//3e-6 for 100 000
double *cs = new double[N];
double *sn = new double[N];
for(int i  =0;i<N;i++){
    double A= (i+0.5)*2*pi/N;
    cs[i]=cos(A);
    sn[i]=sin(A);
}
Run Code Online (Sandbox Code Playgroud)

以下部分近似(中点)sincos(2*pi*(wc2 + t [j]*(cotp*t [j] -wc)))

double A=(wc2+t[j]*(cotp*t[j]-wc));
int B =(int)N*(A-floor(A));
re += cs[B]*f[j];
im += sn[B]*f[j];
Run Code Online (Sandbox Code Playgroud)

另一种方法可能是使用切比雪夫分解.您可以使用orthogonality属性来查找系数.针对指数进行了优化,它看起来像这样:

double fastsin(double x){
    x=x-floor(x/2/pi)*2*pi-pi;//this line can be improved, both inside this 
                              //function and before you input it into the function

    double x2 …
Run Code Online (Sandbox Code Playgroud)

c++ algorithm trigonometry

9
推荐指数
1
解决办法
2116
查看次数

为什么我的正弦算法比默认算法慢得多?

const double pi = 3.1415926535897;

static double mysin(double x) {
    return ((((((-0.000140298 * x - 0.00021075890) * x + 0.008703147) * x -
        0.0003853080) * x - 0.16641544) * x - 0.00010117316) * x +
        1.000023121) * x;
}

static void Main(string[] args) {
    Stopwatch sw = new Stopwatch();

    double a = 0;
    double[] arg = new double[1000000];
    for (int i = 0; i < 1000000; i++) {
        arg[i] = (pi / 2000000);
    } 
    sw.Restart();
    for (int i = 0; …
Run Code Online (Sandbox Code Playgroud)

c# inline

7
推荐指数
1
解决办法
215
查看次数

MATLAB IDE 中的括号组颜色

有没有办法让Matlab配色括号?

A = max(B(:,3))
Run Code Online (Sandbox Code Playgroud)

其中函数括号 formax()应具有相同的颜色,索引括号B(:,3)应具有不同的颜色。

matlab

4
推荐指数
1
解决办法
667
查看次数

标签 统计

algorithm ×1

c# ×1

c++ ×1

inline ×1

matlab ×1

trigonometry ×1