C语言 将数组的每个元素乘以 C 中的数字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16734813/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:27:29  来源:igfitidea点击:

Multiply each element of an array by a number in C

carraysmultiplying

提问by seb

I'm trying to optimize some of my code in C, which is a lot bigger than the snippet below. Coming from Python, I wonder whether you can simply multiply an entire array by a number like I do below.

我正在尝试用 C 优化我的一些代码,这比下面的代码段要大得多。来自 Python,我想知道您是否可以像下面那样简单地将整个数组乘以一个数字。

Evidently, it does not work the way I do it below. Is there any other way that achieves the same thing, or do I have to step through the entire array as in the for loop?

显然,它不像我在下面做的那样工作。有没有其他方法可以达到同样的目的,或者我是否必须像在 for 循环中那样遍历整个数组?

void main()
{
    int i;
    float data[] = {1.,2.,3.,4.,5.};

    //this fails
    data *= 5.0;

    //this works
    for(i = 0; i < 5; i++) data[i] *= 5.0;

}

回答by simonc

There is no short-cut you have to step through each element of the array.

没有捷径可以遍历数组的每个元素。

Note however that in your example, you may achieve a speedup by using intrather than floatfor both your data and multiplier.

但是请注意,在您的示例中,您可以通过使用int而不是float同时使用数据和乘数来实现加速。

回答by cybrhuman

If you want to, you can do what you want through BLAS, Basic Linear Algebra Subprograms, which is optimised. This is not in the C standard, it is a package which you have to install yourself.

如果您愿意,您可以通过优化的BLAS基本线性代数子程序做您想做的事。这不在 C 标准中,它是一个您必须自己安装的包。

Sample code to achieve what you want:

实现您想要的示例代码:

#include <stdio.h>
#include <stdlib.h>
#include <cblas.h>
int main () {            
    int limit =10;
    float *a =  calloc( limit, sizeof(float));
    for ( int i = 0; i < limit ; i++){
        a[i] = i;
    }
    cblas_sscal( limit , 0.5f, a, 1); 

    for ( int i = 0; i < limit ; i++){
        printf("%3f, " , a[i]);
    }
    printf("\n");
}

The names of the functions is not obvious, but reading the guidelines you might start to guess what BLASfunctions does. sscal()can be split into sfor single precisionand scalfor scale, which means that this function works on floats. The same function for double precisionis called dscal().

函数的名称并不明显,但阅读指南您可能会开始猜测BLAS函数的作用。sscal()可以拆分为sfor single precisionscalfor scale,这意味着此函数适用于浮点数。双精度的相同函数称为dscal()

If you need to scale a vector with a constant and adding it to another, BLAS got a function for that too:

如果您需要使用常量缩放向量并将其添加到另一个向量,BLAS 也有一个函数:

saxpy()
s      a x p y
float  a*x + y
y[i] += a*x

As you might guess there is a daxpy()too which works on doubles.

正如您可能猜到的那样,还有一个daxpy()适用于doubles.

回答by cybrhuman

I'm afraid that, in C, you will have to use for(i = 0; i < 5; i++) data[i] *= 5.0;. Python allows for so many more "shortcuts"; however, in C, you have to access each element and then manipulate those values.

恐怕在 C 中,您将不得不使用for(i = 0; i < 5; i++) data[i] *= 5.0;. Python 允许更多的“快捷方式”;但是,在 C 中,您必须访问每个元素,然后操作这些值。

Using the for-loop would be the shortest way to accomplish what you're trying to do to the array.

使用 for 循环将是完成您要对数组执行的操作的最短方法。

EDIT: If you have a large amount of data, there are more efficient (in terms of running time) ways to multiply 5 to each value. Check out loop tiling, for example.

编辑:如果您有大量数据,则有更有效的(就运行时间而言)方法将 5 乘以每个值。例如,检查循环平铺。

回答by mahe5_36

data *= 5.0;

Here datais address of array which is constant. if you want to multiply the first value in that array then use * operator as below.

这里的数据是数组的地址,它是常量。如果要乘以该数组中的第一个值,请使用 * 运算符,如下所示。

*data *= 5.0;