Re: [GiNaC-list] GiNaC::marix::solve(..)
Message: 1 Date: Tue, 02 Jun 2009 18:37:08 +0200 From: Lisa Maletzki <l.maletzki@tu-bs.de> Subject: [GiNaC-list] GiNaC::marix::solve(..) problem To: ginac-list@ginac.de Message-ID: <20090602183708.y2zyinropw4o840k@webmail.tu-bs.de> Content-Type: text/plain; charset=ISO-8859-1; DelSp="Yes"; format="flowed"
Hey,
I have a smallish problem with the method mentioned above (matrix::solve(..)). I have two nxn numeric matrices A and b (from the equation Ax=b) and of course want the matrix x in the end but I keep on getting the error that the first argument must be a matrix of symbols. Now my question how can I do that? I tried it with ex symbolic_matrix(..) but I couldn't get it to work since it is of the type ex and not matrix.
Kind regards,
Lisa
------------------------------
Message: 2 Date: Tue, 02 Jun 2009 19:35:08 +0200 From: Jens Vollinga <jensv@nikhef.nl> Subject: Re: [GiNaC-list] GiNaC::marix::solve(..) problem To: GiNaC discussion list <ginac-list@ginac.de> Message-ID: <4A2562CC.2020405@nikhef.nl> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Hi,
Lisa Maletzki schrieb:
I have a smallish problem with the method mentioned above (matrix::solve(..)). I have two nxn numeric matrices A and b (from the equation Ax=b) and of course want the matrix x in the end but I keep on getting the error that the first argument must be a matrix of symbols. Now my question how can I do that? I tried it with ex symbolic_matrix(..) but I couldn't get it to work since it is of the type ex and not matrix.
take the following example as a guide:
matrix A(2,2); A = 1, 2, 3, 4; symbol x1("x1"), x2("x2"); matrix x(2,1); x = x1, x2; matrix b(2,1); b = 5, 6; cout << A.solve(x, b) << endl;
As the manual warns, you will get an exception thrown if the system is overdetermined. A more robust version is shown in the following example using no matrices:
symbol x1("x1"), x2("x2"); lst equations; equations.append(1*x1 + 2*x2 == 5); equations.append(3*x1 + 4*x2 == 6); cout << lsolve(equations, lst(x1,x2)) << endl;
Regards, Jens
Thank you Jens. But the problem I have is, that I do not know how big my matrix will be. In the smallest case it is a 18x18 matrix, so getting all those symbols would be rather complicated, or am I just thinking that? Kind regards, Lisa
Hi, On Wed, Jun 03, 2009 at 01:17:59PM +0200, Lisa Maletzki wrote:
But the problem I have is, that I do not know how big my matrix will be. In the smallest case it is a 18x18 matrix, so getting all those symbols would be rather complicated, or am I just thinking that?
You might want to use something like this: void generate_symbols(vector<symbol>& vars, const string& pattern) { for (unsigned i = 0; i < vars.size(); ++i) { std::ostringstream ostr; ostr << pattern << i; symbol tmp(ostr.str()); vars[i] = tmp; } } unsigned n = 18; vector<symbol> vars(n); generate_symbols(vars, "x"); // populates vars with symbols "x0", "x1", etc. matrix A(n, n); // your code to fill it in matrix b(n, 1); // your code fill it in matrix x(n, 1); for (unsigned i = 0; i < n; ++i) x(i, 1) = xv[i]; matrix result = A.solve(x, b); Also, be forewarned: using GiNaC for numerical computations (including solving linear systems with coefficients being floating-point numbers) incurs substantial overhead, so you'd better use some linear algebra software, such as GSL, ATLAS, etc. Best regards, Alexei
Zitat von Alexei Sheplyakov <varg@metalica.kh.ua>:
Hi,
On Wed, Jun 03, 2009 at 01:17:59PM +0200, Lisa Maletzki wrote:
But the problem I have is, that I do not know how big my matrix will be. In the smallest case it is a 18x18 matrix, so getting all those symbols would be rather complicated, or am I just thinking that?
You might want to use something like this:
void generate_symbols(vector<symbol>& vars, const string& pattern) { for (unsigned i = 0; i < vars.size(); ++i) { std::ostringstream ostr; ostr << pattern << i; symbol tmp(ostr.str()); vars[i] = tmp; } }
unsigned n = 18; vector<symbol> vars(n); generate_symbols(vars, "x"); // populates vars with symbols "x0", "x1", etc. matrix A(n, n); // your code to fill it in matrix b(n, 1); // your code fill it in
matrix x(n, 1); for (unsigned i = 0; i < n; ++i) x(i, 1) = xv[i];
matrix result = A.solve(x, b);
Also, be forewarned: using GiNaC for numerical computations (including solving linear systems with coefficients being floating-point numbers) incurs substantial overhead, so you'd better use some linear algebra software, such as GSL, ATLAS, etc.
Best regards, Alexei
Thanks again, I think that will do the trick but when compiling it I get a linker error which I cannot solve (I'm fairly new to c++) I can't say if it is just missing a library of if I wrote something in the code wrong. This is the output: g++ -Xlinker `pkg-config --cflags --libs ginac` -o"sep" ./src/bezierPoints.o ./src/testmain.o ./src/xml.o ./src/xmlParser.o ./src/bezierPoints.o: In function `bezierPoints::solve(GiNaC::matrix, int)': /my_folder/Debug/../src/bezierPoints.cpp:63: undefined reference to `bezierPoints::generate_symbols(std::vector<GiNaC::symbol, std::allocator<GiNaC::symbol> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)' The method is declared in the header file and called with the write name in the source file, so typing error can be ruled out. Any help? Kind regards, Lisa
Hi! Lisa Maletzki wrote:
Thanks again, I think that will do the trick but when compiling it I get a linker error which I cannot solve (I'm fairly new to c++) I can't say if it is just missing a library of if I wrote something in the code wrong.
This is the output:
g++ -Xlinker `pkg-config --cflags --libs ginac` -o"sep" ./src/bezierPoints.o ./src/testmain.o ./src/xml.o ./src/xmlParser.o ./src/bezierPoints.o: In function `bezierPoints::solve(GiNaC::matrix, int)': /my_folder/Debug/../src/bezierPoints.cpp:63: undefined reference to `bezierPoints::generate_symbols(std::vector<GiNaC::symbol, std::allocator<GiNaC::symbol> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
The method is declared in the header file and called with the write name in the source file, so typing error can be ruled out. Any help?
Apparently, you've declared the function generate_symbols inside namespace bezierPoints (since that's where the linker is looking for it). Did you also implement it inside namespace bezierPoints? Bye -richy. -- Richard B. Kreckel <http://www.ginac.de/~kreckel/>
Zitat von "Richard B. Kreckel" <kreckel@ginac.de>:
Hi!
Lisa Maletzki wrote:
Thanks again, I think that will do the trick but when compiling it I get a linker error which I cannot solve (I'm fairly new to c++) I can't say if it is just missing a library of if I wrote something in the code wrong.
This is the output:
g++ -Xlinker `pkg-config --cflags --libs ginac` -o"sep" ./src/bezierPoints.o ./src/testmain.o ./src/xml.o ./src/xmlParser.o ./src/bezierPoints.o: In function `bezierPoints::solve(GiNaC::matrix, int)': /my_folder/Debug/../src/bezierPoints.cpp:63: undefined reference to `bezierPoints::generate_symbols(std::vector<GiNaC::symbol, std::allocator<GiNaC::symbol> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
The method is declared in the header file and called with the write name in the source file, so typing error can be ruled out. Any help?
Apparently, you've declared the function generate_symbols inside namespace bezierPoints (since that's where the linker is looking for it). Did you also implement it inside namespace bezierPoints?
Bye -richy. -- Richard B. Kreckel <http://www.ginac.de/~kreckel/> _______________________________________________ GiNaC-list mailing list GiNaC-list@ginac.de https://www.cebix.net/mailman/listinfo/ginac-list
Thanks a bunch for the help a clearly stupid mistake :) Kind regards, Lisa
Zitat von Lisa Maletzki <l.maletzki@tu-bs.de>:
Zitat von "Richard B. Kreckel" <kreckel@ginac.de>:
Hi!
Lisa Maletzki wrote:
Thanks again, I think that will do the trick but when compiling it I get a linker error which I cannot solve (I'm fairly new to c++) I can't say if it is just missing a library of if I wrote something in the code wrong.
This is the output:
g++ -Xlinker `pkg-config --cflags --libs ginac` -o"sep" ./src/bezierPoints.o ./src/testmain.o ./src/xml.o ./src/xmlParser.o ./src/bezierPoints.o: In function `bezierPoints::solve(GiNaC::matrix, int)': /my_folder/Debug/../src/bezierPoints.cpp:63: undefined reference to `bezierPoints::generate_symbols(std::vector<GiNaC::symbol, std::allocator<GiNaC::symbol> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
The method is declared in the header file and called with the write name in the source file, so typing error can be ruled out. Any help?
Apparently, you've declared the function generate_symbols inside namespace bezierPoints (since that's where the linker is looking for it). Did you also implement it inside namespace bezierPoints?
Bye -richy. -- Richard B. Kreckel <http://www.ginac.de/~kreckel/> _______________________________________________ GiNaC-list mailing list GiNaC-list@ginac.de https://www.cebix.net/mailman/listinfo/ginac-list
Thanks a bunch for the help a clearly stupid mistake :)
Kind regards,
Lisa
That was a little overhasty I suppose because I came across another problem. The following snippet of code will give me a matrix::operator(): index out of range. unsigned size = 18; matrix vectorX(size, 1); for (unsigned i = 0; i < size; i++) { vectorX(i, 1) = vars[i]; } when making the matrix of size (size, size) it works but I have not the matrix I need. sub_matrix could correct that but unfortunately it will just end up in a matrix inside a matrix when doing something like this: matrix tmp(size,1); tmp = sub_matrix(vectorX,0,size,1,1); Any idea how to reduce a matrix and get a matrix back or how to work the first one properly? Kind regards, Lisa
Hi! Lisa Maletzki wrote:
That was a little overhasty I suppose because I came across another problem. The following snippet of code will give me a matrix::operator(): index out of range.
unsigned size = 18; matrix vectorX(size, 1); for (unsigned i = 0; i < size; i++) { vectorX(i, 1) = vars[i]; }
Well, vectorX(i, 1) refers to the second column. GiNaC follows C/C++ index notation throughout. -richy. -- Richard B. Kreckel <http://www.ginac.de/~kreckel/>
participants (3)
-
Alexei Sheplyakov
-
Lisa Maletzki
-
Richard B. Kreckel