Matrix inversion algorithm examples

Discussion in 'Java' started by Robert M. Gary, Feb 10, 2006.

  1. Does anyone have any code examples of doing a Matrix inversion that I
    could look at? I'm trying to get a sense of strategies to go about it.
    I understand inversions from a pure mathematical/algebra point of view
    but it seems like there could be different ways of going about
    implmenting it in code. I"m working on a website to do financial
    portfolio modeling and a lot of the algorithms are matrix based. I
    understand the graphics guys use matrices a lot too.

    -Robert
     
    Robert M. Gary, Feb 10, 2006
    #1
    1. Advertising

  2. Robert M. Gary

    Chris Smith Guest

    Robert M. Gary <> wrote:
    > Does anyone have any code examples of doing a Matrix inversion that I
    > could look at? I'm trying to get a sense of strategies to go about it.
    > I understand inversions from a pure mathematical/algebra point of view
    > but it seems like there could be different ways of going about
    > implmenting it in code. I"m working on a website to do financial
    > portfolio modeling and a lot of the algorithms are matrix based. I
    > understand the graphics guys use matrices a lot too.


    First of all, there are several ways to represent matrices. If your
    matrices are "dense" (that is, most cells contain non-zero values), then
    you just store all the values. If your matrices are "sparse" (that is,
    most cells are zero... or some other known value), then you get far
    better performance from a sparse matrix structure that just stores the
    non-zero values. This makes huge differences in the code to perform
    operations.

    Beyond that, I'll let someone else respond who has the time to write a
    better answer.

    --
    www.designacourse.com
    The Easiest Way To Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, Feb 10, 2006
    #2
    1. Advertising

  3. Robert M. Gary

    Chris Lamb Guest

    On 10 Feb 2006 08:52:03 -0800, Robert M. Gary wrote:

    > Does anyone have any code examples of doing a Matrix inversion that I
    > could look at? I'm trying to get a sense of strategies to go about it.
    > I understand inversions from a pure mathematical/algebra point of view
    > but it seems like there could be different ways of going about
    > implmenting it in code. I"m working on a website to do financial
    > portfolio modeling and a lot of the algorithms are matrix based. I
    > understand the graphics guys use matrices a lot too.
    >
    > -Robert


    An example using Gaussian Elimination:

    Jama - http://math.nist.gov/javanumerics/jama/

    Most times, if you are inverting a matrix, you should be inverting
    something small so sparse/dense representation shouldn't matter. If
    you are inverting a matrix so large that sparse representation
    improves algorthm performance, you are doing the wrong thing. Almost
    always if you are inverting say a 1,000x1,000 matrix you havn't
    understood the problem. Also, forming the direct inverse of a large
    matrix will buld up errors such that your answer is near meaningless.

    HTH

    Chris
     
    Chris Lamb, Feb 10, 2006
    #3
  4. Robert M. Gary

    Chris Uppal Guest

    Chris Uppal, Feb 10, 2006
    #4
  5. Chris Lamb wrote:
    > On 10 Feb 2006 08:52:03 -0800, Robert M. Gary wrote:
    >
    >
    >>Does anyone have any code examples of doing a Matrix inversion that I
    >>could look at? I'm trying to get a sense of strategies to go about it.
    >>I understand inversions from a pure mathematical/algebra point of view
    >>but it seems like there could be different ways of going about
    >>implmenting it in code. I"m working on a website to do financial
    >>portfolio modeling and a lot of the algorithms are matrix based. I
    >>understand the graphics guys use matrices a lot too.
    >>
    >>-Robert

    >
    >
    > An example using Gaussian Elimination:
    >
    > Jama - http://math.nist.gov/javanumerics/jama/
    >
    > Most times, if you are inverting a matrix, you should be inverting
    > something small so sparse/dense representation shouldn't matter. If
    > you are inverting a matrix so large that sparse representation
    > improves algorthm performance, you are doing the wrong thing. Almost
    > always if you are inverting say a 1,000x1,000 matrix you havn't
    > understood the problem. Also, forming the direct inverse of a large
    > matrix will buld up errors such that your answer is near meaningless.
    >
    > HTH
    >
    > Chris


    Are you saying that you should never solve large systems of linear
    equations, or just that it is a mistake to do it by directly inverting
    the matrix rather than e.g. by LU factorization?

    Patricia
     
    Patricia Shanahan, Feb 10, 2006
    #5
  6. Robert M. Gary

    Chris Lamb Guest

    On Fri, 10 Feb 2006 20:50:02 GMT, Patricia Shanahan wrote:

    > Chris Lamb wrote:
    >> On 10 Feb 2006 08:52:03 -0800, Robert M. Gary wrote:
    >>
    >>
    >>>Does anyone have any code examples of doing a Matrix inversion that I
    >>>could look at? I'm trying to get a sense of strategies to go about it.
    >>>I understand inversions from a pure mathematical/algebra point of view
    >>>but it seems like there could be different ways of going about
    >>>implmenting it in code. I"m working on a website to do financial
    >>>portfolio modeling and a lot of the algorithms are matrix based. I
    >>>understand the graphics guys use matrices a lot too.
    >>>
    >>>-Robert

    >>
    >>
    >> An example using Gaussian Elimination:
    >>
    >> Jama - http://math.nist.gov/javanumerics/jama/
    >>
    >> Most times, if you are inverting a matrix, you should be inverting
    >> something small so sparse/dense representation shouldn't matter. If
    >> you are inverting a matrix so large that sparse representation
    >> improves algorthm performance, you are doing the wrong thing. Almost
    >> always if you are inverting say a 1,000x1,000 matrix you havn't
    >> understood the problem. Also, forming the direct inverse of a large
    >> matrix will buld up errors such that your answer is near meaningless.
    >>
    >> HTH
    >>
    >> Chris

    >
    > Are you saying that you should never solve large systems of linear
    > equations, or just that it is a mistake to do it by directly inverting
    > the matrix rather than e.g. by LU factorization?


    I hope we do need to solve large systems of equations. After all, my PhD
    was entirely based on that! Your second statement is right, but I prefer
    (well, they perform best in my field) Conjugate Gradients and related
    methods.
    Yes, it is almost invariably a mistake to do it by direct inversion.

    Chris
     
    Chris Lamb, Feb 10, 2006
    #6
  7. Robert M. Gary

    Roedy Green Guest

    On 10 Feb 2006 08:52:03 -0800, "Robert M. Gary" <>
    wrote, quoted or indirectly quoted someone who said :

    >Matrix inversion


    nearly always you just grab a matrix inversion routine from a package.
    Anything you write will almost inevitably be slower or blow up more
    often. It takes quite a bit of cleverness to avoid error propagation.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Feb 10, 2006
    #7
  8. I may end up just using a package, however I would like to also see an
    algorithm just to understand how its working.

    -Robert
     
    Robert M. Gary, Feb 10, 2006
    #8
  9. > your matrices are "dense" (that is, most cells contain non-zero values), then you just store all the values.

    Sorry, mine are dense. Mostly they are matrices of asset price
    variance.

    -Robert
     
    Robert M. Gary, Feb 10, 2006
    #9
  10. "Robert M. Gary" <> wrote in message
    news:...
    > Does anyone have any code examples of doing a Matrix inversion that I
    > could look at? I'm trying to get a sense of strategies to go about it.
    > I understand inversions from a pure mathematical/algebra point of view
    > but it seems like there could be different ways of going about
    > implmenting it in code. I"m working on a website to do financial
    > portfolio modeling and a lot of the algorithms are matrix based. I
    > understand the graphics guys use matrices a lot too.


    Never explicitly inverse a matrix; both for performance and numerical
    stability reasons. You only need the inverse of a matrix conceptually,
    i.e. Ax = b --> x = A^-1 b. For such purposes google for 'LUP decomposition'
    of a matrix. It's faster and introduces far less numerical instability.

    kind regards,

    Jos
     
    Jos A. Horsmeier, Feb 10, 2006
    #10
  11. Robert M. Gary

    Jacob Guest

    Robert M. Gary wrote:

    > Does anyone have any code examples of doing a Matrix inversion that I
    > could look at?


    This class inverts (check the invert() method) a 4x4 matrix
    by the use of cofactors:

    http://geosoft.no/software/matrix4x4/Matrix4x4.java.html

    This is probably the fastest way to invert this type of
    matrix, but might not be generalized to higher dimensions.

    > I understand the graphics guys use matrices a lot too.


    Yes, but we normally stop at 4x4...
     
    Jacob, Feb 11, 2006
    #11
  12. Robert M. Gary

    ersid

    Joined:
    Oct 29, 2008
    Messages:
    1
    Hi To Everyone

    I Am New In Algorithm And Flowcharting Process
    I Would Be Glad If Anyone Knows How To Make An Algorithm Or Flowchart In Order To Add 2 Matrices Together.
    Thank U
     
    ersid, Oct 29, 2008
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jan-Hendrik Huehne

    matrix inversion

    Jan-Hendrik Huehne, Feb 7, 2004, in forum: C++
    Replies:
    2
    Views:
    650
    Victor Bazarov
    Feb 7, 2004
  2. Rafal 'Raf256' Maj

    [ot?] matrix inversion

    Rafal 'Raf256' Maj, Jan 14, 2004, in forum: C Programming
    Replies:
    87
    Views:
    1,893
    Dan Pop
    Jan 19, 2004
  3. lancered
    Replies:
    3
    Views:
    372
    Lou Pecora
    Apr 4, 2007
  4. lancered
    Replies:
    6
    Views:
    770
    Travis Oliphant
    Apr 4, 2007
  5. Sprechen sie von C++

    Matrix inversion

    Sprechen sie von C++, Jun 19, 2010, in forum: C++
    Replies:
    14
    Views:
    854
    Francesco S. Carta
    Jun 20, 2010
Loading...

Share This Page