Ruby | Matrix ** method

The ** is an inbuilt method in Ruby returns the matrix after the matrix is multiplied with self N times. It returns the matrix exponentiation value.

Syntax: Matrix_name ** (number)

Parameters: The function takes a mandatory parameter number which signifies the number of times it will be multiplied with self.



Return Value: It returns the resultant matrix after multiplication.

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby program for ** method in Matrix
# Include matrix 
require "matrix"
  
# Initialize a matrix 
mat = Matrix[[12,41], [61,81]] 
  
# Prints the matrix value after 
# multiplying it 3 times with self 
puts  mat ** 3

chevron_right


Output:

Matrix[[264333, 417298], [620858, 966615]]

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby program for ** method in Matrix
# Include matrix 
require "matrix"
   
# Initialize a matrix 
mat = Matrix[[1, 2, 6], [3, 4, 8], [12, 1, 3]] 
   
# Prints the matrix value after 
# multiplying it 2 times with self 
puts  mat ** 2

chevron_right


Output:

Matrix[[79, 16, 40], [111, 30, 74], [51, 31, 89]]


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.