Functions in Mathematics and Programming

Explaining mathematical notation for functions using programming concepts

Although mathematics inspired programming, many people will find it easier to understand programming syntax than mathematical notation.

So I just got the idea to demystify mathematical notation by showing analogies from different programming languages.

Look at this simple math function.

We can express this function in different programming languages. In python we would write:

In my favorite language, Julia you could write this in a number of ways, the long form:

Or you could use shorthand and write it as:

If you tried to write this in a more low-level language such as C, we get a problem:

Specifying Type Using Set Theory

The problem is that we have to specify type, which our math example did not bother with. This does not mean mathematical notation does not allow you to express argument types, quite the contrary. Look at this example:

It defines f as a function which takes an argument x which must belong to the set of all integers . is notation from set theory, meaning "is element of" or "in". In mathematics we use the symbols ℕ, ℤ, ℚ, ℝ to denote the natural numbers, integers, rational numbers and real numbers.

So we are using set theory notation to indicate what type our arguments are.

Most programming languages are not very flexible in how they deal with numbers. They deal with numbers in very concrete ways. We must e.g. specify whether our function takes arguments which are 8, 16, 32 or 64 bit integers. A mathematician will not specify bit size as that is not relevant to mathematical expressions.

However Julia, which was designed for numerical work can deal with numbers in a more mathematical sense.

This says that the argument x is an integer. In Julia this is an abstract type, unlike C, where integer is short for 64 bit or 32 bit integer depending on architecture.

Julia has abstract and concrete numbers, defined in terms of set theory. So Integer is the union of all concrete integer types such as Int32, UInt32, Int64 etc. Thus the C example written in Julia syntax would have to be written as:

Which is short hand for:

If you want to write a function where the argument may be a real number, meaning an integer or floating point number, then you write:

In mathematics we have the full flexibility of set theory to specify valid argument in detail.

This first example is nonsensical since it is the set union of integers and real numbers.

However reals include all integers, but I just wanted to give you an example using the union of two sets.

Here is a more specific example. We limit f to only be defined when xis one of the numbers 1, 2, 3, or 4.

Most programming language don’t have anything near this sort of flexibility to specify number types.

Julia however does at least allow us to take the union of two sets of numbers. Here we are saying x must be a value within the union of all integer and floating point numbers.


In most programming languages you can specify arrays using syntax similar to what you find below:

The first example is of a vector (one dimensional array) and the second example is of a matrix in Julia (two dimensional array). Mathematical notation is similar:

However the notation used for accessing individual elements is different. In Julia we would access a 1D and 2D array like this:

In this case mathematical notation is quite different.

However functions are used in a lot of cases in mathematics where a programmer would use an array or range.

You could look at i as an index into an array, or perhaps more accurately as a dictionary mapping the i value to the function value. We could write this in numerous ways.

But this would be the more common and compact way.

In theory we could also indirectly define such a relationships in this way:

While not common it illustrates how mathematics is declarative. It describes patterns or relationships. Programming languages in contrast are imperative, they explain how to do something.

Anonymous Functions

You could add up all the values of a function in this way.

In Julia I could accomplish the same by writing:

Technically it is not entirely the same since I am using an array which is ordered, while mathematically I have expressed a set. So strictly speaking I would have to write the Julia code like:

As a programmer you may be familiar with closures, lambdas or anonymous functions. Basically we can express the function inline without giving it a name:

I can accomplish the same using mathematical notation with the “maps to” arrow ↦.

However this is uncommon to do in mathematical notation, since it will usually be obvious which variable actually varies. This way however it is explicit.

Higher Order Functions

What we just saw was an example of a higher-order function. This is a concept from functional programming. It means functions which take other functions as arguments or return new functions, rather than just taking values and returning values.

sum was an example of a higher-order function. More common examples are map, reduce and filter.

Here are some examples of using these in Julia:

map takes a list of values and maps them to another list of values using a supplied function.

reduce reduces a list of values into just one value by applying a function that takes two arguments in succession.

Let me explain with an example how it works. Say we reduce a list of numbers using the function f like this:

This translates into:

These functions take functions and return values. But we could also return functions.

This is just a very explicit form of writing it, but we can write in more compact as:

You can use this in Julia like this:

With these examples it is easier to explain the mathematical concept such as derivatives, integrals and transforms.

Function Derivatives as Higher Order Functions

You where probably introduced to derivation and integration in high school, as part of Calculus. As a quick refresher: The derivative of some function f(x) is another function f'(x) which instead of giving the value of the function at x gives the slope of the function at x.

Above is a simple example of a function with its corresponding derivative. If you think about it, differentiation, which is what we call finding the derivative of a function, is really just calling a higher order function.

There are some alternative ways of writing the derivative of a function, which may help hammer home this point. You can also view differentiation as using the operator D on a function.

To be consistent with how we write integral transforms which I will discuss further down, I could invent my own notation.

This should logically speaking work, but I haven’t seen anybody use that. I am just writing it this way to get across that D returns a new function and the (x) actually refers to the argument to this new function, not the argument to f itself. This programming notation may help you get the idea:

Usually in software we handle differentiation in a numerical way, so one would write something like this to find the derivative of f for x = 3:

Thus we could define the D operator like this:

Integrals as Higher Order Functions

We can think of integration in a similar manner. As a quick recap, integration is to find the area underneath a function.

Integrals are define on a range. E.g. the area under f(x) from a to b.

We will usually write the integral of a function from a to b like this:

This is very close to what the higher order function sum does in Julia. Of course it is not exactly the same because sum cannot deal with infinitesimal (incalculably small) values of dx. But we can se that it gives somewhat similar values. E.g. if f(x) = 2x then the integral is We can test that this is roughly the case.

It doesn’t have to be perfect as we are just trying to build an intuition about how this works. The integral is a higher order function because it takes a function as one of its arguments. sum is a semi-crappy approximation of an integral. It can also be used as a higher order function, since its first argument can be a function.

Integrals and derivatives are fairly straight forward in this context. It should help you relate to the integral transforms that I cover next. They are also higher order functions but a bit more complex to wrap your head around.

Integral Transforms

The best known transforms used in mathematics are of a type called integral transforms. Examples of these are the Fourier transform, Laplace transform and convolution (the latter I am not certain of).

We can express an integral reform using mathematical notation like this:

  • T the transform.
  • f function being transformed
  • u argument of function transform evaluates to (function returned by transform in programming speak).
  • K kernel function. Most integral transforms are defined by the kernel function they use.
  • t dependent variable, also called dummy variable. It is the variable that assumes the values in the range t₁ to t₂

While not necessary, you could make it more explicit which variable is the dependent one:

Let us look at the long and short version of expressing something comparable to this in code. First the long version:

We can express this in a more compressed form:

Both examples clearly show that a mathematical Transform is essentially just a higher-order function. It takes some function f(t) and returns another function of u.

Interpretation of Integral Transforms

You can use transforms on functions which take 1 or more arguments. We can use functions taking single arguments to represent a signal such as a radio wave, a sound signal or just about anything.

In this regard you can think of transforms as taking a signal and transforming it to another signal. Since a signal can be thought of as a function, transforming a signal is the same as taking one function as an argument and returning another function.

The picture above shows a Fourier transform. The input function is the signal in red. We can think about the signal as being composed of multiple simple sinus shaped signals, shown in cyan.

A Fourier transform decomposes the red signal into its constituents and give us the function with the blue spikes, one spike for each frequency

Functions taking two arguments could be used to represent an image. The arguments would represent the x, y coordinates. The value of the function would be the light intensity or color at that coordinate. Here is a simple and somewhat contrived example of how such a function could be defined.

Naturally real functions will not be defined like this.

Laplace Transform

I will not explain what the purpose of the Laplace transform is here, but instead shows you some different possible notation for it as well as give examples of how you would translate this into code.

The transform is denoted with the letter , taking a function f of t as argument and return a function F of s.

In Julia code this could be expressed roughly as:

Or in the longer form:

This is of course different from the mathematical definition, because in code we cannot deal with continuous definition. We are dealing with a discrete version. Instead of an infinitely small dt we have to deal with some sensibly picked Δt.

Nor can we deal with an infinitely large range such as 0:∞, but have to pick some practical approximation. The point of showing this code is just to help you understand the principle.

If we look at our previous definition of an integral transform, you can deduce that the kernel function, K in the Laplace transform is:

Fourier Transform

Let us look at some possible ways of defining the Fourier transform using mathematical notation and code.

You can probably tell that our kernel K in this case is:

We can then define the Fourier transform in code as:

Or in the longer form:

Final Remarks

I hope this article helped you to read mathematical notation and get a glimpse of how you can translate mathematics into code.

Mathematics has the luxury of expressing theoretical perfect worlds which we cannot deal with in code directly. In code we need to find some discrete numerical representation of the problem. We would have to make approximations to the real mathematical concept.

We can e.g. approximate integrals as summing over a range of values with some discrete stepping value. Usually an efficient and accurate code version to implement these transforms will be far more complex that the naive implementations you could create directly from the definitions.

Geek dad, living in Oslo, Norway with passion for UX, Julia programming, science, teaching, reading and writing.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store