Skip to content
Snippets Groups Projects
Commit 44414566 authored by maikov's avatar maikov
Browse files

Add matrix transposition to exponentiation operator.

parent 20bb1eef
No related branches found
No related tags found
No related merge requests found
......@@ -9,5 +9,6 @@ public interface MathOp {
// For a matrix it is a determinant
Value absoluteValue();
// For matrix, if it is not finite then do matrix transposition.
Value exponentiation(Value other);
}
......@@ -3,6 +3,8 @@ package ee.ut.cs.gitlab.maikov.expressions;
import ee.ut.cs.gitlab.maikov.exceptions.InvalidReductionException;
import ee.ut.cs.gitlab.maikov.matrix.Matrix;
// Use Exponentiation with NaN as the power.
@Deprecated
public class MatrixTranspose extends Expression {
private final Expression expr;
......
......@@ -88,20 +88,25 @@ public class MatrixValue extends Value {
@Override
public Value exponentiation(Value other) {
if (other instanceof Number otherNumber) {
SquareMatrix squareMatrix = getSquareMatrix();
int n = (int) otherNumber.getNumber();
if (n == 0) {
squareMatrix.identity();
return new MatrixValue(squareMatrix);
} else if (n < 0) {
return new MatrixValue(InverseMatrix.compute(squareMatrix)).exponentiation(new Number(-n));
} else {
Matrix result = Matrix.clone(this.getMatrix());
for (int i = 1; i < n; i++) {
result.multiply(this.getMatrix());
if (Double.isFinite(otherNumber.getNumber())) {
SquareMatrix squareMatrix = getSquareMatrix();
int n = (int) otherNumber.getNumber();
if (n == 0) {
squareMatrix.identity();
return new MatrixValue(squareMatrix);
} else if (n < 0) {
return new MatrixValue(InverseMatrix.compute(squareMatrix)).exponentiation(new Number(-n));
} else {
Matrix result = Matrix.clone(this.getMatrix());
for (int i = 1; i < n; i++) {
result.multiply(this.getMatrix());
}
return new MatrixValue(result);
}
return new MatrixValue(result);
} else {
return new MatrixValue(this.matrix.transpose());
}
} else {
throw new InvalidOperandException("Ei saa astendada maatriksit maatriksiga või millegi muuga.");
}
......
......@@ -146,7 +146,7 @@ public class ExpressionTest {
5, 6, 7, 8,
9, 10, 11, 12,
});
Expression expr = new MatrixTranspose(new MatrixValue(a));
Expression expr = new Exponentiation(new MatrixValue(a), new Number(Double.NaN));
Expression result = expr.compute();
assertInstanceOf(MatrixValue.class, result);
MatrixValue value = (MatrixValue) result;
......@@ -174,7 +174,7 @@ public class ExpressionTest {
0, 8, 6,
1, 19, 12,
});
Expression expr = new MatrixTranspose(new Multiplication(new MatrixValue(a), new MatrixValue(b)));
Expression expr = new Exponentiation(new Multiplication(new MatrixValue(a), new MatrixValue(b)), new Number(Double.NaN));
Expression result = expr.compute();
assertInstanceOf(MatrixValue.class, result);
MatrixValue value = (MatrixValue) result;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment