Ruby 矩阵**方法
**
是Ruby中的一个内置方法,在矩阵与自己相乘N次后返回矩阵。它返回矩阵的指数化值。
语法 :Matrix_name ** (number)
参数 : 该函数需要一个强制性的参数 number ,表示它将与自己相乘的次数。
返回值 : 它返回乘法后的结果矩阵。
例子 1 :
# Ruby program for ** method in Matrix
# Include matrix
require "matrix"
# Initialize a matrix
mat = Matrix[[12,41], [61,81]]
# Prints the matrix value after
# multiplying it 3 times with self
puts mat ** 3
输出:
Matrix[[264333, 417298], [620858, 966615]]
例2 :
# Ruby program for ** method in Matrix
# Include matrix
require "matrix"
# Initialize a matrix
mat = Matrix[[1, 2, 6], [3, 4, 8], [12, 1, 3]]
# Prints the matrix value after
# multiplying it 2 times with self
puts mat ** 2
输出:
Matrix[[79, 16, 40], [111, 30, 74], [51, 31, 89]]