15 #include "tbci/basics.h"
16 #include "tbci/vector.h"
18 #include "tbci/matrix_sig.h"
25 #if !defined(NO_GD) && !defined(AUTO_DECL)
26 # include "tbci/matrix_gd.h"
32 #ifdef HAVE_GCC295_FRIEND_BUG
33 # define _VEC getvec()
34 # define _ENDVEC getendvec()
37 # define _COL columns()
38 # define _FAC getfac()
41 # define _ENDVEC endvec
49 #ifndef TBCI_DISABLE_EXCEPT
57 :
NumErr(
"Error in Matrix library") {}
67 # pragma interface "matrix.h"
72 template <
typename T>
class Matrix;
73 template <
typename T>
class TMatrix;
76 template <
typename T>
class BdMatrix;
77 template <
typename T>
class Tensor;
95 #if defined(SMP) && !defined(SMP_MATSLICE)
96 # define SMP_MATSLICE 4096
99 # define SMP_MATSLICE2 (SMP_MATSLICE/sizeof(T))
108 template <
typename T>
124 #if 1 //defined(HAVE_GCC295_FRIEND_BUG) || defined(HAVE_PROMOTION_BUG)
152 friend void FRIEND_TBCI2__ do_mat_vec_transmult_exact
FGD (
const unsigned start,
const unsigned end,
162 explicit TMatrix (
const unsigned = 0);
164 TMatrix (
const unsigned,
const unsigned);
166 TMatrix (
const T&,
const unsigned,
const unsigned);
180 static const
char*
mat_info () {
return (
"TMatrix"); }
187 #ifndef HAVE_PROMOTION_BUG
188 # ifndef HAVE_GCC295_TMPLFRNDCLS_BUG
190 template <
typename U>
friend class Matrix;
196 mat (dim>0?
NEW(Tptr,row):0), vec (dim>0?
NEW(
T,dim):0)
198 while (t < endvec) *t++ = *u++; }
203 mat (dim>0?
NEW(Tptr,row):0), vec (dim>0?
NEW(
T,dim):0)
205 while (t < endvec) *t++ = *u++; }
212 TMatrix<T>& operator = (const T&);
218 TMatrix<T>& operator += (const T&);
222 TMatrix<T>& operator -= (const T&);
234 TMatrix<T>& operator + (const T&);
238 TMatrix<T>& operator - (const T&);
249 {
Matrix<T> m (*
this);
return (m * tv); }
251 {
Matrix<T> m (*
this);
return (m * tsv); }
254 #ifndef HAVE_GCC295_FRIEND_BUG
274 #ifndef TBCI_NEW_BRACKET
281 T&
setval(
const T& val,
const unsigned int r,
const unsigned int c)
282 {
return mat[r][
c] = val; }
284 {
return mat[r][
c]; }
287 typename tbci_traits<T>::const_refval_type
288 get (
const unsigned r,
const unsigned c)
const
289 {
return mat[r][
c]; }
290 T&
set (
const T& val,
const unsigned r,
const unsigned c)
291 {
return mat[r][
c] = val; }
292 const T&
getcref(
const unsigned r,
const unsigned c)
const
293 {
return mat[r][
c]; }
296 const T*
getrowptr (
const unsigned r)
const {
return mat[r]; }
302 {
return !(*
this == m); }
305 {
Matrix<T> m (tm);
return (*
this == m); }
307 {
return !(*
this == tm); }
311 {
return !(*
this ==
ts); }
340 void set_row_indexed (
const Vector<T>&,
const unsigned int,
344 void set_row_indexed (
const Vector<T>&,
const unsigned int,
348 const unsigned int,
const unsigned int);
352 template <
unsigned long dim>
393 template <
typename T>
401 template <
typename T>
408 template <
typename T>
416 mat[
i] = ptr; ptr +=
col;
424 const unsigned int ROW =
row;
425 const unsigned int COL =
col;
428 for (
REGISTER unsigned i = 0; i < ROW; ++
i)
429 MAT[i] = VEC + i*COL;
430 endvec = VEC + ROW*COL;
438 dim = 0; row = 0;
col = 0;
439 mat = (
T**)0; vec = (
T*)0; endvec = (
T*)0;
444 template <
typename T>
446 : dim (((unsigned long)d)*d), row(d),
col(d),
freeable (0),
447 mat (d>0?
NEW(Tptr,d):0), vec (d>0?
NEW(
T,d*d):0)
453 template <
typename T>
455 : dim(((unsigned long)r)*c), row (r),
col (c),
freeable (0),
456 mat (r*c>0?
NEW(Tptr,r):0), vec (r*c>0?
NEW(
T,r*c):0)
461 template <
typename T>
463 : dim(((unsigned long)r)*c), row (r),
col (c),
freeable (0),
464 mat (r*c>0?
NEW(Tptr,r):0), vec (r*c>0?
NEW(
T,r*c):0)
471 template <
typename T>
476 vec (v.dim>0?
NEW(
T,v.dim):0)
483 template <
typename T>
485 : dim (m.dim), row (m.row),
col (m.
col),
487 mat (m.dim>0?
NEW(Tptr,m.row):0), vec (m.dim>0?
NEW(
T,m.dim):0)
495 template <
typename T>
498 mat (tm.mat), vec (tm.vec), endvec (tm.endvec)
503 template <
typename T>
508 dim = tm.dim; row = tm.row;
col = tm.col;
freeable = 0;
509 mat = tm.mat; vec = tm.vec; endvec = tm.endvec;
513 template <
typename T>
522 template <
typename T>
532 template <
typename T>
541 template <
typename T>
550 template <
typename T>
559 template <
typename T>
562 return this->
fill (val);
566 template <
typename T>
573 unsigned iend =
MIN(row,
col);
574 for (
REGISTER unsigned i = 0; i < iend; i++)
579 template <
typename T>
588 template <
typename T>
596 template <
typename T>
598 const unsigned int r,
const unsigned off)
605 template <
typename T>
610 const unsigned ROW =
row;
611 for (
REGISTER unsigned r = 0; r < ROW; ++r)
612 mat[r][c] = v.
vec[r];
615 template <
typename T>
617 const unsigned int c,
const unsigned off)
621 const unsigned ROW = v.
dim;
622 for (
REGISTER unsigned r = 0; r < ROW; ++r)
623 mat[r+off][c] = v.
vec[r];
626 template <
typename T>
629 const unsigned ROW =
row;
632 for (
REGISTER unsigned j = 0; j < ROW; ++j)
633 v.
vec[j] = mat[j][c];
639 template <
typename T>
648 template <
typename T>
654 template <
typename T>
662 unsigned long dd =
dim;
663 dim = ((
unsigned long)r)*
c;
665 mat =
NEW(Tptr,r); vec =
NEW(
T,dim);
668 mat = (
T**)0; vec = (
T*)0; endvec = (
T*)0;
677 template <
typename T>
682 dim = ((
unsigned long)r)*
col;
691 mat = (
T**)0; vec = (
T*)0; endvec = (
T*)0;
695 for (
REGISTER unsigned i=0; i<r; i++) {
696 mat[
i] = ptr; ptr +=
col;
698 endvec = ptr; row = r;
700 dim = 0; row = 0;
col = 0;
701 mat = (
T**)0; vec = (
T*)0; endvec = (
T*)0;
706 template <
typename T>
711 unsigned long olddim =
dim;
717 mat =
NEW (Tptr, row);
721 mat[
i] = ptr; ptr +=
col;
726 vec[dim-m.
dim+l] = m.
vec[l];
738 mat = (
T**)0; vec = (
T*)0; endvec = (
T*)0;
739 dim = 0; row = 0;
col = 0;
744 template <
typename T>
755 dim = ((
unsigned long)r)*
c;
758 mat =
NEW(Tptr,r); vec =
NEW(
T,dim);
762 mat = (
T**)0; vec=(
T*)0; endvec =(
T*)0;
767 template <
typename T>
778 dim = ((
unsigned long)m.
row)*m.
col;
785 mat = (
T**)0; vec = (
T*)0; endvec = (
T*)0;
791 template <
typename T>
796 BCHK (nr > row,
MatErr, cheapdownsize does not upsize, nr, *
this);
799 dim = ((
unsigned long)nr) *
col;
800 row = nr; endvec = vec +
dim;
804 template <
typename T>
811 template <
typename T>
815 BCHK (row !=
col,
MatErr, Trace over non quadratic matrix, 0, tr=0);
821 template <
typename T>
832 template <
typename T>
839 template <
typename T>
853 template <
typename T>
872 for (
REGISTER T *p1 = vec, *p2 = ts.
vec; p1 < endvec; p1++, p2++)
873 if (
UNLIKELY(*p1 != *p2 * ts.fac)) {
881 template <
typename T>
889 template <
typename T>
898 template <
typename T>
906 template <
typename T>
916 template <
typename T>
919 template <
typename T>
923 template <
typename T>
931 template <
typename T>
941 template <
typename T>
947 template <
typename T>
954 template <
typename T>
958 template <
typename T>
966 template <
typename T>
969 template <
typename T>
973 template <
typename T>
976 template <
typename T>
980 template <
typename T>
983 template <
typename T>
987 template <
typename T>
991 template <
typename T>
1002 template <typename T>
1009 template <typename T>
1017 template <typename T>
1025 template <typename T>
1033 template <typename T>
1042 template <typename T>
1049 template <
typename T>
1053 template <
typename T>
1060 template <
typename T>
1064 for (
unsigned r = 0; r < this->
row; ++r)
1065 for (
unsigned c = 0;
c < this->
col; ++
c)
1066 trm.
mat[
c][r] = this->mat[r][
c];
1067 this->mark_destroy();
1072 template <typename T>
1078 template <
typename T>
1081 if (this->
col != this->row) {
1084 return this->
swap(transmat);
1087 for (
unsigned r = 1; r < this->
row; ++r)
1088 for (
unsigned c = 0;
c < r; ++
c)
1089 SWAP(this->mat[r][
c], this->mat[c][r]);
1099 template<typename T>
1101 {
return tm.fabs (); }
1104 template<typename T>
1106 {
return m.fabs (); }
1117 template <
typename T>
1130 void clone (
bool =
false, TMatrix<T>* = 0);
1131 #if 1 //def HAVE_GCC295_FRIEND_BUG
1134 void detach (TMatrix<T>* = 0);
1146 #if 1 //def HAVE_GCC295_FRIEND_BUG
1158 : vec(tm.vec), dim(tm.dim), row(tm.row), col(tm.col),
1159 mat(tm.mat), endvec(tm.endvec),
fac(f), mut(
true) {}
1161 : vec(m.vec), dim(m.dim), row(m.row), col(m.col),
1162 mat(m.mat), endvec(m.endvec),
fac(f), mut(
false) {}
1164 : vec(ts.vec), dim(ts.dim), row(ts.row), col(ts.col),
1165 mat(ts.mat), endvec(ts.endvec),
fac(ts.
fac), mut(ts.mut) {}
1168 static const char*
mat_info () {
return (
"TSMatrix"); }
1179 fac = ts.fac; mut = ts.
mut;
return *
this; }
1183 fac = (
T)1; mut =
true;
return *
this; }
1189 #ifndef HAVE_GCC295_FRIEND_BUG
1204 #ifndef HAVE_GCC295_FRIEND_BUG
1221 bool operator == (TMatrix<T>& tm) { Matrix<T> m(tm);
return (*
this == m); }
1230 template <
typename T>
1233 if (
LIKELY(!mut || !dim))
1238 template <
typename T>
1241 if (
LIKELY((!tm && mut) || !dim))
1244 vec =
NEW(
T, dim);
if (!vec) {
1245 dim = 0; mat = (
T**)0;
return;
1247 mat =
NEW(Tptr,row);
if (!mat) {
1251 mat[r] = vec + r*col;
1259 template <
typename T>
1262 if (
LIKELY((mut && !tm)|| !dim))
1264 T* oldv =
vec;
T** oldm =
mat;
bool omut = mut;
1266 if (evl &&
fac != (
T)1) {
1279 template <
typename T>
1294 template <
typename T>
1300 ts.fac = (
T)1;
return TMatrix<T> (
ts);
1302 template <
typename T>
1308 ts.fac = (
T)1;
return TMatrix<T> (
ts);
1311 template <
typename T>
1318 template <
typename T>
1326 template <
typename T>
1334 tm = *
this; tm.
detach ();
1342 return TMatrix<T> (tm);
1345 template <
typename T>
1353 tm = *
this; tm.
detach ();
1361 return TMatrix<T> (tm);
1364 template <
typename T>
1369 ts.fac = (
T)1;
return TMatrix<T> (
ts);
1372 template <
typename T>
1377 ts.fac = (
T)1;
return TMatrix<T> (
ts);
1382 template <typename T>
1387 tm._FAC = (
T)1;
return TMatrix<T> (tm);
1390 template <typename T>
1395 tm._FAC = (
T)1;
return TMatrix<T> (tm);
1399 template <
typename T>
1402 TMatrix<T>
c (row, m.
col);
1406 for (
unsigned i=0; i<
row; i++) {
1407 for (
unsigned j=0; j<m.
col; j++) {
1408 tmp = mat[
i][0] * m.
mat[0][j];
1410 tmp += mat[i][l] * m.
mat[l][j];
1417 template <
typename T>
1420 TMatrix<T>
c (row, tm.
col);
1424 for (
unsigned i=0; i<
row; i++) {
1425 for (
unsigned j=0; j<tm.
col; j++) {
1426 tmp = mat[
i][0] * tm.
get(0,j);
1428 tmp += mat[i][l] * tm.
get(l,j);
1435 template <
typename T>
1441 for (
unsigned r=0; r<
row; r++) {
1444 el += mat[r][
c] * v(
c);
1450 template <
typename T>
1456 for (
unsigned r=0; r<
row; r++) {
1459 el += mat[r][
c] * v.
get(
c);
1466 template <
typename T>
1484 for (
REGISTER T *p1 = vec, *p2 = m.
vec; p1 < endvec; p1++, p2++)
1492 template <
typename T>
1513 for (
REGISTER T *p1 = vec, *p2 = ts.
vec; p1 < endvec; p1++, p2++)
1524 template <typename T>
1526 { ts._FAC = f * ts._FAC;
return ts; }
1529 template <
typename T>
1534 do_vec_fabssqr_exact (dim, vec, res);
1536 do_vec_fabssqr_quick (dim, vec, res);
1542 template <
typename T>
1553 template <typename T>
1555 {
return ts.fabs (); }
1573 template <
typename T>
1593 Matrix (
const T& v,
const unsigned r,
const unsigned c)
1616 #ifndef HAVE_PROMOTION_BUG
1618 template <
typename U>
1622 template <
typename U>
1630 typename tbci_traits<T>::const_refval_type
1647 Matrix<T>&
resize (
const unsigned r,
const unsigned c)
1650 {
return this->
resize (d, d); }
1651 Matrix<T>&
resize (
const T& v,
const unsigned r,
const unsigned c)
1696 TMatrix<T>
operator + (
const Matrix<T>&)
const;
1697 TMatrix<T>
operator - (
const Matrix<T>&)
const;
1698 TMatrix<T>
operator * (
const Matrix<T>&)
const;
1721 bool operator == (const
Matrix<
T>& m) const;
1723 {
return !(*
this == m); }
1726 { Matrix<T> m (tm);
return (*
this == m); }
1728 {
return !(*
this == tm); }
1732 {
return !(*
this ==
ts); }
1737 Matrix<T>&
mult_row (
const T&,
const unsigned);
1738 Matrix<T>&
div_row (
const T&,
const unsigned);
1742 friend STD__ ostream& operator << FGDT (STD__ ostream&, const Matrix<T>&);
1743 friend STD__ istream&
operator >>
FGDT (
STD__ istream&, Matrix<T>&);
1758 template <
typename T>
1765 unsigned int tcol = this->
col; this->col = m.
col; m.
col = tcol;
1773 template <
typename T>
1774 inline typename tbci_traits<T>::const_refval_type
1778 EXPCHK(j>=this->col,
MatErr, illegal col index, j, this->mat[0][0]);
1779 return this->mat[
i][j];
1782 template <
typename T>
1786 EXPCHK(j>=this->col,
MatErr, illegal col index, j, this->mat[0][0]);
1787 return this->mat[
i][j];
1790 template <
typename T>
1800 template <
typename T>
1801 STD__ ostream& operator << (STD__ ostream& os, const Matrix<T>& m)
1803 for (
unsigned r=0; r<m.row; r++)
1808 for (
unsigned c=0;
c<m.col;
c++)
1809 os << m(r,
c) <<
" ";
1816 template <
typename T>
1821 for (
unsigned i=0; i<m.
row; i++)
1829 INST(template <typename T>
class TMatrix friend STD__ ostream&
operator << (
STD__ ostream& os, TMatrix<T> tm);)
1830 template <typename T>
1837 INST(template <typename T>
class TSMatrix friend STD__ ostream&
operator << (
STD__ ostream& os,
const TSMatrix<T>& ts);)
1838 template <typename T>
1839 STD__ ostream&
operator << (
STD__ ostream& os,
const TSMatrix<T>& ts)
1841 return os << Matrix<T>(
ts);
1848 template <
typename T>
1854 template <
typename T>
1863 template <
typename T>
1866 TMatrix<T> t (this->row, this->col);
1870 template <
typename T>
1873 TMatrix<T> t (this->row, this->col);
1878 template <
typename T>
1880 {
return TSMatrix<T> (*
this,
a); }
1882 template <
typename T>
1885 BCHK (a==(
T)0,
MatErr, Divide Mat by 0, 0, *
this);
1886 return TSMatrix<T> (*
this, (
T)1/a);
1889 template <
typename T>
1892 TMatrix<T> t (this->row, this->col);
1897 template <
typename T>
1900 TMatrix<T> t (this->row, this->col);
1906 template <
typename T>
1913 template <
typename T>
1921 template <
typename T>
1925 TSMatrix<T> tm (ts); tm.
detach ();
1927 tm.fac = (
T)1;
return TMatrix<T> (tm);
1929 template <
typename T>
1933 TSMatrix<T> tm (ts); tm.
detach ();
1935 tm.fac = (
T)1;
return TMatrix<T> (tm);
1939 #define COST_MATMAT_OLD(ra,ca,cb) (ra*cb*(COST_UNIT_STORE+COST_LOOP \
1940 +ca*(3*COST_UNIT_LOAD+COST_NU_LOAD+COST_MULT+COST_ADD+COST_LOOP)))
1941 #define COST_MATMAT_NEW(ra,ca,cb) (ra*cb*COST_MEMSET+ra*ca*(2*COST_UNIT_LOAD+COST_LOOP \
1942 +cb*(3*COST_UNIT_LOAD+COST_UNIT_STORE+COST_ADD+COST_MULT+COST_LOOP)))
1943 #ifdef OLD_MAT_MAT_MULT
1944 # define COST_MATMAT(ra,ca,cb) COST_MATMAT_OLD(ra,ca,cb)
1946 # define COST_MATMAT(ra,ca,cb) COST_MATMAT_NEW(ra,ca,cb)
1950 INST(template <typename T>
class TMatrix friend void do_mat_mat_mult (
const unsigned,
const unsigned, \
1951 TMatrix<T> *,
const Matrix<T> *,
const Matrix<T> *);)
1953 #include
"matrix_kernels.h"
1955 #
if defined(SMP) && !defined(NOSMP_MATVEC)
1957 template <typename
T>
1958 inline
void job_mat_mat_mult (struct
thr_ctrl *tc)
1960 do_mat_mat_mult (tc->t_off, tc->t_size,
1961 (TMatrix<T>*)(tc->t_par[0]), (
const Matrix<T>*)(tc->t_par[1]),
1962 (
const Matrix<T>*)(tc->t_par[2]));
1965 template <
typename T>
1968 TMatrix<T>
c(this->row, b.
col);
1971 update_n_thr(n_thr);
1972 #ifndef OLD_MAT_MAT_MULT
1977 do_mat_mat_mult<T> (0UL, this->
row, &
c,
this, &
b);
1982 const unsigned long first = slice_offset(1, n_thr, this->row, (
T*)0);
1983 unsigned long st, en = first;
1985 for (
unsigned t = 0; t < n_thr-1; ++t) {
1986 st = en; en = slice_offset(t+2, n_thr, this->row, (
T*)0);
1988 st, en, &c,
this, &b, (
void*)0);
1991 do_mat_mat_mult<T> (0UL, first, &
c,
this, &
b);
1994 for (
unsigned s = 0; s < n_thr-1; ++s)
2002 template <
typename T>
2005 TMatrix<T>
c(this->row, b.
col);
2006 #ifndef OLD_MAT_MAT_MULT
2010 do_mat_mat_mult <T> (0, this->
row, &
c,
this, &
b);
2015 template <
typename T>
2018 TMatrix<T>
c(this->row, a.
col);
2021 for (
unsigned i=0; i<this->
row; i++) {
2022 for (
unsigned j=0; j<a.
col; j++) {
2023 tmp = this->mat[
i][0] * a.
mat[0][j];
2025 tmp += this->mat[i][l] * a.
mat[l][j];
2026 c.
mat[i][j] = tmp * a.fac;
2033 template <
typename T>
2036 TMatrix<T>
c (this->
operator * (Matrix<T> (a)));
2040 template <
typename T>
2043 Matrix<T> m (*
this);
2048 template <
typename T>
2051 Matrix<T> m (*
this);
2052 return (m * (Matrix<T> (a)));
2055 template <
typename T>
2058 Matrix<T> m (*
this);
2063 template <
typename T>
2068 if (
LIKELY(this->vec == m.
vec || this->dim == 0))
2076 template <
typename T>
2079 if (
LIKELY(this->row != ts.
row || this->col != ts.
col))
2081 if (
LIKELY(this->dim == 0)) {
2093 for (
REGISTER T *p1 = this->vec, *p2 = ts.
vec; p1 < this->endvec; p1++, p2++)
2094 if (
UNLIKELY(*p1 != *p2 * ts.fac)) {
2101 template <
typename T>
2106 do_vec_fabssqr_exact (this->dim, this->vec, res);
2108 do_vec_fabssqr_quick (this->dim, this->vec, res);
2112 template <
typename T>
2123 template<
typename T>
2127 return (*
this * cn);
2131 #if defined(SMP) && !defined(NOSMP_MATVEC)
2133 template <
typename T>
2141 template <
typename T>
2150 template <
typename T>
2156 update_n_thr(n_thr);
2160 do_mat_vec_mult<T> (0, this->row, &tv,
this, &v);
2165 const unsigned long first = slice_offset(1, n_thr, this->row, (
T*)0);
2166 unsigned long st, en = first;
2168 for (
unsigned t = 0; t < n_thr-1; ++t) {
2169 st = en; en = slice_offset(t+2, n_thr, this->row, (
T*)0);
2171 st, en, &tv,
this, &v, (
void*)0);
2174 do_mat_vec_mult<T> (0, first, &tv,
this, &v);
2177 for (
unsigned s = 0; s < n_thr-1; ++s)
2183 template<
typename T>
2188 update_n_thr(n_thr);
2191 do_mat_vec_transmult<T> (0, this->col, &tv,
this, &v);
2194 const unsigned long first = slice_offset(1, n_thr, this->col, (
T*)0);
2196 unsigned long st, en = first;
2197 for (
unsigned t = 0; t < n_thr-1; ++t) {
2201 st = en; en = slice_offset(t+2, n_thr, this->col, (
T*)0);
2203 st, en, &tv,
this, &v, (
void*)0);
2205 do_mat_vec_transmult<T> (0, first, &tv,
this, &v);
2207 for (
unsigned s = 0; s < n_thr-1; ++s)
2215 template <
typename T>
2224 template<
typename T>
2229 do_mat_vec_transmult<T> (0, this->
col, &tv,
this, &v);
2237 template <
typename T>
2247 template <
typename T>
2260 template <
typename T>
2263 BCHK (this->row != v.
size(), MatErr, multiply
rows: wrong sizes, v.
size(), *
this);
2264 for (
unsigned r = 0; r < this->
row; r++)
2267 for (
unsigned c = 0;
c < this->
col;
c++)
2268 this->
operator() (r,
c) *= fac;
2273 template <
typename T>
2276 BCHK (this->row != v.
size(), MatErr, multiply
rows: wrong sizes, v.
size(), *
this);
2277 for (
unsigned r = 0; r < this->
row; r++)
2279 T fac = (
T)1.0 / v (r);
2280 for (
unsigned c = 0;
c < this->
col;
c++)
2281 (*
this) (r,
c) *= fac;
2286 template <
typename T>
2289 BCHK (r>this->row, MatErr, mult_row: wrong
index, r, *
this);
2290 for (
unsigned c = 0;
c < this->
col;
c++)
2291 (*
this) (r,
c) *= f;
2295 template <
typename T>
2298 BCHK (r>this->row, MatErr, mult_row: wrong
index, r, *
this);
2299 BCHK (d == (
T)0, MatErr, div_row by zero, r, *
this);
2301 for (
unsigned c = 0;
c < this->
col;
c++)
2302 (*
this) (r,
c) *= f;
2309 template <
typename T>
2313 const TMatrix<T> & tmat;
2320 Mat_Brack (
const TMatrix<T> &tm,
unsigned int ix) : tmat(tm), idx (ix) {}
2325 {
EXPCHK(j >= tmat.
col, MatErr, Idx2 out of range, j, tmat.
mat[idx][0]);
2326 return tmat.
mat[idx][j]; }
2328 {
EXPCHK(j >= tmat.
col, MatErr, Idx2 out of range, j, tmat.
mat[idx][0]);
2329 return tmat.
mat[idx][j]; }
2332 template <
typename T>
2335 this->vec = mb.tmat.mat[mb.idx];
2336 this->dim = (
unsigned long)mb.tmat.col; this->keep =
true;
2339 template <
typename T>
2342 this->dim = (
unsigned long)mb.tmat.col;
2345 this->vec =
NEW (
T, this->dim);
2347 TBCICOPY (this->vec, mb.tmat.mat[mb.idx],
T, this->dim);
2351 this->dim = 0; this->vec = (
T*)0;
2353 #ifndef TBCI_NEW_BRACKET
2354 template <
typename T>
2361 template <
typename T>
2364 EXPCHK(ix >= row, MatErr, Idx1 out of range, ix, (
T*)0);
2378 #if defined(__GNUC__) && __GNUC__ >= 5 && __GNUC__ < 8 && (defined(__x86_64__) || defined(__i386__))
2379 # define NOFMA __attribute__((target("no-fma")))
2387 template <typename
T>
2390 T (*fn)(
const unsigned i1,
const unsigned i2,
void* par);
2395 template <
typename T>
2399 const unsigned cols = mat->
columns();
2401 for (
unsigned r = firstrow; r < lastrow; ++r)
2403 (*mat)(r,
c) = fn.
fn(r,
c, par);
2407 template <
typename T>
2411 (Matrix<T>*)(tc->
t_par[0]),
2416 template <
typename T>
2422 update_n_thr(n_thr);
2425 do_fill_mat<T> (0,
rows, &
mat, fn, par);
2428 const unsigned long first = slice_offset(1, n_thr, rows, (
T*)0);
2429 unsigned long st, en = first;
2431 for (
unsigned t = 0; t < n_thr-1; ++t) {
2432 st = en; en = slice_offset(t+2, n_thr, rows, (
T*)0);
2434 st, en, &mat, (
void*)&fn, par, (
void*)0);
2437 do_fill_mat<T> (0, first, &
mat, fn, par);
2440 for (
unsigned s = 0; s < n_thr-1; ++s)
2446 template <
typename T>
2449 do_fill_mat<T> (0, mat.
rows(), &
mat, fn, par);
2453 #if defined(SMP) && defined(HAVE_LIBNUMA)
2454 template <
typename T>
2472 const unsigned first = slice_offset(1, n_thr, rows, (
T*)0);
2473 unsigned long st, en = first;
2475 unsigned long res = 0;
2476 for (
unsigned t = 0; t < n_thr-1; ++t) {
2477 st = en; en = slice_offset(t+2, n_thr, rows, (
T*)0);
2489 for (
unsigned t = 0; t < n_thr-1; ++t) {
2499 template <
typename T>
#define TBCICOPY(n, o, t, s)
TMatrix< T > & operator+=(TMatrix< T >)
arithmetics ...
TVector< T > transMult(const Vector_Sig< T > &) const
TSMatrix< T > & operator/(const T &f)
TVector< T > get_col(const unsigned int) const
Column vector.
TMatrix< T > operator+(const Matrix< T > &)
T & setval(const T &val, const unsigned int r, const unsigned int c)
#define BCHKNR(cond, exc, txt, ind)
void do_mat_tsv_mult(const unsigned start, const unsigned end, TVector< T > *res, const Matrix< T > *mat, const TSVector< T > *vec)
TMatrix< T > transposed_copy() const
Inefficient! Use transMult if possible.
#define STD_SMP_TEMPLATE3VV(oper, dm, a1, a2, a3)
void do_mat_vec_mult(const unsigned start, const unsigned end, TVector< T > *res, const Matrix< T > *mat, const Vector< T > *vec)
STD__ istream & operator>>(STD__ istream &in, Matrix< T > &m)
TSMatrix< T > & eval(TMatrix< T > *=0)
TMatrix< T > operator*(const Matrix< T > &) const
unsigned long size() const
void job_mat_vec_transmult(struct thr_ctrl *tc)
unsigned int columns() const
number of columns
Matrix< T > & fill(const T &v=(T) 0)
double fabssqr(const TMatrix< T > &tm)
const T & operator[](unsigned int j) const
The class BdMatrix is an implementation to store and do operations on sparse Matrices with a band str...
TMatrix< T > & transpose()
INST(template< typename T > class TMatrix friend void do_mat_mat_mult(const unsigned, const unsigned, TMatrix< T > *, const Matrix< T > *, const Matrix< T > *);) template< typename T > inline void job_mat_mat_mult(struct thr_ctrl *tc)
TSMatrix< T > & operator*(const T &f)
bool operator!=(const Matrix< T > &m)
Matrix< T > & operator+=(const Matrix< T > &a)
Matrix< T > & operator/=(const T &a)
TMatrix< T > & operator-=(TMatrix< T >)
mat_fill_fn(T(*f)(const unsigned, const unsigned, void *))
Matrix< T > & fill(const Vector< T > &v)
Matrix(const unsigned d=0)
TSMatrix(const Matrix< T > &m, const T &f=(T) 1)
T & setval(const unsigned r, const unsigned c)
exception base class for the TBCI NumLib
F_TMatrix< double > lu_solve(const F_Matrix< double > &A, const F_Matrix< double > &B, int overwriteA=0)
unsigned int rows() const
NAMESPACE_TBCI char gaussj(Matrix< T > &a, Matrix< T > &b)
Common interface definition (signature) for all Matrices.
const unsigned TMatrix< T > * res
NAMESPACE_END NAMESPACE_CSTD double fabs(const TBCI__ TMatrix< T > &tm)
TSMatrix< T > operator/(const T &)
Matrix< T > & mult_row(const T &, const unsigned)
Matrix< T > & resize(const Matrix< T > &m)
Matrix< T > & resize(const unsigned d)
static const char * mat_info()
Implementation of fixed sized Vectors (template argument) which is favorable for small Vectors...
#define STD_SMP_TEMPLATE5(oper, dm, a1, a2, a3, a4, a5)
#define BCHK(cond, exc, txt, ind, rtval)
void par_fill(Matrix< T > &mat, mat_fill_fn< T > fn, void *par)
tbci_traits< T >::const_refval_type operator()(const unsigned int, const unsigned int) const HOT
ro element access
Matrix< T > & mult_rows(const Vector< T > &)
Elementwise ops.
TSMatrix< T > operator*(const T &)
TMatrix< T > & operator+(TMatrix< T >)
#define NAMESPACE_CSTD_END
#define TBCIFILL(n, v, t, s)
BVector< T > & bvfillm(BVector< T > &bv, const Matrix< T > &m)
TMatrix(const Matrix< U > &m)
Matrix(const Matrix< U > &m)
#define REALLOC(v, os, t, s)
TMatrix< T > & operator-()
void(* thr_job_t)(struct thr_ctrl *)
Before the double inclusion guard on purpose!
TMatrix< T > operator-() const
void detach(TMatrix< T > *=0)
TMatrix< T > LU_invert(const BdMatrix< T > &lu)
return the inverse Matrix for an already LU decomposed BdMatrix
TVector< T > get_row(const unsigned int) const
Row vector.
#define STD_SMP_TEMPLATE4C(oper, dm, a1, a2, a3, a4)
Matrix(const Matrix< T > &m)
copy, does a real copy, as TM(M) is invoked (not TM(TM))
T aligned_value_type TALIGN(MIN_ALIGN2)
TSMatrix< T > & operator-()
void set_col(const Vector< T > &, const unsigned int)
Fill complete column.
TMatrix< T > operator+(const Matrix< T > &) const
TSMatrix(const TMatrix< T > &tm, const T &f=(T) 1)
Matrix< T > & div_rows(const Vector< T > &)
void set_row(const Vector< T > &, const unsigned int)
Fill complete row.
void job_fill_mat(struct thr_ctrl *tc)
struct thr_struct * threads
int lu_decomp(Matrix< T > &) HOT
LU decomposes the TBCI::Matrix mat.
void clone(bool=false, TMatrix< T > *=0)
void job_mat_vec_mult(struct thr_ctrl *tc)
TSMatrix< T > & operator=(const TSMatrix< T > &ts)
#define PREFETCH_R(addr, loc)
In case gcc does not yet support __builtin_prefetch(), we have handcoded assembly with gcc for a few ...
void real_destroy()
real destructor
T(* fn)(const unsigned i1, const unsigned i2, void *par)
T & set(const T &val, const unsigned r, const unsigned c)
friend NOINST TMatrix< T > LU_solve FGD(const BdMatrix< T > &, const Matrix< T > &)
Matrix(const TSMatrix< T > &ts)
TSMatrix< T > operator*=(const T &)
TMatrix< T > & row_expand(const unsigned int r)
Set new numbers of rows to matrix (expansion only)
Tensor class including arithmetics.
bool operator==(const Matrix< T > &m) const
Matrix< T > & resize(const T &v, const unsigned r, const unsigned c)
TVector(const unsigned long d=0)
void set_col_partial(const Vector< T > &, const unsigned int, const unsigned int)
Fill partial column.
bool operator==(const Matrix< T > &m)
Comparison.
unsigned long size() const
Matrix(const unsigned r, const unsigned c)
long int Vector< T > & index
Matrix(const TMatrix< U > &tm)
Matrix< T > & resize(const unsigned r, const unsigned c)
Temporary Base Class Idiom: Class TVector is used for temporary variables.
void do_mat_vec_transmult(const unsigned start, const unsigned end, TVector< T > *res, const Matrix< T > *mat, const Vector< T > *vec)
TMatrix< T > & cheapdownsizerow(const unsigned)
Resize number of rows without actually freeing memory (efficiency)
tbci_traits< T >::const_refval_type get(const unsigned long i) const
static const char * mat_info()
Matrix< T > & operator-=(const Matrix< T > &a)
TSMatrix(const TSMatrix< T > &ts)
TSMatrix< T > operator/=(const T &)
TMatrix< T > transpose(const TMatrix< T > &tm)
int numa_optimize(const Matrix< T > &m, bool fault_in)
void do_fill_mat(const unsigned firstrow, const unsigned lastrow, Matrix< T > *mat, mat_fill_fn< T > fn, void *par)
TMatrix(const TMatrix< U > &tm)
Mat_Brack< T > operator[](const unsigned int i) const
T aligned_value_type TALIGN(MIN_ALIGN2)
void SWAP(T &a, T &b)
SWAP function Note: We could implement a swap function without temporaries: a -= b b += a a -= b a = ...
unsigned long size() const
#define STD_SMP_TEMPLATE2C(oper, dm, a1, a2)
MatErr(const char *t, const long i=0)
unsigned int rows() const
number of rows
Matrix(const TMatrix< T > &tm) HOT
alias
#define PREFETCH_W(addr, loc)
TMatrix< T > & resize(const unsigned int, const unsigned int)
Resize Matrix, specifying rows and columns.
#define EXPCHK(cond, exc, txt, ind, rtval)
bool operator==(const Matrix< T > &)
bool operator!=(const Matrix< T > &m)
TSMatrix< T > & operator*=(const T &f)
TSMatrix< T > operator/(const T &) const
T operator()(const unsigned int r, const unsigned int c) HOT
#define STD_SMP_TEMPLATE2V(oper, dm, a1, a2)
#define TBCICLEAR(n, t, s)
#define TBCIDELETE(t, v, sz)
T * getrowptr(const unsigned r)
TMatrix< T > & alias(const TMatrix< T > &m)
void set_row_partial(const Vector< T > &, const unsigned int, const unsigned int)
Fill partial row.
void thread_wait(const int thr_no, struct job_output *out)
unsigned long size() const
number of elements
Matrix< T > & operator*=(const T &a)
friend BVector< T > &FRIEND_TBCI2__ bvfillm FGD(BVector< T > &, const Matrix< T > &m)
TMatrix< T > & clear()
Clear matrix (fill with 0)
#define STD_SMP_TEMPLATE3VC(oper, dm, a1, a2, a3)
Matrix< T > & operator=(const Matrix< T > &m)
Assignment, non-resizing.
const T * getrowptr(const unsigned r) const
Helpers for matvecmul.
static const char * mat_info()
Vector(const unsigned long d=0)
Matrix< T > & div_row(const T &, const unsigned)
T ** mat
C storage layout: mat[row][col].
TVector< T > LU_solve(const BdMatrix< T > &lu, const Vector< T > &b)
Solve the equation Ax = b where A IS already LU decomposed.
const T & getcref(const unsigned r, const unsigned c) const
#define TBCICOMP(n, o, t, s)
TMatrix< T > & resize(const unsigned int d)
Resize Matrix to square shape.
void mark_destroy() const
mark destructible
Matrix(const T &v, const unsigned r, const unsigned c)
T operator()(const unsigned int, const unsigned int) const HOT
Element access (desctructive for TMatrix!)
TSMatrix< T > & operator/=(const T &f)
unsigned int do_exactsum()
bool operator!=(const Matrix< T > &m) const
Matrix< T > & setunit(const T &f=(T) 1)
TMatrix< T > & setunit(const T &=(T) 1)
Set to unit matrix (optionally scaled)
const unsigned TMatrix< T > const Matrix< T > * a
tbci_traits< T >::const_refval_type get(const unsigned r, const unsigned c) const
get, set and getcref are used internally and not for public consumption
T & set(const T &val, const unsigned long i) const
void thread_start_off(const int thr_no, thr_job_t job, const unsigned long off, const unsigned long sz,...)
T aligned_value_type TALIGN(MIN_ALIGN2)
Matrix(const Vector< T > &v, const enum rowcolvec r=colvec)
TMatrix< T > lu_invert(BdMatrix< T > &mat)
return the inverse Matrix for a BdMatrix by doing an LU decomposition
TMatrix< T > & swap(TMatrix< T > &)
TMatrix< T > & operator=(const Matrix< T > &) HOT
assignment, non-resizing
#define LIKELY(expr)
branch prediction note that we sometimes on purpose mark the unlikely possibility likely and vice ver...
double fabssqr() const
Sum over all squared elements.
TVector< T > transMult(const Vector< T > &) const HOT
TMatrix< T > & fill(const T &=(T) 0)
Fill matrix.
#define STD_SMP_TEMPLATE4V(oper, dm, a1, a2, a3, a4)