ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sat, 15 May 2021 07:53:31 +0200Could we have a leaf_count() function in base sagemath?https://ask.sagemath.org/question/57123/could-we-have-a-leaf_count-function-in-base-sagemath/There is **leaf_count()** type function which is standard and buildin in other CAS systems (Mathematica and Maple). It is very useful. It is used to measure the size of a mathematical expression in a standard way.
This will make it easier for example, to compare the size of the anti-derivative from a call to integrate from different CAS systems.
There are couple of ad-hoc attempts on the net now to do this in sagemath, but they do not work for all expressions, (give errors in some cases) and do not produce good measure of the size of the expression.
leaf_count() is described in [https://reference.wolfram.com/language/ref/LeafCount.html](https://reference.wolfram.com/language/ref/LeafCount.html)
and
[https://www.maplesoft.com/support/help/Maple/view.aspx?path=MmaTranslator/Mma/LeafCount](https://www.maplesoft.com/support/help/Maple/view.aspx?path=MmaTranslator/Mma/LeafCount)
*LeafCount counts the number of subexpressions in expr that correspond to "leaves" on the expression tree.*
So basically, if it possible to obtain the expression tree of any sagemath mathematical expression, leaf_count() should just return the number of leaf nodes of that tree.
Currently, the way I obtain leaf_count() for a sagemath expression, is by calling Maple and passing it the expression (as a string), then use Maple's `LeafCount` function there (after converting the expression back to Maple, using parse function).
I use Maple not Mathematica for this, since the syntax of the sagemath expressions are much closer to Maple's.
But I prefer to do this all in sagemath. Much simpler. But the current implementation I saw does not work well for everything, and gives different result of the measure of the expression.
see as an example [https://stackoverflow.com/questions/25202346/how-to-obtain-leaf-count-expression-size-in-sage](https://stackoverflow.com/questions/25202346/how-to-obtain-leaf-count-expression-size-in-sage)
I am sure a sagemath internals expert here could write such a function and add it to sagemath as a standard buildin function. May be for 9.4 version?
The following are some examples
expr:=x;
MmaTranslator:-Mma:-LeafCount(expr)
1
expr:=1/2*(log(b*x + a) - log(b*x - a))/(a*b);
MmaTranslator:-Mma:-LeafCount(expr)
25
expr:=arctan(b*x/a)/(a*b);
MmaTranslator:-Mma:-LeafCount(expr)
14
expr:=-1/12*sqrt(3)*arctan(1/12*(7*sqrt(3)*cos(x)^2 - 4*sqrt(3))/(cos(x)*sin(x)));
MmaTranslator:-Mma:-LeafCount(expr)
31
expr:=[-sqrt(-a*b)*log((a*x - b - 2*sqrt(-a*b)*sqrt(x))/(a*x + b))/(a*b), -2*sqrt(a*b)*arctan(sqrt(a*b)/(a*sqrt(x)))/(a*b)];
MmaTranslator:-Mma:-LeafCount(expr)
68
The sagemath leaf_count() does not have to give same exact value as the above, but it should be very close to it.
Thank you
--NasserNasserSat, 15 May 2021 07:53:31 +0200https://ask.sagemath.org/question/57123/Sagemath, as an alternative of Geogebrahttps://ask.sagemath.org/question/51475/sagemath-as-an-alternative-of-geogebra/I was looking for an alternative of Geogebra - www.geogebra.org (since it's no longer available for my GNU/Linux distro), then I came across SageMath. My question is, is it possible to use **SageMath** as a complete alternative of **Geogebra**? Or there are other good alternative available for Geogebra than SageMath; if so, could you suggest it to me?finnThu, 21 May 2020 08:43:18 +0200https://ask.sagemath.org/question/51475/Microsoft Internet Explorer Version 9 or abovehttps://ask.sagemath.org/question/43383/microsoft-internet-explorer-version-9-or-above/How can I use sage without Microsoft Internet Explorer Version 9 or above? Because this is the system requirement of sage.
I ask this question only for a friend, but I hope someone could give me some suggestions.
Thanks! :)pizzaWed, 15 Aug 2018 09:01:33 +0200https://ask.sagemath.org/question/43383/