STK++ 1.0
STK_LinAlgebra2D.cpp
Go to the documentation of this file.
00001 /*--------------------------------------------------------------------*/
00002 /*     Copyright (C) 2004-2007  Serge Iovleff
00003 
00004     This program is free software; you can redistribute it and/or modify
00005     it under the terms of the GNU Lesser General Public License as
00006     published by the Free Software Foundation; either version 2 of the
00007     License, or (at your option) any later version.
00008 
00009     This program is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012     GNU Lesser General Public License for more details.
00013 
00014     You should have received a copy of the GNU Lesser General Public
00015     License along with this program; if not, write to the
00016     Free Software Foundation, Inc.,
00017     59 Temple Place,
00018     Suite 330,
00019     Boston, MA 02111-1307
00020     USA
00021 
00022     Contact : Serge.Iovleff@stkpp.org
00023 */
00024 
00025 /*
00026  * Project:  Algebra
00027  * Purpose:  Implement 2D Linear Algebra methods with Real Containers
00028  * Author:   Serge Iovleff, serge.iovleff@stkpp.org
00029  *
00030  **/
00031 
00037 #include "../include/STK_LinAlgebra2D.h"
00038 
00039 namespace STK
00040 {
00041 /* @ingroup Algebra
00042  *  @brief trace of a square matrix
00043  *
00044  *  Compute the trace of the matrix A.
00045  *
00046  *  @param[in] A the Matrix
00047  *  @return the sum of the diagonal elements
00048  **/
00049 Real trace( MatrixSquare const& A)
00050 {
00051   Real sum = 0.0;
00052   const Integer first = A.firstCol(), last = A.lastCol();
00053   for (Integer k = first; k<=last; k++)
00054     sum += A(k, k);
00055   return sum;
00056 }
00057 
00058 /* @ingroup Algebra
00059  *  @brief Matrix multiplication by a Vector
00060  *
00061  *  Perform the product of the matrix A by the Vector X
00062  *  The matrix A and X must have the correct range.
00063  *
00064  * The result is created on the stack.
00065  *
00066  *  @param[in] A Left operand
00067  *  @param[in] X Right operand
00068  *  @return a pointer on the result as a Vector
00069  **/
00070 Vector* multLeftTranspose( Matrix const& A, Vector const& X)
00071 {
00072 #ifdef STK_DEBUG
00073   if (A.rangeVe() != X.range())
00074     throw runtime_error("In Vector* multLeftTranspose(A, X) "
00075                              "A.rangeHo() != X.range()");
00076 #endif
00077   // create result
00078   Vector* res = new Vector(A.rangeHo());
00079   // indexes
00080   const Integer firstRow = A.firstRow(), lastRow = A.lastRow();
00081   const Integer firstCol = A.firstCol(), lastCol = A.lastCol();
00082 
00083   for (Integer i=firstCol; i<=lastCol; i++)
00084   {
00085       Real sum = 0.0;
00086       for (Integer k = firstRow; k<=lastRow; k++)
00087         sum += A(k, i) * X[k];
00088       (*res)[i] = sum;
00089   }
00090   return res;
00091 }
00092 
00093 } // Namespace STK
00094