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 2024-11-22 13:31
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.
Reverse iteration root-finding
The sinc function is defined by sinc(x) = sin(x)/x. This function comes up constantly in signal processing. Here’s a plot. We would like to find the location of the function’s peaks. Let’s focus first on the first positive peak, the one that’s somewhere between 5 and 10. Once we can find that one, the rest […]The post Reverse iteration root-finding first appeared on John D. Cook.
Low-tech transparency
I recently received two large data files from a client, with names like foo.xlsx and foo.csv. Presumably these are redundant; the latter is probably an export of the former. I did a spot check and that seems to be the case. Then I had a bright idea: use pandas to make sure the two files […]The post Low-tech transparency first appeared on John D. Cook.
Zeros of trigonometric polynomials
A periodic function has at least as many real zeros as its lowest frequency Fourier component. In more detail, the Sturm-Hurwitz theorem says that has at least 2n zeros in the interval [0, 2π) if an and bn are not both zero. You could take N to be infinity if you’d like. Note that the […]The post Zeros of trigonometric polynomials first appeared on John D. Cook.
Just-in-case revisited
Just-in-time learning means learning something just when you need it. The alternative is just-in-case, learning something in case you need it. I discussed this in an earlier post, and today I’d like to add a little to that discussion. There are some things you need to know (or at least be familiar with) before you […]The post Just-in-case revisited first appeared on John D. Cook.
Data pathology
This post is an expansion of something I wrote on Twitter: Data scientists often complain that the bulk of their work is data cleaning. But if you see data cleaning as the work, not just an obstacle to the work, it can be interesting. You could think of it as data pathology, a kind of […]The post Data pathology first appeared on John D. Cook.
More stability, less stress
It’s been eight years since I started my consulting business. Two of the things I love about having my own business are the stability and the reduced stress. This may sound like a joke, but I’m completely serious. Having a business is ostensibly less stable and more stressful than having a salaried job, but at […]The post More stability, less stress first appeared on John D. Cook.
Quaternion square roots
If y is a quaternion, how many solutions does x² = y have? That is, does every quaternion have a square root, and if so, how many square roots does it have? A quaternion can have more than two roots. There is a example right in the definition of quaternions: i² = j² = k² […]The post Quaternion square roots first appeared on John D. Cook.
Bootstrapping a minimal math library
Sometimes you don’t have all the math functions available that you would like. For example, maybe you have a way to calculate natural logs but you would like to calculate a log base 10. The Unix utility bc is a prime example of this. It only includes six common math functions: sine cosine arctangent natural […]The post Bootstrapping a minimal math library first appeared on John D. Cook.
Average sum of digits
The smaller the base you write numbers in, the smaller their digits sums will be on average. This need not be true of any given number, only for numbers on average. For example, let n = 2021. In base 10, the sum of the digits is 5, but in base 2 the sum of the […]The post Average sum of digits first appeared on John D. Cook.
Random polynomials revisited
A few days ago I wrote about the expected number of roots in a random polynomial where each coefficient is drawn from a standard normal, i.e. a Gaussian distribution with mean 0 and variance 1. Another class of random polynomials, one that comes up in applications to physics, draws each coefficient from a different distribution. […]The post Random polynomials revisited first appeared on John D. Cook.
One infinity or two?
If you want to add ∞ to the real numbers, should you add one infinity or two? The answer depends on context. This post gives several examples each of when its appropriate to add one or two infinities. Two infinities: relativistic addition A couple days ago I wrote about relativistic addition, where the sum of […]The post One infinity or two? first appeared on John D. Cook.
Radio Frequency Bands
The radio spectrum is conventionally [1] divided into frequency bands that seem arbitrary at first glance. For example, VHF runs from 30 to 300 MHz. All the frequency band boundaries are 3 times a power of 10. Why all the 3’s? Two reasons: 3 is roughly the square root of 10, and the speed of […]The post Radio Frequency Bands first appeared on John D. Cook.
Most popular posts of 2020
These have been the most popular posts here this year. Pretending OOP never happened Worst tool for the job Banned math book Divisibility by 13 Memento ComplexitatisThe post Most popular posts of 2020 first appeared on John D. Cook.
...22232425262728293031...