红宝石 |矩阵标识()函数
identity()是 Ruby 中的一个内置方法,它返回一个 NXN 大小的 Identity 矩阵。
Syntax: mat1.identity(N)
Parameters: The function accepts a mandatory parameter N which is the size of the Identity matrix.
Return Value: It returns the Identity matrix.
示例 1 :
# Ruby program for identity() method in Matrix
# Include matrix
require "matrix"
# Initialize a matrix
# using identity method
mat1 = Matrix.identity(2)
# Print the matrix
puts mat1
输出:
Matrix[[1, 0], [0, 1]]
示例 2 :
# Ruby program for identity() method in Matrix
# Include matrix
require "matrix"
# Initialize a matrix
# using identity method
mat1 = Matrix.identity(3)
# Print the matrix
puts mat1
输出:
Matrix[[1, 0, 0], [0, 1, 0], [0, 0, 1]]