Ruby | Matrix scalar() function
Last Updated :
07 Jan, 2020
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:
require "matrix"
mat1 = Matrix.scalar( 2 , 6 )
puts mat1
|
Output:
Matrix[[6, 0], [0, 6]]
Example 2:
require "matrix"
mat1 = Matrix.scalar( 4 , 2 )
puts mat1
|
Output:
Matrix[[2, 0, 0, 0], [0, 2, 0, 0], [0, 0, 2, 0], [0, 0, 0, 2]]
Share your thoughts in the comments
Please Login to comment...