Chemical Data Processing Library C++ API - Version 1.4.0
Matrix.hpp
Go to the documentation of this file.
1 /*
2  * Matrix.hpp
3  *
4  * Copyright (C) 2003 Thomas Seidel <thomas.seidel@univie.ac.at>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library; see the file COPYING. If not, write to
18  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21 
27 #ifndef CDPL_MATH_MATRIX_HPP
28 #define CDPL_MATH_MATRIX_HPP
29 
30 #include <cstddef>
31 #include <cstdint>
32 #include <algorithm>
33 #include <vector>
34 #include <limits>
35 #include <unordered_map>
36 #include <type_traits>
37 #include <utility>
38 #include <initializer_list>
39 #include <memory>
40 
41 #include "CDPL/Math/Check.hpp"
45 #include "CDPL/Math/Functional.hpp"
46 #include "CDPL/Math/TypeTraits.hpp"
49 #include "CDPL/Base/Exceptions.hpp"
50 
51 
52 namespace CDPL
53 {
54 
55  namespace Math
56  {
57 
62  template <typename M>
63  class MatrixReference : public MatrixExpression<MatrixReference<M> >
64  {
65 
67 
68  public:
72  typedef M MatrixType;
73 
77  typedef typename M::ValueType ValueType;
78 
82  typedef typename std::conditional<std::is_const<M>::value,
83  typename M::ConstReference,
84  typename M::Reference>::type Reference;
85 
89  typedef typename M::ConstReference ConstReference;
90 
94  typedef typename M::SizeType SizeType;
95 
99  typedef typename M::DifferenceType DifferenceType;
100 
105 
109  typedef const SelfType ConstClosureType;
110 
116  data(m) {}
117 
125  {
126  return data(i, j);
127  }
128 
136  {
137  return data(i, j);
138  }
139 
145  {
146  return data.getSize1();
147  }
148 
154  {
155  return data.getSize2();
156  }
157 
163  {
164  return data.getMaxSize();
165  }
166 
172  {
173  return data.getMaxSize1();
174  }
175 
181  {
182  return data.getMaxSize2();
183  }
184 
189  bool isEmpty() const
190  {
191  return data.isEmpty();
192  }
193 
198  const MatrixType& getData() const
199  {
200  return data;
201  }
202 
208  {
209  return data;
210  }
211 
218  {
219  data.operator=(r.data);
220  return *this;
221  }
222 
229  template <typename E>
231  {
232  data.operator=(e);
233  return *this;
234  }
235 
242  template <typename E>
244  {
245  data.operator+=(e);
246  return *this;
247  }
248 
255  template <typename E>
257  {
258  data.operator-=(e);
259  return *this;
260  }
261 
268  template <typename T>
269  typename std::enable_if<IsScalar<T>::value, MatrixReference>::type& operator*=(const T& t)
270  {
271  data.operator*=(t);
272  return *this;
273  }
274 
281  template <typename T>
282  typename std::enable_if<IsScalar<T>::value, MatrixReference>::type& operator/=(const T& t)
283  {
284  data.operator/=(t);
285  return *this;
286  }
287 
294  template <typename E>
296  {
297  data.assign(e);
298  return *this;
299  }
300 
307  template <typename E>
309  {
310  data.plusAssign(e);
311  return *this;
312  }
313 
320  template <typename E>
322  {
323  data.minusAssign(e);
324  return *this;
325  }
326 
332  {
333  data.swap(r.data);
334  }
335 
341  friend void swap(MatrixReference& r1, MatrixReference& r2)
342  {
343  r1.swap(r2);
344  }
345 
346  private:
347  MatrixType& data;
348  };
349 
350  template <typename T, typename A>
351  class Matrix;
352  template <typename T, typename A>
353  class Vector;
354 
359  template <typename T>
360  class InitListMatrix : public MatrixContainer<InitListMatrix<T> >
361  {
362 
363  public:
368 
372  typedef std::initializer_list<std::initializer_list<T> > InitializerListType;
373 
377  typedef typename InitializerListType::value_type::value_type ValueType;
378 
382  typedef typename InitializerListType::value_type::const_reference ConstReference;
383 
387  typedef typename InitializerListType::value_type::reference Reference;
388 
392  typedef typename InitializerListType::size_type SizeType;
393 
397  typedef typename std::ptrdiff_t DifferenceType;
398 
403 
407  typedef const SelfType ConstClosureType;
408 
413 
418 
427  list(l), size2(0)
428  {
429  for (const auto& r : l)
430  size2 = std::max(size2, r.size());
431  }
432 
441  {
442  CDPL_MATH_CHECK(i < getSize1() && j < getSize2(), "Index out of range", Base::IndexError);
443 
444  if (j >= (list.begin() + i)->size())
445  return zero;
446 
447  return *((list.begin() + i)->begin() + j);
448  }
449 
458  {
459  CDPL_MATH_CHECK(i < getSize1() && j < getSize2(), "Index out of range", Base::IndexError);
460 
461  if (j >= (list.begin() + i)->size())
462  return zero;
463 
464  return *((list.begin() + i)->begin() + j);
465  }
466 
472  {
473  return list.size();
474  }
475 
481  {
482  return size2;
483  }
484 
489  bool isEmpty() const
490  {
491  return (size2 == 0 || list.size() == 0);
492  }
493 
494  private:
495  InitializerListType list;
496  SizeType size2;
497  static const ValueType zero;
498  };
499 
500  template <typename T>
501  const typename InitListMatrix<T>::ValueType InitListMatrix<T>::zero = InitListMatrix<T>::ValueType();
502 
508  template <typename T, typename A = std::vector<T> >
509  class Matrix : public MatrixContainer<Matrix<T, A> >
510  {
511 
512  typedef Matrix<T, A> SelfType;
513 
514  public:
518  typedef T ValueType;
519 
523  typedef T& Reference;
524 
528  typedef const T& ConstReference;
529 
533  typedef typename A::size_type SizeType;
534 
538  typedef typename A::difference_type DifferenceType;
539 
543  typedef A ArrayType;
544 
548  typedef T* Pointer;
549 
553  typedef const T* ConstPointer;
554 
559 
564 
569 
574 
578  typedef std::shared_ptr<SelfType> SharedPointer;
579 
583  typedef std::initializer_list<std::initializer_list<T> > InitializerListType;
584 
589  size1(0), size2(0), data() {}
590 
597  size1(m), size2(n), data(storageSize(m, n)) {}
598 
606  size1(m), size2(n), data(storageSize(m, n), v) {}
607 
612  Matrix(const Matrix& m):
613  size1(m.size1), size2(m.size2), data(m.data) {}
614 
620  size1(0), size2(0), data()
621  {
622  swap(m);
623  }
624 
634  size1(0), size2(0), data()
635  {
636  assign(l);
637  }
638 
644  template <typename E>
646  size1(e().getSize1()), size2(e().getSize2()), data(storageSize(e().getSize1(), e().getSize2()))
647  {
648  matrixAssignMatrix<ScalarAssignment>(*this, e);
649  }
650 
659  {
660  CDPL_MATH_CHECK(i < getSize1() && j < getSize2(), "Index out of range", Base::IndexError);
661  return data[i * getSize2() + j];
662  }
663 
672  {
673  CDPL_MATH_CHECK(i < getSize1() && j < getSize2(), "Index out of range", Base::IndexError);
674  return data[i * getSize2() + j];
675  }
676 
681  bool isEmpty() const
682  {
683  return data.empty();
684  }
685 
691  {
692  return size1;
693  }
694 
700  {
701  return size2;
702  }
703 
709  {
710  return data.max_size();
711  }
712 
718  {
719  return data;
720  }
721 
726  const ArrayType& getData() const
727  {
728  return data;
729  }
730 
737  {
738  data = m.data;
739  size1 = m.size1;
740  size2 = m.size2;
741  return *this;
742  }
743 
750  {
751  swap(m);
752  return *this;
753  }
754 
761  template <typename C>
763  {
764  return assign(c);
765  }
766 
773  {
774  return assign(l);
775  }
776 
783  template <typename E>
785  {
786  Matrix tmp(e);
787  swap(tmp);
788  return *this;
789  }
790 
797  template <typename C>
799  {
800  return plusAssign(c);
801  }
802 
809  {
810  return plusAssign(l);
811  }
812 
819  template <typename E>
821  {
822  Matrix tmp(*this + e);
823  swap(tmp);
824  return *this;
825  }
826 
833  template <typename C>
835  {
836  return minusAssign(c);
837  }
838 
845  {
846  return minusAssign(l);
847  }
848 
855  template <typename E>
857  {
858  Matrix tmp(*this - e);
859  swap(tmp);
860  return *this;
861  }
862 
869  template <typename T1>
870  typename std::enable_if<IsScalar<T1>::value, Matrix>::type& operator*=(const T1& t)
871  {
872  matrixAssignScalar<ScalarMultiplicationAssignment>(*this, t);
873  return *this;
874  }
875 
882  template <typename T1>
883  typename std::enable_if<IsScalar<T1>::value, Matrix>::type& operator/=(const T1& t)
884  {
885  matrixAssignScalar<ScalarDivisionAssignment>(*this, t);
886  return *this;
887  }
888 
895  template <typename E>
897  {
898  resize(e().getSize1(), e().getSize2(), false);
899  matrixAssignMatrix<ScalarAssignment>(*this, e);
900  return *this;
901  }
902 
909  {
911  resize(ilm.getSize1(), ilm.getSize2(), false);
912  matrixAssignMatrix<ScalarAssignment>(*this, ilm);
913  return *this;
914  }
915 
922  template <typename E>
924  {
925  matrixAssignMatrix<ScalarAdditionAssignment>(*this, e);
926  return *this;
927  }
928 
935  {
936  matrixAssignMatrix<ScalarAdditionAssignment>(*this, InitListMatrix<ValueType>(l));
937  return *this;
938  }
939 
946  template <typename E>
948  {
949  matrixAssignMatrix<ScalarSubtractionAssignment>(*this, e);
950  return *this;
951  }
952 
959  {
960  matrixAssignMatrix<ScalarSubtractionAssignment>(*this, InitListMatrix<ValueType>(l));
961  return *this;
962  }
963 
968  void swap(Matrix& m)
969  {
970  if (this != &m) {
971  std::swap(data, m.data);
972  std::swap(size1, m.size1);
973  std::swap(size2, m.size2);
974  }
975  }
976 
982  friend void swap(Matrix& m1, Matrix& m2)
983  {
984  m1.swap(m2);
985  }
986 
991  void clear(const ValueType& v = ValueType())
992  {
993  std::fill(data.begin(), data.end(), v);
994  }
995 
1003  void resize(SizeType m, SizeType n, bool preserve = true, const ValueType& v = ValueType())
1004  {
1005  if (size1 == m && size2 == n)
1006  return;
1007 
1008  if (preserve) {
1009  Matrix tmp(m, n, v);
1010 
1011  for (SizeType i = 0, min_size1 = std::min(size1, m); i < min_size1; i++)
1012  for (SizeType j = 0, min_size2 = std::min(size2, n); j < min_size2; j++)
1013  tmp(i, j) = (*this)(i, j);
1014 
1015  swap(tmp);
1016 
1017  } else {
1018  data.resize(storageSize(m, n), v);
1019  size1 = m;
1020  size2 = n;
1021  }
1022  }
1023 
1024  private:
1025  SizeType storageSize(SizeType m, SizeType n)
1026  {
1027  CDPL_MATH_CHECK(n == 0 || m <= data.max_size() / n, "Maximum size exceeded", Base::SizeError);
1028  return (m * n);
1029  }
1030 
1031  SizeType size1;
1032  SizeType size2;
1033  ArrayType data;
1034  };
1035 
1041  template <typename T, typename A = std::unordered_map<std::uint64_t, T> >
1042  class SparseMatrix : public MatrixContainer<SparseMatrix<T, A> >
1043  {
1044 
1045  typedef SparseMatrix<T> SelfType;
1046 
1047  public:
1051  typedef T ValueType;
1052 
1056  typedef typename A::key_type KeyType;
1057 
1062 
1066  typedef const T& ConstReference;
1067 
1071  typedef std::uint32_t SizeType;
1072 
1076  typedef std::ptrdiff_t DifferenceType;
1077 
1081  typedef A ArrayType;
1082 
1086  typedef T* Pointer;
1087 
1091  typedef const T* ConstPointer;
1092 
1097 
1102 
1107 
1112 
1116  typedef std::shared_ptr<SelfType> SharedPointer;
1117 
1121  typedef std::initializer_list<std::initializer_list<T> > InitializerListType;
1122 
1127  size1(0), size2(0), data() {}
1128 
1136  size1(m), size2(n), data()
1137  {
1138  CDPL_MATH_CHECK((n == 0 || m <= data.max_size() / n), "Maximum size exceeded", Base::SizeError);
1139  }
1140 
1146  size1(m.size1), size2(m.size2), data(m.data) {}
1147 
1153  size1(0), size2(0), data()
1154  {
1155  swap(m);
1156  }
1157 
1163  size1(0), size2(0), data()
1164  {
1165  assign(l);
1166  }
1167 
1174  template <typename E>
1176  size1(e().getSize1()), size2(e().getSize2()), data()
1177  {
1178  CDPL_MATH_CHECK((size1 == 0 || size2 <= data.max_size() / size1), "Maximum size exceeded", Base::SizeError);
1179  matrixAssignMatrix<ScalarAssignment>(*this, e);
1180  }
1181 
1190  {
1191  CDPL_MATH_CHECK(i < getSize1() && j < getSize2(), "Index out of range", Base::IndexError);
1192 
1193  return Reference(*this, makeKey(i, j));
1194  }
1195 
1204  {
1205  CDPL_MATH_CHECK(i < getSize1() && j < getSize2(), "Index out of range", Base::IndexError);
1206 
1207  typename ArrayType::const_iterator it = data.find(makeKey(i, j));
1208 
1209  if (it == data.end())
1210  return zero;
1211 
1212  return it->second;
1213  }
1214 
1220  {
1221  return data.size();
1222  }
1223 
1228  bool isEmpty() const
1229  {
1230  return (size1 == 0 || size2 == 0);
1231  }
1232 
1238  {
1239  return size1;
1240  }
1241 
1247  {
1248  return size2;
1249  }
1250 
1255  typename ArrayType::size_type getMaxSize() const
1256  {
1257  return data.max_size();
1258  }
1259 
1265  {
1266  return data;
1267  }
1268 
1273  const ArrayType& getData() const
1274  {
1275  return data;
1276  }
1277 
1284  {
1285  data = m.data;
1286  size1 = m.size1;
1287  size2 = m.size2;
1288  return *this;
1289  }
1290 
1297  {
1298  swap(m);
1299  return *this;
1300  }
1301 
1308  template <typename C>
1310  {
1311  return assign(c);
1312  }
1313 
1320  {
1321  return assign(l);
1322  }
1323 
1330  template <typename E>
1332  {
1333  SparseMatrix tmp(e);
1334  swap(tmp);
1335  return *this;
1336  }
1337 
1344  template <typename C>
1346  {
1347  return plusAssign(c);
1348  }
1349 
1356  {
1357  return plusAssign(l);
1358  }
1359 
1366  template <typename E>
1368  {
1369  SparseMatrix tmp(*this + e);
1370  swap(tmp);
1371  return *this;
1372  }
1373 
1380  template <typename C>
1382  {
1383  return minusAssign(c);
1384  }
1385 
1392  {
1393  return minusAssign(l);
1394  }
1395 
1402  template <typename E>
1404  {
1405  SparseMatrix tmp(*this - e);
1406  swap(tmp);
1407  return *this;
1408  }
1409 
1416  template <typename T1>
1417  typename std::enable_if<IsScalar<T1>::value, SparseMatrix>::type& operator*=(const T1& t)
1418  {
1419  matrixAssignScalar<ScalarMultiplicationAssignment>(*this, t);
1420  return *this;
1421  }
1422 
1429  template <typename T1>
1430  typename std::enable_if<IsScalar<T1>::value, SparseMatrix>::type& operator/=(const T1& t)
1431  {
1432  matrixAssignScalar<ScalarDivisionAssignment>(*this, t);
1433  return *this;
1434  }
1435 
1442  template <typename E>
1444  {
1445  resize(e().getSize1(), e().getSize2());
1446  matrixAssignMatrix<ScalarAssignment>(*this, e);
1447  return *this;
1448  }
1449 
1456  {
1458  resize(ilm.getSize1(), ilm.getSize2());
1459  matrixAssignMatrix<ScalarAssignment>(*this, ilm);
1460  return *this;
1461  }
1462 
1469  template <typename E>
1471  {
1472  matrixAssignMatrix<ScalarAdditionAssignment>(*this, e);
1473  return *this;
1474  }
1475 
1482  {
1483  matrixAssignMatrix<ScalarAdditionAssignment>(*this, InitListMatrix<ValueType>(l));
1484  return *this;
1485  }
1486 
1493  template <typename E>
1495  {
1496  matrixAssignMatrix<ScalarSubtractionAssignment>(*this, e);
1497  return *this;
1498  }
1499 
1506  {
1507  matrixAssignMatrix<ScalarSubtractionAssignment>(*this, InitListMatrix<ValueType>(l));
1508  return *this;
1509  }
1510 
1516  {
1517  if (this != &m) {
1518  std::swap(data, m.data);
1519  std::swap(size1, m.size1);
1520  std::swap(size2, m.size2);
1521  }
1522  }
1523 
1529  friend void swap(SparseMatrix& m1, SparseMatrix& m2)
1530  {
1531  m1.swap(m2);
1532  }
1533 
1537  void clear()
1538  {
1539  data.clear();
1540  }
1541 
1549  {
1550  CDPL_MATH_CHECK((n == 0 || m <= data.max_size() / n), "Maximum size exceeded", Base::SizeError);
1551 
1552  for (typename ArrayType::iterator it = data.begin(); it != data.end();) {
1553  const KeyType& key = it->first;
1554 
1555  if (getRowIdx(key) >= m || getColIdx(key) >= n)
1556  it = data.erase(it);
1557  else
1558  ++it;
1559  }
1560 
1561  size1 = m;
1562  size2 = n;
1563  }
1564 
1565  private:
1566  static KeyType makeKey(SizeType i, SizeType j)
1567  {
1568  return ((KeyType(i) << (sizeof(SizeType) * 8)) + j);
1569  }
1570 
1571  static SizeType getRowIdx(const KeyType& key)
1572  {
1573  return (key >> (sizeof(SizeType) * 8));
1574  }
1575 
1576  static SizeType getColIdx(const KeyType& key)
1577  {
1578  return (key & KeyType(~SizeType()));
1579  }
1580 
1581  SizeType size1;
1582  SizeType size2;
1583  ArrayType data;
1584  static const ValueType zero;
1585  };
1586 
1587  template <typename T, typename A>
1588  const typename SparseMatrix<T, A>::ValueType SparseMatrix<T, A>::zero = SparseMatrix<T, A>::ValueType();
1589 
1590  template <typename T, std::size_t N>
1591  class BoundedVector;
1592 
1599  template <typename T, std::size_t M, std::size_t N>
1600  class BoundedMatrix : public MatrixContainer<BoundedMatrix<T, M, N> >
1601  {
1602 
1604 
1605  public:
1609  typedef T ValueType;
1610 
1614  typedef T& Reference;
1615 
1619  typedef const T& ConstReference;
1620 
1624  typedef std::size_t SizeType;
1625 
1629  typedef std::ptrdiff_t DifferenceType;
1630 
1634  typedef ValueType ArrayType[M][N];
1635 
1639  typedef T (*ArrayPointer)[N];
1640 
1644  typedef const T (*ConstArrayPointer)[N];
1645 
1649  typedef T* Pointer;
1650 
1654  typedef const T* ConstPointer;
1655 
1660 
1665 
1670 
1675 
1679  typedef std::shared_ptr<SelfType> SharedPointer;
1680 
1684  typedef std::initializer_list<std::initializer_list<T> > InitializerListType;
1685 
1686 
1690  static const SizeType MaxSize1 = M;
1691 
1695  static const SizeType MaxSize2 = N;
1696 
1701  size1(0), size2(0) {}
1702 
1710  size1(0), size2(0)
1711  {
1712  resize(m, n);
1713  }
1714 
1723  size1(0), size2(0)
1724  {
1725  resize(m, n, v);
1726  }
1727 
1733  size1(m.size1), size2(m.size2)
1734  {
1735  for (SizeType i = 0; i < size1; i++)
1736  std::copy(m.data[i], m.data[i] + size2, data[i]);
1737  }
1738 
1745  size1(0), size2(0)
1746  {
1747  assign(l);
1748  }
1749 
1756  template <typename E>
1758  size1(0), size2(0)
1759  {
1760  resize(e().getSize1(), e().getSize2());
1761  matrixAssignMatrix<ScalarAssignment>(*this, e);
1762  }
1763 
1772  {
1773  CDPL_MATH_CHECK(i < getSize1() && j < getSize2(), "Index out of range", Base::IndexError);
1774  return data[i][j];
1775  }
1776 
1785  {
1786  CDPL_MATH_CHECK(i < getSize1() && j < getSize2(), "Index out of range", Base::IndexError);
1787  return data[i][j];
1788  }
1789 
1794  bool isEmpty() const
1795  {
1796  return (size1 == 0 || size2 == 0);
1797  }
1798 
1804  {
1805  return size1;
1806  }
1807 
1813  {
1814  return size2;
1815  }
1816 
1822  {
1823  return M;
1824  }
1825 
1831  {
1832  return N;
1833  }
1834 
1840  {
1841  return data;
1842  }
1843 
1849  {
1850  return data;
1851  }
1852 
1859  {
1860  if (this != &m) {
1861  for (SizeType i = 0; i < m.size1; i++)
1862  std::copy(m.data[i], m.data[i] + m.size2, data[i]);
1863 
1864  size1 = m.size1;
1865  size2 = m.size2;
1866  }
1867 
1868  return *this;
1869  }
1870 
1877  template <typename C>
1879  {
1880  return assign(c);
1881  }
1882 
1890  {
1891  return assign(l);
1892  }
1893 
1901  template <typename E>
1903  {
1904  BoundedMatrix tmp(e);
1905  return this-> operator=(tmp);
1906  }
1907 
1914  template <typename C>
1916  {
1917  return plusAssign(c);
1918  }
1919 
1926  {
1927  return plusAssign(l);
1928  }
1929 
1936  template <typename E>
1938  {
1939  BoundedMatrix tmp(*this + e);
1940  return this-> operator=(tmp);
1941  }
1942 
1949  template <typename C>
1951  {
1952  return minusAssign(c);
1953  }
1954 
1961  {
1962  return minusAssign(l);
1963  }
1964 
1971  template <typename E>
1973  {
1974  BoundedMatrix tmp(*this - e);
1975  return this-> operator=(tmp);
1976  }
1977 
1984  template <typename T1>
1985  typename std::enable_if<IsScalar<T1>::value, BoundedMatrix>::type& operator*=(const T1& v)
1986  {
1987  matrixAssignScalar<ScalarMultiplicationAssignment>(*this, v);
1988  return *this;
1989  }
1990 
1997  template <typename T1>
1998  typename std::enable_if<IsScalar<T1>::value, BoundedMatrix>::type& operator/=(const T1& v)
1999  {
2000  matrixAssignScalar<ScalarDivisionAssignment>(*this, v);
2001  return *this;
2002  }
2003 
2011  template <typename E>
2013  {
2014  resize(e().getSize1(), e().getSize2());
2015  matrixAssignMatrix<ScalarAssignment>(*this, e);
2016  return *this;
2017  }
2018 
2026  {
2028  resize(ilm.getSize1(), ilm.getSize2());
2029  matrixAssignMatrix<ScalarAssignment>(*this, ilm);
2030  return *this;
2031  }
2032 
2039  template <typename E>
2041  {
2042  matrixAssignMatrix<ScalarAdditionAssignment>(*this, e);
2043  return *this;
2044  }
2045 
2052  {
2053  matrixAssignMatrix<ScalarAdditionAssignment>(*this, InitListMatrix<ValueType>(l));
2054  return *this;
2055  }
2056 
2063  template <typename E>
2065  {
2066  matrixAssignMatrix<ScalarSubtractionAssignment>(*this, e);
2067  return *this;
2068  }
2069 
2076  {
2077  matrixAssignMatrix<ScalarSubtractionAssignment>(*this, InitListMatrix<ValueType>(l));
2078  return *this;
2079  }
2080 
2086  {
2087  if (this != &m) {
2088  SizeType max_size1 = std::max(size1, m.size1);
2089  SizeType max_size2 = std::max(size2, m.size2);
2090 
2091  for (SizeType i = 0; i < max_size1; i++)
2092  std::swap_ranges(data[i], data[i] + max_size2, m.data[i]);
2093 
2094  std::swap(size1, m.size1);
2095  std::swap(size2, m.size2);
2096  }
2097  }
2098 
2104  friend void swap(BoundedMatrix& m1, BoundedMatrix& m2)
2105  {
2106  m1.swap(m2);
2107  }
2108 
2113  void clear(const ValueType& v = ValueType())
2114  {
2115  for (SizeType i = 0; i < size1; i++)
2116  std::fill(data[i], data[i] + size2, v);
2117  }
2118 
2126  {
2129 
2130  size1 = m;
2131  size2 = n;
2132  }
2133 
2141  void resize(SizeType m, SizeType n, const ValueType& v)
2142  {
2145 
2146  if (n > size2) {
2147  SizeType min_size1 = std::min(size1, m);
2148 
2149  for (SizeType i = 0; i < min_size1; i++)
2150  std::fill(data[i] + size2, data[i] + n, v);
2151  }
2152 
2153  if (m > size1)
2154  for (SizeType i = size1; i < m; i++)
2155  std::fill(data[i], data[i] + n, v);
2156 
2157  size1 = m;
2158  size2 = n;
2159  }
2160 
2161  private:
2162  SizeType size1;
2163  SizeType size2;
2164  ArrayType data;
2165  };
2166 
2167  template <typename T, std::size_t M, std::size_t N>
2169  template <typename T, std::size_t M, std::size_t N>
2171 
2178  template <typename T, std::size_t M, std::size_t N>
2179  class CMatrix : public MatrixContainer<CMatrix<T, M, N> >
2180  {
2181 
2182  typedef CMatrix<T, M, N> SelfType;
2183 
2184  public:
2188  typedef T ValueType;
2189 
2193  typedef T& Reference;
2194 
2198  typedef const T& ConstReference;
2199 
2203  typedef std::size_t SizeType;
2204 
2208  typedef std::ptrdiff_t DifferenceType;
2209 
2213  typedef ValueType ArrayType[M][N];
2214 
2218  typedef T (*ArrayPointer)[N];
2219 
2223  typedef const T (*ConstArrayPointer)[N];
2224 
2228  typedef T* Pointer;
2229 
2233  typedef const T* ConstPointer;
2234 
2239 
2244 
2249 
2254 
2258  typedef std::shared_ptr<SelfType> SharedPointer;
2259 
2263  typedef std::initializer_list<std::initializer_list<T> > InitializerListType;
2264 
2265 
2269  static const SizeType Size1 = M;
2270 
2274  static const SizeType Size2 = N;
2275 
2280  {
2281  for (SizeType i = 0; i < M; i++)
2282  std::fill(data[i], data[i] + N, ValueType());
2283  }
2284 
2289  explicit CMatrix(const ValueType& v)
2290  {
2291  for (SizeType i = 0; i < M; i++)
2292  std::fill(data[i], data[i] + N, v);
2293  }
2294 
2300  {
2301  for (SizeType i = 0; i < M; i++)
2302  std::copy(m.data[i], m.data[i] + N, data[i]);
2303  }
2304 
2310  {
2311  assign(l);
2312  }
2313 
2319  template <typename E>
2321  {
2322  matrixAssignMatrix<ScalarAssignment>(*this, e);
2323  }
2324 
2333  {
2334  CDPL_MATH_CHECK(i < getSize1() && j < getSize2(), "Index out of range", Base::IndexError);
2335  return data[i][j];
2336  }
2337 
2346  {
2347  CDPL_MATH_CHECK(i < getSize1() && j < getSize2(), "Index out of range", Base::IndexError);
2348  return data[i][j];
2349  }
2350 
2355  bool isEmpty() const
2356  {
2357  return (M == 0 || N == 0);
2358  }
2359 
2365  {
2366  return M;
2367  }
2368 
2374  {
2375  return N;
2376  }
2377 
2383  {
2384  return M;
2385  }
2386 
2392  {
2393  return N;
2394  }
2395 
2401  {
2402  return data;
2403  }
2404 
2410  {
2411  return data;
2412  }
2413 
2420  {
2421  if (this != &m) {
2422  for (SizeType i = 0; i < M; i++)
2423  std::copy(m.data[i], m.data[i] + N, data[i]);
2424  }
2425 
2426  return *this;
2427  }
2428 
2435  template <typename C>
2437  {
2438  return assign(c);
2439  }
2440 
2446  template <typename T1>
2448  {
2449  return assign(l);
2450  }
2451 
2458  template <typename E>
2460  {
2461  CMatrix tmp(e);
2462  return this->operator=(tmp);
2463  }
2464 
2471  template <typename C>
2473  {
2474  return plusAssign(c);
2475  }
2476 
2483  {
2484  return plusAssign(l);
2485  }
2486 
2493  template <typename E>
2495  {
2496  CMatrix tmp(*this + e);
2497  return this->operator=(tmp);
2498  }
2499 
2506  template <typename C>
2508  {
2509  return minusAssign(c);
2510  }
2511 
2518  {
2519  return minusAssign(l);
2520  }
2521 
2528  template <typename E>
2530  {
2531  CMatrix tmp(*this - e);
2532  return this->operator=(tmp);
2533  }
2534 
2541  template <typename T1>
2542  typename std::enable_if<IsScalar<T1>::value, CMatrix>::type& operator*=(const T1& t)
2543  {
2544  matrixAssignScalar<ScalarMultiplicationAssignment>(*this, t);
2545  return *this;
2546  }
2547 
2554  template <typename T1>
2555  typename std::enable_if<IsScalar<T1>::value, CMatrix>::type& operator/=(const T1& t)
2556  {
2557  matrixAssignScalar<ScalarDivisionAssignment>(*this, t);
2558  return *this;
2559  }
2560 
2567  template <typename E>
2569  {
2570  matrixAssignMatrix<ScalarAssignment>(*this, e);
2571  return *this;
2572  }
2573 
2581  {
2583 
2584  for (SizeType i = 0; i < n_rows; i++) {
2585  const auto& row = *(l.begin() + i);
2586 
2587  if (row.size() < N) {
2588  std::copy(row.begin(), row.end(), data[i]);
2589  std::fill(data[i] + row.size(), data[i] + N, ValueType());
2590 
2591  } else {
2593  std::copy(row.begin(), row.begin() + N, data[i]);
2594  }
2595  }
2596 
2597  for (SizeType i = n_rows; i < M; i++)
2598  std::fill(data[i], data[i] + N, ValueType());
2599 
2600  return *this;
2601  }
2602 
2609  template <typename E>
2611  {
2612  matrixAssignMatrix<ScalarAdditionAssignment>(*this, e);
2613  return *this;
2614  }
2615 
2622  {
2623  matrixAssignMatrix<ScalarAdditionAssignment>(*this, InitListMatrix<ValueType>(l));
2624  return *this;
2625  }
2626 
2633  template <typename E>
2635  {
2636  matrixAssignMatrix<ScalarSubtractionAssignment>(*this, e);
2637  return *this;
2638  }
2639 
2646  {
2647  matrixAssignMatrix<ScalarSubtractionAssignment>(*this, InitListMatrix<ValueType>(l));
2648  return *this;
2649  }
2650 
2655  void swap(CMatrix& m)
2656  {
2657  if (this != &m) {
2658  for (SizeType i = 0; i < M; i++)
2659  std::swap_ranges(data[i], data[i] + N, m.data[i]);
2660  }
2661  }
2662 
2668  friend void swap(CMatrix& m1, CMatrix& m2)
2669  {
2670  m1.swap(m2);
2671  }
2672 
2677  void clear(const ValueType& v = ValueType())
2678  {
2679  for (SizeType i = 0; i < M; i++)
2680  std::fill(data[i], data[i] + N, v);
2681  }
2682 
2683  private:
2684  ArrayType data;
2685  };
2686 
2687  template <typename T, std::size_t M, std::size_t N>
2689  template <typename T, std::size_t M, std::size_t N>
2691 
2696  template <typename T>
2697  class ZeroMatrix : public MatrixContainer<ZeroMatrix<T> >
2698  {
2699 
2700  typedef ZeroMatrix<T> SelfType;
2701 
2702  public:
2706  typedef T ValueType;
2707 
2711  typedef const T& Reference;
2712 
2716  typedef const T& ConstReference;
2717 
2721  typedef std::size_t SizeType;
2722 
2726  typedef std::ptrdiff_t DifferenceType;
2727 
2732 
2737 
2742 
2747 
2752  size1(0), size2(0) {}
2753 
2760  size1(m), size2(n) {}
2761 
2767  size1(m.size1), size2(m.size2) {}
2768 
2777  {
2778  CDPL_MATH_CHECK(i < getSize1() && j < getSize2(), "Index out of range", Base::IndexError);
2779  return zero;
2780  }
2781 
2786  bool isEmpty() const
2787  {
2788  return (size1 == 0 || size2 == 0);
2789  }
2790 
2796  {
2797  return size1;
2798  }
2799 
2805  {
2806  return size2;
2807  }
2808 
2814  {
2815  return std::numeric_limits<SizeType>::max();
2816  }
2817 
2823  {
2824  return std::numeric_limits<SizeType>::max();
2825  }
2826 
2833  {
2834  if (this != &m) {
2835  size1 = m.size1;
2836  size2 = m.size2;
2837  }
2838 
2839  return *this;
2840  }
2841 
2847  {
2848  if (this != &m) {
2849  std::swap(size1, m.size1);
2850  std::swap(size2, m.size2);
2851  }
2852  }
2853 
2859  friend void swap(ZeroMatrix& m1, ZeroMatrix& m2)
2860  {
2861  m1.swap(m2);
2862  }
2863 
2870  {
2871  size1 = m;
2872  size2 = n;
2873  }
2874 
2875  private:
2876  SizeType size1;
2877  SizeType size2;
2878  static const ValueType zero;
2879  };
2880 
2881  template <typename T>
2882  const typename ZeroMatrix<T>::ValueType ZeroMatrix<T>::zero = ZeroMatrix<T>::ValueType();
2883 
2888  template <typename T>
2889  class ScalarMatrix : public MatrixContainer<ScalarMatrix<T> >
2890  {
2891 
2892  typedef ScalarMatrix<T> SelfType;
2893 
2894  public:
2898  typedef T ValueType;
2899 
2903  typedef const T& Reference;
2904 
2908  typedef const T& ConstReference;
2909 
2913  typedef std::size_t SizeType;
2914 
2918  typedef std::ptrdiff_t DifferenceType;
2919 
2924 
2929 
2934 
2939 
2944  size1(0), size2(0), value() {}
2945 
2953  size1(m), size2(n), value(v) {}
2954 
2960  size1(m.size1), size2(m.size2), value(m.value) {}
2961 
2970  {
2971  CDPL_MATH_CHECK(i < getSize1() && j < getSize2(), "Index out of range", Base::IndexError);
2972  return value;
2973  }
2974 
2979  bool isEmpty() const
2980  {
2981  return (size1 == 0 || size2 == 0);
2982  }
2983 
2989  {
2990  return size1;
2991  }
2992 
2998  {
2999  return size2;
3000  }
3001 
3007  {
3008  return std::numeric_limits<SizeType>::max();
3009  }
3010 
3016  {
3017  return std::numeric_limits<SizeType>::max();
3018  }
3019 
3026  {
3027  if (this != &m) {
3028  size1 = m.size1;
3029  size2 = m.size2;
3030  value = m.value;
3031  }
3032 
3033  return *this;
3034  }
3035 
3041  {
3042  if (this != &m) {
3043  std::swap(size1, m.size1);
3044  std::swap(size2, m.size2);
3045  std::swap(value, m.value);
3046  }
3047  }
3048 
3054  friend void swap(ScalarMatrix& m1, ScalarMatrix& m2)
3055  {
3056  m1.swap(m2);
3057  }
3058 
3065  {
3066  size1 = m;
3067  size2 = n;
3068  }
3069 
3070  private:
3071  SizeType size1;
3072  SizeType size2;
3073  ValueType value;
3074  };
3075 
3080  template <typename T>
3081  class IdentityMatrix : public MatrixContainer<IdentityMatrix<T> >
3082  {
3083 
3084  typedef IdentityMatrix<T> SelfType;
3085 
3086  public:
3090  typedef T ValueType;
3091 
3095  typedef const T& Reference;
3096 
3100  typedef const T& ConstReference;
3101 
3105  typedef std::size_t SizeType;
3106 
3110  typedef std::ptrdiff_t DifferenceType;
3111 
3116 
3121 
3126 
3131 
3136  size1(0), size2(0) {}
3137 
3144  size1(m), size2(n) {}
3145 
3151  size1(m.size1), size2(m.size2) {}
3152 
3161  {
3162  CDPL_MATH_CHECK(i < getSize1() && j < getSize2(), "Index out of range", Base::IndexError);
3163  return (i == j ? one : zero);
3164  }
3165 
3170  bool isEmpty() const
3171  {
3172  return (size1 == 0 || size2 == 0);
3173  }
3174 
3180  {
3181  return size1;
3182  }
3183 
3189  {
3190  return size2;
3191  }
3192 
3198  {
3199  return std::numeric_limits<SizeType>::max();
3200  }
3201 
3207  {
3208  return std::numeric_limits<SizeType>::max();
3209  }
3210 
3217  {
3218  if (this != &m) {
3219  size1 = m.size1;
3220  size2 = m.size2;
3221  }
3222 
3223  return *this;
3224  }
3225 
3231  {
3232  if (this != &m) {
3233  std::swap(size1, m.size1);
3234  std::swap(size2, m.size2);
3235  }
3236  }
3237 
3243  friend void swap(IdentityMatrix& m1, IdentityMatrix& m2)
3244  {
3245  m1.swap(m2);
3246  }
3247 
3254  {
3255  size1 = m;
3256  size2 = n;
3257  }
3258 
3259  private:
3260  SizeType size1;
3261  SizeType size2;
3262  static const ValueType zero;
3263  static const ValueType one;
3264  };
3265 
3266  template <typename T>
3267  const typename IdentityMatrix<T>::ValueType IdentityMatrix<T>::zero = IdentityMatrix<T>::ValueType();
3268  template <typename T>
3269  const typename IdentityMatrix<T>::ValueType IdentityMatrix<T>::one = IdentityMatrix<T>::ValueType(1);
3270 
3275  template <typename M>
3277  {};
3278 
3283  template <typename M>
3285  {};
3286 
3291  template <typename M>
3293  {};
3294 
3299  template <typename M>
3301  {};
3302 
3309  template <typename E>
3310  typename E::ValueType
3312  {
3313  typedef typename E::ValueType ValueType;
3314  typedef typename Matrix<ValueType>::SizeType SizeType;
3315 
3316  Matrix<ValueType> lu(e);
3317  std::vector<SizeType> pv(lu.getSize1());
3318  std::size_t num_row_swaps;
3319 
3320  luDecompose(lu, pv, num_row_swaps);
3321 
3322  ValueType res(1);
3323  SizeType size = std::min(lu.getSize1(), lu.getSize2());
3324 
3325  for (SizeType i = 0; i < size; i++)
3326  res *= lu(i, i);
3327 
3328  return (num_row_swaps % 2 == 0 ? res : -res);
3329  }
3330 
3337  template <typename C>
3338  typename C::ValueType
3340  {
3341  typedef typename C::ValueType ValueType;
3342  typedef typename MatrixTemporaryTraits<C>::Type CTemporaryType;
3343  typedef typename CTemporaryType::SizeType SizeType;
3344 
3345  CTemporaryType lu(c);
3346  std::vector<SizeType> pv(lu.getSize1());
3347  std::size_t num_row_swaps;
3348 
3349  luDecompose(lu, pv, num_row_swaps);
3350 
3351  ValueType res(1);
3352  SizeType size = std::min(lu.getSize1(), lu.getSize2());
3353 
3354  for (SizeType i = 0; i < size; i++)
3355  res *= lu(i, i);
3356 
3357  return (num_row_swaps % 2 == 0 ? res : -res);
3358  }
3359 
3368  template <typename E, typename C>
3369  bool
3371  {
3372  typedef typename C::ValueType ValueType;
3373  typedef typename MatrixTemporaryTraits<C>::Type CTemporaryType;
3374  typedef typename CTemporaryType::SizeType SizeType;
3375 
3376  CTemporaryType lu(e);
3377  std::vector<SizeType> pv(lu.getSize1());
3378  std::size_t num_row_swaps;
3379 
3380  if (luDecompose(lu, pv, num_row_swaps) > 0)
3381  return false;
3382 
3383  c().assign(IdentityMatrix<ValueType>(lu.getSize1(), lu.getSize2()));
3384 
3385  return luSubstitute(lu, pv, c);
3386  }
3387 
3394  template <typename C>
3395  bool
3397  {
3398  return invert(c, c);
3399  }
3400 
3405 
3410 
3415 
3420 
3425 
3430 
3435 
3440 
3445 
3450 
3455 
3460 
3465 
3470 
3475 
3480 
3485 
3490 
3495 
3500 
3505 
3510 
3515 
3520 
3525 
3530 
3535 
3540 
3545 
3550 
3555 
3560  } // namespace Math
3561 } // namespace CDPL
3562 
3563 #endif // CDPL_MATH_MATRIX_HPP
Definition of exception classes.
Definition of various preprocessor macros for error checking.
#define CDPL_MATH_CHECK_MAX_SIZE(size, max_size, e)
Throws the exception e if size exceeds max_size, otherwise returns std::min(size, max_size).
Definition: Check.hpp:96
#define CDPL_MATH_CHECK(expr, msg, e)
Throws the exception e with message msg when the boolean expression expr evaluates to false.
Definition: Check.hpp:47
Definition of a proxy type for direct assignment of vector and matrix expressions.
Definition of various functors.
Implementation of matrix LU-decomposition and associated operations.
Implementation of matrix assignment routines.
Definition of various matrix expression types and operations.
Definition of an element proxy for sparse data types.
Definition of type traits.
Thrown to indicate that an index is out of range.
Definition: Base/Exceptions.hpp:152
Thrown to indicate that the size of a (multidimensional) array is not correct.
Definition: Base/Exceptions.hpp:133
Variable-size matrix with fixed upper dimension M×N backed by a stack-allocated array.
Definition: Matrix.hpp:1601
BoundedMatrix & operator-=(InitializerListType l)
Subtracts the rows in l element-wise from this bounded matrix.
Definition: Matrix.hpp:1960
std::initializer_list< std::initializer_list< T > > InitializerListType
The initializer list type accepted by constructors and assignment.
Definition: Matrix.hpp:1684
SizeType getSize1() const
Returns the current row count.
Definition: Matrix.hpp:1803
T(* ArrayPointer)[N]
Pointer-to-row type for raw access to the element array.
Definition: Matrix.hpp:1639
SizeType getSize2() const
Returns the current column count.
Definition: Matrix.hpp:1812
BoundedMatrix(const BoundedMatrix &m)
Constructs a copy of the bounded matrix m.
Definition: Matrix.hpp:1732
T * Pointer
Pointer type to a single element.
Definition: Matrix.hpp:1649
friend void swap(BoundedMatrix &m1, BoundedMatrix &m2)
ADL-enabled free-function form of swap().
Definition: Matrix.hpp:2104
BoundedMatrix & operator=(InitializerListType l)
Assigns the rows in l to this bounded matrix (resizes to match, respecting the bounds).
Definition: Matrix.hpp:1889
void clear(const ValueType &v=ValueType())
Sets every element of the matrix to the value v (dimensions unchanged).
Definition: Matrix.hpp:2113
BoundedMatrix & operator+=(const MatrixContainer< C > &c)
Adds the contents of the matrix container c element-wise to this bounded matrix (no alias check neede...
Definition: Matrix.hpp:1915
void swap(BoundedMatrix &m)
Swaps the contents of this bounded matrix with those of m.
Definition: Matrix.hpp:2085
const MatrixReference< const SelfType > ConstClosureType
Constant closure type used when this matrix appears inside another expression.
Definition: Matrix.hpp:1664
std::enable_if< IsScalar< T1 >::value, BoundedMatrix >::type & operator*=(const T1 &v)
Multiplies every element by the scalar v.
Definition: Matrix.hpp:1985
BoundedMatrix & minusAssign(InitializerListType l)
Subtracts the rows in l element-wise from this matrix without intermediate temporary.
Definition: Matrix.hpp:2075
std::enable_if< IsScalar< T1 >::value, BoundedMatrix >::type & operator/=(const T1 &v)
Divides every element by the scalar v.
Definition: Matrix.hpp:1998
std::size_t SizeType
The unsigned size type.
Definition: Matrix.hpp:1624
BoundedMatrix & operator-=(const MatrixExpression< E > &e)
Subtracts the matrix expression e element-wise from this bounded matrix (via a temporary to handle al...
Definition: Matrix.hpp:1972
static const SizeType MaxSize1
The compile-time maximum number of rows M.
Definition: Matrix.hpp:1690
BoundedMatrix & operator=(const MatrixExpression< E > &e)
Assigns the matrix expression e to this bounded matrix (via a temporary to handle aliasing).
Definition: Matrix.hpp:1902
const T & ConstReference
Constant element reference type.
Definition: Matrix.hpp:1619
const T(* ConstArrayPointer)[N]
Constant pointer-to-row type for raw access to the element array.
Definition: Matrix.hpp:1644
BoundedMatrix & operator+=(const MatrixExpression< E > &e)
Adds the matrix expression e element-wise to this bounded matrix (via a temporary to handle aliasing)...
Definition: Matrix.hpp:1937
std::shared_ptr< SelfType > SharedPointer
A reference-counted smart pointer [SHPTR] for dynamically allocated BoundedMatrix instances.
Definition: Matrix.hpp:1679
BoundedMatrix(const MatrixExpression< E > &e)
Constructs a bounded matrix from the matrix expression e.
Definition: Matrix.hpp:1757
BoundedMatrix & operator-=(const MatrixContainer< C > &c)
Subtracts the contents of the matrix container c element-wise from this bounded matrix (no alias chec...
Definition: Matrix.hpp:1950
ConstArrayPointer getData() const
Returns a const pointer-to-row to the contiguous 2D element array.
Definition: Matrix.hpp:1848
BoundedMatrix & plusAssign(const MatrixExpression< E > &e)
Adds the elements of the matrix expression e to this matrix without intermediate temporary.
Definition: Matrix.hpp:2040
T & Reference
Mutable element reference type.
Definition: Matrix.hpp:1614
BoundedMatrix & assign(const MatrixExpression< E > &e)
Resizes this matrix to match e and assigns its elements without intermediate temporary.
Definition: Matrix.hpp:2012
void resize(SizeType m, SizeType n, const ValueType &v)
Resizes the matrix to m×n elements (newly added elements are set to v).
Definition: Matrix.hpp:2141
BoundedMatrix(SizeType m, SizeType n, const ValueType &v)
Constructs a bounded matrix of size m×n with every element initialized to v.
Definition: Matrix.hpp:1722
BoundedMatrix & assign(InitializerListType l)
Resizes this matrix to match l and assigns its elements.
Definition: Matrix.hpp:2025
BoundedMatrix & operator+=(InitializerListType l)
Adds the rows in l element-wise to this bounded matrix.
Definition: Matrix.hpp:1925
T ValueType
The scalar value type.
Definition: Matrix.hpp:1609
const T * ConstPointer
Constant pointer type to a single element.
Definition: Matrix.hpp:1654
BoundedMatrix & plusAssign(InitializerListType l)
Adds the rows in l element-wise to this matrix without intermediate temporary.
Definition: Matrix.hpp:2051
Reference operator()(SizeType i, SizeType j)
Returns a mutable reference to the element at (i, j).
Definition: Matrix.hpp:1771
ConstReference operator()(SizeType i, SizeType j) const
Returns a const reference to the element at (i, j).
Definition: Matrix.hpp:1784
SelfType MatrixTemporaryType
Concrete temporary matrix type used by expression template machinery.
Definition: Matrix.hpp:1669
BoundedVector< T, M *N > VectorTemporaryType
Concrete temporary vector type used when assembling vectors from this matrix (a bounded vector of cap...
Definition: Matrix.hpp:1674
BoundedMatrix(InitializerListType l)
Constructs a bounded matrix from the initializer list of rows l.
Definition: Matrix.hpp:1744
BoundedMatrix()
Constructs an empty bounded matrix (zero rows, zero columns).
Definition: Matrix.hpp:1700
BoundedMatrix(SizeType m, SizeType n)
Constructs a bounded matrix of size m×n with value-initialized elements.
Definition: Matrix.hpp:1709
static const SizeType MaxSize2
The compile-time maximum number of columns N.
Definition: Matrix.hpp:1695
ArrayPointer getData()
Returns a mutable pointer-to-row to the contiguous 2D element array.
Definition: Matrix.hpp:1839
SizeType getMaxSize2() const
Returns the compile-time maximum column count N.
Definition: Matrix.hpp:1830
void resize(SizeType m, SizeType n)
Resizes the matrix to m×n elements (new elements are left value-uninitialized).
Definition: Matrix.hpp:2125
SizeType getMaxSize1() const
Returns the compile-time maximum row count M.
Definition: Matrix.hpp:1821
MatrixReference< SelfType > ClosureType
Closure type used when this matrix appears inside another expression.
Definition: Matrix.hpp:1659
BoundedMatrix & operator=(const MatrixContainer< C > &c)
Assigns the contents of the matrix container c to this bounded matrix (no alias check needed).
Definition: Matrix.hpp:1878
ValueType ArrayType[M][N]
The fixed-capacity 2D C-array type used for in-memory storage.
Definition: Matrix.hpp:1634
BoundedMatrix & operator=(const BoundedMatrix &m)
Copy-assigns the elements of m to this bounded matrix.
Definition: Matrix.hpp:1858
std::ptrdiff_t DifferenceType
The signed difference type.
Definition: Matrix.hpp:1629
BoundedMatrix & minusAssign(const MatrixExpression< E > &e)
Subtracts the elements of the matrix expression e from this matrix without intermediate temporary.
Definition: Matrix.hpp:2064
bool isEmpty() const
Tells whether the matrix is empty.
Definition: Matrix.hpp:1794
Variable-size vector with a fixed upper capacity N stored in a stack-allocated array.
Definition: Vector.hpp:1518
Fixed-size dense matrix of dimension M×N backed by a stack-allocated array.
Definition: Matrix.hpp:2180
MatrixReference< SelfType > ClosureType
Closure type used when this matrix appears inside another expression.
Definition: Matrix.hpp:2238
T * Pointer
Pointer type to a single element.
Definition: Matrix.hpp:2228
CMatrix & operator-=(const MatrixContainer< C > &c)
Subtracts the contents of the matrix container c element-wise from this fixed-size matrix (no alias c...
Definition: Matrix.hpp:2507
CMatrix & operator=(InitializerListType l)
Assigns the rows in l to this fixed-size matrix (clipped or zero-padded to M×N).
Definition: Matrix.hpp:2447
SizeType getSize2() const
Returns the fixed column count N.
Definition: Matrix.hpp:2373
CMatrix & assign(InitializerListType l)
Assigns the rows in l to this fixed-size matrix (clipped or zero-padded to M×N).
Definition: Matrix.hpp:2580
ValueType ArrayType[M][N]
The fixed-size 2D C-array type used for in-memory storage of M×N elements.
Definition: Matrix.hpp:2213
ConstArrayPointer getData() const
Returns a const pointer-to-row to the contiguous 2D element array.
Definition: Matrix.hpp:2409
ArrayPointer getData()
Returns a mutable pointer-to-row to the contiguous 2D element array.
Definition: Matrix.hpp:2400
std::initializer_list< std::initializer_list< T > > InitializerListType
The initializer list type accepted by constructors and assignment.
Definition: Matrix.hpp:2263
T(* ArrayPointer)[N]
Pointer-to-row type for raw access to the element array.
Definition: Matrix.hpp:2218
CMatrix & operator+=(const MatrixExpression< E > &e)
Adds the matrix expression e element-wise to this fixed-size matrix (via a temporary to handle aliasi...
Definition: Matrix.hpp:2494
BoundedVector< T, M *N > VectorTemporaryType
Concrete temporary vector type used when assembling vectors from this matrix.
Definition: Matrix.hpp:2253
CMatrix & minusAssign(const MatrixExpression< E > &e)
Subtracts the elements of the matrix expression e from this matrix without intermediate temporary.
Definition: Matrix.hpp:2634
ConstReference operator()(SizeType i, SizeType j) const
Returns a const reference to the element at (i, j).
Definition: Matrix.hpp:2345
CMatrix(const ValueType &v)
Constructs an M×N matrix with every element initialized to v.
Definition: Matrix.hpp:2289
CMatrix()
Constructs a zero-initialized M×N matrix.
Definition: Matrix.hpp:2279
std::enable_if< IsScalar< T1 >::value, CMatrix >::type & operator*=(const T1 &t)
Multiplies every element by the scalar t.
Definition: Matrix.hpp:2542
Reference operator()(SizeType i, SizeType j)
Returns a mutable reference to the element at (i, j).
Definition: Matrix.hpp:2332
void clear(const ValueType &v=ValueType())
Sets every element of the matrix to the value v.
Definition: Matrix.hpp:2677
std::size_t SizeType
The unsigned size type.
Definition: Matrix.hpp:2203
CMatrix(const MatrixExpression< E > &e)
Constructs a fixed-size matrix from the matrix expression e.
Definition: Matrix.hpp:2320
CMatrix(InitializerListType l)
Constructs a fixed-size matrix with the contents of the initializer list of rows l.
Definition: Matrix.hpp:2309
CMatrix & operator-=(InitializerListType l)
Subtracts the rows in l element-wise from this fixed-size matrix.
Definition: Matrix.hpp:2517
bool isEmpty() const
Tells whether the matrix is empty (M or N is zero).
Definition: Matrix.hpp:2355
T ValueType
The scalar value type.
Definition: Matrix.hpp:2188
std::enable_if< IsScalar< T1 >::value, CMatrix >::type & operator/=(const T1 &t)
Divides every element by the scalar t.
Definition: Matrix.hpp:2555
const T * ConstPointer
Constant pointer type to a single element.
Definition: Matrix.hpp:2233
void swap(CMatrix &m)
Swaps the contents of this fixed-size matrix with those of m.
Definition: Matrix.hpp:2655
CMatrix & operator=(const CMatrix &m)
Copy-assigns the elements of m to this fixed-size matrix.
Definition: Matrix.hpp:2419
CMatrix & operator=(const MatrixContainer< C > &c)
Assigns the contents of the matrix container c to this fixed-size matrix (no alias check needed).
Definition: Matrix.hpp:2436
const T(* ConstArrayPointer)[N]
Constant pointer-to-row type for raw access to the element array.
Definition: Matrix.hpp:2223
SizeType getMaxSize1() const
Returns the fixed row count M (capacity equals size for Math::CMatrix).
Definition: Matrix.hpp:2382
SizeType getMaxSize2() const
Returns the fixed column count N (capacity equals size for Math::CMatrix).
Definition: Matrix.hpp:2391
CMatrix & operator+=(InitializerListType l)
Adds the rows in l element-wise to this fixed-size matrix.
Definition: Matrix.hpp:2482
static const SizeType Size1
The compile-time fixed row count M.
Definition: Matrix.hpp:2269
CMatrix & plusAssign(InitializerListType l)
Adds the rows in l element-wise to this matrix without intermediate temporary.
Definition: Matrix.hpp:2621
CMatrix & operator-=(const MatrixExpression< E > &e)
Subtracts the matrix expression e element-wise from this fixed-size matrix (via a temporary to handle...
Definition: Matrix.hpp:2529
const T & ConstReference
Constant element reference type.
Definition: Matrix.hpp:2198
std::shared_ptr< SelfType > SharedPointer
A reference-counted smart pointer [SHPTR] for dynamically allocated CMatrix instances.
Definition: Matrix.hpp:2258
CMatrix(const CMatrix &m)
Constructs a copy of the fixed-size matrix m.
Definition: Matrix.hpp:2299
CMatrix & assign(const MatrixExpression< E > &e)
Assigns the elements of the matrix expression e to this fixed-size matrix without intermediate tempor...
Definition: Matrix.hpp:2568
friend void swap(CMatrix &m1, CMatrix &m2)
ADL-enabled free-function form of swap().
Definition: Matrix.hpp:2668
const MatrixReference< const SelfType > ConstClosureType
Constant closure type used when this matrix appears inside another expression.
Definition: Matrix.hpp:2243
SizeType getSize1() const
Returns the fixed row count M.
Definition: Matrix.hpp:2364
CMatrix & operator+=(const MatrixContainer< C > &c)
Adds the contents of the matrix container c element-wise to this fixed-size matrix (no alias check ne...
Definition: Matrix.hpp:2472
T & Reference
Mutable element reference type.
Definition: Matrix.hpp:2193
BoundedMatrix< T, M, N > MatrixTemporaryType
Concrete temporary matrix type used by expression template machinery (a Math::BoundedMatrix of equal ...
Definition: Matrix.hpp:2248
CMatrix & minusAssign(InitializerListType l)
Subtracts the rows in l element-wise from this matrix without intermediate temporary.
Definition: Matrix.hpp:2645
CMatrix & operator=(const MatrixExpression< E > &e)
Assigns the matrix expression e to this fixed-size matrix (via a temporary to handle aliasing).
Definition: Matrix.hpp:2459
CMatrix & plusAssign(const MatrixExpression< E > &e)
Adds the elements of the matrix expression e to this matrix without intermediate temporary.
Definition: Matrix.hpp:2610
static const SizeType Size2
The compile-time fixed column count N.
Definition: Matrix.hpp:2274
std::ptrdiff_t DifferenceType
The signed difference type.
Definition: Matrix.hpp:2208
Constant identity-matrix expression (1 on the diagonal, 0 elsewhere).
Definition: Matrix.hpp:3082
SizeType getMaxSize2() const
Returns the maximum representable column count.
Definition: Matrix.hpp:3206
T ValueType
The scalar value type.
Definition: Matrix.hpp:3090
IdentityMatrix & operator=(const IdentityMatrix &m)
Copy-assigns the dimensions from m.
Definition: Matrix.hpp:3216
const T & Reference
Reference type (always a const reference — elements are immutable).
Definition: Matrix.hpp:3095
ConstReference operator()(SizeType i, SizeType j) const
Returns a const reference to either 1 (on the diagonal) or 0 (off the diagonal).
Definition: Matrix.hpp:3160
void resize(SizeType m, SizeType n)
Resizes the dimensions to m×n.
Definition: Matrix.hpp:3253
IdentityMatrix(SizeType m, SizeType n)
Constructs an identity matrix of size m×n (1 on the diagonal, 0 elsewhere).
Definition: Matrix.hpp:3143
const MatrixReference< const SelfType > ConstClosureType
Constant closure type used when this matrix appears inside another expression.
Definition: Matrix.hpp:3120
SizeType getMaxSize1() const
Returns the maximum representable row count.
Definition: Matrix.hpp:3197
bool isEmpty() const
Tells whether the matrix is empty.
Definition: Matrix.hpp:3170
MatrixReference< SelfType > ClosureType
Closure type used when this matrix appears inside another expression.
Definition: Matrix.hpp:3115
SizeType getSize1() const
Returns the logical number of rows.
Definition: Matrix.hpp:3179
friend void swap(IdentityMatrix &m1, IdentityMatrix &m2)
ADL-enabled free-function form of swap().
Definition: Matrix.hpp:3243
std::ptrdiff_t DifferenceType
The signed difference type.
Definition: Matrix.hpp:3110
IdentityMatrix()
Constructs an empty identity matrix.
Definition: Matrix.hpp:3135
Matrix< T > MatrixTemporaryType
Concrete temporary matrix type used by expression template machinery.
Definition: Matrix.hpp:3125
IdentityMatrix(const IdentityMatrix &m)
Constructs a copy of the identity matrix m.
Definition: Matrix.hpp:3150
std::size_t SizeType
The unsigned size type.
Definition: Matrix.hpp:3105
void swap(IdentityMatrix &m)
Swaps the dimensions with m.
Definition: Matrix.hpp:3230
SizeType getSize2() const
Returns the logical number of columns.
Definition: Matrix.hpp:3188
const T & ConstReference
Constant element reference type.
Definition: Matrix.hpp:3100
Vector< T, std::vector< T > > VectorTemporaryType
Concrete temporary vector type used when assembling vectors from this matrix.
Definition: Matrix.hpp:3130
Lightweight matrix container that wraps a nested std::initializer_list of T values.
Definition: Matrix.hpp:361
Matrix< T, std::vector< T > > MatrixTemporaryType
Concrete temporary matrix type used by expression template machinery.
Definition: Matrix.hpp:412
std::initializer_list< std::initializer_list< T > > InitializerListType
The nested std::initializer_list type wrapped by this matrix.
Definition: Matrix.hpp:372
SizeType getSize2() const
Returns the number of columns (the longest row length).
Definition: Matrix.hpp:480
std::ptrdiff_t DifferenceType
The signed difference type.
Definition: Matrix.hpp:397
Reference operator()(SizeType i, SizeType j)
Returns a mutable reference to the element at row i and column j.
Definition: Matrix.hpp:440
InitializerListType::value_type::reference Reference
Mutable element reference type.
Definition: Matrix.hpp:387
bool isEmpty() const
Tells whether the matrix is empty (zero rows or zero columns).
Definition: Matrix.hpp:489
InitListMatrix(InitializerListType l)
Constructs an InitListMatrix wrapping the nested initializer list l.
Definition: Matrix.hpp:426
InitListMatrix SelfType
Convenience alias for this instantiation.
Definition: Matrix.hpp:367
Vector< T, std::vector< T > > VectorTemporaryType
Concrete temporary vector type used by expression template machinery.
Definition: Matrix.hpp:417
InitializerListType::value_type::const_reference ConstReference
Constant element reference type.
Definition: Matrix.hpp:382
SelfType ClosureType
Closure type used when this matrix appears inside another expression.
Definition: Matrix.hpp:402
const SelfType ConstClosureType
Constant closure type used when this matrix appears inside another expression.
Definition: Matrix.hpp:407
InitializerListType::size_type SizeType
The unsigned size type.
Definition: Matrix.hpp:392
ConstReference operator()(SizeType i, SizeType j) const
Returns a const reference to the element at row i and column j.
Definition: Matrix.hpp:457
InitializerListType::value_type::value_type ValueType
The scalar value type.
Definition: Matrix.hpp:377
SizeType getSize1() const
Returns the number of rows.
Definition: Matrix.hpp:471
Refinement of Math::MatrixExpression marking the derived type as a concrete (writable) matrix contain...
Definition: Expression.hpp:262
CRTP base class of all matrix expression types.
Definition: Expression.hpp:108
Lightweight matrix expression that proxies a reference to an underlying matrix container.
Definition: Matrix.hpp:64
M::ConstReference ConstReference
Constant element reference type.
Definition: Matrix.hpp:89
Reference operator()(SizeType i, SizeType j)
Returns a mutable reference to the element at (i, j).
Definition: Matrix.hpp:124
MatrixReference & operator=(const MatrixReference &r)
Copy-assigns the wrapped matrix from the matrix referenced by r.
Definition: Matrix.hpp:217
std::conditional< std::is_const< M >::value, typename M::ConstReference, typename M::Reference >::type Reference
Mutable element reference type (degrades to ConstReference when the wrapped matrix is const).
Definition: Matrix.hpp:84
bool isEmpty() const
Tells whether the wrapped matrix is empty.
Definition: Matrix.hpp:189
MatrixReference & assign(const MatrixExpression< E > &e)
Assigns the matrix expression e to the wrapped matrix without intermediate temporary.
Definition: Matrix.hpp:295
const MatrixType & getData() const
Returns a const reference to the wrapped matrix.
Definition: Matrix.hpp:198
friend void swap(MatrixReference &r1, MatrixReference &r2)
ADL-enabled free-function form of swap().
Definition: Matrix.hpp:341
std::enable_if< IsScalar< T >::value, MatrixReference >::type & operator*=(const T &t)
Multiplies every element of the wrapped matrix by the scalar t.
Definition: Matrix.hpp:269
SizeType getMaxSize() const
Returns the wrapped matrix's maximum total element count.
Definition: Matrix.hpp:162
M::SizeType SizeType
The unsigned size type of the wrapped matrix.
Definition: Matrix.hpp:94
MatrixReference & operator-=(const MatrixExpression< E > &e)
Subtracts the matrix expression e element-wise from the wrapped matrix.
Definition: Matrix.hpp:256
SizeType getMaxSize1() const
Returns the wrapped matrix's maximum number of rows.
Definition: Matrix.hpp:171
MatrixReference & operator=(const MatrixExpression< E > &e)
Assigns the matrix expression e to the wrapped matrix.
Definition: Matrix.hpp:230
std::enable_if< IsScalar< T >::value, MatrixReference >::type & operator/=(const T &t)
Divides every element of the wrapped matrix by the scalar t.
Definition: Matrix.hpp:282
MatrixReference & operator+=(const MatrixExpression< E > &e)
Adds the matrix expression e element-wise to the wrapped matrix.
Definition: Matrix.hpp:243
SizeType getSize1() const
Returns the wrapped matrix's number of rows.
Definition: Matrix.hpp:144
const SelfType ConstClosureType
Constant closure type used when this proxy appears inside another expression.
Definition: Matrix.hpp:109
MatrixReference & minusAssign(const MatrixExpression< E > &e)
Subtracts the matrix expression e from the wrapped matrix without intermediate temporary.
Definition: Matrix.hpp:321
MatrixReference(MatrixType &m)
Constructs the reference proxy referring to m.
Definition: Matrix.hpp:115
SizeType getSize2() const
Returns the wrapped matrix's number of columns.
Definition: Matrix.hpp:153
MatrixReference & plusAssign(const MatrixExpression< E > &e)
Adds the matrix expression e to the wrapped matrix without intermediate temporary.
Definition: Matrix.hpp:308
SelfType ClosureType
Closure type used when this proxy appears inside another expression.
Definition: Matrix.hpp:104
ConstReference operator()(SizeType i, SizeType j) const
Returns a const reference to the element at (i, j).
Definition: Matrix.hpp:135
M::ValueType ValueType
The element value type of the wrapped matrix.
Definition: Matrix.hpp:77
M MatrixType
The wrapped matrix type.
Definition: Matrix.hpp:72
void swap(MatrixReference &r)
Swaps the contents of the two wrapped matrices.
Definition: Matrix.hpp:331
M::DifferenceType DifferenceType
The signed difference type of the wrapped matrix.
Definition: Matrix.hpp:99
SizeType getMaxSize2() const
Returns the wrapped matrix's maximum number of columns.
Definition: Matrix.hpp:180
MatrixType & getData()
Returns a reference to the wrapped matrix.
Definition: Matrix.hpp:207
Dynamically-sized dense row-major matrix with configurable underlying storage.
Definition: Matrix.hpp:510
A::size_type SizeType
The unsigned size type used by the underlying storage container.
Definition: Matrix.hpp:533
MatrixReference< SelfType > ClosureType
Closure type used when this matrix appears inside another expression.
Definition: Matrix.hpp:558
Matrix(SizeType m, SizeType n, const ValueType &v)
Constructs an m×n matrix with every element initialized to v.
Definition: Matrix.hpp:605
std::enable_if< IsScalar< T1 >::value, Matrix >::type & operator/=(const T1 &t)
Divides every element by the scalar t.
Definition: Matrix.hpp:883
SizeType getSize2() const
Returns the number of columns.
Definition: Matrix.hpp:699
Matrix(const MatrixExpression< E > &e)
Constructs the matrix from the matrix expression e (materializing the expression result).
Definition: Matrix.hpp:645
Matrix & operator+=(InitializerListType l)
Adds the rows in l element-wise to this matrix.
Definition: Matrix.hpp:808
std::enable_if< IsScalar< T1 >::value, Matrix >::type & operator*=(const T1 &t)
Multiplies every element by the scalar t.
Definition: Matrix.hpp:870
SizeType getSize1() const
Returns the number of rows.
Definition: Matrix.hpp:690
bool isEmpty() const
Tells whether the matrix is empty.
Definition: Matrix.hpp:681
Matrix & operator=(const MatrixExpression< E > &e)
Assigns the matrix expression e to this matrix (via a temporary to handle aliasing).
Definition: Matrix.hpp:784
Matrix(Matrix &&m)
Move-constructs a matrix from m (m is left in a valid empty state).
Definition: Matrix.hpp:619
Matrix(const Matrix &m)
Constructs a copy of the matrix m.
Definition: Matrix.hpp:612
Matrix & assign(InitializerListType l)
Resizes this matrix to match l and assigns its elements.
Definition: Matrix.hpp:908
std::shared_ptr< SelfType > SharedPointer
A reference-counted smart pointer [SHPTR] for dynamically allocated Matrix instances.
Definition: Matrix.hpp:578
Vector< T, A > VectorTemporaryType
Concrete temporary vector type compatible with this matrix's value type and storage.
Definition: Matrix.hpp:573
const ArrayType & getData() const
Returns a const reference to the underlying storage container (row-major linear layout).
Definition: Matrix.hpp:726
Matrix & operator-=(const MatrixExpression< E > &e)
Subtracts the matrix expression e element-wise from this matrix (via a temporary to handle aliasing).
Definition: Matrix.hpp:856
Matrix & operator=(const MatrixContainer< C > &c)
Assigns the contents of the matrix container c to this matrix (no alias check needed).
Definition: Matrix.hpp:762
Matrix()
Constructs an empty matrix (zero rows and zero columns).
Definition: Matrix.hpp:588
Matrix(InitializerListType l)
Constructs the matrix from a brace-initializer list of rows.
Definition: Matrix.hpp:633
const T & ConstReference
Constant element reference type.
Definition: Matrix.hpp:528
Matrix & plusAssign(InitializerListType l)
Adds the rows in l element-wise to this matrix without intermediate temporary.
Definition: Matrix.hpp:934
const T * ConstPointer
Constant pointer type for raw element access.
Definition: Matrix.hpp:553
Matrix & operator-=(const MatrixContainer< C > &c)
Subtracts the contents of the matrix container c element-wise from this matrix (no alias check needed...
Definition: Matrix.hpp:834
SizeType getMaxSize() const
Returns the maximum total element count the underlying storage container can hold.
Definition: Matrix.hpp:708
T * Pointer
Pointer type for raw element access.
Definition: Matrix.hpp:548
Reference operator()(SizeType i, SizeType j)
Returns a mutable reference to the element at (i, j).
Definition: Matrix.hpp:658
SelfType MatrixTemporaryType
Concrete temporary matrix type used by expression template machinery.
Definition: Matrix.hpp:568
friend void swap(Matrix &m1, Matrix &m2)
ADL-enabled free-function form of swap().
Definition: Matrix.hpp:982
ConstReference operator()(SizeType i, SizeType j) const
Returns a const reference to the element at (i, j).
Definition: Matrix.hpp:671
void swap(Matrix &m)
Swaps the contents of this matrix with those of m.
Definition: Matrix.hpp:968
Matrix & assign(const MatrixExpression< E > &e)
Resizes this matrix to match the dimensions of e and assigns its elements (without intermediate tempo...
Definition: Matrix.hpp:896
Matrix & operator-=(InitializerListType l)
Subtracts the rows in l element-wise from this matrix.
Definition: Matrix.hpp:844
Matrix & operator=(Matrix &&m)
Move-assigns the contents of m to this matrix.
Definition: Matrix.hpp:749
A ArrayType
The underlying storage container type (row-major linear storage).
Definition: Matrix.hpp:543
void resize(SizeType m, SizeType n, bool preserve=true, const ValueType &v=ValueType())
Resizes the matrix to m×n elements.
Definition: Matrix.hpp:1003
Matrix & operator=(const Matrix &m)
Copy-assigns the contents of m to this matrix.
Definition: Matrix.hpp:736
Matrix & operator=(InitializerListType l)
Assigns the rows in l to this matrix (resizes to match).
Definition: Matrix.hpp:772
const MatrixReference< const SelfType > ConstClosureType
Constant closure type used when this matrix appears inside another expression.
Definition: Matrix.hpp:563
std::initializer_list< std::initializer_list< T > > InitializerListType
Type of the brace-initializer list of lists accepted by the corresponding constructor (one inner list...
Definition: Matrix.hpp:583
Matrix & plusAssign(const MatrixExpression< E > &e)
Adds the elements of the matrix expression e to this matrix without intermediate temporary.
Definition: Matrix.hpp:923
T & Reference
Mutable element reference type.
Definition: Matrix.hpp:523
Matrix & operator+=(const MatrixExpression< E > &e)
Adds the matrix expression e element-wise to this matrix (via a temporary to handle aliasing).
Definition: Matrix.hpp:820
T ValueType
The scalar value type stored in the matrix.
Definition: Matrix.hpp:518
Matrix(SizeType m, SizeType n)
Constructs an m×n matrix with default-initialized elements.
Definition: Matrix.hpp:596
ArrayType & getData()
Returns a mutable reference to the underlying storage container (row-major linear layout).
Definition: Matrix.hpp:717
Matrix & minusAssign(InitializerListType l)
Subtracts the rows in l element-wise from this matrix without intermediate temporary.
Definition: Matrix.hpp:958
A::difference_type DifferenceType
The signed difference type used by the underlying storage container.
Definition: Matrix.hpp:538
Matrix & minusAssign(const MatrixExpression< E > &e)
Subtracts the elements of the matrix expression e from this matrix without intermediate temporary.
Definition: Matrix.hpp:947
Matrix & operator+=(const MatrixContainer< C > &c)
Adds the contents of the matrix container c element-wise to this matrix (no alias check needed).
Definition: Matrix.hpp:798
void clear(const ValueType &v=ValueType())
Sets every element of the matrix to the value v.
Definition: Matrix.hpp:991
Constant matrix expression in which every entry equals the same scalar value.
Definition: Matrix.hpp:2890
std::size_t SizeType
The unsigned size type.
Definition: Matrix.hpp:2913
std::ptrdiff_t DifferenceType
The signed difference type.
Definition: Matrix.hpp:2918
SizeType getMaxSize2() const
Returns the maximum representable column count.
Definition: Matrix.hpp:3015
SizeType getSize2() const
Returns the logical number of columns.
Definition: Matrix.hpp:2997
ConstReference operator()(SizeType i, SizeType j) const
Returns a const reference to the common entry value.
Definition: Matrix.hpp:2969
const MatrixReference< const SelfType > ConstClosureType
Constant closure type used when this matrix appears inside another expression.
Definition: Matrix.hpp:2928
ScalarMatrix(const ScalarMatrix &m)
Constructs a copy of the scalar matrix m.
Definition: Matrix.hpp:2959
void swap(ScalarMatrix &m)
Swaps the dimensions and common value with m.
Definition: Matrix.hpp:3040
const T & ConstReference
Constant reference type to the common value.
Definition: Matrix.hpp:2908
friend void swap(ScalarMatrix &m1, ScalarMatrix &m2)
ADL-enabled free-function form of swap().
Definition: Matrix.hpp:3054
SizeType getSize1() const
Returns the logical number of rows.
Definition: Matrix.hpp:2988
Vector< T, std::vector< T > > VectorTemporaryType
Concrete temporary vector type used when assembling vectors from this matrix.
Definition: Matrix.hpp:2938
SizeType getMaxSize1() const
Returns the maximum representable row count.
Definition: Matrix.hpp:3006
void resize(SizeType m, SizeType n)
Resizes the dimensions to m×n.
Definition: Matrix.hpp:3064
T ValueType
The scalar value type.
Definition: Matrix.hpp:2898
bool isEmpty() const
Tells whether the matrix is empty.
Definition: Matrix.hpp:2979
ScalarMatrix()
Constructs an empty scalar matrix.
Definition: Matrix.hpp:2943
Matrix< T > MatrixTemporaryType
Concrete temporary matrix type used by expression template machinery.
Definition: Matrix.hpp:2933
ScalarMatrix & operator=(const ScalarMatrix &m)
Copy-assigns the dimensions and common value from m.
Definition: Matrix.hpp:3025
ScalarMatrix(SizeType m, SizeType n, const ValueType &v=ValueType())
Constructs a scalar matrix of size m×n in which every entry equals v.
Definition: Matrix.hpp:2952
MatrixReference< SelfType > ClosureType
Closure type used when this matrix appears inside another expression.
Definition: Matrix.hpp:2923
const T & Reference
Reference type (always a const reference — elements are immutable).
Definition: Matrix.hpp:2903
Proxy that exposes a single (key, value) entry of a sparse container as a writable reference.
Definition: SparseContainerElement.hpp:52
Sparse matrix that stores only non-default entries keyed by a packed (row, column) identifier.
Definition: Matrix.hpp:1043
const MatrixReference< const SelfType > ConstClosureType
Constant closure type used when this matrix appears inside another expression.
Definition: Matrix.hpp:1101
std::ptrdiff_t DifferenceType
The signed difference type.
Definition: Matrix.hpp:1076
ArrayType::size_type getMaxSize() const
Returns the maximum number of stored entries the underlying associative container can hold.
Definition: Matrix.hpp:1255
SparseMatrix & operator=(const MatrixExpression< E > &e)
Assigns the matrix expression e to this sparse matrix (via a temporary to handle aliasing).
Definition: Matrix.hpp:1331
std::enable_if< IsScalar< T1 >::value, SparseMatrix >::type & operator*=(const T1 &t)
Multiplies every stored entry by the scalar t.
Definition: Matrix.hpp:1417
const T * ConstPointer
Constant pointer type for raw access to stored entries.
Definition: Matrix.hpp:1091
SparseContainerElement< SelfType > Reference
Mutable element reference type (a proxy object that inserts on assignment to a previously-absent elem...
Definition: Matrix.hpp:1061
SparseMatrix & operator=(const SparseMatrix &m)
Copy-assigns the contents of m to this sparse matrix.
Definition: Matrix.hpp:1283
T ValueType
The scalar value type.
Definition: Matrix.hpp:1051
const T & ConstReference
Constant element reference type.
Definition: Matrix.hpp:1066
void resize(SizeType m, SizeType n)
Resizes the logical dimensions to m×n, dropping any stored entries that fall outside the new bounds.
Definition: Matrix.hpp:1548
SparseMatrix & assign(InitializerListType l)
Resizes this matrix to match l and assigns its elements.
Definition: Matrix.hpp:1455
Reference operator()(SizeType i, SizeType j)
Returns a mutable proxy reference to the element at (i, j).
Definition: Matrix.hpp:1189
SparseMatrix()
Constructs an empty sparse matrix (zero rows, zero columns, no stored entries).
Definition: Matrix.hpp:1126
void clear()
Removes all explicitly stored entries (the logical dimensions remain unchanged).
Definition: Matrix.hpp:1537
std::initializer_list< std::initializer_list< T > > InitializerListType
The initializer list type accepted by constructors and assignment.
Definition: Matrix.hpp:1121
SizeType getSize1() const
Returns the logical number of rows.
Definition: Matrix.hpp:1237
SparseMatrix & operator=(SparseMatrix &&m)
Move-assigns the contents of m to this sparse matrix.
Definition: Matrix.hpp:1296
SelfType MatrixTemporaryType
Concrete temporary matrix type used by expression template machinery.
Definition: Matrix.hpp:1106
SparseMatrix & operator+=(InitializerListType l)
Adds the rows in l element-wise to this sparse matrix.
Definition: Matrix.hpp:1355
SparseMatrix & minusAssign(InitializerListType l)
Subtracts the rows in l element-wise from this matrix without intermediate temporary.
Definition: Matrix.hpp:1505
void swap(SparseMatrix &m)
Swaps the contents of this sparse matrix with those of m.
Definition: Matrix.hpp:1515
SparseMatrix(SizeType m, SizeType n)
Constructs a sparse matrix of size m×n with no stored entries.
Definition: Matrix.hpp:1135
std::enable_if< IsScalar< T1 >::value, SparseMatrix >::type & operator/=(const T1 &t)
Divides every stored entry by the scalar t.
Definition: Matrix.hpp:1430
SparseMatrix & operator-=(const MatrixExpression< E > &e)
Subtracts the matrix expression e element-wise from this sparse matrix (via a temporary to handle ali...
Definition: Matrix.hpp:1403
SparseMatrix(const SparseMatrix &m)
Constructs a copy of the sparse matrix m.
Definition: Matrix.hpp:1145
ConstReference operator()(SizeType i, SizeType j) const
Returns a const reference to the element at (i, j).
Definition: Matrix.hpp:1203
SparseMatrix & assign(const MatrixExpression< E > &e)
Resizes this matrix to match e and assigns its elements without intermediate temporary.
Definition: Matrix.hpp:1443
const ArrayType & getData() const
Returns a const reference to the underlying associative container of stored entries.
Definition: Matrix.hpp:1273
SparseMatrix & operator-=(InitializerListType l)
Subtracts the rows in l element-wise from this sparse matrix.
Definition: Matrix.hpp:1391
SparseMatrix(SparseMatrix &&m)
Move-constructs a sparse matrix from m (m is left in a valid empty state).
Definition: Matrix.hpp:1152
A ArrayType
The underlying associative container type.
Definition: Matrix.hpp:1081
SizeType getSize2() const
Returns the logical number of columns.
Definition: Matrix.hpp:1246
T * Pointer
Pointer type for raw access to stored entries.
Definition: Matrix.hpp:1086
MatrixReference< SelfType > ClosureType
Closure type used when this matrix appears inside another expression.
Definition: Matrix.hpp:1096
std::shared_ptr< SelfType > SharedPointer
A reference-counted smart pointer [SHPTR] for dynamically allocated SparseMatrix instances.
Definition: Matrix.hpp:1116
SparseMatrix & operator-=(const MatrixContainer< C > &c)
Subtracts the contents of the matrix container c element-wise from this sparse matrix (no alias check...
Definition: Matrix.hpp:1381
SparseMatrix(InitializerListType l)
Constructs a sparse matrix from the initializer list of rows l.
Definition: Matrix.hpp:1162
SparseMatrix & operator=(InitializerListType l)
Assigns the rows in l to this sparse matrix (resizes to match).
Definition: Matrix.hpp:1319
SparseMatrix & plusAssign(const MatrixExpression< E > &e)
Adds the elements of the matrix expression e to this matrix without intermediate temporary.
Definition: Matrix.hpp:1470
SparseMatrix & plusAssign(InitializerListType l)
Adds the rows in l element-wise to this matrix without intermediate temporary.
Definition: Matrix.hpp:1481
friend void swap(SparseMatrix &m1, SparseMatrix &m2)
ADL-enabled free-function form of swap().
Definition: Matrix.hpp:1529
SparseMatrix & operator+=(const MatrixContainer< C > &c)
Adds the contents of the matrix container c element-wise to this sparse matrix (no alias check needed...
Definition: Matrix.hpp:1345
SizeType getNumElements() const
Returns the number of explicitly stored (non-default) entries.
Definition: Matrix.hpp:1219
ArrayType & getData()
Returns a mutable reference to the underlying associative container of stored entries.
Definition: Matrix.hpp:1264
Vector< T, std::vector< T > > VectorTemporaryType
Concrete temporary vector type used when assembling vectors from this matrix.
Definition: Matrix.hpp:1111
SparseMatrix(const MatrixExpression< E > &e)
Constructs a sparse matrix from the matrix expression e.
Definition: Matrix.hpp:1175
SparseMatrix & operator+=(const MatrixExpression< E > &e)
Adds the matrix expression e element-wise to this sparse matrix (via a temporary to handle aliasing).
Definition: Matrix.hpp:1367
SparseMatrix & minusAssign(const MatrixExpression< E > &e)
Subtracts the elements of the matrix expression e from this matrix without intermediate temporary.
Definition: Matrix.hpp:1494
bool isEmpty() const
Tells whether the matrix is empty (zero rows or zero columns).
Definition: Matrix.hpp:1228
A::key_type KeyType
The packed (row, column) key type used by the underlying associative container.
Definition: Matrix.hpp:1056
std::uint32_t SizeType
The unsigned size type.
Definition: Matrix.hpp:1071
SparseMatrix & operator=(const MatrixContainer< C > &c)
Assigns the contents of the matrix container c to this sparse matrix (no alias check needed).
Definition: Matrix.hpp:1309
Dynamically-sized dense vector with configurable underlying storage.
Definition: Vector.hpp:480
Constant matrix expression whose entries are all zero.
Definition: Matrix.hpp:2698
friend void swap(ZeroMatrix &m1, ZeroMatrix &m2)
ADL-enabled free-function form of swap().
Definition: Matrix.hpp:2859
T ValueType
The scalar value type.
Definition: Matrix.hpp:2706
std::ptrdiff_t DifferenceType
The signed difference type.
Definition: Matrix.hpp:2726
const T & ConstReference
Constant reference type to the zero element.
Definition: Matrix.hpp:2716
SizeType getSize1() const
Returns the logical number of rows.
Definition: Matrix.hpp:2795
SizeType getMaxSize2() const
Returns the maximum representable column count.
Definition: Matrix.hpp:2822
ConstReference operator()(SizeType i, SizeType j) const
Returns a const reference to the zero element.
Definition: Matrix.hpp:2776
Vector< T, std::vector< T > > VectorTemporaryType
Concrete temporary vector type used when assembling vectors from this matrix.
Definition: Matrix.hpp:2746
bool isEmpty() const
Tells whether the matrix is empty (either dimension is zero).
Definition: Matrix.hpp:2786
const T & Reference
Reference type (always a const reference — all elements are zero).
Definition: Matrix.hpp:2711
SizeType getSize2() const
Returns the logical number of columns.
Definition: Matrix.hpp:2804
void swap(ZeroMatrix &m)
Swaps the dimensions with m.
Definition: Matrix.hpp:2846
void resize(SizeType m, SizeType n)
Resizes the dimensions to m×n.
Definition: Matrix.hpp:2869
const MatrixReference< const SelfType > ConstClosureType
Constant closure type used when this matrix appears inside another expression.
Definition: Matrix.hpp:2736
ZeroMatrix & operator=(const ZeroMatrix &m)
Copy-assigns the dimensions from m.
Definition: Matrix.hpp:2832
Matrix< T > MatrixTemporaryType
Concrete temporary matrix type used by expression template machinery.
Definition: Matrix.hpp:2741
SizeType getMaxSize1() const
Returns the maximum representable row count.
Definition: Matrix.hpp:2813
ZeroMatrix(const ZeroMatrix &m)
Constructs a copy of the zero matrix m.
Definition: Matrix.hpp:2766
ZeroMatrix()
Constructs an empty zero matrix.
Definition: Matrix.hpp:2751
MatrixReference< SelfType > ClosureType
Closure type used when this matrix appears inside another expression.
Definition: Matrix.hpp:2731
std::size_t SizeType
The unsigned size type.
Definition: Matrix.hpp:2721
ZeroMatrix(SizeType m, SizeType n)
Constructs a zero matrix of size m×n.
Definition: Matrix.hpp:2759
constexpr unsigned int A
Generic type that covers any element except hydrogen.
Definition: AtomType.hpp:637
constexpr unsigned int M
Generic type that covers any element that is a metal.
Definition: AtomType.hpp:657
constexpr unsigned int N
Specifies Nitrogen.
Definition: AtomType.hpp:97
constexpr unsigned int T
Specifies Hydrogen (Tritium).
Definition: AtomType.hpp:67
constexpr unsigned int r
Specifies that the stereocenter has r configuration.
Definition: CIPDescriptor.hpp:76
constexpr unsigned int m
Specifies that the stereocenter has m configuration.
Definition: CIPDescriptor.hpp:116
CMatrix< long, 2, 2 > Matrix2L
Bounded 2×2 matrix holding signed integers of type long.
Definition: Matrix.hpp:3514
CMatrix< float, 3, 3 > Matrix3F
Bounded 3×3 matrix holding floating-point values of type float.
Definition: Matrix.hpp:3489
ZeroMatrix< double > DZeroMatrix
Memory-efficient immutable matrix where all elements have the value zero of type double.
Definition: Matrix.hpp:3409
bool luSubstitute(const MatrixExpression< E1 > &lu, VectorExpression< E2 > &b)
Solves for b in place, given the LU decomposition lu (without pivoting).
Definition: LUDecomposition.hpp:195
CMatrix< unsigned long, 3, 3 > Matrix3UL
Bounded 3×3 matrix holding unsigned integers of type unsigned long.
Definition: Matrix.hpp:3534
Matrix< double > DMatrix
Unbounded dense matrix holding floating-point values of type double..
Definition: Matrix.hpp:3469
ScalarMatrix< float > FScalarMatrix
Memory-efficient immutable matrix where all elements have the same value of type float.
Definition: Matrix.hpp:3424
ZeroMatrix< long > LZeroMatrix
Memory-efficient immutable matrix where all elements have the value zero of type long.
Definition: Matrix.hpp:3414
ScalarMatrix< double > DScalarMatrix
Memory-efficient immutable matrix where all elements have the same value of type double.
Definition: Matrix.hpp:3429
SparseMatrix< unsigned long > SparseULMatrix
Unbounded sparse matrix holding unsigned integers of type unsigned long.
Definition: Matrix.hpp:3559
ZeroMatrix< float > FZeroMatrix
Memory-efficient immutable matrix where all elements have the value zero of type float.
Definition: Matrix.hpp:3404
IdentityMatrix< double > DIdentityMatrix
Memory-efficient immutable identity matrix with element values of type double.
Definition: Matrix.hpp:3449
CMatrix< long, 3, 3 > Matrix3L
Bounded 3×3 matrix holding signed integers of type long.
Definition: Matrix.hpp:3519
Matrix< unsigned long > ULMatrix
Unbounded dense matrix holding unsigned integers of type unsigned long.
Definition: Matrix.hpp:3479
CMatrix< double, 2, 2 > Matrix2D
Bounded 2×2 matrix holding floating-point values of type double.
Definition: Matrix.hpp:3499
SparseMatrix< float > SparseFMatrix
Unbounded sparse matrix holding floating-point values of type float..
Definition: Matrix.hpp:3544
IdentityMatrix< long > LIdentityMatrix
Memory-efficient immutable identity matrix with element values of type long.
Definition: Matrix.hpp:3454
MatrixRow< M > row(MatrixExpression< M > &e, typename MatrixRow< M >::SizeType i)
Returns a mutable row proxy for row i of the matrix expression e.
Definition: MatrixProxy.hpp:1372
E::ValueType det(const MatrixExpression< E > &e)
Returns the determinant of the matrix expression e.
Definition: Matrix.hpp:3311
CMatrix< unsigned long, 4, 4 > Matrix4UL
Bounded 4×4 matrix holding unsigned integers of type unsigned long.
Definition: Matrix.hpp:3539
CMatrix< double, 4, 4 > Matrix4D
Bounded 4×4 matrix holding floating-point values of type double.
Definition: Matrix.hpp:3509
SparseMatrix< long > SparseLMatrix
Unbounded sparse matrix holding signed integers of type long.
Definition: Matrix.hpp:3554
CMatrix< float, 4, 4 > Matrix4F
Bounded 4×4 matrix holding floating-point values of type float.
Definition: Matrix.hpp:3494
Matrix< long > LMatrix
Unbounded dense matrix holding signed integers of type long.
Definition: Matrix.hpp:3474
Matrix< float > FMatrix
Unbounded dense matrix holding floating-point values of type float..
Definition: Matrix.hpp:3464
IdentityMatrix< unsigned long > ULIdentityMatrix
Memory-efficient immutable identity matrix with element values of type unsigned long.
Definition: Matrix.hpp:3459
ZeroMatrix< unsigned long > ULZeroMatrix
Memory-efficient immutable matrix where all elements have the value zero of type unsigned long.
Definition: Matrix.hpp:3419
CMatrix< long, 4, 4 > Matrix4L
Bounded 4×4 matrix holding signed integers of type long.
Definition: Matrix.hpp:3524
CMatrix< double, 3, 3 > Matrix3D
Bounded 3×3 matrix holding floating-point values of type double.
Definition: Matrix.hpp:3504
SparseMatrix< double > SparseDMatrix
Unbounded sparse matrix holding floating-point values of type double..
Definition: Matrix.hpp:3549
ScalarMatrix< unsigned long > ULScalarMatrix
Memory-efficient immutable matrix where all elements have the same value of type unsigned long.
Definition: Matrix.hpp:3439
IdentityMatrix< float > FIdentityMatrix
Memory-efficient immutable identity matrix with element values of type float.
Definition: Matrix.hpp:3444
bool invert(const MatrixExpression< E > &e, MatrixContainer< C > &c)
Computes the inverse of the matrix expression e and stores it in c.
Definition: Matrix.hpp:3370
E::SizeType luDecompose(MatrixExpression< E > &e)
Computes an in-place LU decomposition of the matrix e without partial pivoting.
Definition: LUDecomposition.hpp:51
CMatrix< unsigned long, 2, 2 > Matrix2UL
Bounded 2×2 matrix holding unsigned integers of type unsigned long.
Definition: Matrix.hpp:3529
CMatrix< float, 2, 2 > Matrix2F
Bounded 2×2 matrix holding floating-point values of type float.
Definition: Matrix.hpp:3484
ScalarMatrix< long > LScalarMatrix
Memory-efficient immutable matrix where all elements have the same value of type long.
Definition: Matrix.hpp:3434
The namespace of the Chemical Data Processing Library.
Selects a concrete temporary matrix type compatible with the matrix expression M.
Definition: TypeTraits.hpp:337
M::MatrixTemporaryType Type
The concrete temporary matrix type compatible with the matrix expression M.
Definition: TypeTraits.hpp:342
Selects a concrete temporary vector type compatible with the vector expression V.
Definition: TypeTraits.hpp:323