Is there a matrix solver library? lazarus / 21-General ---------------------------------------------------------------------- daringly · 2021-07-05 16:44 I am dealing with a 2000x2000 matrix, and trying to solve these repeatedly and rapidly. Has someone already built a linear algebra library to solve this? ---------------------------------------------------------------------- wp · 2021-07-05 16:48 In numlib (comes with fpc) you can find some routines to solve systems of linear equations: https://wiki.lazarus.freepascal.org/NumLib_Docume ntation#Solving_systems_of_linear_equations_.28unit_sle.29. Don't know if it can handle 2000x2000 matrices, and I don't know about its speed - just try it. Alternatively you can look into - DMath (included in Online-Package-Manager), - LMath (https://sourceforge.net/p/lmath- library/code/HEAD/tree/LMath/), or - mrmath (https://github.com/mikerabat/mrmath). And there are probably some more... ---------------------------------------------------------------------- daringly · 2021-07-05 17:01 Perfect, thanks! ---------------------------------------------------------------------- wp · 2021-07-05 19:05 Took the numlib wiki example and generalized it to solution of a 2000x2000 system. It runs for 9 seconds on my system. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program Project1; uses  sysutils, math, typ, sle, omv; const  n = 2000;  EPS = 1E-9; var  A: array[1..n, 1..n] of ArbFloat;  b: array[1..n] of ArbFloat;  x: array[1..n] of ArbFloat;  b_test: array[1..n] of ArbFloat;  ca: ArbFloat = 0;  i, j: Integer;  term: Integer = 0;  err: Boolean;  t: TDateTime; begin  // Generate random matrix A  for i := 1 to n do    for j := 1 to n do      A[i, j] := Random;   // Generate random vector b  for i := 1 to n do    b[i] := Random;   // Solve  WriteLn('Solving system A x = b for x (with ', n, ' unknowns)...');  t := Now;  slegen(n, n, A[1, 1], b[1], x[1], ca, term);  t := Now - t;   WriteLn('Calculation time: ', FormatDateTime('s.zzz" sec"', t));   if term = 1 then begin    WriteLn('Solution found.');     err := false;  WriteLn('Checking result: A x = (must be equal to b)');     omvmmv(A[1,1], n, n, n, x[1], b_test[1]);     for i := 1 to n do      if not SameValue(b[i], b_test[i], EPS) then      begin        err := true;        WriteLn('Result verification error at index ', i);      end;    if not err then      WriteLn('Correct.');  end  else    WriteLn('Solution could not be calculated');   WriteLn;  WriteLn('Press ENTER to close.');  ReadLn;end. ---------------------------------------------------------------------- marcov · 2021-07-05 19:26 Always leave the random() bits outside of the timing. FPC random is relatively slow. ---------------------------------------------------------------------- wp · 2021-07-05 19:37 Yes, time measurement only includes the slegen instruction. ---------------------------------------------------------------------- Paolo · 2021-07-05 20:31 @daringly, What do you mean "repeatedly" ? ---------------------------------------------------------------------- daringly · 2021-07-05 21:06 It's a sports problem. Each day, new tennis matches are played. I put tennis metrics into an array, and solve for the players' ability with least squares. I must have blow-torched 20 hours trying to do some of this, and everything I need is right there in the library (my past failures were a combination of coding errors and misapplying a rating algorithm). If I test something over 5 years, I'd generate this matrix about 1800 times, solve, and evaluate. And that's each iteration with an idea. ---------------------------------------------------------------------- BobDog · 2021-07-06 01:22 With high school Gaussian method I get about 10 seconds for a 2000 x 2000 matrix. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i    program gauss;   {$MACRO ON}  {$DEFINE delta := abs(rhs[n1]/10000000)}    usesSysUtils,DateUtils; const size=2000;type mat = array of array of double;type vec= array of double;  procedure swap(var a, b: double); inline ;   var temp: double;begin  temp := a; a := b; b := temp;end;  procedure pivot(num:integer;var b:mat;var r:vec;n:integer); inline ; var p1,p2,g:integer; begin  For p1 := num To n - 1  do  begin        For p2  := p1 + 1 To n  do        begin            If (Abs(b[p1,num])< Abs(b[p2,num])) Then            begin                Swap (r[p1],r[p2]);                For g :=1 To n do  Swap(b[p1,g],b[p2,g]);            End;            end;            end; end;  function matmult(m1:mat;m2:vec):vec; var rxc :Double ; r,k:longint; rows:longint=size ; ans:vec; begin setlength(ans,size+1); For r :=1 To rows do begin      rxc:=0;        For k  := 1 To rows do   rxc:=rxc+m1[r,k]*m2[k];      ans[r]:=rxc ;end;exit(ans); end;  procedure GaussJordan(b:mat;r:vec;var ans:vec) ; var n,k,row,g,z,j:longint; f:double; label lbl;begin     n:=size;  For k :=1 To n-1  do    begin         //pivot(k,b,r,n);    // skip the pivot here        For row  :=k To n-1 do        begin            If (b[row+1,k]=0) Then goto lbl;             f:=b[k,k]/b[row+1,k];          r[row+1]:=r[row+1]*f-r[k];            For g :=1 To n do             begin                b[(row+1),g]:=b[(row+1),g]*f-b[k,g];             end;        end;        lbl:    end;    //back substitute    For z :=n downTo 1 do    begin        ans[z]:=r[z]/b[z,z];        For j  := n downTo z+1 do ans[z]:=ans[z]-(b[z,j]*ans[j]/b[z,z]);    end;End; varm:mat;check:vec;rhs:vec;ans:vec;n1,n2:integer;D1,D2: TDateTime;  beginSetLength(m, size+1, size+1);setlength(rhs,size+1);se tlength(ans,size+1);setlength(check,size+1) ;  for n1 :=1 to size  do begin      for n2 :=1 to size do      begin            m[n1,n2]:=random;            end;end;writeln('random matrix made');for n1:=1 to size do rhs[n1]:=random; writeln('random vector made');  writeln('please wait . . ');  D1:=now; GaussJordan(m,rhs,ans); D2:=now ;  check:=matmult(m,ans);   for n1:=1 to size do begin if(abs(check[n1]-rhs[n1])> delta) then writeln('error',check[n1],' ',rhs[n1],',',abs(check[n1]-rhs[n1])); end; writeln('milliseconds for gaussian elimination ', MilliSecondsBetween(D1,D2)); writeln('done, press return . . .');  readln; end. Please note: I have turned pivoting off for speed.