forum.alglib.net
http://forum.alglib.net/

Solving sparse NxM linear eqs
http://forum.alglib.net/viewtopic.php?f=2&t=1601
Page 1 of 1

Author:  sunaj [ Thu Mar 20, 2014 8:25 am ]
Post subject:  Solving sparse NxM linear eqs

Hi,

I have a non square system of linear equations and I'm currently using rmatrixsolvels to solve it. This works fine.
But my system is very large and computationally expensive to solve. It is also a sparse system (it has relatively few values outside the diagonal).
Can I solve the system more efficiently using sparse matrix functions (http://www.alglib.net/matrixops/sparse.php) and if so - how?

Any help will be much appreciated:)

Author:  sunaj [ Mon Mar 24, 2014 1:06 pm ]
Post subject:  Re: Solving sparse NxM linear eqs

I found a solution using http://www.stanford.edu/group/SOL/software/lsqr/ - much faster and much more memory efficient.

Author:  Sergey.Bochkanov [ Tue Mar 25, 2014 8:48 am ]
Post subject:  Re: Solving sparse NxM linear eqs

Sorry for replying too late, but ALGLIB has LSQR solver :)

Author:  sunaj [ Mon Apr 07, 2014 1:17 pm ]
Post subject:  Re: Solving sparse NxM linear eqs

Hi again,

I've had a second look at the alglib LSQR solver (because the other leaks memory and I can't figure out why). But when I use linlsqrsolvesparse, it does not give the same results as when I use rmatrixsolvels ( the other LSQR sovler gives exactly the same as rmatrixsolvels). Am I doing something wrong (sorry for my messy test code)?

Thanks in advance!
Code:
   string tmpstr = JFile::readFile("..\\testLinAlg\\vb.csv");
   alglib::real_2d_array mac;
   int n,m;
   double *b = JStr::toNumArr<double>(tmpstr,n,newline);
   double **a = new double*[n];
   vector<string> al = JFile::readFileLines("..\\testLinAlg\\ma.csv",true);
   if (al.size()!=n) throw JException("Files do not match.");
   for (int r=0;r<n;r++) {
      al[r] = al[r].substr(0,al[r].length()-2);
      a[r] = JStr::toNumArr<double>(al[r],m,",");
   }

   //4 check
   double *d = new double[n*m];
   memset(d,0,n*m*sizeof(double));
   mac.setcontent(n,m,d);

   //pop data
   sparsematrix ma;
    sparsecreate(n, m, ma);
   for (int r=0;r<n;r++){
      for (int c=0;c<m;c++){
         if (a[r][c]!=0) {
            mac[r][c] = a[r][c];
            sparseset(ma, r, c, a[r][c]);
         }
      }
   }
   sparseconverttocrs(ma);
   real_1d_array rhs;
   //rhs.setlength(n);
   //for (int r=0;r<n;r++)rhs[r] = b[r];
   rhs.setcontent(n,b);

   //check with full solver
   alglib::real_1d_array check;
   alglib::ae_int_t      info;
   alglib::densesolverlsreport repc;
   alglib::rmatrixsolvels(mac,n,m,rhs,0.0,info,repc,check);
   cout << "velAdj:" << endl; showMatrix(check,10); //last value: 0.000263801


   //solve
    linlsqrstate s;
    linlsqrreport rep;
    real_1d_array x;
    linlsqrcreate(n, m, s);
   linlsqrsetcond(s, 1.0e-10,1.0e-10,n+m+50);
    linlsqrsolvesparse(s, ma, rhs);
    linlsqrresults(s, x, rep);

   printf("%d\n", int(rep.terminationtype)); // EXPECTED: 4
   //printf("%s\n", x.tostring(6).c_str());
   double *res = x.getcontent();
    cout << JArray::join<double>(res,min(m,10),newline) << endl; //last value: 0.000123301


Attachments:
vb.csv [9.8 KiB]
Downloaded 640 times
ma.csv [822.3 KiB]
Downloaded 617 times

Page 1 of 1 All times are UTC
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/