红宝石 |矩阵标量()函数
scalar()是 Ruby 中的一个内置方法,它返回一个 N x N 对角矩阵,其中每个对角元素都是值。
Syntax: mat1.scalar(N, value)
Parameters: The function accepts twos mandatory parameters N and value where N is the size of the Identity matrix and value is the value to be assigned to the diagonals
Return Value: It returns the diagonal matrix.
示例 1 :
# Ruby program for scalar() method in Matrix
# Include matrix
require "matrix"
# Initialize a matrix
# using scalar method
mat1 = Matrix.scalar(2 ,6)
# Print the matrix
puts mat1
输出:
Matrix[[6, 0], [0, 6]]
示例 2 :
# Ruby program for scalar() method in Matrix
# Include matrix
require "matrix"
# Initialize a matrix
# using scalar method
mat1 = Matrix.scalar(4 , 2)
# Print the matrix
puts mat1
输出:
Matrix[[2, 0, 0, 0], [0, 2, 0, 0], [0, 0, 2, 0], [0, 0, 0, 2]]