What is a good language to develop in for simple, yet customizable math programs?

I'm writing to ask for some guidance on choosing a language and course of action in learning programming.

I've seen thread after thread with questions from newbies, asking, "What is the best language to start with?" and then it always starts a flame war or someone just answers, "There's no best language, it's best to pick one and start learning it." My question is a little bit more focused than that.

First off, I've been programming my whole life, in very limited capacities. My deepest training was in C++. Whilst in my EECS degree program, I resolved to never be a software developer because I couldn't stand not interacting with people for such long periods of time. Instead I realized I wanted to be a math teacher, and so that is the path I have taken.

But now that I'm well down that path, I've started to realize that perhaps I could develop my own software to help me in the classroom. If I want to demonstrate the Euclidean algorithm, what better way than to have a piece of software that breaks down the process? Students could run that software as part of their studies, and the advanced students might even develop programs for themselves. Or, with an Ipad in hand, why not have an app that lets students take their own attendance? It would certainly streamline some of the needs of classroom management.

There's obviously a lot of great stuff already out there for math, and for education, but I want a way to more directly create things specific to my lectures. If I'm teaching a specific way of calculating a percent, I want to create an app that aligns with my teaching style, not just another calculator app that requires the student to learn twice.

The most I use in class right now is iWork Numbers/Microsoft Excel for my stats class. Students can learn the basic statistical functions, and turn some of their data into graphs.

I have dabbled a bit with R, and used Maple in college. I've started the basic tutorials for OS X/iOS development and have actually made good progress making an OS X app that takes a text string, converts it to numbers, and performs encryption using modular addition and multiplication. I sometimes use Wolfram|Alpha to save myself some time in getting quick solutions to equations or base conversions. I know of MatLab, Mathematica, and recently people have been telling me to check into Python or Ruby. I also know basic HTML, and while it's forgotten now, learned Javascript and PERL in college.

If I keep on the path of Obj-C/Cocoa, I think it will have great benefits. Unfortunately, anything I produced for Mac would only be usable on a Mac, so it wouldn't be universal for all of my students. Perhaps then learning a web language would be better. Second, I'm wondering if the primary use is mathematical, then perhaps my time would be better spent learning Mathematica Programming Language, or R, or something based less on GUI and more on simple coding of algorithms, maybe Python or Ruby?

It seems that Mathematica already has a lot of demos for different math concepts, so why reinvent the wheel is also a question I have. I think overall, it would be good to have more control and design things the way I need. And then, if I do want to make an "Attendance" app or something else, I would already have the programming experience to more easily design something for my iPad or MacBook.

The related question to this is what is a good language to teach to my students? In his TED talk, Conrad Wolfram says one of the best ways to check the understanding of a student is have them write a program. But if Mathematica does the math virtually automatically for them, then I'm not sure that will get the deeper experience of working out logic for themselves, like you do when you're writing C, or a traditional procedural language.

I know that programming takes time to learn, but I also know that at this point, my goal is not to be able to make an app like "Tiny Wings." With the app store ease, some of my work may be an extra revenue stream, but I see myself as more of a hobbyist, and now teacher looking to software development specifically for its ability to help me demonstrate mathematical concepts.

I think I will push ahead with Obj-C/Cocoa for OSX/iOS, but if anyone has some better guidance regarding all of the other available stuff, it would be much appreciated. I don't think I would want to go fully to the web (I like apps), but perhaps someone could suggest a nice way of bridging what I produce in XCode to a universal web version. For example, if you come up with an algorithm in obj-c is it easiest to transition that to ruby and run it online, or is there another approach that works better?


Solution 1:

I'd suggest to look into python because it's a modern language with an easy syntax.

Also for mathematical application you could look at sage. That's basically an extension of python which provides an interface to many mathematical programs (among others maxima, octave, gap, R, etc..).

This is also convenient because you have a "built-in" representation of mathematical objects. For example, if you want to manipulate polynomials, you don't need to define a class to represent polynomials. You do something like that:

def gcd(P,Q):
    F, G, R = P, Q, P % Q

    while (R != 0):
        F, G, R = G, R, F % G

    return G

print gcd(5,6)    # Compute gcd of integers

R.<x> = PolynomialRing(QQ)
P, Q = x^7-1, x^14-1

print gcd(P,Q)    # Compute gcd of polynomials

Solution 2:

Nothing beats Haskell for general-purpose mathematical programming. The wiki's quite extensive and the IRC channel (#haskell on Freenode) is great for asking questions. If you statically link your binaries on compilation, you should be able to run your programs on just about any system (with a few exceptions, e.g., libgmp).

Haskell code reads (roughly) like mathematical notation once you get the hang of it, so it can really help to tie things together for your students who are motivated to write their own programs. The purely functional style can be beneficial, as well, since it focuses less on I/O and the marshalling of data (perfectly useful in applications, perhaps less so in pure math), and more on the actual creation and refinement of functions and algorithms. You can even compose functions just as you would on paper.

If you want to get really serious, you could also look into Coq or Agda, but those might be a bit much for most classes.

For a Haskell program idea for an educator, check out this link.

Wiki Edit: A nice list of arguments can also be found at: Eleven Reasons to use Haskell as a Mathematician and the book The Haskell Road to Logic, Maths and Programming

Code examples:

Greatest Common Divisor

-- Using gcd1 because gcd is already defined in the Prelude
-- Essentially, a function which takes two values in the 
-- Integral typeclass and returns another from the same.
gcd1 :: Integral a => a -> a -> a
gcd1 0 0 = undefined -- ugly, but you don't need to worry about it right now
gcd1 a 0 = abs a
gcd1 a b = gcd1 b (a `mod` b)

In the Haskell Prelude, gcd 0 0 is undefined by convention, but defining your own gcd1 with gcd1 0 0 = 0 may be useful in certain situations. The Wiki article discusses this.

Using this, you can also create a function to compute the $\gcd$ of a list of numbers.

-- Takes a list of Integrals and returns an Integral
gcdList :: Integral a => [a] -> a
gcdList = foldl gcd1 0

The Wiki article on folds may be instructive, in this case.

Euler's totient function

totient n = length [x | x <- [1..n], gcd1 n x == 1]
-- using our function gcd1, though one could also use gcd
-- from the Prelude. Note, this is not an efficient 
-- implementation, just an illustrative one.

Increment

$\lambda x \to x + 1$

f = (\x -> x + 1)
-- or
f = (+ 1) -- both versions are equivalent

Solution 3:

I'm a Java developer by profession, but I wouldn't really recommend it as a language if (a) you're just starting out, and (b) you really want to enjoy programming. For a first time programmer coming from a mathematics background, it can feel quite restrictive (no first class functions - shocking!)

My big recommendation would be Python. It has a beautifully expressive and concise syntax, and it's very powerful. It's also completely free, unlike Mathematica, Matlab or Maple. Here's a couple of examples. The Euclidean algorithm in Python:

def gcd(a,b):
    while b:
        a, b = b, a%b
    return a

Very concise, and the code describes exactly what the algorithm does - no obscure syntax. It's now trivial to write a function that computes Euler's totient function:

def totient(n):
    return len([i for i in range(1,n) if gcd(i,n)==1])

In fact that's slightly inefficient, as it calculates the totient function by keeping a list of all the numbers less than $n$ that are coprime to $n$, and returns the length of the list. But it does indicate how powerful Python is - often you find that a complicated task can be completed with just a couple of lines.

To demonstrate Python's flexibility with functions, here's a function that takes an integer $n$ and returns a function that increments its argument by $n$:

def increment(n):
    return lambda x: x+n

Because Java lacks first-class functions, its not just that it would be more work to write this function: it's actually impossible to write this in Java.

Solution 4:

Perhaps Maple or Mathematica. Especially if your school already has a site license. Then, instead of writing a Euclidean algorithm demonstration of your own, you could look and see what other instructors elsewhere have written and submitted for others to use.