3

(I did not know there is sage group at stackoverflow. I asked this before at sage support usenet, but that is very slow).

Here is the question:

Is there a way in sage to determine the size of expression as given typically by leaf count? Similar to what is documented in Mathematica leafCount[] here

http://reference.wolfram.com/language/ref/LeafCount.html

"gives the total number of indivisible subexpressions in expr."

And also similar to Maple's

http://www.maplesoft.com/support/help/Maple/view.aspx?path=MmaTranslator/Mma/LeafCount&term=leafcount

I need a way to measure the size of resulting expression from sage to compare it with Mathematica's result and Maple's as well. I currently use leafCount() for this since both Maple and Mathematica have this function.

Does sage have similar function or another way to obtain this measure?

For example, given this expression

 (c + integrate(e^(2*x + sin(x)), x))*e^(-sin(x))

Then in Mathematica I would write

  Clear[x, c];
  expr = (c + Integrate[Exp[2*x + Sin[x]], x])*Exp[-Sin[x]];
  LeafCount[expr]

          19

Update: To answer the question below on how the leaves are counted. If one draws a tree expression, then it is each node in the expression tree. For the above, it is

Mathematica graphics

Similarly, LeafCount[x + y] gives 3 since

Mathematica graphics

1

1 Answer 1

2

This is not built in, though perhaps it should be. As long as you are comfortable writing your own tree traversal, the discussions here and here should be helpful.

Here is a wrong implementation that should help you. It is wrong because I don't get 19 - basically, I get the number of constants and xs, which is not what you want. I don't know how such subexpressions are counted. But I think using a combination of expr.operands() and expr.operator() will get what you are looking for.

def tree(expr): 
    if expr.operator() is None: 
        return expr 
    else: 
        return map(tree, expr.operands()) 

expr = (x + integrate(e^(2*x + sin(x)), x))*e^(-sin(x))
len(flatten(tree(expr)))

Edit: If a leaf is really any node (not just leaves in the graph theory sense, which seems odd), then probably something here is indeed useful. Not my code:

sage: def tree(expr): 
....:         if expr.operator() is None: 
....:                 return expr 
....:         else: 
....:                 return [expr.operator()]+map(tree, expr.operands()) 
....:     
sage: var('y')
y
sage: tree(x+y)
[<function operator.add>, x, y]
sage: tree((y+integrate(e^(2*x+sin(x)),x))*e^(-sin(x)))
[<function operator.mul>,
 [<function operator.add>,
  y,
  [integrate,
   [exp, [<function operator.add>, [<function operator.mul>, x, 2], [sin, x]]],
   x]],
 [exp, [<function operator.mul>, [sin, x], -1]]]
sage: len(tree((y+integrate(e^(2*x+sin(x)),x))*e^(-sin(x))))
3
sage: len(flatten(tree((y+integrate(e^(2*x+sin(x)),x))*e^(-sin(x)))))
17

You'll notice that the answer is different because Sage counts exp as a primitive operator, not e^stuff.

See also the answer to your sage-support question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.