Added power function

This commit is contained in:
2022-02-07 15:47:34 +00:00
parent 39aaf4f207
commit d5203db4e0

View File

@@ -1,7 +1,7 @@
//Matrix/src/main/java/com/mattrixwv/IntegerMatrix.java
//Mattrixwv
// Created: 02-01-22
//Modified: 02-03-22
//Modified: 02-07-22
package com.mattrixwv.matrix;
@@ -10,6 +10,7 @@ import java.util.StringJoiner;
import com.mattrixwv.matrix.exceptions.InvalidCoordinatesException;
import com.mattrixwv.matrix.exceptions.InvalidGeometryException;
import com.mattrixwv.matrix.exceptions.InvalidRowSizeException;
import com.mattrixwv.matrix.exceptions.InvalidScalarException;
public class IntegerMatrix{
@@ -504,6 +505,29 @@ public class IntegerMatrix{
//Return the new matrix
return new IntegerMatrix(newGrid);
}
public IntegerMatrix pow(int power){
//Make sure the matrix is square so it can be multiplied
if(!isSquare()){
throw new InvalidGeometryException("The matrix must be square to raise it to a power");
}
//Make sure the power is positive
if(power < 0){
throw new InvalidScalarException("The power must be >= 0");
}
else if(power == 0){
return new IntegerMatrix(getNumRows(), getNumCols(), 1);
}
//Create a new matrix for the product
IntegerMatrix newMatrix = clone();
//Multiply the current grid power times
for(int currentPower = 1;currentPower < power;++currentPower){
newMatrix = newMatrix.multiply(this);
}
//Return the new grid
return newMatrix;
}
public int dotProduct(IntegerMatrix rightSide){
//Make sure the matrices have compatable geometry
if(getNumCols() != rightSide.getNumRows()){