Open In App

Ruby | Matrix imag() function

Improve
Improve
Like Article
Like
Save
Share
Report

The imag() is an inbuilt method in Ruby returns a matrix with only imaginary part in it. The other index are assigned to zero.

Syntax: mat1.imag()

Parameters: The function does not takes any parameter.

Return Value: It returns a matrix with only imaginary part.

Example 1:




#Ruby program for imag() method in Matrix
  
#Include matrix
require "matrix"
  
#Initialize a matrix
    mat1
    = Matrix[[ Complex(1, 2), 21 ], [ 31, Complex(9, 12) ]]
  
#prints the imaginary part
      puts mat1.imag()


Output:

Matrix[[2, 0], [0, 12]]

Example 2:




#Ruby program for imag() method in Matrix
  
#Include matrix
require "matrix"
  
#Initialize a matrix
    mat1
    = Matrix[[ Complex(19, 3), Complex(12, 3) ], [ Complex(7, 8), Complex(91, 2) ]]
  
#prints the imaginary part
      puts mat1.imag()


Output:

Matrix[[3, 3], [8, 2]]

Last Updated : 07 Jan, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads