Ask Your Question

In what way am I using the "&" sign incorrectly?

asked 2018-01-31 20:05:16 +0100

ds22 gravatar image

updated 2018-01-31 22:37:18 +0100

Ex 4.3 Dichotomy function

(a, b, N)
def Dichotomy(a,b,N):
    if f(a)*f(b) >= 0:
        print "Don't bother me or my wrath shall fall upon you"
    lb = a
    ub= b
    for i in range(1, N):
        mp = (lb+ub)/2
        if f(mp) == 0:
            lb = mp
            up = mp
        if(((f(mp)<0) & (f(lb)<0)) | ((f(mp)>0) & (f(lb)>0))):
            lp = mp
        else: up = mp
    return [lb,ub,ub-lb]

Don't bother me or my wrath shall fall upon you

Error in lines 1-1
Traceback (most recent call last):
  File "/cocalc/lib/python2.7/site-packages/smc_sagews/", line 1013, in execute
    exec compile(block+'\n', '', 'single') in namespace, locals
  File "", line 1, in 
  File "", line 11, in Dichotomy
TypeError: unsupported operand type(s) for &: 'sage.symbolic.expression.Expression' and 
edit retag flag offensive close merge delete


You did not provide the function f that you are using in your example.

Maybe you could add an extra argument f to dichotomy.

slelievre gravatar imageslelievre ( 2018-01-31 20:44:55 +0100 )edit

Note: arguments of Python functions don't need to be declared as symbolic variables.

(You can remove the line var(...).)

slelievre gravatar imageslelievre ( 2018-01-31 20:46:42 +0100 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2018-01-31 20:40:21 +0100

slelievre gravatar image

For boolean "and" in Sage, use and, not &.

For boolean "or" in Sage, use or, not |.

edit flag offensive delete link more


After that, rhere are some things left for you to debug in your function definition...

But since it seems this is an exercise, I will not spoil your debugging pleasure.

slelievre gravatar imageslelievre ( 2018-01-31 20:53:51 +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: 2018-01-31 20:04:52 +0100

Seen: 241 times

Last updated: Jan 31 '18