Monday, May 14, 2007

Maths and web technology

Like many in the IT industry, I studied maths and science at university. I got used to dealing with concepts fundamental to understanding the world, such as sets, functions, and logic - the same concepts that John Von Neumann and Alan Turing used to define the first computers.

You might think these concepts would carry over to modern computer science - but actually, it's surprising how often the IT industry forgets them, to its own massive detriment.

This might be down to Silicon Valley's eternal optimism that it can rip up the rule book and invent new ways of doing things. It might also be down to the random-walk way in which innovation occurs, or the legacy of many quick patches and minor tweaks.

Either way, I've put together a map of internet technology against the fundamental areas in maths; the gaps show there are many opportunities for improving IT, especially in the areas of equality, geometry, and dynamics.

Sets, Lists, and Trees
A set is probably the most fundamental mathematical concept - an unordered collection of objects. Basic set operations include order (i.e. number of members), union, and intersect.

Sets are only rarely used on the web. Instead, special cases of ordered sets (i.e. lists), or hierarchical sets (i.e. trees), or linked sets (i.e. graphs) are used instead, most obviously in javascript arrays, HTML structure, and search engines respectively.

That's because these extra properties give them more power, and cover most of the use cases. So in this area, web technology maps to maths pretty well.

Equality, Functions and Logic
The equals sign is used so often in HTML and javascript that you might think equality is pretty much covered. But it's not - the equals sign is used to temporarily assign a value to a variable, rather than provide a definition that applies over time.

For example, there is no direct way to say "keep the width of this HTML table at double the value typed in the input box", so that whenever the value changes, the table automatically re-jigs itself. You can do this in spreadsheet formulas without programming events, so why not in javascript?

What's needed is a functional approach to web programming - see my earlier blog entry for more details.

Without a decent foundation for equality, it becomes needlessly complex and awkward to program events and animation. You also end up with many statements controlling variable values, when really only one will do.

One basic area of logic that the web doesn't cater for is automatically re-arranging equations. In the example above, if you manually stretched the HTML table, then the value in the input box should change in order to maintain the equality.

Fields
Integers and real numbers are both examples of fields in maths - sets of numbers with two standard operators defined (addition and multiplication) and identities for each operator (0 and 1 respectively).

I haven't seen any general approach to fields in a programming language - but by covering common special cases (such as integers and real numbers) using data types, I think they're ok.

The obvious other special cases are complex numbers and multi-dimensional fields, e.g. three dimensional vectors. The first is pretty rare except in physics, the second can already be accomplished by manually creating a new data type and overloading the addition, multiplication and equality symbols.

Geometry
The web is pretty poor at basic geometry. Even though it's only two dimensional, HTML restricts itself to static vertical and horizontal coordinates - you can't specify angles, even though the concept is two and a half thousand years old!

SVG is a bit better, in that it defines basic linear transformations (rotations, scaling, and shears). But there's no way to naturally apply these transformations to HTML elements, even in a compound SVG + HTML document. And even in SVG, you can't specify paths using a function like sin(x) - you have to produce a list of pre-calculated points, and rely on the renderer to join them up using a Bezier curve.

Finally, SVG is really missing a trick in not allowing curvilinear coordinates. These allow pages and page elements to be stretched and squished in arbitrary ways. It's just the thing for graphic designers!

Calculus and Dynamics
Calculus is obviously not possible on the web, except by manually creating complicated javascript libraries. There is only one area where it creeps in - in SMIL, you can edit the speed of an audio or video element, and do basic animations.

This is probably because calculus has a reputation as a very technical subject, and the business value is not immediately clear.

But there is one area where calculus' business value is immense - animations. You simply can't use speed and acceleration variables without some understanding of how they relate - which is governed by calculus.

So I recommend a couple of advanced XPath functions

  • speed(node_value) - sets / returns the rate of change of the node value
  • accel(node_value) - sets / returns the acceleration of the node value
These functions really aren't that complicated, but they allow you to implement vastly more powerful dynamics than SMIL - as an earlier blog entry discusses.

They're also reliant on functional programming, where an equation holds true over time. For example, imagine
accel(//div1/@css:left) = //gas_pedal/@value
which would accelerate div1 to the right by the amount held in the gas_pedal node, which could alter with user input.

You simply can't do this using SMIL!

Probability
In mathematics and computer simulations, you often model situations with several possible outcomes. In javascript, the Math.random() function returns an unbiased random number between 0 and 1, which can be used in the modelling.

This single feature is already enough for very powerful models. For example, imagine writing this:
< div width="100 * Math.random() ^ 2"> Hello World < /div>
which assigns a probability distribution to the width, biased towards values near 0px.

Web Technology can learn from mathematics
Even the simple mapping above shows that there are some big opportunities for further development of the web. Because they're based on fundamental mathematics, they're guaranteed to stand the course of time.

Authors of current web specs (e.g. SVG and SMIL) should look at geometry, dynamics, and equality, and integrate centuries of learning into their approach.

1 comment:

stelt said...

you need to be careful in picking your subset of functions to prevent all sorts of trouble like calculation time, error and having multiple solutions