Subscript order is reversed in NMatrix from the conventional mathematical notation (aij = 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' |
% 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) |
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) ] ] |