forum.alglib.net http://forum.alglib.net/ |
|
rmatrixsolve vs. rmatrixsolvels http://forum.alglib.net/viewtopic.php?f=2&t=319 |
Page 1 of 1 |
Author: | ciwstevie [ Mon Mar 14, 2011 10:12 pm ] |
Post subject: | rmatrixsolve vs. rmatrixsolvels |
Hi, Maybe this is a stupid newbie question but I am not sure whether I have understood the difference between the two functions rmatrixsolve and rmatrixsolvels The problem is rather that I am not sure which function to use for my problem: I have a system of linear equations that may not always have an exact solution (I am having a square matrix A). Therefore I want to solve the system in a least-square sense. Looking at the documentation of "rmatrixsolve" I am not sure whether this is the function I should be using. Is "rmatrixsolvels" the way to go? In addition it would be nice to set constraints on my solution vector (to e.g. limit the solution to non-negative values). Is this possible with any of the linear system solvers or do I have to go for an optimisation routine then? Any proposal which one I should choose in case? Thanks a lot in advance for some guidance ! ciwstevie |
Author: | Sergey.Bochkanov [ Tue Mar 15, 2011 7:19 am ] |
Post subject: | Re: rmatrixsolve vs. rmatrixsolvels |
You should use rmatrixsolvels() - it can solve degenerate (and even non-square) systems. As for nonnegativity, there is no linear solver with such constraints, so you should use nonlinear optimizer. MinBLEIC optimizer from Optimization package should work (especially with diagonal preconditioner, which is easy to calculate in your case). |
Author: | sunaj [ Wed Mar 16, 2011 1:20 pm ] |
Post subject: | Re: rmatrixsolve vs. rmatrixsolvels |
Hi, I've tried using rmatrixsolvels on my system of linear equations, but I do not understand the results. E.g. Equation 0 states that rx[0] + rx[1] = 1, but in the results rx[0]=0.385714 and rx[1]=0.414286 (which clearly is !=1) Help is much appreciated. Sunaj Code: Code: void testSolveLinEq() { alglib::real_2d_array m; alglib::real_1d_array r; int nx = 11; int ny = 10; m.setlength(ny,nx); for (int x=0;x<nx;x++) for (int y=0;y<ny;y++) m[y][x] = 0; m[0][0] = 1; m[0][1] = 1; m[1][1] = -1; m[1][3] = 1; m[2][0] = -1; m[2][2] = 1; m[3][3] = -1; m[3][6] = 1; m[4][5] = 1; m[4][6] = -1; m[4][8] = 1; m[5][2] = -1; m[5][4] = 1; m[5][5] = -1; m[6][4] = -1; m[6][7] = 1; m[7][8] = -1; m[7][10] = 1; m[8][9] = 1; m[8][10] = -1; m[9][7] = -1; m[9][9] = -1; r.setlength(ny); r[0] = 1; r[1] = 0; r[2] = 0; r[3] = 0; r[4] = 0; r[5] = 0; r[6] = 0; r[7] = 0; r[8] = 0.5; r[9] = 0.5; alglib::real_1d_array rx; alglib::ae_int_t info; alglib::densesolverlsreport rep; alglib::rmatrixsolvels(m,ny,nx,r,0.0,info,rep,rx); for (int x=0;x<nx;x++) cout << "rx[" << x << "]=" << rx[x] << endl; } Results: rx[0]=0.385714 rx[1]=0.414286 rx[2]=0.185714 rx[3]=0.214286 rx[4]=-0.0857143 rx[5]=-0.0714286 rx[6]=0.0142857 rx[7]=-0.285714 rx[8]=-0.114286 rx[9]=-0.0142857 rx[10]=-0.314286 |
Author: | Sergey.Bochkanov [ Fri Mar 18, 2011 6:53 am ] |
Post subject: | Re: rmatrixsolve vs. rmatrixsolvels |
It is because your system is rank deficient - smallest singular value is zero. So you can't satisfy all equations simultaneously. |
Author: | sunaj [ Wed Mar 23, 2011 9:37 am ] |
Post subject: | Re: rmatrixsolve vs. rmatrixsolvels |
Ah yes. I've screwed up the sign on my boundary, it should have been: Code: r.setlength(ny); r[0] = 1; r[1] = 0; r[2] = 0; r[3] = 0; r[4] = 0; r[5] = 0; r[6] = 0; r[7] = 0; r[8] = -0.5; r[9] = -0.5; Thank you. |
Page 1 of 1 | All times are UTC |
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group http://www.phpbb.com/ |