initialization of a gsl_matrix

857 views Asked by At

I am using GNU scientific library, and I want to initialize a matrix with values, but I can't understand how to do without a loop :

This works :

  gsl_matrix * m = gsl_matrix_alloc (3, 3);

  for (int i = 0; i < 3; i++)
    for (int j = 0; j < 3; j++)
      gsl_matrix_set (m, i, j, 100*i + j);

But I would like to do something like that :

    double data[] = { i  , 1/2.0, 1/3.0,
                      1/2.0, 1/3.0, 1/4.0,
                      1/3.0, 1/4.0, 1/5.0};

    gsl_matrix mat = gsl_matrix_from_array(data); // does not exists

Is there a way to do that ?

1

There are 1 answers

0
AndrewCox On BEST ANSWER

You could use std::copy() to do this.

#include <iostream>
#include <gsl/gsl_matrix.h>

using namespace std;

int main(void){

    double data[] = {1,2,3,4};
    gsl_matrix *m = gsl_matrix_alloc(2,2);
    copy(data, data+4, m->data);

    printf("Matrix:\n%f %f\n%f %f\n", gsl_matrix_get(m, 0, 0), gsl_matrix_get(m, 0, 1),
        gsl_matrix_get(m, 1, 0), gsl_matrix_get(m, 1, 1));
    return 0;
}