开发者

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 understandi开发者_如何学Pythonng? I'm willing to listen and read, since I feel this ability is really important for a developer to have.


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.


Use sin(),cos(),sqrt().

For the vectors you need array() and for the sum you need a loop (for/foreach). Vector length = array length, use count.


Well, you can write mathematical ecuations into your code using mathematical API´s. For example in java there is Math for this purpose. Or you can write your own methods(functions) that will be able to perform these operations... let see the ecuation in the posted link. The first ecuation: Suma of absolute values of difference of pi and qi (p with index i). pi and qi you can store them inside an array. Then for the absolute value you can write your own function that is able to do this operation, for example(I used C/C++ style syntax):

int absoluteValue(int number){
     if (number < 0) return number = number * (-1);
     else return number;
}

And for suma too: its only a simple for() cycle. If you have more difficult mathematical operations, of course it is more difficult to implement them. You simply have to write to PC step by step instructions how to perform the operation. Exactly how do you, when you are calculating something.

Hope this was helpful :)


If the goal is to evaluate an expression (involving mathematical functions, operations, etc.) then the procedure is usually fairly straightforward (however complicated in code and computer steps). We might call this explicit mathematics, when an equation's left-hand side is computed by evaluation of the right-hand side.

But equations in mathematical articles can have an entirely implicit meaning and utility. For example a differential equation does not often give a programmer an obvious method to find its solution. These kinds of task require a programmer to supplement the equations found in the literature with some knowledge of numerical analysis.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜