Open In App

Ruby | Matrix inv() function

Improve
Improve
Like Article
Like
Save
Share
Report

The inv() is an inbuilt method in Ruby returns the inverse of the given matrix.

Syntax: mat1.inv()

Parameters: The function does not takes any parameter.

Return Value: It returns the inverse of a matrix.

Example 1:




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


Output:

Matrix[[-313/24692-459/24692i, 777/24692+35/24692i], [1147/24692+155/74076i, -101/74076-227/74076i]]

Example 2:




#Ruby program for inv() method in Matrix
  
#Include matrix
require "matrix"
  
#Initialize a matrix
    mat1
    = Matrix[[ 1, -8 ], [ -2, -8 ]]
  
#prints the inv matrix
      puts mat1.inv()


Output:

Matrix[[1/3, -1/3], [-1/12, -1/24]]

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