linear_algebra_double.h
00001 /* Copyright (C) 2003-2005  Martin Förg
00002 
00003  * This library is free software; you can redistribute it and/or
00004  * modify it under the terms of the GNU Lesser General Public
00005  * License as published by the Free Software Foundation; either
00006  * version 2.1 of the License, or (at your option) any later version.
00007  * 
00008  * This library is distributed in the hope that it will be useful,
00009  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011  * Lesser General Public License for more details.
00012  * 
00013  * You should have received a copy of the GNU Lesser General Public
00014  * License along with this library; if not, write to the Free Software
00015  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00016  *
00017  * Contact:
00018  *   martin.o.foerg@googlemail.com
00019  *
00020  */
00021 
00022 #ifndef linear_algebrad_h
00023 #define linear_algebrad_h
00024 
00025 #include "square_matrix.h"
00026 #include "vector.h"
00027 #include "row_vector.h"
00028 #include "diagonal_matrix.h"
00029 #include "symmetric_matrix.h"
00030 #include "band_matrix.h"
00031 #include <complex>
00032 
00033 //-------------------------------------
00034 // Matrix operations
00035 //-------------------------------------
00036 
00037 namespace fmatvec {
00041 int svd(Matrix<General, Ref, Ref, double> &A,    Matrix<General, Ref, Ref, double> &S,  SquareMatrix<Ref, double> &U, SquareMatrix<Ref, double> &VT, int Rueckgabe);
00042 
00049   Vector<Ref, std::complex<double> > eigval(const SquareMatrix<Ref, double> &A);
00050 
00059   int eigvec(const SquareMatrix<Ref, double> &A, SquareMatrix<Ref, std::complex<double> > &eigenvectors, Vector<Ref, std::complex<double> > &eigenvalues);
00060 
00071   int eigvec(const Matrix<Symmetric, Ref, Ref, double> &A, const Matrix<Symmetric, Ref, Ref, double> &B, SquareMatrix<Ref, double> &eigenvectors, Vector<Ref, double> &eigenvalues);
00072 
00079   Vector<Ref, double> eigval(const Matrix<Symmetric, Ref, Ref, double> &A);
00080 
00092   Vector<Ref, double> eigvalSel(const Matrix<Symmetric, Ref, Ref, double> &A, int il, int iu, double abstol = 0);
00093 
00115   int Doolittle_LU_with_Pivoting_Solve(double *A, double B[], int pivot[], double x[], int n);
00116 
00126   Matrix<General, Ref, Ref, double> slvLU(const SquareMatrix<Ref, double> &A, const Matrix<General, Ref, Ref, double> &B);
00127 
00137   Vector<Ref, double> slvLU(const SquareMatrix<Ref, double> &A, const Vector<Ref, double> &b);
00138 
00149   Vector<Ref, double> slvLU(const SquareMatrix<Ref, double> &A, const Vector<Ref, double> &x, int & info);
00150 
00161   Matrix<General, Ref, Ref, double> slvLUFac(const SquareMatrix<Ref, double> &A, const Matrix<General, Ref, Ref, double> &B, const Vector<Ref, int> &ipiv);
00162 
00173   Vector<Ref, double> slvLUFac(const SquareMatrix<Ref, double> &A, const Vector<Ref, double> &b, const Vector<Ref, int> &ipiv);
00174 
00184   Matrix<General, Ref, Ref, double> slvLL(const Matrix<Symmetric, Ref, Ref, double> &A, const Matrix<General, Ref, Ref, double> &B);
00185 
00195   Vector<Ref, double> slvLL(const Matrix<Symmetric, Ref, Ref, double> &A, const Vector<Ref, double> &b);
00196 
00206   Matrix<General, Ref, Ref, double> slvQR(const SquareMatrix<Ref, double> &A, const Matrix<General, Ref, Ref, double> &B);
00207 
00217   Vector<Ref, double> slvQR(const SquareMatrix<Ref, double> &A, const Vector<Ref, double> &b);
00218 
00226   SquareMatrix<Ref, double> inv(const SquareMatrix<Ref, double> &A);
00227 
00235   Matrix<Symmetric, Ref, Ref, double> inv(const Matrix<Symmetric, Ref, Ref, double> &A);
00236 
00244   Matrix<Diagonal, Ref, Ref, double> inv(const Matrix<Diagonal, Ref, Ref, double> &A);
00245 
00254   Matrix<General, Ref, Ref, double> facLU(const Matrix<General, Ref, Ref, double> &A, Vector<Ref, int> &ipiv);
00255 
00264   SquareMatrix<Ref, double> facLU(const SquareMatrix<Ref, double> &A, Vector<Ref, int> &ipiv);
00265 
00277   int facLU(double *A, int pivot[], int n);
00278   
00286   Matrix<Symmetric, Ref, Ref, double> facLL(const Matrix<Symmetric, Ref, Ref, double> &A);
00287 
00294   double nrm1(const Vector<Ref, double> &x);
00295 
00302   double nrm2(const Vector<Ref, double> &x);
00303 
00310   double nrmInf(const Vector<Ref, double> &x);
00311   
00319   double nrm1(const Matrix<General, Ref, Ref, double> &A);
00320 
00327   double nrm2(const Matrix<General, Ref, Ref, double> &A);
00328 
00336   double nrmInf(const Matrix<General, Ref, Ref, double> &A);
00337 
00344   double nrmFro(const Matrix<General, Ref, Ref, double> &A);
00345 
00352   double rho(const SquareMatrix<Ref, double> &A);
00353 
00360   double rho(const Matrix<Symmetric, Ref, Ref, double> &A);
00361 
00362   Vector<Ref, double> slvLLFac(const Matrix<Symmetric, Ref, Ref, double> &A, const Vector<Ref, double> &x);
00363 
00364   Matrix<General, Ref, Ref, double> slvLLFac(const Matrix<Symmetric, Ref, Ref, double> &A, const Matrix<General, Ref, Ref, double> &X);
00365 
00366   Matrix<General, Ref, Ref, double> slvLS(const Matrix<General, Ref, Ref, double> &A, const Matrix<General, Ref, Ref, double> &B, double rcond = -1);
00367   
00368   Vector<Ref, double> slvLS(const Matrix<General, Ref, Ref, double> &A, const Vector<Ref, double> &b, double rcond = -1);
00369 
00370 //Matrix<General,Ref,Ref,double> slvLU(CBLAS_SIDE side, CBLAS_UPLO uplo, CBLAS_DIAG unit, const SquareMatrix<Ref,double> &A, const Matrix<General,Ref,Ref,double> &X, const Vector<Ref,int> &ipiv );
00371 
00379 //Matrix<General,Ref,Ref,double> swap(const Matrix<General,Ref,Ref,double> &A, const Vector<Ref,int> &ipiv );
00380 
00391   template <int size>
00392   Vector<Fixed<size>, double> slvLU(const SquareMatrix<Fixed<size>, double> &A, const Vector<Fixed<size>, double> &b, int & info) {
00393 
00394 #ifndef FMATVEC_NO_VOID_CHECK
00395     if (size == 0)
00396       return b;
00397 #endif
00398 
00399     int ipiv[size];
00400 
00401     SquareMatrix<Fixed<size>, double> LU = A;
00402 
00403     info = facLU(LU(), ipiv, size);
00404 
00405     Vector<Fixed<size>, double> x = b;
00406     Vector<Fixed<size>, double> y = b;
00407 
00408     info = Doolittle_LU_with_Pivoting_Solve(LU(), y(), ipiv, x(), size);
00409 
00410     return x;
00411 
00412   }
00413 
00414 }
00415 
00416 #endif