vkostyukov / la4j

Linear Algebra for Java
http://la4j.org
Apache License 2.0
373 stars 108 forks source link

Matrix Power() is not working properly #302

Closed ashokrajab closed 1 year ago

ashokrajab commented 5 years ago

matrix.power(2); This should square every element in my matrix and return the resultant matrix. But what it is actually doing is... it is multiplying itself and thrown error - java.lang.IllegalArgumentException: The number of rows in the left-hand matrix should be equal to the number of columns in the right-hand matrix.

Oliver-Loeffler commented 5 years ago

Actually the method matrix.power(n) works properly. What you are looking for is a function which applies an exponent to each element.

What matrix.power(n) is doing: http://mathonline.wikidot.com/powers-of-a-matrix#toc0

I have to see through all the functions and conceps where such element-wise power function would fit into or if it already exists. This is called Hadamard product.

Reference: https://en.wikipedia.org/wiki/Hadamard_product_(matrices)

Oliver-Loeffler commented 5 years ago

There is now a new method Matrix.hadamardPower(int n) which technically does what was asked for. Please try out.

This function uses the Hadmard product in order to produce Matrices with powers of n for each element. This function does not modify the origin matrix, a new matrix is created. When n=1 a copy of the source matrix is returned. As ⊚ is used to symbolize the Hadamard product operator, the operation looks like:
(A⊚B)ij = (A)ij⊚(B)ij

There are also test cases included for demonstration.

ashokrajab commented 5 years ago

Thank you, Oliver.

Oliver-Loeffler commented 1 year ago

Glad to See that there is still some life in LA4j.