fn_solve.hpp

Go to the documentation of this file.
00001 // Copyright (C) 2009 NICTA
00002 // 
00003 // Authors:
00004 // - Conrad Sanderson (conradsand at ieee dot org)
00005 // 
00006 // This file is part of the Armadillo C++ library.
00007 // It is provided without any warranty of fitness
00008 // for any purpose. You can redistribute this file
00009 // and/or modify it under the terms of the GNU
00010 // Lesser General Public License (LGPL) as published
00011 // by the Free Software Foundation, either version 3
00012 // of the License or (at your option) any later version.
00013 // (see http://www.opensource.org/licenses for more info)
00014 
00015 
00016 //! \addtogroup fn_solve
00017 //! @{
00018 
00019 
00020 
00021 //! Solve a system of linear equations, i.e., A*X = B, where X is unknown.
00022 //! For a square matrix A, this function is conceptually the same as X = inv(A)*B,
00023 //! but is done more efficiently.
00024 //! The number of rows in A and B must be the same.
00025 //! B can be either a column vector or a matrix.
00026 //! This function will also try to provide approximate solutions
00027 //! to under-determined as well as over-determined systems (non-square A matrices).
00028 
00029 template<typename eT, typename T1, typename T2>
00030 inline
00031 bool
00032 solve(Mat<eT>& X, const Base<eT,T1>& A_in, const Base<eT,T2>& B_in)
00033   {
00034   arma_extra_debug_sigprint();
00035   
00036   const unwrap<T1> tmp1(A_in.get_ref());
00037   const unwrap<T2> tmp2(B_in.get_ref());
00038   
00039   const Mat<eT>& A = tmp1.M;
00040   const Mat<eT>& B = tmp2.M;
00041   
00042   arma_debug_check( (A.n_rows != B.n_rows), "solve(): number of rows in A and B must be the same" );
00043   
00044   if(A.n_rows == A.n_cols)
00045     {
00046     return auxlib::solve(X, A, B);
00047     }
00048   else
00049   if(A.n_rows > A.n_cols)
00050     {
00051     arma_extra_debug_print("solve(): detected over-determined system");
00052     return auxlib::solve_od(X, A, B);
00053     }
00054   else
00055     {
00056     arma_extra_debug_print("solve(): detected under-determined system");
00057     return auxlib::solve_ud(X, A, B);
00058     }
00059   }
00060 
00061 
00062 
00063 template<typename eT, typename T1, typename T2>
00064 inline
00065 Mat<eT>
00066 solve(const Base<eT,T1>& A_in, const Base<eT,T2>& B_in)
00067   {
00068   arma_extra_debug_sigprint();
00069   
00070   Mat<eT> X;
00071   bool info = solve(X, A_in, B_in);
00072   
00073   if(info == false)
00074     {
00075     arma_print("solve(): solution not found");
00076     }
00077   
00078   return X;
00079   }
00080 
00081 
00082 
00083 //! @}