# forum.alglib.net

ALGLIB forum
 It is currently Sun Aug 11, 2024 8:18 am

 All times are UTC

### Forum rules

1. This forum can be used for discussion of both ALGLIB-related and general numerical analysis questions
2. This forum is English-only - postings in other languages will be removed.

 Page 1 of 1 [ 5 posts ]
 Print view Previous topic | Next topic
Author Message
 Post subject: rmatrixsolve vs. rmatrixsolvelsPosted: Mon Mar 14, 2011 10:12 pm

Joined: Mon Mar 14, 2011 10:03 pm
Posts: 1
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

Top

 Post subject: Re: rmatrixsolve vs. rmatrixsolvelsPosted: Tue Mar 15, 2011 7:19 am

Joined: Fri May 07, 2010 7:06 am
Posts: 922
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).

Top

 Post subject: Re: rmatrixsolve vs. rmatrixsolvelsPosted: Wed Mar 16, 2011 1:20 pm

Joined: Wed Mar 16, 2011 1:11 pm
Posts: 5
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

Top

 Post subject: Re: rmatrixsolve vs. rmatrixsolvelsPosted: Fri Mar 18, 2011 6:53 am

Joined: Fri May 07, 2010 7:06 am
Posts: 922
It is because your system is rank deficient - smallest singular value is zero. So you can't satisfy all equations simultaneously.

Top

 Post subject: Re: rmatrixsolve vs. rmatrixsolvelsPosted: Wed Mar 23, 2011 9:37 am

Joined: Wed Mar 16, 2011 1:11 pm
Posts: 5
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.

Top

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 5 posts ]

 All times are UTC

#### Who is online

Users browsing this forum: Bing [Bot] and 63 guests

 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum

Search for: