# How does Rescale[] handle infinities?

Rescale appears to be a simple function. It just does a simple linear $y = a x + b$ type rescaling of the values:

Rescale@Range[0, 10]
(* {0, 1/10, 1/5, 3/10, 2/5, 1/2, 3/5, 7/10, 4/5, 9/10, 1} *)


Or does it? The documentation never says that it's linear. What happens if the input has an infinite quantity?

Rescale[Prepend[Range[10], -Infinity]]
(* {0, 1/10, 1/9, 1/8, 1/7, 1/6, 1/5, 1/4, 1/3, 1/2, 1} *)

ListPlot[%]


That's a little strange. The output numbers are no longer equispaced.

With both $-\infty$ and $\infty$ present, we get things like

Rescale[{-Infinity, 0, 1, 2, Infinity}]
(* {0, 1/2, 1/2 (-1 + Sqrt[5]), 1/Sqrt[2], 1} *)


What does Rescale do when infinities are present? What's the justification for this behaviour? Where is it documented?

We should note that Rescale[list] is really Rescale[list, {Min[list], Max[list]}], so it's in fact the infinities in the second argument that trigger this. Such behaviour is acceptable if it's documented and if it's explicitly requested by putting infinities in the second argument. But in my situation an infinite quantity slipped into the input only by accident, and I suddenly started getting unexpected strange results.

• Very surprising. Have a look at the results of Rescale[x, {a, ∞}], Rescale[x, {-∞, b}], and Rescale[x, {-∞, ∞}]… how did you encounter this? – J. M. will be back soon Jun 19 '15 at 12:14
• @J.M. Actually it's very boring: 1. make visualization functions that needs to transform numbers to colours, thus it needs to rescale to $[0,1]$ 2. try to visualize the logarithm of some data and forget to remove the zeros, which introduces -Infinity 3. stare at the result and be surprised about how the colours have just changed – Szabolcs Jun 19 '15 at 12:16
• Okay, that's the application I did have in mind. I tried the three I gave in my comment in Alpha; quite a surprise! – J. M. will be back soon Jun 19 '15 at 12:19
• The last one works (have a look at its Taylor expansion), but conventionally I've tended to use the logistic function (see e.g. the Gaussian curvature coloring question I answered a while back), arctangent, or hyperbolic tangent myself. I'd say the choices do seem rather arbitrary. – J. M. will be back soon Jun 19 '15 at 12:23
• It's a bit disturbing that Rescale automatically and unexpectedly (no warning!) switches algorithm if a single Infinity creeps into the input, while functions where infinities don't matter at all, such as DiagonalMatrix, error out: try DiagonalMatrix[{-Infinity, 0}], it fails. The truly nonsensical DiagonalMatrix[{"asd", Graphics@Circle[]}] on the other hand works. – Szabolcs Jun 19 '15 at 12:35

The answers of the original questions by Szabolcs:

What does Rescale do when infinities are present? What's the justification for this behaviour? Where is it documented?

were guessed correctly with the comment:

If I may be allowed to speculate, these were picked because they do the job advertised and are "conveniently" algebraic. They certainly work splendidly when used as variable substitutions for integrals with infinite limits. – J. M.

I programmed Rescale in the Mathematica kernel both in C and in top level. Originally, the request for Rescale was derived from image manipulation cases in FrontEnd. It is obviously useful otherwise. I wanted the Rescale functionality for top level implementation of integration algorithms in the NIntegrate framework. See the numerous uses of Rescale in the advanced NIntegrate documentation. For example, in NIntegrate Integration Strategies.

Generally speaking, within NIntegrate's framework I wanted to use Rescale for converting infinite regions and for handling singularities.

After some discussions the Rescale transformations of ranges with Infinity were left implemented but undocumented. The main reasons being that (1) there is a good adherence with Rescale for finite intervals, and (2) there are too many such mathematical transformations and the selection of the particular one implemented in Rescale is hard to explain.

(Sorry for my delayed reply. I found this question by accident today.)

Edit

Here is some code that shows Rescale formulas for different combinations of ranges:

 r1pairs = {{a, b}, {-∞, b}, {a, ∞}, {a, -∞}, {-∞, ∞}};
r2pairs = {{c, d}, {-∞, d}, {c, ∞}, {c, -∞}, {-∞, ∞}};
tbl = Table[Simplify @ Rescale[x, r1, r2], {r1, r1pairs}, {r2, r2pairs}];
Prepend[r2pairs, ""]], Alignment -> Left, Dividers -> All,
Background -> {{GrayLevel[0.95], White}, {GrayLevel[0.95], White}}]


The result should be something like this:

(The ranges that are the second Rescale argument are given in the first column; the ranges that are the third Rescale argument are given on the first row.)

• Anton, welcome to Mathematica.StackExchange and thank you for your contribution! – Mr.Wizard Sep 10 '15 at 20:58
• @Anton From the other answers it is not immediately clear what the transformation Rescale uses for ranges with Infinity. Could you please include the formula into your answer for convenience? – Alexey Popkov Sep 11 '15 at 3:06
• @Mr.Wizard Thanks! – Anton Antonov Sep 15 '15 at 3:37
• @AlexeyPopkov I added a table with formulas and code that generates it. (Can be executed for other ranges of interest.) – Anton Antonov Sep 15 '15 at 3:46

This functionality is undocumented, but evident when Spelunking:

Rescale[x, {DirectedInfinity[-1], DirectedInfinity[1]}]
(* (-2 + x + Sqrt[4 + x^2])/(2 x) *)


Rescale[x, {-Infinity, Infinity}] is equivalent to Rescale[x, {-Infinity, Infinity}, {0, 1}]. The relevant entry in the definition when spelunking:

Needs["Spelunk"]
Spelunk[Rescale]


This returns a number of replacement rules for Rescale with infinities. The relevant rule for my example is:

Rescale[x_, {DirectedInfinity[d1_],
DirectedInfinity[d2_]}, {a : Except[_DirectedInfinity],
b : Except[_DirectedInfinity]}] /; !
TrueQ[{d1, d2} == {-1, 1} || {d1, d2} == {1, -1}] :=
Block[{valid, res},
valid = SystemDumpvalidDoublyInfiniteComplexRangeQ[d1, d2];
If[valid,
res = Rescale[x/(d2/Abs[d2]), {-\[Infinity], \[Infinity]}, {a, b}]];
res /; valid]


As you can see, however, the rule applies if, and only if, the infinities are not "negative-to-positive" or "positive-to-negative". Failing that, the offset in their direction is accounted for as a complex multiplier added to x and the problem is reduced to rescaling a negative-to-positive infinity.

It seems, that Rescale reduces every problem with infinities to a problem such as Rescale[x, {-Infinity, Infinity}, {0, 1}] or similar. There is not an explicit rule, however, listed for this problem. It seems, that somewhere deeper down, there is a built-in rule that does not show up with Spelunk that says "once we have reduced our problem to Rescale[x, {-Infinity, Infinity}, {0, 1}], return (-2 + x + Sqrt[4 + x^2])/(2 x). This is made evident by

Trace[Rescale[x, {-\[Infinity], \[Infinity]}]]

... Rescale[x, {-\[Infinity], \[Infinity]}], (-2 + x + Sqrt[4 + x^2])/(2 x)


So in a few words, almost all of the functionality with infinities is "documented" (in a hidden way, accessible only through spelunking), except the final transformations from the basic cases such as the one I described to the functions of x, which does not even show up in the output of Spelunk.

• Have you tried Trace[]-ing the two other examples in my first comment? What comes up? – J. M. will be back soon Jun 19 '15 at 14:53
• @J.M. Nothing. I have actually looked at the definition before posting here, noticed that the main case is not accessible, and tried tracing with TraceInternal. I wouldn't really say "code can be decompiled sans comments or formatting" is equivalent to "documented". – Szabolcs Jun 19 '15 at 15:10
• @Szabolcs, "…'code can be decompiled sans comments or formatting' is equivalent to 'documented'…" - I don't consider 'em equivalent either. One would hope something along the lines of "…if the lower or upper limit is a DirectedInfinity[], Rescale[] uses a different formula…" was in the docs. Feh… – J. M. will be back soon Jun 19 '15 at 15:15
• If I may be allowed to speculate, these were picked because they do the job advertised and are "conveniently" algebraic. They certainly work splendidly when used as variable substitutions for integrals with infinite limits. – J. M. will be back soon Jun 19 '15 at 15:19
• @Szabolcs regarding "code can be decompiled sans comments or formatting" is equivalent to "documented". I figured it's appropriate to add sarcastic quotation marks to my use of the word documented. The final replacement rules are even less "documented" though. It's a shame, rescaling infinite ranges is very useful for NDSolve` with infinite ranges. – LLlAMnYP Jun 19 '15 at 15:26