x not defined in continued fraction script

25 views
Skip to first unread message

David Collett

unread,
Mar 19, 2020, 4:47:30 PM3/19/20
to sympy
Hi, everyone. I'm new to python and to sympy.

On the sympy documentation site (https://docs.sympy.org/latest/tutorial/simplification.html#example-continued-fractions), near the end is the following script:

def list_to_frac(l): expr = Integer(0) for i in reversed(l[1:]): expr += i expr = 1/expr return l[0] + expr
print(list_to_frac([x, y, z]))
__________

The output they show (in the SymPy Live Shell) is:

x + (1 / (y + 1/z))

However, when I copied this to a text file and run it from the terminal (MacOS, Python 3.x), I get the following error:

NameError: name 'x' is not defined


How do I solve this problem?


(If instead of x, y, z, I substitute numbers, such as list_to_frac([1,1,3,1,1,5,1,1,7]) then a fraction is returned (1463/822) in this case.



Thanks for your help.


David




𝑥+1𝑦+1𝑧

David Bailey

unread,
Mar 19, 2020, 5:44:46 PM3/19/20
to sy...@googlegroups.com
On 19/03/2020 20:18, David Collett wrote:
Hi, everyone. I'm new to python and to sympy.

On the sympy documentation site (https://docs.sympy.org/latest/tutorial/simplification.html#example-continued-fractions), near the end is the following script:

def list_to_frac(l): expr = Integer(0) for i in reversed(l[1:]): expr += i expr = 1/expr return l[0] + expr
print(list_to_frac([x, y, z]))
__________
The output they show (in the SymPy Live Shell) is:
x + (1 / (y + 1/z))
However, when I copied this to a text file and run it from the terminal (MacOS, Python 3.x), I get the following error:

NameError: name 'x' is not defined

How do I solve this problem?

Notice that as it is your script makes no reference to SymPy, even though you have presumably installed it.

Your script needs to start by importing SymPy and creating the desired algebraic symbols, e.g.

import sympy

from sympy import symbols

x,y,z=symbols("x y z")


This makes x reference the SymPy symbol "x", and likewise for y, and z

Note there are various other ways to achieve this, such as:

from sympy.abc import x,y,z      


(If instead of x, y, z, I substitute numbers, such as list_to_frac([1,1,3,1,1,5,1,1,7]) then a fraction is returned (1463/822) in this case.

I am not clear if the fraction was what you wanted, or if you desired the continued fraction expression itself. I experimented a bit using UnevaluatedExpr, but it isn't easy to get a clean looking continued fraction expression when using integers. However you could use a list such as [x1,x1,x3,x1,x1,x5,x1,x1,x7] thus:

x1,x3,x5,x7=symbols("x1 x3 x5 x7")

list_to_frac( [x1,x1,x3,x1,x1,x5,x1,x1,x7])

producing the result:

x1 + 1/(x1 + 1/(x3 + 1/(x1 + 1/(x1 + 1/(x5 + 1/(x1 + 1/(x1 + 1/x7)))))))

If however, you just wanted the answer converted to a floating point number, then try

N(list_to_frac([1,1,3,1,1,5,1,1,7]))

David


David Collett

unread,
Mar 19, 2020, 8:11:24 PM3/19/20
to sympy
Thanks very much for taking the time to help.

I made the changes you suggested, and it works.

Yes, I would prefer it to print the actual continued fraction rather than a simple fraction, but I can't figure it out.

Also, in the SymPy Live Shell, the output of the continued fraction is beautifully displayed. I realize that this same output isn't possible in the Terminal. But is there any way to output nice-looking math formulas such as this? 

Thanks again for your help.

David

-----

Aaron Meurer

unread,
Mar 19, 2020, 10:14:34 PM3/19/20
to sympy
If you run the init_printing() function in the terminal it will enable
pretty printing. Or you can use the isympy script which does this
automatically.

Aaron Meurer
> --
> You received this message because you are subscribed to the Google Groups "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/2f902f4d-5b29-49d6-a5a1-be40cc1c8a91%40googlegroups.com.

David Bailey

unread,
Mar 20, 2020, 6:48:04 AM3/20/20
to sy...@googlegroups.com
On 20/03/2020 00:11, David Collett wrote:
Thanks very much for taking the time to help.

I made the changes you suggested, and it works.

That is good to hear! I am actually fairly new to SymPy and Python, but I am exploring them as something of a hobby. I haven't yet seen some SymPy documentation that really explores the basics of the relationship between Python and SymPy. In particular the issue of what is a SymPy variable (i.e. an algebraic variable) and exactly how it differs from a Python variable.  Perhaps you only became interested in Python because of SymPy. This can compound the possible ways to get confused.

This might help to unscramble things for you, but just use it for learning - not for real software!

fred=symbols('x')

fred

a=symbols('b')

b=symbols('a')

c=a

d=b

Now play about with fred, a, b,c, d

This will show you the difference between SymPy's algebraic symbols and Python's variables. In particular, when you write x=symbols('x'), the fact that you equate the Python variable x to the SymPy algebraic symbol x is purely a convenience to keep you sane.



Yes, I would prefer it to print the actual continued fraction rather than a simple fraction, but I can't figure it out.

Also, in the SymPy Live Shell, the output of the continued fraction is beautifully displayed. I realize that this same output isn't possible in the Terminal. But is there any way to output nice-looking math formulas such as this?

I am sure someone here will solve that for you, and the various shells such as Jupyter will use Latex, if it is loaded on your machine, so you could probably get the continued fraction displayed that way.

I have an obsolete website that I used when I was a Mathematica consultant, and I plan to replace that with a website devoted to SymPy - which is totally free, and comes with far less opaque baggage than Mathematica! I want to start with all the basics.

David



Thanks again for your help.

David

-----

On Thursday, March 19, 2020 at 1:47:30 PM UTC-7, David Collett wrote:
Hi, everyone. I'm new to python and to sympy.

On the sympy documentation site (https://docs.sympy.org/latest/tutorial/simplification.html#example-continued-fractions), near the end is the following script:

def list_to_frac(l): expr = Integer(0) for i in reversed(l[1:]): expr += i expr = 1/expr return l[0] + expr
print(list_to_frac([x, y, z]))
__________
The output they show (in the SymPy Live Shell) is:
x + (1 / (y + 1/z))
However, when I copied this to a text file and run it from the terminal (MacOS, Python 3.x), I get the following error:

NameError: name 'x' is not defined

How do I solve this problem?

(If instead of x, y, z, I substitute numbers, such as list_to_frac([1,1,3,1,1,5,1,1,7]) then a fraction is returned (1463/822) in this case.

Thanks for your help.

David

𝑥+1𝑦+1𝑧
--

David Collett

unread,
Mar 21, 2020, 12:31:50 PM3/21/20
to sympy
Thank you all again for your help. I'm starting to learn Python late in life. Hopefully it will keep my mind young.


On Thursday, March 19, 2020 at 1:47:30 PM UTC-7, David Collett wrote:
Reply all
Reply to author
Forward
0 new messages