]> www.ginac.de Git - ginac.git/blobdiff - ginac/inifcns.cpp
- added lst::remove_first() and lst::remove_last()
[ginac.git] / ginac / inifcns.cpp
index b55261eb1a0072939b0a1bb43a2a317d681778ec..0ee9b2737fd30ef8b31fe39dcb965f887ee114b8 100644 (file)
@@ -475,15 +475,15 @@ ex lsolve(const ex &eqns, const ex &symbols)
                for (unsigned c=0; c<symbols.nops(); c++) {
                        ex co = eq.coeff(ex_to_symbol(symbols.op(c)),1);
                        linpart -= co*symbols.op(c);
-                       sys.set(r,c,co);
+                       sys(r,c) = co;
                }
                linpart = linpart.expand();
-               rhs.set(r,0,-linpart);
+               rhs(r,0) = -linpart;
        }
        
        // test if system is linear and fill vars matrix
        for (unsigned i=0; i<symbols.nops(); i++) {
-               vars.set(i,0,symbols.op(i));
+               vars(i,0) = symbols.op(i);
                if (sys.has(symbols.op(i)))
                        throw(std::logic_error("lsolve: system is not linear"));
                if (rhs.has(symbols.op(i)))
@@ -509,18 +509,109 @@ ex lsolve(const ex &eqns, const ex &symbols)
        return sollist;
 }
 
-/** non-commutative power. */
-ex ncpow(const ex & basis, unsigned exponent)
+// Symmetrize/antisymmetrize over a vector of objects
+static ex symm(const ex & e, exvector::const_iterator first, exvector::const_iterator last, bool asymmetric)
 {
-       if (exponent == 0)
-               return _ex1();
+       // Need at least 2 objects for this operation
+       int num = last - first;
+       if (num < 2)
+               return e;
+
+       // Transform object vector to a list
+       exlist iv_lst;
+       iv_lst.insert(iv_lst.begin(), first, last);
+       lst orig_lst(iv_lst, true);
+
+       // Create index vectors for permutation
+       unsigned *iv = new unsigned[num], *iv2;
+       for (unsigned i=0; i<num; i++)
+               iv[i] = i;
+       iv2 = (asymmetric ? new unsigned[num] : NULL);
+
+       // Loop over all permutations (the first permutation, which is the
+       // identity, is unrolled)
+       ex sum = e;
+       while (std::next_permutation(iv, iv + num)) {
+               lst new_lst;
+               for (unsigned i=0; i<num; i++)
+                       new_lst.append(orig_lst.op(iv[i]));
+               ex term = e.subs(orig_lst, new_lst);
+               if (asymmetric) {
+                       memcpy(iv2, iv, num * sizeof(unsigned));
+                       term *= permutation_sign(iv2, iv2 + num);
+               }
+               sum += term;
+       }
+
+       delete[] iv;
+       delete[] iv2;
+
+       return sum / factorial(numeric(num));
+}
+
+ex symmetrize(const ex & e, exvector::const_iterator first, exvector::const_iterator last)
+{
+       return symm(e, first, last, false);
+}
+
+ex antisymmetrize(const ex & e, exvector::const_iterator first, exvector::const_iterator last)
+{
+       return symm(e, first, last, true);
+}
 
+ex symmetrize_cyclic(const ex & e, exvector::const_iterator first, exvector::const_iterator last)
+{
+       // Need at least 2 objects for this operation
+       int num = last - first;
+       if (num < 2)
+               return e;
+
+       // Transform object vector to a list
+       exlist iv_lst;
+       iv_lst.insert(iv_lst.begin(), first, last);
+       lst orig_lst(iv_lst, true);
+       lst new_lst = orig_lst;
+
+       // Loop over all cyclic permutations (the first permutation, which is
+       // the identity, is unrolled)
+       ex sum = e;
+       for (unsigned i=0; i<num-1; i++) {
+               ex perm = new_lst.op(0);
+               new_lst.remove_first().append(perm);
+               sum += e.subs(orig_lst, new_lst);
+       }
+       return sum / num;
+}
+
+/** Symmetrize expression over a list of objects (symbols, indices). */
+ex ex::symmetrize(const lst & l) const
+{
        exvector v;
-       v.reserve(exponent);
-       for (unsigned i=0; i<exponent; ++i)
-               v.push_back(basis);
+       v.reserve(l.nops());
+       for (unsigned i=0; i<l.nops(); i++)
+               v.push_back(l.op(i));
+       return symm(*this, v.begin(), v.end(), false);
+}
 
-       return ncmul(v, true);
+/** Antisymmetrize expression over a list of objects (symbols, indices). */
+ex ex::antisymmetrize(const lst & l) const
+{
+       exvector v;
+       v.reserve(l.nops());
+       for (unsigned i=0; i<l.nops(); i++)
+               v.push_back(l.op(i));
+       return symm(*this, v.begin(), v.end(), true);
+}
+
+/** Symmetrize expression by cyclic permutation over a list of objects
+ *  (symbols, indices). */
+ex ex::symmetrize_cyclic(const lst & l) const
+{
+       exvector v;
+       v.reserve(l.nops());
+       for (unsigned i=0; i<l.nops(); i++)
+               v.push_back(l.op(i));
+       return GiNaC::symmetrize_cyclic(*this, v.begin(), v.end());
 }
 
 /** Force inclusion of functions from initcns_gamma and inifcns_zeta