Trouble with Matrix::inverse

Ask a Question related to Ruby, Design and Development.

  1. #1

    Default Trouble with Matrix::inverse

    I decided to use Ruby to write a simple program to help my daughter
    with her math homework, which involves solving systems of linear
    equations using matrices. It's been a long time since I learned all
    this stuff, but I think I still remember the basics of it. But I keep
    getting answers that don't make sense to me when I use the
    Matrix::inverse function. Can somebody help me out?

    Here's a simple program:
    #!/usr/bin/env ruby
    require 'matrix'
    c=Matrix[[1,2],[2,1]]
    d=c.inverse
    p1=c*d
    puts "c is " +c.to_s
    puts "d is " +d.to_s
    puts "product is "+p1.to_s


    And here are the results:
    c is Matrix[[1, 2], [2, 1]]
    d is Matrix[[-1, 1], [0, -1]]
    product is Matrix[[-1, -1], [-2, 1]]

    The problem is that any matrix multiplied by its inverse is supposed
    to yield the appropriate identity matrix. In this case, the product
    is definitely NOT the identity matrix.

    I get the same results under Ruby 1.6.7, 1.6.8, and 1.8.0.

    I humbly request that somebody illuminate me as to the error(s) of my
    ways.

    Thanks!

    Ray
    rbovet Guest

  2. Similar Questions and Discussions

    1. inverse of distribute into layers
      hi everybody.i am trying to edit a flash template with flash cs4.the problem is that there are some pictures in there that are hidden ( dont ask how...
    2. Inverse of model(s)UnderLoc?
      I'd really like a function that returns the point in the image plane to which a point on a 3D object will be projected. For instance, given a point...
    3. Is there an inverse of ParagraphFormat
      Hi, I'm trying to build a forum and want to allow users to edit their message boxes (textarea boxes). I use the ParagraphFormat whenever I...
    4. Math::MatrixReal->{inverse,det}() complexity?
      Hi folks, Does anyone know anything about the complexity of the methods ->inverse() and ->det() available in Math::MatrixReal? I ran a...
    5. Camera control become inverse :(
      Hi there! I would like to use camera motion to interact with my 3D model instead of using drag Model fonction. My script work just fine but it...
  3. #2

    Default Re: Trouble with Matrix::inverse

    >>>>> "r" == rbovet <rbovet@digitalglobe.com> writes:

    r> #!/usr/bin/env ruby
    r> require 'matrix'

    add this line

    require 'mathn'

    r> c=Matrix[[1,2],[2,1]]

    svg% cat b.rb
    #!/usr/bin/ruby
    require 'matrix'
    require 'mathn'

    c=Matrix[[1,2],[2,1]]
    d=c.inverse
    p1=c*d
    puts "c is " +c.to_s
    puts "d is " +d.to_s
    puts "product is "+p1.to_s
    svg%

    svg% b.rb
    c is Matrix[[1, 2], [2, 1]]
    d is Matrix[[-1/3, 2/3], [2/3, -1/3]]
    product is Matrix[[1, 0], [0, 1]]
    svg%


    --

    Guy Decoux
    ts Guest

  4. #3

    Default Re: Trouble with Matrix::inverse

    rbovet wrote:
    > I humbly request that somebody illuminate me as to the error(s) of my
    > ways.
    Umm, me too. I just worked it out by hand and [[-1, 1], [0, -1]] is not
    the inverse of the original matrix. The inverse of [[1, 2], [2, 1]]
    should be [[-1/3, 2/3], [2/3, -1/3]].

    Lyle Johnson Guest

  5. #4

    Default Re: Trouble with Matrix::inverse

    ts wrote:
    > add this line
    >
    > require 'mathn'
    <snip>

    Thanks for the reminder, Guy. It was using integer math since the
    elements of the matrix are integers.

    Ray, as an alternative to Guy's suggestion, you could also do this:

    c = Matrix[ [1.0, 2.0], [2.0, 1.0]]
    d = c.inverse
    p1 = c*d

    and get the expected results (using floating point arithmetic instead).

    Lyle Johnson Guest

  6. #5

    Default Re: Trouble with Matrix::inverse

    Lyle Johnson <lyle@users.sourceforge.net> wrote in message news:<3F9EAFA1.8020705@users.sourceforge.net>...
    > ts wrote:
    >
    > > add this line
    > >
    > > require 'mathn'
    >
    > <snip>
    >
    > Thanks for the reminder, Guy. It was using integer math since the
    > elements of the matrix are integers.
    >
    > Ray, as an alternative to Guy's suggestion, you could also do this:
    >
    > c = Matrix[ [1.0, 2.0], [2.0, 1.0]]
    > d = c.inverse
    > p1 = c*d
    >
    > and get the expected results (using floating point arithmetic instead).

    Lyle and Guy,

    Thank you both for your rapid responses! It's a delight to get such
    fast and informative replies to a question. Putting the values into a
    floating point format made clear to me what the problem was, but the
    real gem was to find out about the mathn class, which is really cool!
    It makes it much more "human-like" in its mathematical prowess.

    Ray
    rbovet Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139