Open In App

Ruby | Matrix scalar() function

Last Updated : 07 Jan, 2020
Improve
Improve
Like Article
Like
Save
Share
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 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 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]]

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads