使用 - >对于gsl_matrix结构指针

时间:2011-10-13 18:15:52

标签: c++ gsl

我希望有一个快捷方式来为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

1 个答案:

答案 0 :(得分:1)

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);

参考文献: