Ruby | Matrix inverse() function Last Updated : 12 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report The inverse() is an inbuilt method in Ruby returns the inverse of the given matrix. Syntax: mat1.inverse() Parameters: The function does not takes any parameter. Return Value: It returns the inverse of a matrix. Example 1: CPP #Ruby program for inverse() method in Matrix #Include matrix require "matrix" #Initialize a matrix mat1 = Matrix[[ Complex(1, 2), 21 ], [ 31, Complex(9, 12) ]] #prints the inverse matrix puts mat1.inverse() Output: Matrix[[-313/24692-459/24692i, 777/24692+35/24692i], [1147/24692+155/74076i, -101/74076-227/74076i]] Example 2: CPP #Ruby program for inverse() method in Matrix #Include matrix require "matrix" #Initialize a matrix mat1 = Matrix[[ 1, -8 ], [ -2, -8 ]] #prints the inverse matrix puts mat1.inverse() Output: Matrix[[1/3, -1/3], [-1/12, -1/24]] Comment More infoAdvertise with us Next Article Ruby | Matrix imag() function G gopaldave Follow Improve Article Tags : Ruby Ruby-Methods Ruby Matrix-class Similar Reads Ruby | Matrix inv() function 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: CPP #Ruby program for inv() method in Matrix #Include matrix require "matrix 1 min read Ruby | Matrix I() function The I() is an inbuilt method in Ruby returns a Identity matrix of N X N size. Syntax: mat1.I(N) Parameters: The function accepts a mandatory parameter N which is the size of the Identity matrix. Return Value: It returns the Identity matrix. Example 1: Ruby # Ruby program for I() method in Matrix # I 1 min read Ruby | Matrix imaginary() function The imaginary() is an inbuilt method in Ruby returns a matrix with only imaginary part in it. The other index are assigned to zero. Syntax: mat1.imaginary() Parameters: The function does not takes any parameter. Return Value: It returns a matrix with only imaginary part. Example 1: CPP #Ruby program 1 min read Ruby | Matrix hash() function The hash() is an inbuilt method in Ruby returns the hash-code of the matrix. Syntax: mat1.hash()Parameters: The function does not accepts any parameter.Return Value: It returns the hash-code of the matrix. Example 1:Â Â Ruby # Ruby program for hash() method in Matrix # Include matrix require "matrix" 1 min read Ruby | Matrix imag() function 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: CPP #Ruby program for imag( 1 min read Ruby | Matrix identity() function The identity() is an inbuilt method in Ruby returns an Identity matrix of N X N size. Syntax: mat1.identity(N) Parameters: The function accepts a mandatory parameter N which is the size of the Identity matrix. Return Value: It returns the Identity matrix. Example 1: Ruby # Ruby program for identity( 1 min read Like