Ruby Matrix square?()函数
square?() 是Ruby中一个内置的方法,它返回一个布尔值。如果它是一个方形矩阵,则返回真,否则返回假。
语法 :mat1.square?()
参数 :该函数需要检查矩阵是否为方形矩阵。
返回值 :如果是正方形矩阵则返回真,否则返回假。
例子 1 :
# Ruby program for square?() method in Matrix
# Include matrix
require "matrix"
# Initialize a matrix
mat1 = Matrix[[3, 12], [2, 8]]
# Prints if square? or not
puts mat1.square?()
输出:
true
例2 :
# Ruby program for square?() method in Matrix
# Include matrix
require "matrix"
# Initialize a matrix
mat1 = Matrix[[1, 0], [0, 1], [1, 2]]
# Prints if square? or not
puts mat1.square?()
输出:
false