Sympy should suffice

June 6, 2013 Leave a comment

I have just received a copy of Instant SymPy Starter, by Ronan Lamy—a no-nonsense guide to the main properties of SymPy, the Python library for symbolic mathematics. This short monograph packs everything you should need, with neat examples included, in about 50 pages. Well-worth its money.

To celebrate, I would like to pose a few coding challenges on the use of this library, based on a fun geometric puzzle from cut-the-knot: Rhombus in Circles

Segments \overline{AB} and \overline{CD} are equal. Lines AB and CD intersect at M. Form four circumcircles: (E)=(ACM), (F)=(ADM), (G)=(BDM), (H)=(BCM). Prove that the circumcenters E, F, G, H form a rhombus, with \angle EFG = \angle AMC.

rhombusincircles

Note that if this construction works, it must do so independently of translations, rotations and dilations. We may then assume that M is the origin, that the segments have length one, A=(2,0), B=(1,0), and that for some parameters a>0, \theta \in (0, \pi), it is C=(a+1) (\cos \theta, \sin\theta), D=a (\cos\theta, \sin\theta). We let SymPy take care of the computation of circumcenters:

import sympy
from sympy import *

# Point definitions
M=Point(0,0)
A=Point(2,0)
B=Point(1,0)
a,theta=symbols('a,theta',real=True,positive=True)
C=Point((a+1)*cos(theta),(a+1)*sin(theta))
D=Point(a*cos(theta),a*sin(theta))

#Circumcenters
E=Triangle(A,C,M).circumcenter
F=Triangle(A,D,M).circumcenter
G=Triangle(B,D,M).circumcenter
H=Triangle(B,C,M).circumcenter

Finding that the alternate angles are equal in the quadrilateral EFGH is pretty straightforward:

In [11]: P=Polygon(E,F,G,H)

In [12]: P.angles[E]==P.angles[G]
Out[12]: True

In [13]: P.angles[F]==P.angles[H]
Out[13]: True

To prove it a rhombus, the two sides that coincide on each angle must be equal. This presents us with the first challenge: Note for example that if we naively ask SymPy whether the triangle \triangle EFG is equilateral, we get a False statement:

In [14]: Triangle(E,F,G).is_equilateral()
Out[14]: False

In [15]: F.distance(E)
Out[15]: Abs((a/2 - cos(theta))/sin(theta) - (a - 2*cos(theta) + 1)/(2*sin(theta)))

In [16]: F.distance(G)
Out[16]: sqrt(((a/2 - cos(theta))/sin(theta) - (a - cos(theta))/(2*sin(theta)))**2 + 1/4)

Part of the reason is that we have not indicated anywhere that the parameter theta is to be strictly bounded above by \pi (we did indicate that it must be strictly positive). The other reason is that SymPy does not handle identities well, unless the expressions to be evaluated are perfectly simplified. For example, if we trust the routines of simplification of trigonometric expressions alone, we will not be able to resolve this problem with this technique:

In [17]: trigsimp(F.distance(E)-F.distance(G),deep=True)==0
Out[17]: False

Finding that \angle EFG = \angle AMC with SymPy is not that easy either. This is the second challenge.

How would the reader resolve this situation?


Instant SymPy Starter

A nice application of Fatou’s Lemma

June 1, 2013 Leave a comment

Let me show you an exciting technique to prove some convergence statements using exclusively functional inequalities and Fatou’s Lemma. The following are two classic problems solved this way. Enjoy!

Exercise 1 Let {(X, \mathcal{F}, \mu)} be a measurable space and suppose {\{f_n\}_{n\in \mathbb{N}}} is a sequence of measurable functions in {L_1(\mu)} that converge almost everywhere to a function {f \in L_1(\mu)}, and such that the sequence of norms {\big\{ \lVert f_n \rVert_1 \big\}_{n \in \mathbb{N}}} converges to {\lVert f \rVert_1}. Prove that the sequence of integrals {\int_E \lvert f_n \rvert\, d\mu} converges to the integral {\int_E \lvert f \rvert\, d\mu} for every measurable set {E}.

Proof: Note first that

\displaystyle  \begin{array}{rcl}  \lvert f_n - f\rvert\, \boldsymbol{1}_E \leq \lvert f_n - f \rvert \leq \lvert f_n \rvert + \lvert f \rvert. \end{array}

Set then {g_n = \lvert f_n \rvert + \lvert f \rvert - \lvert f_n-f \rvert\, \boldsymbol{1}_E} (which are non-negative functions) and apply Fatou’s Lemma to that sequence. We have then

\displaystyle  \begin{array}{c}  \int_X \liminf_n g_n\, d\mu \leq \liminf_n \int_X g_n\, d\mu \\ \\ \int_X \liminf_n \big( \lvert f_n \rvert + \lvert f \rvert - \lvert f_n - f \rvert\, \boldsymbol{1}_E \big)\, d\mu \leq \liminf_n \int_X \big( \lvert f_n \rvert + \lvert f \rvert - \lvert f_n - f \rvert\, \boldsymbol{1}_E \big)\, d\mu \\ \\ \int_X \big( 2\lvert f \rvert - \limsup_n \lvert f_n-f \rvert\, \boldsymbol{1}_E\big)\, d\mu \leq \liminf_n \int_X \lvert f_n \rvert\, d\mu + \lVert f \rVert_1 - \limsup_n \int_E \lvert f_n -f \rvert\, d\mu \\ \\ 2\lVert f\rVert_1 \leq 2\lVert f \rVert_1 - \limsup_n \int_E \lvert f_n - f\rvert\, d\mu, \end{array}

which implies

\displaystyle  \begin{array}{rcl}  0 \leq \liminf_n \int_E \lvert f_n-f \rvert\, d\mu \leq \limsup_n \int_E \lvert f_n-f \rvert\, d\mu \leq 0. \end{array}

It must then be {\int_E \lvert f_n - f \rvert\, d\mu = 0}. But this proves the statement, since

\displaystyle  \begin{array}{c}  \bigg\lvert \int_E \lvert f_n\rvert\, d\mu - \int_E \lvert f \rvert\, d\mu \bigg\rvert = \bigg\lvert \int_E \big( \lvert f_n \rvert - \lvert f \rvert \big)\, d\mu \bigg\rvert \\  \leq \int_E \Big\lvert \lvert f_n \rvert - \lvert f \rvert \Big\rvert\, d\mu \leq \int_E \lvert f_n - f \rvert\, d\mu \end{array}

\Box

Exercise 2 Let {(X, \mathcal{F}, \mu)} be a finite measure space and let {1<p<\infty}. Suppose that {\{ f_n \}_{n \in \mathbb{N}}} is a sequence of measurable functions in {L_p(\mu)} whose norms are uniformly bounded in {n} and which converge almost everywhere to a function {f}. Prove that the sequence {\big\{ \int_X f_ng\, d\mu \big\}_{n \in \mathbb{N}}} converges to {\int_x fg\, d\mu} for all {g \in L_q(\mu)} where {q} is the conjugate exponent of {p}.

Proof: The proof is very similar to the previous problem. We start by noticing that under the hypotheses of the problem,

\displaystyle  \begin{array}{c}  \bigg\lvert \int_x f_ng\, d\mu - \int_X fg\, d\mu \bigg\rvert = \bigg\lvert \int_X (f_n -f)g\, d\mu \bigg\rvert \\ \leq \lVert (f_n-f)g \rVert_1 \leq \lVert f_n - f \rVert_p\, \lVert g \rVert_q. \end{array}

If we prove that {\lim_n \lVert f_n-f \rVert_p = 0}, we are done.

We will achieve this by using the convexity of {\lvert \cdot \rvert^p}, since in that case it is

\displaystyle  \begin{array}{rcl}  \frac{\lvert f_n - f \rvert^p}{2^p} \leq \tfrac{1}{2} \lvert f_n \rvert^p + \tfrac{1}{2} \lvert f \rvert^p. \end{array}

hence,

\displaystyle  \begin{array}{rcl}  \lvert f_n -f \rvert^p \leq 2^{p-1} \big( \lvert f_n \rvert^p + \lvert f \rvert^p \big). \end{array}

Set then {g_n = 2^{p-1} \big( \lvert f_n\rvert^p + \lvert f \rvert^p\big) - \lvert f_n-f \rvert^p} (which are non-negative functions) and apply Fatou’s Lemma as before. \Box

Have a child, plant a tree, write a book

April 23, 2013 Leave a comment

Or more importantly: rear your children to become nice people, water those trees, and make sure that your books make a good impact.

I recently enjoyed the rare pleasure of having a child (my first!) and publishing a book almost at the same time. Since this post belongs in my professional blog, I will exclusively comment on the latter: Learning SciPy for Numerical and Scientific Computing, published by Packt in a series of technical books focusing on Open Source software.

Keep in mind that the book is for a very specialized audience: not only do you need a basic knowledge of Python, but also a somewhat advanced command of mathematics/physics, and an interest in engineering or scientific applications. This is an excerpt of the detailed description of the monograph, as it reads in the publisher’s page:

It is essential to incorporate workflow data and code from various sources in order to create fast and effective algorithms to solve complex problems in science and engineering. Data is coming at us faster, dirtier, and at an ever increasing rate. There is no need to employ difficult-to-maintain code, or expensive mathematical engines to solve your numerical computations anymore. SciPy guarantees fast, accurate, and easy-to-code solutions to your numerical and scientific computing applications.

Learning SciPy for Numerical and Scientific Computing unveils secrets to some of the most critical mathematical and scientific computing problems and will play an instrumental role in supporting your research. The book will teach you how to quickly and efficiently use different modules and routines from the SciPy library to cover the vast scope of numerical mathematics with its simplistic practical approach that is easy to follow.

The book starts with a brief description of the SciPy libraries, showing practical demonstrations for acquiring and installing them on your system. This is followed by the second chapter which is a fun and fast-paced primer to array creation, manipulation, and problem-solving based on these techniques.

The rest of the chapters describe the use of all different modules and routines from the SciPy libraries, through the scope of different branches of numerical mathematics. Each big field is represented: numerical analysis, linear algebra, statistics, signal processing, and computational geometry. And for each of these fields all possibilities are illustrated with clear syntax, and plenty of examples. The book then presents combinations of all these techniques to the solution of research problems in real-life scenarios for different sciences or engineering — from image compression, biological classification of species, control theory, design of wings, to structural analysis of oxides.

The book is also being sold online in Amazon, where it has been received with pretty good reviews. I have found other random reviews elsewhere, with similar welcoming comments:

Project Euler with Julia

January 4, 2013 Leave a comment

Disclaimer: Project Euler discourages the posting of solutions to their problems, to avoid spoilers. The three solutions I have presented in my blog are to the three first problems (the easiest and most popular), as a means to advertise and encourage my readers to “push the envelope” and go beyond brute-force solutions.

Just for fun, and as a means to learn Julia, I will be attempting problems from the Project Euler coding exclusively in that promising computer language.

The first problem is very simple:

Multiples of 3 and 5
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multip les is 23.

Find the sum of all the multiples of 3 or 5 below 1000.

One easy-to-code way is by dealing with arrays:

julia> sum( filter( x->((x%3==0) | (x%5==0)), [1:999] ) )
233168

A much better way is to obtain it via summation formulas: Note that the sum of all multiples of three between one and 999 is given by

3+6+\dotsb+999 = 3 (1+2+\dotsb+333) = 3 \frac{333\cdot 334}{2} = 166833.

Similarly, the sum of all multiples of five between one and 995 is given by

5+10+\dotsb+995 = 5 (1+2+\dotsb+199) = 5 \frac{199\cdot 200}{2} = 99500.

Finally, we need to subtract the sum of the multiples of both three and five, since they have been counted twice. This sum is

15+30+\dotsb+990 = 15 (1+2+\dotsb+66)= 15 \frac{66\cdot 67}{2}= 33165.

The final sum is then, 166833+99500-33165=233168.

Seked

December 16, 2012 Leave a comment

If a pyramid is 250 cubits high and the side of its base 360 cubits long, what is its seked?


Take half of 360; it makes 180. Multiply 250 so as to get 180; it makes 1/2 1/5 1/50 of a cubit. A cubit is 7 palms. Multiply 7 by 1/2 1/5 1/50:

\begin{array}{l|lll} 1&7&&\\ 1/2&3&1/2& \\ 1/5 &1&1/3&1/15\\ 1/50&&1/10&1/25 \end{array}

The seked is 5 \tfrac{1}{25} palms [that is, (3+1/2) + (1+1/3+1/15) + (1/10+1/25)].

A’h-mose. The Rhind Papyrus. 33 AD

The image above presents one of the problems included in the Rhind Papyrus, written by the scribe Ahmes (A’h-mose) circa 33 AD. This is a description of all the hieroglyphs, as translated by August Eisenlohr:

The question for the reader, after going carefully over the English translation is: What does seked mean?

Nezumi San

December 14, 2012 Leave a comment

On January first, a pair of mice appeared in a house and bore six male mice and six female mice. At the end of January there are fourteen mice: seven male and seven female.

On the first of February, each of the seven pairs bore six male and six female mice, so that at the end of February there are ninety-eight mice in forty-nine pairs. From then on, each pair of mice bore six more pairs every month.

  1. Find the number of mice at the end of December.
  2. Assume that the length of each mouse is 4 sun. If all the mice line up, each biting the tail of the one in front, find the total length of mice
Jinkō-ki, 1715

Ruthless Thieves Stealing a Roll of Cloth

December 14, 2012 Leave a comment

Some thieves stole a long roll of silk cloth from a warehouse. In a bush far from the warehouse, they counted the length of the cloth. If each thief gets 6 hiki, then 6 hiki is left over, but if each thief takes 7 hiki then the last thief gets no cloth at all. Find the number of thieves and the length of the cloth.

Jinkō-ki, 1643
Follow

Get every new post delivered to your Inbox.

Join 46 other followers

%d bloggers like this: