From d5203db4e01f1e74135ecb2d1c9ffbb40489cb9b Mon Sep 17 00:00:00 2001 From: Matthew Ellison Date: Mon, 7 Feb 2022 15:47:34 +0000 Subject: [PATCH] Added power function --- .../com/mattrixwv/matrix/IntegerMatrix.java | 26 ++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/mattrixwv/matrix/IntegerMatrix.java b/src/main/java/com/mattrixwv/matrix/IntegerMatrix.java index 0f64b18..1de8305 100644 --- a/src/main/java/com/mattrixwv/matrix/IntegerMatrix.java +++ b/src/main/java/com/mattrixwv/matrix/IntegerMatrix.java @@ -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()){