Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Advice on writing math equations into code

I'm a self taught developer (about 3 years now), and I want to improve my development skills by learning how to write math equations into code.

It's something that continues to bother me, I see many books and articles accompanied by glowing math equations, which genuinely look really interesting. I can read parts of them (multiplication, division, decimals, sigma, variables) but I have troubles when going off to implement them in code.

For example, how would one go about getting started to understand these kind of equations: http://en.wikipedia.org/wiki/Manhattan_distance and going off to write them in code?

Any recommendations of places to get started? Is it not the code issue, but lack of fundamental math understanding? I'm willing to listen and read, since I feel this ability is really important for a developer to have.

like image 483
Dreamslash Avatar asked Jul 31 '11 21:07

Dreamslash


People also ask

How do you write equations in math?

An equation is a mathematical sentence with an equal sign. To translate a word sentence into an equation, choose a variable to represent one of the unspecified numbers or measures in the sentence. This is called defining a variable. Then use the variable to write equations for the unspecified numbers.

Can you write math equations in HTML?

Welcome to a tutorial on how to write Math equations in HTML. The Internet has come a long way from the Stone Age, and HTML is now fully capable of handling various types of content, different languages, images, audio, and videos. But there is seemingly one mystery left unanswered… How do we write Math equations?

How to write math equations in WordPress?

Writing Math Equations in WordPress using MathML MathML is a markup language designed to easily publish mathematical equations on the web. It uses a semantic XML markup similar to HTML. WordPress doesn’t support MathML out of the box, and if you added the MathML code in the post editor, then it will not render correctly.

How do you translate a sentence into an equation?

To translate a word sentence into an equation, choose a variable to represent one of the unspecified numbers or measures in the sentence. This is called defining a variable. Then use the variable to write equations for the unspecified numbers. In the following examples 1-4, translate each sentence into an equation.


1 Answers

I can read parts of them (multiplication, division, decimals, sigma, variables)
but I have troubles when going off to implement them in code.

Well, the crucial thing I guess is that you need to be able to break down the formula you're interested into its component pieces, and if you can understand how to code those pieces individually, you are in a position to then glue them back together in the form of code.

Let us take as an example the Manhattan distance between two points in two-dimensional space with a fixed (x, y) coordinate system. You want to write a function that will take two points and give you the Manhattan distance between those points. Let's forgo the use of object-oriented concepts here and just assume that you have four input variables:

x1, the x-coordinate of the first point
y1, the y-coordinate of the first point
x2, the x-coordinate of the second point
y2, the y-coordinate of the second point

So our function will be like

function mdistance (x1, y1, x2, y2) {
    ???
}

What should the insides of the function (the function body) look like? Now we inspect the mathematical formula that we want to rewrite as code. Wikipedia's version (under "Formal description") treats the case of arbitrary dimensions - we can do this too, but for now we're considering the 2-dimensional case only. So their n is 2 as far as we are concerned, and we want to compute |x1 - x2| + |y1 - y2|. That is the result of losing the sigma notation in favour of an expression describing a sum with two summands. But we still haven't worked out how |a - b| can be expressed in computer code.

So now the function could look like

function mdistance (x1, y1, x2, y2) {
    return bars(x1, x2) + bars(y1, y2);
}

And that, as far as it goes, is fine, because we've isolated the thing we don't yet know how to do as another function, called bars(). Once we define bars(), the function mdistance() will work just fine, assuming of course that our definition for bars() is sensible. So the problem is just to define bars(). By breaking the problem into its component parts, we've made our job easier, because we just have to make each part work - which is simpler than making everything work at once.

So how should bars() be defined? Well, |a - b| just expresses the idea "the absolute value of a - b". PHP has a built-in function for the absolute value of a real number; it is abs(). So we define bars() like this:

function bars (a, b) {
    return abs(a - b);
}

Now our function mdistance() will work just the way we want.

like image 62
Hammerite Avatar answered Sep 22 '22 04:09

Hammerite