use of cbit.vcell.matrix.RationalExpMatrix in project vcell by virtualcell.
the class FastSystemAnalyzer method refreshInvarianceMatrix.
/**
*/
private void refreshInvarianceMatrix() throws MathException, ExpressionException {
//
// the invariance's are expressed in matrix form
//
// |a a a a a -1 0 0| |x1| |0|
// |a a a a a 0 -1 0| * |x2| = |0|
// |a a a a a 0 0 -1| |x3| |0|
// |x4|
// |x5|
// |c1|
// |c2|
// |c3|
//
Variable[] vars = new Variable[fastVarList.size()];
fastVarList.copyInto(vars);
int numVars = fastVarList.size();
int rows = fastSystem.getNumFastInvariants();
int cols = numVars + fastSystem.getNumFastInvariants();
RationalExpMatrix matrix = new RationalExpMatrix(rows, cols);
Enumeration<FastInvariant> fastInvariantsEnum = fastSystem.getFastInvariants();
for (int i = 0; i < rows && fastInvariantsEnum.hasMoreElements(); i++) {
FastInvariant fi = (FastInvariant) fastInvariantsEnum.nextElement();
Expression function = fi.getFunction();
for (int j = 0; j < numVars; j++) {
Variable var = (Variable) fastVarList.elementAt(j);
Expression exp = function.differentiate(var.getName());
exp.bindExpression(null);
exp = exp.flatten();
RationalExp coeffRationalExp = RationalExpUtils.getRationalExp(exp);
matrix.set_elem(i, j, coeffRationalExp);
}
matrix.set_elem(i, numVars + i, -1);
}
// Print
System.out.println("origMatrix");
matrix.show();
//
// gaussian elimination on the matrix give the following representation
// note that some column pivoting (variable re-ordering) is sometimes required to
// determine N-r dependent vars
//
// |10i0iccc|
// |01i0iccc| where (c)'s are the coefficients for constants of invariances
// |00i1iccc| (i)'s are the coefficients for dependent vars in terms of independent vars
//
// Print
System.out.println("reducedMatrix");
if (rows > 0) {
try {
matrix.gaussianElimination(new RationalExpMatrix(rows, rows));
} catch (MatrixException e) {
e.printStackTrace(System.out);
throw new MathException(e.getMessage());
}
}
matrix.show();
for (int i = 0; i < vars.length; i++) {
System.out.print(vars[i].getName() + " ");
}
System.out.println("");
//
for (int i = 0; i < rows; i++) {
for (int j = 0; j < rows; j++) {
RationalExp rexp = matrix.get(i, j).simplify();
matrix.set_elem(i, j, rexp);
}
}
for (int i = 0; i < rows; i++) {
//
if (!matrix.get(i, i).isConstant() || matrix.get(i, i).getConstant().doubleValue() != 1) {
for (int j = i + 1; j < numVars; j++) {
if (matrix.get(i, j).isConstant() && matrix.get(i, j).getConstant().doubleValue() == 1.0) {
for (int ii = 0; ii < rows; ii++) {
RationalExp temp = matrix.get(ii, i);
matrix.set_elem(ii, i, matrix.get(ii, j));
matrix.set_elem(ii, j, temp);
}
Variable tempVar = vars[i];
vars[i] = vars[j];
vars[j] = tempVar;
break;
}
}
}
}
// Print
for (int i = 0; i < vars.length; i++) {
System.out.print(vars[i].getName() + " ");
}
System.out.println("");
matrix.show();
//
// separate into dependent and indepent variables, and chop off identity matrix (left N-r columns)
//
// T |iiccc| T
// [x1 x2 x4] = -1 * |iiccc| * [x3 x5 c1 c2 c3]
// |iiccc|
//
//
int numInvariants = fastSystem.getNumFastInvariants();
dependentVarList.removeAllElements();
for (int i = 0; i < numInvariants; i++) {
dependentVarList.addElement(vars[i]);
}
independentVarList.removeAllElements();
for (int i = numInvariants; i < vars.length; i++) {
independentVarList.addElement(vars[i]);
}
int new_cols = independentVarList.size() + numInvariants;
dependencyMatrix = new RationalExpMatrix(rows, new_cols);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < new_cols; j++) {
RationalExp rexp = matrix.get(i, j + dependentVarList.size()).simplify().minus();
dependencyMatrix.set_elem(i, j, rexp);
}
}
// Print
System.out.println("\n\nDEPENDENCY MATRIX");
dependencyMatrix.show();
System.out.print("dependent vars: ");
for (int i = 0; i < dependentVarList.size(); i++) {
System.out.print(((Variable) dependentVarList.elementAt(i)).getName() + " ");
}
System.out.println("");
System.out.print("independent vars: ");
for (int i = 0; i < independentVarList.size(); i++) {
System.out.print(((Variable) independentVarList.elementAt(i)).getName() + " ");
}
System.out.println("");
}
Aggregations