Hugintrunk  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MathStuff.cpp
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2007-2008 Anael Orlinski
3 *
4 * This file is part of Panomatic.
5 *
6 * Panomatic is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * Panomatic is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with Panomatic; if not, write to the Free Software
18 * <http://www.gnu.org/licenses/>.
19 */
20 
21 #include "MathStuff.h"
22 #include <math.h>
23 
24 bool lfeat::Math::SolveLinearSystem33(double* solution, double sq[3][3])
25 {
26  const int size = 3;
27  int row, col, c, pivot = 0, i;
28  double coef, temp, mult;
29 
30  /* Triangularize the matrix. */
31  for (col = 0; col < size - 1; col++)
32  {
33  /* Pivot row with largest coefficient to top. */
34  double maxc = -1.0;
35  for (row = col; row < size; row++)
36  {
37  coef = sq[row][col];
38  coef = (coef < 0.0 ? - coef : coef);
39  if (coef > maxc)
40  {
41  maxc = coef;
42  pivot = row;
43  }
44  }
45  if (pivot != col)
46  {
47  /* Exchange "pivot" with "col" row (this is no less efficient
48  than having to perform all array accesses indirectly). */
49  for (i = 0; i < size; i++)
50  {
51  temp = sq[pivot][i];
52  sq[pivot][i] = sq[col][i];
53  sq[col][i] = temp;
54  }
55  temp = solution[pivot];
56  solution[pivot] = solution[col];
57  solution[col] = temp;
58  }
59  /* Do reduction for this column. */
60  for (row = col + 1; row < size; row++)
61  {
62  mult = sq[row][col] / sq[col][col];
63  for (c = col; c < size; c++) /* Could start with c=col+1. */
64  {
65  sq[row][c] -= mult * sq[col][c];
66  }
67  solution[row] -= mult * solution[col];
68  }
69  }
70 
71  /* Do back substitution. Pivoting does not affect solution order. */
72  for (row = size - 1; row >= 0; row--)
73  {
74  double val = solution[row];
75  for (col = size - 1; col > row; col--)
76  {
77  val -= solution[col] * sq[row][col];
78  }
79  solution[row] = val / sq[row][row];
80  }
81  return true;
82 }
83 
84 bool lfeat::Math::Normalize(double* iVec, int iLen)
85 {
86 
87  int i;
88  double fac, sqlen = 0.0;
89 
90  for (i = 0; i < iLen; i++)
91  {
92  const double val = iVec[i];
93  sqlen += val * val;
94  }
95  if (sqlen == 0.0)
96  {
97  return false;
98  }
99 
100  fac = 1.0 / sqrt(sqlen);
101  for (i = 0; i < iLen; i++)
102  {
103  iVec[i] *= fac;
104  }
105 
106  return true;
107 }
108 
static bool SolveLinearSystem33(double *solution, double sq[3][3])
Definition: MathStuff.cpp:24
static bool Normalize(double *iVec, int iLen)
Definition: MathStuff.cpp:84