1 //----------------------------------------------------------------------------
2 // Anti-Grain Geometry - Version 2.4
3 // Copyright (C) 2002-2005 Maxim Shemanarev (http://www.antigrain.com)
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.
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
23 #include "agg_basics.h"
28 //=============================================================swap_arrays
29 template<class T
> void swap_arrays(T
* a1
, T
* a2
, unsigned n
)
32 for(i
= 0; i
< n
; i
++)
41 //============================================================matrix_pivot
42 template<unsigned Rows
, unsigned Cols
>
45 static int pivot(double m
[Rows
][Cols
], unsigned row
)
52 for(i
= row
; i
< Rows
; i
++)
54 if((tmp
= fabs(m
[i
][row
])) > max_val
&& tmp
!= 0.0)
68 swap_arrays(m
[k
], m
[row
], Cols
);
77 //===============================================================simul_eq
78 template<unsigned Size
, unsigned RightCols
>
81 static bool solve(const double left
[Size
][Size
],
82 const double right
[Size
][RightCols
],
83 double result
[Size
][RightCols
])
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....
111 for(j
= k
; j
< Size
+ RightCols
; j
++)
116 for(i
= k
+ 1; i
< Size
; i
++)
119 for (j
= k
; j
< Size
+ RightCols
; j
++)
121 tmp
[i
][j
] -= a1
* tmp
[k
][j
];
127 for(k
= 0; k
< RightCols
; k
++)
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
];