void LUIncPreconditioner::solveMxb(Vector &x, const Vector &b) const { #ifdef DEBUG assert(x.getLength() == b.getLength() ); #endif Vector y( b.getLength() ); // TEMPORARY VECTOR forwardSubstitution(y,b); backwardSubstitution(x,y); }
static Vec<double> lsSolve(const Mat<double> &L, const Mat<double> &U, const Vec<double> &b) { Vec<double> x(L.ys()); // Solve Ly=b, Here y=x forwardSubstitution(L, b, x); // Solve Ux=y, Here x=y backwardSubstitution(U, x, x); return x; }
double * LUSolver(double **A, double *b, int n){ double **L, **U, *c, *x; L=allocateDoubleMatrix(n, n); U=allocateDoubleMatrix(n, n); LUFactotisation(n,A,U,L); c=forwardSubstitution(n, L, b); x=backSubstitution(n, U, c); return x; }
/* * Computer problem 4.2 #2 */ void problem42_2() { // variables struct matrix mat,chol,cholT; struct matrix4 eq = {{ {0.05,0.07,0.06,0.05}, {0.07,0.10,0.08,0.07}, {0.06,0.08,0.10,0.09}, {0.05,0.07,0.09,0.10} }}; struct array ans = {{0.23,0.32,0.33,0.31}}; struct array y,x; // setup mat.m4 = eq; mat.size = NUM4; // print printf("Problem 4.2 #2\n"); printf("Solve this system by Cholesky method:\n"); printf("0.05x1 + 0.07x2 + 0.06x3 + 0.05x4 = 0.23\n"); printf("0.07x1 + 0.10x2 + 0.08x3 + 0.07x4 = 0.32\n"); printf("0.06x1 + 0.08x2 + 0.10x3 + 0.09x4 = 0.33\n"); printf("0.05x1 + 0.07x2 + 0.09x3 + 0.10x4 = 0.31\n"); // do the work chol = cholesky(mat); // transpose cholT = transposeMatrix(chol); // solve Ly = b (for y) y = forwardSubstitution(chol,ans); // solve L^Tx = y (for x) x = backSubstitution(cholT,y); // print out results printf("\nCholesky\n"); printMatrix(chol); printf("\nTranspose \n"); printMatrix(cholT); printf("\nLy = b (for y)\n"); printArray(y,"y"); printf("L^Tx = y (for x)\n"); printArray(x,"x"); printf("\n"); return; }
void gauss() { int i, j; forwardSubstitution(); reverseElimination(); if(isinf(mx[0][n])) { printf("hiba"); return; } for (i = 0; i < n; ++i) { // hack! (to avoid -0.00000 and use 0.00000) if(mx[i][n]<0&&mx[i][n]>-0.00001) mx[i][n]=0; if(i+1<n) printf("%.5f ", mx[i][n]); else printf("%.5f", mx[i][n]); } }