## Easy Simpson’s rule

Simpson’s rule, as I’m sure all readers of this blog will know, is a simple rule for numerical integration (or quadrature). It can be obtained in two ways, either by determining the interpolating polynomial over three equally spaced points and integrating that polynomial, or by finding the values $w_i$ for which

$\displaystyle{\int_a^bf(x)\,dx = w_0f(a)+w_1f(a+h)+w_2f(b)}$

(where $h=(b-a)/2$) is accurate for $f(x)=1,x,x^2$.  Over the interval $[0,2]\,$ Simpson’s rule is

$\displaystyle{\int_0^2f(x)\,dx = \frac{1}{3}\Bigl(f(0)+4f(1)+f(2)\Bigr)}$

and over a general interval

$\displaystyle{\int_a^bf(x)\,dx = \frac{b-a}{6}\Bigl(f(a)+4f(a+h)+f(b)\Bigr)}.$

Simpson’s rule has the added advantage that it is also accurate for cubic polynomials.

On its own the rule is not hugely accurate, so one way of increasing the accuracy is to divide the interval into smaller, equally sized regions and apply Simpson’s rule to each of them. This provides the composite Simpson’s rule:

$\displaystyle{\int_a^bf(x)\,dx \approx \frac{b-a}{3n}\Bigl(f(a)+4f(a+h)+2f(a+2h)+4f(a+3h)+\cdots\Bigr.}$

$\displaystyle{\Bigl.+2f(a+(n-2)h)+4f(a+(n-1)h)+f(b)\Bigr)}$

where $n$ is even and $h=(b-a)/n$. Sometimes this rule is written

$\displaystyle{\int_a^bf(x)\,dx \approx \frac{b-a}{6n}\Bigl(f(a)+4f(a+h)+2f(a+2h)+4f(a+3h)+\cdots\Bigr.}$

$\displaystyle{\vspace{1cm}\Bigl.+2f(a+(2n-2)h)+4f(a+(2n-1)h)+f(b)\Bigr)}$

where there is no restriction on $n$ and $h=(b-a)/2n$. For a spirited criticism of this rule, both from practical and pedagogical perspectives, see here.

However, in a standard generic mathematics course, Simpson’s rule is as far as most students get with quadrature. The trouble is that the order of the weights (the coefficients of the function values):

$1,4,2,4,2,4,...,2,4,1$

is tricky to program. Either you have to run two loops, one for the 4′s and the other for the 2′s, or you have to do a little test at each stage to see whether you are at a 2 or 4 point, or you might invoke the modulus function. None of those ways are particularly straightforward, especially if, like many beginning students, you have little programming experience.

So here’s a super easy way. We start with end points $a$ and $b$ and an even integer $n$. We first create a list of all the values at which the function is to be computed:

$xs=[a+kh \mbox{\; for\;} k=0,1,2,\ldots n]$

where $h=(b-a)/n$, and then a list of the weights:

$w=[3-(-1)^k \mbox{\; for\;} k=0,1,2,\ldots n].$

This last list is $[2,4,2,4,\ldots,2,4,2]$.

We then compute

$\sum w_if(xs_i)$

However! note that our list of weights starts and finishes with 2′s instead of 1′s. We correct for this by simply subtracting $f(a)$ and $f(b)$ from the sum. Our “easy” Simpson’s rule is then:

$\displaystyle{\frac{b-a}{2n}\Bigl(\sum w_if(xs_i)-f(a)-f(b)\Bigr)}.$

This is almost trivial to compute. For example, on a TI-nspire CAS calculator, we might have:

$a:=0$

$b:=1$

$\mbox{Define\;}f(x)=e^{-x^2}$

$n:=16$

$h:=(b-a)/n$

$xs:=\mbox{seq}(a+k\cdot h,k,0,n)$

$w:=\mbox{seq}(3-(-1)^k,k,0,n)$

$(\mbox{sum}(w\cdot f(xs))-f(a)-f(b))\cdot (b-a)/n$

The TI-nspire has the nice attribute that a function applied to a list will produce a list of all function values, and the product of two lists is a list containing all the products of corresponding elements. The Casio Classpad has the same functionality with lists, and the above commands can be used.

This approach of using $3-(-1)^k$ to obtaining the weights for Simpson’s rule seems not widely used, but it seems to me to be absurdly simple. I’ve only come across it in one other place.

## Initial comparison of TI-nspire CAS and Casio Classpad 330 calculators

The TI-nspire CAS and the Classpad 330 are the top of the line CAS calculators, and are used in both secondary and tertiary education worldwide.  And each one has its passionate adherents and equally passionate detractors.  However, it’s hard to find a balanced comparison (would you expect to find the TI calculator favourably reviewed in a Casio forum?) TI have released a color version: the TI-nspire CX CAS; and Casio have announced a color version of the ClassPad, to be available in the second half of this year.

For the purposes of this comparison, I’ll use the current monochrome TI-nspire CAS, and the current ClassPad 330.  Here is what they look like:

Also this post will be entirely based on using the handheld calculators, not the computer software which is also available for each of them. And it will be by its very nature superficial; I hope to cover particular topics in more detail in later posts.

Interface

The TI-nspire has a screen size of 320×240 pixels; the ClassPad 160×240, exactly half the size of the TI-nspire. This means that the TI-nspire display is sharper. The TI-nspire has a menu driven system entirely driven by button presses; the ClassPad uses a touch screen controlled with a stylus. I find the TI-nspire much easier to use; I don’t particularly like jabbing the stylus at a screen with such poor resolution. It’s worth noting as a side issue that the stylus is easily lost; they seem to get looser in their housing with age. And many second hand ClassPads are advertised with “stylus missing”.

The ClassPad also defaults on readability. Here’s a couple of screenshots from Casio:

Note that at the top of the screen there is a menu bar, underneath of which is a “toolbar”. At the low resolution of the screen, I find the toolbar icons to be all but unreadable. Some of them are menu headers, which when selected produce a tools menu. This is a list of tiny icons, which to my eyes could mean anything. (Even with my reading glasses, and my eyesight, though not as sharp as once it was, is not that bad.) It may have been a cost issue: producing a touch screen cheaply, but I find the low resolution of the ClassPad a real hindrance to its use.

Commands on the TI-nspire are usually obtained with at most 4 keystrokes: a menu key, then at most 3 menu choices. Commands on the ClassPad, resolution notwithstanding, are also fairly easy to obtain, either with the menu bar, or from the onscreen keyboard. This image:

shows that Casio have in fact designed a very clever interface which makes good use of the limited space to obtain most commands and functions easily.

For comparison, here is a TI screenshot, showing a linear function and some values:

Note that the screen, though maybe cluttered for some, is still quite readable and sharp.

The TI-nspire uses a “Documents” system, and a document may have any number of different “pages”: calculation, geometry, lists & spreadsheet, data & statistics and so on. Variables may be transferred between pages, so that a variable, say a list created and named in a spreadsheet, can be opened in a calculator page. The ClassPad has “eActivities”; here is a description from a Casio page:

An eActivity is a new, exciting innovation. It is small packet of learning that can be written by anyone, even yourself! It is loaded into the calculator and a student is then free to work through it. It is normally a scripted learning experience that has all the functions of the calculator at its disposal. eActivities can be used to present students with assignments, learning opportunities or in fact to build your own simple software…

Mathematical strength

I think that the calculators are much of a muchness here. True, the ClassPad offers some goodies such as the Laplace and Fourier transforms, which might give it a slight edge. However, there are also a few anomalies. The TI-nspire provides LU factorization of a matrix $M$ in the form of the matrices $P,\,L,\,U$, where $P$ is a permutation matrix for which $PM=LU$; the ClassPad just returns $L$ and $U$. This means that given a matrix $M$ the ClassPad will return $L$ and $U$ matrices for which possibly $LU\ne M$. An example is the matrix

$\displaystyle{M=\begin{bmatrix}0&4&3\\ 1&2&-1\\ 3&1&2\end{bmatrix}}$

for which

$\displaystyle{P=\begin{bmatrix}0&0&1\\ 1&0&0\\ 0&1&0\end{bmatrix},\,L=\begin{bmatrix}1&0&0\\ 0&1&0\\ \frac{1}{3}&\frac{5}{12}&1\end{bmatrix},\,U=\begin{bmatrix}3&1&2\\ 0&4&3\\ 0&0&-\frac{35}{12}\end{bmatrix}}$

This may be a small worry, but surely it’s a simple thing and worth getting right?

I haven’t applied (as yet) a CAS test-suite to either calculator. You have to remember that in terms of raw computing power, these calculators are lightweights, and can only be expected to solve relatively small problems. But here’s one test I did yesterday. On the TI-nspire, I found the factorization of Cole’s number:

$2^{67}-1=193707721 \cdot 761838257287$

performed almost immediately. Impressive! Just to check this wasn’t a fluke, I obtained the following factorization:

$24!-1=625793187653 \cdot 991459181683$

in a little under three minutes. I tried the first factorization on the ClassPad and got nothing in a reasonable time (several minutes). So either the TI-nspire uses a better factorizing algorithm, or it has a more powerful processor.

This is an issue I’ll try exploring in more detail in later posts.

Variables and programming

The TI-nspire has three ways of assigning a value to a variable:

• Define x=3
• 3 $\rightarrow$ x (the arrow is obtained by using the “sto” key)
• x:=3 (The “:=” is obtained with two key presses: crtl, :=)

• 3 $\Rightarrow$ x
• x:=3

without, however, any single key to obtain “:=”. And only the arrow method can be used in programs.

Both calculators use a version of Basic on the handhelds. Apparently the scripting language lua can be used with both, but I suspect only on the associated computer software, and not directly on the handhelds. And they both provide the full gamut of control statements (loops, tests etc). Both calculators distinguish between functions and programs, although in slightly different ways.

The ClassPad offers very nice sequence handling: you can set up a number of interlinked sequences, and set ‘em off. For example, you can enter

$\displaystyle{\begin{array}{rl}a_{n+1}&=(4-b_n+c_n)/4\\ a_0&=1\end{array}}$

$\displaystyle{\begin{array}{rl}b_{n+1}&=(2-2\times a_n-c_n)/5\\ b_0&=1\end{array}}$

$\displaystyle{\begin{array}{rl}c_{n+1}&=(11-a_n)/3\\ c_0&=1\end{array}}$

(which are the sort of equations which may be encountered in Jacobi’s iteration method for linear equations), in the Sequence page, and just run them. The TI-nspire has a “Lists & Spreadsheet” page, in which you can perform similar calculations, but not as elegantly as this.

As a mini-comparison, here are for-loops on each:

 TI-nspire ClassPad For i,1,10x:=x+1EndFor For 1$\Rightarrow$i to 10x+1$\Rightarrow$xNext

Graphics

Both calculators support 2D graphing (they would hardly have a market if they didn’t). The ClassPad also supports 3D graphing, which is supposed to be a huge feather in its cap, but in fact I find this to be of limited use. Just as an experiment, I attempted to plot the monkey saddle surface $z=x^3-3xy^2$ and couldn’t make any sense of the result. Even with zooming, moving the graph around with the stylus, I couldn’t obtain any intuitive feel of the shape of the graph. Partly this is because there doesn’t seem to be any hidden line algorithm available. However, the drag and drop capability of the ClassPad, with the stylus, is one area which it shines. The TI-nspire’s touchpad, although it works, is clumsy in comparison.

Which is better?

I much prefer (for my purposes) the TI-nspire’s sharper screen and interface. However, when the new colour model fx-CP400 is released I will certainly reconsider. I have no loyalty to either brand. In the end, it comes down to what you and everybody else is using. My daughter is using a ClassPad 330 for her year 11 mathematics (its use is mandated by her school) and is very satisfied with it. I would imagine that even with the models discussed in this post (the monochrome TI-nspire already superseded, the ClassPad 330 about to be), either one would happily manage pretty much all school, and most college, mathematics.

## Maxima on Android

Such a long long time since I last posted!  Very poor blogging etiquette.  Anyway, to break the drought, here’s news of the best thing to come my way in the last six months – fully featured Maxima running on Android (this screenshot from its website):

At a conference I attended late last year, sponsored by calculator companies, the Maple reps (both of whom I knew), claimed that calculators were an old-fashioned outmoded technology, especially now that with smartphones, ipods etc, you can have access to the Internet in your pocket (Wolfram Alpha, anybody)?  I still think there’s a lot to be said though for a device which you can use independently of a network, and in fact I believe most calculator users use very little of their functionality.  How many CAS calculator users, I wonder, program on or with their machines?

But now here’s a fully featured, mature CAS running on a portable device – the first such one, as far as I know.  There are in fact other CAS’s available for portable systems: PocketCAS, Mathomatic, Yacas (which I’ve written about before), no doubt others.  None of them, as far as I can tell, work particularly well, or are in any way complete.  It doesn’t take long before you run up against a wall of something they are unable to do.

So I see Maxima as being the first complete portable CAS – and I’m delighted to see it on Android.  It works fine on a smartphone, and is quite wonderful on a tablet.

## Cyclic quadrilaterals & Brahmagupta’s formula

I suppose every reader of this ‘ere blog will know Heron’s formula for the area $K$ of a triangle with sides $a,b,c$:

$K = \sqrt{s(s-a)(s-b)(s-c)}$

where $s$ is the “semi-perimeter”:

$\displaystyle{s=\frac{a+b+c}{2}.}$

The formula is not at all hard to prove: see the Wikipedia page for two elementary proofs.

However, I have only recently become aware of Brahmagupta’s formula for the area of a cyclic quadrilateral. A cyclic quadrilateral, if you didn’t know, is a (convex) quadrilateral all of whose points lie on a circle:

And if the edges have lengths $a,b,c,d$ as shown, then the formula states that the area is given by

$K = \sqrt{(s-a)(s-b)(s-c)(s-d)}$

where as above $s$ is the semi-perimeter:

$\displaystyle{s=\frac{a+b+c+d}{2}.}$

This can be seen to be a generalization of Heron’s formula. Although the formula is named for Brahmagupta (598 – 670), who does indeed seem to have been the first to state it, there is no evidence that he had a proof, or even recognized that the formula was valid only for cyclic quadrilaterals.

There are of course innumerable proofs of Brahmagupta’s formula; one elegant way starts by noting that any formula for the area of a cyclic quadrilateral in terms of edge lengths must be symmetric in its variables. We can see this by considering the four triangles from the center of the circle: they can be arranged in any order, and the total area of course is unchanged. If we assume that the area $K^2$ is a symmetric 4-degree polynomial in the variables, then it’s very straightforward to obtain the result.

Cyclic quadrilaterals have a host of wonderful theorems and results. Here’s one I particularly like – it’s a Sangaku problem:

Suppose that $A,B,C,D$ are the vertices of a cyclic quadrilateral. Then the centres of the incircles of the four triangles $ABC, ABD, ACD, BCD$ form a rectangle.

Here’s a picture (created with geogebra):

You can find an angle-chasing proof here.

## Creativity and mathematics

Recently, in The Conversation, the Vice Chancellor of Monash University, wrote an article discussing MOOCs. He made some criticisms about the nature of assessment and grading that MOOCs offer. However, my attention was grabbed by two sentences:

The other major problem the MOOCs haven’t solved is assessment. They work very well for subjects like maths, which have objectively right and wrong answers, and can therefore be pretty easily marked by computers.

Now, here we have the Vice Chancellor of one of Australia’s leading universities – and indeed, one of the world’s leading universities (and incidently the University where I did both my Masters and my PhD) demonstrating an extraordinary lack of understanding about the fundamental nature of mathematics. He seems to think that mathematics is all about teaching students (in the fine words of John Power from Leeds University) about “finding ‘x’”. I suppose he thinks this is what mathematicians do: they “find ‘x’”.

But this seems to me to be symptomatic of how the world at large sees mathematics and mathematicians: that mathematics is, in essence, a cut-and-dry discipline, with objectively “correct answers”. And mathematicians are seen as dry creatures whose discipline and practice totally lacks the creativity that one might see, for example, in the arts. A poet, a painter, even a literary critic, must needs demonstrate creativity in their work, but not so for the mathematician.

Part of the problem is the way mathematics is taught at schools: from the earliest days of kindergarten onwards, mathematics is taught as if everybody is going to be an engineer. (Please don’t take this as a slight against engineers, for whom I have an almost infinite respect; this is merely a comment on the sort of mathematics that engineers are taught and use.) There is a careful gradation from counting, measurement, through to arithmetic, algebra and geometry, culminating in calculus. You’ll notice one thing missing: proofs!

All mathematicians know that it is the concept of proof which makes mathematics into an exciting, creative intellectual discipline. Here there is no “finding ‘x’”, but the deep, sustained, concentrated endeavour to provide a link between two apparently disparate abstract entities. Right from Pythagoras’ theorem (or, as one of my school teachers liked to say: Euclid Book 1 proposition 47) which magically joins algebra and geometry, right up to say, the classification of finite simple groups, proofs have been the excitement and glory of mathematics.

I know I’m biased, but I can’t think of any human activity which requires the same degree of creativity as does mathematics. There are no short cuts, there are no hiding places: a proof, like a sculpture, is exposed to the cold light of day. And this was also the view of Bertrand Russell:

Mathematics, rightly viewed, possesses not only truth, but supreme beauty – a beauty cold and austere, like that of sculpture.

Another British mathematician, G. H. Hardy, put it this way:

A mathematician, like a painter or poet, is a maker of patterns. If his patterns are more permanent than theirs, it is because they are made with ideas.

It is this lack of exposure to the excitement of proofs (and there are plenty of proofs which require very little background, such as Euclid’s proof of the infinity of the primes, and some proofs of Pythagoras’ theorem) which is partly to blame for the low esteem in which mathematicians are held.

And of course this strikes at the very heart of my complaint of the quote from the Monash Vice Chancellor. Yes, you may well ask a computer to check if you have correctly “found ‘x’”, but that is hardly mathematics, or if it is, it’s only a very tiny corner of it. It’s as if “painting” meant not the glories of Rembrandt, Gainsborough, Jackson Pollock but the necessary work of the house painter. I haven’t yet seen an online system which will help in any way budding mathematicians work their way through proofs – their creation, and their evaluation.

One of the reasons I’m not a very good mathematician is that I don’t have enough of the level of creativity that good mathematics requires: I find it hard to see how a proof can be built. However, I have enough mathematical background to appreciate fine mathematics. And this is what I also try to do with my students: the curriculum may well require them to “find ‘x’”, but I try to help them build intellectual bridges between ideas. And of course, the excitement when they realize that they can indeed do this is possibly the greatest delight a teacher can know.

Please, fellow mathematicians and mathematics educators – don’t take ignorant slights against mathematics lying down. Over two thousand years of sustained human effort by some of the greatest minds in history deserves more than “pretty easily marked by computers”.

## The best Matlab alternative (2)

A year or so ago, I published a blog post called The best Matlab Alternative.  In that post, I discussed the merits of Scilab, Octave and Freemat, and decided that for me, Octave suited me best.  However, Scilab is probably the more full featured and powerful.

I’ve since discovered that there’s another contender, in some ways better than all of the above: Python.  My requirement for a “Matlab alternative” was that:

1. It must install under Windows. This is absolutely necessary for me, at least, since most of my students use Windows. A very few use OSX, and hardly any use Linux.
2. It must, as far as possible, be identical in use with Matlab. The commands should be the same, and simple scripts and functions should transfer between the two with no effort.
3. It should have a reasonably nice interface, as similar as possible to Matlab’s own. A basic command line only interface running in a terminal won’t cut it with my students.

Suppose we drop condition 2, and require software which can do pretty much most of what Matlab can do, in a nice visual environment, we find Python coming out looking very good indeed.  Now, I have been a lover of Matlab, and its open source alternatives, for many years, and I do like the rapid prototyping possible with Matlab, as well as its ease for dealing with large data types.  And vectorization (where a single command is automatically applied to every element of a multi-dimensional array) provides some wonderfully elegant and simple code.  (It can also of course provide for dense obtuseness.)

However, I have also become aware of some criticisms of Matlab.  A major one is vendor lock-in: the code, the language, the IDE, is all owned by The Mathworks.  If they decide to radically change something, you have no alternative but keep paying the license fees to keep up.  Second is that the language has no object-oriented capabilities, at least in a natural way;(see comment below) that the language has no standard as such – it’s just whatever The Mathworks decide to provide.  Thirdly is the namespace: all functions exist as top-level objects, and all functions (“m-files”) in the Matlab path (which usually will include multiple directories) are available when you start up.  Fourth is that an m-file can only contain one function (and maybe some sub-functions), whose name must be the same name as the file.  This means that if you create some new functions – and most users do – you need to be sure that it doesn’t conflict with any other functions.  This problem is exacerbated if you include lots of toolboxes.  The Image Processing Toolbox alone adds 270 new functions to the Matlab namespace  and there are 38 other toolboxes available.  That’s a lot of functions.

One list of criticisms is available here; another (called “Abandon Matlab”) is here.  Note that the second site recommends (as well as Python), R, which the first site also pans.  There’s no accounting for tastes.

So, back to Python.

Python is a deservedly popular language, supported by a huge user base, tons of books, articles, websites, conferences, seminars, and so on.  It is fully open source, and it is managed by a non-profit organization called the Python Software Foundation.

Python has lots of libraries for scientific computing, starting with numpy (“numeric python”) and scipy (“scientific python”).   And there are others – such as scikits-image which extends scipy’s image processing capabilities (the Python Imaging Library does not seem to be currently in active development).  Many more are listed here.

What’s more, Python has lots of IDEs which mean that you can use it pretty much as you would use Matlab, Octave or Scilab.  For a nice visual IDE with all the trimmings, there are Eric IDE, Spyder, or PyDev, which is an Eclipse plugin, plus lots more, both open source and commercial.

For those of us who are satisfied with less, there’s iPython, an enhanced shell (which is what I like to use).  And there are again lots more, including editors and all sorts of other goodies for development, here.

Let’s finish with a tiny example: to create a 1500 x 1500 random matrix, invert it, and check the trace of the product of the matrix and its inverse.  I’ll do all this in the iPython shell, which I invoke from my linux command line with ipython.

Once in the iPython shell, we import the pylab modules; this includes numpy, scipy, and the plotting library matplotlib:

In[1]: from pylab import *


(In fact this can be done from the command line with iPython, but this command should work in any Python environment.)

Now to define the matrix, compute its inverse, and check the trace of their product:

In [2]: m = mat(rand(1500,1500))
In [3]: mi = linalg.inv(m)
In [4]: np.trace(m*mi)
Out[4]: 1500.0


And of course all this is nice and fast (on my linux laptop):

In [5]: %time m = mat(rand(1500,1500)); mi = linalg.inv(m); np.trace(m*mi)
CPU times: user 6.52 s, sys: 0.21 s, total: 6.73 s
Wall time: 3.78 s