Ask Your Question

Is it possible to define a variable with a conditional statement and then simplify an expression with it?

asked 2023-05-08 18:44:22 +0100

ZL gravatar image

I have two real-valued variables, A and E. I want to define a third variable D such that:

if 0 <= E: D = A
elif E < 0: D = A + E

These variables are part of an extremely long expression (over 1200 characters) that I am trying to simplify. Working with a shorter related expression (around 100 characters), I found that a lot of the D and E instances either canceled out or reduced to As that could be combined with other As in the same parts of the expression, regardless of if E is assumed to be positive or negative. Working through the long expression by hand in the same way would be the height of tedium, so I'm trying to see if Sage can help out.

Is this possible? Is there a better way to get the desired result? I've tried a few different syntaxes, but all give the error "SyntaxError: multiple statements found while compiling a single statement". I am brand-new to Sage, so forgive me if this should be obvious. I've browsed the documentation but didn't see a clear answer one way or the other.

edit retag flag offensive close merge delete


It unclear what is your actual question. Please elaborate. As a side note, you don't need elif E < 0: in your code, a simple else: will be enough.

Max Alekseyev gravatar imageMax Alekseyev ( 2023-05-08 21:35:27 +0100 )edit

Is it possible to define a variable D equal to [the variable A] if [the variable E is positive] and equal to [A + E] if [the variable E is negative], and have Sage simplify an expression containing those three variables first with the assumption that E is positive and then with the assumption that E is negative?

ZL gravatar imageZL ( 2023-05-08 23:25:47 +0100 )edit

What is the type of value of E? An example would really be helpful.

Max Alekseyev gravatar imageMax Alekseyev ( 2023-05-09 00:15:14 +0100 )edit

As I said in the first sentence of my question, E is a real-valued number. It may be positive, zero, or negative. Its exact value, and that of A, is determined outside the scope of the expressions I'm working with. The only other constraint is that 0 <= A + E.

ZL gravatar imageZL ( 2023-05-09 16:05:42 +0100 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2023-05-08 21:56:47 +0100

Emmanuel Charpentier gravatar image

is :

D=A + E if E < 0 else A

what you are looking for ?

BTW, you could also write it D=A+(E*(E<0)) or D=A+min(E,0), probably among others...

edit flag offensive delete link more


When I enter your snippet, I get this error: "TypeError: unsupported operand parent(s) for >: 'Integer Ring' and '<class 'function'="">'".

ZL gravatar imageZL ( 2023-05-08 23:19:54 +0100 )edit

The error tells that E is a function and not numerical value.

Max Alekseyev gravatar imageMax Alekseyev ( 2023-05-09 00:16:03 +0100 )edit

The error tells that E is a function and not numerical value.

That depends of the value bound to the d identifier. @ZL's (non-) example didn't specify it, I did by : var("A, D, E"), Max (and possibly ZL) did not, and got the default binding (the derivative operator...).

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2023-05-09 08:59:49 +0100 )edit

I must have declared the variables incorrectly. Using Emmanuel's declaration it works perfectly. Thank you!

ZL gravatar imageZL ( 2023-05-09 16:36:23 +0100 )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


Asked: 2023-05-08 18:44:22 +0100

Seen: 86 times

Last updated: May 08 '23