GSL lib<matrix.h> C++ How to pass a matrix to a function?

as the tittle stated, I would like to ask how can you pass a
gsl_vector* e = gsl_matrix* S = gsl_matrix_calloc; ( n, n)

to a function?
Last edited on
Witch function?
1
2
3
4

void foo(double* matrix);
...
foo(S->data);
Last edited on
thank you for your help,
for example after defining my matrix S as nxn matrix
I try to pass it to function update, how can I do that?
1
2
3
4
5
6
7
8
9
#include <stdio.h>
#include <gsl/gsl_vector.h>
#include <gsl/gsl_matrix.h>

void update (double* S ) {....(do something) ...};
int main(){
	gsl_matrix* S = gsl_matrix_calloc ( n, n);
	update (S);
};

Last edited on
I don't understand what you want to achive?
You cant pass gsl_matrix pointer if function requests pointer to a double, instead pass "data" member:
 
upadate(S->data);
Hi, it was just illustrative,
I totally have no idea how to pass the gsl_matrix pointer to a function.
So as you suggest, should it be like this?

1
2
3
4
5
6
7
8
9
10
#include <stdio.h>
#include <gsl/gsl_vector.h>
#include <gsl/gsl_matrix.h>

void update (const gsl_matrix*S) { a = gsl_matrix_get (S, i, j) ..etc.};

int main(){
	gsl_matrix* S = gsl_matrix_calloc ( n, n);
	update (S );
};


Last edited on
I am sorry but it looks like you should take a step back, you should first learn about functions, pointers...and other basic stuff before touching that "advanced" library you got your self into because it will only bring you pain, take your precious time and you'll advance nowhere.
thank you,
I understood the concept of functions and pointers

Just one more question I want to make clear, what type of data will the
gsl_vector* e = gsl_matrix* S = gsl_matrix_calloc( n, n) ;
return to me?

is it a pointer, a class or struct?
Last edited on
If i were you i would not "connect" those "=" operators like that, except if it is for primitive types!
You should look in GSL help documents for reference about gsl_matrix_calloc function and what it does!
http://www.network-theory.co.uk/docs/gslref/Matrixallocation.html


what type of data will the return to me?

It will return pointer to a gsl_matrix witch is a struct.

Edit: Didn't read your line correctly, this should never compile:
gsl_vector* e = gsl_matrix* S = gsl_matrix_calloc( n, n) ;
You can't enter type after = operator!
This is valid:
 
gsl_matrix* S = gsl_matrix_calloc( n, n) ;

for vector you should use something else.
Last edited on
sorry, it was typo :D

so S becomes a pointer to gsl_matrix and so, I should modify my function as
1
2
3
4
5
6
void update (gsl_matrix*S) { a = gsl_matrix_get (S, i, j) ..etc.};

int main(){
	gsl_matrix* S = gsl_matrix_calloc ( n, n);
	update (S);
};


is this correct? Thank you for still being patient with me, I was supposed to understand the GSL code, so I have to fiddle with it
Last edited on
Could you please post somewhat complete code to see exactly what are you trying to do?
Something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
void update (gsl_matrix* S) 
{ 
	for(unsigned int i = 0; i < S->size1; ++i)
	{
		for(unsigned int j = 0; j < S->size2; ++j)
		{
			double a = gsl_matrix_get (S, i, j);
			// do something with a
		}
	}
};

int main()
{
	const unsigned int n = 3;
	gsl_matrix* S = gsl_matrix_calloc(n, n);// allocates 3x3 matrix
	update(S);
	// here do some more stuff

	gsl_matrix_free(S);// free your matrix

	std::cout << "Press enter to exit...";
	std::cin.get();
	return 0;
}
Topic archived. No new replies allowed.