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, 09 Mar 2016 15:23:25 +0100substitute() raises exception where python does nothttps://ask.sagemath.org/question/32744/substitute-raises-exception-where-python-does-not/ In my usual workflow, I derive symbolic equations and then substitute their arguments by values stored in dictionaries to obtain numerical solutions. Now I found out that the substitute() command raises division by zero exception where standard python does not. This is a bit of a problem, as these exceptions prevent my code from evaluating all data sets once such an exception occurs and I don't want to wrap each line of code in a try-except construct. Does anyone know how the exception could be avoided? Here is an example:
var('y a x')
eq_y = y == a/x
vdict = {}
vdict[a] = 1.
vdict[x] = 0.
vdict[a]/vdict[x]
+infinity
However, if I substitute vdict into eq_y, I get an exception:
eq_y.subs(vdict)
Traceback (click to the left of this block for traceback)
...
ValueError: power::eval(): division by zero
stanWed, 09 Mar 2016 15:23:25 +0100https://ask.sagemath.org/question/32744/accessing data included in the error messagehttps://ask.sagemath.org/question/8517/accessing-data-included-in-the-error-message/I am quite new to Sage, so sorry in advance if the question is silly. I have searched the web and found not much that would be related.
I am trying to implement an factorization algorithm using elliptic curves (homework assignment). Say we are trying to factorize some n. The key point in the algorithm is to draw some knowledge from an error in arithmetic on a curve modulo n. More precisely, the error arises when (and only when) we are trying to invert some element in the modulo arithmetic when it is not invertible - and it means that it is not coprime to n, and thus taking their gcd we can find a factor of n.
What I don't know is how to get to the data included in the error message from inside the program.
An example is as follows:
n = 51
K = 720
C = EllipticCurve(Integers(n), [2,49])
P = C([1,1])
try:
K*P
except ZeroDivisionError:
raise
In the above code an error arises when computing K*P, which is caught and then thrown once more (since I don't know what else to do with it). The error message says:
Traceback (click to the left of this block for traceback)
...
ZeroDivisionError: Inverse of 34 does not exist (characteristic = 51 =
17*3)
I would like to get my hands on the number 34 appearing in the error message, without actually breaking the computation, so that it can be used in the program.
Ideally, it would work somewhat like that:
n = 51
K = 720
C = EllipticCurve(Integers(n), [2,49])
P = C([1,1])
try:
K*P
except ZeroDivisionError:
d = some_magic_function()
g = gcd(d,n)
print n, 'has divisor', g
I would appreciate any hints as to how it can be done.
Disclaimer: I have mentioned the question being connected to homework. The assignment was however only to cause the error message to appear, an then copy-paste the interesting number and resume computation. I find this approach somewhat dirty and hoped to look for something neater. I believe I am not doing anything immoral here.jakub.koniecznyTue, 29 Nov 2011 12:59:37 +0100https://ask.sagemath.org/question/8517/Any way to detect division by zero?https://ask.sagemath.org/question/7800/any-way-to-detect-division-by-zero/In Sage, is there any way (function call) to check whether a given expression will produce division by zero error. Is something like the following possible?
if IsDivisionByZero(expr):
return False
ShuWed, 08 Dec 2010 19:02:45 +0100https://ask.sagemath.org/question/7800/