Matrix

Discussion in 'Ruby' started by v.srikrishnan@gmail.com, Sep 10, 2006.

  1. Guest

    Hi all,
    a) Is there a way to create a non-square matrix in ruby?
    b) How does one set any arbitrary element of an array?

    I searched for these bu there was a post about 3 years back, if
    anything has been done it would be better to use that rather than
    something i should write!
    Thanks
    krishnan
     
    , Sep 10, 2006
    #1
    1. Advertising

  2. Guest

    Hi --

    On Mon, 11 Sep 2006, wrote:

    > Hi all,
    > a) Is there a way to create a non-square matrix in ruby?
    > b) How does one set any arbitrary element of an array?
    >
    > I searched for these bu there was a post about 3 years back, if
    > anything has been done it would be better to use that rather than
    > something i should write!


    Check out the Matrix class -- look for matrix.rb in the Ruby
    distribution.


    David

    --
    David A. Black |
    Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]
    DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]
    [1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com
    [2] http://dablog.rubypal.com | [4] http://www.rubycentral.org
     
    , Sep 10, 2006
    #2
    1. Advertising

  3. wrote:
    > Hi --
    >
    > On Mon, 11 Sep 2006, wrote:
    >
    >> Hi all,
    >> a) Is there a way to create a non-square matrix in ruby?
    >> b) How does one set any arbitrary element of an array?
    >>
    >> I searched for these bu there was a post about 3 years back, if
    >> anything has been done it would be better to use that rather than
    >> something i should write!

    >
    > Check out the Matrix class -- look for matrix.rb in the Ruby
    > distribution.
    >
    >
    > David
    >

    You can only set an arbitrary element of a Matrix when it is created.
    Once created, you can't change any of the entries.
     
    M. Edward (Ed) Borasky, Sep 11, 2006
    #3
  4. "M. Edward (Ed) Borasky" <> wrote in message
    news:...
    > wrote:
    >> Hi --
    >>
    >> On Mon, 11 Sep 2006, wrote:
    >>
    >>> Hi all,
    >>> a) Is there a way to create a non-square matrix in ruby?
    >>> b) How does one set any arbitrary element of an array?
    >>>
    >>> I searched for these bu there was a post about 3 years back, if
    >>> anything has been done it would be better to use that rather than
    >>> something i should write!

    >>
    >> Check out the Matrix class -- look for matrix.rb in the Ruby
    >> distribution.
    >>

    > You can only set an arbitrary element of a Matrix when it is created.
    > Once created, you can't change any of the entries.


    The []= operator does seem conspicuously missing. If anyone knows why
    this was (obviously) deliberately left out, please say something!

    Of course, if this is useful to you, you can always add it, yourself!
    I think it would go something like this...


    require 'matrix'

    class Matrix # are for kids!
    def []= (i, j, v)
    @rows[j] = v
    end
    end
     
    Just Another Victim of the Ambient Morality, Sep 11, 2006
    #4
  5. Dave Burt Guest

    Just Another Victim of the Ambient Morality wrote:
    > "M. Edward (Ed) Borasky" <> wrote
    >> You can only set an arbitrary element of a Matrix when it is created.
    >> Once created, you can't change any of the entries.

    >
    > The []= operator does seem conspicuously missing. If anyone knows why
    > this was (obviously) deliberately left out, please say something!


    It's by design. A Matrix is immutable, like Ruby's other numbers
    (Fixnum, Rational, Complex...)

    Consider subclassing rather than modifying the base class for this reason.

    class MutableMatrix < Matrix
    def []= ...

    Cheers,
    Dave
     
    Dave Burt, Sep 11, 2006
    #5
  6. MonkeeSage Guest

    Dave Burt wrote:
    > It's by design. A Matrix is immutable, like Ruby's other numbers
    > (Fixnum, Rational, Complex...)


    Why should a Matrix be immutable? It's not a type like a Fixnum, it's a
    data structure, like an Array or Hash. What is gained by creating a new
    Matrix from a changing data stream, rather than updating an existing
    one?

    Regards,
    Jordan
     
    MonkeeSage, Sep 11, 2006
    #6
  7. Paul Lutus wrote:
    > The point I am making is that the matrix
    > can't
    > function if it is thought of as nine scalar values rather than a 3x3
    > square
    > matrix.


    I think the point they are making is that not everyone uses matrixes for
    the same thing. I agree that for most applications, they should be
    treated as immutable. But they are saying that they not only think they
    should be modifyable, but that they need them to be modifyable to be
    useful to them under their circumstances.

    --
    Posted via http://www.ruby-forum.com/.
     
    William Crawford, Sep 11, 2006
    #7
  8. Paul Lutus wrote:
    > MonkeeSage wrote:
    >
    >> Dave Burt wrote:
    >>> It's by design. A Matrix is immutable, like Ruby's other numbers
    >>> (Fixnum, Rational, Complex...)

    >> Why should a Matrix be immutable? It's not a type like a Fixnum, it's a
    >> data structure, like an Array or Hash. What is gained by creating a new
    >> Matrix from a changing data stream, rather than updating an existing
    >> one?

    >
    > In mathematics, matrices of various kinds are regarded as a unit, usually
    > indivisible. For example, they're used a lot in computer graphics
    > processing to hold rotation matrices.
    >
    > The rotation matrices are loaded with specific values that accomplish
    > rotations and translations, and until the computed viewpoint changes, the
    > entire matrix is used to process all the image vertices.
    >
    > Then, when the viewpoint changes, the entire matrix is recomputed, and a new
    > immutable matrix is created. The point I am making is that the matrix can't
    > function if it is thought of as nine scalar values rather than a 3x3 square
    > matrix.


    The immutability of a Matrix violates Whoever's Law, which states

    It is possible to write Fortran programs in any language.

    <ducking>

    Seriously, though, I agree with the poster who said some mathematicians
    want to be able to compute and change elements of a matrix. I happen to
    be one of those, and the subclass trick someone posted is one I'll need
    to use if I use the "Matrix" concept in my Ruby code. Given how slow it
    is, though, that seems unlikely.
     
    M. Edward (Ed) Borasky, Sep 11, 2006
    #8
  9. Guest

    Thanks everyone. I had seen the same answers in the post i mentioned
    (2-3 yrs now i think). i was wondering why these small things had not
    been added. In my humble opinion having these small things in the basic
    Matrix class would be helpful to beginners. I do not say that the basic
    class should have numerical stuff like SVD, LU decomposition or other
    linear algebra stuff etc. but these things would help. Also,if someone
    wants to "freeze" a matrix i think ruby has an operation for it(am
    learning ruby).

    Thanks again
    krishnan
    Paul Lutus wrote:
    > wrote:
    >
    > > Hi all,
    > > a) Is there a way to create a non-square matrix in ruby?

    >
    > Another poster has answered this one.
    >
    > > b) How does one set any arbitrary element of an array?

    >
    > Like this:
    >
    > -------------------------------------------
    >
    > #! /usr/bin/ruby
    >
    > size = 9
    >
    > # create, preset value
    >
    > array = Array.new(size) { Array.new(size) {"."} }
    >
    > # change one value
    >
    > array[4][4] = '*'
    >
    > # show it
    >
    > array.each do |b|
    > b.each do |i|
    > print b
    > end
    > puts ""
    > end
    >
    > Output:
    >
    > .........
    > .........
    > .........
    > .........
    > ....*....
    > .........
    > .........
    > .........
    > .........
    >
    > --
    > Paul Lutus
    > http://www.arachnoid.com
     
    , Sep 11, 2006
    #9
  10. Dave Burt Guest

    William Crawford wrote:
    > Paul Lutus wrote:
    >> The point I am making is that the matrix
    >> can't
    >> function if it is thought of as nine scalar values rather than a 3x3
    >> square
    >> matrix.

    >
    > I think the point they are making is that not everyone uses matrixes for
    > the same thing. I agree that for most applications, they should be
    > treated as immutable. But they are saying that they not only think they
    > should be modifyable, but that they need them to be modifyable to be
    > useful to them under their circumstances.


    If you want a matrix-like data structure (rather than a mathematical
    matrix), why not use an array of arrays?

    Cheers, Dave
     
    Dave Burt, Sep 11, 2006
    #10
  11. MonkeeSage Guest

    Dave Burt wrote:
    > If you want a matrix-like data structure (rather than a mathematical
    > matrix), why not use an array of arrays?


    A mathematical matrix is just a dimensional arrangement of data into
    "rows" and "columns", thus allowing for certain algorithmic
    relationships to be defined and applied (e.g., canonical
    decompositions). Every matrix (i.e., the actual matrix considered as a
    complex value) is immutable and has a unique identity, just like every
    number -- that's true -- but they can also be permuted or otherwise
    transformed into different matrices. Why should one have to do that
    constructively (i.e., construct a new matrix "from scratch"), rather
    than destructively (i.e., change the existing matrix "in place"). Think
    of it like this: n = 1; n = n + 1 -- even though the number 1 is
    immutable and has a unique identity, the reference to it is not, and
    can thus be destructively altered by assignment with the result that it
    refers to new identity. It would be a pain to have to do o = n + 1, p =
    n + 2 (i.e., creating new references every time the data changes), just
    because 1 is immutable. Why should a matrix be any different? Mabye I'm
    missing something...
     
    MonkeeSage, Sep 12, 2006
    #11
  12. On 9/11/06, MonkeeSage <> wrote:
    > Dave Burt wrote:
    > > If you want a matrix-like data structure (rather than a mathematical
    > > matrix), why not use an array of arrays?

    >
    > A mathematical matrix is just a dimensional arrangement of data into
    > "rows" and "columns", thus allowing for certain algorithmic
    > relationships to be defined and applied (e.g., canonical
    > decompositions). Every matrix (i.e., the actual matrix considered as a
    > complex value) is immutable and has a unique identity, just like every
    > number -- that's true -- but they can also be permuted or otherwise
    > transformed into different matrices. Why should one have to do that
    > constructively (i.e., construct a new matrix "from scratch"), rather
    > than destructively (i.e., change the existing matrix "in place"). Think
    > of it like this: n = 1; n = n + 1 -- even though the number 1 is
    > immutable and has a unique identity, the reference to it is not, and
    > can thus be destructively altered by assignment with the result that it
    > refers to new identity. It would be a pain to have to do o = n + 1, p =
    > n + 2 (i.e., creating new references every time the data changes), just
    > because 1 is immutable. Why should a matrix be any different? Mabye I'm
    > missing something...


    Yes, you're missing the distinction between a variable which
    references a sequence of objects over time, and the objects it
    references.

    a = 1
    a = 2

    Does NOT change 1 to 2.

    Fortran II used to let you change 1 to 2:

    subroutine a(arg)
    arg = 2
    end subroutine a

    call a(1)

    But this was considered a bug and was changed in Fortran IV.


    --
    Rick DeNatale

    My blog on Ruby
    http://talklikeaduck.denhaven2.com/
     
    Rick DeNatale, Sep 12, 2006
    #12
  13. Guest

    On Tue, 12 Sep 2006, MonkeeSage wrote:

    > Dave Burt wrote:
    >> If you want a matrix-like data structure (rather than a mathematical
    >> matrix), why not use an array of arrays?

    >
    > A mathematical matrix is just a dimensional arrangement of data into
    > "rows" and "columns", thus allowing for certain algorithmic
    > relationships to be defined and applied (e.g., canonical
    > decompositions). Every matrix (i.e., the actual matrix considered as a
    > complex value) is immutable and has a unique identity, just like every
    > number -- that's true -- but they can also be permuted or otherwise
    > transformed into different matrices. Why should one have to do that
    > constructively (i.e., construct a new matrix "from scratch"), rather
    > than destructively (i.e., change the existing matrix "in place"). Think
    > of it like this: n = 1; n = n + 1 -- even though the number 1 is
    > immutable and has a unique identity, the reference to it is not, and
    > can thus be destructively altered by assignment with the result that it
    > refers to new identity. It would be a pain to have to do o = n + 1, p =
    > n + 2 (i.e., creating new references every time the data changes), just
    > because 1 is immutable. Why should a matrix be any different? Mabye I'm
    > missing something...


    http://narray.rubyforge.org/

    and it even dovetails with

    http://rb-gsl.rubyforge.org/

    you can go back and forth.

    -a
    --
    in order to be effective truth must penetrate like an arrow - and that is
    likely to hurt. -- wei wu wei
     
    , Sep 12, 2006
    #13
  14. Guest

    On Tue, 12 Sep 2006, Rick DeNatale wrote:

    > Yes, you're missing the distinction between a variable which
    > references a sequence of objects over time, and the objects it
    > references.
    >
    > a = 1
    > a = 2
    >
    > Does NOT change 1 to 2.
    >
    > Fortran II used to let you change 1 to 2:
    >
    > subroutine a(arg)
    > arg = 2
    > end subroutine a
    >
    > call a(1)
    >
    > But this was considered a bug and was changed in Fortran IV.


    but wouldn't it make to days go quickly! nothing beats debuggin to burn up
    the clock... perhaps and RCR?

    -a
    --
    in order to be effective truth must penetrate like an arrow - and that is
    likely to hurt. -- wei wu wei
     
    , Sep 12, 2006
    #14
  15. "MonkeeSage" <> wrote in message
    news:...
    > Dave Burt wrote:
    >> If you want a matrix-like data structure (rather than a mathematical
    >> matrix), why not use an array of arrays?

    >
    > A mathematical matrix is just a dimensional arrangement of data into
    > "rows" and "columns", thus allowing for certain algorithmic
    > relationships to be defined and applied (e.g., canonical
    > decompositions). Every matrix (i.e., the actual matrix considered as a
    > complex value) is immutable and has a unique identity, just like every
    > number -- that's true -- but they can also be permuted or otherwise
    > transformed into different matrices. Why should one have to do that
    > constructively (i.e., construct a new matrix "from scratch"), rather
    > than destructively (i.e., change the existing matrix "in place"). Think
    > of it like this: n = 1; n = n + 1 -- even though the number 1 is
    > immutable and has a unique identity, the reference to it is not, and
    > can thus be destructively altered by assignment with the result that it
    > refers to new identity. It would be a pain to have to do o = n + 1, p =
    > n + 2 (i.e., creating new references every time the data changes), just
    > because 1 is immutable. Why should a matrix be any different? Mabye I'm
    > missing something...


    As far as I can tell, matrices are no different. Your example shows
    that 1 is immutable but references to it can be re-assigned, so you can do:

    n = 3
    n = n + 1


    Matrices are no different and, so, you can do:


    n = Matrix.scalar(2, 3)
    n = n + Matrix.I(2)


    In both cases, the "number" types are immutable but the references to
    them are not, as you say. They both work exactly the same way. What are
    you trying to say?
     
    Just Another Victim of the Ambient Morality, Sep 12, 2006
    #15
  16. MonkeeSage Guest

    Rick DeNatale wrote:
    > Yes, you're missing the distinction between a variable which
    > references a sequence of objects over time, and the objects it
    > references.
    >
    > a = 1
    > a = 2
    >
    > Does NOT change 1 to 2.


    Hmm, I guess I wasn't clear, because that was my point (and another
    person also didn't understand me). My point was that n can be
    destructively altered by assignment, but its value (1) cannot be. So
    even though a particular matrix (as a value) is immutible and unique, a
    matrix (as a concept, viz., the data structure, it's reference) need
    not be. Given matrix r, represented simply as a dimensional array:

    r = [
    [1, 1, 1],
    [1, 1, 1],
    [1, 1, 1]
    ]

    What would be the difference between:

    n = 1
    n = n + 1

    And:

    r[1][1] = r[1][1] + 1

    You have a new matrix (as a value), so you're not violating identity,
    but you keep the same data structure by destructive assignment ([]=),
    rather than creating a new one with the new value in row 1, column 1.

    That can easily be added, as was mentioned above, but the rationale for
    it not being there is what doesn't make sense to me. The rationale is
    that a matrix (as a value) is immutable and unique; but so are regular
    integers and that doesn't hinder their "container" (i.e., a reference)
    from being mutable. We don't have to construct a new variable every
    time we transform an integer to a different integer; why should a
    member of matrix be any different and require a new matrix to be
    constructed rather than allowing the reference to the member to be
    destructively modified?

    Anyhow, I don't really care if []= is there by default, I can add it if
    I want it; but I just don't understand why "a matrix [as a value] is
    immutable" means that []= shouldn't be there regarding the data
    structure.

    Regards,
    Jordan
     
    MonkeeSage, Sep 12, 2006
    #16
  17. Michael Ulm Guest

    Paul Lutus wrote:

    > MonkeeSage wrote:
    >
    > / ...
    >
    >
    >>Anyhow, I don't really care if []= is there by default, I can add it if
    >>I want it; but I just don't understand why "a matrix [as a value] is
    >>immutable" means that []= shouldn't be there regarding the data
    >>structure.

    >
    >
    > You might as well ask why there is no formal method to change the third
    > digit of an integer in class Fixnum. The reply would be that integers are
    > ordinarily dealt with in their entirety, and for special needs, the user of
    > the class can write custom code to deal with the border cases, but there's
    > no point in trying to include all the marginal utilities in the formal
    > class definition.
    >
    > Most matrix operations operate on matrices as units. Here's an example, a
    > matrix from my library, responsible for rotating objects in three-space:
    >

    --snip--
    >
    > In this case, the matrix at the heart of the class is the most economical
    > way to accomplish the mathematical objective, and there's really no point
    > in being able to change any of its constituent parts, once it has been
    > constructed. It would be very easy to modify any single array cell, but
    > there's simply no point.
    >
    > I think the writers of the Matrix class realized this, and didn't bother to
    > include an assignment operator on the ground that it wouldn't have any
    > general utility.
    >


    Then they would have been wrong. The lack of the []= operator has been a
    major PITA for me, as I often need matrix functions (FWIW I'm a
    mathematician, working on applications of artificial inteligence).
    The implementations of many higher level matrix operations needs the
    assignment operator. Try implementing a SVD algorithm without it.

    Regards,

    Michael

    --
    Michael Ulm
    R&D Team
    ISIS Information Systems Austria
    tel: +43 2236 27551-219, fax: +43 2236 21081
    e-mail:
    Visit our Website: www.isis-papyrus.com

    ---------------------------------------------------------------
    This e-mail is only intended for the recipient and not legally
    binding. Unauthorised use, publication, reproduction or
    disclosure of the content of this e-mail is not permitted.
    This email has been checked for known viruses, but ISIS accepts
    no responsibility for malicious or inappropriate content.
    ---------------------------------------------------------------
     
    Michael Ulm, Sep 12, 2006
    #17
  18. Michael Ulm Guest

    Just Another Victim of the Ambient Morality wrote:

    > "MonkeeSage" <> wrote in message
    > news:...
    >
    >>Dave Burt wrote:
    >>
    >>>If you want a matrix-like data structure (rather than a mathematical
    >>>matrix), why not use an array of arrays?

    >>
    >>A mathematical matrix is just a dimensional arrangement of data into
    >>"rows" and "columns", thus allowing for certain algorithmic
    >>relationships to be defined and applied (e.g., canonical
    >>decompositions). Every matrix (i.e., the actual matrix considered as a
    >>complex value) is immutable and has a unique identity, just like every
    >>number -- that's true -- but they can also be permuted or otherwise
    >>transformed into different matrices. Why should one have to do that
    >>constructively (i.e., construct a new matrix "from scratch"), rather
    >>than destructively (i.e., change the existing matrix "in place"). Think
    >>of it like this: n = 1; n = n + 1 -- even though the number 1 is
    >>immutable and has a unique identity, the reference to it is not, and
    >>can thus be destructively altered by assignment with the result that it
    >>refers to new identity. It would be a pain to have to do o = n + 1, p =
    >>n + 2 (i.e., creating new references every time the data changes), just
    >>because 1 is immutable. Why should a matrix be any different? Mabye I'm
    >>missing something...

    >
    >
    > As far as I can tell, matrices are no different. Your example shows
    > that 1 is immutable but references to it can be re-assigned, so you can do:
    >
    > n = 3
    > n = n + 1
    >
    >
    > Matrices are no different and, so, you can do:
    >
    >
    > n = Matrix.scalar(2, 3)
    > n = n + Matrix.I(2)
    >
    >
    > In both cases, the "number" types are immutable but the references to
    > them are not, as you say. They both work exactly the same way. What are
    > you trying to say?
    >
    >


    I think he's trying to say, that there is no rational reason for
    leaving out the []= operator, and I agree.

    Regards,

    Michael

    --
    Michael Ulm
    R&D Team
    ISIS Information Systems Austria
    tel: +43 2236 27551-219, fax: +43 2236 21081
    e-mail:
    Visit our Website: www.isis-papyrus.com

    ---------------------------------------------------------------
    This e-mail is only intended for the recipient and not legally
    binding. Unauthorised use, publication, reproduction or
    disclosure of the content of this e-mail is not permitted.
    This email has been checked for known viruses, but ISIS accepts
    no responsibility for malicious or inappropriate content.
    ---------------------------------------------------------------
     
    Michael Ulm, Sep 12, 2006
    #18
  19. Dave Burt Guest

    Michael Ulm wrote:
    > Just Another Victim of the Ambient Morality wrote:
    >
    >> "MonkeeSage" <> wrote in message
    >> n = 3
    >> n = n + 1
    >>
    >>
    >> Matrices are no different and, so, you can do:
    >>
    >>
    >> n = Matrix.scalar(2, 3)
    >> n = n + Matrix.I(2)
    >>
    >>
    >> In both cases, the "number" types are immutable but the references
    >> to them are not, as you say. They both work exactly the same way.
    >> What are you trying to say?
    >>
    >>

    >
    > I think he's trying to say, that there is no rational reason for
    > leaving out the []= operator, and I agree.



    The benefit is that you can assume no other code is modifying your matrix.

    n = Matrix.I(2)
    some_function(n)
    p n # same as before

    It's a good feature.

    []= would be useful, too, but I think to keep the immutability a better
    addition that provides the same functionality could be provided; a
    modified_at(x,y,value) method or something, but naming it is hard.

    Cheers,
    Dave
     
    Dave Burt, Sep 12, 2006
    #19
  20. Dave Burt wrote:
    > If you want a matrix-like data structure (rather than a mathematical
    > matrix), why not use an array of arrays?


    Because then I'd have to write all the matrix functions to go along with
    them.

    --
    Posted via http://www.ruby-forum.com/.
     
    William Crawford, Sep 12, 2006
    #20
    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. walala
    Replies:
    2
    Views:
    2,334
    joris
    Mar 24, 2010
  2. lvcargnini

    Matrix composed by two matrix

    lvcargnini, Jul 4, 2006, in forum: VHDL
    Replies:
    3
    Views:
    2,691
    Jonathan Bromley
    Jul 5, 2006
  3. Holgerson

    Matrix*Vector and Vector*Matrix

    Holgerson, Oct 25, 2007, in forum: C++
    Replies:
    3
    Views:
    427
    Holgerson
    Oct 26, 2007
  4. Terry Reedy
    Replies:
    0
    Views:
    564
    Terry Reedy
    Apr 2, 2009
  5. Robert Kern
    Replies:
    0
    Views:
    607
    Robert Kern
    Apr 2, 2009
Loading...

Share This Page