Ruby | Matrix scalar() function Last Updated : 07 Jan, 2020 Comments Improve Suggest changes Like Article Like Report The scalar() is an inbuilt method in Ruby returns an N x N diagonal matrix where each diagonal element is value. 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. Example 1: Ruby # 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 Output: Matrix[[6, 0], [0, 6]] Example 2: Ruby # 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 Output: Matrix[[2, 0, 0, 0], [0, 2, 0, 0], [0, 0, 2, 0], [0, 0, 0, 2]] Comment More infoAdvertise with us Next Article Ruby | Matrix scalar() function gopaldave Follow Improve Article Tags : Ruby Ruby-Methods Ruby Matrix-class Similar Reads Ruby | Matrix singular?() function The singular?() is an inbuilt method in Ruby returns a boolean value. It returns true if it is a singular matrix, else it returns false. It returns error if anything other than square matrix is used. Syntax: mat1.singular?() Parameters: The function needs the matrix to be checked for singular matrix 1 min read Ruby | Matrix square?() function The square?() is an inbuilt method in Ruby returns a boolean value. It returns true if it is a square matrix, else it returns false. Syntax: mat1.square?() Parameters: The function needs the matrix to be checked for square matrix or not. Return Value: It returns true if it is a square matrix, else i 1 min read Ruby | Matrix tr() function The tr() is an inbuilt method in Ruby returns the trace i.e., sum of diagonal elements of the matrix. Syntax: mat1.tr() Parameters: The function needs the matrix whose trace is to be returned. Return Value: It returns the trace. Example 1: Ruby # Ruby program for tr() method in Matrix # Include matr 1 min read Ruby | Matrix t() function The t() is an inbuilt method in Ruby returns the transpose of the matrix. Syntax: mat1.t() Parameters: The function needs the matrix to be transposed. Return Value: It returns the transposed matrix. Example 1: Ruby # Ruby program for t() method in Matrix # Include matrix require "matrix" # 1 min read Ruby | Matrix zero() function The zero() is an inbuilt method in Ruby returns an N x M zero matrix where each element is zero. Syntax: mat1.zero(N, M) Parameters: The function accepts two mandatory parameters N and M which is the size of the matrix. Return Value: It returns the zero matrix of size N x M. Example 1: Ruby # Ruby p 1 min read Ruby | Matrix to_s() function The to_s() is an inbuilt method in Ruby returns a string containing the matrix as the object. Syntax: mat1.to_s() Parameters: The function needs the matrix whose string object is to be returned. Return Value: It returns a string or self. Example 1: Ruby # Ruby program for to_s() method in Matrix # I 1 min read Ruby | Matrix zero?() function The zero?() is an inbuilt method in Ruby returns a boolean value. It returns true if it is a zero matrix, else it returns false. Zero matrix means that the matrix has all itselements as 0.Syntax: mat1.zero?()Parameters: The function needs the matrix to be checked for zero matrix.Return Value: It ret 1 min read Ruby | Matrix vstack() function The vstack() is an inbuilt method in Ruby returns a new matrix resulting by stacking vertically the receiver with the given matrices. It requires a matrix which is stacked upon vertically. Syntax: mat1.vstack(mat2) Parameters: The function needs a matrix which is to be stacked vertically. Return Val 1 min read Ruby | Matrix unitary?() function The unitary?() is an inbuilt method in Ruby returns a boolean value. It returns true if it is a unitary matrix, else it returns false. It returns error if anything other than unitary matrix is used. Syntax: mat1.unitary?() Parameters: The function needs the matrix to be checked for unitary matrix or 1 min read Ruby | Matrix trace() function The trace() is an inbuilt method in Ruby returns the trace i.e., sum of diagonal elements of the matrix. Syntax: mat1.trace() Parameters: The function needs the matrix whose trace is to be returned. Return Value: It returns the trace. Example 1: Ruby # Ruby program for trace() method in Matrix # Inc 1 min read Like