Ask Your Question
1

Defining a protected division operator

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

joaofbsm gravatar image

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

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

Comments

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 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
0

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

eric_g gravatar image

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

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

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

It yields

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

Comments

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 -0500 )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: 2017-12-19 01:56:34 -0500

Seen: 139 times

Last updated: Dec 19 '17