Ask Your Question
1

piecewise defined function via def

asked 2018-02-11 03:43:20 +0100

newuser gravatar image

updated 2023-01-09 23:59:45 +0100

tmonteil gravatar image

I've made the following experiment with Sage:

    def f(x):

        if (0<= x<= 1/2):
            return 1
        else:
            return 0 
assume(0<= x<= 1/2)  
show(f(x))  
show(f(1/3))

However I get outputs 0 and 1 respectively. Can someone clarify please? Thanks.

edit retag flag offensive close merge delete

Comments

Sometimes one should constrain sage to evaluate...

def f(x):
    if bool(0<= x<= 1/2):
        return 1
    else:
        return 0

assume(0<= x<= 1/2)  
show(f(x))  
show(f(1/3))

This gives:

\newcommand{\Bold}[1]{\mathbf{#1}}1
\newcommand{\Bold}[1]{\mathbf{#1}}1
dan_fulea gravatar imagedan_fulea ( 2018-02-11 21:50:04 +0100 )edit
tmonteil gravatar imagetmonteil ( 2018-02-13 11:33:23 +0100 )edit

2 Answers

Sort by ยป oldest newest most voted
1

answered 2018-02-11 22:54:34 +0100

eric_g gravatar image

I think the issue is the double inequality in assume: only the first one is taken into account, as one can check with the function assumptions(), which returns the list of assumptions known to Sage:

sage: assume(0 <= x <= 1/2)
sage: assumptions()
[0 <= x]

Actually, one shall use assume with two single inequalities instead of a double inequality:

sage: assume(0 <= x, x <= 1/2)
sage: assumptions()
[0 <= x, x <= (1/2)]

Then your function will return 1 for f(x), as expected.

edit flag offensive delete link more
1

answered 2018-02-13 16:55:39 +0100

tmonteil gravatar image

updated 2018-02-13 17:32:21 +0100

This is clearly a bug, thanks for reporting ! See trac ticket 24726

Note that this is actually not the problem with assume but with symbolic expression involving more than one comparison operator:

sage: 0 <= x <= 1/2
0 <= x

sage: e = 0 <= x <= 1/2
sage: e.operator()
<built-in function le>
sage: e.operands()
[0, x]

Note the following weird behaviour:

sage: assume(0 <= x)
sage: 0 <= x <= 1/2
x <= (1/2)

This is because now 0 <= x is evaluated to True, and True and something returns something.

edit flag offensive delete link more

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-02-11 03:43:20 +0100

Seen: 556 times

Last updated: Feb 13 '18