Next: , Previous: Matrix allocation, Up: Matrices


8.4.2 Accessing matrix elements

The functions for accessing the elements of a matrix use the same range checking system as vectors. You can turn off range checking by recompiling your program with the preprocessor definition GSL_RANGE_CHECK_OFF.

The elements of the matrix are stored in “C-order”, where the second index moves continuously through memory. More precisely, the element accessed by the function gsl_matrix_get(m,i,j) and gsl_matrix_set(m,i,j,x) is

     m->data[i * m->tda + j]

where tda is the physical row-length of the matrix.

— Function: double gsl_matrix_get (const gsl_matrix * m, size_t i, size_t j)

This function returns the (i,j)-th element of a matrix m. If i or j lie outside the allowed range of 0 to n1-1 and 0 to n2-1 then the error handler is invoked and 0 is returned.

— Function: void gsl_matrix_set (gsl_matrix * m, size_t i, size_t j, double x)

This function sets the value of the (i,j)-th element of a matrix m to x. If i or j lies outside the allowed range of 0 to n1-1 and 0 to n2-1 then the error handler is invoked.

— Function: double * gsl_matrix_ptr (gsl_matrix * m, size_t i, size_t j)
— Function: const double * gsl_matrix_const_ptr (const gsl_matrix * m, size_t i, size_t j)

These functions return a pointer to the (i,j)-th element of a matrix m. If i or j lie outside the allowed range of 0 to n1-1 and 0 to n2-1 then the error handler is invoked and a null pointer is returned.