2
* Copyright (C) 2007-2008 Anael Orlinski
4
* This file is part of Panomatic.
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.
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.
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
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
#include "MathStuff.h"
24
using namespace lfeat;
26
bool Math::SolveLinearSystem33(double *solution, double sq[3][3])
29
int row, col, c, pivot = 0, i;
30
double maxc, coef, temp, mult, val;
32
/* Triangularize the matrix. */
33
for (col = 0; col < size - 1; col++)
35
/* Pivot row with largest coefficient to top. */
37
for (row = col; row < size; row++)
40
coef = (coef < 0.0 ? - coef : coef);
49
/* Exchange "pivot" with "col" row (this is no less efficient
50
than having to perform all array accesses indirectly). */
51
for (i = 0; i < size; i++)
54
sq[pivot][i] = sq[col][i];
57
temp = solution[pivot];
58
solution[pivot] = solution[col];
61
/* Do reduction for this column. */
62
for (row = col + 1; row < size; row++)
64
mult = sq[row][col] / sq[col][col];
65
for (c = col; c < size; c++) /* Could start with c=col+1. */
66
sq[row][c] -= mult * sq[col][c];
67
solution[row] -= mult * solution[col];
71
/* Do back substitution. Pivoting does not affect solution order. */
72
for (row = size - 1; row >= 0; row--) {
74
for (col = size - 1; col > row; col--)
75
val -= solution[col] * sq[row][col];
76
solution[row] = val / sq[row][row];
81
bool Math::Normalize(double* iVec, int iLen)
85
double val, fac, sqlen = 0.0;
87
for (i = 0; i < iLen; i++) {
94
fac = 1.0 / sqrt(sqlen);
95
for (i = 0; i < iLen; i++)