/********************************************************************/ /* Copyright (c) 2017 System fugen G.K. and Yuzi Mizuno */ /* All rights reserved. */ /********************************************************************/ #include "MGCLStdAfx.h" #include "mg/LinearEquation.h" #include "mg/Matrix.h" #include "mg/BPointSeq.h" #include "cskernel/b1bfac.h" #include "cskernel/b1bslv.h" #include "cskernel/b1hfac.h" #include "cskernel/b1hslv.h" #if defined(_DEBUG) #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif using namespace std; // Solve linear equations. // // //LU factorization using pivot. //Factorize W to LU in the linear equation W*X=A. //Using output W, the solution of W*X=A is obtained by solveLU. //Here, L is n by n lower triangular matrix and U is n by n upper //triangular matrix. U's diagonal is (1). void factorizeLU( MGMatrix& W,//left-hand side matrix of n*n is input, and //factorized LU matrix will be output where n=A.length(). //left-bottom triangle including the diagonal will contain L, //right-upper triangle excluding the diagonal will contain U. int* id //array of int id[n]. //pivot id will be output in id[i] for i=0,...,n-1 ){ int i,j,k,n=W.sdim(); int nm1=n-1, nm2=n-2; for(i=0; i=0; k--){ for(j=k+1; j=0; i--) X(i,j)=X(i,j)/W(i,0)-X(i+1,j)*W(i,1)-X(i+2,j)*W(i,2); } /* std::cout<=0) wa+=X(im2,0)*Wsave(im2,2); int im1=i-1;if(im1>=0) wa+=X(im1,0)*Wsave(im1,1); wa+=X(i,0)*Wsave(i,0); int ip1=i+1;if(ip1nlower); assert(nlower>=0); return b1bfac_(W.data(), W.capacity(), n, nlower, nband-1-nlower); } /// solveBandLU is the companion routine of factorizeBandLU. /// solveBandLU returns the solution of the linear system M*X = A /// in place of M, given the LU-Factorization for M obtained by factorizeBandLU /// in W. /// /// ****** M E T H O D ****** /// (With M = L*U, as stored in W,) the unit lower triangular system /// L(U*X) = B is solved for Y = U*X, and Y stored in B. Then the /// upper triangular syrtem U*X = Y is solved for X . The calculations /// are so arranged that the innermost loops stay within columns. /// Here, B=A(.,i) and X=X(.,i) for i=0,...,A.sdim()-1. void solveBandLU( const MGBPointSeq& W,///< factorized LU matrix is input ///< which is obtained by factorizeBandLU. ///< Left-bottom triangle including the diagonal contain L, ///< right-upper triangle excluding the diagonal contain U. ///< Refer to factorizeBandLU. int nlower, ///< number of bands of the matrix M below the main diagonal. const MGBPointSeq& A,///< right hand side vector. A.length() is W.sdim() ///< =n(order of the matrix M). MGBPointSeq& X ///< solved X will be output. X.length() will be A.length(), ///< and X.sdim() will be A.sdim(). ){ int n=W.sdim(), nband=W.length(), nsd=A.sdim(), nroww=W.capacity(); assert(A.length()==n); int nupper=nband-1-nlower; assert(nupper>=0); const double* wd=W.data(); X=A; for(int i=0; i work(new double[n]); b1hfac_(W.data(), nband, n, work.get()); } /// Solves the linear system C*X = A, provided W contains the Cholesky factorization. /// The Cholesky factorization is obtained by factorizeCholeLU. See factorizeCholeLU. void solveCholeLU( const MGBPointSeq& W,///< contains the Cholesky factorization ///< for C, can be obtained by factorizeCholeLU. ///< Refer to factorizeCholeLU. const MGBPointSeq& A,///< right hand side vector. A.length() is W.sdim()=n(order of the matrix C). MGBPointSeq& X ///< solved X will be output. X.length() will be A.length(), ///< and X.sdim() will be A.sdim(). ){ int n=W.sdim(), nband=W.length(), nsd=A.sdim(); assert(A.length()==n); const double* wd=W.data(); X=A; for(int i=0; i