# 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 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.

( 2018-01-31 13:44:55 -0500 )edit

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

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

( 2018-01-31 13:46:42 -0500 )edit

Sort by ยป oldest newest most voted

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

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

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.

( 2018-01-31 13:53:51 -0500 )edit