MathStuff.cpp

Go to the documentation of this file.
00001 /*
00002 * Copyright (C) 2007-2008 Anael Orlinski
00003 *
00004 * This file is part of Panomatic.
00005 *
00006 * Panomatic is free software; you can redistribute it and/or modify
00007 * it under the terms of the GNU General Public License as published by
00008 * the Free Software Foundation; either version 2 of the License, or
00009 * (at your option) any later version.
00010 *
00011 * Panomatic is distributed in the hope that it will be useful,
00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 * GNU General Public License for more details.
00015 *
00016 * You should have received a copy of the GNU General Public License
00017 * along with Panomatic; if not, write to the Free Software
00018 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 */
00020 
00021 #include "MathStuff.h"
00022 #include <math.h>
00023 
00024 using namespace lfeat;
00025 
00026 bool Math::SolveLinearSystem33(double* solution, double sq[3][3])
00027 {
00028     const int size = 3;
00029     int row, col, c, pivot = 0, i;
00030     double maxc, coef, temp, mult, val;
00031 
00032     /* Triangularize the matrix. */
00033     for (col = 0; col < size - 1; col++)
00034     {
00035         /* Pivot row with largest coefficient to top. */
00036         maxc = -1.0;
00037         for (row = col; row < size; row++)
00038         {
00039             coef = sq[row][col];
00040             coef = (coef < 0.0 ? - coef : coef);
00041             if (coef > maxc)
00042             {
00043                 maxc = coef;
00044                 pivot = row;
00045             }
00046         }
00047         if (pivot != col)
00048         {
00049             /* Exchange "pivot" with "col" row (this is no less efficient
00050             than having to perform all array accesses indirectly). */
00051             for (i = 0; i < size; i++)
00052             {
00053                 temp = sq[pivot][i];
00054                 sq[pivot][i] = sq[col][i];
00055                 sq[col][i] = temp;
00056             }
00057             temp = solution[pivot];
00058             solution[pivot] = solution[col];
00059             solution[col] = temp;
00060         }
00061         /* Do reduction for this column. */
00062         for (row = col + 1; row < size; row++)
00063         {
00064             mult = sq[row][col] / sq[col][col];
00065             for (c = col; c < size; c++)        /* Could start with c=col+1. */
00066             {
00067                 sq[row][c] -= mult * sq[col][c];
00068             }
00069             solution[row] -= mult * solution[col];
00070         }
00071     }
00072 
00073     /* Do back substitution.  Pivoting does not affect solution order. */
00074     for (row = size - 1; row >= 0; row--)
00075     {
00076         val = solution[row];
00077         for (col = size - 1; col > row; col--)
00078         {
00079             val -= solution[col] * sq[row][col];
00080         }
00081         solution[row] = val / sq[row][row];
00082     }
00083     return true;
00084 }
00085 
00086 bool Math::Normalize(double* iVec, int iLen)
00087 {
00088 
00089     int i;
00090     double val, fac, sqlen = 0.0;
00091 
00092     for (i = 0; i < iLen; i++)
00093     {
00094         val = iVec[i];
00095         sqlen += val * val;
00096     }
00097     if (sqlen == 0.0)
00098     {
00099         return false;
00100     }
00101 
00102     fac = 1.0 / sqrt(sqlen);
00103     for (i = 0; i < iLen; i++)
00104     {
00105         iVec[i] *= fac;
00106     }
00107 
00108     return true;
00109 }
00110 

Generated on Fri Aug 22 01:25:48 2014 for Hugintrunk by  doxygen 1.3.9.1