public List <List <double> > solve(List <List <double> > f) { try { List <DiagMatrix> alpha = new List <DiagMatrix>(N); List <List <double> > beta = new List <List <double> >(N + 1); alpha.Add(null); beta.Add(null); alpha.Add(DiagMatrix.inverse(mainDiag[0]) * upDiag[0]); beta.Add(DiagMatrix.inverse(mainDiag[0]) * f[0]); for (int i = 1; i < N - 1; ++i) { DiagMatrix tmp = DiagMatrix.inverse(mainDiag[i] - downDiag[i] * alpha[i]); alpha.Add(tmp * upDiag[i]); beta.Add(tmp * diff(f[i], downDiag[i] * beta[i])); } beta.Add(DiagMatrix.inverse(mainDiag[N - 1] - downDiag[N - 1] * alpha[N - 1]) * diff(f[N - 1], downDiag[N - 1] * beta[N - 1])); List <List <double> > x = new List <List <double> >(N); for (int i = 0; i < N; ++i) { x.Add(null); } x[N - 1] = beta[N]; for (int i = N - 2; i >= 0; --i) { x[i] = diff(beta[i + 1], alpha[i + 1] * x[i + 1]); } return(x); } catch (Exception ex) { throw ex; } }
public static DiagMatrix inverse(DiagMatrix arg) { try { DiagMatrix res = new DiagMatrix(arg.order); for (int i = 0; i < res.order; ++i) { if (arg.data[i] == 0) { throw new Exception("Inversion error: inverse matrix does not exist"); } res.data[i] = 1.0 / arg.data[i]; } return(res); } catch (Exception ex) { throw ex; } }
public static DiagMatrix operator -(DiagMatrix d1, DiagMatrix d2) { try { if (d1.order != d2.order) { throw new Exception("Subtraction error: different sizes of matrices"); } int order = d1.order; DiagMatrix res = new DiagMatrix(order); for (int i = 0; i < order; ++i) { res.data[i] = d1.data[i] - d2.data[i]; } return(res); } catch (Exception ex) { throw ex; } }
public DiagMatrix(DiagMatrix other) { order = other.order; data = other.data; }