Blog Archives

Root-finding with noisy functions

Suppose you have function g(x) and you want to find x so that g(x) = 0. However, you don’t have direct access to g(x). Instead you can evaluate f(x) which is g(x) plus random noise. Typically f(x) would be an

Tagged with: , ,
Posted in Python, Software development

Calculating pi with AGM and mpmath

This post gives an algorithm based on the arithmetic-geometric mean that rapidly converges to pi. I’ll use it to illustrate multiple precision arithmetic using Python’s mpmath module. Given two non-negative numbers a and b, their arithmetic mean is (a +

Tagged with:
Posted in Math, Python

Using SciPy with IronPython

Three years ago I wrote a post about my disappointment using SciPy with IronPython. A lot has changed since then, so I thought I’d write a short follow-up post. To install NumPy and SciPy for use with IronPython, follow the

Tagged with: , ,
Posted in Python

Machine Learning in Action

A couple months ago I briefly reviewed Machine Learning for Hackers by Drew Conway and John Myles White. Today I’m looking at Machine Learning in Action by Peter Harrington and comparing the two books. Both books are about the same

Tagged with:
Posted in Python

Solutions to knight's random walk

My previous post asked this question: Start a knight at a corner square of an otherwise-empty chessboard. Move the knight at random by choosing uniformly from the legal knight-moves at each step. What is the mean number of moves until

Tagged with: ,
Posted in Python

Python as a Lisp dialect

From Peter Norvig: Basically, Python can be seen as a dialect of Lisp with “traditional” syntax … Python supports all of Lisp’s essential features except macros, and you don’t miss macros all that much because it does have eval, and

Tagged with: ,
Posted in Python

Math languages vs. application languages

Last Friday I posted on @SciPyTip a summary of why I like SciPy, the scientific programming library for Python. I’d rather do math in a general-purpose language than try to do general-purpose programming in a math language. Mathematical software is

Tagged with: ,
Posted in Python

SciPy integration misunderstanding

Today I needed to compute an integral similar to this: I used the following SciPy code to compute the integral: from scipy.integrate import quad def f(x): return 0.01*x**-3 integral, error = quad(f, 1000, sp.inf, epsrel = 1e-6) print integral, error

Tagged with: , ,
Posted in Python

Approximating Earth as a sphere

Isaac Newton suggested in 1687 that the earth is not a perfectly round sphere but rather an ellipsoid, and he was right. But since our planet is roughly a sphere, it’s often useful to approximate it by a sphere. So

Tagged with: , ,
Posted in Math, Python, Science

Mixing R, Python, and Perl in 14 lines of code

This is a continuation of my previous post, Running Python and R inside Emacs. That post shows how to execute independent code blocks in Emacs org-mode. This post illustrates calling one code block from another, each written in a different

Tagged with: ,
Posted in Python

Running Python and R inside Emacs

Emacs org-mode lets you manage blocks of source code inside a text file. You can execute these blocks and have the output display in your text file. Or you could export the file, say to HTML or PDF, and show

Tagged with: , , , ,
Posted in Python

Example of not inverting a matrix: optimization

People are invariably surprised when they hear it’s hardly ever necessary to invert a matrix. It’s very often necessary solve linear systems of the form Ax = b, but in practice you almost never do this by inverting A. This

Tagged with: , ,
Posted in Python

How to compute jinc(x)

The function jinc(x) that I wrote about yesterday is almost trivial to implement, but not quite. I’ll explain why it’s not quite as easy as it looks and how one might implement it in C and Python.

Tagged with: ,
Posted in Python

The Python ecosystem

The hard part about getting started with Python is not the language but the ecosystem. It’s easy to find good references on the Python language, but it’s harder to learn what packages are available, how to install them, etc. That

Tagged with:
Posted in Python

Python is a voluntary language

People who write Python choose to write Python. I don’t hear people say “I use Python at work because I have to, but I’d rather be writing Java.” But often I do hear people say they’d like to use Python

Tagged with: ,
Posted in Python, Software development