I.6 · Mathematical Language
Which curve is which?
Six curves carry most of applied mathematics. Learn to recognise each one, to see what its parameters do, and to know where it stops being defined or stops being computable.
Try it: six families on one plane
Draw these functions
Or start from
Coordinate plane from −7 to 7 on both axes. Vertical scale a is 1 and horizontal shift h is 0. The drawn curves are y equals a times g of x minus h for e to the x, natural log of x, sine of x. A vertical dashed line marks the input x = 1. Values at that input: e to the x: approximately 2.718; natural log of x: exactly 0; sine of x: approximately 0.841.
- input
- 1
- shifted input x − h
- 1
- a·e^(x − h)
- 2.718282
- a·ln(x − h)
- 0
- a·sin(x − h)
- 0.841471
= exact · ≈ rounded to 6 decimals · ⚠ undefined
At x = 1 the highest curve is eˣ (2.718282) and the lowest is ln x (0).
from −3 to 3, up to 2 decimal places
from −4 to 4, up to 2 decimal places
from −6 to 6, up to 2 decimal places
Try it before reading on. Switch the curves on one at a time and drag the input along the axis, then press who wins? and watch how quickly leaves the plane on the right. Three of these curves, , and , all climb; they do not climb in the same way. Switch on and drag to the left of zero: its row turns into a message, not a number. By the end of the chapter you will be able to say which curve is which from its shape, to read what two parameters do to any of them, and to say exactly where each stops being defined and where each stops being computable on a real machine.
Intuition: shape first
Most of the functions an engineer or a machine-learning practitioner meets are built from six simple shapes. Learn the shapes first; the formulas then explain them.
- The line, . Steady change: each step right adds the same amount. Its graph is straight.
- The parabola, . Change that speeds up away from the bottom. It is symmetric: and give the same value, so squaring forgets signs.
- The exponential, . Change in proportion to the amount already there. It is always positive, it never reaches the axis, and on the right it grows faster than any line or parabola, although not immediately.
- The logarithm, . The exponential read backwards: it asks what power of gives ? It exists only for , falls without bound as approaches , and climbs more and more slowly to the right.
- The sine and the cosine, and . A wave that repeats every and never leaves the band from to . The cosine is the sine started a quarter of a period earlier.
Here is the number for which the exponential has the pleasant property that its steepness at any point equals its height there. The book writes because is irrational and the digits are a rounding; the stage above uses the same rule, and it writes only for values that are exactly known.
Definition
All six share one pattern. Take a basic function from the list and two numbers and :
The number is the vertical scale: it stretches the graph away from the -axis, and a negative also flips it upside down. The number is the horizontal shift: the graph of is the graph of moved right by , which is the opposite of what the minus sign tempts you to expect (the value had at is now found at ).
Every function here has a domain, the inputs for which it exists, and a range, the outputs it can produce. These come from and the shift, and they are the first thing to know:
- and and and and accept every real number; accepts only , so exists only for , with a vertical asymptote at .
- The range of is the interval from to . The range of is the positive numbers when and the negative numbers when . The range of is every real number.
- If the result is the constant (where it exists), whichever you chose. The stage flags that as a special case, because the shape disappears.
An exponential has a small surprise hidden in this pattern: shifting it and scaling it are the same operation, because . Slide and watch the curve; then set close to (about for ) and the two curves become indistinguishable. Exactly equal needs , an irrational number that the two-decimal field cannot hold. No other family does this.
Mechanics: shifts, scales and inverses
Shifts and scales
Use the stage at the top (or the full copy in the Explore section) to see the two parameters act. Set only on, then raise : the curve and its vertical asymptote move right together, and the thick bar on the -axis, where has no value, grows with them. Set only the sine on and raise to : the range becomes and the zeros do not move, because a vertical stretch never moves the points where a function is . Raise instead and the zeros slide.
Inverse pairs and their honest domains
Two functions are inverses when each undoes the other. The exponential and the logarithm are one such pair, and squaring and the square root are another. On a graph, inverses are mirror images across the line , because undoing a function swaps the roles of input and output, and swapping coordinates is exactly that mirror.
The honest statement of the pair needs the domains:
The first holds everywhere because is defined and positive for every . The second needs because does not exist otherwise. The square-and-root pair has the same trap in a different place: , which equals only when , and only for , since does not exist for negative .
Undo it: a function and its inverse
Try these round trips
Coordinate plane from −5 to 5 on both axes with the exponential e to the x, the logarithm ln x, and the dashed line y = x: each graph is the mirror image of the other across that line. The point (1.5, 4.481689) lies on the graph of the first function and its mirror image (4.481689, 1.5) lies across the dashed line y = x.
- input
- 1.5
- step 1: eˣ (as a double)
- 4.481689
- step 2: ln (as a double)
- 1.500000
- distance from x
- 0
= exact (or the double the computer holds) · ≈ rounded to 6 decimals · ⚠ undefined
The point (1.5, 4.481689) and its mirror image (4.481689, 1.5) swap their coordinates.
ln(eˣ) = x for every real x. The identity holds at this x. The round trip returned x. ln undoes eˣ for every real x. The computer rounds eˣ once and ln once, so the result matches x to within a few units in the last place, which is all a double can promise.
from −800 to 800, up to 2 decimal places
Try the presets in turn. comes back, and so does : negative inputs are fine for the exponential. cannot even start, because the first step has no value. comes back as , not : the square forgot the sign, and the stage shows where the lost point lands on the other branch of the parabola. And the two overflow presets show that a round trip that is mathematically perfect can still fail on a computer; the row says so, labelled “as a double”.
The unit circle
Trigonometry begins with one circle. The unit circle is the circle of radius centred at the origin. Start at and walk counter-clockwise a distance along the circle; the point you reach is, by definition,
The distance walked, , is measured in radians: one full turn is because the circumference of a circle of radius is . So is a half-turn, a quarter-turn, and radians. Walking clockwise is a negative angle, matching the convention used for vector directions in the vectors chapter. Walking a whole turn more brings you back to the same point, which is why both functions repeat: .
Because the point lies on the circle, its coordinates satisfy : it is Pythagoras applied to the right triangle with the radius as hypotenuse.
Walk around the unit circle
Jump to an exact angle
The unit circle in a plane from −1.6 to 1.6 on both axes. The point at angle π/6 radians is in quadrant I. Its x-coordinate is cosine and equals exactly square root of 3 over 2, approximately 0.866; its y-coordinate is sine and equals exactly one half, approximately 0.500. A coloured horizontal segment shows the cosine and a vertical segment shows the sine.
- angle
- π/6 rad0.523599 rad…
- the same angle in degrees
- 30°
- cosine: the x-coordinate
- √3/20.866025…
- sine: the y-coordinate
- 1/20.500000
- sum of the squares
- 1
- sine one full turn later
- 1/20.500000
- arcsin of the sine
- π/6 rad0.523599 rad…
- atan2 of the coordinates
- π/6 rad0.523599 rad…
= exact · ≈ rounded to 6 decimals
The graphs of sine and cosine from −2π to 2π radians. A vertical line marks the angle θ = π/6; the sine graph is at height exactly one half, approximately 0.500 and the cosine graph at height exactly square root of 3 over 2, approximately 0.866.
The graphs use different scales on the two axes (θ across, height up), unlike the circle above, which has the same scale in both directions.
The point is in quadrant I: cos θ > 0 and sin θ > 0. Its reference angle (the acute angle to the x-axis) is π/6. arcsin(sin θ) = θ here, because θ lies between −π/2 and π/2. atan2(sin θ, cos θ) = θ here, because θ lies in (−π, π].
from −24 to 24, whole numbers only
Some angles have exact coordinates built from , and small fractions. The stage in its exact mode snaps to multiples of and shows them with . The table lists the first half-turn, straight from the model:
| angle θ | degrees | cos θ (exact) | sin θ (exact) | cos θ ≈ | sin θ ≈ |
|---|---|---|---|---|---|
| 0 | 0° | 1 | 0 | 1.000000 | 0.000000 |
| π/12 | 15° | (√6+√2)/4 | (√6−√2)/4 | 0.965926 | 0.258819 |
| π/6 | 30° | √3/2 | 1/2 | 0.866025 | 0.500000 |
| π/4 | 45° | √2/2 | √2/2 | 0.707107 | 0.707107 |
| π/3 | 60° | 1/2 | √3/2 | 0.500000 | 0.866025 |
| 5π/12 | 75° | (√6−√2)/4 | (√6+√2)/4 | 0.258819 | 0.965926 |
| π/2 | 90° | 0 | 1 | 0.000000 | 1.000000 |
| 7π/12 | 105° | −(√6−√2)/4 | (√6+√2)/4 | −0.258819 | 0.965926 |
| 2π/3 | 120° | −1/2 | √3/2 | −0.500000 | 0.866025 |
| 3π/4 | 135° | −√2/2 | √2/2 | −0.707107 | 0.707107 |
| 5π/6 | 150° | −√3/2 | 1/2 | −0.866025 | 0.500000 |
| 11π/12 | 165° | −(√6+√2)/4 | (√6−√2)/4 | −0.965926 | 0.258819 |
| π | 180° | −1 | 0 | −1.000000 | 0.000000 |
The other quadrants follow from the first by reflection. Take the reference angle, the acute angle between the radius and the -axis, read its unsigned values from the table, and put the signs back from the quadrant: in quadrant II cosine is negative and sine positive, in quadrant III both are negative, in quadrant IV cosine is positive and sine negative.
Two inverse functions go with the circle, and both have limited ranges. returns only angles from to , so only for those ; for it returns , the other angle with the same sine. The two-argument function , met in the vectors chapter, returns the angle in from both coordinates, so it recovers up to whole turns. The verdict line under the stage says, for each angle, whether and give the angle back.
Changing the base
The exponential is one member of a family. For any base with we have , so every exponential is the natural one with the input rescaled by the constant . The inverse of is the logarithm to the base , written : the power of that gives . It exists for only.
Base is excluded because for every : it is a constant, not an exponential, and its “logarithm” would have to answer what power of gives ? — nothing does. For a base between and the curve falls instead of rising (halving rather than doubling).
Any logarithm is a natural logarithm divided by a constant, which is how a computer evaluates all of them:
The same constant gives the doubling time. For the quantity doubles every time grows by , and for it halves every . And it converts units of information: counts bits and counts nats.
Finally, these identities hold for positive numbers and are what make logarithms useful (the last is the special case of the third, which is how bits are computed):
Change the base, check the identities
Try these
Coordinate plane from −4 to 8 on both axes with the graphs of b to the x and the logarithm of x to the base b for b = 2, and the dashed line y = x: the two graphs are mirror images across it. At x = 2 the power is exactly 4 and the logarithm is exactly 1.
- base
- 2
- the power b to the x
- 4
- logarithm of x to the base b
- 1
- natural logarithm of the base
- 0.693147
- time to double
- 1
- x in bits: log base 2
- 1
- x in nats: natural log
- 0.693147
= exact · ≈ rounded to 6 decimals · ⚠ undefined
b > 1: b^x grows, and doubles every 1 unit of x.
from −4 to 8, up to 2 decimal places
Four identities, checked on a computer
Each identity is exact in mathematics. The computer rounds every step, so each card shows both sides, their difference and the tolerance it is judged against.
from −10 to 1000, up to 2 decimal places
from −10 to 1000, up to 2 decimal places
from −20 to 20, up to 2 decimal places
- left side, ln(ab)
- ≈ 3.178054
- right side, ln a + ln b
- ≈ 3.178054
- difference, left − right
- ≈ 4.44×10⁻¹⁶
- tolerance (a few units in the last place)
- ≤ 5.65×10⁻¹⁵
✓ within tolerance: the two sides agree as far as a double can tell
- left side, ln(aᵏ)
- ≈ 5.375278
- right side, k · ln a
- ≈ 5.375278
- difference, left − right
- ≈ 0
- tolerance (a few units in the last place)
- ≤ 9.55×10⁻¹⁵
✓ within tolerance: the two sides agree as far as a double can tell
- left side, a
- ≈ 6
- right side, b^(ln a / ln b)
- ≈ 6
- difference, left − right
- ≈ 0
- tolerance (a few units in the last place)
- ≤ 2.98×10⁻¹⁴
✓ within tolerance: the two sides agree as far as a double can tell
- left side, log₂ a
- ≈ 2.584963
- right side, ln a / ln 2
- ≈ 2.584963
- difference, left − right
- ≈ 0
- tolerance (a few units in the last place)
- ≤ 4.59×10⁻¹⁵
✓ within tolerance: the two sides agree as far as a double can tell
Exact mathematics says both sides of each identity are equal. The computer rounds every step, so the two sides can differ in the last digits; the stage shows that difference and a tolerance of a few units in the last place. The checker also shows when an identity cannot be checked at all: if is not positive, does not exist and the card says so, rather than inventing a number.
Worked examples
Four computations, each shown step by step with every number taken from the same model as the stages. Step forward and back with the buttons; going back selects an earlier step, and nothing is undone.
Worked examples, step by step
Choose an example
Read the parameters. The vertical scale is a = 2 and the graph is shifted to the right by h = 1. We want the value at x = 5.
Executable reference code
Run the Python examples below to follow the calculations step by step. The comments record what each line returns.
First the exponential and the logarithm: the round trip, the places where each fails, a square and its root, change of base, and bits and nats. Notice the line math.log(1000, 10): it returns 2.9999999999999996, because it divides two rounded logarithms, while math.log10(1000.0) is built for the job and returns 3.0.
import math
# ln undoes e^x for EVERY real x: ln(e^x) = x. The computer rounds twice, so the answer can differ in the last digits.
x = 1.5
back = math.log(math.exp(x)) # 1.5 (here it happens to come back exactly)
# e^x is larger than the largest double (about 1.8e308) once x > 709.78. Python raises an error; NumPy would return inf.
try:
math.exp(710.0)
except OverflowError:
overflow = "OverflowError"
# ln x does not exist for x <= 0: no power of e is zero or negative.
try:
math.log(0.0)
except ValueError:
no_log_of_zero = "ValueError"
# e^(ln x) = x only for x > 0. At x = 10 the round trip is a hair off, because ln 10 is stored rounded.
exp_of_ln = math.exp(math.log(10.0)) # 10.000000000000002: compare with math.isclose, never ==
exp_of_ln_close = math.isclose(exp_of_ln, 10.0, rel_tol=1e-12) # True
# Square then root: sqrt(x**2) is |x|, not x. The sign is lost on the way up.
sqrt_of_square = math.sqrt((-3.0) ** 2) # 3.0
# Change of base: log_b a = ln a / ln b. In floating point the quotient is not always the integer you expect.
log2_of_8 = math.log2(8.0) # 3.0, exactly (math.log2 is built for powers of two)
log_1000_base_10 = math.log(1000, 10) # 2.9999999999999996 (a quotient of two rounded logarithms)
log10_of_1000 = math.log10(1000.0) # 3.0 (the dedicated function)
# Bits and nats: log2(a) = ln(a) / ln(2).
bits = math.log(1024.0) / math.log(2.0) # 10.0
nats = math.log(1024.0) # 6.931471805599453
# A doubling time: how far must x go for 3**x to double? log_3(2) = ln 2 / ln 3.
doubling_time = math.log(2.0) / math.log(3.0) # 0.6309297535714574
print(back, overflow, no_log_of_zero, exp_of_ln, exp_of_ln_close, sqrt_of_square, log2_of_8, log_1000_base_10, log10_of_1000, bits, nats, doubling_time)Then trigonometry. All Python trigonometric functions take radians. The file shows that is not zero (it is about ), why you compare with a tolerance, and the two behaviours from the unit circle: is not , and atan2 returns the angle modulo a turn.
import math
# Python's trigonometric functions take RADIANS. A full turn is 2*pi.
theta = math.pi / 6
cos_t, sin_t = math.cos(theta), math.sin(theta) # 0.8660254037844387 and 0.49999999999999994: sqrt(3)/2 and 1/2 up to rounding
# cos^2 + sin^2 = 1 holds exactly in mathematics; the doubles agree to about 1e-16, so compare with isclose.
pythagorean_gap = cos_t**2 + sin_t**2 - 1.0
pythagorean_ok = math.isclose(cos_t**2 + sin_t**2, 1.0, rel_tol=1e-15)
# pi is stored rounded, so sin(pi) is not 0 and cos(pi/2) is not 0. Never test a trigonometric value with ==.
sin_pi = math.sin(math.pi) # 1.2246467991473532e-16
cos_half_pi = math.cos(math.pi / 2) # 6.123233995736766e-17
sin_pi_is_zero = math.isclose(sin_pi, 0.0, abs_tol=1e-12) # True: ask for a tolerance
# Periodicity: adding a full turn gives the same point (to rounding).
periodic = math.isclose(math.sin(theta + 2 * math.pi), math.sin(theta), rel_tol=0, abs_tol=1e-15)
# arcsin(sin t) is NOT t unless t is in [-pi/2, pi/2]: arcsin only returns angles in that interval.
arcsin_of_sin_2 = math.asin(math.sin(2.0)) # 1.1415926535897933, which is pi - 2 up to rounding
# atan2(sin t, cos t) IS t modulo 2*pi, in (-pi, pi]: it sees the signs of both components.
atan2_of_4 = math.atan2(math.sin(4.0), math.cos(4.0)) # -2.2831853071795867, which is 4 - 2*pi up to rounding
# Degrees and radians: 180 degrees is pi radians.
thirty_degrees = math.radians(30.0) # 0.5235987755982988
back_to_degrees = math.degrees(math.pi / 6) # 29.999999999999996: close to 30, not equal
print(cos_t, sin_t, pythagorean_ok, sin_pi, cos_half_pi, sin_pi_is_zero, periodic, arcsin_of_sin_2, atan2_of_4, thirty_degrees, back_to_degrees)Finally the practical form of the exponential in machine learning: the softmax, which overflows if written naively, and the standard shift that fixes it.
import numpy as np
# Scores ("logits") from a model. Softmax turns them into probabilities: exp(s_i) / sum_j exp(s_j).
logits = np.array([1000.0, 1001.0, 1002.0])
# e^1000 is far above the largest double (about 1.8e308), so NumPy returns inf, and inf / inf is nan.
with np.errstate(over="ignore", invalid="ignore"):
naive = np.exp(logits) / np.exp(logits).sum() # [nan nan nan]
# The probabilities do not change if every score is shifted by the same amount: e^(s - m) / sum e^(s_j - m) = e^s / sum e^(s_j).
# Shift by the largest score m, so the largest exponent is e^0 = 1 and nothing can overflow.
shifted = np.exp(logits - logits.max())
stable = shifted / shifted.sum() # [0.09003057 0.24472847 0.66524096]
# The same idea for log(sum(exp(s))): m + log(sum(exp(s - m))).
log_sum_exp = logits.max() + np.log(shifted.sum()) # 1002.4076059644444
print(naive, stable, log_sum_exp)Explore: change the parameters
This is the whole comparison on one plane, with the facts about each visible curve in cards underneath: domain, range, zeros, asymptote, behaviour and symmetry. These questions have definite answers; check them against the stage.
Slide, stretch and compare
Draw these functions
Or start from
Coordinate plane from −7 to 7 on both axes. Vertical scale a is 1 and horizontal shift h is 0. The drawn curves are y equals a times g of x minus h for e to the x, natural log of x, sine of x. A vertical dashed line marks the input x = 1. Values at that input: e to the x: approximately 2.718; natural log of x: exactly 0; sine of x: approximately 0.841.
- input
- 1
- shifted input x − h
- 1
- a·e^(x − h)
- 2.718282
- a·ln(x − h)
- 0
- a·sin(x − h)
- 0.841471
= exact · ≈ rounded to 6 decimals · ⚠ undefined
At x = 1 the highest curve is eˣ (2.718282) and the lowest is ln x (0).
a·e^(x − h)
- Domain
- all real numbers
- Range
- (0, ∞)
- Zeros
- none
- Asymptote
- the x-axis (y = 0), approached as x goes to −∞
- Behaviour
- increasing everywhere, faster and faster
- Symmetry
- none
a·ln(x − h)
- Domain
- x > 0
- Range
- all real numbers
- Zeros
- x = 1
- Asymptote
- the vertical line x = 0
- Behaviour
- increasing, but ever more slowly
- Symmetry
- none
a·sin(x − h)
- Domain
- all real numbers
- Range
- [−1, 1]
- Zeros
- x = kπ for every integer k
- Asymptote
- none
- Behaviour
- oscillates between −1 and 1, repeating every 2π
- Symmetry
- turned half a turn about the point (0, 0)
from −3 to 3, up to 2 decimal places
from −4 to 4, up to 2 decimal places
from −6 to 6, up to 2 decimal places
- Switch on , and together with and . Where do the line and the parabola cross? Does ever fall below ? (For positive it never does: the smallest ratio is , at . For negative it does, until they cross near .) The exercise below asks the same question for , where the answer is different.
- Turn on and raise to . Where is its zero, and where is its asymptote? Why is the zero not at the asymptote?
- Set on the parabola. What happens to its range, and to the facts card?
- Turn on the sine and cosine with . Which value of for the sine brings it closest to the cosine? (Think of a quarter of a period, and of the direction a positive moves a graph. The exact answer is ; the arrows and the buttons step by , so they reach ; typing a value allows two decimals.)
- Make . What do all the families have in common?
Common mistakes
Numerical considerations
The exponential has a narrow window in which a double can hold it. The largest double is about , so overflows just above ; the smallest positive double is about , so underflows to exactly just below . The table is computed by the same model as the stages above.
| x | eˣ as a double | what happened |
|---|---|---|
| 1 | ≈ 2.718282 | a finite double |
| 10 | ≈ 22026.465795 | a finite double |
| 100 | ≈ 2.68812×10⁴³ | a finite double |
| 700 | ≈ 1.01423×10³⁰⁴ | a finite double |
| 709 | ≈ 8.21841×10³⁰⁷ | a finite double |
| 710 | = ∞ | overflow: the true value is too large for a double |
| −700 | ≈ 9.85968×10⁻³⁰⁵ | a finite double |
| −745 | ≈ 4.94066×10⁻³²⁴ | a finite double |
| −746 | = 0 | underflow: the true value is too small for a double |
| −800 | = 0 | underflow: the true value is too small for a double |
When the stored value is or , the stage writes because it is stating a fact about the double (“the stored value is ”), and it adds a note giving the truth: the real value is finite and large, or positive and tiny. Python and NumPy even disagree on what to do. math.exp(710.0) raises OverflowError; np.exp(710.0) quietly returns inf, and inf / inf is nan, which then poisons every number it touches.
Three more places where the machine and the mathematics part ways. First, is only up to rounding: math.exp(math.log(10.0)) is 10.000000000000002. Second, is stored rounded, so is about , not , and is about . Third, math.log(1000, 10) is 2.9999999999999996 but math.log10(1000.0) is 3.0: when a dedicated function for your base exists, use it.
Connections to engineering and machine learning
- Softmax and log-sum-exp. A classifier turns scores into probabilities . With scores near the exponentials overflow. Subtracting the largest score multiplies the top and the bottom by the same number , so the probabilities do not change, and every exponent becomes at most . The same shift gives , the standard stable form used in the loss functions of almost every framework.
- Logarithms turn products into sums. The likelihood of independent events each with probability is , which underflows to in a double. The log-likelihood is , an ordinary number. This is why models are trained on the log-likelihood.
- Bits and nats. Cross-entropy and information are measured in nats when computed with (the default in most libraries) and in bits with ; one nat is bits. Telling equally likely cases apart takes bits.
- Exponential growth and decay. Compound growth, radioactive decay, learning-rate schedules and the attenuation of a signal are all ; the doubling or halving time is the one number that describes them.
- Sine and cosine. Every periodic signal is built from them, and a rotation of the plane by is the matrix whose entries are and . Positional encodings in transformers use sines and cosines of many frequencies; all of them take radians.
- The logarithm as a compressor. On a log axis, multiplying a quantity by a fixed factor moves it a fixed distance. This is why spectra, learning curves and file sizes are plotted that way.
Exercises
Answer each question and press Check. A wrong answer tells you which mistake it looks like. After one attempt you may open a worked solution.
Summary and checkpoints
- Six shapes carry most of the work: the line, the parabola, the exponential, the logarithm, the sine and the cosine. Each is for a basic : stretches vertically, shifts right.
- Domains matter. exists only for positive inputs; only for non-negative ones. for every , but only for , and .
- Inverse functions are mirror images across .
- On the unit circle, is the point at distance (radians) from , and . returns only angles in ; returns angles in .
- ; the doubling time is ; bits use base , nats use base .
- In code: radians everywhere, tolerances instead of
==, shift exponents before using them, and expectexpto overflow near .
Checkpoints. You are ready to move on when you can, without looking:
- Sketch and name its domain, its asymptote and its zero.
- Say why is not always , and for which it is.
- Give and of , with signs, and say which quadrant that is.
- Explain why , and what it equals.
- Compute and explain how it differs from .
- Say what happens to in Python and in NumPy, and how to avoid the overflow in a softmax.