private void Factorize(int n, ReusableLU lu, DenseMatrix tmp, DenseMatrix jac, DenseMatrix mas, double fac) { var a = tmp.Values; var b = jac.Values; bool dae = mas != null; // Not tested. if (!dae) { // M = identity, Jacobian a full matrix for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { a[i * n + j] = -b[i * n + j]; } a[i * n + i] += fac; } } else { // M is a full matrix, Jacobian a full matrix for (int j = 0; j < n; j++) { for (int i = 0; i < n; i++) { a[i * n + j] = mas.At(i, j) * fac - b[i * n + j]; } } } lu.Compute(tmp); }
/// <summary> /// /// </summary> /// <param name="n">dimension of the system</param> /// <param name="fcn">subroutine computing the value of f(x,y)</param> /// <param name="autonomous">f(x,y) independent of x (autonomous)</param> /// <param name="jac">subroutine which computes the partial derivatives of f(x,y) with respect to y</param> /// <param name="dfx">subroutine which computes the partial derivatives of f(x,y) with respect to x</param> /// <param name="mas">the mass-matrix m.</param> /// <param name="rtol">relative error tolerances</param> /// <param name="atol">absolute error tolerances</param> /// <param name="controller"></param> public Rosenbrock4(int n, Action <double, double[], double[]> fcn, bool autonomous, Action <double, double[], DenseMatrix> jac, Action <double, double[], double[]> dfx, DenseMatrix mas, double rtol, double atol, RosenbrockErrorController controller) { this.n = n; this.fcn = fcn; this.autonomous = autonomous; this.jac = jac; this.dfx = dfx; this.mas = mas; this.rtol = rtol; this.atol = atol; this.controller = controller; ynew = new double[n]; dy1 = new double[n]; dy = new double[n]; ak1 = new double[n]; ak2 = new double[n]; ak3 = new double[n]; ak4 = new double[n]; ak5 = new double[n]; ak6 = new double[n]; fx = new double[n]; cont = new double[4 * n]; lu = new ReusableLU(n); fjac = new DenseMatrix(n); mjac = new DenseMatrix(n); }
void Solve(int n, ReusableLU lu, DenseMatrix mas, double[] dy, double[] ak, double[] fx, double[] ynew, double hd, bool stage1) { if (hd == 0.0) { for (int i = 0; i < n; i++) { ak[i] = dy[i]; } } else { for (int i = 0; i < n; i++) { ak[i] = dy[i] + hd * fx[i]; } } bool dae = mas != null; // Not tested. if (!dae) { // M = identity, Jacobian a full matrix if (stage1) { for (int i = 0; i < n; i++) { ak[i] += ynew[i]; } } } else { // M is a full matrix, Jacobian a full matrix for (int i = 0; i < n; i++) { double sum = 0.0; for (int j = 0; j < n; ++j) { sum += mas.At(i, j) * ynew[j]; } ak[i] += sum; } } lu.Solve(ak, ak); }