]> www.ginac.de Git - ginac.git/blobdiff - ginac/ex.cpp
- fixed bug in multiply_lcm() (see paranoia_check9)
[ginac.git] / ginac / ex.cpp
index 6a7a0aa91bdf3b823bf48beda6cbd735948523a6..789911258b43cfd57f7098675ad05cebda23f91a 100644 (file)
@@ -3,7 +3,7 @@
  *  Implementation of GiNaC's light-weight expression handles. */
 
 /*
- *  GiNaC Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany
+ *  GiNaC Copyright (C) 1999-2000 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
@@ -31,9 +31,9 @@
 #include "debugmsg.h"
 #include "utils.h"
 
-#ifndef NO_GINAC_NAMESPACE
+#ifndef NO_NAMESPACE_GINAC
 namespace GiNaC {
-#endif // ndef NO_GINAC_NAMESPACE
+#endif // ndef NO_NAMESPACE_GINAC
 
 //////////
 // default constructor, destructor, copy constructor assignment operator and helpers
@@ -62,7 +62,7 @@ ex::~ex()
     }
 }
 
-ex::ex(ex const & other) : bp(other.bp)
+ex::ex(const ex & other) : bp(other.bp)
 {
     debugmsg("ex copy constructor",LOGLEVEL_CONSTRUCT);
     GINAC_ASSERT(bp!=0);
@@ -70,7 +70,7 @@ ex::ex(ex const & other) : bp(other.bp)
     ++bp->refcount;
 }
 
-ex const & ex::operator=(ex const & other)
+const ex & ex::operator=(const ex & other)
 {
     debugmsg("ex operator=",LOGLEVEL_ASSIGNMENT);
     GINAC_ASSERT(bp!=0);
@@ -95,43 +95,45 @@ ex const & ex::operator=(ex const & other)
 // public
 
 #ifndef INLINE_EX_CONSTRUCTORS
-ex::ex(basic const & other)
+
+ex::ex(const basic & other)
 {
     debugmsg("ex constructor from basic",LOGLEVEL_CONSTRUCT);
     construct_from_basic(other);
 }
-#endif
 
-ex::ex(int const i)
+ex::ex(int i)
 {
     debugmsg("ex constructor from int",LOGLEVEL_CONSTRUCT);
-    construct_from_basic(numeric(i));
+    construct_from_int(i);
 }
 
-ex::ex(unsigned int const i)
+ex::ex(unsigned int i)
 {
     debugmsg("ex constructor from unsigned int",LOGLEVEL_CONSTRUCT);
-    construct_from_basic(numeric(i));
+    construct_from_uint(i);
 }
 
-ex::ex(long const i)
+ex::ex(long i)
 {
     debugmsg("ex constructor from long",LOGLEVEL_CONSTRUCT);
-    construct_from_basic(numeric(i));
+    construct_from_long(i);
 }
 
-ex::ex(unsigned long const i)
+ex::ex(unsigned long i)
 {
     debugmsg("ex constructor from unsigned long",LOGLEVEL_CONSTRUCT);
-    construct_from_basic(numeric(i));
+    construct_from_ulong(i);
 }
 
 ex::ex(double const d)
 {
     debugmsg("ex constructor from double",LOGLEVEL_CONSTRUCT);
-    construct_from_basic(numeric(d));
+    construct_from_double(d);
 }
-    
+
+#endif // ndef INLINE_EX_CONSTRUCTORS
+
 //////////
 // functions overriding virtual functions from bases classes
 //////////
@@ -248,7 +250,7 @@ bool ex::info(unsigned inf) const
     // polynomial^(int) * polynomial^(int) * ... is in normal form
     if (!is_ex_exactly_of_type(*this, mul))
         return false;
-    for (int i=0; i<nops(); i++) {
+    for (unsigned i=0; i<nops(); i++) {
         if (is_ex_exactly_of_type(op(i), power)) {
             if (!op(i).op(1).info(info_flags::integer))
                 return false;
@@ -264,7 +266,7 @@ bool ex::info(unsigned inf) const
     }
 }
 
-int ex::nops() const
+unsigned ex::nops() const
 {
     GINAC_ASSERT(bp!=0);
     return bp->nops();
@@ -276,25 +278,25 @@ ex ex::expand(unsigned options) const
     return bp->expand(options);
 }
 
-bool ex::has(ex const & other) const
+bool ex::has(const ex & other) const
 {
     GINAC_ASSERT(bp!=0);
     return bp->has(other);
 }
 
-int ex::degree(symbol const & s) const
+int ex::degree(const symbol & s) const
 {
     GINAC_ASSERT(bp!=0);
     return bp->degree(s);
 }
 
-int ex::ldegree(symbol const & s) const
+int ex::ldegree(const symbol & s) const
 {
     GINAC_ASSERT(bp!=0);
     return bp->ldegree(s);
 }
 
-ex ex::coeff(symbol const & s, int const n) const
+ex ex::coeff(const symbol & s, int n) const
 {
     GINAC_ASSERT(bp!=0);
     return bp->coeff(s,n);
@@ -320,7 +322,7 @@ ex ex::numer(bool normalize) const
     if (!is_ex_exactly_of_type(n, mul))
         return n;
     ex res = _ex1();
-    for (int i=0; i<n.nops(); i++) {
+    for (unsigned i=0; i<n.nops(); i++) {
         if (!is_ex_exactly_of_type(n.op(i), power) || !n.op(i).op(1).info(info_flags::negint))
             res *= n.op(i);
     }
@@ -347,14 +349,14 @@ ex ex::denom(bool normalize) const
     if (!is_ex_exactly_of_type(n, mul))
         return _ex1();
     ex res = _ex1();
-    for (int i=0; i<n.nops(); i++) {
+    for (unsigned i=0; i<n.nops(); i++) {
         if (is_ex_exactly_of_type(n.op(i), power) && n.op(i).op(1).info(info_flags::negint))
             res *= power(n.op(i), -1);
     }
     return res;
 }
 
-ex ex::collect(symbol const & s) const
+ex ex::collect(const symbol & s) const
 {
     GINAC_ASSERT(bp!=0);
     return bp->collect(s);
@@ -372,13 +374,13 @@ ex ex::evalf(int level) const
     return bp->evalf(level);
 }
 
-ex ex::subs(lst const & ls, lst const & lr) const
+ex ex::subs(const lst & ls, const lst & lr) const
 {
     GINAC_ASSERT(bp!=0);
     return bp->subs(ls,lr);
 }
 
-ex ex::subs(ex const & e) const
+ex ex::subs(const ex & e) const
 {
     GINAC_ASSERT(bp!=0);
     return bp->subs(e);
@@ -390,34 +392,34 @@ exvector ex::get_indices(void) const
     return bp->get_indices();
 }
 
-ex ex::simplify_ncmul(exvector const & v) const
+ex ex::simplify_ncmul(const exvector & v) const
 {
     GINAC_ASSERT(bp!=0);
     return bp->simplify_ncmul(v);
 }
 
-ex ex::operator[](ex const & index) const
+ex ex::operator[](const ex & index) const
 {
     debugmsg("ex operator[ex]",LOGLEVEL_OPERATOR);
     GINAC_ASSERT(bp!=0);
     return (*bp)[index];
 }
 
-ex ex::operator[](int const i) const
+ex ex::operator[](int i) const
 {
     debugmsg("ex operator[int]",LOGLEVEL_OPERATOR);
     GINAC_ASSERT(bp!=0);
     return (*bp)[i];
 }
 
-ex ex::op(int const i) const
+ex ex::op(int i) const
 {
     debugmsg("ex op()",LOGLEVEL_MEMBER_FUNCTION);
     GINAC_ASSERT(bp!=0);
     return bp->op(i);
 }
 
-ex & ex::let_op(int const i)
+ex & ex::let_op(int i)
 {
     debugmsg("ex let_op()",LOGLEVEL_MEMBER_FUNCTION);
     makewriteable();
@@ -426,7 +428,7 @@ ex & ex::let_op(int const i)
 }
 
 #ifndef INLINE_EX_CONSTRUCTORS
-int ex::compare(ex const & other) const
+int ex::compare(const ex & other) const
 {
     GINAC_ASSERT(bp!=0);
     GINAC_ASSERT(other.bp!=0);
@@ -439,7 +441,7 @@ int ex::compare(ex const & other) const
 #endif // ndef INLINE_EX_CONSTRUCTORS
 
 #ifndef INLINE_EX_CONSTRUCTORS
-bool ex::is_equal(ex const & other) const
+bool ex::is_equal(const ex & other) const
 {
     GINAC_ASSERT(bp!=0);
     GINAC_ASSERT(other.bp!=0);
@@ -469,17 +471,17 @@ unsigned ex::gethash(void) const
     return bp->gethash();
 }
 
-ex ex::exadd(ex const & rh) const
+ex ex::exadd(const ex & rh) const
 {
     return (new add(*this,rh))->setflag(status_flags::dynallocated);
 }
 
-ex ex::exmul(ex const & rh) const
+ex ex::exmul(const ex & rh) const
 {
     return (new mul(*this,rh))->setflag(status_flags::dynallocated);
 }
 
-ex ex::exncmul(ex const & rh) const
+ex ex::exncmul(const ex & rh) const
 {
     return (new ncmul(*this,rh))->setflag(status_flags::dynallocated);
 }
@@ -492,20 +494,20 @@ void ex::makewriteable()
     GINAC_ASSERT(bp!=0);
     GINAC_ASSERT(bp->flags & status_flags::dynallocated);
     if (bp->refcount > 1) {
-        basic * bp2=bp->duplicate();
+        basic * bp2 = bp->duplicate();
         ++bp2->refcount;
         bp2->setflag(status_flags::dynallocated);
         --bp->refcount;
-        bp=bp2;
+        bp = bp2;
     }
     GINAC_ASSERT(bp->refcount == 1);
-}    
+}
 
-void ex::construct_from_basic(basic const & other)
+void ex::construct_from_basic(const basic & other)
 {
     if ((other.flags & status_flags::evaluated)==0) {
         // cf. copy constructor
-        ex const & tmpex = other.eval(1); // evaluate only one (top) level
+        const ex & tmpex = other.eval(1); // evaluate only one (top) level
         bp = tmpex.bp;
         GINAC_ASSERT(bp!=0);
         GINAC_ASSERT(bp->flags & status_flags::dynallocated);
@@ -515,9 +517,9 @@ void ex::construct_from_basic(basic const & other)
         }
     } else {
         if (other.flags & status_flags::dynallocated) {
-            bp=&const_cast<basic &>(other);
+            bp = &const_cast<basic &>(other);
         } else {
-            bp=other.duplicate();
+            bp = other.duplicate();
             bp->setflag(status_flags::dynallocated);
         }
         GINAC_ASSERT(bp!=0);
@@ -528,6 +530,143 @@ void ex::construct_from_basic(basic const & other)
     GINAC_ASSERT(bp->flags & status_flags::dynallocated);
 }
 
+void ex::construct_from_int(int i)
+{
+    switch (i) {  // some tiny efficiency-hack
+    case -2:
+        bp = _ex_2().bp;
+        ++bp->refcount;
+        break;
+    case -1:
+        bp = _ex_1().bp;
+        ++bp->refcount;
+        break;
+    case 0:
+        bp = _ex0().bp;
+        ++bp->refcount;
+        break;
+    case 1:
+        bp = _ex1().bp;
+        ++bp->refcount;
+        break;
+    case 2:
+        bp = _ex2().bp;
+        ++bp->refcount;
+        break;
+    default:
+        bp = new numeric(i);
+        bp->setflag(status_flags::dynallocated);
+        ++bp->refcount;
+        GINAC_ASSERT((bp->flags) & status_flags::dynallocated);
+        GINAC_ASSERT(bp->refcount=1);
+    }
+}
+    
+void ex::construct_from_uint(unsigned int i)
+{
+    switch (i) {  // some tiny efficiency-hack
+    case -2:
+        bp = _ex_2().bp;
+        ++bp->refcount;
+        break;
+    case -1:
+        bp = _ex_1().bp;
+        ++bp->refcount;
+        break;
+    case 0:
+        bp = _ex0().bp;
+        ++bp->refcount;
+        break;
+    case 1:
+        bp = _ex1().bp;
+        ++bp->refcount;
+        break;
+    case 2:
+        bp = _ex2().bp;
+        ++bp->refcount;
+        break;
+    default:
+        bp = new numeric(i);
+        bp->setflag(status_flags::dynallocated);
+        ++bp->refcount;
+        GINAC_ASSERT((bp->flags) & status_flags::dynallocated);
+        GINAC_ASSERT(bp->refcount=1);
+    }
+}
+    
+void ex::construct_from_long(long i)
+{
+    switch (i) {  // some tiny efficiency-hack
+    case -2:
+        bp = _ex_2().bp;
+        ++bp->refcount;
+        break;
+    case -1:
+        bp = _ex_1().bp;
+        ++bp->refcount;
+        break;
+    case 0:
+        bp = _ex0().bp;
+        ++bp->refcount;
+        break;
+    case 1:
+        bp = _ex1().bp;
+        ++bp->refcount;
+        break;
+    case 2:
+        bp = _ex2().bp;
+        ++bp->refcount;
+        break;
+    default:
+        bp = new numeric(i);
+        bp->setflag(status_flags::dynallocated);
+        ++bp->refcount;
+        GINAC_ASSERT((bp->flags) & status_flags::dynallocated);
+        GINAC_ASSERT(bp->refcount=1);
+    }
+}
+    
+void ex::construct_from_ulong(unsigned long i)
+{
+    switch (i) {  // some tiny efficiency-hack
+    case -2:
+        bp = _ex_2().bp;
+        ++bp->refcount;
+        break;
+    case -1:
+        bp = _ex_1().bp;
+        ++bp->refcount;
+        break;
+    case 0:
+        bp = _ex0().bp;
+        ++bp->refcount;
+        break;
+    case 1:
+        bp = _ex1().bp;
+        ++bp->refcount;
+        break;
+    case 2:
+        bp = _ex2().bp;
+        ++bp->refcount;
+        break;
+    default:
+        bp = new numeric(i);
+        bp->setflag(status_flags::dynallocated);
+        ++bp->refcount;
+        GINAC_ASSERT((bp->flags) & status_flags::dynallocated);
+        GINAC_ASSERT(bp->refcount=1);
+    }
+}
+    
+void ex::construct_from_double(double d)
+{
+    bp = new numeric(d);
+    bp->setflag(status_flags::dynallocated);
+    ++bp->refcount;
+    GINAC_ASSERT((bp->flags) & status_flags::dynallocated);
+    GINAC_ASSERT(bp->refcount=1);
+}
+    
 //////////
 // static member variables
 //////////
@@ -547,6 +686,6 @@ void ex::construct_from_basic(basic const & other)
 // none
 
 
-#ifndef NO_GINAC_NAMESPACE
+#ifndef NO_NAMESPACE_GINAC
 } // namespace GiNaC
-#endif // ndef NO_GINAC_NAMESPACE
+#endif // ndef NO_NAMESPACE_GINAC