64
$\begingroup$

Sometimes, I use Mathematica to do some hypothesis on homework to make the question easier. For instance, when I have to compute big sums when $n\to\infty$ and Mathematica can't give the exact answer, I set $n$ to a very big number and I get an approximate value.

Is it possible with this value to get possible closed forms just like in Wolfram Alpha (which by the way runs Mathematica's kernel) ?

Example :

I have to find out the sum of all $\frac{1}{2p!}$ when $p$ goes from $0$ to $+\infty$ and let's say Mathematica doesn't directly say it's $\frac{e}{2}$, but it gives 1.66 for a big value of $n$. Is there a function which can figure out which constant is near this number?

$\endgroup$
2
  • $\begingroup$ Not exactly related to your question, but if you have to calculate something as n towards some limit, you can use the Limit function. $\endgroup$ Jan 17, 2012 at 23:15
  • $\begingroup$ I know this. In fact, in Mathematica, you can just use Infinity directly in Sum if you want to get the limit of a sum like the one I showed. $\endgroup$
    – Cydonia7
    Jan 18, 2012 at 11:43

11 Answers 11

37
$\begingroup$

I can offer a round-about method.

First compute the numerical approximation. I obtain, to high precision,

In[24]:= N[Sum[1/(2*n!), {n, 0, 100}], 100]

Out[24]= 1.\
3591409142295226176801437356763312488786235468499797874834838138620383\
15176773797285691089262583214

Now paste that into a Wolfram|Alpha query, accessed by clicking on the '+' sign at upper left of a fresh input cell. This gives, among other things, possible closed forms.

To the best of my knowledge, the heuristic methods used by W|A for this task are not directly exposed in any other way in Mathematica proper.

$\endgroup$
4
  • $\begingroup$ So I'll use Wolfram for this issue. Thank you for your answer ! $\endgroup$
    – Cydonia7
    Jan 17, 2012 at 22:10
  • 13
    $\begingroup$ In Mathematica 8: WolframAlpha[ToString@N[Sum[1/(2*n!), {n, 0, 100}], 100], IncludePods -> "PossibleClosedForm", AppearanceElements -> {"Pods"}] $\endgroup$
    – ragfield
    Jan 17, 2012 at 22:22
  • 9
    $\begingroup$ How does Wolfram Alpha compare to the inverse symbolic calculator? isc.carma.newcastle.edu.au/standard $\endgroup$
    – Phira
    Jan 18, 2012 at 17:38
  • 2
    $\begingroup$ @Phira It would also be interesting to see how the AskConstants package compares to ISC... $\endgroup$
    – Simon
    Jan 7, 2017 at 23:53
42
$\begingroup$

Here's some code that I used recently, based on code by Paul Abbott [1, 2].

Clear[TranscendentalRecognize]
TranscendentalRecognize[num_Real, basis_List, ord_?Positive, debug:(True|False):False] := 
 Module[{vect, mat, lr, ans}, 
  vect = Round[10^Floor[ord - 1] Join[{num}, N[basis, ord]]];
  mat = Append[IdentityMatrix[Length[vect]], vect];
  lr = LatticeReduce[Transpose[mat]];
  If[debug, Print[lr // TableForm]];
  (* If a row of lr starts with zero, then it's just a relationship between the basis
     element. We move such rows to the end. *)
  While[lr[[1, 1]] === 0, lr = RotateLeft[lr]];
  (* Now that the first element of the first row is nonzero, we choose it as our best
     solution and normalize the answer *)
  ans = First[lr[[1]]]^-1 Most[Rest[lr[[1]]]].basis;
  Sign[N@ans] Sign[num] ans]

TranscendentalRecognize[num_Real, basis_List] :=
           TranscendentalRecognize[num, basis, Precision[num]]

Then, we can identify your number using something like

num = Sum[1/(2`50*n!), {n, 0, 100}]
TranscendentalRecognize[num, {E, Pi, EulerGamma}]
(* Returns
1.3591409142295226176801437356763312488786235468500
E/2 *)

Compare with

WolframAlpha[ToString[num], IncludePods -> "PossibleClosedForm"]

This code can be cleaned up a little and made more efficient by using the PSLQ based algorithm FindIntegerNullVector introduced in Mathematica version 8.

This is probably something like that which Wolfram|Alpha does. It just tries a combination of various common transcendentals until it comes up with something that looks close. To see some code on how you might write something like that, look at the mpmath (Python) implementation - Number Identification. For example, here's a quick IPython session:

In [1]: from mpmath import *

In [2]: mp.dps = 20;

In [3]: identify(1.35914091422952261768014, ['e', 'pi'])
Out[3]: '((1/2)*e)'
$\endgroup$
5
  • $\begingroup$ Nice snippet of code but you still have to give the constant you are thinking of as an argument. $\endgroup$
    – Cydonia7
    Jan 17, 2012 at 22:35
  • 1
    $\begingroup$ @Skydreamer: Thanks. For some reason I thought that mpmath used heuristics to help guess the basic constants, much in the same way that W|A does. But it looks like I was wrong.... Of course, there's always the ISC and OEIS to help you guess. $\endgroup$
    – Simon
    Jan 17, 2012 at 22:44
  • 5
    $\begingroup$ Somehow, this having less upvotes than "ask Wolfram Alpha" bothers me... $\endgroup$
    – acl
    Jan 18, 2012 at 13:18
  • $\begingroup$ @acl: Well, my answer doesn't completely satisfy the OP's requirements. Also, both Paul and Daniel played with LLL based transcendental number recognition in Mma way back then (only Paul published code). So I don't mind Daniel getting more upvotes! $\endgroup$
    – Simon
    Jan 18, 2012 at 21:05
  • $\begingroup$ It's not Daniel (or any other person) I am bothered with... $\endgroup$
    – acl
    Jan 18, 2012 at 21:11
19
$\begingroup$

Following up on Simon's note in his answer:

This code can be cleaned up a little and made more efficient by using the PSLQ-based algorithm FindIntegerNullVector[] introduced in Mathematica version 8.

here is a re-implementation of Abbott's TranscendentalRecognize[]:

TranscendentalRecognize[num_?NumericQ, basis_?VectorQ] := Module[{lr, ans},
  lr = FindIntegerNullVector[Prepend[N[basis, Precision[num]], num]];
  ans = Rest[lr].basis/First[lr];
  Sign[N[ans]] Sign[num] ans]

As already noted, this will only work for recognizing linear combinations of any constant given in the list basis (it thus can't be used to recognize $\pi/e$ from its decimal expansion, for instance).

To use the same example:

TranscendentalRecognize[1.35914091422952261768014, {Pi, E}]
   E/2
$\endgroup$
2
  • $\begingroup$ Nice snippet, I just can't understand why it works but that's nice ! $\endgroup$
    – Cydonia7
    Oct 3, 2012 at 16:00
  • 3
    $\begingroup$ Well, discussing the theory of PSLQ will take a fair amount of deep math; suffice it to say that FindIntegerNullVector[] tries its best to find integers $c_1,\dots,c_n$ such that given constants, $a_1,\dots,a_n$, $c_1 a_1+\cdots+c_n a_n$ is as near to zero as possible. $\endgroup$ Oct 3, 2012 at 16:07
18
$\begingroup$

I asked a similar question on MathGroup some years ago.

If you read that thread, you'll get some pointers about how these algorithms work (they're based on lattice reduction, see also here).

  • There's a package by Eric Weisstein for doing something like this: see ToExact and TranscendentalRecognize in Simplify.m.

  • This issue of the Mathematica Journal is relevant too (see Tricks of the Trade -- Transcendental Recognition)

  • Plouffe's inverter may also be of interest

$\endgroup$
2
  • $\begingroup$ Really nice brainstorming on this topic. Thank you ! $\endgroup$
    – Cydonia7
    Jan 24, 2012 at 15:52
  • $\begingroup$ The link to Eric Weisstein package seems dead now. Here is a cached version from Web Archive: tinyurl.com/y6c7kjcd $\endgroup$ Aug 7, 2019 at 20:53
12
$\begingroup$

David Stoutemyer and Dale Myers have released a package that does a good job of identifying exact forms from their approximate values. It can be downloaded from AskConstants.org
David presented the package at the Wolfram Tech Conference 2016.

You still need to choose which functions and constants you want to include in the search, but it has a good default starting set. It also supplies a nice graph showing the proposed exact forms and how much merit they have. Screenshot of application working

$\endgroup$
1
  • $\begingroup$ That looks pretty good! $\endgroup$
    – Cydonia7
    Jan 9, 2017 at 6:36
10
$\begingroup$

Here's a variant of Danny's answer, that's perhaps a bit more automated:

WolframAlpha[ToString[
  NSum[1/(2*n!), {n, 0, Infinity}, WorkingPrecision -> 50]],
 {{"PossibleClosedForm", 1}, "ComputableData"}]
$\endgroup$
10
$\begingroup$

This answer doesn't use Mathematica, but another great resource for this sort of thing is the Online Encyclopedia of Integer Sequences. I've used it successfully many times when Mathematica has given me a decimal approximation but I've been after an analytical expression. Among other things, the encyclopedia includes the decimal expansions of a huge number of exact quantities. For your example, the following search returns the result 'E/2':

Search OEIS for 3,5,9,1,4,0,9,1

You can of course also include the leading 1 in your search, but I've found it useful in the past not to -- if it turned out that the number you had was actually 2+e/2, for example, the above search would still let you determine that the decimal portion came from e/2, whereas the OEIS won't include the expansion of n+e/2 for any non-zero integers n.

$\endgroup$
2
9
$\begingroup$

Rationalize may be an option, although I doubt it is as powerful as what WA is doing. Here's the documentation.

$\endgroup$
3
  • 5
    $\begingroup$ If need be, RootApproximant[] does the job for general algebraic numbers. $\endgroup$ Jan 17, 2012 at 22:16
  • $\begingroup$ Rationalize returns a rational number (fraction). But we know that the exact solution to the problem of the o/p is not rational, and the question is asking about a general approach, and in general the solution is not rational. I'm almost ready to downvote this reply. $\endgroup$ Jun 29, 2012 at 16:02
  • $\begingroup$ @AndreasLauschke the question is "can it propose closed forms", and what I suggest does. That this won't work for an example doesn't make it not an answer to the general question. But of course you are free to downvote anything you want. $\endgroup$
    – acl
    Jun 29, 2012 at 17:18
5
$\begingroup$

This question reminds me somewhat of a post on the Wolfram Blog about finding rational approximations to Pi. It may be of interest to you.

The only way I know to approach this sort of thing is by genetic programming. Expressions in Mathematica can be manipulated like trees. You could generate a population of random expressions, see how closely they come to your approximation, then take the best few, change them slightly, and repeat again and again.

In this way you search the space of "nice" exact expressions built from some building blocks. I imagine, however, that this would be a difficult algorithm to write.

$\endgroup$
2
  • $\begingroup$ Interesting blog post, I'll read about this but I don't think I'm gonna try to implement this, that's just... hard ! $\endgroup$
    – Cydonia7
    Jan 17, 2012 at 22:16
  • $\begingroup$ something along these lines: stackoverflow.com/a/8483298/559318 may also be fun to play with. $\endgroup$
    – acl
    Jan 17, 2012 at 22:16
5
$\begingroup$

This is extremely hacky, because I don't know how to do proper html processing in Mathematica. The following looks looks up the value on the Inverse Symbolic Calculator and returns the best match.

InverseSymbolic[x_] := 
 Module[{url, result, start, end, preprocess, guess, formatted},
  url = "http://isc.carma.newcastle.edu.au/standardCalc";
  result = StringJoin[Flatten[Import[url,
      "RequestMethod" -> "POST",
      "RequestParameters" -> {"input" -> ToString@x}]]];

  preprocess  = 
   StringReplace[
    result, {"Best guess: " -> "!RESULT!", "</h2>" -> "!RESULT!"}];
  {start, end} = StringPosition[preprocess, "!RESULT!"][[1 ;; 2]];
  guess = StringTake[preprocess, {Last@start + 1, First@end - 1}];

  formatted = StringReplace[guess, {"(" -> "[", ")" -> "]"}];

  If[SyntaxQ[formatted],
   ToExpression[formatted],
   If[SyntaxQ[guess], 
    ToExpression[guess],
    guess]
   ]
  ]

This relies on the fact that the "best guess" is located between the strings Best guess and </h2> within the HTML.

It first replaces these with a temporary marker to delineate the answer from the rest of the text. After the replacement, it finds the position of the markers and returns what's in between it.

Finally, it tries to replace parenthesis with square brackets and sees if that's syntactically correct. If not, it tries the one with parenthesis. If either of those two are fine as Mathematica statements, it converts them to expressions. Otherwise, it just returns it as a string.

$\endgroup$
2
  • $\begingroup$ Nice! However, it does not fail very elegantly when ISC returns "Standard inverse calculate found nothing." $\endgroup$
    – Simon
    Jan 19, 2012 at 7:29
  • $\begingroup$ @Simon: Like I said it's very hacky :( There's a lot more I want to fix with this. $\endgroup$ Jan 19, 2012 at 7:31
1
$\begingroup$

Use the built in function Sum:

Sum[1/(2 p!), {p, 0, Infinity}]

Mathematica outputs E/2.

$\endgroup$
9
  • 2
    $\begingroup$ My question is about sums it can't compute. I gave a simple example but there are harder sums that don't give an exact result. $\endgroup$
    – Cydonia7
    Jan 17, 2012 at 21:49
  • $\begingroup$ If you want Wolfram|Alpha like results, you can always use WolframAlpha["Sum[1/(2 p!),{p,0,Infinity}]"] $\endgroup$
    – Eli Lansey
    Jan 17, 2012 at 21:52
  • $\begingroup$ @Eli I think the question being asked is "can we get Mathematica to propose a possible exact number based on an approximation to it?" $\endgroup$
    – JOwen
    Jan 17, 2012 at 21:53
  • $\begingroup$ @Sky: something like a Mathematica interface for the ISC, then? $\endgroup$ Jan 17, 2012 at 21:53
  • 2
    $\begingroup$ @Sky: oops. Here is the new location. $\endgroup$ Jan 17, 2012 at 22:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.