Tips for writing mathematical equations in code

I am a self-learning developer (about 3 years), and I want to improve my development skills by learning to write mathematical equations in code.

Something that bothers me, I see a lot of books and articles, accompanied by luminous mathematical equations that really look really interesting. I can read parts of them (multiplication, division, decimals, sigma, variables), but I have problems with their implementation in the code.

For example, how would you begin to understand such equations: http://en.wikipedia.org/wiki/Manhattan_distance and leave to write them into code

Any recommendations on places to start? Isn't that a code problem, but a lack of a fundamental understanding of mathematics? I am ready to listen and read, since I believe that this ability is really important for the developer.

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

It’s good that I believe that you need to break down the formula that you are interested in into its components, and if you can understand how to code these parts individually, you can then glue them back in code form.

Take the Manhattan distance between two points in two-dimensional space with a fixed coordinate system (x, y) as an example. You want to write a function that takes two points and gives you the Manhattan distance between these points. Let me stop using 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 look like

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

What should the internal functions (function body) look like? Now we are checking the mathematical formula that we want to rewrite as code. The Wikipedia version (in the "Formal Description" section) considers the case of arbitrary measurements - we can do this too, but now we are only considering the two-dimensional case. Therefore, their n is 2, as far as we know, and we want to calculate |x1 - x2| + |y1 - y2| |x1 - x2| + |y1 - y2| . This is the result of the loss of sigma notation in favor of an expression describing the sum with two terms. But we still haven't figured out how |a - b| can be expressed in computer code.

So now the function may look like

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

And this, as far as possible, is beautiful, because we isolated what we don’t yet know how to do as another function called bars() . Once we define bars() , the mdistance() function will work fine, assuming, of course, that our definition for bars() reasonable. Therefore, the problem is only in the definition of bars() . Parsing the problem into its component parts, we simplified our work, because we just need to make each part work, which is easier than making everything work right away.

So how to define bars() ? Well, |a - b| just expresses the idea of ​​"absolute value a - b ". PHP has a built-in function for the absolute value of a real number; this is abs() . Therefore, we define bars() as follows:

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

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

+11
source

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

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

0
source

Well, you can write math expressions to your code using math APIs. For example, Java has math for this purpose. Or you can write your own methods (functions) that can perform these operations ... let's look at the exchange in the posted link. First assessment: Sum of the absolute values ​​of the difference pi and qi (p with index i). pi and qi you can store them inside an array. Then for an absolute value, you can write your own function that can perform 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 simple for () loop. If you have more complex mathematical operations, they are more difficult to implement. You just need to write step-by-step instructions on the PC how to perform the operation. Just like you when you figure something out.

Hope this was helpful :)

0
source

If the goal is to evaluate the expression (including mathematical functions, operations, etc.), then the procedure is usually quite simple (no matter how complicated it is in the steps of the code and computer). We can call this explicit mathematics when the left side of the equation is calculated by estimating the right side.

But equations in mathematical articles can have completely implicit meaning and utility. For example, a differential equation often gives the programmer an obvious way to find his solution. These tasks require the programmer to supplement the equations found in the literature with some knowledge of numerical analysis.

0
source

All Articles