Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Hello, @JonasA! This is not a bug; the problem is in your definition of Qpart. Since you are using multi-line code, you should indicate Python (and thus Sage) where it starts and where it ends. There are two ways of doing this (that I'm aware of):

  1. You could use the line continuation character \ (which doesn't work inside strings):

    Qpart(x) = 5 * (unit_step(x) \
        * sin(x) \
        - sin(x))
    

    That clearly indicates that the first line continues with the second, and the second with the third, but the third does not continue with another line.

  2. You could enclose the defining expression of Qpart with parenthesis. Python and Sage assume that an expression that starts with a ( hasn't ended until the corresponding ) is found. In order for this to work, the whole expression must be enclosed; it's not enough to have unclosed parenthesis. In this case, your code should look like this:

    Qpart(x) = (5 * (unit_step(x)   
        * sin(x)
        - sin(x)))
    

    I don't recommend this for your formula, since you already have a certain number of parenthesis, and increasing this number could potentially make your code less clear.

However, I am baffled by the fact that your second piece of code does work. My guess is that

5 * (unit_step(x)   
    * sin(x)
    - sin(x))

IS one single complete expression by itself, so Python is clever enough to recognize its start and its end. On the contrary, your first piece of code:

Qpart(x) = 5 * (unit_step(x)
    * sin(x)
    - sin(x))

is NOT one single expression by itself, since there is another one in the way, namely, Qpart(x) =, so Python could be confused by this, and this avoids understanding the complete formula.

That being said you should always indicate that an expression continues, even is Python is clever enough to recognize it by itself. This isn't just considered a good programming practice, it also avoids potential errors in the code going unnoticed, which could be catastrophic. (And it also frees you from the responsibility of having to figure out when Python will understand a piece of code or not.)

Hello, @JonasA! This is not a bug; the problem is in your definition of Qpart. Since you are using multi-line code, you should indicate Python (and thus Sage) where it starts and where it ends. There are two ways of doing this (that I'm aware of):

  1. You could use the line continuation character \ (which doesn't work inside strings):

    Qpart(x) = 5 * (unit_step(x) \
        * sin(x) \
        - sin(x))
    

    That clearly indicates that the first line continues with the second, and the second with the third, but the third does not continue with another line.

  2. You could enclose the defining expression of Qpart with parenthesis. Python and Sage assume that an expression that starts with a ( hasn't ended until the corresponding ) is found. In order for this to work, the whole expression must be enclosed; it's not enough to have unclosed parenthesis. In this case, your code should look like this:

    Qpart(x) = (5 * (unit_step(x)   
        * sin(x)
        - sin(x)))
    

    I don't recommend this for your formula, since you already have a certain number of parenthesis, and increasing this number could potentially make your code less clear.

However, I am baffled by the fact that your second piece of code does work. My guess is that

5 * (unit_step(x)   
    * sin(x)
    - sin(x))

IS one single complete expression by itself, so Python is clever enough to recognize its start and its end. On the contrary, your first piece of code:

Qpart(x) = 5 * (unit_step(x)
    * sin(x)
    - sin(x))

is NOT one single expression by itself, since there is another one in the way, namely, Qpart(x) =, so Python could be confused by this, and this avoids understanding the complete formula.

That being said said, you should always indicate that an expression continues, even is Python is clever enough to recognize it by itself. This isn't just considered a good programming practice, it also avoids potential errors in the code going unnoticed, which could be catastrophic. (And it also frees you from the responsibility of having to figure out when Python will understand a piece of code or not.)