27#ifndef EASY3D_CORE_MATRIX_H
28#define EASY3D_CORE_MATRIX_H
35#include <easy3d/core/random.h>
115 void set(
int row,
int col, FT v);
118 const FT &
get(
int row,
int col)
const;
130 const FT *
data()
const {
return data_; }
148 void set_row(
const std::vector<FT> &v,
int row);
181 template<
typename FT2>
184 template<
typename FT2>
207 void copy_from_array(
const FT *v);
208 void set_by_scalar(
const FT &x);
220 template<
typename FT>
222 template<
typename FT>
226 template<
typename FT>
228 template<
typename FT>
230 template<
typename FT>
232 template<
typename FT>
234 template<
typename FT>
236 template<
typename FT>
238 template<
typename FT>
240 template<
typename FT>
242 template<
typename FT>
244 template<
typename FT1,
typename FT2>
246 template<
typename FT1,
typename FT2>
248 template<
typename FT1,
typename FT2>
250 template<
typename FT1,
typename FT2>
255 template<
typename FT>
258 template<
typename FT>
261 template<
typename FT>
263 template<
typename FT>
264 std::vector<FT>
mult(
const Matrix<FT> &,
const std::vector<FT> &);
267 template<
typename FT>
269 template<
typename FT>
271 template<
typename FT>
273 template<
typename FT>
277 template<
typename FT>
279 template<
typename FT>
283 template<
typename FT>
285 template<
typename FT>
287 template<
typename FT>
289 template<
typename FT>
293 template<
typename FT>
298 template<
typename FT>
302 template<
typename FT>
304 template<
typename FT>
309 template<
typename FT>
313 template<
typename FT>
315 template<
typename FT>
317 template<
typename FT>
319 template<
typename FT>
321 template<
typename FT>
323 template<
typename FT>
325 template<
typename FT>
329 template<
typename FT>
333 template<
typename FT>
337 template<
typename FT>
343 template<
typename FT>
348 template<
typename FT>
357 template<
typename FT>
358 std::ostream &
operator<<(std::ostream &,
const std::vector<FT> &);
359 template<
typename FT>
360 std::istream &
operator>>(std::istream &, std::vector<FT> &);
363 template<
typename FT>
364 std::vector<FT>
operator-(
const std::vector<FT> &);
365 template<
typename FT>
366 std::vector<FT>
operator+(
const std::vector<FT> &,
const std::vector<FT> &);
367 template<
typename FT>
368 std::vector<FT>
operator+(
const std::vector<FT> &,
const FT &);
369 template<
typename FT>
370 std::vector<FT>
operator+(
const FT &,
const std::vector<FT> &);
371 template<
typename FT>
372 std::vector<FT>
operator-(
const std::vector<FT> &,
const std::vector<FT> &);
373 template<
typename FT>
374 std::vector<FT>
operator-(
const std::vector<FT> &,
const FT &);
375 template<
typename FT>
376 std::vector<FT>
operator-(
const FT &,
const std::vector<FT> &);
377 template<
typename FT1,
typename FT2>
378 std::vector<FT1>
operator*(
const std::vector<FT1> &,
const FT2 &);
379 template<
typename FT1,
typename FT2>
380 std::vector<FT1>
operator*(
const FT2 &,
const std::vector<FT1> &);
381 template<
typename FT1,
typename FT2>
382 std::vector<FT1>
operator/(
const std::vector<FT1> &,
const FT2 &);
383 template<
typename FT1,
typename FT2>
384 std::vector<FT1>
operator/(
const FT2 &,
const std::vector<FT1> &);
388 template<
typename FT>
389 std::vector<FT>
elem_mult(
const std::vector<FT> &,
const std::vector<FT> &);
390 template<
typename FT>
391 std::vector<FT>
elem_divd(
const std::vector<FT> &,
const std::vector<FT> &);
392 template<
typename FT>
393 std::vector<FT> &
elem_mult_eq(std::vector<FT> &,
const std::vector<FT> &);
394 template<
typename FT>
395 std::vector<FT> &
elem_divd_eq(std::vector<FT> &,
const std::vector<FT> &);
398 template<
typename FT>
399 FT
operator*(
const std::vector<FT> &,
const std::vector<FT> &);
400 template<
typename FT>
401 FT
dot(
const std::vector<FT> &,
const std::vector<FT> &);
404 template<
typename FT>
405 std::vector<FT>
random(
int size);
408 template<
typename FT>
409 FT
norm(
const std::vector<FT> &);
410 template<
typename FT>
411 FT
norm(
const std::vector<std::complex<FT>> &);
412 template<
typename FT>
413 void swap(std::vector<FT> &, std::vector<FT> &);
414 template<
typename FT>
415 std::vector<FT>
linspace(FT, FT,
int);
416 template<
typename FT>
417 FT
sum(
const std::vector<FT> &);
418 template<
typename FT>
419 FT
min(
const std::vector<FT> &);
420 template<
typename FT>
421 FT
max(
const std::vector<FT> &);
422 template<
typename FT>
423 FT
mean(
const std::vector<FT> &);
425 template<
typename FT>
426 std::vector<FT>
abs(
const std::vector<std::complex<FT>> &);
427 template<
typename FT>
428 std::vector<FT>
arg(
const std::vector<std::complex<FT>> &);
429 template<
typename FT>
430 std::vector<FT>
real(
const std::vector<std::complex<FT>> &);
431 template<
typename FT>
432 std::vector<FT>
imag(
const std::vector<std::complex<FT>> &);
435 template<
typename FT>
436 std::vector<std::complex<FT>>
complex_vector(
const std::vector<FT> &);
438 template<
typename FT>
439 std::vector<std::complex<FT>>
452 template<
typename FT>
453 void Matrix<FT>::init(
int rows,
int cols) {
456 nTotal_ = nRow_ * nColumn_;
458 data_ =
new FT[nTotal_];
459 prow_ =
new FT *[nRow_];
461 assert(data_ != NULL);
462 assert(prow_ != NULL);
465 for (
int i = 0; i < nRow_; ++i) {
475 template<
typename FT>
476 inline void Matrix<FT>::copy_from_array(
const FT *v) {
477 for (
long i = 0; i < nTotal_; ++i)
485 template<
typename FT>
486 inline void Matrix<FT>::set_by_scalar(
const FT &x) {
487 for (
long i = 0; i < nTotal_; ++i)
495 template<
typename FT>
496 void Matrix<FT>::destroy() {
507 template<
typename FT>
508 Matrix<FT> Matrix<FT>::cofactor(
int _i,
int _j)
const {
509 assert(0 <= _i && _i < nRow_ && 0 <= _j && _j < nColumn_);
511 for(
int i = 0; i < mat.rows(); i++) {
512 for(
int j = 0; j < mat.cols(); j++){
515 mat.data()[i * mat.cols() + j] = data_[i * nColumn_ + j];
517 mat.data()[i * mat.cols() + j] = data_[i * nColumn_ + (j + 1)];
521 mat.data()[i * mat.cols() + j] = data_[(i + 1) * nColumn_ + j];
523 mat.data()[i * mat.cols() + j] = data_[(i + 1) * nColumn_ + (j + 1)];
536 template<
typename FT>
538 : data_(0), prow_(0), nRow_(0), nColumn_(0), nTotal_(0) {
541 template<
typename FT>
543 init(A.nRow_, A.nColumn_);
544 copy_from_array(A.data_);
547 template<
typename FT>
553 template<
typename FT>
557 copy_from_array(array.data());
560 template<
typename FT>
563 copy_from_array(array);
566 template<
typename FT>
567 Matrix<FT>::~Matrix() {
575 template<
typename FT>
577 if (data_ == A.data_)
580 if (nRow_ == A.nRow_ && nColumn_ == A.nColumn_)
581 copy_from_array(A.data_);
584 init(A.nRow_, A.nColumn_);
585 copy_from_array(A.data_);
595 template<
typename FT>
605 template<
typename FT>
612 template<
typename FT>
619 template<
typename FT>
624 assert(col < nColumn_);
625 return prow_[row][col];
628 template<
typename FT>
633 assert(col < nColumn_);
634 return prow_[row][col];
638 template<
typename FT>
644 assert(col < nColumn_);
648 template<
typename FT>
654 assert(col < nColumn_);
655 return prow_[row][col];
661 template<
typename FT>
663 for (
int i = 0; i < nRow_; i++) {
664 for (
int j = 0; j < nColumn_; j++) {
675 template<
typename FT>
677 for (
int i = 0; i < nRow_; i++) {
678 for (
int j = 0; j < nColumn_; j++) {
679 prow_[i][j] = (i == j) ? v : FT(0);
685 template<
typename FT>
687 for (
int i = 0; i < nRow_; i++) {
688 for (
int j = 0; j < nColumn_; j++) {
694 template<
typename FT>
699 template<
typename FT>
708 template<
typename FT>
710 if (
rows == nRow_ &&
cols == nColumn_)
723 template<
typename FT>
727 std::vector<FT> tmp(nColumn_);
728 for (
int j = 0; j < nColumn_; ++j)
729 tmp[j] = prow_[row][j];
738 template<
typename FT>
741 assert(col < nColumn_);
742 std::vector<FT> tmp(nRow_);
743 for (
int i = 0; i < nRow_; ++i)
744 tmp[i] = prow_[i][col];
753 template<
typename FT>
757 assert(v.size() == nColumn_);
758 for (
int j = 0; j < nColumn_; ++j)
759 prow_[row][j] = v[j];
766 template<
typename FT>
769 assert(col < nColumn_);
770 assert(v.size() == nRow_);
771 for (
int i = 0; i < nRow_; ++i)
772 prow_[i][col] = v[i];
776 template<
typename FT>
779 for (
int r = 0; r < nRow_; r++) {
780 for (
int c = 0; c < nColumn_; c++) {
781 t(c, r) = (*this)(r, c);
788 template<
typename FT>
790 assert(nRow_ == nColumn_ && nRow_ != 0);
793 }
else if(nRow_ == 2) {
794 return data_[0]*data_[3] - data_[1]*data_[2];
795 }
else if(nRow_ == 3) {
796 return - data_[8]*data_[1]*data_[3] - data_[7]*data_[5]*data_[0] - data_[2]*data_[4]*data_[6]
797 + data_[6]*data_[1]*data_[5] + data_[7]*data_[3]*data_[2] + data_[0]*data_[4]*data_[8];
800 for(
int i = 0; i < nRow_; i++){
801 value += std::pow(-1.0, i) * data_[i * nColumn_] * cofactor(i, 0).determinant();
808 template<
typename FT>
810 assert(nRow_ == nColumn_);
813 mat.
data()[0] = 1.0 / data_[0];
816 for(
int i = 0; i < nRow_; i++){
817 for(
int j = 0; j < nColumn_; j++){
818 mat.
data()[i * mat.
cols() + j] = std::pow(-1.0, i + j) * cofactor(j, i).determinant();
826 template<
typename FT>
828 int range = std::min(nRow_, nColumn_);
830 for (
int i = 0; i < range; i++) {
831 trac += (*this)[i][i];
839 template<
typename FT>
844 for (
int i = 0; i < nRow_; ++i) {
846 for (
int j = 0; j < nColumn_; ++j)
853 template<
typename FT>
855 assert(nRow_ == rhs.
rows());
856 assert(nColumn_ == rhs.
cols());
858 FT **rowPtrL = prow_;
860 FT **rowPtrR = rhs.prow_;
861 const FT *colPtrR = 0;
863 for (
int i = 0; i < nRow_; ++i) {
864 colPtrL = *rowPtrL++;
865 colPtrR = *rowPtrR++;
866 for (
int j = 0; j < nColumn_; ++j)
867 *colPtrL++ += *colPtrR++;
877 template<
typename FT>
882 for (
int i = 0; i < nRow_; ++i) {
884 for (
int j = 0; j < nColumn_; ++j)
891 template<
typename FT>
893 assert(nRow_ == rhs.
rows());
894 assert(nColumn_ == rhs.
cols());
896 FT **rowPtrL = prow_;
898 FT **rowPtrR = rhs.prow_;
899 const FT *colPtrR = 0;
901 for (
int i = 0; i < nRow_; ++i) {
902 colPtrL = *rowPtrL++;
903 colPtrR = *rowPtrR++;
904 for (
int j = 0; j < nColumn_; ++j)
905 *colPtrL++ -= *colPtrR++;
915 template<
typename FT>
916 template<
typename FT2>
921 for (
int i = 0; i < nRow_; ++i) {
923 for (
int j = 0; j < nColumn_; ++j)
934 template<
typename FT>
935 template<
typename FT2>
940 for (
int i = 0; i < nRow_; ++i) {
942 for (
int j = 0; j < nColumn_; ++j)
952 template<
typename FT>
957 out <<
"size: " << rows <<
" by " << cols <<
"\n";
958 for (
int i = 0; i < rows; ++i) {
959 for (
int j = 0; j < cols; ++j) {
960 if (std::abs(A[i][j]) < 1e-6)
963 out << A[i][j] <<
"\t";
975 template<
typename FT>
980 if (!(rows == A.
rows() && cols == A.
cols()))
983 for (
int i = 0; i < rows; ++i)
984 for (
int j = 0; j < cols; ++j)
994 template<
typename FT>
1000 for (
int i = 0; i < rows; ++i)
1001 for (
int j = 0; j < cols; ++j)
1002 tmp[i][j] = -A[i][j];
1009 template<
typename FT>
1016 template<
typename FT>
1025 template<
typename FT>
1033 template<
typename FT>
1040 template<
typename FT>
1048 template<
typename FT>
1057 template<
typename FT>
1061 int rows = A1.
rows();
1062 int cols = A2.
cols();
1083 template<
typename FT>
1085 assert(A.
cols() == b.size());
1087 int rows = A.
rows();
1090 std::vector<FT> tmp(rows);
1097 template<
typename FT1,
typename FT2>
1105 template<
typename FT1,
typename FT2>
1112 template<
typename FT1,
typename FT2>
1120 template<
typename FT1,
typename FT2>
1123 int rows = A.
rows();
1124 int clumns = A.
cols();
1127 for (
int i = 0; i < rows; ++i)
1128 for (
int j = 0; j < clumns; ++j)
1129 tmp[i][j] = s / A[i][j];
1138 template<
typename FT>
1144 assert(B.
rows() == K);
1148 const FT *pRow, *pCol;
1150 for (
int i = 0; i < M; i++)
1151 for (
int j = 0; j < N; ++j) {
1156 for (
int k = 0; k < K; ++k) {
1157 sum += (*pRow) * (*pCol);
1170 template<
typename FT>
1175 assert(b.size() == N);
1179 const FT *pRow, *pCol;
1181 for (
int i = 0; i < M; i++) {
1186 for (
int j = 0; j < N; ++j) {
1187 sum += (*pRow) * (*pCol);
1200 template<
typename FT>
1206 assert(B.
rows() == K);
1210 const FT *pRow, *pCol;
1212 for (
int i = 0; i < M; i++)
1213 for (
int j = 0; j < N; ++j) {
1218 for (
int k = 0; k < K; ++k) {
1219 sum += (*pRow) * (*pCol);
1233 template<
typename FT>
1238 assert(b.size() == N);
1240 std::vector<FT> c(M);
1242 const FT *pRow, *pCol;
1244 for (
int i = 0; i < M; i++) {
1249 for (
int j = 0; j < N; ++j) {
1250 sum += (*pRow) * (*pCol);
1261 template<
typename FT>
1268 template<
typename FT>
1275 template<
typename FT>
1282 template<
typename FT>
1289 template<
typename FT>
1291 int rows = A.
cols();
1292 int clumns = A.
rows();
1295 for (
int i = 0; i < rows; ++i)
1296 for (
int j = 0; j < clumns; ++j)
1297 tmp[i][j] = A[j][i];
1304 template<
typename FT>
1306 int rows = A.
cols();
1307 int clumns = A.
rows();
1310 for (
int i = 0; i < rows; ++i)
1311 for (
int j = 0; j < clumns; ++j)
1312 tmp[i][j] = conj(A[j][i]);
1319 template<
typename FT>
1323 int rows = A1.
cols();
1324 int cols = A2.
cols();
1328 for (
int i = 0; i < rows; ++i)
1329 for (
int j = 0; j < cols; ++j)
1330 for (
int k = 0; k < K; ++k)
1331 tmp[i][j] += A1[k][i] * A2[k][j];
1338 template<
typename FT>
1340 assert(A.
rows() == v.size());
1342 int rows = A.
rows();
1343 int cols = A.
cols();
1345 std::vector<FT> tmp(cols);
1346 for (
int i = 0; i < cols; ++i)
1347 for (
int j = 0; j < rows; ++j)
1348 tmp[i] += A[j][i] * v[j];
1355 template<
typename FT>
1359 int rows = A1.
rows();
1360 int cols = A2.
rows();
1364 for (
int i = 0; i < rows; ++i)
1365 for (
int j = 0; j < cols; ++j)
1366 for (
int k = 0; k < K; ++k)
1367 tmp[i][j] += A1[i][k] * A2[j][k];
1374 template<
typename FT>
1376 int rows = a.size();
1377 int cols = b.size();
1380 for (
int i = 0; i < rows; ++i)
1381 for (
int j = 0; j < cols; ++j)
1382 tmp[i][j] = a[i] * b[j];
1389 template<
typename FT>
1392 for (
int i = 0; i < row; i++) {
1393 for (
int j = 0; j < col; j++) {
1400 template<
typename FT>
1403 for (
int i = 0; i < N; ++i)
1411 template<
typename FT>
1413 int nColumn_ = A.
rows();
1414 if (nColumn_ > A.
cols())
1415 nColumn_ = A.
cols();
1417 std::vector<FT> tmp(nColumn_);
1418 for (
int i = 0; i < nColumn_; ++i)
1426 template<
typename FT>
1428 int N =
static_cast<int>(d.size());
1431 for (
int i = 0; i < N; ++i)
1442 template<
typename FT>
1444 int range = std::min(A.
rows(), A.
cols());
1446 for (
int i = 0; i < range; i++) {
1454 template<
typename FT>
1460 for (
int i = 0; i < m; ++i)
1461 for (
int j = 0; j < n; ++j)
1462 sum += A[i][j] * A[i][j];
1464 return std::sqrt(
sum);
1469 template<
typename FT>
1474 assert(m == rhs.
rows());
1475 assert(n == rhs.
cols());
1477 for (
int i = 0; i < m; ++i)
1478 for (
int j = 0; j < n; ++j)
1479 std::swap(lhs(i, j), rhs(i, j));
1484 template<
typename FT>
1488 std::vector<FT> s(n);
1490 for (
int j = 0; j < n; ++j)
1491 for (
int i = 0; i < m; ++i)
1498 template<
typename FT>
1502 std::vector<FT>
sum(n);
1504 for (
int j = 0; j < n; ++j) {
1506 for (
int i = 1; i < m; ++i)
1517 template<
typename FT>
1521 std::vector<FT>
sum(n);
1523 for (
int j = 0; j < n; ++j) {
1525 for (
int i = 1; i < m; ++i)
1536 template<
typename FT>
1543 template<
typename FT>
1549 for (
int i = 0; i < m; ++i)
1550 for (
int j = 0; j < n; ++j)
1551 tmp[i][j] = std::abs(A[i][j]);
1557 template<
typename FT>
1563 for (
int i = 0; i < m; ++i)
1564 for (
int j = 0; j < n; ++j)
1565 tmp[i][j] =
arg(A[i][j]);
1571 template<
typename FT>
1577 for (
int i = 0; i < m; ++i)
1578 for (
int j = 0; j < n; ++j)
1579 tmp[i][j] = A[i][j].
real();
1586 template<
typename FT>
1592 for (
int i = 0; i < m; ++i)
1593 for (
int j = 0; j < n; ++j)
1594 tmp[i][j] = A[i][j].
imag();
1601 template<
typename FT>
1603 int rows = rA.
rows();
1604 int cols = rA.
cols();
1607 for (
int i = 0; i < rows; ++i)
1608 for (
int j = 0; j < cols; ++j)
1609 cA[i][j] = rA[i][j];
1615 template<
typename FT>
1617 int rows = mR.
rows();
1618 int cols = mR.
cols();
1620 assert(rows == mI.
rows());
1621 assert(cols == mI.
cols());
1624 for (
int i = 0; i < rows; ++i)
1625 for (
int j = 0; j < cols; ++j)
1626 cA[i][j] = std::complex<FT>(mR[i][j], mI[i][j]);
1636 template<
typename FT>
1637 std::ostream &
operator<<(std::ostream &out,
const std::vector<FT> &A) {
1638 out <<
"size: " << A.size() <<
"\n";
1639 for (std::size_t i = 0; i < A.size(); ++i)
1640 out << A[i] <<
"\t";
1647 template<
typename FT>
1652 for (
int i = 0; i < size; ++i)
1659 template<
typename FT>
1661 std::vector<FT> tmp = A;
1662 for (std::size_t i = 0; i < tmp.size(); ++i)
1670 template<
typename FT>
1671 std::vector<FT>
operator+(
const std::vector<FT> &A,
const std::vector<FT> &B) {
1672 assert(A.size() == B.size());
1674 std::vector<FT> tmp = A;
1675 for (std::size_t i = 0; i < B.size(); ++i)
1682 template<
typename FT>
1683 std::vector<FT>
operator+(
const std::vector<FT> &A,
const FT &s) {
1684 std::vector<FT> tmp = A;
1685 for (std::size_t i = 0; i < A.size(); ++i)
1692 template<
typename FT>
1693 std::vector<FT>
operator+(
const FT &s,
const std::vector<FT> &A) {
1694 std::vector<FT> tmp = A;
1695 for (std::size_t i = 0; i < A.size(); ++i)
1702 template<
typename FT>
1703 std::vector<FT>
operator-(
const std::vector<FT> &A,
const std::vector<FT> &B) {
1704 assert(A.size() == B.size());
1706 std::vector<FT> tmp = A;
1707 for (std::size_t i = 0; i < B.size(); ++i)
1714 template<
typename FT>
1715 std::vector<FT>
operator-(
const std::vector<FT> &A,
const FT &s) {
1716 std::vector<FT> tmp = A;
1717 for (std::size_t i = 0; i < A.size(); ++i)
1724 template<
typename FT>
1725 std::vector<FT>
operator-(
const FT &s,
const std::vector<FT> &A) {
1726 std::vector<FT> tmp = A;
1727 for (std::size_t i = 0; i < A.size(); ++i)
1728 tmp[i] = s - tmp[i];
1734 template<
typename FT1,
typename FT2>
1735 std::vector<FT1>
operator*(
const std::vector<FT1> &A,
const FT2 &s) {
1736 std::vector<FT1> tmp = A;
1737 for (std::size_t i = 0; i < A.size(); ++i)
1744 template<
typename FT1,
typename FT2>
1745 std::vector<FT1>
operator*(
const FT2 &s,
const std::vector<FT1> &A) {
1750 template<
typename FT1,
typename FT2>
1751 std::vector<FT1>
operator/(
const std::vector<FT1> &A,
const FT2 &s) {
1752 std::vector<FT1> tmp = A;
1753 for (std::size_t i = 0; i < A.size(); ++i)
1760 template<
typename FT1,
typename FT2>
1761 std::vector<FT1>
operator/(
const FT2 &s,
const std::vector<FT1> &A) {
1762 std::vector<FT1> tmp = A;
1763 for (std::size_t i = 0; i < A.size(); ++i)
1764 tmp[i] = s / tmp[i];
1770 template<
typename FT>
1771 inline std::vector<FT>
elem_mult(
const std::vector<FT> &v1,
const std::vector<FT> &v2) {
1772 assert(v1.size() == v2.size());
1773 std::vector<FT> result = v1;
1774 for (std::size_t i = 0; i < v2.size(); ++i) {
1782 template<
typename FT>
1783 inline std::vector<FT> &
elem_mult_eq(std::vector<FT> &v1,
const std::vector<FT> &v2) {
1784 assert(v1.size() == v2.size());
1785 for (std::size_t i = 0; i < v2.size(); ++i) {
1794 template<
typename FT>
1795 inline std::vector<FT>
elem_divd(
const std::vector<FT> &v1,
const std::vector<FT> &v2) {
1796 assert(v1.size() == v2.size());
1797 std::vector<FT> result = v1;
1798 for (std::size_t i = 0; i < v2.size(); ++i) {
1806 template<
typename FT>
1807 inline std::vector<FT> &
elem_divd_eq(std::vector<FT> &v1,
const std::vector<FT> &v2) {
1808 assert(v1.size() == v2.size());
1809 for (std::size_t i = 0; i < v2.size(); ++i) {
1818 template<
typename FT>
1819 FT
operator*(
const std::vector<FT> &v1,
const std::vector<FT> &v2) {
1820 assert(v1.size() == v2.size());
1823 typename std::vector<FT>::const_iterator itr1 = v1.begin();
1824 typename std::vector<FT>::const_iterator itr2 = v2.begin();
1826 while (itr1 != v1.end())
1827 sum += (*itr1++) * (*itr2++);
1833 template<
typename FT>
1834 FT
dot(
const std::vector<FT> &v1,
const std::vector<FT> &v2) {
1835 assert(v1.size() == v2.size());
1838 typename std::vector<FT>::const_iterator itr1 = v1.begin();
1839 typename std::vector<FT>::const_iterator itr2 = v2.begin();
1841 while (itr1 != v1.end())
1842 sum += (*itr1++) * (*itr2++);
1849 template<
typename FT>
1850 FT
sum(
const std::vector<FT> &v) {
1852 typename std::vector<FT>::const_iterator itr = v.begin();
1854 while (itr != v.end())
1861 template<
typename FT>
1862 FT
min(
const std::vector<FT> &v) {
1864 for (std::size_t i = 1; i < v.size(); ++i)
1872 template<
typename FT>
1873 FT
max(
const std::vector<FT> &v) {
1875 for (std::size_t i = 1; i < v.size(); ++i)
1883 template<
typename FT>
1884 inline FT
mean(
const std::vector<FT> &v) {
1885 return sum(v) / FT(v.size());
1889 template<
typename FT>
1891 std::vector<FT> V(size);
1898 template<
typename FT>
1899 inline FT
norm(
const std::vector<FT> &v) {
1901 typename std::vector<FT>::const_iterator itr = v.begin();
1903 while (itr != v.end()) {
1904 sum += (*itr) * (*itr);
1908 return FT(std::sqrt(
sum));
1912 template<
typename FT>
1913 FT
norm(
const std::vector<std::complex<FT> > &v) {
1915 typename std::vector<std::complex<FT> >::const_iterator itr = v.begin();
1917 while (itr != v.end()) {
1918 sum += std::norm(*itr++);
1921 return FT(std::sqrt(
sum));
1925 template<
typename FT>
1926 void swap(std::vector<FT> &lhs, std::vector<FT> &rhs) {
1927 typename std::vector<FT>::iterator itrL = lhs.begin(),
1930 while (itrL != lhs.end())
1931 std::swap(*itrL++, *itrR++);
1935 template<
typename FT>
1938 return std::vector<FT>();
1940 return std::vector<FT>(1, a);
1942 FT dx = (b - a) / (n - 1);
1944 std::vector<FT> tmp(n);
1945 for (
int i = 0; i < n; ++i)
1946 tmp[i] = a + i * dx;
1953 template<
typename FT>
1954 std::vector<FT>
abs(
const std::vector<std::complex<FT> > &v) {
1955 std::vector<FT> tmp(v.size());
1956 typename std::vector<FT>::iterator itrL = tmp.begin();
1957 typename std::vector<std::complex<FT> >::const_iterator itrR = v.begin();
1959 while (itrL != tmp.end())
1960 *itrL++ = std::abs(*itrR++);
1966 template<
typename FT>
1967 std::vector<FT>
arg(
const std::vector<std::complex<FT> > &v) {
1968 std::vector<FT> tmp(v.size());
1969 typename std::vector<FT>::iterator itrL = tmp.begin();
1970 typename std::vector<std::complex<FT> >::const_iterator itrR = v.begin();
1972 while (itrL != tmp.end())
1973 *itrL++ = std::arg(*itrR++);
1979 template<
typename FT>
1980 std::vector<FT>
real(
const std::vector<std::complex<FT> > &v) {
1981 std::vector<FT> tmp(v.size());
1982 typename std::vector<FT>::iterator itrL = tmp.begin();
1983 typename std::vector<std::complex<FT> >::const_iterator itrR = v.begin();
1985 while (itrL != tmp.end())
1986 *itrL++ = (*itrR++).
real();
1992 template<
typename FT>
1993 std::vector<FT>
imag(
const std::vector<std::complex<FT> > &v) {
1994 std::vector<FT> tmp(v.size());
1995 typename std::vector<FT>::iterator itrL = tmp.begin();
1996 typename std::vector<std::complex<FT> >::const_iterator itrR = v.begin();
1998 while (itrL != tmp.end())
1999 *itrL++ = (*itrR++).
imag();
2005 template<
typename FT>
2009 std::vector<std::complex<FT> > cv(N);
2010 typename std::vector<std::complex<FT> >::iterator itrL = cv.begin();
2011 typename std::vector<FT>::const_iterator itrR = rv.begin();
2013 while (itrR != rv.end())
2020 template<
typename FT>
2021 std::vector<std::complex<FT> >
complex_vector(
const std::vector<FT> &vR,
const std::vector<FT> &vI) {
2022 int N =
static_cast<int>(vR.size());
2024 assert(N == vI.size());
2026 std::vector<std::complex<FT> > cv(N);
2027 typename std::vector<std::complex<FT> >::iterator itrC = cv.begin();
2028 typename std::vector<FT>::const_iterator itrR = vR.begin(),
2031 while (itrC != cv.end())
2032 *itrC++ = std::complex<FT>(*itrR++, *itrI++);
A matrix representation, which supports dynamic sizes.
Definition matrix.h:48
const FT * operator[](int row) const
Return the row_th row as a 1D array (const version).
Definition matrix.h:613
FT * data()
Definition matrix.h:124
Matrix< FT > & operator*=(const FT2 &v)
computed assignment. Multiply each element by v
Definition matrix.h:917
Matrix< FT > & operator/=(const FT2 &v)
computed assignment. Divide each element by v
Definition matrix.h:936
FT & operator()(int row, int col)
Return the element at (row, col).
Definition matrix.h:620
Matrix(int rows, int cols, const FT &x=FT(0))
Definition matrix.h:548
Matrix()
Definition matrix.h:537
Matrix< FT > & operator+=(const FT &v)
computed assignment. Add each element by v
Definition matrix.h:840
Matrix< FT > & operator=(const Matrix< FT > &A)
Assign A to this matrix.
Definition matrix.h:576
Matrix(const Matrix< FT > &A)
Definition matrix.h:542
Matrix< FT > & operator-=(const Matrix< FT > &)
computed assignment. Subtract A from this matrix.
Definition matrix.h:892
const FT & get(int row, int col) const
Get the value of the element at (row, col).
Definition matrix.h:650
void load_identity(FT v=FT(1))
Definition matrix.h:676
Matrix(int rows, int cols, const std::vector< FT > &array)
Definition matrix.h:554
std::vector< FT > get_column(int col) const
Get the matrix's column vector as a 1D array.
Definition matrix.h:739
Matrix< FT > & operator+=(const Matrix< FT > &A)
computed assignment. Add A to this matrix.
Definition matrix.h:854
const FT * data() const
Definition matrix.h:130
void set_column(const std::vector< FT > &v, int col)
Set the matrix's column vector.
Definition matrix.h:767
void load_zero()
Set all elements to zero.
Definition matrix.h:662
Matrix< FT > & resize(int rows, int cols)
Change the size/dimension of the matrix.
Definition matrix.h:709
FT * operator[](int row)
Return the row_th row as a 1D array.
Definition matrix.h:606
std::vector< FT > get_row(int row) const
Get the matrix's row vector as a 1D array.
Definition matrix.h:724
Matrix< FT > inverse() const
Return the inverse of the matrix (for positive square matrices only)
Definition matrix.h:809
const FT & operator()(int row, int col) const
Return the element at (row, col) (const version).
Definition matrix.h:629
void set_row(const std::vector< FT > &v, int row)
Set the matrix's row vector.
Definition matrix.h:754
void set(int row, int col, FT v)
Set the value of the element at (row, col).
Definition matrix.h:640
Matrix< FT > transpose() const
Return the transposed matrix.
Definition matrix.h:777
Matrix< FT > & operator-=(const FT &v)
computed assignment. Subtract each element by v
Definition matrix.h:878
FT trace() const
Definition matrix.h:827
Matrix(int rows, int cols, const FT *array)
Definition matrix.h:561
Matrix< FT > & operator=(const FT &x)
Assign x to every entry of this matrix.
Definition matrix.h:596
FT determinant() const
Return the determinant of the matrix (for square matrices only)
Definition matrix.h:789
void load_random()
Set all elements to a random value (in the range [0.0, 1.0])
Definition matrix.h:686
Definition collider.cpp:182
Matrix< FT > operator+(const Matrix< FT > &, const FT &)
Definition matrix.h:1010
T trace(const Mat< N, N, T > &m)
Returns the trace (sum of elements on the main diagonal) of an N x N (square) matrix.
Mat< M, N, T > transpose(const Mat< N, M, T > &m)
Transposes a matrix.
Definition mat.h:1120
Matrix< FT > operator-(const Matrix< FT > &)
arithmetic operators
Definition matrix.h:995
Matrix< FT > & elem_divd_eq(Matrix< FT > &, const Matrix< FT > &)
Definition matrix.h:1283
Matrix< FT > elem_divd(const Matrix< FT > &, const Matrix< FT > &)
Definition matrix.h:1276
std::vector< FT > mean(const Matrix< FT > &)
Definition matrix.h:1537
Matrix< FT > transpose_mult(const Matrix< FT > &, const Matrix< FT > &)
transpose multiplication
Definition matrix.h:1320
Matrix< FT > diagonal(const std::vector< FT > &)
diagonal matrix
Definition matrix.h:1427
Matrix< FT > elem_mult(const Matrix< FT > &, const Matrix< FT > &)
element-wise multiplication / division
Definition matrix.h:1262
Matrix< FT1 > operator/(const Matrix< FT1 > &, const FT2 &)
matrix-scalar division
Definition matrix.h:1114
Matrix< FT > mult_transpose(const Matrix< FT > &, const Matrix< FT > &)
Definition matrix.h:1356
FT min()
Function returning minimum representable value for a given type.
void swap(Matrix< FT > &, Matrix< FT > &)
Definition matrix.h:1470
Matrix< FT > random(int row, int col)
Generate a matrix with all its elements randomly initialized in the range [0.0, 1....
Definition matrix.h:1390
Matrix< FT > identity(int N, const FT &v=FT(1))
Definition matrix.h:1401
Matrix< std::complex< FT > > complex_matrix(const Matrix< FT > &)
Convert real matrix to complex matrix.
Definition matrix.h:1602
float random_float()
Random real in [0, 1].
Definition random.h:39
Matrix< FT > real(const Matrix< std::complex< FT > > &A)
Get real part of a std::complex matrix.
Definition matrix.h:1572
std::vector< std::complex< FT > > complex_vector(const std::vector< FT > &)
Get imaginary part of a complex vector.
Definition matrix.h:2006
std::vector< FT > linspace(FT, FT, int)
Swap two vectors.
Definition matrix.h:1936
Matrix< FT > imag(const Matrix< std::complex< FT > > &A)
Get imaginary part of a std::complex matrix.
Definition matrix.h:1587
T determinant(const Mat< N, N, T > &m)
Computes the determinant of a square matrix.
Definition mat.h:1143
void mult(const Matrix< FT > &, const Matrix< FT > &, Matrix< FT > &)
multiplication
Definition matrix.h:1139
std::ostream & operator<<(std::ostream &os, Graph::Vertex v)
Output stream support for Graph::Vertex.
Definition graph.h:1300
FT max()
Function returning maximum representable value for a given type.
std::istream & operator>>(std::istream &is, GenericLine< DIM, FT > &line)
Input stream support for GenericLine.
Definition line.h:183
Matrix< FT > arg(const Matrix< std::complex< FT > > &A)
Get angle of a std::complex matrix.
Definition matrix.h:1558
Matrix< FT > abs(const Matrix< std::complex< FT > > &A)
Get magnitude of a std::complex matrix.
Definition matrix.h:1544
FT dot(const std::vector< FT > &, const std::vector< FT > &)
Inner product for vectors.
Definition matrix.h:1834
std::vector< FT > sum(const Matrix< FT > &)
Definition matrix.h:1485
Matrix< FT > conjugate_transpose(const Matrix< FT > &)
Definition matrix.h:1305
FT norm(const Matrix< FT > &)
utilities
Definition matrix.h:1455
Matrix< FT > & elem_mult_eq(Matrix< FT > &, const Matrix< FT > &)
Definition matrix.h:1269
Mat< N, M, T > operator*(T lhs, const Mat< N, M, T > &rhs)
Multiplies a scalar by a matrix.
Definition mat.h:1000