ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 24 Nov 2017 03:41:18 -0600How can I readline a one-term polynomial from a user?http://ask.sagemath.org/question/39760/how-can-i-readline-a-one-term-polynomial-from-a-user/I would like to ask the user for a function. How can I do that? Is there something like readline?Fri, 24 Nov 2017 02:08:11 -0600http://ask.sagemath.org/question/39760/how-can-i-readline-a-one-term-polynomial-from-a-user/Comment by veritas for <p>I would like to ask the user for a function. How can I do that? Is there something like readline?</p>
http://ask.sagemath.org/question/39760/how-can-i-readline-a-one-term-polynomial-from-a-user/?comment=39764#post-id-39764I would like the user to type a function in(the user of the programme).Fri, 24 Nov 2017 03:21:56 -0600http://ask.sagemath.org/question/39760/how-can-i-readline-a-one-term-polynomial-from-a-user/?comment=39764#post-id-39764Comment by B r u n o for <p>I would like to ask the user for a function. How can I do that? Is there something like readline?</p>
http://ask.sagemath.org/question/39760/how-can-i-readline-a-one-term-polynomial-from-a-user/?comment=39761#post-id-39761What do you mean by a "one-term polynomial"?
As for the existence of a readline, you can either use [`read_data` to read from a file](http://doc.sagemath.org/html/en/reference/interfaces/sage/interfaces/read_data.html), or any Python method to read data from a file. You can also [read from the standard input](https://en.wikibooks.org/wiki/Python_Programming/Input_and_Output), and then process the data you get. In particular, if you have a string representation `s` of some object of a class `C`, you can usually construct the object using `C(s)`. For instance, if you define `R.<x> = ZZ[]`, then `R('x^2 + x + 1')` returns the polynomial `x^2 + x + 1`. Using `eval(s)` will have the same effect here (since `x` is known to SageMath as the generator of the polynomial ring).Fri, 24 Nov 2017 02:23:28 -0600http://ask.sagemath.org/question/39760/how-can-i-readline-a-one-term-polynomial-from-a-user/?comment=39761#post-id-39761Answer by tmonteil for <p>I would like to ask the user for a function. How can I do that? Is there something like readline?</p>
http://ask.sagemath.org/question/39760/how-can-i-readline-a-one-term-polynomial-from-a-user/?answer=39765#post-id-39765In Python, you can read a string from the user prompt with the function `raw_input`:
sage: a = raw_input()
2*x+3
sage: a
'2*x+3'
sage: type(a)
<type 'str'>
Then you can transform that string into a symbolic expression:
sage: b = SR(a)
sage: b
2*x + 3
sage: type(b)
<type 'sage.symbolic.expression.Expression'>
sage: sin(b)
sin(2*x + 3)
You can put it within a function:
sage: def user_square():
....: print 'Please enter a function to be squared: ',
....: a = raw_input()
....: return SR(a)^2
sage: user_square()
Please enter a function to be squared: cos(d)+sin(t)
(cos(d) + sin(t))^2
If you want to make something fancy, with input boxes and sliders, you can have a look at Sage `interact`.Fri, 24 Nov 2017 03:41:18 -0600http://ask.sagemath.org/question/39760/how-can-i-readline-a-one-term-polynomial-from-a-user/?answer=39765#post-id-39765