33#define ABORT_ON_INDEX_CHECK abort()
35#define ABORT_ON_INDEX_CHECK
37#define CHECK_INDEX(label,i,n) if (i < 1 || i > n) { \
38 std::cerr << label << i <<" is out of range [1,"<< n <<"]"<< std::endl; \
39 ABORT_ON_INDEX_CHECK; }
41#define CHECK_INDEX(label,i,n)
42#define ABORT_ON_INDEX_CHECK
46#define ABORT_ON_SINGULARITY abort()
48#define ABORT_ON_SINGULARITY
71 vector(
const T* values,
size_t n) { this->
fill(values,n); }
92 bool zero(T tol = T(0))
const
95 [tol](T v) { return std::fabs(v) <= tol; });
101 using VecIter =
typename std::vector<T>::iterator;
113 operator const std::vector<T>&()
const {
return myVec; }
115 operator std::vector<T>&() {
return myVec; }
125 CHECK_INDEX(
"vector::operator(): Index ",i,
myVec.size());
132 CHECK_INDEX(
"vector::operator(): Index ",i,
myVec.size());
139 void fill(
const T* values,
size_t n = 0)
141 if (n >
myVec.size())
143 memcpy(
myVec.data(),values,
myVec.size()*
sizeof(T));
169 for (
size_t i = 0; i <
myVec.size() && i < X.size(); i++)
176 for (
size_t i = 0; i <
myVec.size() && i < X.size(); i++)
177 myVec[i] *= (X[i] == T(0) ? T(0) : T(1)/X[i]);
186 vector<T>&
add(
const std::vector<T>& X,
const T& alfa = T(1),
187 unsigned int ofsx = 0,
int stridex = 1,
188 unsigned int ofsy = 0,
int stridey = 1);
197 this->
add(X,T(1)-alfa);
215 T
dot(
const T* v,
size_t nv,
216 size_t off1 = 0,
int inc1 = 1,
217 size_t off2 = 0,
int inc2 = 1)
const;
225 T
dot(
const std::vector<T>& v,
226 size_t off1 = 0,
int inc1 = 1,
227 size_t off2 = 0,
int inc2 = 1)
const
229 return this->
dot(v.data(),v.size(),off1,inc1,off2,inc2);
235 T
norm2(
size_t off = 0,
int inc = 1)
const;
241 T
normInf(
size_t& off,
int inc = 1,
bool sign =
false)
const;
254 T
asum(
size_t off = 0,
int inc = 1)
const;
260 T
sum(
size_t off = 0,
int inc = 1,
size_t max = 0)
const
263 if (inc < 1 ||
myVec.empty())
268 for (
size_t i = off; i <
max; i += inc)
277 bool resize(
size_t n,
char forceClear = 0)
279 if (n ==
myVec.size())
288 myVec.resize(n,T(0));
316 matrixBase(
size_t n_1,
size_t n_2,
size_t n_3 = 1,
size_t n_4 = 1)
325 memcpy(
n,mat.
n,
sizeof(
n));
338 void redim(
size_t n_1,
size_t n_2,
size_t n_3,
size_t n_4,
bool forceClear)
343 if (this->
size() == n_1*n_2*n_3*n_4)
349 if (
n[0] == n_1 &&
n[1] == n_2 &&
n[2] == n_3 &&
n[3] == n_4)
355 size_t oldSize = this->
size();
360 if (this->
size() == oldSize)
376 size_t dim(
short int d = 1)
const {
return d > 0 && d <= 4 ?
n[d-1] : 0; }
378 size_t size()
const {
return n[0]*
n[1]*
n[2]*
n[3]; }
382 bool zero(T tol = T(0))
const {
return elem.zero(tol); }
387 operator const std::vector<T>&()
const {
return elem; }
389 operator std::vector<T>&() {
return elem; }
394 return n[0]*c <
elem.size() ?
elem.ptr() +
n[0]*c :
nullptr;
397 const T*
ptr(
size_t c = 0)
const
399 return n[0]*c <
elem.size() ?
elem.ptr() +
n[0]*c :
nullptr;
403 typename std::vector<T>::iterator
begin() {
return elem.begin(); }
405 typename std::vector<T>::iterator
end() {
return elem.end(); }
413 void fill(
const T* values,
size_t n = 0) {
elem.fill(values,
n); }
422 T
norm2(
int inc = 1)
const {
return elem.norm2(0,inc); }
425 T
asum(
int inc = 1)
const {
return elem.asum(0,inc); }
432 return elem.sum(0,inc);
433 else if (inc == 0 || (inc *= -1) >
static_cast<int>(
n[1]))
436 return elem.sum((inc-1)*
n[0],1,inc*
n[0]);
473 for (
size_t r = 0; r <
ncol; r++)
474 for (
size_t c = 0; c <
nrow; c++)
476 else if (!mat.
elem.empty())
488 void resize(
size_t r,
size_t c,
bool forceClear =
false)
490 this->
redim(r,c,1,1,forceClear);
499 const int curRows =
nrow;
500 if (setRows) incRows -= curRows;
501 const int newRows = curRows + incRows;
502 if (newRows < 1 ||
ncol < 1)
505 else if (incRows < 0)
508 T* newMat = this->
ptr() + newRows;
509 for (
size_t c = 1; c <
ncol; c++, newMat += newRows)
510 memmove(newMat,this->
ptr(c),newRows*
sizeof(T));
514 else if (incRows > 0)
517 size_t oldRows =
nrow;
520 T* oldMat = this->
ptr() + oldRows*(
ncol-1);
521 for (
size_t c =
ncol-1; c > 0; c--, oldMat -= oldRows)
523 memmove(this->
ptr(c),oldMat,oldRows*
sizeof(T));
524 for (
size_t r =
nrow-1; r >= oldRows; r--)
539 size_t oldRows =
nrow;
540 size_t offset = prepend ? B.
nrow : 0;
543 T* oldMat = this->
ptr() + oldRows*(
ncol-1);
544 for (
size_t c =
ncol; c > 0; c--, oldMat -= oldRows)
546 if (c > 1 || prepend)
547 memmove(this->
ptr(c-1)+offset,oldMat,oldRows*
sizeof(T));
549 for (
size_t r = 1; r <= B.
nrow; r++)
550 this->
elem[r-1+
nrow*(c-1)] = B(r,c);
552 for (
size_t r =
nrow; r > oldRows; r--)
553 this->
elem[r-1+
nrow*(c-1)] = B(r-oldRows,c);
580 memcpy(this->
n,A.
n,
sizeof(A.
n));
589 size_t nval = X.size() < this->
elem.size() ? X.size() : this->
elem.size();
590 std::copy(X.begin(),X.begin()+nval,this->elem.begin());
591 std::fill(this->
elem.begin()+nval,this->elem.end(),T(0));
599 CHECK_INDEX(
"matrix::operator(): Row-index ",r,
nrow);
600 CHECK_INDEX(
"matrix::operator(): Column-index ",c,
ncol);
608 CHECK_INDEX(
"matrix::operator(): Row-index ",r,
nrow);
609 CHECK_INDEX(
"matrix::operator(): Column-index ",c,
ncol);
616 CHECK_INDEX(
"matrix::getRow: Row-index ",r,
nrow);
621 for (
size_t i = 0; i <
ncol; i++)
629 CHECK_INDEX(
"matrix::getColumn: Column-index ",c,
ncol);
633 std::vector<T> col(
nrow);
634 memcpy(col.data(),this->ptr(c-1),
nrow*
sizeof(T));
640 void fill(
const std::vector<T>& v,
size_t n,
size_t m = 0)
642 if (
n == 0 || v.size() <
n)
644 if (m == 0) m = v.size()/
n;
647 this->
elem.fill(v.data());
648 else if ((
n = v.size()/m) >
nrow)
649 for (
size_t c = 0; c <
ncol; c++)
652 for (
size_t c = 0; c <
ncol; c++)
653 for (
size_t r = 0; r <
n; r++)
660 CHECK_INDEX(
"matrix::fillColumn: Column-index ",c,
ncol);
661 size_t ndata =
nrow > data.size() ? data.size() :
nrow;
662 memcpy(this->
ptr(c-1),data.data(),ndata*
sizeof(T));
668 CHECK_INDEX(
"matrix::fillColumn: Column-index ",c,
ncol);
669 memcpy(this->
ptr(c-1),data,
nrow*
sizeof(T));
675 CHECK_INDEX(
"matrix::fillRow: Row-index ",r,
nrow);
677 this->
elem.fill(data);
678 else for (
size_t i = 0; i <
ncol; i++)
684 bool transposed =
false)
686 size_t nr = transposed ? block.
cols() : block.
rows();
687 size_t nc = transposed ? block.
rows() : block.
cols();
688 for (
size_t i = 1; i <= nr && i+r-1 <=
nrow; i++)
690 size_t ip = i+r-2 +
nrow*(c-1);
691 for (
size_t j = 1; j <= nc && j+c-1 <=
ncol; j++, ip +=
nrow)
692 this->
elem[ip] = transposed ? block(j,i) : block(i,j);
698 bool transposed =
false)
700 size_t nr = transposed ? block.
cols() : block.
rows();
701 size_t nc = transposed ? block.
rows() : block.
cols();
702 for (
size_t i = 1; i <= nr && i+r-1 <=
nrow; i++)
704 size_t ip = i+r-2 +
nrow*(c-1);
705 for (
size_t j = 1; j <= nc && j+c-1 <=
ncol; j++, ip +=
nrow)
706 this->
elem[ip] += s*(transposed ? block(j,i) : block(i,j));
712 bool addTo =
false,
bool transposed =
false)
const
714 size_t nr = transposed ? block.
cols() : block.
rows();
715 size_t nc = transposed ? block.
rows() : block.
cols();
716 for (
size_t i = 1; i <= nr && i+r-1 <=
nrow; i++)
718 size_t ip = i+r-2 +
nrow*(c-1);
719 for (
size_t j = 1; j <= nc && j+c-1 <=
ncol; j++, ip +=
nrow)
721 (transposed ? block(j,i) : block(i,j)) += this->
elem[ip];
723 (transposed ? block(j,i) : block(i,j)) = this->
elem[ip];
734 for (
size_t r = 0; r <
nrow && r <
ncol; r++)
745 for (
size_t r = 0; r <
nrow && r <
ncol; r++)
755 this->
diag(d.data(), d.size());
765 return this->
scale(d.data(), d.size());
772 for (
size_t r = 0; r <
nrow; r++)
773 for (
size_t c = 0; c <
ncol; c++)
788#define THIS(i,j) this->operator()(i,j)
796 return THIS(1,1)*THIS(2,2) - THIS(2,1)*THIS(1,2);
798 return THIS(1,1)*(THIS(2,2)*THIS(3,3) - THIS(3,2)*THIS(2,3))
799 - THIS(1,2)*(THIS(2,1)*THIS(3,3) - THIS(3,1)*THIS(2,3))
800 + THIS(1,3)*(THIS(2,1)*THIS(3,2) - THIS(3,1)*THIS(2,2));
802 std::cerr <<
"matrix::det: Not available for "
803 <<
nrow <<
"x"<<
ncol <<
" matrices"<< std::endl;
804 ABORT_ON_SINGULARITY;
819 else if (Det <= tol && Det >= -tol) {
820 std::cerr <<
"matrix::inverse: Singular matrix |A|="<< Det << std::endl;
821 ABORT_ON_SINGULARITY;
826 THIS(1,1) = T(1) / Det;
827 else if (
ncol == 2) {
829 B(1,1) = THIS(2,2) / Det;
830 B(2,1) = -THIS(2,1) / Det;
831 B(1,2) = -THIS(1,2) / Det;
832 B(2,2) = THIS(1,1) / Det;
835 else if (
ncol == 3) {
837 B(1,1) = (THIS(2,2)*THIS(3,3) - THIS(3,2)*THIS(2,3)) / Det;
838 B(2,1) = -(THIS(2,1)*THIS(3,3) - THIS(3,1)*THIS(2,3)) / Det;
839 B(3,1) = (THIS(2,1)*THIS(3,2) - THIS(3,1)*THIS(2,2)) / Det;
840 B(1,2) = -(THIS(1,2)*THIS(3,3) - THIS(3,2)*THIS(1,3)) / Det;
841 B(2,2) = (THIS(1,1)*THIS(3,3) - THIS(3,1)*THIS(1,3)) / Det;
842 B(3,2) = -(THIS(1,1)*THIS(3,2) - THIS(3,1)*THIS(1,2)) / Det;
843 B(1,3) = (THIS(1,2)*THIS(2,3) - THIS(2,2)*THIS(1,3)) / Det;
844 B(2,3) = -(THIS(1,1)*THIS(2,3) - THIS(2,1)*THIS(1,3)) / Det;
845 B(3,3) = (THIS(1,1)*THIS(2,2) - THIS(2,1)*THIS(1,2)) / Det;
859 for (
size_t r = 0; r <
nrow; r++)
860 for (
size_t c = 0; c < r; c++)
863 if (diff < -tol || diff > tol)
902 bool transA =
false,
bool transB =
false,
903 bool addTo =
false,
const T& alpha = T(1));
918 bool transA =
false,
bool addTo =
false);
933 bool transB =
false,
bool addTo =
false);
944 bool multiply(
const std::vector<T>& X, std::vector<T>& Y,
945 bool transA =
false,
char addTo = 0)
const;
952 bool multiply(
const std::vector<T>& X, std::vector<T>& Y,
953 const T& alpha,
const T& beta = T(0),
954 bool transA =
false,
int stridex = 1,
int stridey = 1,
955 unsigned int ofsx = 0,
unsigned int ofsy = 0)
const;
959 bool addTo =
false, T alpha = T(1));
969 for (
size_t i = 0; i <
nrow; i++)
971 return *std::max_element(sums.
begin(),sums.
end());
979 if ((transA ?
nrow :
ncol) == X.size())
982 std::cerr <<
"matrix::multiply: Incompatible matrices: A("
983 <<
nrow <<
','<<
ncol <<
"), X("<< X.size() <<
")\n"
984 <<
" when computing Y = "
985 << (transA ?
"A^t":
"A") <<
" * X"<< std::endl;
986 ABORT_ON_INDEX_CHECK;
992 bool transA,
bool transB,
size_t&
M,
size_t& N,
size_t&
K)
1000 std::cerr <<
"matrix::multiply: Incompatible matrices: A("
1003 <<
" when computing C = "
1004 << (transA ?
"A^t":
"A") <<
" * "
1005 << (transB ?
"B^t":
"B") << std::endl;
1006 ABORT_ON_INDEX_CHECK;
1013 bool transA,
size_t&
M,
size_t& N,
size_t&
K)
1017 N =
K > 0 ? B.size()/
K : 0;
1018 if (N*
K == B.size() && !B.
empty())
1021 std::cerr <<
"matrix::multiply: Incompatible matrices: A("
1022 << A.
nrow <<
','<< A.
ncol <<
"), B(r*c="<< B.size() <<
")\n"
1023 <<
" when computing C = "
1024 << (transA ?
"A^t":
"A") <<
" * B"<< std::endl;
1025 ABORT_ON_INDEX_CHECK;
1032 bool transB,
size_t&
M,
size_t& N,
size_t&
K)
1036 M =
K > 0 ? A.size() /
K : 0;
1037 if (
M*
K == A.size() && !A.
empty())
1040 std::cerr <<
"matrix::multiply: Incompatible matrices: A(r*c="<< A.size()
1041 <<
"), B("<< B.
nrow <<
","<< B.
ncol <<
")\n"
1042 <<
" when computing C = A * "
1043 << (transB ?
"B^t":
"B") << std::endl;
1044 ABORT_ON_INDEX_CHECK;
1049 bool compatible(
const std::vector<T>& X,
const std::vector<T>& Y)
1051 if (X.size() ==
nrow && Y.size() ==
ncol)
1054 std::cerr <<
"matrix::outer_product: Incompatible matrix and vectors: A("
1056 << X.size() <<
"), Y("<< Y.size() <<
")\n"
1057 <<
" when computing A += X*Y^t"
1059 ABORT_ON_INDEX_CHECK;
1067 if (n1 !=
nrow) this->
elem.clear();
1084 cblas_sscal(myVec.size(),c,myVec.data(),1);
1091 cblas_dscal(myVec.size(),c,myVec.data(),1);
1097 size_t o1,
int i1,
size_t o2,
int i2)
const
1099 int n1 = i1 > 1 || i1 < -1 ? myVec.size()/abs(i1) : myVec.size()-o1;
1100 int n2 = i2 > 1 || i2 < -1 ? nv/abs(i2) : nv-o2;
1101 int n = n1 < n2 ? n1 : n2;
1102 return cblas_sdot(n,myVec.data()+o1,i1,v+o2,i2);
1107 size_t o1,
int i1,
size_t o2,
int i2)
const
1109 int n1 = i1 > 1 || i1 < -1 ? myVec.size()/abs(i1) : myVec.size()-o1;
1110 int n2 = i2 > 1 || i2 < -1 ? nv/abs(i2) : nv-o2;
1111 int n = n1 < n2 ? n1 : n2;
1112 return cblas_ddot(n,myVec.data()+o1,i1,v+o2,i2);
1118 int n = inc > 1 || inc < -1 ? myVec.size()/abs(inc) : myVec.size()-off;
1119 return cblas_snrm2(n,myVec.data()+off,inc);
1125 int n = inc > 1 || inc < -1 ? myVec.size()/abs(inc) : myVec.size()-off;
1126 return cblas_dnrm2(n,myVec.data()+off,inc);
1132 if (inc < 1 || myVec.empty())
1135 const float* v = myVec.data() + off;
1136 off = 1 + cblas_isamax(myVec.size()/inc,v,inc);
1137 return sign ? v[(off-1)*inc] : fabsf(v[(off-1)*inc]);
1143 if (inc < 1 || myVec.empty())
1146 const double* v = myVec.data() + off;
1147 off = 1 + cblas_idamax(myVec.size()/inc,v,inc);
1148 return sign ? v[(off-1)*inc] : fabs(v[(off-1)*inc]);
1154 int n = inc > 1 || inc < -1 ? myVec.size()/abs(inc) : myVec.size()-off;
1155 return cblas_sasum(n,myVec.data()+off,inc);
1161 int n = inc > 1 || inc < -1 ? myVec.size()/abs(inc) : myVec.size()-off;
1162 return cblas_dasum(n,myVec.data()+off,inc);
1168 unsigned int ofsx,
int stridex,
1169 unsigned int ofsy,
int stridey)
1171 if (myVec.empty() && stridex > 0 && stridey > 0)
1172 myVec.resize(ofsy+stridey*(X.size()-ofsx)/stridex);
1174 int nx = stridex == 0 ? 1 : 1 + (X.size()-ofsx-1)/abs(stridex);
1175 int ny = stridey == 0 ? 1 : 1 + (myVec.size()-ofsy-1)/abs(stridey);
1176 int n = nx < ny ? (stridex == 0 ? ny : nx) : (stridey == 0 ? nx : ny);
1178 cblas_saxpy(n,alfa,X.data()+ofsx,stridex,myVec.data()+ofsy,stridey);
1185 unsigned int ofsx,
int stridex,
1186 unsigned int ofsy,
int stridey)
1188 if (myVec.empty() && stridex > 0 && stridey > 0)
1189 myVec.resize(ofsy+stridey*(X.size()-ofsx)/stridex);
1191 int nx = stridex == 0 ? 1 : 1 + (X.size()-ofsx-1)/abs(stridex);
1192 int ny = stridey == 0 ? 1 : 1 + (myVec.size()-ofsy-1)/abs(stridey);
1193 int n = nx < ny ? (stridex == 0 ? ny : nx) : (stridey == 0 ? nx : ny);
1195 cblas_daxpy(n,alfa,X.data()+ofsx,stridex,myVec.data()+ofsy,stridey);
1203 int n = this->size() < A.size() ? this->size() : A.size();
1205 cblas_saxpy(n,alfa,A.ptr(),1,this->ptr(),1);
1213 int n = this->size() < A.size() ? this->size() : A.size();
1215 cblas_daxpy(n,alfa,A.ptr(),1,this->ptr(),1);
1222 cblas_sscal(this->size(),c,this->ptr(),1);
1229 cblas_dscal(this->size(),c,this->ptr(),1);
1236 for (
size_t c = 0; c < dim && c < ncol; c++)
1237 cblas_sscal(nrow,d[c],this->ptr(c),1);
1244 for (
size_t c = 0; c < dim && c < ncol; c++)
1245 cblas_dscal(nrow,d[c],this->ptr(c),1);
1251 std::vector<float>& Y,
1252 bool transA,
char addTo)
const
1254 if (!this->compatible(X,transA))
1256 else if (!addTo || Y.empty())
1258 Y.resize(transA ? ncol : nrow);
1259 if (addTo) std::fill(Y.begin(),Y.end(),0.0f);
1262 cblas_sgemv(CblasColMajor,
1263 transA ? CblasTrans : CblasNoTrans,
1264 nrow, ncol, addTo < 0 ? -1.0f : 1.0f,
1266 X.data(), 1, addTo ? 1.0f : 0.0f,
1274 std::vector<double>& Y,
1275 bool transA,
char addTo)
const
1277 if (!this->compatible(X,transA))
1279 else if (!addTo || Y.empty())
1281 Y.resize(transA ? ncol : nrow);
1282 if (addTo) std::fill(Y.begin(),Y.end(),0.0);
1285 cblas_dgemv(CblasColMajor,
1286 transA ? CblasTrans : CblasNoTrans,
1287 nrow, ncol, addTo < 0 ? -1.0 : 1.0,
1289 X.data(), 1, addTo ? 1.0 : 0.0,
1297 std::vector<float>& Y,
1298 const float& alpha,
const float& beta,
1299 bool transA,
int stridex,
int stridey,
1300 unsigned int ofsx,
unsigned int ofsy)
const
1302 if (stridex == 0 || stridey == 0)
1304 std::cerr <<
"matrix::multiply: Stride must be non-zero ("
1305 << stridex <<
", "<< stridey <<
")"<< std::endl;
1306 ABORT_ON_INDEX_CHECK;
1310 if (ofsx == 0 && stridex == 1 && !this->compatible(X,transA))
1312 else if (beta == 0.0f || Y.empty())
1314 Y.resize(ofsy + 1 + ((transA ? ncol : nrow)-1)*abs(stridey));
1315 if (beta != 0.0f) std::fill(Y.begin(),Y.end(),0.0f);
1318 cblas_sgemv(CblasColMajor,
1319 transA ? CblasTrans : CblasNoTrans,
1322 X.data()+ofsx, stridex, beta,
1323 Y.data()+ofsy, stridey);
1330 std::vector<double>& Y,
1331 const double& alpha,
const double& beta,
1332 bool transA,
int stridex,
int stridey,
1333 unsigned int ofsx,
unsigned int ofsy)
const
1335 if (stridex == 0 || stridey == 0)
1337 std::cerr <<
"matrix::multiply: Stride must be non-zero ("
1338 << stridex <<
", "<< stridey <<
")"<< std::endl;
1339 ABORT_ON_INDEX_CHECK;
1343 if (ofsx == 0 && stridex == 1 && !this->compatible(X,transA))
1345 else if (beta == 0.0 || Y.empty())
1347 Y.resize(ofsy + 1 + ((transA ? ncol : nrow)-1)*abs(stridey));
1348 if (beta != 0.0) std::fill(Y.begin(),Y.end(),0.0);
1351 cblas_dgemv(CblasColMajor,
1352 transA ? CblasTrans : CblasNoTrans,
1355 X.data()+ofsx, stridex, beta,
1356 Y.data()+ofsy, stridey);
1363 const matrix<float>& B,
1364 bool transA,
bool transB,
1365 bool addTo,
const float& alpha)
1368 if (!this->compatible(A,B,transA,transB,
M,N,
K))
1373 else if (!addTo || this->empty())
1376 cblas_sgemm(CblasColMajor,
1377 transA ? CblasTrans : CblasNoTrans,
1378 transB ? CblasTrans : CblasNoTrans,
1382 addTo ? 1.0f : 0.0f,
1390 const matrix<double>& B,
1391 bool transA,
bool transB,
1392 bool addTo,
const double& alpha)
1395 if (!this->compatible(A,B,transA,transB,
M,N,
K))
1400 else if (!addTo || this->empty())
1403 cblas_dgemm(CblasColMajor,
1404 transA ? CblasTrans : CblasNoTrans,
1405 transB ? CblasTrans : CblasNoTrans,
1417 const std::vector<float>& B,
1418 bool transA,
bool addTo)
1421 if (!this->compatible(A,B,transA,
M,N,
K))
1423 else if (!addTo || this->empty())
1426 cblas_sgemm(CblasColMajor,
1427 transA ? CblasTrans : CblasNoTrans, CblasNoTrans,
1431 addTo ? 1.0f : 0.0f,
1439 const std::vector<double>& B,
1440 bool transA,
bool addTo)
1443 if (!this->compatible(A,B,transA,
M,N,
K))
1445 else if (!addTo || this->empty())
1448 cblas_dgemm(CblasColMajor,
1449 transA ? CblasTrans : CblasNoTrans, CblasNoTrans,
1461 const matrix<float>& B,
1462 bool transB,
bool addTo)
1465 if (!this->compatible(A,B,transB,
M,N,
K))
1467 else if (!addTo || this->empty())
1470 cblas_sgemm(CblasColMajor,
1471 CblasNoTrans, transB ? CblasTrans : CblasNoTrans,
1475 addTo ? 1.0f : 0.0f,
1483 const matrix<double>& B,
1484 bool transB,
bool addTo)
1487 if (!this->compatible(A,B,transB,
M,N,
K))
1489 else if (!addTo || this->empty())
1492 cblas_dgemm(CblasColMajor,
1493 CblasNoTrans, transB ? CblasTrans : CblasNoTrans,
1505 const std::vector<float>& Y,
1506 bool addTo,
float alpha)
1509 this->resize(X.size(),Y.size());
1510 else if (!this->compatible(X,Y))
1513 cblas_sgemm(CblasColMajor,
1514 CblasNoTrans, CblasTrans,
1515 nrow, ncol, 1, alpha,
1518 addTo ? 1.0f : 0.0f,
1526 const std::vector<double>& Y,
1527 bool addTo,
double alpha)
1530 this->resize(X.size(),Y.size());
1531 else if (!this->compatible(X,Y))
1534 cblas_dgemm(CblasColMajor,
1535 CblasNoTrans, CblasTrans,
1536 nrow, ncol, 1, alpha,
1550 template<
class T>
inline
1558 template<
class T>
inline
1560 size_t o1,
int i1,
size_t o2,
int i2)
const
1564 for (i = o1, j = o2; i < myVec.size() && j < nv; i += i1, j += i2)
1565 dotprod += myVec[i] * v[j];
1569 template<
class T>
inline
1573 if (inc < 1 || myVec.size() <= off)
1577 for (
size_t i = off; i < myVec.size(); i += inc)
1578 xsum += myVec[i]*myVec[i];
1582 template<
class T>
inline
1586 if (inc < 1 || myVec.size() <= off)
1590 for (
size_t i = off; i < myVec.size(); i += inc)
1591 if (myVec[i] > amax)
1594 xmax = amax = myVec[i];
1596 else if (myVec[i] < -amax)
1603 return sign ? xmax : amax;
1606 template<
class T>
inline
1610 if (inc < 1 || myVec.size() <= off)
1613 for (
size_t i = off; i < myVec.size(); i += inc)
1614 xsum += myVec[i] < T(0) ? -myVec[i] : myVec[i];
1618 template<
class T>
inline
1620 unsigned int ofsx,
int stridex,
1621 unsigned int ofsy,
int stridey)
1623 if (stridex < 0 || stridey < 0 || stridex+stridey == 0)
1625 std::cerr <<
"vector::add: Negative stride not supported ("
1626 << stridex <<
", "<< stridey <<
")"<< std::endl;
1627 ABORT_ON_INDEX_CHECK;
1631 std::vector<T>& Y = myVec;
1632 if (Y.empty() && stridex > 0)
1633 Y.resize(ofsy+stridey*(X.size()-ofsx)/stridex);
1635 for (; ofsx < X.size() && ofsy < Y.size(); ofsx += stridex, ofsy += stridey)
1636 Y[ofsy] += alfa*X[ofsx];
1640 template<
class T>
inline
1646 for (
size_t i = 0; i < X.
size() && i < Y.
size(); i++)
1651 template<
class T>
inline
1654 for (T& x : this->elem)
1659 template<
class T>
inline
1662 for (
size_t c = 0; c < dim && c < ncol; c++)
1663 for (
size_t r = 0; r < nrow; r++)
1664 this->elem[r+nrow*c] *= d[c];
1668 template<
class T>
inline
1670 bool transA,
char addTo)
const
1672 if (!this->compatible(X,transA))
1674 else if (!addTo || Y.empty())
1676 Y.
resize(transA ? ncol : nrow);
1677 std::fill(Y.begin(),Y.end(),T(0));
1680 for (
size_t i = 0; i < Y.size(); i++)
1681 for (
size_t j = 0; j < X.size(); j++)
1683 Y[i] += THIS(j+1,i+1) * (addTo < 0 ? -X[j] : X[j]);
1685 Y[i] += THIS(i+1,j+1) * (addTo < 0 ? -X[j] : X[j]);
1690 template<
class T>
inline
1692 const T& alpha,
const T& beta,
1693 bool transA,
int stridex,
int stridey,
1694 unsigned int ofsx,
unsigned int ofsy)
const
1696 if (stridex <= 0 || stridey <= 0)
1698 std::cerr <<
"matrix::multiply: Non-positive stride not supported ("
1699 << stridex <<
", "<< stridey <<
")"<< std::endl;
1700 ABORT_ON_INDEX_CHECK;
1704 if (ofsx == 0 && stridex == 1 && !this->compatible(X,transA))
1706 else if (beta == T(0) || Y.empty())
1708 Y.resize(ofsy + 1 + ((transA ? ncol : nrow)-1)*stridey);
1709 std::fill(Y.begin(),Y.end(),T(0));
1711 else if (beta != T(1))
1712 for (
size_t i = ofsy; i < Y.size(); i += stridey)
1716 for (a = 1, i = ofsy; i < Y.size(); a++, i += stridey)
1717 for (b = 1, j = ofsx; j < X.size(); b++, j += stridex)
1719 Y[i] += alpha * THIS(b,a) * X[j];
1721 Y[i] += alpha * THIS(a,b) * X[j];
1726 template<
class T>
inline
1729 bool transA,
bool transB,
bool addTo,
1733 if (!this->compatible(A,B,transA,transB,
M,N,
K))
1738 else if (!addTo || this->empty())
1739 this->resize(
M,N,
true);
1741 for (
size_t i = 1; i <=
M; i++)
1742 for (
size_t j = 1; j <= N; j++)
1743 for (
size_t k = 1; k <=
K; k++)
1744 if (transA && transB)
1745 THIS(i,j) += alpha*A(k,i)*B(j,k);
1747 THIS(i,j) += alpha*A(k,i)*B(k,j);
1749 THIS(i,j) += alpha*A(i,k)*B(j,k);
1751 THIS(i,j) += alpha*A(i,k)*B(k,j);
1756 template<
class T>
inline
1758 bool transA,
bool addTo)
1761 if (!this->compatible(A,B,transA,
M,N,
K))
1763 else if (!addTo || this->empty())
1764 this->resize(
M,N,
true);
1766 for (
size_t i = 1; i <=
M; i++)
1767 for (
size_t j = 1; j <= N; j++)
1768 for (
size_t k = 1; k <=
K; k++)
1770 THIS(i,j) += A(k,i)*B[k-1+
K*(j-1)];
1772 THIS(i,j) += A(i,k)*B[k-1+
K*(j-1)];
1777 template<
class T>
inline
1779 bool transB,
bool addTo)
1782 if (!this->compatible(A,B,transB,
M,N,
K))
1784 else if (!addTo || this->empty())
1785 this->resize(
M,N,
true);
1787 for (
size_t i = 1; i <=
M; i++)
1788 for (
size_t j = 1; j <= N; j++)
1789 for (
size_t k = 1; k <=
K; k++)
1791 THIS(i,j) += A[i-1+
M*(k-1)]*B(j,k);
1793 THIS(i,j) += A[i-1+
M*(k-1)]*B(k,j);
1798 template<
class T>
inline
1800 const std::vector<T>& Y,
1801 bool addTo, T alpha)
1804 this->resize(X.size(),Y.size());
1805 else if (!this->compatible(X,Y))
1809 for (
size_t j = 0; j < ncol; j++)
1810 for (
size_t i = 0; i < nrow; i++)
1811 this->elem[i+nrow*j] += alpha*X[i]*Y[j];
1813 for (
size_t j = 0; j < ncol; j++)
1814 for (
size_t i = 0; i < nrow; i++)
1815 this->elem[i+nrow*j] = alpha*X[i]*Y[j];
1854 else for (
size_t i = 0; i < X.
size(); i++)
1857 return s << std::endl;
1863 size_t m = 0, n = 0;
1865 while (s.get(c) && isspace(c));
1866 bool symmetric = (c ==
'S' || c ==
's');
1867 bool columnori = (c ==
'C' || c ==
'c');
1874 else if (isalpha(c))
1885 for (
size_t i = 1; i <= m; i++)
1887 while (s.get(c) && isspace(c));
1892 for (
size_t j = (symmetric ? i : 1); j <= n; j++)
1894 s >> (columnori ? A(j,i) : A(i,j));
1895 if (symmetric && j > i)
1911 return s <<
" (empty)"<< std::endl;
1914 for (
size_t i = 1; i <= A.
rows(); i++)
1916 size_t c1 = symm ? i : 1;
1917 s <<
"\nRow "<< i <<
": "<<
trunc(A(i,c1));
1918 for (
size_t j = c1+1; j <= A.
cols(); j++)
1919 s <<
' '<<
trunc(A(i,j));
1922 return s << std::endl;
1927 std::ostream& s = std::cout)
1930 s << label <<
" = [";
1934 for (
size_t i = 1; i <= X.
size(); i++)
1935 s <<
' '<<
trunc(X(i));
1936 s <<
" ];"<< std::endl;
1941 std::ostream& s = std::cout)
1944 s << label <<
" = [";
1948 size_t nsp = label ? 4 + strlen(label) : 1;
1949 for (
size_t i = 1; i <= A.
rows(); i++)
1954 for (
size_t k = 0; k < nsp; k++) s <<
' ';
1956 for (
size_t j = 1; j <= A.
cols(); j++)
1957 s <<
' '<<
trunc(A(i,j));
1959 s <<
" ];"<< std::endl;
BLAS support for various platforms.
static SystemMatrix * K
Pointer to coefficient matrix A.
Definition EigSolver.C:91
static SystemMatrix * M
Pointer to coefficient matrix B.
Definition EigSolver.C:92
virtual bool empty() const
Checks if the matrix is empty.
Definition SystemMatrix.h:249
Common base class for multi-dimensional (2D and 3D) matrices.
Definition matrix.h:308
size_t n[4]
Dimension of the matrix.
Definition matrix.h:440
size_t dim(short int d=1) const
Query dimensions.
Definition matrix.h:376
matrixBase< T > & add(const matrixBase< T > &A, const T &alfa)
Add the given matrix A scaled by alfa to *this.
Definition matrix.h:1641
matrixBase(vector< T > &vec)
Constructor using an external vector for matrix element storage.
Definition matrix.h:313
void fill(const T *values, size_t n=0)
Fill the matrix with data from an array.
Definition matrix.h:413
matrixBase()
The constructor is protected to allow sub-class instances only.
Definition matrix.h:311
vector< T > & elem
Actual matrix elements, stored column by column.
Definition matrix.h:441
std::vector< T >::iterator end()
Iterator to the end of the matrix elements.
Definition matrix.h:405
void fill(T s)
Fill the matrix with a scalar value.
Definition matrix.h:411
matrixBase(const matrixBase< T > &mat, bool copyContent=true)
Copy constructor.
Definition matrix.h:322
const vector< T > & toVec() const
Type casting to a one-dimensional utl::vector, for access.
Definition matrix.h:385
size_t size() const
Query total matrix size.
Definition matrix.h:378
T norm2(int inc=1) const
Return the Euclidean norm of the matrix.
Definition matrix.h:422
std::vector< T >::iterator begin()
Iterator to the start of the matrix elements.
Definition matrix.h:403
void clear()
Clears the matrix and sets its dimension to zero.
Definition matrix.h:408
T asum(int inc=1) const
Return the sum of the absolute value of the matrix elements.
Definition matrix.h:425
T sum(int inc=1) const
Return the sum of the matrix elements.
Definition matrix.h:429
bool empty() const
Check if the matrix is empty.
Definition matrix.h:380
bool zero(T tol=T(0)) const
Check if the matrix elements are all zero.
Definition matrix.h:382
virtual void clearIfNrowChanged(size_t n1, size_t n2, size_t n3)=0
Clears the matrix content if the first dimension(s) changed.
void redim(size_t n_1, size_t n_2, size_t n_3, size_t n_4, bool forceClear)
Resize the matrix to dimension .
Definition matrix.h:338
T * ptr(size_t c=0)
Access through pointer.
Definition matrix.h:392
matrixBase(size_t n_1, size_t n_2, size_t n_3=1, size_t n_4=1)
Constructor creating a matrix of dimension .
Definition matrix.h:316
const T * ptr(size_t c=0) const
Reference through pointer.
Definition matrix.h:397
vector< T > myElem
Internal matrix storage.
Definition matrix.h:444
matrixBase< T > & multiply(const T &c)
Multiplication of this matrix by a scalar c.
Definition matrix.h:1652
Two-dimensional rectangular matrix with some algebraic operations.
Definition matrix.h:456
matrix< T > & operator*=(T c)
Multiplication with a scalar.
Definition matrix.h:881
const T & operator()(size_t r, size_t c) const
Index-1 based element reference.
Definition matrix.h:606
size_t cols() const
Query number of matrix columns.
Definition matrix.h:572
bool multiply(const std::vector< T > &X, std::vector< T > &Y, const T &alpha, const T &beta=T(0), bool transA=false, int stridex=1, int stridey=1, unsigned int ofsx=0, unsigned int ofsy=0) const
Matrix-vector multiplication.
Definition matrix.h:1691
matrix< T > & transpose()
Replace the current matrix by its transpose.
Definition matrix.h:769
matrix< T > & diag(const T *d, size_t dim=0)
Create a diagonal matrix.
Definition matrix.h:739
bool multiply(const std::vector< T > &X, std::vector< T > &Y, bool transA=false, char addTo=0) const
Matrix-vector multiplication.
Definition matrix.h:1669
T trace() const
Return the trace of the matrix (sum of its diagonal elements).
Definition matrix.h:782
matrix(size_t r, size_t c)
Constructor creating a matrix of dimension .
Definition matrix.h:464
void extractBlock(matrix< T > &block, size_t r, size_t c, bool addTo=false, bool transposed=false) const
Extract a block of the matrix to another matrix.
Definition matrix.h:711
void fillRow(size_t r, const T *data)
Fill a row of the matrix.
Definition matrix.h:673
matrix< T > & scale(const T *d, size_t dim)
Scale the columns of a matrix.
Definition matrix.h:1660
void clearIfNrowChanged(size_t n1, size_t, size_t) override
Clears the content if the number of rows changed.
Definition matrix.h:1065
T normInf() const
Return the infinite norm of the matrix.
Definition matrix.h:962
matrix< T > & expandRows(int incRows, bool setRows=false)
Increase or decrease the number of rows in the matrix.
Definition matrix.h:497
matrix< T > & add(const matrix< T > &A, T alfa=T(1))
Add the given matrix A scaled by alfa to *this.
Definition matrix.h:875
size_t & ncol
Number of matrix columns.
Definition matrix.h:1072
bool augmentCols(const matrix< T > &B)
Increase the number of columns by augmenting the given matrix.
Definition matrix.h:559
bool compatible(const std::vector< T > &X, const std::vector< T > &Y)
Check dimension compatibility for outer product multiplication.
Definition matrix.h:1049
bool augmentRows(const matrix< T > &B, bool prepend=false)
Increase the number of rows by augmenting the given matrix.
Definition matrix.h:534
matrix< T > & multiply(const matrix< T > &A, const matrix< T > &B, bool transA=false, bool transB=false, bool addTo=false, const T &alpha=T(1))
Matrix-matrix multiplication.
Definition matrix.h:1727
matrix< T > & diag(const std::vector< T > &d)
Create a diagonal matrix.
Definition matrix.h:750
bool isSymmetric(T tol=T(0)) const
Check for symmetry.
Definition matrix.h:854
T det() const
Compute the determinant of a square matrix.
Definition matrix.h:791
T inverse(T tol=T(0))
Compute the inverse of a square matrix.
Definition matrix.h:814
matrix< T > & operator+=(const matrix< T > &A)
Add the given matrix A to *this.
Definition matrix.h:871
T & operator()(size_t r, size_t c)
Index-1 based element access.
Definition matrix.h:597
void fillColumn(size_t c, const T *data)
Fill a column of the matrix.
Definition matrix.h:666
matrix(const matrix< T > &mat, bool transposed=false)
Copy constructor, optionally creates the transpose of mat.
Definition matrix.h:467
void fillBlock(const matrix< T > &block, size_t r, size_t c, bool transposed=false)
Fill a block of the matrix with another matrix.
Definition matrix.h:683
matrix< T > & scale(const std::vector< T > &d)
Scale the columns of a matrix.
Definition matrix.h:763
bool compatible(const matrix< T > &A, const matrix< T > &B, bool transA, bool transB, size_t &M, size_t &N, size_t &K)
Check dimension compatibility for matrix-matrix multiplication.
Definition matrix.h:991
matrix< T > & operator=(const std::vector< T > &X)
Overloaded assignment operator.
Definition matrix.h:586
size_t & nrow
Number of matrix rows.
Definition matrix.h:1071
matrix< T > & operator/=(T d)
Division by a scalar.
Definition matrix.h:883
T colsum(size_t c) const
Return the sum of a matrix column.
Definition matrix.h:786
matrix< T > & multiply(T c)
Multiplication of this matrix by a scalar c.
Definition matrix.h:885
void resize(size_t r, size_t c, bool forceClear=false)
Resize the matrix to dimension .
Definition matrix.h:488
T rowsum(size_t r) const
Return the sum of a matrix row.
Definition matrix.h:784
size_t rows() const
Query number of matrix rows.
Definition matrix.h:570
virtual ~matrix()
Empty destructor.
Definition matrix.h:480
matrix()
Constructor creating an empty matrix.
Definition matrix.h:459
std::vector< T > getColumn(size_t c) const
Extract a column from the matrix.
Definition matrix.h:627
void addBlock(const matrix< T > &block, T s, size_t r, size_t c, bool transposed=false)
Add a scalar multiple of another matrix to a block of the matrix.
Definition matrix.h:697
bool multiplyMat(const std::vector< T > &A, const matrix< T > &B, bool transB=false, bool addTo=false)
Matrix-matrix multiplication.
Definition matrix.h:1778
bool outer_product(const std::vector< T > &X, const std::vector< T > &Y, bool addTo=false, T alpha=T(1))
Outer product between two vectors.
Definition matrix.h:1799
void fillColumn(size_t c, const std::vector< T > &data)
Fill a column of the matrix.
Definition matrix.h:658
matrix< T > & diag(T d, size_t dim=0)
Create a diagonal matrix.
Definition matrix.h:728
bool multiplyMat(const matrix< T > &A, const std::vector< T > &B, bool transA=false, bool addTo=false)
Matrix-matrix multiplication.
Definition matrix.h:1757
matrix< T > & operator=(const matrix< T > &A)
Assignment operator.
Definition matrix.h:575
matrix(vector< T > &vec)
Constructor using an external vector for matrix element storage.
Definition matrix.h:461
bool compatible(const std::vector< T > &X, bool transA) const
Check dimension compatibility for matrix-vector multiplication.
Definition matrix.h:976
matrix< T > & operator-=(const matrix< T > &A)
Subtract the given matrix A from *this.
Definition matrix.h:873
bool compatible(const matrix< T > &A, const std::vector< T > &B, bool transA, size_t &M, size_t &N, size_t &K)
Check dimension compatibility for matrix-matrix multiplication, when the matrix B is represented by a...
Definition matrix.h:1012
void fill(const std::vector< T > &v, size_t n, size_t m=0)
Fill the matrix with vector data.
Definition matrix.h:640
vector< T > getRow(size_t r) const
Extract a row from the matrix.
Definition matrix.h:614
bool compatible(const std::vector< T > &A, const matrix< T > &B, bool transB, size_t &M, size_t &N, size_t &K)
Check dimension compatibility for matrix-matrix multiplication, when the matrix A is represented by a...
Definition matrix.h:1031
A vector class with some added algebraic operations.
Definition matrix.h:64
VecIter begin()
Start of the vector container, for update.
Definition matrix.h:108
VecIter end()
End of the vector container, for update.
Definition matrix.h:110
vector< T > & operator/=(T d)
Division by a scalar.
Definition matrix.h:164
vector()
Constructor creating an empty vector.
Definition matrix.h:67
vector(const T *values, size_t n)
Constructor creating a vector from a C-array.
Definition matrix.h:71
vector< T > & operator=(const std::vector< T > &X)
Overloaded assignment operator.
Definition matrix.h:76
vector< T > & operator-=(const vector< T > &X)
Subtract the given vector X from *this.
Definition matrix.h:184
void fill(T s)
Fill the vector with a scalar value.
Definition matrix.h:137
void fill(const T *values, size_t n=0)
Fill the vector with data from an array.
Definition matrix.h:139
T asum(size_t off=0, int inc=1) const
Return the sum of the absolute value of the vector elements.
Definition matrix.h:1607
T max() const
Return the largest element of the vector.
Definition matrix.h:247
typename std::vector< T >::iterator VecIter
Convenience alias for non-const iterators.
Definition matrix.h:101
const T * ptr() const
Reference through pointer.
Definition matrix.h:85
void push_back(T c)
Append a scalar value to the vector, increasing its size by one.
Definition matrix.h:147
bool empty() const
Is the vector empty (zero size)?
Definition matrix.h:90
T normInf(int inc=1) const
Return the infinite norm of the vector (no index offset).
Definition matrix.h:244
T & operator()(size_t i)
Index-1 based element access.
Definition matrix.h:123
vector< T > & operator+=(const vector< T > &X)
Add the given vector X to *this.
Definition matrix.h:182
void reserve(size_t n)
Pre-allocation of vector length to n.
Definition matrix.h:293
void push_back(const T *p, const T *q)
Append a range of values increasing the size by q-p.
Definition matrix.h:156
bool zero(T tol=T(0)) const
Is the vector elements all zero?
Definition matrix.h:92
vector< T > & relax(T alfa, const std::vector< T > &X, const std::vector< T > &Y)
Perform where Z = *this.
Definition matrix.h:203
vector< T > & add(const std::vector< T > &X, const T &alfa=T(1), unsigned int ofsx=0, int stridex=1, unsigned int ofsy=0, int stridey=1)
Add the given vector X scaled by alfa to *this.
Definition matrix.h:1619
T norm2(size_t off=0, int inc=1) const
Return the Euclidean norm of the vector.
Definition matrix.h:1570
ConstVecIter begin() const
Start of the vector container, for access.
Definition matrix.h:104
void swap(vector< T > &vec)
Swap the content with another vector.
Definition matrix.h:159
vector< T > & operator*=(const std::vector< T > &X)
Component-wise multiplication with a vector.
Definition matrix.h:167
const T & operator()(size_t i) const
Index-1 based element reference.
Definition matrix.h:130
std::vector< T > myVec
Internal vector storage.
Definition matrix.h:298
typename std::vector< T >::const_iterator ConstVecIter
Convenience alias for const iterators.
Definition matrix.h:99
vector< T > & operator/=(const std::vector< T > &X)
Component-wise division with a vector.
Definition matrix.h:174
vector< T > & relax(T alfa, const std::vector< T > &X)
Perform where Y = *this.
Definition matrix.h:192
T & operator[](size_t i)
Index-0 based element access.
Definition matrix.h:118
ConstVecIter end() const
End of the vector container, for access.
Definition matrix.h:106
T min() const
Return the smallest element of the vector.
Definition matrix.h:249
void push_back(ConstVecIter i1, ConstVecIter i2)
Append a range of values increasing the size by i2-i1.
Definition matrix.h:150
vector(const std::vector< T > &X)
Overloaded copy constructor.
Definition matrix.h:73
bool resize(size_t n, char forceClear=0)
Resize the vector to length n.
Definition matrix.h:277
T sum(size_t off=0, int inc=1, size_t max=0) const
Return the sum of the vector elements.
Definition matrix.h:260
void clear()
Clear the vector, setting its size to zero.
Definition matrix.h:295
T dot(const T *v, size_t nv, size_t off1=0, int inc1=1, size_t off2=0, int inc2=1) const
Dot product between *this and another vector.
Definition matrix.h:1559
vector< T > & operator*=(T c)
Multiplication with a scalar.
Definition matrix.h:1551
size_t size() const
Size of the vector.
Definition matrix.h:88
T * ptr()
Access through pointer.
Definition matrix.h:83
const T & operator[](size_t i) const
Index-0 based element reference.
Definition matrix.h:120
T normInf(size_t &off, int inc=1, bool sign=false) const
Return the infinite norm of the vector, or signed max value.
Definition matrix.h:1583
vector(size_t n)
Constructor creating a vector of length n.
Definition matrix.h:69
T dot(const std::vector< T > &v, size_t off1=0, int inc1=1, size_t off2=0, int inc2=1) const
Dot product between *this and another vector.
Definition matrix.h:225
General utility classes and functions.
Definition SIMoptions.h:22
T trunc(T v)
Truncate a value to zero when it is less than a given threshold.
Definition matrix.h:1831
std::istream & operator>>(std::istream &s, vector< T > &X)
Read the vector X from the stream s.
Definition matrix.h:1838
double zero_print_tol
Zero tolerance for printing numbers.
Definition MatVec.C:24
std::ostream & operator<<(std::ostream &s, const vector< T > &X)
Print the vector X to the stream s.
Definition matrix.h:1849
int nval_per_line
Number of values to print per line.
Definition MatVec.C:23
const char RETAIN
Flag for vector::resize() method telling it to retain its content.
Definition matrix.h:55
void writeMatlab(const char *label, const vector< T > &X, std::ostream &s=std::cout)
Print the vector X to the stream s in matlab format.
Definition matrix.h:1926
Global parameters for controlling the print of vectors and matrices.