gpt4 book ai didi

c++ - 使用 -> 获取 gsl_matrix 结构指针

转载 作者:行者123 更新时间:2023-11-28 08:16:03 27 4
gpt4 key购买 nike

我想要一个为 gsl_matrix 数据指针赋值的快捷方式,而不是将 gsl_matrix_set 写 16 次,但我无法让它工作。一个独立的小样本:

#include <stdio.h>
#include <stdlib.h>
#include <gsl/gsl_linalg.h>
#include <gsl/gsl_matrix.h>

gsl_matrix* get_rates(double k) {

int nChar = 4;
gsl_matrix *rates = gsl_matrix_calloc(nChar, nChar);

//TODO: this apparently fails
rates->data = (double[]) {-1, k/(k+2), 1/(k+2), 1/(k+2),
k/(k+2), -1, 1/(k+2), 1/(k+2),
1/(k+2), 1/(k+2), -1, k/(k+2),
1/(k+2), 1/(k+2), k/(k+2), -1};

return (rates);

}

int main(void) {

gsl_matrix *rates;

rates = get_rates(2.0);

gsl_matrix_fprintf (stdout, rates, " %f");

return(0);
}

返回:

-1.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000

最佳答案

double data[16] = {
-1, k/(k+2), 1/(k+2), 1/(k+2),
k/(k+2), -1, 1/(k+2), 1/(k+2),
1/(k+2), 1/(k+2), -1, k/(k+2),
1/(k+2), 1/(k+2), k/(k+2), -1};

memcpy(rates->data, data, sizeof data);

引用资料:

关于c++ - 使用 -> 获取 gsl_matrix 结构指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7758558/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com