]> www.ginac.de Git - ginac.git/blobdiff - ginac/power.cpp
- fix LaTeX-output bug reported by Stefan, remove obsolete has(matrix,ex).
[ginac.git] / ginac / power.cpp
index 6b8c184ef7a1e031bd4347771aa5b370120d91dc..15fed248b889d73b24fd3954ed01c6f659152c3e 100644 (file)
@@ -71,6 +71,8 @@ power::power(const ex & lh, const ex & rh) : basic(TINFO_power), basis(lh), expo
        GINAC_ASSERT(basis.return_type()==return_types::commutative);
 }
 
+/** Ctor from an ex and a bare numeric.  This is somewhat more efficient than
+ *  the normal ctor from two ex whenever it can be used. */
 power::power(const ex & lh, const numeric & rh) : basic(TINFO_power), basis(lh), exponent(rh)
 {
        debugmsg("power ctor from ex,numeric",LOGLEVEL_CONSTRUCT);
@@ -142,7 +144,7 @@ void power::print(const print_context & c, unsigned level) const
                 && (is_ex_exactly_of_type(basis, symbol) || is_ex_exactly_of_type(basis, constant))) {
                        int exp = ex_to_numeric(exponent).to_int();
                        if (exp > 0)
-                               c.s << "(";
+                               c.s << '(';
                        else {
                                exp = -exp;
                                if (is_of_type(c, print_csrc_cl_N))
@@ -151,7 +153,7 @@ void power::print(const print_context & c, unsigned level) const
                                        c.s << "1.0/(";
                        }
                        print_sym_pow(c, ex_to_symbol(basis), exp);
-                       c.s << ")";
+                       c.s << ')';
 
                // <expr>^-1 is printed as "1.0/<expr>" or with the recip() function of CLN
                } else if (exponent.compare(_num_1()) == 0) {
@@ -160,7 +162,7 @@ void power::print(const print_context & c, unsigned level) const
                        else
                                c.s << "1.0/(";
                        basis.print(c);
-                       c.s << ")";
+                       c.s << ')';
 
                // Otherwise, use the pow() or expt() (CLN) functions
                } else {
@@ -169,9 +171,9 @@ void power::print(const print_context & c, unsigned level) const
                        else
                                c.s << "pow(";
                        basis.print(c);
-                       c.s << ",";
+                       c.s << ',';
                        exponent.print(c);
-                       c.s << ")";
+                       c.s << ')';
                }
 
        } else {
@@ -183,24 +185,28 @@ void power::print(const print_context & c, unsigned level) const
                                c.s << "sqrt(";
                        basis.print(c);
                        if (is_of_type(c, print_latex))
-                               c.s << "}";
+                               c.s << '}';
                        else
-                               c.s << ")";
+                               c.s << ')';
                } else {
-                       if (precedence <= level) {
+                       if (precedence() <= level) {
                                if (is_of_type(c, print_latex))
                                        c.s << "{(";
                                else
                                        c.s << "(";
                        }
-                       basis.print(c, precedence);
-                       c.s << "^";
-                       exponent.print(c, precedence);
-                       if (precedence <= level) {
+                       basis.print(c, precedence());
+                       c.s << '^';
+                       if (is_of_type(c, print_latex))
+                               c.s << '{';
+                       exponent.print(c, precedence());
+                       if (is_of_type(c, print_latex))
+                               c.s << '}';
+                       if (precedence() <= level) {
                                if (is_of_type(c, print_latex))
                                        c.s << ")}";
                                else
-                                       c.s << ")";
+                                       c.s << ')';
                        }
                }
        }
@@ -470,17 +476,16 @@ ex power::evalf(int level) const
        return power(ebasis,eexponent);
 }
 
-ex power::subs(const lst & ls, const lst & lr) const
+ex power::subs(const lst & ls, const lst & lr, bool no_pattern) const
 {
-       const ex & subsed_basis=basis.subs(ls,lr);
-       const ex & subsed_exponent=exponent.subs(ls,lr);
+       const ex &subsed_basis = basis.subs(ls, lr, no_pattern);
+       const ex &subsed_exponent = exponent.subs(ls, lr, no_pattern);
 
-       if (are_ex_trivially_equal(basis,subsed_basis)&&
-               are_ex_trivially_equal(exponent,subsed_exponent)) {
-               return inherited::subs(ls, lr);
-       }
-       
-       return power(subsed_basis, subsed_exponent);
+       if (are_ex_trivially_equal(basis, subsed_basis)
+        && are_ex_trivially_equal(exponent, subsed_exponent))
+               return basic::subs(ls, lr, no_pattern);
+       else
+               return ex(power(subsed_basis, subsed_exponent)).bp->basic::subs(ls, lr, no_pattern);
 }
 
 ex power::simplify_ncmul(const exvector & v) const
@@ -671,9 +676,7 @@ ex power::expand_add(const add & a, int n) const
                        cout << "k_cum[" << i << "]=" << k_cum[i] << endl;
                        cout << "upper_limit[" << i << "]=" << upper_limit[i] << endl;
                }
-               for (exvector::const_iterator cit=term.begin(); cit!=term.end(); ++cit) {
-                       cout << *cit << endl;
-               }
+               for_each(term.begin(), term.end(), ostream_iterator<ex>(cout, "\n"));
                cout << "end term" << endl;
                */
                
@@ -830,14 +833,6 @@ ex power::expand_noncommutative(const ex & basis, const numeric & exponent,
 }
 */
 
-//////////
-// static member variables
-//////////
-
-// protected
-
-unsigned power::precedence = 60;
-
 // helper function
 
 ex sqrt(const ex & a)