Subscript order is reversed in NMatrix
from the conventional mathematical notation (a_{ij} = a[j,i]) ,
because NArray's dimension order follows FORTRAN style.

In the operations between NMatrix/NVector and NArray, NArray works as Scalar. The dimensions following the class dimension(s) work in the same way as NArray's dimension.

This makes it easy to handle, e.g., a 100x100 two-dimensional array of 3-dimensional Vector. (Quiz: How many dimensional array is this?)

Example:

% irb -r nmatrix irb(main):001:0> m = NMatrix.float(2,2).indgen! NMatrix.float(2,2): [ [ 0.0, 1.0 ], [ 2.0, 3.0 ] ] irb(main):002:0> a = NArray.float(2).indgen!+1 NArray.float(2): [ 1.0, 2.0 ] irb(main):003:0> a * m NMatrix.float(2,2,2): [ [ [ 0.0, 1.0 ], [ 2.0, 3.0 ] ], [ [ 0.0, 2.0 ], [ 4.0, 6.0 ] ] ] irb(main):004:0> a + m TypeError: Illegal operation: NArray + NMatrix ./nmatrix.rb:109:in `coerce_rev' (irb):4:in `+' (irb):4:in `irb_binding' |

- If all the class dimensions are element indices (Integer), class dimensions are removed and convert to NArray. (Policy: Element extraction makes Scalar.)
- If at least one non-element index (e.g. Range) appears in the class dimension, all the the class dimensions remain even though there is some element index. (Policy: Class dimensions are saved.)
- Any dimensions following the class dimension(s) obey the manner of NArray's multi-dimension. (removed if Element indices, remain if non-element indices. If all the indices are element, convert to non-array object (e.g. Float) )

% irb -r nmatrix irb(main):001:0> m = NMatrix.float(2,2,2).indgen! NMatrix.float(2,2,2): [ [ [ 0.0, 1.0 ], [ 2.0, 3.0 ] ], [ [ 4.0, 5.0 ], [ 6.0, 7.0 ] ] ] irb(main):002:0> m[1,1,true] NArray.float(2): [ 3.0, 7.0 ] irb(main):003:0> m[0..1,1,true] NMatrix.float(2,1,2): [ [ [ 2.0, 3.0 ] ], [ [ 6.0, 7.0 ] ] ] |

y = m * x |

x = y / m |

x = m.lu.solve(y) |

- LU-factorize
`m`with NMatrix#lu method and return an instance of NMatrixLU class. - solve with NMatrixLU#solve method.

You should pay attention to the order of notation:

x = y / m |

x = m**-1 * y |

You can LU factorize Ruby Object arrays;

% irb -r nmatrix -r rational irb(main):001:0> m = NMatrix.object(4,4).collect!{Rational(rand(10))} NMatrix.object(4,4): [ [ Rational(3, 1), Rational(6, 1), Rational(5, 1), Rational(3, 1) ], [ Rational(1, 1), Rational(6, 1), Rational(2, 1), Rational(0, 1) ], [ Rational(0, 1), Rational(3, 1), Rational(1, 1), Rational(7, 1) ], [ Rational(2, 1), Rational(8, 1), Rational(9, 1), Rational(1, 1) ] ] irb(main):002:0> m/m NMatrix.object(4,4): [ [ Rational(1, 1), Rational(0, 1), Rational(0, 1), Rational(0, 1) ], [ Rational(0, 1), Rational(1, 1), Rational(0, 1), Rational(0, 1) ], [ Rational(0, 1), Rational(0, 1), Rational(1, 1), Rational(0, 1) ], [ Rational(0, 1), Rational(0, 1), Rational(0, 1), Rational(1, 1) ] ] |

Back to Numerical Ruby

Masahiro Tanaka <masa at ir.isas.ac.jp> Last modified: Nov 22, 2000