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.Mon, 09 Mar 2020 22:02:39 +0100Some code gives error in sagemath 9 but OK in 8.9https://ask.sagemath.org/question/50204/some-code-gives-error-in-sagemath-9-but-ok-in-89/After I upgraded to sagemath 9.0 some of the code that used to work now gives error. The code was used to obtain an estimate of size of expression.
>sage --version
SageMath version 9.0, Release Date: 2020-01-01
>which sage
/bin/sage
It looks like sagemath 9 now uses python 3.0 while 8.9 used python 2 (since I had to change all my print statements to use () to make them work.
Here is an example of function that now gives an error. This is in file, say `bug_sage.py`
#!/usr/bin/env sage
from sage.all import *
def tree(expr):
if expr.operator() is None:
return expr
else:
return [expr.operator()]+map(tree, expr.operands())
var('x')
tree(x*e**((x*log(x) + 1)/log(x)))
Now from command line at Linux, I type `sage ./bug_sage.py` and it gives the error
Traceback (most recent call last):
File "./bug_sage.py", line 12, in <module>
print (tree(x*e**((x*log(x) + 1)/log(x))))
File "./bug_sage.py", line 9, in tree
return [expr.operator()]+map(tree, expr.operands())
TypeError: can only concatenate list (not "map") to list
In 8.9, no error is generated.
It looks like this is due to change in Python itself? This function is meant to generate list of all operands in expression in order to estimate the size of the expression. It is later used as follows
len(flatten(tree(anti)))
Any idea how to fix it? Or do you suggest better way to obtain size of expression (called leaf count in other CAS systems).
Mon, 09 Mar 2020 21:52:26 +0100https://ask.sagemath.org/question/50204/some-code-gives-error-in-sagemath-9-but-ok-in-89/Answer by rburing for <p>After I upgraded to sagemath 9.0 some of the code that used to work now gives error. The code was used to obtain an estimate of size of expression.</p>
<pre><code>>sage --version
SageMath version 9.0, Release Date: 2020-01-01
>which sage
/bin/sage
</code></pre>
<p>It looks like sagemath 9 now uses python 3.0 while 8.9 used python 2 (since I had to change all my print statements to use () to make them work.</p>
<p>Here is an example of function that now gives an error. This is in file, say <code>bug_sage.py</code></p>
<pre><code>#!/usr/bin/env sage
from sage.all import *
def tree(expr):
if expr.operator() is None:
return expr
else:
return [expr.operator()]+map(tree, expr.operands())
var('x')
tree(x*e**((x*log(x) + 1)/log(x)))
</code></pre>
<p>Now from command line at Linux, I type <code>sage ./bug_sage.py</code> and it gives the error</p>
<pre><code>Traceback (most recent call last):
File "./bug_sage.py", line 12, in <module>
print (tree(x*e**((x*log(x) + 1)/log(x))))
File "./bug_sage.py", line 9, in tree
return [expr.operator()]+map(tree, expr.operands())
TypeError: can only concatenate list (not "map") to list
</code></pre>
<p>In 8.9, no error is generated. </p>
<p>It looks like this is due to change in Python itself? This function is meant to generate list of all operands in expression in order to estimate the size of the expression. It is later used as follows</p>
<pre><code> len(flatten(tree(anti)))
</code></pre>
<p>Any idea how to fix it? Or do you suggest better way to obtain size of expression (called leaf count in other CAS systems).</p>
https://ask.sagemath.org/question/50204/some-code-gives-error-in-sagemath-9-but-ok-in-89/?answer=50205#post-id-50205Yes, you are right: `map` no longer returns a `list` but rather an iterator. So you can do e.g.:
def tree(expr):
if expr.operator() is None:
return expr
else:
return [expr.operator()]+list(map(tree, expr.operands()))
You can also avoid map and write a list comprehension instead:
def tree(expr):
if expr.operator() is None:
return expr
else:
return [expr.operator()]+[tree(op) for op in expr.operands()]Mon, 09 Mar 2020 22:02:39 +0100https://ask.sagemath.org/question/50204/some-code-gives-error-in-sagemath-9-but-ok-in-89/?answer=50205#post-id-50205