Ask Your Question

Defining a protected division operator

asked 2017-12-19 01:56:34 -0600

joaofbsm gravatar image

updated 2017-12-19 23:58:58 -0600

Is it possible to define a protected division operator in SageMath that turns division by zero into the constant 1? I want to be able to do this when simplifying an expression in Sage. What would be the best approach to it?

edit retag flag offensive close merge delete


It sounds like you would like to change the behaviour of division in "SymbolicRing" (where simplification is explicitly available). The result would be that you cannot trust the answer at all. Perhaps it is possible to replace the zeros you end up dividing by with a variable, say "zero" and simplify that expression? Then "zero/zero" gets simplified to 1. Then you could substitute "zero=0" afterwards.

nbruin gravatar imagenbruin ( 2017-12-19 12:06:01 -0600 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2017-12-19 06:28:41 -0600

eric_g gravatar image

updated 2017-12-19 06:31:01 -0600

Well, using Python exceptions, you may define a division function like this:

def div(x, y):
        return x/y
    except ZeroDivisionError:
        return 1

It yields

sage: div(2,3)
sage: div(2,0)
sage: div(x,x-x)
edit flag offensive delete link more


Thanks for your answer, Eric. Where do I need to define this function so Sage knows about it when evaluating the operation? Also, I'm running a Python script to simplify an equation, so, will it call this function when simplifying it? This is done by the following lines of code:

eq = "div(x0, x1)"
print(sage_eval(eq, locals=local_vars))
joaofbsm gravatar imagejoaofbsm ( 2017-12-20 00:34:22 -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


Asked: 2017-12-19 01:56:34 -0600

Seen: 43 times

Last updated: Dec 19 '17