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 * <http://www.gnu.org/licenses/>.
00019 */
00020 
00021 #include "MathStuff.h"
00022 #include <math.h>
00023 
00024 bool lfeat::Math::SolveLinearSystem33(double* solution, double sq[3][3])
00025 {
00026     const int size = 3;
00027     int row, col, c, pivot = 0, i;
00028     double coef, temp, mult;
00029 
00030     /* Triangularize the matrix. */
00031     for (col = 0; col < size - 1; col++)
00032     {
00033         /* Pivot row with largest coefficient to top. */
00034         double maxc = -1.0;
00035         for (row = col; row < size; row++)
00036         {
00037             coef = sq[row][col];
00038             coef = (coef < 0.0 ? - coef : coef);
00039             if (coef > maxc)
00040             {
00041                 maxc = coef;
00042                 pivot = row;
00043             }
00044         }
00045         if (pivot != col)
00046         {
00047             /* Exchange "pivot" with "col" row (this is no less efficient
00048             than having to perform all array accesses indirectly). */
00049             for (i = 0; i < size; i++)
00050             {
00051                 temp = sq[pivot][i];
00052                 sq[pivot][i] = sq[col][i];
00053                 sq[col][i] = temp;
00054             }
00055             temp = solution[pivot];
00056             solution[pivot] = solution[col];
00057             solution[col] = temp;
00058         }
00059         /* Do reduction for this column. */
00060         for (row = col + 1; row < size; row++)
00061         {
00062             mult = sq[row][col] / sq[col][col];
00063             for (c = col; c < size; c++)        /* Could start with c=col+1. */
00064             {
00065                 sq[row][c] -= mult * sq[col][c];
00066             }
00067             solution[row] -= mult * solution[col];
00068         }
00069     }
00070 
00071     /* Do back substitution.  Pivoting does not affect solution order. */
00072     for (row = size - 1; row >= 0; row--)
00073     {
00074         double val = solution[row];
00075         for (col = size - 1; col > row; col--)
00076         {
00077             val -= solution[col] * sq[row][col];
00078         }
00079         solution[row] = val / sq[row][row];
00080     }
00081     return true;
00082 }
00083 
00084 bool lfeat::Math::Normalize(double* iVec, int iLen)
00085 {
00086 
00087     int i;
00088     double fac, sqlen = 0.0;
00089 
00090     for (i = 0; i < iLen; i++)
00091     {
00092         const double val = iVec[i];
00093         sqlen += val * val;
00094     }
00095     if (sqlen == 0.0)
00096     {
00097         return false;
00098     }
00099 
00100     fac = 1.0 / sqrt(sqlen);
00101     for (i = 0; i < iLen; i++)
00102     {
00103         iVec[i] *= fac;
00104     }
00105 
00106     return true;
00107 }
00108 

Generated on 24 Aug 2016 for Hugintrunk by  doxygen 1.4.7