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.