*/
#include <iostream>
-#include <typeinfo>
#include <stdexcept>
#include "basic.h"
#include "symbol.h"
#include "lst.h"
#include "ncmul.h"
+#include "print.h"
#include "archive.h"
#include "utils.h"
#include "debugmsg.h"
// 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.
* 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);
* @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
ex basic::collect(const ex & s) const
{
ex x;
- for (int n=this->ldegree(s); n<=this->degree(s); n++)
+ 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. */
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;
}
// public
-/** Substitute symbols in expression and return the result as a new expression.
- * There are two valid types of replacement arguments: 1) a relational like
- * symbol==ex and 2) a list of relationals lst(symbol1==ex1,symbol2==ex2,...),
- * which is converted to subs(lst(symbol1,symbol2,...),lst(ex1,ex2,...)).
- * In addition, an object of class idx can be used instead of a symbol. */
+/** 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)) {
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"));
}
- if (!e.op(i).op(0).info(info_flags::symbol)) {
- if (!e.op(i).op(0).info(info_flags::idx)) {
- throw(std::invalid_argument("basic::subs(ex): lhs must be a symbol or an idx"));
- }
- }
- ls.append(e.op(i).op(0));
- lr.append(e.op(i).op(1));
+ ls.append(r.op(0));
+ lr.append(r.op(1));
}
- return subs(ls,lr);
+ return subs(ls, lr);
}
/** Compare objects to establish canonical ordering.
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;
}
// 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;
// protected
unsigned basic::precedence = 70;
-unsigned basic::delta_indent = 4;
//////////
// global variables