]> www.ginac.de Git - ginac.git/blobdiff - ginac/matrix.cpp
Use neseted initializer lists to construct matrix objects.
[ginac.git] / ginac / matrix.cpp
index 52a1f1b423cd4b3eafea7a3b8c6d8a185565d26c..50f4631546a5795216287f27c9b9f272731fefa6 100644 (file)
@@ -73,20 +73,6 @@ matrix::matrix(unsigned r, unsigned c) : row(r), col(c), m(r*c, _ex0)
        setflag(status_flags::not_shareable);
 }
 
-// protected
-
-/** Ctor from representation, for internal use only. */
-matrix::matrix(unsigned r, unsigned c, const exvector & m2)
-  : row(r), col(c), m(m2)
-{
-       setflag(status_flags::not_shareable);
-}
-matrix::matrix(unsigned r, unsigned c, exvector && m2)
-  : row(r), col(c), m(std::move(m2))
-{
-       setflag(status_flags::not_shareable);
-}
-
 /** Construct matrix from (flat) list of elements. If the list has fewer
  *  elements than the matrix, the remaining matrix elements are set to zero.
  *  If the list has more elements than the matrix, the excessive elements are
@@ -107,6 +93,40 @@ matrix::matrix(unsigned r, unsigned c, const lst & l)
        }
 }
 
+/** Construct a matrix from an 2 dimensional initializer list.
+ *  Throws an exception if some row has a different length than all the others.
+ */
+matrix::matrix(std::initializer_list<std::initializer_list<ex>> l)
+  : row(l.size()), col(l.begin()->size())
+{
+       setflag(status_flags::not_shareable);
+
+       m.reserve(row*col);
+       for (const auto & r : l) {
+               unsigned c = 0;
+               for (const auto & e : r) {
+                       m.push_back(e);
+                       ++c;
+               }
+               if (c != col)
+                       throw std::invalid_argument("matrix::matrix{{}}: wrong dimension");
+       }
+}
+
+// protected
+
+/** Ctor from representation, for internal use only. */
+matrix::matrix(unsigned r, unsigned c, const exvector & m2)
+  : row(r), col(c), m(m2)
+{
+       setflag(status_flags::not_shareable);
+}
+matrix::matrix(unsigned r, unsigned c, exvector && m2)
+  : row(r), col(c), m(std::move(m2))
+{
+       setflag(status_flags::not_shareable);
+}
+
 //////////
 // archiving
 //////////
@@ -1221,9 +1241,8 @@ ex matrix::determinant_minor() const
                                for (unsigned j=fc; j<n-c; ++j)
                                        Pkey[j] = Pkey[j-1]+1;
                } while(fc);
-               // next column, so change the role of A and B:
-               A.swap(B);
-               B.clear();
+               // next column, clear B and change the role of A and B:
+               A = std::move(B);
        }
        
        return det;
@@ -1586,6 +1605,23 @@ ex diag_matrix(const lst & l)
        return M;
 }
 
+ex diag_matrix(std::initializer_list<ex> l)
+{
+       size_t dim = l.size();
+
+       // Allocate and fill matrix
+       matrix &M = *new matrix(dim, dim);
+       M.setflag(status_flags::dynallocated);
+
+       unsigned i = 0;
+       for (auto & it : l) {
+               M(i, i) = it;
+               ++i;
+       }
+
+       return M;
+}
+
 ex unit_matrix(unsigned r, unsigned c)
 {
        matrix &Id = *new matrix(r, c);