vfs: check userland buffers before reading them.
[haiku.git] / headers / libs / agg / agg_simul_eq.h
blob3d0dce4b4473c376ccea4b04389db0232d79d8ea
1 //----------------------------------------------------------------------------
2 // Anti-Grain Geometry - Version 2.4
3 // Copyright (C) 2002-2005 Maxim Shemanarev (http://www.antigrain.com)
4 //
5 // Permission to copy, use, modify, sell and distribute this software
6 // is granted provided this copyright notice appears in all copies.
7 // This software is provided "as is" without express or implied
8 // warranty, and with no claim as to its suitability for any purpose.
9 //
10 //----------------------------------------------------------------------------
11 // Contact: mcseem@antigrain.com
12 // mcseemagg@yahoo.com
13 // http://www.antigrain.com
14 //----------------------------------------------------------------------------
16 // Solving simultaneous equations
18 //----------------------------------------------------------------------------
19 #ifndef AGG_SIMUL_EQ_INCLUDED
20 #define AGG_SIMUL_EQ_INCLUDED
22 #include <math.h>
23 #include "agg_basics.h"
25 namespace agg
28 //=============================================================swap_arrays
29 template<class T> void swap_arrays(T* a1, T* a2, unsigned n)
31 unsigned i;
32 for(i = 0; i < n; i++)
34 T tmp = *a1;
35 *a1++ = *a2;
36 *a2++ = tmp;
41 //============================================================matrix_pivot
42 template<unsigned Rows, unsigned Cols>
43 struct matrix_pivot
45 static int pivot(double m[Rows][Cols], unsigned row)
47 int k = int(row);
48 double max_val, tmp;
50 max_val = -1.0;
51 unsigned i;
52 for(i = row; i < Rows; i++)
54 if((tmp = fabs(m[i][row])) > max_val && tmp != 0.0)
56 max_val = tmp;
57 k = i;
61 if(m[k][row] == 0.0)
63 return -1;
66 if(k != int(row))
68 swap_arrays(m[k], m[row], Cols);
69 return k;
71 return 0;
77 //===============================================================simul_eq
78 template<unsigned Size, unsigned RightCols>
79 struct simul_eq
81 static bool solve(const double left[Size][Size],
82 const double right[Size][RightCols],
83 double result[Size][RightCols])
85 unsigned i, j, k;
86 double a1;
88 double tmp[Size][Size + RightCols];
90 for(i = 0; i < Size; i++)
92 for(j = 0; j < Size; j++)
94 tmp[i][j] = left[i][j];
96 for(j = 0; j < RightCols; j++)
98 tmp[i][Size + j] = right[i][j];
102 for(k = 0; k < Size; k++)
104 if(matrix_pivot<Size, Size + RightCols>::pivot(tmp, k) < 0)
106 return false; // Singularity....
109 a1 = tmp[k][k];
111 for(j = k; j < Size + RightCols; j++)
113 tmp[k][j] /= a1;
116 for(i = k + 1; i < Size; i++)
118 a1 = tmp[i][k];
119 for (j = k; j < Size + RightCols; j++)
121 tmp[i][j] -= a1 * tmp[k][j];
127 for(k = 0; k < RightCols; k++)
129 int m;
130 for(m = int(Size - 1); m >= 0; m--)
132 result[m][k] = tmp[m][Size + k];
133 for(j = m + 1; j < Size; j++)
135 result[m][k] -= tmp[m][j] * result[j][k];
139 return true;
147 #endif