0

我想要一个为 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
4

1 回答 1

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

参考:

于 2011-10-13T18:25:30.343 回答