Ask Your Question
1

TypeError when trying to read a string entered in a graphical user interface and convert it to an expression

asked 2018-03-08 07:17:48 -0500

joaoff gravatar image

Hi!

I am trying to read a string entered by a user in a GUI constructed with tkinter, and convert this string to a function. However, Sage rises a TypeError when I do this. A minimal example follows.

import tkinter

def compute_phaser_polynomials():
    var('omega')

    str_GroupDelaySpec = entry1.get() # string with the expression of the group delay as a function of omega
    #str_GroupDelaySpec = 'omega + 5/2'
    print(str_GroupDelaySpec)
    print(type(str_GroupDelaySpec))

    gds.<omega> = QQ[]
    group_delay_spec(omega) = gds(str_GroupDelaySpec)
    print(group_delay_spec(omega))
    print(type(group_delay_spec(omega)))

root = tkinter.Tk()

entry1 = tkinter.Entry(root)
entry1.grid()

button1 = tkinter.Button(root, text="Compute phaser polynomials", command=compute_phaser_polynomials)
button1.grid(row=1)

root.mainloop()

When tkinter is not involved, the conversion works.

gds.<omega> = QQ[]
f(omega) = gds('omega + 5/2')
print(f(omega))
print(f(omega).parent())
print(type(f(omega)))

Out: omega + 5/2
Out: Symbolic Ring
Out: <type 'sage.symbolic.expression.Expression'>

Am I doing something wrong? Is this a bug?

Thanks!

edit retag flag offensive close merge delete

2 answers

Sort by » oldest newest most voted
1

answered 2018-03-08 13:45:44 -0500

dan_fulea gravatar image

After replacing

group_delay_spec(omega) = gds(str_GroupDelaySpec)
print(group_delay_spec(omega))
print(type(group_delay_spec(omega)))

by

group_delay_spec = gds(str_GroupDelaySpec)
print(group_delay_spec)
print(type(group_delay_spec))

the above worked for me, the Tkinter input box was opened, i typed omega + 5/2 inside and pressed the run button. Results in the sage console launching the Tk box:

omega + 5/2
<type 'str'>
omega + 5/2
<type 'sage.rings.polynomial.polynomial_rational_flint.Polynomial_rational_flint'>

Inside of the Tk-function there is no way to "preparse" the "function" group_delay_spec(omega) (depending on no omega in fact), the left side in group_delay_spec(omega) = gds(str_GroupDelaySpec) must have a sense "as is". If a function is really needed, the the lambda definition or a clear def block should work.

edit flag offensive delete link more

Comments

@dan_fulea your answer was very helpful. Based on it, I've formulated another answer that I added to my own question.

joaoff gravatar imagejoaoff ( 2018-03-08 15:03:05 -0500 )edit
1

answered 2018-03-08 14:58:31 -0500

joaoff gravatar image

Hi, @dan_fulea!

Thank you for the answer, it really works. However, I'm still confused, as your solution returned an object of type "ring", and I was expecting an object of type "symbolic". Being honest, as my background is in engineering, I do not know what a "ring" is, I have never being introduced to the concept of "rings" before. Nevertheless, as you observed the issue in preparsing the function, I searched a little more and found another solution that returns a symbolic object. If I change

gds.<omega> = QQ[]
group_delay_spec(omega) = gds(str_GroupDelaySpec)
print(group_delay_spec(omega))
print(type(group_delay_spec(omega)))

by

group_delay_spec(omega) = sage_eval(str_GroupDelaySpec, locals={'omega':omega})
print(group_delay_spec(omega))
print(type(group_delay_spec(omega)))

the code returns

omega + 5/2
<type 'str'>
omega + 5/2
<type 'sage.symbolic.expression.Expression'>
edit flag offensive delete link more

Comments

A ring is mostly just a place where you can add and multiply, with multiplication and addition working well together.

The integers form a ring, the rationals form a ring, the reals form a ring, the complex numbers form a ring.

Polynomials with coefficients in a given ring also form a ring.

Matrices with entries in a given ring also form a ring.

slelievre gravatar imageslelievre ( 2018-03-26 09:32:24 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2018-03-08 07:17:48 -0500

Seen: 47 times

Last updated: Mar 08