Ask Your Question
1

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

asked 2018-01-31 13:05:16 -0600

ds22 gravatar image

updated 2018-01-31 15:37:18 -0600

Ex 4.3 Dichotomy function

var('a','b','N')
(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]

Dichotomy(1,2,10)
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/sage_server.py", 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 
'sage.symbolic.expression.Expression'
edit retag flag offensive close merge delete

Comments

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 13:44:55 -0600 )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 13:46:42 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2018-01-31 13:40:21 -0600

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

Comments

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 13:53:51 -0600 )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

Stats

Asked: 2018-01-31 13:04:52 -0600

Seen: 37 times

Last updated: Jan 31