# Revision history [back]

Concerning the first question, you could try the following code:

var("x")
@interact
def Taylor(
f = input_box(default="e^x", type=str),
n = slider(vmin=0, vmax=10, step_size=1,
default=3, label="Select the order n: ")):

try:
f = SR(f)
if x not in f.variables():
print("Illegal input. Try again")
else:
print("Taylor expansion:\n")
print(f, " = " , f.series(x==0, n))
except TypeError:
print("Please, finish typing a valid function")


See this SageMath Cell.

The first problem we have to face arises when SageMath reads the contents of the input box. In a Jupyter notebook, this happens many times per second while you type; in a SageMath Cell, just after hiting the Enter key. Without the type=str option in input_box, SageMath expects f to be an expression, so it can raise several kinds of errors, such as SyntaxError or NameError exceptions, for example, if the input is e^x+ (due to an unexpected EOF while parsing) or e^x+y (if y has not been defined as a variable). The option type=str avoids these errors, since f is just a string.

Now we can type a string without annoying error messages (well, still not; see below). The string is stored in f. But, before computing any series expansion, we have to convert f to a symbolic expression, done in the line f = SR(f), and check if x is a variable present in f. Hence, something like sin(a*x) is allowed, whereas t^2 just yields the error message «Illegal input…». If f contains a valid function, the Taylor expasion is obtained and printed. Then, in any case, the user can modify the expression or type a new one.

Why the main code is inside a try/except block? Suppose you want the expansion of e^(x^2). While you type, the content of the input box is processed. So, after writing, say e^(x^, SageMath would raise a TypeError exception, since e^(x^ is a malformed expression. Thanks to the the try/except commands, we can catch these errors and print a message to invite the user to finish typing.

Finally, I have replaced the taylor method by the series one, since, in general, f is not equal to the Taylor polynomial.

Concerning the second question, you can use similar code, like the following one:

R.<x,y,z> = PolynomialRing(QQ)

@interact
def Taylor(
f = input_box(default="x+y+z", type=str)):
try:
f = SR(f)
if f not in R:
print("Illegal input. Try again")
else:
print("The polynomial belongs to R")
except TypeError:
print("Please, finish typing a polynomial")


See this SageMath Cell

Concerning the first question, you could try the following code:

var("x")
@interact
def Taylor(
f = input_box(default="e^x", type=str),
n = slider(vmin=0, vmax=10, step_size=1,
default=3, label="Select the order n: ")):

try:
f = SR(f)
if x not in f.variables():
print("Illegal input. Try again")
else:
print("Taylor expansion:\n")
print(f, " = " , f.series(x==0, n))
except TypeError:
print("Please, finish typing a valid function")


See this SageMath Cell.

The first problem we have to face arises when SageMath reads the contents of the input box. In a Jupyter notebook, this happens many times per second while you type; in a SageMath Cell, just after hiting the Enter key. Without the type=str option in input_box, SageMath expects f to be an expression, so it can raise several kinds of errors, such as SyntaxError or NameError exceptions, for example, if the input is e^x+ (due to an unexpected EOF while parsing) or e^x+y (if y has not been defined as a variable). The option type=str avoids these errors, since f is just a string.

Now we can type a string without annoying error messages (well, still not; see below). The string is stored in f. But, before computing any series expansion, we have to convert f to a symbolic expression, done in the line f = SR(f), and check if x is a variable present in f. Hence, something like sin(a*x) is allowed, whereas t^2 just yields the error message «Illegal input…». If f contains a valid function, the Taylor expasion is obtained and printed. Then, in any case, the user can modify the expression or type a new one.

Why the main code is inside a try/except block? Suppose you want the expansion of e^(x^2). While you type, the content of the input box is processed. So, after writing, say e^(x^, SageMath would raise a TypeError exception, since e^(x^ is a malformed expression. Thanks to the the try/except commands, we can catch these errors and print a message to invite the user to finish typing.

Finally, I have replaced the taylor method by the series one, since, in general, f is not equal to the Taylor polynomial.

Concerning the second question, you can use similar code, like the following one:

R.<x,y,z> = PolynomialRing(QQ)

@interact
def Taylor(
f = input_box(default="x+y+z", type=str)):
try:
f = SR(f)
if f not in R:
print("Illegal input. Try again")
else:
print("The polynomial belongs to R")
except TypeError:
print("Please, finish typing a polynomial")


See this SageMath Cell

Concerning the first question, you could try the following code:

var("x")
@interact
def Taylor(
f = input_box(default="e^x", type=str),
n = slider(vmin=0, vmax=10, step_size=1,
default=3, label="Select the order n: ")):

try:
f = SR(f)
if x not in f.variables():
print("Illegal input. Try again")
else:
print("Taylor expansion:\n")
print(f, " = " , f.series(x==0, n))
except TypeError:
print("Please, finish typing a valid function")


See this SageMath Cell.

The first problem we have to face arises when SageMath reads the contents of the input box. In a Jupyter notebook, this happens many times per second while you type; in a SageMath Cell, just after hiting the Enter key. Without the type=str option in input_box, SageMath expects f to be an expression, so it can raise several kinds of errors, such as SyntaxError or NameError exceptions, for example, if the input is e^x+ (due to an unexpected EOF while parsing) or e^x+y (if y has not been defined as a variable). The option type=str avoids these errors, since f is just a string.

Now we can type a string without annoying error messages (well, still not; see below). The string is stored in f. But, before computing any series expansion, we have to convert f to a symbolic expression, done in the line f = SR(f), and check if x is a variable present in f. Hence, something like sin(a*x) is allowed, whereas t^2 just yields the error message «Illegal input…». If f contains a valid function, the Taylor expasion is obtained and printed. Then, in any case, the user can modify the expression or type a new one.

Why the main code is inside a try/except block? Suppose you want the expansion of e^(x^2). While you type, the content of the input box is processed. So, after writing, say e^(x^, SageMath would raise a TypeError exception, since e^(x^ is a malformed expression. Thanks to the the try/except commands, we can catch these errors and print a message to invite the user to finish typing.

Finally, I have replaced the taylor method by the series one, since, in general, f is not equal to the Taylor polynomial.

Concerning the second question, you can use similar code, like the following one:

R.<x,y,z> = PolynomialRing(QQ)

@interact
def Taylor(
f = input_box(default="x+y+z", type=str)):
try:
f = SR(f)
if f not in R:
print("Illegal input. Try again")
else:
print("The polynomial belongs to R")
except TypeError:
print("Please, finish typing a polynomial")


See this SageMath Cell

EDIT.

According to Sébastien comment, it is better to move part of the code to an else clause. So, here we have a reformulation of the code:

var("x")
@interact
def Taylor(
f = input_box(default="e^x", type=str),
n = slider(vmin=0, vmax=10, step_size=1,
default=3, label="Select the order n: ")):
try:
f = SR(f)
except TypeError:
print("Please, finish typing a valid function")
else:
if x not in f.variables():
print("Illegal input. Try again")
else:
print("Taylor expansion:\n")
print(f, " = " , f.series(x==0, n))


And also, for the second script:

R.<x,y,z> = PolynomialRing(QQ)

@interact
def Taylor(
f = input_box(default="x+y+z", type=str)):
try:
f = SR(f)
except TypeError:
print("Please, finish typing a polynomial")
else:
if f not in R:
print("Illegal input. Try again")
else:
print("The polynomial belongs to R")