ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 29 Nov 2011 07:07:43 -0600accessing data included in the error messagehttp://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.Tue, 29 Nov 2011 05:59:37 -0600http://ask.sagemath.org/question/8517/accessing-data-included-in-the-error-message/Answer by DSM for <p>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.</p>
<p>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.</p>
<p>What I don't know is how to get to the data included in the error message from inside the program.</p>
<p>An example is as follows:</p>
<pre><code>n = 51
K = 720
C = EllipticCurve(Integers(n), [2,49])
P = C([1,1])
try:
K*P
except ZeroDivisionError:
raise
</code></pre>
<p>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:</p>
<pre><code>Traceback (click to the left of this block for traceback)
...
ZeroDivisionError: Inverse of 34 does not exist (characteristic = 51 =
17*3)
</code></pre>
<p>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.</p>
<p>Ideally, it would work somewhat like that:</p>
<pre><code>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
</code></pre>
<p>I would appreciate any hints as to how it can be done.</p>
<p>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.</p>
http://ask.sagemath.org/question/8517/accessing-data-included-in-the-error-message/?answer=12951#post-id-12951The current idiom is something like this:
n = 51
K = 720
C = EllipticCurve(Integers(n), [2, 49])
P = C([1,1])
try:
K*P
except ZeroDivisionError as err:
d = Integer(err.args[0].split()[2])
g = gcd(d,n)
print n, 'has divisor', g
You'll sometimes see "except ZeroDivisionError, e:" which is an older style. The args[0].split()[2] stuff is because the exception which is thrown has a string as the first argument of the args tuple:
sage: err.args
('Inverse of 34 does not exist (characteristic = 51 = 17*3)',)
so it's not so pretty to extract it in this case.
The relevant section of [the Python tutorial](http://docs.python.org/tutorial/errors.html) covers it well.Tue, 29 Nov 2011 06:20:19 -0600http://ask.sagemath.org/question/8517/accessing-data-included-in-the-error-message/?answer=12951#post-id-12951Comment by jakub.konieczny for <p>The current idiom is something like this:</p>
<pre><code>n = 51
K = 720
C = EllipticCurve(Integers(n), [2, 49])
P = C([1,1])
try:
K*P
except ZeroDivisionError as err:
d = Integer(err.args[0].split()[2])
g = gcd(d,n)
print n, 'has divisor', g
</code></pre>
<p>You'll sometimes see "except ZeroDivisionError, e:" which is an older style. The args[0].split()[2] stuff is because the exception which is thrown has a string as the first argument of the args tuple:</p>
<pre><code>sage: err.args
('Inverse of 34 does not exist (characteristic = 51 = 17*3)',)
</code></pre>
<p>so it's not so pretty to extract it in this case.</p>
<p>The relevant section of <a href="http://docs.python.org/tutorial/errors.html">the Python tutorial</a> covers it well.</p>
http://ask.sagemath.org/question/8517/accessing-data-included-in-the-error-message/?comment=20801#post-id-20801Thank you very much! It works just as I hoped it would. I wish more manuals included the "as ... " part of the "except" thing.Tue, 29 Nov 2011 07:07:43 -0600http://ask.sagemath.org/question/8517/accessing-data-included-in-the-error-message/?comment=20801#post-id-20801