Izzhov
Izzhov

Reputation: 163

Find number of rows/columns in a GSL matrix?

Say I have some gsl_matrix * A. I want to write a function that retrieves e.g. the number of rows in this matrix, without having access to anything else besides the object A itself.

Example:

int num_rows(gsl_matrix * A){
    //some operation(s) on A that find the number of rows in the matrix
    //store that number in an int r
    return r;
}

What could I write to do this for me?

Upvotes: 2

Views: 1987

Answers (1)

NathanOliver
NathanOliver

Reputation: 180825

From https://www.gnu.org/software/gsl/manual/html_node/Matrices.html

gsl_matrix is defined as:

typedef struct
{
  size_t size1;
  size_t size2;
  size_t tda;
  double * data;
  gsl_block * block;
  int owner;
} gsl_matrix;

And

The number of rows is size1. The range of valid row indices runs from 0 to size1-1. Similarly size2 is the number of columns. The range of valid column indices runs from 0 to size2-1. The physical row dimension tda, or trailing dimension, specifies the size of a row of the matrix as laid out in memory.

So if you want the number of rows in A then you would use:

int num_rows(gsl_matrix * A){
    int r = A->size1;
    return r;
}

Upvotes: 4

Related Questions