]> www.ginac.de Git - ginac.git/blobdiff - ginac/matrix.cpp
Make .eval() evaluate top-level only.
[ginac.git] / ginac / matrix.cpp
index 52a1f1b423cd4b3eafea7a3b8c6d8a185565d26c..c842885f8103c0a2cc6530b6fc65b6b203197c56 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
 //////////
@@ -207,28 +227,6 @@ ex & matrix::let_op(size_t i)
        return m[i];
 }
 
-/** Evaluate matrix entry by entry. */
-ex matrix::eval(int level) const
-{
-       // check if we have to do anything at all
-       if ((level==1)&&(flags & status_flags::evaluated))
-               return *this;
-       
-       // emergency break
-       if (level == -max_recursion_level)
-               throw (std::runtime_error("matrix::eval(): recursion limit exceeded"));
-       
-       // eval() entry by entry
-       exvector m2(row*col);
-       --level;
-       for (unsigned r=0; r<row; ++r)
-               for (unsigned c=0; c<col; ++c)
-                       m2[r*col+c] = m[r*col+c].eval(level);
-       
-       return (new matrix(row, col, std::move(m2)))->setflag(status_flags::dynallocated |
-                                                             status_flags::evaluated);
-}
-
 ex matrix::subs(const exmap & mp, unsigned options) const
 {
        exvector m2(row * col);
@@ -1221,9 +1219,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;
@@ -1553,8 +1550,7 @@ ex lst_to_matrix(const lst & l)
        }
 
        // Allocate and fill matrix
-       matrix &M = *new matrix(rows, cols);
-       M.setflag(status_flags::dynallocated);
+       matrix & M = dynallocate<matrix>(rows, cols);
 
        unsigned i = 0;
        for (auto & itr : l) {
@@ -1574,8 +1570,23 @@ ex diag_matrix(const lst & l)
        size_t dim = l.nops();
 
        // Allocate and fill matrix
-       matrix &M = *new matrix(dim, dim);
-       M.setflag(status_flags::dynallocated);
+       matrix & M = dynallocate<matrix>(dim, dim);
+
+       unsigned i = 0;
+       for (auto & it : l) {
+               M(i, i) = it;
+               ++i;
+       }
+
+       return M;
+}
+
+ex diag_matrix(std::initializer_list<ex> l)
+{
+       size_t dim = l.size();
+
+       // Allocate and fill matrix
+       matrix & M = dynallocate<matrix>(dim, dim);
 
        unsigned i = 0;
        for (auto & it : l) {
@@ -1588,8 +1599,8 @@ ex diag_matrix(const lst & l)
 
 ex unit_matrix(unsigned r, unsigned c)
 {
-       matrix &Id = *new matrix(r, c);
-       Id.setflag(status_flags::dynallocated | status_flags::evaluated);
+       matrix & Id = dynallocate<matrix>(r, c);
+       Id.setflag(status_flags::evaluated);
        for (unsigned i=0; i<r && i<c; i++)
                Id(i,i) = _ex1;
 
@@ -1598,8 +1609,8 @@ ex unit_matrix(unsigned r, unsigned c)
 
 ex symbolic_matrix(unsigned r, unsigned c, const std::string & base_name, const std::string & tex_base_name)
 {
-       matrix &M = *new matrix(r, c);
-       M.setflag(status_flags::dynallocated | status_flags::evaluated);
+       matrix & M = dynallocate<matrix>(r, c);
+       M.setflag(status_flags::evaluated);
 
        bool long_format = (r > 10 || c > 10);
        bool single_row = (r == 1 || c == 1);
@@ -1640,8 +1651,8 @@ ex reduced_matrix(const matrix& m, unsigned r, unsigned c)
 
        const unsigned rows = m.rows()-1;
        const unsigned cols = m.cols()-1;
-       matrix &M = *new matrix(rows, cols);
-       M.setflag(status_flags::dynallocated | status_flags::evaluated);
+       matrix & M = dynallocate<matrix>(rows, cols);
+       M.setflag(status_flags::evaluated);
 
        unsigned ro = 0;
        unsigned ro2 = 0;
@@ -1669,8 +1680,8 @@ ex sub_matrix(const matrix&m, unsigned r, unsigned nr, unsigned c, unsigned nc)
        if (r+nr>m.rows() || c+nc>m.cols())
                throw std::runtime_error("sub_matrix(): index out of bounds");
 
-       matrix &M = *new matrix(nr, nc);
-       M.setflag(status_flags::dynallocated | status_flags::evaluated);
+       matrix & M = dynallocate<matrix>(nr, nc);
+       M.setflag(status_flags::evaluated);
 
        for (unsigned ro=0; ro<nr; ++ro) {
                for (unsigned co=0; co<nc; ++co) {