]> www.ginac.de Git - ginac.git/blobdiff - ginac/inifcns.cpp
- permutation_sign() and shaker_sort() are no longer inlined
[ginac.git] / ginac / inifcns.cpp
index 0daa97989abf8260202a0db99cfb572f576379ec..03acbc7f2f42d4c221905652a39b21cdd3e041e2 100644 (file)
@@ -82,7 +82,8 @@ static ex csgn_eval(const ex & arg)
        if (is_ex_exactly_of_type(arg, numeric))
                return csgn(ex_to_numeric(arg));
        
-       else if (is_ex_exactly_of_type(arg, mul)) {
+       else if (is_ex_of_type(arg, mul) &&
+                is_ex_of_type(arg.op(arg.nops()-1),numeric)) {
                numeric oc = ex_to_numeric(arg.op(arg.nops()-1));
                if (oc.is_real()) {
                        if (oc > 0)
@@ -101,7 +102,7 @@ static ex csgn_eval(const ex & arg)
                                return -csgn(I*arg/oc).hold();
                }
        }
-   
+       
        return csgn(arg).hold();
 }
 
@@ -509,19 +510,74 @@ ex lsolve(const ex &eqns, const ex &symbols)
 }
 
 /** non-commutative power. */
-ex ncpower(const ex &basis, unsigned exponent)
+ex ncpow(const ex & basis, unsigned exponent)
 {
-       if (exponent==0) {
+       if (exponent == 0)
                return _ex1();
-       }
 
        exvector v;
        v.reserve(exponent);
-       for (unsigned i=0; i<exponent; ++i) {
+       for (unsigned i=0; i<exponent; ++i)
                v.push_back(basis);
+
+       return ncmul(v, true);
+}
+
+// Symmetrize/antisymmetrize over a vector of objects
+static ex symm(const ex & e, exvector::const_iterator first, exvector::const_iterator last, bool asymmetric)
+{
+       // Need at least 2 objects for this operation
+       int num = last - first;
+       if (num < 2)
+               return e;
+
+       // Sort object vector, transform it into a list, and make a copy so we
+       // will know which objects get substituted for which
+       exlist iv_lst;
+       iv_lst.insert(iv_lst.begin(), first, last);
+       shaker_sort(iv_lst.begin(), iv_lst.end(), ex_is_less());
+       lst orig_lst(iv_lst);
+
+       // Loop over all permutations (the first permutation, which is the
+       // identity, is unrolled)
+       ex sum = e;
+       while (next_permutation(iv_lst.begin(), iv_lst.end(), ex_is_less())) {
+               ex term = e.subs(orig_lst, lst(iv_lst));
+               if (asymmetric) {
+                       exlist test_lst = iv_lst;
+                       term *= permutation_sign(test_lst.begin(), test_lst.end(), ex_is_less());
+               }
+               sum += term;
        }
+       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(const ex & e, const lst & l)
+{
+       exvector v;
+       v.reserve(l.nops());
+       for (unsigned i=0; i<l.nops(); i++)
+               v.push_back(l.op(i));
+       return symm(e, v.begin(), v.end(), false);
+}
 
-       return ncmul(v,1);
+ex antisymmetrize(const ex & e, const lst & l)
+{
+       exvector v;
+       v.reserve(l.nops());
+       for (unsigned i=0; i<l.nops(); i++)
+               v.push_back(l.op(i));
+       return symm(e, v.begin(), v.end(), true);
 }
 
 /** Force inclusion of functions from initcns_gamma and inifcns_zeta