27#ifndef EASY3D_CORE_MATRIX_H
28#define EASY3D_CORE_MATRIX_H
113 void set(
int row,
int col, FT v);
116 const FT &
get(
int row,
int col)
const;
128 const FT *
data()
const {
return data_; }
140 std::vector <FT>
get_row(
int row)
const;
146 void set_row(
const std::vector <FT> &v,
int row);
149 void set_column(
const std::vector <FT> &v,
int col);
176 template<
typename FT2>
179 template<
typename FT2>
202 void copy_from_array(
const FT *v);
203 void set_by_scalar(
const FT &x);
215 template<
typename FT>
216 std::ostream &
operator<<(std::ostream &,
const Matrix<FT> &);
217 template<
typename FT>
218 std::istream &
operator>>(std::istream &, Matrix<FT> &);
221 template<
typename FT>
222 Matrix<FT>
operator-(
const Matrix<FT> &);
223 template<
typename FT>
224 Matrix<FT>
operator+(
const Matrix<FT> &,
const FT &);
225 template<
typename FT>
226 Matrix<FT>
operator+(
const FT &,
const Matrix<FT> &);
227 template<
typename FT>
228 Matrix<FT>
operator+(
const Matrix<FT> &,
const Matrix<FT> &);
229 template<
typename FT>
230 Matrix<FT>
operator-(
const Matrix<FT> &,
const FT &);
231 template<
typename FT>
232 Matrix<FT>
operator-(
const FT &,
const Matrix<FT> &);
233 template<
typename FT>
234 Matrix<FT>
operator-(
const Matrix<FT> &,
const Matrix<FT> &);
235 template<
typename FT>
236 Matrix<FT>
operator*(
const Matrix<FT> &,
const Matrix<FT> &);
237 template<
typename FT>
238 std::vector <FT>
operator*(
const Matrix<FT> &,
const std::vector <FT> &);
239 template<
typename FT1,
typename FT2>
240 Matrix<FT1>
operator*(
const Matrix<FT1> &,
const FT2 &);
241 template<
typename FT1,
typename FT2>
242 Matrix<FT1>
operator*(
const FT2 &,
const Matrix<FT1> &);
243 template<
typename FT1,
typename FT2>
244 Matrix<FT1>
operator/(
const Matrix<FT1> &,
const FT2 &);
245 template<
typename FT1,
typename FT2>
246 Matrix<FT1>
operator/(
const FT2 &,
const Matrix<FT1> &);
250 template<
typename FT>
251 void mult(
const Matrix<FT> &,
const Matrix<FT> &,
253 template<
typename FT>
254 void mult(
const Matrix<FT> &,
const std::vector <FT> &,
256 template<
typename FT>
257 Matrix<FT>
mult(
const Matrix<FT> &,
const Matrix<FT> &);
258 template<
typename FT>
259 std::vector <FT>
mult(
const Matrix<FT> &,
const std::vector <FT> &);
262 template<
typename FT>
263 Matrix<FT>
elem_mult(
const Matrix<FT> &,
const Matrix<FT> &);
264 template<
typename FT>
265 Matrix<FT>
elem_divd(
const Matrix<FT> &,
const Matrix<FT> &);
266 template<
typename FT>
267 Matrix<FT> &
elem_mult_eq(Matrix<FT> &,
const Matrix<FT> &);
268 template<
typename FT>
269 Matrix<FT> &
elem_divd_eq(Matrix<FT> &,
const Matrix<FT> &);
272 template<
typename FT>
273 Matrix<FT>
transpose(
const Matrix<FT> &);
274 template<
typename FT>
278 template<
typename FT>
279 Matrix<FT>
transpose_mult(
const Matrix<FT> &,
const Matrix<FT> &);
280 template<
typename FT>
281 std::vector <FT>
transpose_mult(
const Matrix<FT> &,
const std::vector <FT> &);
282 template<
typename FT>
283 Matrix<FT>
mult_transpose(
const Matrix<FT> &,
const Matrix<FT> &);
284 template<
typename FT>
285 Matrix<FT>
mult_transpose(
const std::vector <FT> &,
const std::vector <FT> &);
288 template<
typename FT>
289 Matrix<FT>
identity(
int,
const FT &);
290 template<
typename FT>
291 Matrix<FT>
diagonal(
const std::vector <FT> &);
292 template<
typename FT>
293 std::vector <FT>
diagonal(
const Matrix<FT> &);
297 template<
typename FT>
298 FT
trace(
const Matrix<FT> &);
301 template<
typename FT>
302 FT
norm(
const Matrix<FT> &);
303 template<
typename FT>
304 void swap(Matrix<FT> &, Matrix<FT> &);
305 template<
typename FT>
306 std::vector <FT>
sum(
const Matrix<FT> &);
307 template<
typename FT>
308 std::vector <FT>
min(
const Matrix<FT> &);
309 template<
typename FT>
310 std::vector <FT>
max(
const Matrix<FT> &);
311 template<
typename FT>
312 std::vector <FT>
mean(
const Matrix<FT> &);
313 template<
typename FT>
314 Matrix<FT>
abs(
const Matrix<std::complex < FT>
317 template<
typename FT>
318 Matrix<FT>
arg(
const Matrix<std::complex < FT>
321 template<
typename FT>
322 Matrix<FT>
real(
const Matrix<std::complex < FT>
325 template<
typename FT>
326 Matrix<FT>
imag(
const Matrix<std::complex < FT>
331 template<
typename FT>
332 Matrix<std::complex < FT> >
336 template<
typename FT>
337 Matrix<std::complex < FT> >
345 template<
typename FT>
346 std::ostream &
operator<<(std::ostream &,
const std::vector <FT> &);
347 template<
typename FT>
348 std::istream &
operator>>(std::istream &, std::vector <FT> &);
351 template<
typename FT>
352 std::vector <FT>
operator-(
const std::vector <FT> &);
353 template<
typename FT>
354 std::vector <FT>
operator+(
const std::vector <FT> &,
const std::vector <FT> &);
355 template<
typename FT>
356 std::vector <FT>
operator+(
const std::vector <FT> &,
const FT &);
357 template<
typename FT>
358 std::vector <FT>
operator+(
const FT &,
const std::vector <FT> &);
359 template<
typename FT>
360 std::vector <FT>
operator-(
const std::vector <FT> &,
const std::vector <FT> &);
361 template<
typename FT>
362 std::vector <FT>
operator-(
const std::vector <FT> &,
const FT &);
363 template<
typename FT>
364 std::vector <FT>
operator-(
const FT &,
const std::vector <FT> &);
365 template<
typename FT1,
typename FT2>
366 std::vector <FT1>
operator*(
const std::vector <FT1> &,
const FT2 &);
367 template<
typename FT1,
typename FT2>
368 std::vector <FT1>
operator*(
const FT2 &,
const std::vector <FT1> &);
369 template<
typename FT1,
typename FT2>
370 std::vector <FT1>
operator/(
const std::vector <FT1> &,
const FT2 &);
371 template<
typename FT1,
typename FT2>
372 std::vector <FT1>
operator/(
const FT2 &,
const std::vector <FT1> &);
376 template<
typename FT>
377 std::vector <FT>
elem_mult(
const std::vector <FT> &,
const std::vector <FT> &);
378 template<
typename FT>
379 std::vector <FT>
elem_divd(
const std::vector <FT> &,
const std::vector <FT> &);
380 template<
typename FT>
381 std::vector <FT> &
elem_mult_eq(std::vector <FT> &,
const std::vector <FT> &);
382 template<
typename FT>
383 std::vector <FT> &
elem_divd_eq(std::vector <FT> &,
const std::vector <FT> &);
386 template<
typename FT>
387 FT
operator*(
const std::vector <FT> &,
const std::vector <FT> &);
388 template<
typename FT>
389 FT
dot(
const std::vector <FT> &,
const std::vector <FT> &);
392 template<
typename FT>
393 FT
norm(
const std::vector <FT> &);
394 template<
typename FT>
395 FT
norm(
const std::vector <std::complex<FT>> &);
396 template<
typename FT>
397 void swap(std::vector <FT> &, std::vector <FT> &);
398 template<
typename FT>
401 template<
typename FT>
402 FT
sum(
const std::vector <FT> &);
403 template<
typename FT>
404 FT
min(
const std::vector <FT> &);
405 template<
typename FT>
406 FT
max(
const std::vector <FT> &);
407 template<
typename FT>
408 FT
mean(
const std::vector <FT> &);
410 template<
typename FT>
411 std::vector <FT>
abs(
const std::vector <std::complex<FT>> &);
412 template<
typename FT>
413 std::vector <FT>
arg(
const std::vector <std::complex<FT>> &);
414 template<
typename FT>
415 std::vector <FT>
real(
const std::vector <std::complex<FT>> &);
416 template<
typename FT>
417 std::vector <FT>
imag(
const std::vector <std::complex<FT>> &);
420 template<
typename FT>
421 std::vector <std::complex<FT>>
complex_vector(
const std::vector <FT> &);
423 template<
typename FT>
424 std::vector <std::complex<FT>>
425 complex_vector(
const std::vector <FT> &,
const std::vector <FT> &);
437 template<
typename FT>
438 void Matrix<FT>::init(
int rows,
int cols) {
441 nTotal_ = nRow_ * nColumn_;
443 data_ =
new FT[nTotal_];
444 prow_ =
new FT *[nRow_];
446 assert(data_ != NULL);
447 assert(prow_ != NULL);
450 for (
int i = 0; i < nRow_; ++i) {
460 template<
typename FT>
461 inline void Matrix<FT>::copy_from_array(
const FT *v) {
462 for (
long i = 0; i < nTotal_; ++i)
470 template<
typename FT>
471 inline void Matrix<FT>::set_by_scalar(
const FT &x) {
472 for (
long i = 0; i < nTotal_; ++i)
480 template<
typename FT>
481 void Matrix<FT>::destroy() {
492 template<
typename FT>
493 Matrix<FT> Matrix<FT>::cofactor(
int _i,
int _j)
const {
494 assert(0 <= _i && _i < nRow_ && 0 <= _j && _j < nColumn_);
495 Matrix<FT> mat(nRow_ - 1, nColumn_ - 1);
496 for(
int i = 0; i < mat.rows(); i++) {
497 for(
int j = 0; j < mat.cols(); j++){
500 mat.data()[i * mat.cols() + j] = data_[i * nColumn_ + j];
502 mat.data()[i * mat.cols() + j] = data_[i * nColumn_ + (j + 1)];
506 mat.data()[i * mat.cols() + j] = data_[(i + 1) * nColumn_ + j];
508 mat.data()[i * mat.cols() + j] = data_[(i + 1) * nColumn_ + (j + 1)];
521 template<
typename FT>
523 : data_(0), prow_(0), nRow_(0), nColumn_(0), nTotal_(0) {
526 template<
typename FT>
528 init(A.nRow_, A.nColumn_);
529 copy_from_array(A.data_);
532 template<
typename FT>
538 template<
typename FT>
542 copy_from_array(array.data());
545 template<
typename FT>
548 copy_from_array(array);
551 template<
typename FT>
560 template<
typename FT>
562 if (data_ == A.data_)
565 if (nRow_ == A.nRow_ && nColumn_ == A.nColumn_)
566 copy_from_array(A.data_);
569 init(A.nRow_, A.nColumn_);
570 copy_from_array(A.data_);
580 template<
typename FT>
590 template<
typename FT>
597 template<
typename FT>
604 template<
typename FT>
609 assert(col < nColumn_);
610 return prow_[row][col];
613 template<
typename FT>
618 assert(col < nColumn_);
619 return prow_[row][col];
623 template<
typename FT>
629 assert(col < nColumn_);
633 template<
typename FT>
639 assert(col < nColumn_);
640 return prow_[row][col];
648 template<
typename FT>
651 for (
int i = 0; i < nRow_; i++) {
652 for (
int j = 0; j < nColumn_; j++) {
663 template<
typename FT>
666 for (
int i = 0; i < nRow_; i++) {
667 for (
int j = 0; j < nColumn_; j++) {
668 prow_[i][j] = (i == j) ? v : FT(0);
674 template<
typename FT>
679 template<
typename FT>
688 template<
typename FT>
690 if (rows == nRow_ && cols == nColumn_)
703 template<
typename FT>
707 std::vector<FT> tmp(nColumn_);
708 for (
int j = 0; j < nColumn_; ++j)
709 tmp[j] = prow_[row][j];
718 template<
typename FT>
721 assert(col < nColumn_);
722 std::vector<FT> tmp(nRow_);
723 for (
int i = 0; i < nRow_; ++i)
724 tmp[i] = prow_[i][col];
733 template<
typename FT>
737 assert(v.size() == nColumn_);
738 for (
int j = 0; j < nColumn_; ++j)
739 prow_[row][j] = v[j];
746 template<
typename FT>
749 assert(col < nColumn_);
750 assert(v.size() == nRow_);
751 for (
int i = 0; i < nRow_; ++i)
752 prow_[i][col] = v[i];
756 template<
typename FT>
759 for (
int r = 0; r < nRow_; r++) {
760 for (
int c = 0; c < nColumn_; c++) {
761 t(c, r) = (*this)(r, c);
768 template<
typename FT>
770 assert(nRow_ == nColumn_ && nRow_ != 0);
773 }
else if(nRow_ == 2) {
774 return data_[0]*data_[3] - data_[1]*data_[2];
775 }
else if(nRow_ == 3) {
776 return - data_[8]*data_[1]*data_[3] - data_[7]*data_[5]*data_[0] - data_[2]*data_[4]*data_[6]
777 + data_[6]*data_[1]*data_[5] + data_[7]*data_[3]*data_[2] + data_[0]*data_[4]*data_[8];
780 for(
int i = 0; i < nRow_; i++){
781 value += std::pow(-1.0, i) * data_[i * nColumn_] * cofactor(i, 0).determinant();
788 template<
typename FT>
790 assert(nRow_ == nColumn_);
793 mat.
data()[0] = 1.0 / data_[0];
796 for(
int i = 0; i < nRow_; i++){
797 for(
int j = 0; j < nColumn_; j++){
798 mat.
data()[i * mat.
cols() + j] = std::pow(-1.0, i + j) * cofactor(j, i).determinant();
806 template<
typename FT>
808 int range = std::min(nRow_, nColumn_);
810 for (
int i = 0; i < range; i++) {
811 trac += (*this)[i][i];
819 template<
typename FT>
824 for (
int i = 0; i < nRow_; ++i) {
826 for (
int j = 0; j < nColumn_; ++j)
833 template<
typename FT>
835 assert(nRow_ == rhs.
rows());
836 assert(nColumn_ == rhs.
cols());
838 FT **rowPtrL = prow_;
840 FT **rowPtrR = rhs.prow_;
841 const FT *colPtrR = 0;
843 for (
int i = 0; i < nRow_; ++i) {
844 colPtrL = *rowPtrL++;
845 colPtrR = *rowPtrR++;
846 for (
int j = 0; j < nColumn_; ++j)
847 *colPtrL++ += *colPtrR++;
857 template<
typename FT>
862 for (
int i = 0; i < nRow_; ++i) {
864 for (
int j = 0; j < nColumn_; ++j)
871 template<
typename FT>
873 assert(nRow_ == rhs.
rows());
874 assert(nColumn_ == rhs.
cols());
876 FT **rowPtrL = prow_;
878 FT **rowPtrR = rhs.prow_;
879 const FT *colPtrR = 0;
881 for (
int i = 0; i < nRow_; ++i) {
882 colPtrL = *rowPtrL++;
883 colPtrR = *rowPtrR++;
884 for (
int j = 0; j < nColumn_; ++j)
885 *colPtrL++ -= *colPtrR++;
895 template<
typename FT>
896 template<
typename FT2>
901 for (
int i = 0; i < nRow_; ++i) {
903 for (
int j = 0; j < nColumn_; ++j)
914 template<
typename FT>
915 template<
typename FT2>
920 for (
int i = 0; i < nRow_; ++i) {
922 for (
int j = 0; j < nColumn_; ++j)
932 template<
typename FT>
937 out <<
"size: " << rows <<
" by " << cols <<
"\n";
938 for (
int i = 0; i < rows; ++i) {
939 for (
int j = 0; j < cols; ++j) {
940 if (std::abs(A[i][j]) < 1e-6)
943 out << A[i][j] <<
"\t";
955 template<
typename FT>
960 if (!(rows == A.
rows() && cols == A.
cols()))
963 for (
int i = 0; i < rows; ++i)
964 for (
int j = 0; j < cols; ++j)
974 template<
typename FT>
980 for (
int i = 0; i < rows; ++i)
981 for (
int j = 0; j < cols; ++j)
982 tmp[i][j] = -A[i][j];
989 template<
typename FT>
996 template<
typename FT>
1005 template<
typename FT>
1013 template<
typename FT>
1020 template<
typename FT>
1028 template<
typename FT>
1037 template<
typename FT>
1041 int rows = A1.
rows();
1042 int cols = A2.
cols();
1063 template<
typename FT>
1065 assert(A.
cols() == b.size());
1067 int rows = A.
rows();
1070 std::vector<FT> tmp(rows);
1077 template<
typename FT1,
typename FT2>
1085 template<
typename FT1,
typename FT2>
1092 template<
typename FT1,
typename FT2>
1100 template<
typename FT1,
typename FT2>
1103 int rows = A.
rows();
1104 int clumns = A.
cols();
1107 for (
int i = 0; i < rows; ++i)
1108 for (
int j = 0; j < clumns; ++j)
1109 tmp[i][j] = s / A[i][j];
1118 template<
typename FT>
1124 assert(B.
rows() == K);
1128 const FT *pRow, *pCol;
1130 for (
int i = 0; i < M; i++)
1131 for (
int j = 0; j < N; ++j) {
1136 for (
int k = 0; k < K; ++k) {
1137 sum += (*pRow) * (*pCol);
1150 template<
typename FT>
1155 assert(b.size() == N);
1159 const FT *pRow, *pCol;
1161 for (
int i = 0; i < M; i++) {
1166 for (
int j = 0; j < N; ++j) {
1167 sum += (*pRow) * (*pCol);
1180 template<
typename FT>
1186 assert(B.
rows() == K);
1190 const FT *pRow, *pCol;
1192 for (
int i = 0; i < M; i++)
1193 for (
int j = 0; j < N; ++j) {
1198 for (
int k = 0; k < K; ++k) {
1199 sum += (*pRow) * (*pCol);
1213 template<
typename FT>
1218 assert(b.size() == N);
1220 std::vector<FT> c(M);
1222 const FT *pRow, *pCol;
1224 for (
int i = 0; i < M; i++) {
1229 for (
int j = 0; j < N; ++j) {
1230 sum += (*pRow) * (*pCol);
1241 template<
typename FT>
1248 template<
typename FT>
1255 template<
typename FT>
1262 template<
typename FT>
1269 template<
typename FT>
1271 int rows = A.
cols();
1272 int clumns = A.
rows();
1275 for (
int i = 0; i < rows; ++i)
1276 for (
int j = 0; j < clumns; ++j)
1277 tmp[i][j] = A[j][i];
1284 template<
typename FT>
1286 int rows = A.
cols();
1287 int clumns = A.
rows();
1290 for (
int i = 0; i < rows; ++i)
1291 for (
int j = 0; j < clumns; ++j)
1292 tmp[i][j] = conj(A[j][i]);
1299 template<
typename FT>
1303 int rows = A1.
cols();
1304 int cols = A2.
cols();
1308 for (
int i = 0; i < rows; ++i)
1309 for (
int j = 0; j < cols; ++j)
1310 for (
int k = 0; k < K; ++k)
1311 tmp[i][j] += A1[k][i] * A2[k][j];
1318 template<
typename FT>
1320 assert(A.
rows() == v.size());
1322 int rows = A.
rows();
1323 int cols = A.
cols();
1325 std::vector<FT> tmp(cols);
1326 for (
int i = 0; i < cols; ++i)
1327 for (
int j = 0; j < rows; ++j)
1328 tmp[i] += A[j][i] * v[j];
1335 template<
typename FT>
1339 int rows = A1.
rows();
1340 int cols = A2.
rows();
1344 for (
int i = 0; i < rows; ++i)
1345 for (
int j = 0; j < cols; ++j)
1346 for (
int k = 0; k < K; ++k)
1347 tmp[i][j] += A1[i][k] * A2[j][k];
1354 template<
typename FT>
1356 int rows = a.size();
1357 int cols = b.size();
1360 for (
int i = 0; i < rows; ++i)
1361 for (
int j = 0; j < cols; ++j)
1362 tmp[i][j] = a[i] * b[j];
1369 template<
typename FT>
1372 for (
int i = 0; i < N; ++i)
1380 template<
typename FT>
1382 int nColumn_ = A.
rows();
1383 if (nColumn_ > A.
cols())
1384 nColumn_ = A.
cols();
1386 std::vector<FT> tmp(nColumn_);
1387 for (
int i = 0; i < nColumn_; ++i)
1395 template<
typename FT>
1397 int N =
static_cast<int>(d.size());
1400 for (
int i = 0; i < N; ++i)
1411 template<
typename FT>
1413 int range = std::min(A.
rows(), A.
cols());
1415 for (
int i = 0; i < range; i++) {
1423 template<
typename FT>
1429 for (
int i = 0; i < m; ++i)
1430 for (
int j = 0; j < n; ++j)
1431 sum += A[i][j] * A[i][j];
1433 return std::sqrt(
sum);
1438 template<
typename FT>
1443 assert(m == rhs.
rows());
1444 assert(n == rhs.
cols());
1446 for (
int i = 0; i < m; ++i)
1447 for (
int j = 0; j < n; ++j)
1448 std::swap(lhs(i, j), rhs(i, j));
1453 template<
typename FT>
1457 std::vector<FT> s(n);
1459 for (
int j = 0; j < n; ++j)
1460 for (
int i = 0; i < m; ++i)
1467 template<
typename FT>
1471 std::vector<FT>
sum(n);
1473 for (
int j = 0; j < n; ++j) {
1475 for (
int i = 1; i < m; ++i)
1486 template<
typename FT>
1490 std::vector<FT>
sum(n);
1492 for (
int j = 0; j < n; ++j) {
1494 for (
int i = 1; i < m; ++i)
1505 template<
typename FT>
1512 template<
typename FT>
1518 for (
int i = 0; i < m; ++i)
1519 for (
int j = 0; j < n; ++j)
1520 tmp[i][j] = std::abs(A[i][j]);
1526 template<
typename FT>
1532 for (
int i = 0; i < m; ++i)
1533 for (
int j = 0; j < n; ++j)
1534 tmp[i][j] =
arg(A[i][j]);
1540 template<
typename FT>
1546 for (
int i = 0; i < m; ++i)
1547 for (
int j = 0; j < n; ++j)
1548 tmp[i][j] = A[i][j].
real();
1555 template<
typename FT>
1561 for (
int i = 0; i < m; ++i)
1562 for (
int j = 0; j < n; ++j)
1563 tmp[i][j] = A[i][j].
imag();
1570 template<
typename FT>
1572 int rows = rA.
rows();
1573 int cols = rA.
cols();
1576 for (
int i = 0; i < rows; ++i)
1577 for (
int j = 0; j < cols; ++j)
1578 cA[i][j] = rA[i][j];
1584 template<
typename FT>
1586 int rows = mR.
rows();
1587 int cols = mR.
cols();
1589 assert(rows == mI.
rows());
1590 assert(cols == mI.
cols());
1593 for (
int i = 0; i < rows; ++i)
1594 for (
int j = 0; j < cols; ++j)
1595 cA[i][j] = std::complex<FT>(mR[i][j], mI[i][j]);
1605 template<
typename FT>
1606 std::ostream &
operator<<(std::ostream &out,
const std::vector<FT> &A) {
1607 out <<
"size: " << A.size() <<
"\n";
1608 for (std::size_t i = 0; i < A.size(); ++i)
1609 out << A[i] <<
"\t";
1616 template<
typename FT>
1621 for (
int i = 0; i < size; ++i)
1628 template<
typename FT>
1630 std::vector<FT> tmp = A;
1631 for (std::size_t i = 0; i < tmp.size(); ++i)
1639 template<
typename FT>
1640 std::vector<FT>
operator+(
const std::vector<FT> &A,
const std::vector<FT> &B) {
1641 assert(A.size() == B.size());
1643 std::vector<FT> tmp = A;
1644 for (std::size_t i = 0; i < B.size(); ++i)
1651 template<
typename FT>
1652 std::vector<FT>
operator+(
const std::vector<FT> &A,
const FT &s) {
1653 std::vector<FT> tmp = A;
1654 for (std::size_t i = 0; i < A.size(); ++i)
1661 template<
typename FT>
1662 std::vector<FT>
operator+(
const FT &s,
const std::vector<FT> &A) {
1663 std::vector<FT> tmp = A;
1664 for (std::size_t i = 0; i < A.size(); ++i)
1671 template<
typename FT>
1672 std::vector<FT>
operator-(
const std::vector<FT> &A,
const std::vector<FT> &B) {
1673 assert(A.size() == B.size());
1675 std::vector<FT> tmp = A;
1676 for (std::size_t i = 0; i < B.size(); ++i)
1683 template<
typename FT>
1684 std::vector<FT>
operator-(
const std::vector<FT> &A,
const FT &s) {
1685 std::vector<FT> tmp = A;
1686 for (std::size_t i = 0; i < A.size(); ++i)
1693 template<
typename FT>
1694 std::vector<FT>
operator-(
const FT &s,
const std::vector<FT> &A) {
1695 std::vector<FT> tmp = A;
1696 for (std::size_t i = 0; i < A.size(); ++i)
1697 tmp[i] = s - tmp[i];
1703 template<
typename FT1,
typename FT2>
1704 std::vector<FT1>
operator*(
const std::vector<FT1> &A,
const FT2 &s) {
1705 std::vector<FT1> tmp = A;
1706 for (std::size_t i = 0; i < A.size(); ++i)
1713 template<
typename FT1,
typename FT2>
1714 std::vector<FT1>
operator*(
const FT2 &s,
const std::vector<FT1> &A) {
1719 template<
typename FT1,
typename FT2>
1720 std::vector<FT1>
operator/(
const std::vector<FT1> &A,
const FT2 &s) {
1721 std::vector<FT1> tmp = A;
1722 for (std::size_t i = 0; i < A.size(); ++i)
1729 template<
typename FT1,
typename FT2>
1730 std::vector<FT1>
operator/(
const FT2 &s,
const std::vector<FT1> &A) {
1731 std::vector<FT1> tmp = A;
1732 for (std::size_t i = 0; i < A.size(); ++i)
1733 tmp[i] = s / tmp[i];
1739 template<
typename FT>
1740 inline std::vector<FT>
elem_mult(
const std::vector<FT> &v1,
const std::vector<FT> &v2) {
1741 assert(v1.size() == v2.size());
1742 std::vector<FT> result = v1;
1743 for (std::size_t i = 0; i < v2.size(); ++i) {
1751 template<
typename FT>
1752 inline std::vector<FT> &
elem_mult_eq(std::vector<FT> &v1,
const std::vector<FT> &v2) {
1753 assert(v1.size() == v2.size());
1754 for (std::size_t i = 0; i < v2.size(); ++i) {
1763 template<
typename FT>
1764 inline std::vector<FT>
elem_divd(
const std::vector<FT> &v1,
const std::vector<FT> &v2) {
1765 assert(v1.size() == v2.size());
1766 std::vector<FT> result = v1;
1767 for (std::size_t i = 0; i < v2.size(); ++i) {
1775 template<
typename FT>
1776 inline std::vector<FT> &
elem_divd_eq(std::vector<FT> &v1,
const std::vector<FT> &v2) {
1777 assert(v1.size() == v2.size());
1778 for (std::size_t i = 0; i < v2.size(); ++i) {
1787 template<
typename FT>
1788 FT
operator*(
const std::vector<FT> &v1,
const std::vector<FT> &v2) {
1789 assert(v1.size() == v2.size());
1792 typename std::vector<FT>::const_iterator itr1 = v1.begin();
1793 typename std::vector<FT>::const_iterator itr2 = v2.begin();
1795 while (itr1 != v1.end())
1796 sum += (*itr1++) * (*itr2++);
1802 template<
typename FT>
1803 FT
dot(
const std::vector<FT> &v1,
const std::vector<FT> &v2) {
1804 assert(v1.size() == v2.size());
1807 typename std::vector<FT>::const_iterator itr1 = v1.begin();
1808 typename std::vector<FT>::const_iterator itr2 = v2.begin();
1810 while (itr1 != v1.end())
1811 sum += (*itr1++) * (*itr2++);
1818 template<
typename FT>
1819 FT
sum(
const std::vector<FT> &v) {
1821 typename std::vector<FT>::const_iterator itr = v.begin();
1823 while (itr != v.end())
1830 template<
typename FT>
1831 FT
min(
const std::vector<FT> &v) {
1833 for (std::size_t i = 1; i < v.size(); ++i)
1841 template<
typename FT>
1842 FT
max(
const std::vector<FT> &v) {
1844 for (std::size_t i = 1; i < v.size(); ++i)
1852 template<
typename FT>
1853 FT
mean(
const std::vector<FT> &v) {
1854 return sum(v) / FT(v.size());
1858 template<
typename FT>
1859 FT
norm(
const std::vector<FT> &v) {
1861 typename std::vector<FT>::const_iterator itr = v.begin();
1863 while (itr != v.end()) {
1864 sum += (*itr) * (*itr);
1868 return FT(std::sqrt(
sum));
1872 template<
typename FT>
1873 FT
norm(
const std::vector<std::complex<FT> > &v) {
1875 typename std::vector<std::complex<FT> >::const_iterator itr = v.begin();
1877 while (itr != v.end()) {
1878 sum += std::norm(*itr++);
1881 return FT(std::sqrt(
sum));
1885 template<
typename FT>
1886 void swap(std::vector<FT> &lhs, std::vector<FT> &rhs) {
1887 typename std::vector<FT>::iterator itrL = lhs.begin(),
1890 while (itrL != lhs.end())
1891 std::swap(*itrL++, *itrR++);
1895 template<
typename FT>
1898 return std::vector<FT>();
1900 return std::vector<FT>(1, a);
1902 FT dx = (b - a) / (n - 1);
1904 std::vector<FT> tmp(n);
1905 for (
int i = 0; i < n; ++i)
1906 tmp[i] = a + i * dx;
1913 template<
typename FT>
1914 std::vector<FT>
abs(
const std::vector<std::complex<FT> > &v) {
1915 std::vector<FT> tmp(v.size());
1916 typename std::vector<FT>::iterator itrL = tmp.begin();
1917 typename std::vector<std::complex<FT> >::const_iterator itrR = v.begin();
1919 while (itrL != tmp.end())
1920 *itrL++ = std::abs(*itrR++);
1926 template<
typename FT>
1927 std::vector<FT>
arg(
const std::vector<std::complex<FT> > &v) {
1928 std::vector<FT> tmp(v.size());
1929 typename std::vector<FT>::iterator itrL = tmp.begin();
1930 typename std::vector<std::complex<FT> >::const_iterator itrR = v.begin();
1932 while (itrL != tmp.end())
1933 *itrL++ = std::arg(*itrR++);
1939 template<
typename FT>
1940 std::vector<FT>
real(
const std::vector<std::complex<FT> > &v) {
1941 std::vector<FT> tmp(v.size());
1942 typename std::vector<FT>::iterator itrL = tmp.begin();
1943 typename std::vector<std::complex<FT> >::const_iterator itrR = v.begin();
1945 while (itrL != tmp.end())
1946 *itrL++ = (*itrR++).
real();
1952 template<
typename FT>
1953 std::vector<FT>
imag(
const std::vector<std::complex<FT> > &v) {
1954 std::vector<FT> tmp(v.size());
1955 typename std::vector<FT>::iterator itrL = tmp.begin();
1956 typename std::vector<std::complex<FT> >::const_iterator itrR = v.begin();
1958 while (itrL != tmp.end())
1959 *itrL++ = (*itrR++).
imag();
1965 template<
typename FT>
1969 std::vector<std::complex<FT> > cv(N);
1970 typename std::vector<std::complex<FT> >::iterator itrL = cv.begin();
1971 typename std::vector<FT>::const_iterator itrR = rv.begin();
1973 while (itrR != rv.end())
1980 template<
typename FT>
1981 std::vector<std::complex<FT> >
complex_vector(
const std::vector<FT> &vR,
const std::vector<FT> &vI) {
1982 int N =
static_cast<int>(vR.size());
1984 assert(N == vI.size());
1986 std::vector<std::complex<FT> > cv(N);
1987 typename std::vector<std::complex<FT> >::iterator itrC = cv.begin();
1988 typename std::vector<FT>::const_iterator itrR = vR.begin(),
1991 while (itrC != cv.end())
1992 *itrC++ = std::complex<FT>(*itrR++, *itrI++);
A matrix representation, which supports dynamic sizes.
Definition: matrix.h:46
FT * data()
Definition: matrix.h:122
Matrix< FT > & operator*=(const FT2 &v)
computed assignment. Multiply each element by v
Definition: matrix.h:897
Matrix< FT > & operator/=(const FT2 &v)
computed assignment. Divide each element by v
Definition: matrix.h:916
FT & operator()(int row, int col)
Return the element at (row, col).
Definition: matrix.h:605
Matrix()
Definition: matrix.h:522
Matrix< FT > & operator+=(const FT &v)
computed assignment. Add each element by v
Definition: matrix.h:820
Matrix< FT > & operator=(const Matrix< FT > &A)
Assign A to this matrix.
Definition: matrix.h:561
const FT & get(int row, int col) const
Get the value of the element at (row, col).
Definition: matrix.h:635
void load_identity(FT v=FT(1))
Definition: matrix.h:665
std::vector< FT > get_column(int col) const
Get the matrix's column vector as a 1D array.
Definition: matrix.h:719
int cols() const
Return the number of columns.
Definition: matrix.h:680
const FT * data() const
Definition: matrix.h:128
void set_column(const std::vector< FT > &v, int col)
Set the matrix's column vector.
Definition: matrix.h:747
void load_zero()
Set all elements to zero.
Definition: matrix.h:650
Matrix< FT > & resize(int rows, int cols)
Change the size/dimension of the matrix.
Definition: matrix.h:689
FT * operator[](int row)
Return the row_th row as a 1D array.
Definition: matrix.h:591
std::vector< FT > get_row(int row) const
Get the matrix's row vector as a 1D array.
Definition: matrix.h:704
Matrix< FT > inverse() const
Return the inverse of the matrix (for positive square matrices only)
Definition: matrix.h:789
void set_row(const std::vector< FT > &v, int row)
Set the matrix's row vector.
Definition: matrix.h:734
void set(int row, int col, FT v)
Set the value of the element at (row, col).
Definition: matrix.h:625
int rows() const
Return the number of rows.
Definition: matrix.h:675
Matrix< FT > transpose() const
Return the transposed matrix.
Definition: matrix.h:757
Matrix< FT > & operator-=(const FT &v)
computed assignment. Subtract each element by v
Definition: matrix.h:858
FT trace() const
Definition: matrix.h:807
FT determinant() const
Return the determinant of the matrix (for square matrices only)
Definition: matrix.h:769
Definition: collider.cpp:182
Matrix< FT > operator+(const Matrix< FT > &, const FT &)
Definition: matrix.h:990
Mat< M, N, T > transpose(const Mat< N, M, T > &m)
Transpose m.
Definition: mat.h:907
Matrix< FT > operator-(const Matrix< FT > &)
arithmetic operators
Definition: matrix.h:975
Matrix< FT > & elem_divd_eq(Matrix< FT > &, const Matrix< FT > &)
Definition: matrix.h:1263
Matrix< FT > elem_divd(const Matrix< FT > &, const Matrix< FT > &)
Definition: matrix.h:1256
std::vector< FT > mean(const Matrix< FT > &)
Definition: matrix.h:1506
Matrix< FT > transpose_mult(const Matrix< FT > &, const Matrix< FT > &)
transpose multiplication
Definition: matrix.h:1300
Matrix< FT > diagonal(const std::vector< FT > &)
Definition: matrix.h:1396
Matrix< FT > elem_mult(const Matrix< FT > &, const Matrix< FT > &)
element-wise multiplication / division
Definition: matrix.h:1242
Matrix< FT1 > operator/(const Matrix< FT1 > &, const FT2 &)
matrix-scalar division
Definition: matrix.h:1094
Matrix< FT > mult_transpose(const Matrix< FT > &, const Matrix< FT > &)
Definition: matrix.h:1336
FT max()
Function returning maximum representable value for a given type.
T trace(const Mat< N, N, T > &m)
Return the trace (sum of elements on the main diagonal) of N by N (square) matrix m.
void swap(Matrix< FT > &, Matrix< FT > &)
Definition: matrix.h:1439
T determinant(const Mat< N, N, T > &m)
Return the determinant of N x N (square) matrix m.
Matrix< std::complex< FT > > complex_matrix(const Matrix< FT > &)
Convert real matrix to complex matrix.
Definition: matrix.h:1571
Matrix< FT > real(const Matrix< std::complex< FT > > &A)
Get real part of a std::complex matrix.
Definition: matrix.h:1541
std::vector< std::complex< FT > > complex_vector(const std::vector< FT > &)
Convert real vector to complex vector.
Definition: matrix.h:1966
Matrix< FT > imag(const Matrix< std::complex< FT > > &A)
Get imaginary part of a std::complex matrix.
Definition: matrix.h:1556
std::vector< FT > linspace(FT, FT, int)
Generates a vector of n points linearly spaced between and including a and b.
Definition: matrix.h:1896
std::istream & operator>>(std::istream &is, GenericLine< DIM, FT > &line)
Definition: line.h:133
void mult(const Matrix< FT > &, const Matrix< FT > &, Matrix< FT > &)
multiplication
Definition: matrix.h:1119
std::ostream & operator<<(std::ostream &os, Graph::Vertex v)
Definition: graph.h:920
FT min()
Function returning minimum representable value for a given type.
Matrix< FT > arg(const Matrix< std::complex< FT > > &A)
Get angle of a std::complex matrix.
Definition: matrix.h:1527
Matrix< FT > identity(int, const FT &)
unit and diagonal matrix
Definition: matrix.h:1370
Matrix< FT > abs(const Matrix< std::complex< FT > > &A)
Get magnitude of a std::complex matrix.
Definition: matrix.h:1513
FT dot(const std::vector< FT > &, const std::vector< FT > &)
Inner product for vectors.
Definition: matrix.h:1803
std::vector< FT > sum(const Matrix< FT > &)
Definition: matrix.h:1454
Matrix< FT > conjugate_transpose(const Matrix< FT > &)
Definition: matrix.h:1285
FT norm(const Matrix< FT > &)
utilities
Definition: matrix.h:1424
Matrix< FT > & elem_mult_eq(Matrix< FT > &, const Matrix< FT > &)
Definition: matrix.h:1249
Mat< N, M, T > operator*(T lhs, const Mat< N, M, T > &rhs)
Component-wise scalar-matrix multiplication.
Definition: mat.h:787