]> www.ginac.de Git - ginac.git/blobdiff - ginac/basic.cpp
- color/clifford objects have representation label to distinguish elements
[ginac.git] / ginac / basic.cpp
index 1b3882590b708f7ea0e7fdb4fd0cfd3eaae17a95..5b66a04d14bbf1fcb32be4008f294d903e8a2d3d 100644 (file)
@@ -21,7 +21,6 @@
  */
 
 #include <iostream>
-#include <typeinfo>
 #include <stdexcept>
 
 #include "basic.h"
 #include "symbol.h"
 #include "lst.h"
 #include "ncmul.h"
-#include "idx.h"
-#include "indexed.h"
-#include "tensor.h"
-#include "function.h"
+#include "print.h"
 #include "archive.h"
 #include "utils.h"
 #include "debugmsg.h"
@@ -113,46 +109,25 @@ void basic::archive(archive_node &n) const
 
 // public
 
-/** Output to ostream formatted as parsable (as in ginsh) input.
- *  Generally, superfluous parenthesis should be avoided as far as possible. */
-void basic::print(std::ostream & os, unsigned upper_precedence) const
+/** Output to stream.
+ *  @param c print context object that describes the output formatting
+ *  @param level value that is used to identify the precedence or indentation
+ *               level for placing parentheses and formatting */
+void basic::print(const print_context & c, unsigned level) const
 {
-       debugmsg("basic print",LOGLEVEL_PRINT);
-       os << "[" << class_name() << " object]";
-}
+       debugmsg("basic print", LOGLEVEL_PRINT);
 
-/** Output to ostream in ugly raw format, so brave developers can have a look
- *  at the underlying structure. */
-void basic::printraw(std::ostream & os) const
-{
-       debugmsg("basic printraw",LOGLEVEL_PRINT);
-       os << "[" << class_name() << " object]";
-}
+       if (is_of_type(c, print_tree)) {
 
-/** Output to ostream formatted in tree- (indented-) form, so developers can
- *  have a look at the underlying structure. */
-void basic::printtree(std::ostream & os, unsigned indent) const
-{
-       debugmsg("basic printtree",LOGLEVEL_PRINT);
-       os << std::string(indent,' ') << "type=" << class_name()
-          << ", hash=" << hashvalue
-          << " (0x" << std::hex << hashvalue << std::dec << ")"
-          << ", flags=" << flags
-          << ", nops=" << nops() << std::endl;
-       for (unsigned i=0; i<nops(); ++i) {
-               op(i).printtree(os,indent+delta_indent);
-       }
-}
+               c.s << std::string(level, ' ') << class_name()
+                   << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
+                   << ", nops=" << nops()
+                   << std::endl;
+               for (unsigned i=0; i<nops(); ++i)
+                       op(i).print(c, level + static_cast<const print_tree &>(c).delta_indent);
 
-/** Output to ostream formatted as C-source.
- *
- *  @param os a stream for output
- *  @param type variable type (one of the csrc_types)
- *  @param upper_precedence operator precedence of caller
- *  @see ex::printcsrc */
-void basic::printcsrc(std::ostream & os, unsigned type, unsigned upper_precedence) const
-{
-       debugmsg("basic print csrc", LOGLEVEL_PRINT);
+       } else
+               c.s << "[" << class_name() << " object]";
 }
 
 /** Little wrapper arount print to be called within a debugger.
@@ -160,7 +135,7 @@ void basic::printcsrc(std::ostream & os, unsigned type, unsigned upper_precedenc
  *  debugger because it might not know what cout is.  This method can be
  *  invoked with no argument and it will simply print to stdout.
  *
- *  @see basic::print*/
+ *  @see basic::print */
 void basic::dbgprint(void) const
 {
        this->print(std::cerr);
@@ -173,7 +148,7 @@ void basic::dbgprint(void) const
  *  @see basic::printtree */
 void basic::dbgprinttree(void) const
 {
-       this->printtree(std::cerr,0);
+       this->print(print_tree(std::cerr));
 }
 
 /** Create a new copy of this on the heap.  One can think of this as simulating
@@ -245,33 +220,106 @@ bool basic::has(const ex & other) const
        return false;
 }
 
-/** Return degree of highest power in symbol s. */
+/** Return degree of highest power in object s. */
 int basic::degree(const ex & s) const
 {
        return 0;
 }
 
-/** Return degree of lowest power in symbol s. */
+/** Return degree of lowest power in object s. */
 int basic::ldegree(const ex & s) const
 {
        return 0;
 }
 
-/** Return coefficient of degree n in symbol s. */
+/** Return coefficient of degree n in object s. */
 ex basic::coeff(const ex & s, int n) const
 {
        return n==0 ? *this : _ex0();
 }
 
-/** Sort expression in terms of powers of some symbol.
- *  @param s symbol to sort in. */
-ex basic::collect(const ex & s) const
+/** Sort expression in terms of powers of some object(s).
+ *  @param s object(s) to sort in
+ *  @param distributed recursive or distributed form (only used when s is a list) */
+ex basic::collect(const ex & s, bool distributed) const
 {
        ex x;
-       for (int n=this->ldegree(s); n<=this->degree(s); n++)
-               x += this->coeff(s,n)*power(s,n);
+       if (is_ex_of_type(s, lst)) {
+
+               // List of objects specified
+               if (s.nops() == 1)
+                       return collect(s.op(0));
+
+               else if (distributed) {
+
+                       // Get lower/upper degree of all symbols in list
+                       int num = s.nops();
+                       struct sym_info {
+                               ex sym;
+                               int ldeg, deg;
+                               int cnt;  // current degree, 'counter'
+                               ex coeff; // coefficient for degree 'cnt'
+                       };
+                       sym_info *si = new sym_info[num];
+                       ex c = *this;
+                       for (int i=0; i<num; i++) {
+                               si[i].sym = s.op(i);
+                               si[i].ldeg = si[i].cnt = this->ldegree(si[i].sym);
+                               si[i].deg = this->degree(si[i].sym);
+                               c = si[i].coeff = c.coeff(si[i].sym, si[i].cnt);
+                       }
+
+                       while (true) {
+
+                               // Calculate coeff*x1^c1*...*xn^cn
+                               ex y = _ex1();
+                               for (int i=0; i<num; i++) {
+                                       int cnt = si[i].cnt;
+                                       y *= power(si[i].sym, cnt);
+                               }
+                               x += y * si[num - 1].coeff;
+
+                               // Increment counters
+                               int n = num - 1;
+                               while (true) {
+                                       si[n].cnt++;
+                                       if (si[n].cnt <= si[n].deg) {
+                                               // Update coefficients
+                                               ex c;
+                                               if (n == 0)
+                                                       c = *this;
+                                               else
+                                                       c = si[n - 1].coeff;
+                                               for (int i=n; i<num; i++)
+                                                       c = si[i].coeff = c.coeff(si[i].sym, si[i].cnt);
+                                               break;
+                                       }
+                                       if (n == 0)
+                                               goto done;
+                                       si[n].cnt = si[n].ldeg;
+                                       n--;
+                               }
+                       }
+
+done:          delete[] si;
+
+               } else {
+
+                       // Recursive form
+                       x = *this;
+                       for (int n=s.nops()-1; n>=0; n--)
+                               x = x.collect(s[n]);
+               }
+
+       } else {
+
+               // Only one object specified
+               for (int n=this->ldegree(s); n<=this->degree(s); ++n)
+                       x += this->coeff(s,n)*power(s,n);
+       }
        
-       return x;
+       // correct for lost fractional arguments and return
+       return x + (*this - x).expand();
 }
 
 /** Perform automatic non-interruptive symbolic evaluation on expression. */
@@ -341,10 +389,17 @@ bool basic::contract_with(exvector::iterator self, exvector::iterator other, exv
        return false;
 }
 
-/** Substitute a set of symbols by arbitrary expressions. The ex returned
+/** Substitute a set of objects by arbitrary expressions. The ex returned
  *  will already be evaluated. */
 ex basic::subs(const lst & ls, const lst & lr) const
 {
+       GINAC_ASSERT(ls.nops() == lr.nops());
+
+       for (unsigned i=0; i<ls.nops(); i++) {
+               if (is_equal(*ls.op(i).bp))
+                       return lr.op(i);
+       }
+
        return *this;
 }
 
@@ -466,11 +521,11 @@ ex basic::expand(unsigned options) const
 
 // public
 
-/** Substitute objects (symbols, indices, tensors, functions, indexed) in
- *  expression and return the result as a new expression.  There are two
- *  valid types of replacement arguments: 1) a relational like object==ex
- *  and 2) a list of relationals lst(object1==ex1,object2==ex2,...), which
- *  is converted to subs(lst(object1,object2,...),lst(ex1,ex2,...)). */
+/** Substitute objects in an expression (syntactic substitution) and return
+ *  the result as a new expression.  There are two valid types of
+ *  replacement arguments: 1) a relational like object==ex and 2) a list of
+ *  relationals lst(object1==ex1,object2==ex2,...), which is converted to
+ *  subs(lst(object1,object2,...),lst(ex1,ex2,...)). */
 ex basic::subs(const ex & e) const
 {
        if (e.info(info_flags::relation_equal)) {
@@ -482,20 +537,14 @@ ex basic::subs(const ex & e) const
        lst ls;
        lst lr;
        for (unsigned i=0; i<e.nops(); i++) {
-               if (!e.op(i).info(info_flags::relation_equal)) {
+               ex r = e.op(i);
+               if (!r.info(info_flags::relation_equal)) {
                        throw(std::invalid_argument("basic::subs(ex): argument must be a list or equations"));
                }
-               ex s = e.op(i).op(0);
-               ex r = e.op(i).op(1);
-               if (!is_ex_of_type(s, symbol) && !is_ex_of_type(s, idx) &&
-                   !is_ex_of_type(s, tensor) && !is_ex_of_type(s, function) &&
-                       !is_ex_of_type(s, indexed)) {
-                       throw(std::invalid_argument("basic::subs(ex): lhs must be a symbol, idx, tensor, function or indexed"));
-               }
-               ls.append(s);
-               lr.append(r);
+               ls.append(r.op(0));
+               lr.append(r.op(1));
        }
-       return subs(ls,lr);
+       return subs(ls, lr);
 }
 
 /** Compare objects to establish canonical ordering.
@@ -515,18 +564,18 @@ int basic::compare(const basic & other) const
        if (typeid_this<typeid_other) {
 //             std::cout << "hash collision, different types: " 
 //                       << *this << " and " << other << std::endl;
-//             this->printraw(std::cout);
+//             this->print(print_tree(std::cout));
 //             std::cout << " and ";
-//             other.printraw(std::cout);
+//             other.print(print_tree(std::cout));
 //             std::cout << std::endl;
                return -1;
        }
        if (typeid_this>typeid_other) {
 //             std::cout << "hash collision, different types: " 
 //                       << *this << " and " << other << std::endl;
-//             this->printraw(std::cout);
+//             this->print(print_tree(std::cout));
 //             std::cout << " and ";
-//             other.printraw(std::cout);
+//             other.print(print_tree(std::cout));
 //             std::cout << std::endl;
                return 1;
        }
@@ -537,9 +586,9 @@ int basic::compare(const basic & other) const
 //     if ((cmpval!=0) && (hash_this<0x80000000U)) {
 //             std::cout << "hash collision, same type: " 
 //                       << *this << " and " << other << std::endl;
-//             this->printraw(std::cout);
+//             this->print(print_tree(std::cout));
 //             std::cout << " and ";
-//             other.printraw(std::cout);
+//             other.print(print_tree(std::cout));
 //             std::cout << std::endl;
 //     }
 //     return cmpval;
@@ -590,7 +639,6 @@ void basic::ensure_if_modifiable(void) const
 // protected
 
 unsigned basic::precedence = 70;
-unsigned basic::delta_indent = 4;
 
 //////////
 // global variables