小编gou*_*aha的帖子

Matlab计算L2范数非常慢

我有以下 MATLAB 片段:

>> R = randn(3000,6000);   % build a random 3000 by 6000 matrix
>> tic; norm(R, 1); toc;
Elapsed time is 0.005586 seconds.
>> tic; norm(R, 2); toc;
Elapsed time is 3.019667 seconds.
>> tic; norm(R, inf); toc;
Elapsed time is 0.005393 seconds.
>> 
Run Code Online (Sandbox Code Playgroud)

我的问题是,为什么 L2 范数计算比 L1 或 L 无穷范数慢得多?当然,这是一个用于测试目的的随机矩阵,但对于我工作中的实际矩阵,我可以在经过的时间方面看到类似的模式。

然而在Julia上,结果如下

julia> @time norm(R, 1);
  0.007156 seconds (1 allocation: 16 bytes)

julia> @time norm(R, 2);
  0.009142 seconds (1 allocation: 16 bytes)

julia> @time norm(R, Inf);
  0.034633 seconds …
Run Code Online (Sandbox Code Playgroud)

performance matlab matrix linear-algebra julia

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

蒙特卡罗积分高斯函数f(x)= exp(-x ^ 2/2)在C不正确的输出中

我正在写一个简短的程序来近似高斯函数f(x)= exp(-x ^ 2/2)的定积分,我的代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

double gaussian(double x) {
    return exp((-pow(x,2))/2);
}

int main(void) {
    srand(0);
    double valIntegral, yReal = 0, xRand, yRand, yBound;
    int xMin, xMax, numTrials, countY = 0;

    do {
        printf("Please enter the number of trials (n): ");
        scanf("%d", &numTrials);
        if (numTrials < 1) {
            printf("Exiting.\n");
            return 0;
        }  
        printf("Enter the interval of integration (a b): ");
        scanf("%d %d", &xMin, &xMax);      
        while (xMin > xMax) { //keeps looping until a valid interval …
Run Code Online (Sandbox Code Playgroud)

c random integration montecarlo

2
推荐指数
1
解决办法
589
查看次数