5 #pragma GCC diagnostic ignored "-Wstrict-overflow"
24 inline double conj(
const double x) {
return x; }
27 template<
typename T>
class matrix;
39 matrix(
const int n,
const int m);
40 matrix(
const int n,
const int m, T*
p);
45 T&
el (
const int i,
const int j);
46 T&
element(
const int i,
const int j) {
return el(i, j); }
47 const T&
el (
const int i,
const int j)
const;
48 const T&
element(
const int i,
const int j)
const {
return el(i, j); }
73 const matrix&
print(std::ostream& os = std::cout)
const;
83 void _create(
const int,
const int);
100 for (
int i = 0;
i <
n; ++
i)
113 assert(_nrows == _ncols);
114 for (
int i = 0;
i < _nrows; ++
i)
125 int* indx =
new int[_ncols];
127 assert(_nrows == _ncols);
130 for (
int i = 0;
i < _nrows; ++
i)
142 return abs(std::complex<double>(t));
150 int i, j, k, imax = 0;
151 assert(_nrows == _ncols);
156 T *vv =
new T[_nrows];
159 for (i = 0; i < _nrows; ++
i) {
161 for (j = 0; j < _nrows; ++j) {
165 if (
mag(big) == 0.0) {
167 std::cerr <<
"singular matrix " << std::endl;
175 for (j = 0; j < _nrows; ++j) {
176 for (i = 0; i < j; ++
i) {
178 for (k = 0; k <
i; ++k)
179 sum -= r.
el(i, k) * r.
el(k, j);
183 for (i = j; i < _nrows; ++
i) {
185 for (k = 0; k < j; ++k) {
186 sum -= r.
el(i, k) * r.
el(k, j);
189 if (
mag(vv[i] * sum) >=
mag(big)) {
190 big =
mag(vv[i] * sum);
195 for (k = 0; k < _nrows; ++k) {
197 r.
el(imax, k) = r.
el(j, k);
204 if (r.
el(j, j) == 0.0)
207 dum = 1.0 / r.
el(j, j);
208 for (i = j + 1; i < _nrows; ++
i)
224 int *indx =
new int[_nrows];
238 int i, ii = -1, ip, j;
242 for (i = 0; i < _nrows; ++
i) {
245 r.el(ip, 0) = r.el(i, 0);
247 for (j = ii; j < _nrows; ++j)
248 sum -= element(i, j) * r.element(j, 0);
254 for (i = _nrows-1; i > -1; --
i) {
256 for (j = i + 1; j < _nrows; ++j)
257 sum -= element(i, j) * r.element(j, 0);
258 r.el(i, 0) = sum / element(i, i);
270 int *indx =
new int[_ncols];
271 matrix r (_nrows, _ncols);
272 matrix lu(_nrows, _ncols);
275 for (j = 0; j < _nrows; ++j) {
280 for (i = 0; i < _nrows; ++
i)
281 r.
el(i, j) = x.
el(i, 0);
288 throw "singular matrix";
299 for (
int i = 0;
i < _nrows; ++
i)
300 for (
int j = 0; j < _ncols; ++j)
311 for (
int i = 0;
i < _nrows; ++
i)
312 for (
int j = 0; j < _ncols; ++j)
313 r.
el(j,
i) = el(
i, j);
334 for (
int i = 0;
i < _nrows; ++
i)
335 for (
int j = 0; j < M.
_ncols; ++j)
336 r.
el(
i, j) = el(
i, j) + M.
el(
i, j);
346 for (
int i = 0;
i < _nrows; ++
i)
347 for (
int j = 0; j < M.
_ncols; ++j)
348 el(
i, j) += M.
el(
i, j);
356 for (
int i = 0;
i < _nrows; ++
i)
357 for (
int j = 0; j < _ncols; ++j)
369 for (
int i = 0;
i < _nrows; ++
i)
370 for (
int j = 0; j < M.
_ncols; ++j)
371 r.
el(
i, j) = el(
i, j) - M.
el(
i, j);
381 for (
int i = 0;
i < _nrows; ++
i)
382 for (
int j = 0; j < M.
_ncols; ++j)
383 el(
i, j) -= M.
el(
i, j);
392 assert(_ncols == M.
_nrows);
394 for (
int i = 0;
i < _nrows; ++
i)
395 for (
int j = 0; j < M.
_ncols; ++j)
396 for (
int k = 0; k < _ncols; ++k)
397 r.
el(
i, j) += el(
i, k) * M.
el(k, j);
416 for (
int i = 0;
i < nrows(); ++
i)
417 for (
int j = 0; j < ncols(); ++j)
418 r.
el(
i, j) = a * element(
i, j);
428 assert((i < _nrows) && (j < _ncols));
429 return _f[j + i * _ncols];
438 assert((i < _nrows) && (j < _ncols));
439 return _f[j + i * _ncols];
453 memset(_f, 0, n * m *
sizeof(T));
537 const unsigned int nmbDigits = std::numeric_limits<double>::digits10 + 1;
538 std::ostringstream s;
539 s.precision(nmbDigits);
540 s.setf(std::ios_base::scientific, std::ios_base::floatfield);
541 s << _nrows <<
" " << _ncols << std::endl;
542 for (
int i = 0;
i < _nrows; ++
i) {
543 for (
int j = 0; j < _ncols; ++j)
544 s << _f[j +
i * _ncols] <<
"\t";
560 for (
int i = 0; i < _nrows; ++
i)
561 for (
int j = 0; j < _ncols; ++j) {
562 is >> _f[j + i * _ncols];
564 throw "trunc matrix input";
594 memset(_f, 0, _nrows * _ncols *
sizeof(T));
604 assert((_nrows == 3) && (_ncols == 3));
605 for (
int i = 0;
i < 3; ++
i)
606 for (
int j = 0; j < 3; ++j)
607 R.
el(
i) += el(
i, j) * V.
el(j);
617 assert((_nrows == 4) && (_ncols == 4));
618 for (
int i = 0;
i < 4; ++
i)
619 for (
int j = 0; j < 4; ++j)
620 r.
el(
i) += el(
i, j) * v.
el(j);