I resolve the following equation :
To solve it, I would like to use matrix exponential :
I thought about 3 ways to do it :
- I could have missed it but Oj-Algo could have a simple way to compute exp(A) (I did not find it in MatrixStore javadoc)
- I get matrix D and V from EigenValue methods ([A] = [V][D][V]-1) and then I compute
Then the question that comes first is how I apply x->exp(x*t) function to all diagonal elements of D ?
- Last idea is basically the same as 2. but I previously store the scalar-matrix product in a new matrix ([X] = [D]*(-t)) and then I compute :
Can you help me find the best way/methods/class I should use ? Thank you
NB : This question is a "follow up question" : initial question
EDIT : This is what i have tried for now, Is it the best way to do it ? :
import static org.ojalgo.function.PrimitiveFunction.EXP;
public class SolveDifferentialEquationTest
{
private static final PhysicalStore.Factory<Double, PrimitiveDenseStore> matrixFactory = PrimitiveDenseStore.FACTORY;
public static void main(String[] args)
{
SparseStore<Double> matrix;
final PhysicalStore<Double> diagMatrix;
final PhysicalStore<Double> eigenVectorMatrix;
final PhysicalStore<Double> inverseEigenVectorMatrix;
final Eigenvalue<Double> eigenvalue;
final int time = 100;
PhysicalStore<Double> initialVector;
final PhysicalStore<Double> finalVector;
int dim = 2000;
matrix = SparseStore.PRIMITIVE.make(dim, dim);
initialVector = matrixFactory.makeZero(dim,1);
// fill matrix and initialVector
//...
//Decompose matrix
eigenvalue = Eigenvalue.PRIMITIVE.make(matrix);
eigenvalue.decompose(matrix);
diagMatrix = eigenvalue.getD().copy();
eigenVectorMatrix = eigenvalue.getV().copy();
InverterTask<Double> inverter = InverterTask.PRIMITIVE.make(eigenVectorMatrix);
try {
inverseEigenVectorMatrix = inverter.invert(eigenVectorMatrix).copy();
} catch (RecoverableCondition e) {
throw new RuntimeException(e);
}
// Construct exp(Dt)
diagMatrix.multiply(time);
diagMatrix.modifyDiagonal(EXP);
// Compute
finalVector = inverseEigenVectorMatrix.multiply(diagMatrix)
.multiply(eigenVectorMatrix)
.multiply(initialVector)
.copy();
}
}