On 9/8/07, Joel B. Mohler <[EMAIL PROTECTED]> wrote:
> I'm really impressed with the symbolic module.  It's remarkably robust and
> useful -- and feels much better than maxima by itself ever did to me.

I have the same experience and find it quite odd, since
so much of the real work is being done by Maxima after
all.  In any case, I'm glad it is useful.  Thanks to Bobby Moretti!

> My question is this:  I have a large polynomial-ish expression which I'm
> outputting to latex.  I'd rather have it printed with the constant term first
> in increasing order of degree rather than now it prints in decreasing degree
> order.   Is this customizable?  I can't find anything about order in
> calculus.py.

This is definitely not implemented.   If
  sage: f = 2/3 + x + 2*x^2 + 3*x^5 - (3/2)*x^7
then here's how
  sage: latex(f)
works.
  (1) It instead does latex on g = f.simplify()
  (2) latex(g) calls g._latex_()
  (3) g._latex_() goes through the operands and latexs
     each in turn and puts it all together.  Type
     f._latex_?? to see the code that is run.

One way to make the change you want would be
to define a function that, given a polynomial-ish
object, returns a new symbolic object h with the
terms in the order you want, then modify the
_latex_ method to have an option to not simplify
before texing.

The coeffs funtion gives the coefficients of a
polynomial in ascending order of degree:
   sage: f = 2/3 + x + 2*x^2 + 3*x^5 - (pi/2)*x^7
   sage: f.coeffs(x)
   [[2/3, 0], [1, 1], [2, 2], [3, 5], [-1*pi/2, 7]]
so what you'd have to do is just construct the polynomial
with the terms in that order, then call
_latex_ without simplifying.

sage: f = (pi/2)*x^7 + x + 2*x^2 + 3*x^5 - 2/3; f
pi*x^7/2 + 3*x^5 + 2*x^2 + x - 2/3
sage: c = f.coeffs(x); c
[[-2/3, 0], [1, 1], [2, 2], [3, 5], [pi/2, 7]]
sage: g = sum(a*x^b for a,b in c); g
pi*x^7/2 + 3*x^5 + 2*x^2 + x - 2/3
sage: g._latex_(simplify=False)  # requires attached patch
'0 + {-\\frac{2}{3} \\cdot {x}^{0} } + {1 \\cdot {x}^{1} } + {2 \\cdot
{x}^{2} } + {3 \\cdot {x}^{5} } + {\\frac{\\pi}{2} \\cdot {x}^{7} }'

Drat, that doesn't work since individual terms have to be simplified...
So better would be a function to sort the terms (which are stored in a binary
tree), and then latex without simplify would do what you want.

Summary: There is no simple way to do what you want right now  though
somebody could write it with some work.  Using f.coeffs might be
helpful.

William

--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sage-support
URLs: http://sage.math.washington.edu/sage/ and http://sage.scipy.org/sage/
-~----------~----~----~----~------~----~------~--~---

# HG changeset patch
# User William Stein <[EMAIL PROTECTED]>
# Date 1189361567 25200
# Node ID 7ac2f34ad006f51235f8a667e4f1c2fd58b20d09
# Parent  6352e6bf7f6094e58ff373078d41a5edb9d2ee8a
Add a "don't simplify" option to certain _latex_ methods in calculus.

diff -r 6352e6bf7f60 -r 7ac2f34ad006 sage/calculus/calculus.py
--- a/sage/calculus/calculus.py	Sun Sep 09 07:48:11 2007 -0700
+++ b/sage/calculus/calculus.py	Sun Sep 09 11:12:47 2007 -0700
@@ -2988,13 +2988,19 @@ class SymbolicArithmetic(SymbolicOperati
                 s[0] = '(%s)'%s[0]
             return '%s%s' % (symbols[op], s[0])
         
-    def _latex_(self):
+    def _latex_(self, simplify=True):
         # if we are not simplified, return the latex of a simplified version
-        if not self.is_simplified():
+        if simplify and not self.is_simplified():
             return self.simplify()._latex_()
         op = self._operator
         ops = self._operands
-        s = [x._latex_() for x in self._operands]
+        s = []
+        for x in self._operands:
+            try:
+                s.append(x._latex_(simplify=simplify))
+            except TypeError:
+                s.append(x._latex_())
+        ## what it used to be --> s = [x._latex_() for x in self._operands]
 
         if op is operator.add:
             return '%s + %s' % (s[0], s[1])

Reply via email to