Mathematica Overflow[] error : Why and how-to bypass? - wolfram-mathematica

I never had an overflow error in Mathematica, the following happened.
I demo-ed the principle of RSA-encryption as follows:
n = 11*13
m = EulerPhi[n]
e = 7
GCD[e, m]
d = PowerMod[e, -1, m]
cipher2[m_String] := Map[Mod[#^e, n] &, ToCharacterCode[m]]
decipher2[x_Integer] := FromCharacterCode[Map[Mod[#^d, n] &, x]]
In[207]:= cipher2["StackOverflow"]
decipher2[cipher2["StackOverflow"]]
Out[207]= {8,129,59,44,68,40,79,62,49,119,4,45,37}
Out[208]= StackOverflow
No problem sofar.
Then I changed the prime numbers to a more realistically, but still very moderate size.
n = 252097800611*252097800629
In[236]:= cipher2["StackOverflow"]
decipher2[cipher2["StackOverflow"]]
Out[236]= {27136050989627, 282621973446656, 80798284478113, \
93206534790699, 160578147647843, 19203908986159, 318547390056832, \
107213535210701, 250226879128704, 114868566764928, 171382426877952, \
207616015289871, 337931541778439}
During evaluation of In[236]:= General::ovfl: Overflow occurred in computation. >>
During evaluation of In[236]:= General::ovfl: Overflow occurred in computation. >>
Out[237]= FromCharacterCode[{Overflow[], Overflow[], Overflow[],
Overflow[], Overflow[], Overflow[], Overflow[], Overflow[],
Overflow[], Overflow[], Overflow[], Overflow[], Overflow[]}]
Question : Have I simply gone through the limits of Mathematica? Have I used an incorrect approach? What is the by-pass, if any ??

Try using PowerMod in the decyphering operation:
n = 252097800611*252097800629;
m = EulerPhi[n];
e = 7;
Print[GCD[e, m]];
d = PowerMod[e, -1, m];
Print[{"n" -> n, "m" -> m, "e" -> e, "d" -> d}];
Grid[
Join[{
{"Input", "Encrypted", "Decrypt with Mod", "Decrypt with PowerMod"}},
Table[{i, (j = Mod[i^e, n]), Mod[j^d, n], PowerMod[j, d, n]}, {i, 40}]],
Frame -> All]

Yes, you have gone through the limits of Mathematica. The maximum number that can be represented on a system in a particular version of Mathematica is shown by $MaxNumber. In your second example, d=18158086021982021938023 and hence 27136050989627^d is way way larger than $MaxNumber.
You can use PowerMod in the second step too like you did for d, which will compute a^b mod n more efficiently than Mod. With decipher2[x_List] := FromCharacterCode[Map[PowerMod[#, d, n] &, x]], you get:
cipher2["StackOverflow"]
decipher2[cipher2["StackOverflow"]]
Out[1]= {27136050989627, 282621973446656, 80798284478113, \
93206534790699, 160578147647843, 19203908986159, 318547390056832, \
107213535210701, 250226879128704, 114868566764928, 171382426877952, \
207616015289871, 337931541778439}
Out[2]= "StackOverflow"

Yep, as the other guy answered you have well and truly reached the $MaxNumber Mathematica can handle.
There is a bypass which will find mod for many large numbers larger than $MaxNumber.
Rather than imputing large numbers directly into Mathematica, such as 163840000000^18158086021982021938023 which is absolutely huge, use Modular arithmetic to save Mathematica the trouble of having to compute such a large number.
You should be able to develop a Mathematica Code for this, I do not yet know how to do this. But you can do it by hand, by finding:
Mod[Mod[Mod[Mod[Mod[Mod[Mod[Mod[163840000000^181,n]^580,n]^860,n]^219,n]^820,n]^219,n]^380,n]^23,n]
Which gives the correct answer you are looking for, without exceeding $MaxNumber

Related

Mathematica: Integration of Bessel Function & Exponent Function & Trigonometric Function

I have an integral with the form
Int[k_]:=Integrate[Exp[-x]xSin[x]BesselJ[0,k*x],{x,0,10}]
where BesselJ[0,kr] is the modified Bessel function of the first kind.
Now i can't get the directly answer from Mathematica..
I want to get the curve of Int[k], maybe a approximate is also acceptable..What can I do then?
Since the function doesn't have an antiderivative, your best bet is to numerically integrate. Example:
Int[k_] := NIntegrate[Exp[-x] x Sin[x] BesselJ[0, k x], {x, 0, 10}]
Plot[Int[k], {k, -5, 5}]
PS: I have edited your question, as you had some typos. You cannot use I as the symbol (it messes the complex i), and also when defining a function have to use := instead of =.
Even setting the constants to unity, Mathematica cannot find a formula for the integral. I.e.
a = b = k = d = 1;
Integrate[(a r Exp[-r] - b r Sin[k (r - d)] Exp[-r]) BesselJ[0, k r], r]
The integral is returned unchanged.
Simplifying things a bit shows some progress, returning a formula.
Integrate[Sin[k (r - d)] BesselJ[0, k r], r]
But adding back in one of the exponents throws it again.
Integrate[Sin[k (r - d)] Exp[-r] BesselJ[0, k r], r]

Solve linear system of equations with symbolic expressions

Hi I am trying to solve a linear system of equations with mathematica. I have 18 equations and 18 Unknowns and the coefficient matrix has full rank. All entries are symbolic since I am trying to solve the problem analytically. Unfortunately Mathematica never stops the evaluation. I have prepared a minimal working example:
n = 18
A = Table[AA[i, j], {i, 1, n}, {j, 1, n}];
A // MatrixForm
x = Table[xx[i], {i, 1, n}]
b = Table[bb[i], {i, 1, n}]
MatrixRank[A]
sol = Timing[Solve[{A.x == b}, x, Reals]]
A.x == b //. sol[[2]][[1]] // Simplify
For n=2,3,4,.. all works perfectly well. But with n=10... nothing works anymore.
Why has mathematica such problems solving this?
Is there a way to solve this problem?
Thanks for help,
Andreas
You simply need more memory:
The symbolic solution involves n+1 determinants, here is an estimate of the memory needed.
bc[n_] := (A = Det[Array[a, {n, n}]];ByteCount[A])
ListLogPlot[
t = Table[ {n, (n + 1) bc[n] /1024^3 // N} , {n,2,10}], Joined -> True]
extrapolating to n=18 we can see you'll need only about 10^8 Gigabytes..
(thats 1000x more than the largest supercomputers for anyone not getting the point )

Mathematica 8 overflow error by Mod command (RSA-code) [duplicate]

I never had an overflow error in Mathematica, the following happened.
I demo-ed the principle of RSA-encryption as follows:
n = 11*13
m = EulerPhi[n]
e = 7
GCD[e, m]
d = PowerMod[e, -1, m]
cipher2[m_String] := Map[Mod[#^e, n] &, ToCharacterCode[m]]
decipher2[x_Integer] := FromCharacterCode[Map[Mod[#^d, n] &, x]]
In[207]:= cipher2["StackOverflow"]
decipher2[cipher2["StackOverflow"]]
Out[207]= {8,129,59,44,68,40,79,62,49,119,4,45,37}
Out[208]= StackOverflow
No problem sofar.
Then I changed the prime numbers to a more realistically, but still very moderate size.
n = 252097800611*252097800629
In[236]:= cipher2["StackOverflow"]
decipher2[cipher2["StackOverflow"]]
Out[236]= {27136050989627, 282621973446656, 80798284478113, \
93206534790699, 160578147647843, 19203908986159, 318547390056832, \
107213535210701, 250226879128704, 114868566764928, 171382426877952, \
207616015289871, 337931541778439}
During evaluation of In[236]:= General::ovfl: Overflow occurred in computation. >>
During evaluation of In[236]:= General::ovfl: Overflow occurred in computation. >>
Out[237]= FromCharacterCode[{Overflow[], Overflow[], Overflow[],
Overflow[], Overflow[], Overflow[], Overflow[], Overflow[],
Overflow[], Overflow[], Overflow[], Overflow[], Overflow[]}]
Question : Have I simply gone through the limits of Mathematica? Have I used an incorrect approach? What is the by-pass, if any ??
Try using PowerMod in the decyphering operation:
n = 252097800611*252097800629;
m = EulerPhi[n];
e = 7;
Print[GCD[e, m]];
d = PowerMod[e, -1, m];
Print[{"n" -> n, "m" -> m, "e" -> e, "d" -> d}];
Grid[
Join[{
{"Input", "Encrypted", "Decrypt with Mod", "Decrypt with PowerMod"}},
Table[{i, (j = Mod[i^e, n]), Mod[j^d, n], PowerMod[j, d, n]}, {i, 40}]],
Frame -> All]
Yes, you have gone through the limits of Mathematica. The maximum number that can be represented on a system in a particular version of Mathematica is shown by $MaxNumber. In your second example, d=18158086021982021938023 and hence 27136050989627^d is way way larger than $MaxNumber.
You can use PowerMod in the second step too like you did for d, which will compute a^b mod n more efficiently than Mod. With decipher2[x_List] := FromCharacterCode[Map[PowerMod[#, d, n] &, x]], you get:
cipher2["StackOverflow"]
decipher2[cipher2["StackOverflow"]]
Out[1]= {27136050989627, 282621973446656, 80798284478113, \
93206534790699, 160578147647843, 19203908986159, 318547390056832, \
107213535210701, 250226879128704, 114868566764928, 171382426877952, \
207616015289871, 337931541778439}
Out[2]= "StackOverflow"
Yep, as the other guy answered you have well and truly reached the $MaxNumber Mathematica can handle.
There is a bypass which will find mod for many large numbers larger than $MaxNumber.
Rather than imputing large numbers directly into Mathematica, such as 163840000000^18158086021982021938023 which is absolutely huge, use Modular arithmetic to save Mathematica the trouble of having to compute such a large number.
You should be able to develop a Mathematica Code for this, I do not yet know how to do this. But you can do it by hand, by finding:
Mod[Mod[Mod[Mod[Mod[Mod[Mod[Mod[163840000000^181,n]^580,n]^860,n]^219,n]^820,n]^219,n]^380,n]^23,n]
Which gives the correct answer you are looking for, without exceeding $MaxNumber

Mathematica: FindRoot errors

FindRoot[
27215. - 7.27596*10^-12 x + 52300. x^2 - 9977.4 Log[1. - 1. x] == 0
,
{x, 0.000001}
]
converges to the solution {x -> -0.0918521} but how can I get Mathematica to avoid the following error message before the solution:
FindRoot::nlnum: The function value {Indeterminate} is not a list of numbers with dimensions {1} at {x} = {1.}. >>
I am using FindRoot to solve some pretty messy expressions. I also sometimes receive the following error, though Mathematica will still yield an answer, but am wondering if there is a way to avoid it as well:
FindRoot::lstol: The line search decreased the step size to within tolerance specified by AccuracyGoal and PrecisionGoal but was unable to find a sufficient decrease in the merit function. You may need more than MachinePrecision digits of working precision to meet these tolerances. >>
The solution you are getting is not the actual solution. The message indicates something was wrong and FindRoot returns the last value of x. This is the last item under 'More Information' for FindRoot:
If FindRoot does not succeed in finding a solution to the accuracy you specify within MaxIterations steps, it returns the most recent approximation to a solution that it found. You can then apply FindRoot again, with this approximation as a starting point.
For example, in this case there is also no solution:
FindRoot[x^2 + 1 == 0, {x, 1}]
You will get a FindRoot::jsing warning and Mathematica returns {x -> 0.} (which is the most recent approximation).
A similar case like this, but with a Log function:
FindRoot[1 + Log[1 + x]^2 == 0, {x, 2}]
Gives a FindRoot::nlnum similar to what you are seeing and returns {x -> 0.000269448} (which is the most recent approximation in this case).
This is a plot of the same function, for illustration purposes:
If you want to include complex roots, consider this part of the documentation for FindRoot (under 'More Information' also):
You can always tell FindRoot to search for complex roots by adding 0.I to the starting value.
So, for example, you can take a starting value near one complex root, like so:
FindRoot[x^2 + 1 == 0, {x, 1 + 1. I}]
Which converges (without messages) to {x -> 8.46358*10^-23 + 1. I} (so basically I).
Or with a starting value near the other complex root:
FindRoot[x^2 + 1 == 0, {x, 1 - 1. I}]
You will get basically -I (to be precise you get {x -> 8.46358*10^-23 - 1. I}).
There isn't a real solution to this equation. Mathematica ends up getting somewhere near the minimum of the function, and reports this because that's where the algorithm converges to.
Plot[27215. - 7.27596*10^-12 x + 52300. x^2 - 9977.4 Log[1. - 1. x],
{x, -2, 0.09}, AxesOrigin -> {0, 0}]
Mathematica does warn you about this:
In[30]:= x /.
Table[FindRoot[
27215. - 7.27596*10^-12 x + 52300. x^2 - 9977.4 Log[1. - 1. x] ==
0, {x, y}], {y, -0.01, 0.01, 0.0002}]
During evaluation of In[30]:= FindRoot::nlnum: The function value {Indeterminate} is not a list of numbers with dimensions {1} at {x} = {1.}. >>
During evaluation of In[30]:= FindRoot::nlnum: The function value {Indeterminate} is not a list of numbers with dimensions {1} at {x} = {1.}. >>
During evaluation of In[30]:= FindRoot::nlnum: The function value {Indeterminate} is not a list of numbers with dimensions {1} at {x} = {1.}. >>
During evaluation of In[30]:= General::stop: Further output of FindRoot::nlnum will be suppressed during this calculation. >>
During evaluation of In[30]:= FindRoot::lstol: The line search decreased the step size to within tolerance specified by AccuracyGoal and PrecisionGoal but was unable to find a sufficient decrease in the merit function. You may need more than MachinePrecision digits of working precision to meet these tolerances. >>
During evaluation of In[30]:= FindRoot::lstol: The line search decreased the step size to within tolerance specified by AccuracyGoal and PrecisionGoal but was unable to find a sufficient decrease in the merit function. You may need more than MachinePrecision digits of working precision to meet these tolerances. >>
During evaluation of In[30]:= FindRoot::lstol: The line search decreased the step size to within tolerance specified by AccuracyGoal and PrecisionGoal but was unable to find a sufficient decrease in the merit function. You may need more than MachinePrecision digits of working precision to meet these tolerances. >>
During evaluation of In[30]:= General::stop: Further output of FindRoot::lstol will be suppressed during this calculation. >>
Out[30]= {-0.0883278, -0.0913649, -0.0901617, -0.0877546, -0.0877383, \
-0.088508, -0.0937041, -0.0881606, -0.0912122, -0.0899562, \
-0.0876965, -0.0879619, -0.0877441, -0.101551, -0.0915088, \
-0.0880611, -0.0959972, -0.0930364, -0.0902243, -0.0877198, \
-0.0881157, -0.107205, -0.103746, -0.100439, -0.0972646, -0.094208, \
-0.0912554, -0.0878633, -0.089473, -0.0884659, -0.0876997, \
-0.0876936, -0.0879112, -0.104396, -0.100987, -0.0976638, -0.0879892, \
-0.087777, -0.0881334, -0.0880071, -0.0880255, -0.0880285, \
-0.0880345, -0.0911966, -0.0879797, -0.0890295, -0.087701, \
-0.0952537, -0.0941312, -0.0929994, -0.0918578, -0.0885677, \
-0.0895444, -0.0883719, -0.103914, -0.102701, -0.0885007, -0.0915083, \
-0.098988, -0.0963068, -0.0891533, -0.0907357, -0.0881215, \
-0.0893928, -0.108191, -0.104756, -0.101456, -0.0982737, -0.0951949, \
-0.0922072, -0.0892996, -0.0878794, -0.0877164, -0.0896659, \
-0.0886859, -0.0876952, -0.0909219, -0.0899049, -0.0888758, \
-0.0878343, -0.0952044, -0.0941281, -0.0887345, -0.0919322, \
-0.0886726, -0.0876955, -0.0877232, -0.0878879, -0.0877578, \
-0.101642, -0.0916633, -0.0991254, -0.0877255, -0.0936139, \
-0.0907846, -0.0877205, -0.0877454, -0.0881589, -0.0893507, \
-0.0878747, -0.0876961}

Trying to get Mathematica to approximate an integral

I am trying to get Mathematica to approximate an integral that is a function of various parameters. I don't need it to be extremely precise -- the answer will be a fraction, and 5 digits would be nice, but I'd settle for as few as 2.
The problem is that there is a symbolic integral buried in the main integral, and I can't use NIntegrate on it since its symbolic.
F[x_, c_] := (1 - (1 - x)^c)^c;
a[n_, c_, x_] := F[a[n - 1, c, x], c];
a[0, c_, x_] = x;
MyIntegral[n_,c_] :=
NIntegrate[Integrate[(D[a[n,c,y],y]*y)/(1-a[n,c,x]),{y,x,1}],{x,0,1}]
Mathematica starts hanging when n is greater than 2 and c is greater than 3 or so (generally as both n and c get a little higher).
Are there any tricks for rewriting this expression so that it can be evaluated more easily? I've played with different WorkingPrecision and AccuracyGoal and PrecisionGoal options on the outer NIntegrate, but none of that helps the inner integral, which is where the problem is. In fact, for the higher values of n and c, I can't even get Mathematica to expand the inner derivative, i.e.
Expand[D[a[4,6,y],y]]
hangs.
I am using Mathematica 8 for Students.
If anyone has any tips for how I can get M. to approximate this, I would appreciate it.
Since you only want a numerical output (or that's what you'll get anyway), you can convert the symbolic integration into a numerical one using just NIntegrate as follows:
Clear[a,myIntegral]
a[n_Integer?Positive, c_Integer?Positive, x_] :=
a[n, c, x] = (1 - (1 - a[n - 1, c, x])^c)^c;
a[0, c_Integer, x_] = x;
myIntegral[n_, c_] :=
NIntegrate[D[a[n, c, y], y]*y/(1 - a[n, c, x]), {x, 0, 1}, {y, x, 1},
WorkingPrecision -> 200, PrecisionGoal -> 5]
This is much faster than performing the integration symbolically. Here's a comparison:
yoda:
myIntegral[2,2]//Timing
Out[1]= {0.088441, 0.647376595...}
myIntegral[5,2]//Timing
Out[2]= {1.10486, 0.587502888...}
rcollyer:
MyIntegral[2,2]//Timing
Out[3]= {1.0029, 0.647376}
MyIntegral[5,2]//Timing
Out[4]= {27.1697, 0.587503006...}
(* Obtained with WorkingPrecision->500, PrecisionGoal->5, MaxRecursion->20 *)
Jand's function has timings similar to rcollyer's. Of course, as you increase n, you will have to increase your WorkingPrecision way higher than this, as you've experienced in your previous question. Since you said you only need about 5 digits of precision, I've explicitly set PrecisionGoal to 5. You can change this as per your needs.
To codify the comments, I'd try the following. First, to eliminate infinite recursion with regards to the variable, n, I'd rewrite your functions as
F[x_, c_] := (1 - (1-x)^c)^c;
(* see note below *)
a[n_Integer?Positive, c_, x_] := F[a[n - 1, c, x], c];
a[0, c_, x_] = x;
that way n==0 will actually be a stopping point. The ?Positive form is a PatternTest, and useful for applying additional conditions to the parameters. I suspect the issue is that NIntegrate is re-evaluating the inner Integrate for every value of x, so I'd pull that evaluation out, like
MyIntegral[n_,c_] :=
With[{ int = Integrate[(D[a[n,c,y],y]*y)/(1-a[n,c,x]),{y,x,1}] },
NIntegrate[int,{x,0,1}]
]
where With is one of several scoping constructs specifically for creating local constants.
Your comments indicate that the inner integral takes a long time, have you tried simplifying the integrand as it is a derivative of a times a function of a? It seems like the result of a chain rule expansion to me.
Note: as per Yoda's suggestion in the comments, you can add a cacheing, or memoization, mechanism to a. Change its definition to
d:a[n_Integer?Positive, c_, x_] := d = F[a[n - 1, c, x], c];
The trick here is that in d:a[ ... ], d is a named pattern that is used again in d = F[...] cacheing the value of a for those particular parameter values.

Resources