]> www.ginac.de Git - ginac.git/blobdiff - ginac/basic.cpp
- prepared for 1.0.13 release
[ginac.git] / ginac / basic.cpp
index 8baa3536a83d296e78facabc7436d4cdefa46a2e..f625e92b342a39e3538fa397649d5fc836e7e237 100644 (file)
@@ -3,7 +3,7 @@
  *  Implementation of GiNaC's ABC. */
 
 /*
- *  GiNaC Copyright (C) 1999-2001 Johannes Gutenberg University Mainz, Germany
+ *  GiNaC Copyright (C) 1999-2003 Johannes Gutenberg University Mainz, Germany
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
 #include "lst.h"
 #include "ncmul.h"
 #include "relational.h"
+#include "wildcard.h"
 #include "print.h"
 #include "archive.h"
 #include "utils.h"
-#include "debugmsg.h"
 
 namespace GiNaC {
 
 GINAC_IMPLEMENT_REGISTERED_CLASS_NO_CTORS(basic, void)
 
 //////////
-// default ctor, dtor, copy ctor assignment operator and helpers
+// default ctor, dtor, copy ctor, assignment operator and helpers
 //////////
 
 // public
 
 basic::basic(const basic & other) : tinfo_key(TINFO_basic), flags(0), refcount(0)
 {
-       debugmsg("basic copy ctor", LOGLEVEL_CONSTRUCT);
        copy(other);
 }
 
 const basic & basic::operator=(const basic & other)
 {
-       debugmsg("basic operator=", LOGLEVEL_ASSIGNMENT);
        if (this != &other) {
                destroy(true);
                copy(other);
@@ -82,8 +80,6 @@ const basic & basic::operator=(const basic & other)
 /** Construct object from archive_node. */
 basic::basic(const archive_node &n, const lst &sym_lst) : flags(0), refcount(0)
 {
-       debugmsg("basic ctor from archive_node", LOGLEVEL_CONSTRUCT);
-
        // Reconstruct tinfo_key from class name
        std::string class_name;
        if (n.find_string("class", class_name))
@@ -113,8 +109,6 @@ void basic::archive(archive_node &n) const
  *               level for placing parentheses and formatting */
 void basic::print(const print_context & c, unsigned level) const
 {
-       debugmsg("basic print", LOGLEVEL_PRINT);
-
        if (is_of_type(c, print_tree)) {
 
                c.s << std::string(level, ' ') << class_name()
@@ -160,7 +154,6 @@ unsigned basic::precedence(void) const
  *  construction of an ex from a basic. */
 basic * basic::duplicate() const
 {
-       debugmsg("basic duplicate",LOGLEVEL_DUPLICATE);
        return new basic(*this);
 }
 
@@ -196,9 +189,9 @@ ex & basic::let_op(int i)
 
 ex basic::operator[](const ex & index) const
 {
-       if (is_exactly_of_type(*index.bp,numeric))
-               return op(static_cast<const numeric &>(*index.bp).to_int());
-       
+       if (is_ex_exactly_of_type(index,numeric))
+               return op(ex_to<numeric>(index).to_int());
+
        throw(std::invalid_argument("non-numeric indices not supported by this type"));
 }
 
@@ -213,9 +206,8 @@ ex basic::operator[](int i) const
  *  but e.has(x+y) is false. */
 bool basic::has(const ex & pattern) const
 {
-       GINAC_ASSERT(pattern.bp!=0);
        lst repl_lst;
-       if (match(*pattern.bp, repl_lst))
+       if (match(pattern, repl_lst))
                return true;
        for (unsigned i=0; i<nops(); i++)
                if (op(i).has(pattern))
@@ -244,19 +236,22 @@ ex basic::map(map_function & f) const
 /** Return degree of highest power in object s. */
 int basic::degree(const ex & s) const
 {
-       return 0;
+       return is_equal(ex_to<basic>(s)) ? 1 : 0;
 }
 
 /** Return degree of lowest power in object s. */
 int basic::ldegree(const ex & s) const
 {
-       return 0;
+       return is_equal(ex_to<basic>(s)) ? 1 : 0;
 }
 
 /** Return coefficient of degree n in object s. */
 ex basic::coeff(const ex & s, int n) const
 {
-       return n==0 ? *this : _ex0();
+       if (is_equal(ex_to<basic>(s)))
+               return n==1 ? _ex1 : _ex0;
+       else
+               return n==0 ? *this : _ex0;
 }
 
 /** Sort expanded expression in terms of powers of some object(s).
@@ -295,7 +290,7 @@ ex basic::collect(const ex & s, bool distributed) const
                        while (true) {
 
                                // Calculate coeff*x1^c1*...*xn^cn
-                               ex y = _ex1();
+                               ex y = _ex1;
                                for (int i=0; i<num; i++) {
                                        int cnt = si[i].cnt;
                                        y *= power(si[i].sym, cnt);
@@ -471,7 +466,7 @@ bool basic::match(const ex & pattern, lst & repl_lst) const
                // be the same expression)
                for (unsigned i=0; i<repl_lst.nops(); i++) {
                        if (repl_lst.op(i).op(0).is_equal(pattern))
-                               return is_equal(*repl_lst.op(i).op(1).bp);
+                               return is_equal(ex_to<basic>(repl_lst.op(i).op(1)));
                }
                repl_lst.append(pattern == *this);
                return true;
@@ -479,7 +474,7 @@ bool basic::match(const ex & pattern, lst & repl_lst) const
        } else {
 
                // Expression must be of the same type as the pattern
-               if (tinfo() != pattern.bp->tinfo())
+               if (tinfo() != ex_to<basic>(pattern).tinfo())
                        return false;
 
                // Number of subexpressions must match
@@ -489,10 +484,10 @@ bool basic::match(const ex & pattern, lst & repl_lst) const
                // No subexpressions? Then just compare the objects (there can't be
                // wildcards in the pattern)
                if (nops() == 0)
-                       return is_equal_same_type(*pattern.bp);
+                       return is_equal_same_type(ex_to<basic>(pattern));
 
                // Check whether attributes that are not subexpressions match
-               if (!match_same_type(*pattern.bp))
+               if (!match_same_type(ex_to<basic>(pattern)))
                        return false;
 
                // Otherwise the subexpressions must match one-to-one
@@ -513,14 +508,14 @@ ex basic::subs(const lst & ls, const lst & lr, bool no_pattern) const
 
        if (no_pattern) {
                for (unsigned i=0; i<ls.nops(); i++) {
-                       if (is_equal(*ls.op(i).bp))
+                       if (is_equal(ex_to<basic>(ls.op(i))))
                                return lr.op(i);
                }
        } else {
                for (unsigned i=0; i<ls.nops(); i++) {
                        lst repl_lst;
-                       if (match(*ls.op(i).bp, repl_lst))
-                               return lr.op(i).bp->subs(repl_lst, true); // avoid infinite recursion when re-substituting the wildcards
+                       if (match(ex_to<basic>(ls.op(i)), repl_lst))
+                               return lr.op(i).subs(repl_lst, true); // avoid infinite recursion when re-substituting the wildcards
                }
        }
 
@@ -580,7 +575,7 @@ struct derivative_map_function : public map_function {
 ex basic::derivative(const symbol & s) const
 {
        if (nops() == 0)
-               return _ex0();
+               return _ex0;
        else {
                derivative_map_function map_derivative(s);
                return map(map_derivative);
@@ -675,7 +670,7 @@ ex basic::expand(unsigned options) const
                return (options == 0) ? setflag(status_flags::expanded) : *this;
        else {
                expand_map_function map_expand(options);
-               return map(map_expand).bp->setflag(options == 0 ? status_flags::expanded : 0);
+               return ex_to<basic>(map(map_expand)).setflag(options == 0 ? status_flags::expanded : 0);
        }
 }