atlas news
    
John D.Cook : Python
16  novembre     19h13
Adding an imaginary unit to a finite field
John    Let p be a prime number. Then the integers mod p form a finite field. The number of elements in a finite field must be a power of a prime, i.e. the order q pn for some n. When n 1, we can take the elements of our field to be polynomials of [&] The post Adding an imaginary unit to a finite field...
21  août     13h55
A recipe for creating random fractals
John    Last week I gave an example of a randomly generated fractal and mentioned that it was a particularly special case of a more general algorithm for generating similar fractals found in [1]. Here’s the general pattern. First, create a non-singular matrix M with integer entries and let k be the...
31  juillet     01h11
Base58Check encoding in Python
John    The previous post began by saying Bitcoin’s Wallet Import Format (WIF) is essentially Base58 encoding with a checksum. More specifically, WIF uses Base58Check encoding. This post will fill in the missing details and show how to carry out computing Base58Check in Python. There are multiple ways to...
16  décembre     17h03
Perpetual Calendars
John    The previous post explained why the Gregorian calendar is the way it is, and that it consists of a whole number of weeks. It follows that the Gregorian calendar repeats itself every 400 years. For example, the calendar for 2025 will be exactly the same as the calendar for 1625 and 2425. There are...
21  mars     01h07
MD5 hash collision example
John    Marc Stevens gave an example of two alphanumeric strings that differ in only one byte that have the same MD5 hash value. It may seem like beating a dead horse to demonstrate weaknesses in MD5, but it’s instructive to study the flaws of broken methods. And despite the fact that MD5 has been broken...
04  mars     18h55
Finite differences and Pascal’s triangle
John    The key to solving a lot of elementary what-number-comes-next puzzles is to take first or second differences. For example, if asked what the next item in the series 14, 29, 50, 77, 110, ... the answer (or at lest the answer the person posing the question is most likely looking for) is 149. You...
10  février     15h45
Factoring pseudoprimes
John    Fermat’s little theorem says that if p is a prime number, then for any positive integer b p we have bpâ ’1 1 (mod p). This theorem gives a necessary but not sufficient condition for a number to be prime. Fermat’s primality test The converse of Fermat’s little theorem is not always true, but [&] The...
01  janvier     21h12
Computing inverse factorial
John    I needed the inverse factorial function for my previous post. I was sure I’d written a post on computing the inverse factorial, and intended to reuse the code from that earlier post. But when I searched I couldn’t find anything, so I’m posting the code here for my future reference and for anyone...
20  novembre     11h55
Kepler triangle
John    A Kepler triangle is a right triangle whose sides are in geometric progression. That is, if the sides have length a b c, then b a c b k. All Kepler triangles are similar because the proportionality constant k can only take on one value. To see this, we first pick our units [&] The post Kepler...
28  septembre     23h24
Cross-platform way to enter Unicode characters
John    The previous post describes the hoops I jumped through to enter Unicode characters on a Mac. Here’s a script to run from the command line that will copy Unicode characters to the system clipboard. It runs anywhere the Python module pyperclip runs. # usr bin env python3 import sys import pyperclip...
27  août     21h47
Curvature at Cairo
John    I was flipping through Gravitation [1] this weekend and was curious about an illustration on page 309. This post reproduces that graph. The graph is centered at Cairo, Egypt and includes triangles whose side lengths are the distances between cities. The triangles are calculated using only distances...
    20h02
Calculating the intersection of two circles
John    Given the equations for two circles, how can you tell whether they intersect? And if they do intersect, how do you find the point(s) of intersection? MathWorld gives a derivation, but I’d like to add the derivation there in two ways. First, I’d like to be more explicit about the number of solutions...
18  juillet     19h04
Filtering on how words are being used
John    Yesterday I wrote about how you could use the spaCy Python library to find proper nouns in a document. Now suppose you want to refine this and find proper nouns that are the subjects of sentences or proper nouns that are direct objects. This post was motivated by a project in which I needed to [&]...
23  mai     13h44
AM over GM
John    Suppose you take the arithmetic mean and the geometric mean of the first n integers. The ratio of these two means converges to e 2 as n grows [1]. In symbols, Now suppose we wanted to visualize the convergence by plotting the expression on the left side for a sequence of ns. First let’s let n [&]...
29  avril     14h56
Golden integration
John    Let Ï be the golden ratio. The fractional parts of nÏ bounce around in the unit interval in a sort of random way. Technically, the sequence is quasi-random. Quasi-random sequences are like random sequences but better in the sense that they explore a space more efficiently than random sequences. For...
30  novembre     01h15
Elliptic functions of a complex argument in Python
John    I used Mathematica to create the graphics for my previous two posts because SciPy didn’t have the functions I needed. In particular, elliptic integrals and elliptic functions in SciPy only take real-valued arguments, but I needed to use complex arguments. Also, I needed theta functions, which are...
16  novembre     13h35
When a cubic or quartic has a double root
John    Thanks to the quadratic equation, it’s easy to tell whether a quadratic equation has a double root. The equation has a double root if and only if the discriminant is zero. The discriminant of a cubic is much less known, and the analogs for higher order polynomials are unheard of. There is a...
15  octobre     14h40
Using Python as a statistical calculator
John    This post is for someone unfamiliar with SciPy who wants to use Python to do basic statistical calculations. More specifically, this post will look at working with common families of random variables normal, exponential, gamma, etc. and how to calculate probabilities with these. All the statistical...
07  octobre     12h33
Unicode numbers
John    There are 10 digits in ASCII, and I bet you can guess what they are. In ASCII, a digit is a decimal is a number. Things are much wilder in Unicode. There are hundreds of decimals, digits, and numeric characters, and they’re different sets. The following Python code loops through all possible...
06  octobre     21h39
Regex to match ICD-11 code
John    ICD codes are diagnostic codes created by the WHO. (Three TLAs in just the opening paragraph ) The latest version, ICD-11, went into effect in January of this year. A few countries are using ICD-11 now; it’s expected to be at least a couple years before the US moves from ICD-10 to ICD-11. (I still...
06  septembre     00h17
Literate programming to reduce errors
John    I had some errors in a recent blog post that might have been eliminated if I had programmatically generated the content of the post rather than writing it by hand. I rewrote the example in this post in using org-mode. My org file looked like this: # begin src python :session :exports none lax lat...
04  septembre     11h00
Computing VIN checksums
John    I’ve had to work a little with VIN numbers lately, and so I looked back at a post I wrote on the subject three years ago. That post goes into the details of Vehicle Identification Numbers and the quirky algorithm used to compute the check sum. This post captures the algorithm in Python code. See [&...
16  août     18h40
Dump a pickle file to a readable text file
John    I got a data file from a client recently in pickle format. I happen to know that pickle is a binary format for serializing Python objects, but trying to open a pickle file could be a puzzle if you didn’t know this. Be careful There are a couple problems with using pickle files for data [&] The post...
15  août     13h24
Keeping data and code together with org-mode
John    With org-mode you can keep data, code, and documentation in one file. Suppose you have an org-mode file containing the following table. # NAME: mydata Drug Patients ------ ---------- X 232 Y 351 Z 117 Note that there cannot be a blank line between the [&] The post Keeping data and code together...
03  août     13h05
Inline computed content in org-mode
John    The previous post discussed how to use org-mode as a notebook. You can have blocks of code and blocks of results, analogous to cells in a Jupyter notebook. The code and the results export as obvious blocks when you export the org file to another format, such as LaTeX or HTML. And that’s fine for [&...
02  août     14h49
Org-mode as a lightweight notebook
John    You can think of org-mode as simply a kind of markdown, a plain text file that can be exported to fancier formats such as HTML or PDF. It’s a lot more than that, but that’s a reasonable place to start. Org-mode also integrates with source code. You can embed code in your file and have [&] The post...
30  juillet     13h44
Complex AGM
John    The arithmetic-geometric mean (AGM) of two non-negative real numbers a and b is defined as the limit of the iteration starting with a0 a and b0 b and an 1 (an bn) bn 1 s(an bn) for n 0. This sequence converges very quickly and is useful in numerical algorithms. [&] The post Complex AGM first...
16  juin     20h16
Letter-like Unicode symbols
John    Unicode provides a way to distinguish symbols that look alike but have different meanings. We can illustrate this with the following Python code. import unicodedata as u for pair in [(’K’, ’â ’), (’Î ’, ’â ’), (’â ’, ’ ’)]: for c in pair: print(format(ord(c), ’4X’), u.bidirectional(c), u.name(c))...
04  juin     18h55
General tetrahedral numbers
John    Start with a list of ones: 1, 1, 1, 1, 1, ... Taking the partial sums of this sequence gives consecutive numbers. That is, the nth number of the new series is the sum of the first n terms of the previous series. 1, 2, 3, 4, 5, ... If we take partial sums again, [&] The post General tetrahedral...
14  mai     16h23
Sampling with replacement until you’ve seen everything
John    Suppose you have a standard deck of 52 cards. You pull out a card, put it back in the deck, shuffle, and pull out another card. How long would you expect to do this until you’ve seen every card? Here’s a variation on the same problem. Suppose you’re a park ranger keeping data on tagged [&] The post...