double gaussian
_biLang_
·
·
个人记录
namespace mihomo
{
using std::swap;
using std::endl;
using std::ostream;
inline const bool
equal(const double& __x,
const double& __y)
{ return fabs(__x-__y) < 1e-9; }
class gaussian
{
private:
int _row;
int _col;
double** _LHS;
double* _RHS;
int _statue;
public:
gaussian(int __row, int __col)
: _row(__row), _col(__col)
{
_LHS = new double* [_row];
_RHS = new double [_row] ();
int __i, __j;
for (__i = 0; __i < _row; __i++)
_LHS[__i] = new double [_col] ();
_statue = 0;
}
~gaussian(void)
{
int __i;
for (__i = 0; __i < _row; __i++)
delete [] (_LHS[__i]);
delete [] (_LHS);
delete [] (_RHS);
}
inline double*
operator[](int __pos)
{ return _LHS[__pos]; }
inline double&
operator()(int __pos)
{ return _RHS[__pos]; }
friend inline ostream&
operator<<( ostream& os,
gaussian& __x )
{
int __i, __j;
os << "--------------------------------\n";
for (__i = 0; __i < __x._row; __i++)
{
for (__j = 0; __j < __x._col; __j++)
os << __x[__i][__j] << ' ';
os << "|" << __x._RHS[__i] << endl;
}
os << "--------------------------------\n";
}
inline const int&
solve(void)
{
int __i, __j, __k, __mx, __nw;
double __coef;
for (__k = 0; __k < _row && __k < _col; __k++)
{
#ifdef debug
cout << (*this);
#endif
__mx = __nw;
for (__i = __nw + 1; __i < _row; __i++)
if (abs(_LHS[__i][__k]) > abs(_LHS[__mx][__k]))
__mx = __i;
if (equal(_LHS[__mx][__k], 0)) continue;
if (__mx != __nw)
swap(_LHS[__mx], _LHS[__nw]),
swap(_RHS[__mx], _RHS[__nw]);
#ifdef debug
cout << (*this);
#endif
__coef = _LHS[__nw][__k];
for (__j = __k; __j < _col; __j++)
_LHS[__nw][__j] /= __coef;
_RHS[__nw] /= __coef;
#ifdef debug
cout << (*this);
#endif
for (__i = 0; __i < _row; __i++)
{
if (__i == __nw) continue;
__coef = _LHS[__i][__k];
for (__j = __k; __j < _col; __j++)
_LHS[__i][__j] -= __coef * _LHS[__nw][__j];
_RHS[__i] -= __coef * _RHS[__nw];
}
__nw++;
#ifdef debug
cout << (*this);
#endif
}
if (__nw == _row) return _statue = 1;
while (__nw < _row)
if (!equal(_RHS[__nw++], 0)) return _statue = -1;
return _statue = INT_MAX;
}
inline const int&
statue(void) const
{ return _statue; }
};
};