Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I need to do some maths, so it would be great if you could point me to some good cpan modules. I need: Matrix / vector multiplication to find a matrix which has determinant 1 and solves some linear equations e.g. Mx+b = h My+b = i Mz+b = j with M being the matrix, the rest vectors. Can you tell me of a good (and fast!) module that does this? I will have to solve a LOT of those so speed is an issue. Thank you very much :)

Replies are listed 'Best First'.
Re: solving linear equations
by dec (Beadle) on Aug 31, 2009 at 12:58 UTC
    I haven't used PDL, but Math::Matrix might suit your needs. You mention that speed is important, so a couple of caveats: it's written purely in Perl (no optimised C code; I don't know if PDL or any other matrix module uses optimised C code), and the equation solver uses the Gauss Jordan elimination algorithm which has a run time of O(n^3). This last point is probably only important if you're using very large arrays... you didn't mention this, though, so I'm guessing you're using only relatively small arrays with fewer than (say) 30 unknowns. If that's the case, the Gauss-Jordan will most likely be fast enough.

    As for your example of the sorts of systems you want to solve, I take it that each equation you gave represents a complete set of simultaneous equations? It wasn't clear, but I wondered whether you meant that all three matrix equations needed to be solved together rather than individually. If that is the case, can you rewrite the equations to involve only one matrix (the co-efficients) times a vector (of unknowns) = a vector (the result)?

Re: solving linear equations
by ikegami (Patriarch) on Aug 31, 2009 at 04:14 UTC
Re: solving linear equations
by ambrus (Abbot) on Sep 01, 2009 at 10:42 UTC

    Try something like PDL or Math::GSL::BLAS etc or write your own module that suits you well.

      I am currently looking at Math::Matrix, but might end up using PDL.

      @dec: yes, all equations are a set of linear equations and yes, they need to be solved simultaneously. Rewriting is not an option, though...

      Math::Matrix looks good but I will have to make hundreds or maybe even thousands of runs and am not quite sure yet whether perl would be fast enough for that.

      I think I might just try math::matrix first as it seems easier to use quickly and then see if I need more speed ;)

      @ambrus: yes, of course that would be an option but I am not that good at numerical problem solving, so I am glad to use code already written and probably optimized to a point where I might not get...

      @all: sry for horrible format and thx for all your answers!

Re: solving linear equations
by etj (Priest) on May 31, 2022 at 22:04 UTC
    PDL was already mentioned, but I don't know of any better starting point for this linear-algebra type problem than PDL::LinearAlgebra, probably one of the msolve* routines. If anyone does know of such, I'd love to know of it!