Added power function
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
//Matrix/src/main/java/com/mattrixwv/IntegerMatrix.java
|
//Matrix/src/main/java/com/mattrixwv/IntegerMatrix.java
|
||||||
//Mattrixwv
|
//Mattrixwv
|
||||||
// Created: 02-01-22
|
// Created: 02-01-22
|
||||||
//Modified: 02-03-22
|
//Modified: 02-07-22
|
||||||
package com.mattrixwv.matrix;
|
package com.mattrixwv.matrix;
|
||||||
|
|
||||||
|
|
||||||
@@ -10,6 +10,7 @@ import java.util.StringJoiner;
|
|||||||
import com.mattrixwv.matrix.exceptions.InvalidCoordinatesException;
|
import com.mattrixwv.matrix.exceptions.InvalidCoordinatesException;
|
||||||
import com.mattrixwv.matrix.exceptions.InvalidGeometryException;
|
import com.mattrixwv.matrix.exceptions.InvalidGeometryException;
|
||||||
import com.mattrixwv.matrix.exceptions.InvalidRowSizeException;
|
import com.mattrixwv.matrix.exceptions.InvalidRowSizeException;
|
||||||
|
import com.mattrixwv.matrix.exceptions.InvalidScalarException;
|
||||||
|
|
||||||
|
|
||||||
public class IntegerMatrix{
|
public class IntegerMatrix{
|
||||||
@@ -504,6 +505,29 @@ public class IntegerMatrix{
|
|||||||
//Return the new matrix
|
//Return the new matrix
|
||||||
return new IntegerMatrix(newGrid);
|
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){
|
public int dotProduct(IntegerMatrix rightSide){
|
||||||
//Make sure the matrices have compatable geometry
|
//Make sure the matrices have compatable geometry
|
||||||
if(getNumCols() != rightSide.getNumRows()){
|
if(getNumCols() != rightSide.getNumRows()){
|
||||||
|
|||||||
Reference in New Issue
Block a user