ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 20 Dec 2017 07:34:22 +0100Defining a protected division operatorhttps://ask.sagemath.org/question/40224/defining-a-protected-division-operator/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?Tue, 19 Dec 2017 08:56:34 +0100https://ask.sagemath.org/question/40224/defining-a-protected-division-operator/Comment by nbruin for <p>Is it possible to define a protected division operator in SageMath that turns division by zero into the constant <strong>1</strong>? I want to be able to do this when simplifying an expression in Sage. What would be the best approach to it?</p>
https://ask.sagemath.org/question/40224/defining-a-protected-division-operator/?comment=40233#post-id-40233It 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.Tue, 19 Dec 2017 19:06:01 +0100https://ask.sagemath.org/question/40224/defining-a-protected-division-operator/?comment=40233#post-id-40233Answer by eric_g for <p>Is it possible to define a protected division operator in SageMath that turns division by zero into the constant <strong>1</strong>? I want to be able to do this when simplifying an expression in Sage. What would be the best approach to it?</p>
https://ask.sagemath.org/question/40224/defining-a-protected-division-operator/?answer=40230#post-id-40230Well, 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)
1Tue, 19 Dec 2017 13:28:41 +0100https://ask.sagemath.org/question/40224/defining-a-protected-division-operator/?answer=40230#post-id-40230Comment by joaofbsm for <p>Well, using Python exceptions, you may define a division function like this:</p>
<pre><code>def div(x, y):
try:
return x/y
except ZeroDivisionError:
return 1
</code></pre>
<p>It yields </p>
<pre><code>sage: div(2,3)
2/3
sage: div(2,0)
1
sage: div(x,x-x)
1
</code></pre>
https://ask.sagemath.org/question/40224/defining-a-protected-division-operator/?comment=40246#post-id-40246Thanks 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))Wed, 20 Dec 2017 07:34:22 +0100https://ask.sagemath.org/question/40224/defining-a-protected-division-operator/?comment=40246#post-id-40246