Feed john-d-cook John D. Cook

Favorite IconJohn D. Cook

Link https://www.johndcook.com/blog
Feed http://feeds.feedburner.com/TheEndeavour?format=xml
Updated 2025-04-19 13:31
What makes the log10 trick work?
In my post on mentally calculating logarithms, I showed that log10 x ≈ (x – 1)/(x + 1) for 1/√10 ≤ x ≤ √10. You could convert this into an approximation for logs in any base by multiplying by the right scaling factor, but why does it work out so simply for base 10? Define […]The post What makes the log10 trick work? first appeared on John D. Cook.
Simple approximation for surface area of an ellipsoid
After writing the previous post, I wondered where else you might be able to use r-means to create accurate approximations. I thought maybe this would apply to the surface area of an ellipsoid, and a little searching around showed that Knud Thomsen thought of this in 2004. The general equation for the surface of an […]The post Simple approximation for surface area of an ellipsoid first appeared on John D. Cook.
Simple approximation for perimeter of an ellipse
The perimeter of an ellipse cannot be computed in closed form. That is, no finite combination of elementary functions will give you the exact value. But we will present a simple approximation that is remarkably accurate. So this post has two parts: exact calculation, and simple approximation. Exact perimeter The perimeter can be computed exactly […]The post Simple approximation for perimeter of an ellipse first appeared on John D. Cook.
Books and revealed preferences
Revealed preferences are the preferences we demonstrate by our actions. These may be different from our stated preferences. Even if we’re being candid, we may not be self-aware. One of the secrets to the success of Google’s PageRank algorithm is that it ranks based on revealed preferences: If someone links to a site, they’re implicitly […]The post Books and revealed preferences first appeared on John D. Cook.
Mentally calculating 10^x
This is the last in a sequence of three posts on mental calculation. The first looked at computing sine, cosine, and tangent in degrees. The second looked at computing logarithms, initially in base 10 but bootstrapping from there to other bases as well. In the previous post, we showed that log10 x ≈ (x-1)/(x+1) for […]The post Mentally calculating 10^x first appeared on John D. Cook.
Mentally calculating logarithms
The previous post looked at approximations for trig functions that are simple enough to compute without a calculator. I wondered whether I could come up with something similar for logarithms. I start with log base 10. Later in the post I show how to get to find logs in other bases from logs base 10. […]The post Mentally calculating logarithms first appeared on John D. Cook.
Simple trig function approximations
Anthony Robin gives three simple approximations for trig functions in degrees in [1]. The following plots show that these approximations are pretty good. It’s hard to distinguish the approximate and exact curves. The accuracy of the approximations is easier to see when we subtract off the exact values. The only problem is that the tangent […]The post Simple trig function approximations first appeared on John D. Cook.
Computing Fourier series coefficients with the FFT
The Discrete Fourier Transform (DFT) is a mathematical function, and the Fast Fourier Transform (FFT) is an algorithm for computing that function. Since the DFT is almost always computed via the FFT, the distinction between the two is sometimes lost. It is often not necessary to distinguish between the two. In my previous post, I […]The post Computing Fourier series coefficients with the FFT first appeared on John D. Cook.
Support of a signal and its FFT
The previous post looked at the Fourier uncertainty principle. This post looks at an analogous result for the discrete Fourier transform. The uncertainty principle for the (continuous) Fourier transform says a signal cannot be localized in both the time domain and the frequency domain. The more bunched up a function is, the more spread out […]The post Support of a signal and its FFT first appeared on John D. Cook.
Fourier uncertainty principle
Heisenberg’s uncertainty principle says there is a limit to how well you can know both the position and momentum of anything at the same time. The product of the uncertainties in the two quantities has a lower bound. There is a closely related principle in Fourier analysis that says a function and its Fourier transform […]The post Fourier uncertainty principle first appeared on John D. Cook.
Fourier transforms in Mathematica
Unfortunately there are many slightly different ways to define the Fourier transform. So the first two questions when using Mathematica (or any other software) to compute Fourier transforms are what definition of Fourier transform does it use, and what to do if you want to use a different definition. The answer to the first question […]The post Fourier transforms in Mathematica first appeared on John D. Cook.
Bessel determinants
The Bessel functions J and Y are analogous to sine and cosine. Bessel functions come up in polar coordinates the way sines and cosines come up in rectangular coordinates. There are J and Y functions of various orders, conventionally written with a subscript ν. I recently ran across a curious set of relations between these […]The post Bessel determinants first appeared on John D. Cook.
Computing normal probabilities with a simple calculator
If you need to calculate Φ(x), the CDF of a standard normal random variable, but don’t have Φ on your calculator, you can use the approximation [1] Φ(x) ≈ 0.5 + 0.5*tanh(0.8 x). If you don’t have a tanh function on your calculator, you can use tanh(0.8x) = (exp(1.6x) – 1) / (exp(1.6x) + 1). […]The post Computing normal probabilities with a simple calculator first appeared on John D. Cook.
Illustrating Gershgorin disks with NumPy
Gershgorin’s theorem gives bounds on the locations of eigenvalues for an arbitrary square complex matrix. The eigenvalues are contained in disks, known as Gershgorin disks, centered on the diagonal elements of the matrix. The radius of the disk centered on the kth diagonal element is the sum of the absolute values of the elements in […]The post Illustrating Gershgorin disks with NumPy first appeared on John D. Cook.
Calculating π with factorials
In honor of Pi Day, I present the following equation for calculating π using factorials. It’s not a very useful formula for π, but an amusing one. It takes a practical formula for approximating factorials, Stirling’s formula, and turns it around to make an impractical formula for approximating π. It does converge to π albeit […]The post Calculating π with factorials first appeared on John D. Cook.
Pareto and Pandas
This post muses about what it means to learn a software library. I’ll use Pandas as an example, but the post isn’t just about Pandas. Suppose you say “I want to learn Pandas.” That implicitly assumes Pandas one thing, and in a sense it is. In another sense Pandas is hundreds of things. At the […]The post Pareto and Pandas first appeared on John D. Cook.
i^i^i …
This post plots the sequence That is, we define a sequence by z1 = i and for k > 1, I ran across this in [1], but the sequence goes back as far as Euler. Here’s a plot of the points: This plot has three spiral arms, as if there are three sequences converging to […]The post i^i^i … first appeared on John D. Cook.
Broadcasting and functors
In my previous post, I looked at the map Δ that takes a column vector to a diagonal matrix. I even drew a commutative diagram, which foreshadows a little category theory. Suppose you have a function f of a real or complex variable. To an R programmer, if x is a vector, it’s obvious that […]The post Broadcasting and functors first appeared on John D. Cook.
Moving between vectors and diagonal matrices
This is the first of two posts on moving between vectors and diagonal matrices. The next post is Broadcasting and functors. Motivation When I first saw the product of two vectors in R, I was confused. If x and y are vectors, what does x*y mean? An R programmer would say “You multiply components together, […]The post Moving between vectors and diagonal matrices first appeared on John D. Cook.
Adding tubes to knots
Several months ago I wrote a blog post about Lissajous curves and knots that included the image below. Here’s an improved version of the same knot. The original image was like tying the knot in thread. The new image is like tying it in rope, which makes it easier to see. The key was to […]The post Adding tubes to knots first appeared on John D. Cook.
A stiffening spring
Imagine a spring with stiffness k1 attached to a ceiling and a mass m1 handing from the spring. There’s a second spring attached to the first mass with stiffness k2 and a mass m2 handing from that. The motion of the system is described by the pair of differential equations If the second spring were […]The post A stiffening spring first appeared on John D. Cook.
tcgrep: grep rewritten in Perl
In The Perl Cookbook, Tom Christiansen gives his rewrite of the Unix utility grep that he calls tcgrep. You don’t have to know Perl to use tcgrep, but you can send it Perl regular expressions. Why not grep with PCRE? You can get basically the same functionality as tcgrep by using grep with its PCRE […]The post tcgrep: grep rewritten in Perl first appeared on John D. Cook.
Scaled Beta2 distribution
I recently ran across a new probability distribution called the “Scaled Beta2” or “SBeta2” for short in [1]. For positive argument x and for positive parameters p, q, and b, its density is This is a heavy-tailed distribution. For large x, the probability density is O(x–q-1), the same as a Student-t distribution with q degrees […]The post Scaled Beta2 distribution first appeared on John D. Cook.
Applications of (1-z)/(1+z)
I keep running into the function f(z) = (1-z)/(1+z). The most recent examples include applications to radio antennas and mental calculation. More on these applications below. Involutions A convenient property of our function f is that it is its own inverse, i.e. f( f(x) ) = x. The technical term for this is that f […]The post Applications of (1-z)/(1+z) first appeared on John D. Cook.
Saxophone ranges
I stumbled on a recording of a contrabass saxophone last night and wondered just how low it was [1], so I decided to write this post giving the ranges of each of the saxophones. The four most common saxophones are baritone, tenor, alto, and soprano. These correspond to the instruments in the image above. There […]The post Saxophone ranges first appeared on John D. Cook.
Fraction comparison trick
If you want to determine whether a/b > c/d, it is often enough to test whether a+d > b+c. Said another way a/b is usually greater than c/d when a+d is greater than b+c. This sounds imprecise if not crazy. But it is easy to make precise and [1] shows that it is true. Examples […]The post Fraction comparison trick first appeared on John D. Cook.
Normal probability fixed point
Let Z be a standard normal random variable. Then there is a unique x such that Pr(Z < x) = x. That is, Φ has a unique fixed point where Φ is the CDF of a standard normal. It’s easy to find the fixed point: start anywhere and iterate Φ. Here’s a cobweb plot that […]The post Normal probability fixed point first appeared on John D. Cook.
The time-traveling professor
Suppose you were a contemporary professor sent back in time. You need to continue your academic career, but you can’t let anyone know that you’re from the future. You can’t take anything material with you but you retain your memories. At first thought you might think you could become a superstar, like the musician in […]The post The time-traveling professor first appeared on John D. Cook.
The debauch of indices
This morning I was working on a linear algebra problem for a client that I first solved by doing calculations with indices. As I was writing things up I thought of the phrase “the debauch of indices” that mathematicians sometimes use to describe tensor calculations. The idea is that calculations with lots of indices are […]The post The debauch of indices first appeared on John D. Cook.
Not-to-do list
There is an apocryphal [1] story that Warren Buffet once asked someone to list his top 25 goals in order. Buffet then told him that he should avoid items 6 through 25 at all costs. The idea is that worthy but low-priority goals distract from high-priority goals. Paul Graham wrote something similar about fake work. […]The post Not-to-do list first appeared on John D. Cook.
Herd immunity countdown
A few weeks ago I wrote a post giving a back-of-the-envelope calculation regarding when the US would reach herd immunity to SARS-COV-2. As I pointed out repeatedly, this is only a rough estimate because it makes numerous simplifying assumptions and is based on numbers that have a lot of uncertainty around them. See that post […]The post Herd immunity countdown first appeared on John D. Cook.
Martin’s doileys
An iteration due to artist Barry Martin produces intricate doiley-like patterns by iterating a simple mathematical function. I ran across this via [1]. The images produced are sensitive to small changes in the starting parameters x and y, as well as to the parameters a, b, and c. Here are three examples: And here’s the […]The post Martin’s doileys first appeared on John D. Cook.
Divisibility by any prime
Here is a general approach to determining whether a number is divisible by a prime. I’ll start with a couple examples before I state the general rule. This method is documented in [1]. First example: Is 2759 divisible by 31? Yes, because and 0 is divisible by 31. Is 75273 divisible by 61? No, because […]The post Divisibility by any prime first appeared on John D. Cook.
Finding pi in pi with Perl
Here’s a frivolous problem whose solution illustrates three features of Perl: Arbitrary precision floating point Lazy quantifiers in regular expressions Returning the positions of matched groups. Our problem is to look for the digits 3, 1, 4, and 1 in the decimal part of π. First, we get the first 100 digits of π after […]The post Finding pi in pi with Perl first appeared on John D. Cook.
Solving for neck length
A few days ago I wrote about my experiment with a wine bottle and a beer bottle. I blew across the empty bottles and measured the resulting pitch, then compared the result to the pitch you would get in theory if the bottle were a Helmholtz resonator. See the previous post for details. Tonight I […]The post Solving for neck length first appeared on John D. Cook.
Trig functions across programming languages
Programming languages are inconsistent in their support for trig functions, and inconsistent in the names they use for the functions they support. Several times I’ve been irritated by this and said that I should make a comparison chart someday, and today I finally did it. Here’s the chart. The C column also stands for languages […]The post Trig functions across programming languages first appeared on John D. Cook.
Da Vinci on wave propagation
From Leonardo da Vinci: The impetus is much quicker than the water, for it often happens that the wave flees the place of its creation, while the water does not; like the waves made in a field of grain by the wind, where we see the waves running across the field while the grain remains […]The post Da Vinci on wave propagation first appeared on John D. Cook.
Pitch of a big wine bottle
Yesterday my daughter came by and dropped off a huge blue wine bottle (empty). She had started removing the label, but as you can see she didn’t get very far yet. It’s an Incanto Chardonnay Pinot Grigio from Trader Joe’s. I blew across the top of the bottle to hear what sound it makes, and […]The post Pitch of a big wine bottle first appeared on John D. Cook.
More images from an oddball coordinate system
Three months ago I posted some images created by graphing equations in circular coordinates. Not polar coordinates as you might expect, but a strange coordinate system based on circles in another way. The most interesting thing about this coordinate system is that familiar functions product unfamiliar graphs. This morning I played around with the code […]The post More images from an oddball coordinate system first appeared on John D. Cook.
Fibonacci numbers and hyperbolic sine
Richard Askey came up with the following formula for Fibonacci numbers: Here φ is the golden ratio, (1 + √5)/2. We’ll use this formula as the jumping off point to discuss the implications of how equations are written, complex logarithms, and floating point computing in Python. Reading forward and backward Of course every equation of […]The post Fibonacci numbers and hyperbolic sine first appeared on John D. Cook.
Herd immunity on the back of an envelope
This post presents a back-of-the-envelope calculation regarding COVID herd immunity in the US. Every input to the calculation is only roughly known, and I’m going to make simplifying assumptions left and right. So take this all with a grain of salt. According to a recent article, about 26 million Americans have been vaccinated against COVID, […]The post Herd immunity on the back of an envelope first appeared on John D. Cook.
Pythagorean chaos
If a and b are two distinct positive integers, then |a²-b²|, 2ab, a²+b² is a Pythagorean triple. The result still gives the sides of a right triangle if the starting points aren’t integers In [1], Nick Lord looks at what happens if you iterate this procedure, using the output of one step as the input […]The post Pythagorean chaos first appeared on John D. Cook.
Python triple quote strings and regular expressions
There are several ways to quote strings in Python. Triple quotes let strings span multiple lines. Line breaks in your source file become line break characters in your string. A triple-quoted string in Python acts something like “here doc” in other languages. However, Python’s indentation rules complicate matters because the indentation becomes part of the […]The post Python triple quote strings and regular expressions first appeared on John D. Cook.
Mentally computing 3rd and 5th roots
A couple years ago I wrote about a trick for mentally computing the fifth root of an integer if you know that the number you’re starting with is the fifth power of a two-digit number. This morning I wrote up a similar (and simpler) trick for cube roots as a thread on @AlgebraFact. You can […]The post Mentally computing 3rd and 5th roots first appeared on John D. Cook.
How it started, how it’s going
This morning I created several mathematical versions of the popular “How it started, how it’s going” meme and posted them on Twitter. First, the Koch snowflake on @AlgebraFact: Then the logistic bifurcation on @AnalysisFact: Then cellular automaton “Rule 90” on @CompSciFact: And finally, the Lorenz system on @Diff_eq:The post How it started, how it’s going first appeared on John D. Cook.
Newton’s method spirals
In [1] the authors look at applying Newton’s root-finding method to the function f(z) = zp where p = a + bi. They show that if you start Newton’s method at z = 1, the kth iterate will be (1 – 1/p)k. This converges to 0 when a > 1/2, runs around in circles when […]The post Newton’s method spirals first appeared on John D. Cook.
Sums of consecutive reciprocals
The sum of the reciprocals of consecutive integers is never an integer. That is, for all positive integers m and n with n > m, the sum is never an integer. This was proved by József Kürschák in 1908. This means that the harmonic numbers defined by are never integers for n > 1. The […]The post Sums of consecutive reciprocals first appeared on John D. Cook.
Peaks of Sinc
Yesterday I wrote two posts about finding the peaks of the sinc function. Both focused on numerical methods, the first using a contraction mapping and the second using Newton’s method. This post will focus on the locations of the peaks rather than ways of computing them. The first post included this discussion of the peak […]The post Peaks of Sinc first appeared on John D. Cook.
Gell-Mann amnesia and its opposite
Michael Crichton coined the term Gell-Mann Amnesia effect to describe forgetting how unreliable a source is in one area when you trust it in another area. In Crichton’s words: Briefly stated, the Gell-Mann Amnesia effect is as follows. You open the newspaper to an article on some subject you know well. In Murray [Gell-Mann]’s case, […]The post Gell-Mann amnesia and its opposite first appeared on John D. Cook.
Rate of convergence for Newton’s method
In the previous post I looked at the problem of finding the peaks of the sinc function. In this post we use this problem to illustrate how two formulations of the same problem can behave very differently with Newton’s method. The previous post mentioned finding the peaks by solving either x cos x – sin […]The post Rate of convergence for Newton’s method first appeared on John D. Cook.
...24252627282930313233...