* 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);
/** 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))
n.add_string("class", class_name());
}
-//////////
-// functions overriding virtual functions from bases classes
-//////////
-
-// none
-
//////////
// new virtual functions which can be overridden by derived classes
//////////
* 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()
* construction of an ex from a basic. */
basic * basic::duplicate() const
{
- debugmsg("basic duplicate",LOGLEVEL_DUPLICATE);
return new basic(*this);
}
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"));
}
* 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))
/** 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).
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);
return x + (*this - x).expand();
}
-/** Perform automatic non-interruptive symbolic evaluation on expression. */
+/** Perform automatic non-interruptive term rewriting rules. */
ex basic::eval(int level) const
{
// There is nothing to do for basic objects:
// 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;
} 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
// 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
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
}
}
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);
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);
}
}