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} *)


Mathematica graphics

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} *)

I would have expected some error messages about indeterminate results instead.

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.

  • $\begingroup$ Very surprising. Have a look at the results of Rescale[x, {a, ∞}], Rescale[x, {-∞, b}], and Rescale[x, {-∞, ∞}]… how did you encounter this? $\endgroup$ – J. M. is computer-less Jun 19 '15 at 12:14
  • 1
    $\begingroup$ @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 $\endgroup$ – Szabolcs Jun 19 '15 at 12:16
  • $\begingroup$ Okay, that's the application I did have in mind. I tried the three I gave in my comment in Alpha; quite a surprise! $\endgroup$ – J. M. is computer-less Jun 19 '15 at 12:19
  • 1
    $\begingroup$ 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. $\endgroup$ – J. M. is computer-less Jun 19 '15 at 12:23
  • 3
    $\begingroup$ 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. $\endgroup$ – 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.)


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}];
Grid[Prepend[MapThread[Prepend, {tbl, r1pairs}], 
  Prepend[r2pairs, ""]], Alignment -> Left, Dividers -> All, 
 Background -> {{GrayLevel[0.95], White}, {GrayLevel[0.95], White}}]

The result should be something like this:

Rescale table

(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.)

  • 3
    $\begingroup$ Anton, welcome to Mathematica.StackExchange and thank you for your contribution! $\endgroup$ – Mr.Wizard Sep 10 '15 at 20:58
  • 1
    $\begingroup$ @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? $\endgroup$ – Alexey Popkov Sep 11 '15 at 3:06
  • $\begingroup$ @Mr.Wizard Thanks! $\endgroup$ – Anton Antonov Sep 15 '15 at 3:37
  • 1
    $\begingroup$ @AlexeyPopkov I added a table with formulas and code that generates it. (Can be executed for other ranges of interest.) $\endgroup$ – 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:


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 = System`Dump`validDoublyInfiniteComplexRangeQ[d1, d2];
   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.

  • $\begingroup$ Have you tried Trace[]-ing the two other examples in my first comment? What comes up? $\endgroup$ – J. M. is computer-less Jun 19 '15 at 14:53
  • 2
    $\begingroup$ @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". $\endgroup$ – Szabolcs Jun 19 '15 at 15:10
  • $\begingroup$ @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… $\endgroup$ – J. M. is computer-less Jun 19 '15 at 15:15
  • 2
    $\begingroup$ 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. $\endgroup$ – J. M. is computer-less Jun 19 '15 at 15:19
  • $\begingroup$ @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. $\endgroup$ – LLlAMnYP Jun 19 '15 at 15:26

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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