Hot answers tagged implementation-details
29
Interpolation function methods
Interpolation supports two methods:
Hermite interpolation (default, or Method->"Hermite")
B-spline interpolation (Method->"Spline")
Hermite method
I really can't find any good reference to Hermite method within Mathematica's documentation. Instead, I recommend you to take a look at this Wikipedia article.
The ...
22
Link to the code on GitHub
I have been using this. It's mostly Leonid's code from the stackoverflow question you linked to, but it uses Definition instead of DownValues. Symbol names are printed without any context, but the full symbol name is put into a Tooltip so you can always find out what context a symbol is in.
Update
FullDefinition[symbol] claims ...
18
What you observed seems to be an instance of the general behavior of the pattern-matcher when used with what I call "syntactic patterns" - patterns which only reflect the rigid structure of an expression, like e.g. _f. The speed-up with respect to the scanning is because the main evaluation loop is avoided - for FreeQ and MemberQ, the scannng is done all ...
15
Major update at the bottom. First part may be obsolescent.
A brute force approach:
Define a function that provides a measure of the difference between the automatically adjusted image and an image with given contrast, brightness and gamma adjustments (for now, this only works for images that are made of a raster of color triplets):
ClearAll[f];
...
14
I know this isn't exactly what you want, but just a stupid idea:
ClearAll[newf];
points = RandomReal[1, {1000000}];(*we have lots of points...*)
nf = Nearest[points];(*... and the corresponding NearestFunction*)
newf[oldf_, newpoints_List] := (Nearest[Union[oldf[#], Nearest[newpoints][#]], #] &);
newf[nf, {3, 4, 5}][1.98]
Edit
Here is a version that ...
13
As I suggested in my answer to a related packed-array question, the main problem is IMO not in the data structure (packed array) per se, but in all the functions which must work with this data structure together and in concert, to make it really well-integrated into the language. Notice that there isn't a separate boolean atomic type in Mathematica, True and ...
11
I've always considered the "suitable for symbolic manipuation" line to be a bit of truth wrapped in marketing speak and not meant to mean anything mathematically precise. The documentation center guides and tutorials are good examples of hyperbole in technical documentation (see for instance, the opening lines in Mathematical Typesetting).
Coming to the ...
11
As Danny points out, you can find the list of licensed software using the "About Mathematica..." box, available under the Mathematica menu on my Macintosh and under the Help menu for a PC. As a dialog box, that Notebook is not searchable and a bit inconvenient to work with. If we open it with a text editor and examine the contents, we find that the cells ...
8
I would just use strings, for all their fragility:
ClearAll[print];
print[sym_, {conts_String}] :=
With[{altptrn = Alternatives @@ Reverse[SortBy[{conts}, StringLength]]},
Print@StringReplace[ToString[InputForm@FullDefinition@sym],
(x : (_ | "") ~~ altptrn ~~ y : (_ | "")) /; ! (x === "\"" && y === "\"") :>
...
8
(nextPrime[#1] = #2) & @@@ {{-3, 2}, {-2, 2}, {-1, 2}, {0, 2}, {1, 2}, {2, 3}};
nextPrime[n_Integer?EvenQ] := nextPrime[n - 1];
nextPrime[n_Integer] /; PrimeQ[n + 2] := n + 2;
nextPrime[n_Integer] := nextPrime[n + 2]
nextPrime[n_ /; n \[Element] Reals] := nextPrime[Floor@n]
8
It's quite easy to show that Fold doesn't use the memory that would be required to store intermediate results.
$HistoryLength = 0;
big = Range[1*^7];
ByteCount[big]
MaxMemoryUsed[] //N
40000124
5.49458*10^7
Fold[# + 1 &, big, Range@100];
MaxMemoryUsed[] //N
1.34909*10^8
FoldList by comparison (with a much shorter Range):
FoldList[# + ...
8
Since there are two parts to your question. I will address the one directly dealing with Binomial.
For the purposes of discrete mathematics, the binomial is defined through its generating function:
$$
(1+x)^{\alpha} = \sum_{m=0}^\infty \binom{\alpha}{m} x^m
$$
It makes evaluations of sums using generating functions much easier if the sum were to run ...
8
I can only direct you to Some Notes on Internal Implementation:
Differentiation and Integration
Differentiation uses caching to avoid recomputing partial results.
For indefinite integrals, an extended version of the Risch algorithm
is used whenever both the integrand and integral can be expressed in
terms of elementary functions, ...
7
It uses a resultant computation. The idea is this. We are given algebraic numbers $x$ and $y$, where $p(x)=0$ and $q(y)=0$ are the minimal polynomials. We want to find the defining polynomial for $z=x+y$. We use $p(x)=p(z-y)$ and $q(y)$, and eliminate $y$ using the classical method of resultants.
Here is how it would go for your example.
p[x_] := #^5 - # - ...
7
I e-mailed support at Wolfram Research and recieved the following response:
"Spectral graph drawing methods construct the layout using eigenvectors of certain matrices associated with the graph (Laplacian matrix). Reference: Hall, K.M. "An r-dimensional Quadratic Placement Algorithm.", Management Science 17, pp. 219-229 (1970)."
Here's a link to the ...
7
After some work and clarification from Leonid it becomes clear this is a case where SubValues is the exact solution. As this answer points out SubValues are patterns of the form
food[d][f] := a;
which is the correct form for accessing parts of an "data-like" object since the sub value has access to the containing expression parts.
Now to build on a ...
6
Given a large n, to find k largest primes below n (as well as above) the best approach uses NextPrime (it has been added to Mathematica 6) :
NextPrime[n] gives the next prime above n.
NextPrime[n,k] gives the k-th prime above n. If k is negative it gives k-th largest prime below n.
k need not be a single number but it may be a list of ...
6
Addendum
If you just want the greatest 10 primes less than M, you can start from Prime[PrimePi[M]-9]. By doing so, you gain a speed increase of 2 orders of magnitude when M = 100000.
M = 100000;
m = PrimePi[M]
AbsoluteTiming[Table[Prime[k], {k, m - 9, m}]]
9592
{0.000171, {99877, 99881, 99901, 99907, 99923, 99929, 99961, 99971, 99989, 99991}}
Now ...
6
As suggested in the comments, having N outside of Nest is the cause of the problems.
ListLogPlot[{
{#, Timing[N[Nest[(# + 2/#)/2 &, 1, #], 123];][[1]]} & /@ Range[22],
{#, Timing[Nest[N[(# + 2/#)/2, 123] &, 1, #];][[1]]} & /@ Range[22]
}, Frame -> True, Joined -> True, PlotMarkers -> Automatic]
gives
I suspect the ...
6
Following R.M's suggestion, and shamelessly lifting code from the Wizard’s fine answer there, you can use Stack[] and get the following:
SetAttributes[withTaggedMsg, HoldAll]
withTaggedMsg[] :=
Function[,
Internal`InheritedBlock[{MessagePacket}, Unprotect[MessagePacket];
MessagePacket[name__, BoxData[obj_, form_]] /; ! TrueQ[$tagMsg] :=
...
6
NearestFunction seems to have a structure that can be guessed at:
pts = {{1, 0}, {0, 2}, {3, 1}, {-1, 4}};
nf = Nearest[pts];
List @@ nf
{1, (* unknown *)
{4, 2}, (* number of points, dimension *)
3, (* unknown *)
{{1., 0., 3., -1.}, {0., 2., 1., 4.}}, (* transpose of points as Real *)
{{1, 0}, {0, 2}, {3, 1}, {-1, 4}}, (* original points *)
None, ...
5
You can use === (SameQ). Indeterminate is a special head, so that Equal (==) on it remains unevaluated:
Indeterminate == Indeterminate
(* Indeterminate == Indeterminate *)
while
Indeterminate === Indeterminate
(* True *)
5
As long as we're giving methods that mimic the behavior, here is a quick implementation based on Kronecker's theorem.
RoUQ[u_] :=
If[! (Abs[N[u]] == 1), False,
If[! AlgebraicIntegerQ[u], False,
(f = MinimalPolynomial[u, x];
n = Exponent[f, x];
cf = CoefficientList[f, x]/Coefficient[f, x^n];
M = Table[
If[j == i + 1, 1,
If[i ...
5
You can check here for Function Approximations Package References
References
Hairer, E. and G. Wanner. Solving Ordinary Differential Equations II: Stiff and Differential-Algebraic Problems. Springer, 1991.
Iserles, A. and S. P. Nørsett. Order Stars. Chapman and Hall, 1991.
Lambert, J. D. Numerical Methods for Ordinary Differential Systems:
The Initial ...
5
With only one argument, ImageAdjust[img] performs histogram stretching, ie. pixel values, which run from min to max in img, are rescaled so they run from 0 to 1 in the ouput image.
The setting {contrast, brightness, gamma} = {0, 0, 1} does not change anything:
ImageAdjust[img, {0, 0, 1}] === img gives True.
4
For reference, here is the v7 code behind NextPrime, which is hard to read before stripping all the private context names.
NextPrime[1]; (* preload the definition *)
Unprotect[NextPrime];
ClearAttributes[NextPrime, ReadProtected];
$Context = "NumberTheory`NextPrimeDump`";
FullDefinition[NextPrime]
Yields:
Attributes[NextPrime] = {Listable}
NextPrime[-3] ...
4
I have not tested this yet but here is one possible approach:
contextFreeDefinition[sym_Symbol, contexts_List] :=
Internal`InheritedBlock[{sym},
ClearAttributes[sym, ReadProtected];
If[contexts =!= {}, Message[contextFreeDefinition::contexts, contexts]];
Block[{ipf = ToString @ InputForm @ FullDefinition @ sym},
ipf = ...
4
If Abs[x]==1 and Element[Arg[x]/(2 Pi), Rationals], then the number is a root of unity
$$ \arg[x]= 2 \pi \frac{p}{q}$$
$p,q\in \mathbb{Z}$
and
$$ |x|=1 $$
$$ \to x^q=1 $$
because
$ |x^q|=|x|^q=1^q=1 $
and
$ \arg[x^q] = \arg[x] q = 2 \pi p $
So perhaps this mimics the behaviour
ru = TrueQ[Abs[#] == 1 && Simplify@Element[Arg[#]/(2 Pi), Rationals] ...
4
Here is a variation that is a bit on the slow side but will at least scale fairly well. The idea is to use buckets of NearestFunction objects, as well as a list of "lone" points, and take closest neighbors from amongst neighbors obtained from these separately. Adding new points gets amortized in the sense that usually we add to the "lone" list, then empty ...
4
There are many unrelated methods that FindShortestTour can use. Check the documentation undr Details please:
Possible settings for the Method option include "AllTours", "CCA",
"Greedy", "GreedyCycle", "IntegerLinearProgramming", "OrOpt",
"OrZweig", "RemoveCrossings", "SpaceFillingCurve",
"SimulatedAnnealing", and "TwoOpt".
For small numbers ...
Only top voted, non community-wiki answers of a minimum length are eligible



