14#ifndef TBCI_F_MATRIX_H
15#define TBCI_F_MATRIX_H
17#include "tbci/vector.h"
18#include "tbci/matrix_sig.h"
19#include "tbci/matrix.h"
20#include "tbci/cscmatrix.h"
23#if !defined(NO_GD) && !defined(AUTO_DECL)
24# include "tbci/f_matrix_gd.h"
29#ifdef HAVE_GCC295_FRIEND_BUG
31# define _ENDVEC getendvec()
34# define _COL columns()
38# define _ENDVEC endvec
53template <
typename T>
class Tensor;
55template <
typename T>
class Matrix;
56template <
typename T>
class TMatrix;
59# pragma interface "f_matrix.h"
92#ifdef HAVE_GCC295_FRIEND_BUG
94 T*
const getvec()
const {
return vec; }
95 T*
const getendvec()
const {
return endvec; }
101 F_TMatrix (
const unsigned,
const unsigned);
102 F_TMatrix (
const T&,
const unsigned,
const unsigned);
119 static const char*
mat_info () {
return (
"F_TMatrix"); }
169#ifndef HAVE_GCC295_FRIEND_BUG
179 T&
operator () (
const unsigned int,
const unsigned int)
const;
182 const T&
get (
const unsigned r,
const unsigned c)
const {
return mat[
c][r]; }
186 {
return !(*
this == m); }
191 {
return !(*
this == tm); }
195 {
return !(*
this ==
ts); }
206 void setval(
const T val,
const unsigned int r,
const unsigned int c)
208 T&
setval(
const unsigned int r,
const unsigned int c)
225 double fabs ()
const;
246 for (
unsigned int c = 0;
c <
col; ++
c)
247 for (
unsigned int r = 0; r <
row; ++r)
258 for (
unsigned int i=0;
i<
col;
i++) {
276 :
dim((unsigned long)d*d),
col(d),
row(d),
360 BCHK(
dim !=
a.dim,
MatErr, Different sizes in assignment,
a.dim, *
this );
391 return this->
fill (val);
460# define LAPACK_INLINE
470 unsigned long dd =
dim;
471 dim = ((
unsigned long)r)*
c;
534 for(
unsigned int i=0;
i<
rows();
i++)
535 for(
unsigned int j=
i+1; j<
columns(); j++)
539 STD__ cout <<
"Not implemented!" <<
STD__ endl;
548 STD__ cout <<
"WRONG: F_TMatrix<T>::herm()" <<
STD__ endl;
551 for(
unsigned int i=0;
i<
rows();
i++)
554 for(
unsigned int j=
i+1; j<
columns(); j++)
563 STD__ cout <<
"Not implemented!" <<
STD__ endl;
572 STD__ cout <<
"Wrong: F_TMatrix<T>::conf()" <<
STD__ endl;
590 if (
vec == m.
vec ||
dim == 0)
return true;
599 if (
row !=
ts.row ||
col !=
ts.col)
return (
ts.destroy(),
false);
600 if (
dim == 0) {
return true; }
603 if (
vec ==
ts.vec)
return true;
608 if (
vec ==
ts.vec)
return false;
610 if (*p1 != *p2 *
ts.fac) {
ts.destroy ();
return false; }
612 ts.destroy ();
return true;
616#define F_TMFORALL_M(op) \
617template <typename T> \
618inline F_TMatrix<T>& F_TMatrix<T>::operator op (const F_Matrix<T>& a) \
620 BCHK(row != a.row, MatErr, number of rows differ in op, a.row, *this ); \
621 BCHK(col != a.col, MatErr, number of cols differ in op, a.col, *this ); \
622 for (REGISTER T *p1 = vec, *p2 = a.vec; p1 < endvec; p1++, p2++) *p1 op *p2; \
631#define F_TMFORALL_TM(op) \
632template <typename T> \
633inline F_TMatrix<T>& F_TMatrix<T>::operator op (F_TMatrix<T> a)\
635 return this->operator op (F_Matrix<T>(a)); \
642#define F_TMFORALL_TS(op) \
643template <typename T> \
644inline F_TMatrix<T>& F_TMatrix<T>::operator op (F_TSMatrix<T> ts) \
646 BCHK(row != ts.row, MatErr, number of rows differ in op, ts.row, (ts.destroy(),*this) ); \
647 BCHK(col != ts.col, MatErr, number of cols differ in op, ts.col, (ts.destroy(),*this) ); \
648 for (REGISTER T *p1 = vec, *p2 = ts.vec; p1 < endvec; p1++, p2++) \
649 *p1 op##= *p2 * ts.fac; \
650 ts.destroy (); return *this; \
658#define F_TMFORALL_T(op) \
659template <typename T> \
660inline F_TMatrix<T>& F_TMatrix<T>::operator op (const T& a) \
662 for (REGISTER T* ptr = vec; ptr < endvec; ptr++) *ptr op a; \
685#define F_STDDEF_TMM(op) \
686template <typename T> \
687inline F_TMatrix<T>& F_TMatrix<T>::operator op (const F_Matrix<T>& a) \
688{ return this->operator op##= (a); }
694#define F_STDDEF_TMTM(op) \
695template <typename T> \
696inline F_TMatrix<T>& F_TMatrix<T>::operator op (F_TMatrix<T> a) \
697{ return this->operator op##= (F_Matrix<T>(a)); }
703#define F_STDDEF_TMT(op) \
704template <typename T> \
705inline F_TMatrix<T>& F_TMatrix<T>::operator op (const T& a) \
706{ return this->operator op##= (a); }
726#define F_STDDEF_TTM(op) \
727INST(template <typename T> class F_TMatrix friend F_TMatrix<T> operator op (const T&, F_TMatrix<T>);) \
728template <typename T> \
729inline F_TMatrix<T> operator op (const T& a, F_TMatrix<T> b) \
731 for (REGISTER T* ptr=b._VEC; ptr<b._ENDVEC; ptr++) \
749#define F_STDDEF_TM(op) \
750INST(template <typename T> class F_TMatrix friend F_TMatrix<T> operator op (const T&, const F_Matrix<T>&);) \
751template <typename T> \
752inline F_TMatrix<T> operator op (const T& a, const F_Matrix<T>& b) \
754 F_TMatrix<T> c (b._ROW, b._COL); \
755 for (REGISTER T *p1=c._VEC, *p2=b._VEC; p1<c._ENDVEC; p1++, p2++) \
796#ifdef HAVE_GCC295_FRIEND_BUG
798 T*
const getvec ()
const {
return vec; }
799 T*
const getendvec ()
const {
return endvec; }
800 T& getfac () {
return fac; }
801 const T& getfac ()
const {
return fac; }
829 T get (
const unsigned r,
const unsigned c)
const {
return mat[
c][r] *
fac; }
835 static const char*
mat_info () {
return (
"F_TSMatrix"); }
845 fac = (
T)1;
mut =
true;
return *
this; }
864#ifndef HAVE_GCC295_FRIEND_BUG
895 for (
unsigned int i=0;
i<m1.
rows();
i++)
896 for (
unsigned int j=0; j<m2.
columns(); j++)
900 for (
unsigned int x=m2.
pcol[j];
x<m2.
pcol[j+1];
x++)
925 if ((!tm &&
mut) || !
dim)
return;
943 if ((
mut && !tm)|| !
dim)
return;
946 if (evl &&
fac != (
T)1)
980#define F_STDDEF_TSM(op) \
981template <typename T> \
982inline F_TMatrix<T> F_TSMatrix<T>::operator op (const F_Matrix<T>& m) \
984 BCHK(row != m.row || col != m.col, MatErr, Op op on diff size Mats, m.row, F_TMatrix<T> (*this)); \
985 F_TSMatrix<T> ts (*this); ts.detach (); \
986 for (REGISTER T *p1 = ts.vec, *p2 = vec, *p3 = m.vec; p2 < endvec; p1++, p2++, p3++) \
987 *p1 = *p2 * fac op *p3; \
988 ts.fac = (T)1; return F_TMatrix<T> (ts); \
995#define F_STDDEF_TSTM(op) \
996template <typename T> \
997inline F_TMatrix<T> F_TSMatrix<T>::operator op (const F_TMatrix<T>& tm) \
999 BCHK(row != tm.row || col != tm.col, MatErr, Op op on diff size Mats, tm.row, tm); \
1000 for (REGISTER T *p1 = tm.vec, *p2 = vec; p2 < endvec; p1++, p2++) \
1001 *p1 = *p2 * fac op *p1; \
1002 destroy (); return tm; \
1009#define F_STDDEF_TSTS(op) \
1010template <typename T> \
1011inline F_TMatrix<T> F_TSMatrix<T>::operator op (F_TSMatrix<T> ts) \
1013 BCHK(row != ts.row || col != ts.col, MatErr, Op op on diff size Mats, ts.row, F_TMatrix<T> (*this)); \
1015 if (!mut && ts.mut) tm = ts; \
1016 else { tm = *this; tm.detach (); } \
1017 for (REGISTER T *p1 = tm.vec, *p2 = vec, *p3 = ts.vec; p2 < endvec; p1++, p2++, p3++) \
1018 *p1 = *p2 * fac op *p3 * ts.fac; \
1019 if (!mut && ts.mut) destroy (); else ts.destroy (); \
1020 tm.fac = (T)1; return F_TMatrix<T> (tm); \
1027#define F_STDDEF_TST(op) \
1028template <typename T> \
1029inline F_TMatrix<T> F_TSMatrix<T>::operator op (const T& a) \
1031 F_TSMatrix<T> ts (*this); ts.detach (); \
1032 for (REGISTER T *p1 = ts.vec, *p2 = vec; p2 < endvec; p1++, p2++) \
1033 *p1 = *p2 * fac op a; \
1034 ts.fac = (T)1; return F_TMatrix<T> (ts); \
1041#define F_STDDEF_TTS(op) \
1042INST(template <typename T> class F_TSMatrix friend F_TMatrix<T> operator op (const T&, F_TSMatrix<T>);) \
1043template <typename T> \
1044inline F_TMatrix<T> operator op (const T& a, F_TSMatrix<T> ts) \
1046 F_TSMatrix<T> tm (ts); tm.detach (); \
1047 for (REGISTER T *p1 = tm._VEC, *p2 = ts._VEC; p1 < tm._ENDVEC; p1++, p2++) \
1048 *p1 = a op *p2 * ts._FAC; \
1049 tm._FAC = (T)1; return F_TMatrix<T> (tm); \
1056template <typename
T>
1062 for (
unsigned int r=0; r<
row; r++) {
1063 for (
unsigned int c=0;
c<m.col;
c++) {
1064 tmp =
mat[0][r] * m(0,
c);
1066 tmp +=
mat[l][r] * m(l,
c);
1074template <
typename T>
1080 for (
unsigned int r=0; r<
row; r++)
1084 el +=
mat[
c][r] * v(
c);
1091template <
typename T>
1095 if (
dim == 0) {
return true; }
1098 if (
vec == m.
vec)
return true;
1103 if (
vec == m.
vec)
return false;
1105 if (*p1 *
fac != *p2) {
destroy ();
return false; }
1111template <
typename T>
1115 if (
dim == 0) {
return true; }
1125 if (*p1 *
fac != *p2) {
destroy ();
ts.destroy ();
return false; }
1130template <
typename T>
1134 for (
unsigned int c = 0;
c < this->col; ++
c)
1135 for (
unsigned int r = 0; r < this->row; ++r)
1142template <
typename T>
1147 for (; ptr <
endvec; ptr++)
1155template <typename T>
1157{
ts._FAC = f *
ts._FAC;
return ts; }
1170template <
typename T>
1204 static const char*
mat_info () {
return (
"F_Matrix"); }
1207 T&
operator () (
const unsigned int,
const unsigned int)
const;
1210 const T&
get (
const unsigned r,
const unsigned c)
const
1211 {
return this->mat[
c][r]; }
1213 unsigned int columns ()
const {
return this->row; }
1214 unsigned int rows ()
const {
return this->col; }
1215 unsigned long size ()
const {
return this->dim; }
1221 void setval(
const T val,
const unsigned int r,
const unsigned int c)
1237 {
return this->
resize (d, d); }
1293 {
return !(*
this == m); }
1298 {
return !(*
this == tm); }
1302 {
return !(*
this ==
ts); }
1315 double fabs ()
const;
1321template <
typename T>
1325 for (
unsigned int r=0; r<m.
rows(); r++)
1330template <
typename T>
1334 for (
unsigned int r = 0; r < this->row; ++r)
1335 for (
unsigned int c = 0;
c < this->col; ++
c)
1339template <
typename T>
1343 for (
unsigned int c = 0;
c < this->col; ++
c)
1344 for (
unsigned int r = 0; r < this->row; ++r)
1350template <
typename T>
1353 for (
unsigned int r=0; r<m.
row; r++) {
1354 for (
unsigned int c=0;
c<m.
col;
c++)
1355 os << m(r,
c) <<
" ";
1362template <typename T>
1371template <typename T>
1374 return os << F_Matrix<T>(
ts);
1378template <
typename T>
1383 for (
unsigned int i=0;
i<m.
col;
i++)
1395#define F_MSTDDEF_T(op) \
1396template <typename T> \
1397inline F_TMatrix<T> F_Matrix<T>::operator op (const T& a) const \
1399 F_TMatrix<T> t (this->row, this->col); \
1400 for (REGISTER T *p1=t.vec, *p2=this->vec; p2<this->endvec; ++p1, ++p2) \
1408template <typename
T>
1412template <
typename T>
1419#define F_MSTDDEF_M(op) \
1420template <typename T> \
1421inline F_TMatrix<T> F_Matrix<T>::operator op (const F_Matrix<T>& a) const \
1423 F_TMatrix<T> t (this->row, this->col); \
1424 BCHK(this->dim != a.dim, MatErr, Operator op on diff size matrices, a.dim, t); \
1425 for (REGISTER T *p1=t.vec, *p2=this->vec, *p3=a.vec; p2<this->endvec; ++p1, ++p2, ++p3) \
1434#define F_MSTDDEF_TM(op) \
1435template <typename T> \
1436inline F_TMatrix<T> F_Matrix<T>::operator op (F_TMatrix<T> a) const \
1438 BCHK(this->dim != a.dim, MatErr, Applying op on diff. size matrices, a.dim, a); \
1439 for (REGISTER T *p1=a.vec, *p2=this->vec; p2<this->endvec; ++p1, ++p2) \
1447#define F_MSTDDEF_TS(op) \
1448template <typename T> \
1449inline F_TMatrix<T> F_Matrix<T>::operator op (F_TSMatrix<T> ts) const \
1451 BCHK(this->dim != ts.dim, MatErr, Applying op on diff. size matrices, ts.dim, (ts.destroy(),F_TMatrix<T> (*this)) ); \
1452 F_TSMatrix<T> tm (ts); tm.detach (); \
1453 for (REGISTER T *p1=tm.vec, *p2=this->vec, *p3=ts.vec; p2<this->endvec; ++p1, ++p2, ++p3) \
1454 *p1 = *p2 op *p3 * ts.fac; \
1455 tm.fac = (T)1; return F_TMatrix<T> (tm); \
1463template <typename
T>
1468 for (
unsigned int r=0; r<this->row; ++r) {
1471 for (
REGISTER unsigned int l=1; l<this->col; ++l)
1472 tmp += (*
this)(r, l) *
a(l,
c);
1479template <
typename T>
1485 for (
unsigned int r=0; r<this->row; ++r) {
1487 tmp = (*this)(
c, 0) *
a.mat[
c][0];
1488 for (
REGISTER unsigned int l=1; l<this->col; ++l)
1489 tmp += (*
this)(r, l) *
a.mat[
c][l];
1493 a.destroy ();
return ftm;
1497template <
typename T>
1504template <
typename T>
1512template <
typename T>
1519template <
typename T>
1527template <
typename T>
1530 if (this->row != m.
row || this->col != m.
col)
1532 if (this->vec == m.
vec || this->dim == 0)
1540template <
typename T>
1543 if (this->row !=
ts.row || this->col !=
ts.col)
1544 return (
ts.destroy(),
false);
1545 if (this->vec == 0) {
1548 if (
ts.fac == (
T)1) {
1549 if (this->vec ==
ts.vec)
1552 ts.destroy ();
return false;
1555 if (this->vec ==
ts.vec)
1558 if (*p1 != *p2 *
ts.fac) {
1559 ts.destroy ();
return false;
1571 for (
unsigned int r=0; r<this->row; ++r) {
1574 el += this->
operator() (r,
c) * v(
c);
1584 return (*
this * cn);
1588template <
typename T>
1593 for (
REGISTER unsigned int j=0; j<this->row; ++j)
1594 v.vec[j] = this->mat[j][
i];
1599template <
typename T>
1605template <
typename T>
1611template <
typename T>
1617template <
typename T>
1623template <
typename T>
1628 return this->mat[
c][r];
1641template <
typename T>
1645 for (
REGISTER T *ptr = this->vec; ptr < this->endvec; ++ptr)
1651template <
typename T>
1655 for (
unsigned r = 0; r < this->row; ++r)
1656 for (
unsigned c = 0;
c < this->col; ++
c)
1657 trm.
mat[
c][r] = this->mat[r][
c];
1666template <typename T>
1672template <
typename T>
1675 if (this->col != this->row) {
1678 return this->
swap(transmat);
1681 for (
unsigned r = 1; r < this->row; ++r)
1682 for (
unsigned c = 0;
c < r; ++
c)
1683 SWAP(this->mat[r][
c], this->mat[
c][r]);
1688template <
typename T>
1692 SWAP(this->dim, m.
dim);
1693 SWAP(this->row, m.
row);
1695 unsigned int tcol = this->col; this->col = m.
col; m.
col = tcol;
1696 SWAP(this->mat, m.
mat);
1697 SWAP(this->vec, m.
vec); SWAP(this->endvec, m.
endvec);
1713template <typename T>
1715{
return ftm.fabs (); }
1718template <typename T>
1720{
return fm.fabs (); }
1723template <typename T>
1725{
return ftsm.fabs (); }
long int Vector< T > & index
const Vector< T > const Vector< T > & x
#define BCHK(cond, exc, txt, ind, rtval)
#define LIKELY(expr)
branch prediction note that we sometimes on purpose mark the unlikely possibility likely and vice ver...
#define TBCICOMP(n, o, t, s)
#define NAMESPACE_CSTD_END
#define BCHKNR(cond, exc, txt, ind)
#define TBCIFILL(n, v, t, s)
#define TBCICOPY(n, o, t, s)
The class BdMatrix is an implementation to store and do operations on sparse Matrices with a band str...
exception class: Use MatErr from matrix.h
unsigned int columns() const
unsigned int rows() const
query matrix dimensions
CTensor< T > & fill(const T &value)
F_Matrix< T > & operator-=(const F_Matrix< T > &a)
TVector< T > get_col(const unsigned int) const
T aligned_value_type TALIGN(MIN_ALIGN2)
F_Matrix< T > & setunit(const T &f=1)
void set_col(const Vector< T > &, const unsigned int)
F_TSMatrix< T > operator/(const T &) const
bool operator==(const F_Matrix< T > &m) const
F_TMatrix< T > operator+(const F_Matrix< T > &) const
F_Matrix< T > & resize(const unsigned d)
static const char * mat_info()
F_Matrix< T > & operator/=(const T &a)
F_Matrix(const T &v, const unsigned r, const unsigned c)
F_TMatrix< T > herm() const
F_TMatrix< T > operator-() const
T * get_fortran_matrix() const
F_TMatrix< T > operator*(const F_Matrix< T > &) const
void setval(const T val, const unsigned int r, const unsigned int c)
F_Matrix(const unsigned r, const unsigned c)
void set_row(const Vector< T > &, const unsigned int)
unsigned long size() const
F_Matrix< T > & operator*=(const T &a)
F_Matrix< T > & resize(const unsigned r, const unsigned c)
bool operator!=(const F_Matrix< T > &m) const
F_Matrix< T > & operator=(const F_Matrix< T > &m)
F_TMatrix< T > trans() const
F_Matrix< T > & resize(const T &v, const unsigned r, const unsigned c)
F_Matrix(const F_TMatrix< T > &tm)
T & operator()(const unsigned int, const unsigned int) const
F_Matrix< T > & fill(const T &v=0)
F_Matrix< T > & operator+=(const F_Matrix< T > &a)
TVector< T > get_row(const unsigned int) const
const T & get(const unsigned r, const unsigned c) const
unsigned int columns() const
F_Matrix(const unsigned d=0)
F_TMatrix< T > conj() const
unsigned int rows() const
F_Matrix(F_TSMatrix< T > &ts)
F_Matrix(const F_Matrix< T > &m)
Temporary Base Class (non referable!) (acc.
F_TSMatrix< T > operator/(const T &)
const T & get(const unsigned r, const unsigned c) const
F_TMatrix< T > transposed_copy() const
Inefficient! Use transMult if possible.
bool operator!=(const F_Matrix< T > &m)
F_TMatrix< T > & setunit(const T &=1)
F_TMatrix< T > & resize(const unsigned int, const unsigned int)
F_TMatrix< T > & swap(F_TMatrix< T > &)
F_TSMatrix< T > operator/=(const T &)
F_TSMatrix< T > operator*(const T &)
F_TMatrix(const unsigned=0)
T aligned_value_type TALIGN(MIN_ALIGN2)
F_TMatrix< T > & operator-=(F_TMatrix< T >)
T * get_fortran_matrix() const
F_TMatrix< T > & transpose()
unsigned long size() const
TVector< T > get_col(const unsigned int) const
F_TMatrix< T > & fill(const T &=0)
bool operator==(const F_Matrix< T > &m)
void set_col(const Vector< T > &, const unsigned int)
F_TMatrix< T > & operator-()
static const char * mat_info()
T & setval(const unsigned int r, const unsigned int c)
void setval(const T val, const unsigned int r, const unsigned int c)
unsigned int rows() const
TVector< T > get_row(const unsigned int) const
F_TMatrix< T > & operator=(const F_Matrix< T > &)
unsigned int columns() const
friend class F_TSMatrix< T >
F_TMatrix< T > & alias(const F_Matrix< T > &m)
F_TMatrix< T > & operator+(F_TMatrix< T >)
F_TMatrix< T > & resize(const unsigned int d)
void set_row(const Vector< T > &, const unsigned int)
F_TSMatrix< T > operator*=(const T &)
friend class F_Matrix< T >
F_TMatrix< T > & operator+=(F_TMatrix< T >)
T & operator()(const unsigned int, const unsigned int) const
T ** mat
Fortran storage layout: mat[col][row].
Temporary object for scaled matrices.
F_TSMatrix(const F_Matrix< T > &m, const T &f=(T) 1)
F_TSMatrix< T > & eval(F_TMatrix< T > *=0)
F_TMatrix< T > operator+(const F_Matrix< T > &)
F_TSMatrix< T > & operator/(const T &f)
F_TSMatrix(const F_TSMatrix< T > &ts)
friend class F_TMatrix< T >
F_TSMatrix< T > & operator-()
bool operator!=(const F_Matrix< T > &m)
F_TSMatrix< T > & operator*=(const T &f)
T operator()(const unsigned r, const unsigned c)
F_TSMatrix(const F_TMatrix< T > &tm, const T &f=(T) 1)
void clone(bool=false, F_TMatrix< T > *=0)
void detach(F_TMatrix< T > *=0)
T aligned_value_type TALIGN(MIN_ALIGN2)
static const char * mat_info()
F_TSMatrix< T > & operator=(const F_TSMatrix< T > &ts)
bool operator==(const F_Matrix< T > &)
F_TSMatrix< T > & operator/=(const T &f)
T get(const unsigned r, const unsigned c) const
F_TSMatrix< T > & operator*(const T &f)
T & setval(const T &val, const unsigned int r, const unsigned int c)
Temporary Base Class Idiom: Class TVector is used for temporary variables.
unsigned long size() const
T & set(const T &val, const unsigned long i) const
Tensor class including arithmetics.
double fabs(const TBCI__ cplx< T > &c)
double fabssqr(const cplx< T > &c)
NAMESPACE_END NAMESPACE_CSTD TBCI__ cplx< T > sqrt(const TBCI__ cplx< T > &z)
#define F_STDDEF_TSTM(op)
#define F_TMFORALL_TS(op)
STD__ istream & operator>>(STD__ istream &in, F_Matrix< T > &m)
#define F_STDDEF_TSTS(op)
F_TMatrix< T > transpose(const F_TMatrix< T > &ftm)
#define F_TMFORALL_TM(op)
#define F_STDDEF_TMTM(op)
return F_TMatrix< T >(tm)
STD__ ostream & operator<<(STD__ ostream &os, const F_Matrix< T > &m)
#define TBCIDELETE(t, v, sz)
const unsigned TMatrix< T > const Matrix< T > * a