3 mathematical tools worth to know as programmer

Seyhak Ly, published on

4 min, 790 words

There are questions that intrigue humanity since the dawn of times:

  • "What is the point of life?",
  • "Is there life after death?",
  • "Does God exists?",
  • "Be or not to be?",
  • "Do I really need to know math as programmer?"

Although all of those questions are very interesting, in this post I will focus on the last one. I will try to convince you that knowledge about some mathematical tools may be useful for programmer by presenting three, I utilized in the past year.

Interpolation

It is simply estimating intermediate value based on other values.

Linear Interpolation of array code

function interpolateArray(data, fitCount){
    const linearInterpolate = (before, after, atPoint) => (
        before + (after - before) * atPoint
    )
    const interpolatedData = []
    const newData = []
    const springFactor = new Number((data.length - 1) / (fitCount - 1))
    newData[0] = data[0]
    for ( let i = 1; i < fitCount - 1; i++) {
        const tmp = i * springFactor
        const before = new Number(Math.floor(tmp)).toFixed()
        const after = new Number(Math.ceil(tmp)).toFixed()
        const atPoint = tmp - before
        newData[i] = linearInterpolate(data[before], data[after], atPoint)
    }
    newData[fitCount - 1] = data[data.length - 1]
    return newData
}

arrayBefore = [1, 10]
arrayAfter = interpolateArray(arrayBefore, 10)
// arrayAfter = [1, 2, 3, ..., 10]

Some of interpolation types other than linear are:

  • Polynomial: approximate complicated curves using adequate equation, example: cubic spline interpolation
  • Nearest neighbour: the simplest method of interpolation. As name points out looks for nearest chart point and assigns its value.

Nearest neighbour

Interpolation is widely used in computer graphics for example to determine transitional colors between two points. This simple usage implies many use cases, as it may be utilized in other tools for example: transforming 2D sprite into 3D. Even if you don’t work in graphic engines industry, it may be handy for data presenting.

I had to use it in this scenario: suppose you need to draw a chart for 30 days period, but... you have data only for every 3rd of the days in the date span. You would like to expand the array, just to fill voids with estimated data. This is where interpolation came in handy. Although there are many types of interpolation. The one most useful here was be linear.

Linear Regression

Linear Regression is tool that transform scalar data into linear one. It is also very useful in estimating future data.

In order to this method of analysis to be valid, the data must meet those requirements:

  • Be continuous, for example: time, sells, energy over time,
  • Be independent, variable A should not be based on previous value,
  • All variables should have the same finite variance,

If we would like to present some trend between related data this is the way to go.

Nearest neighbour

Linear Regression pseudo code

1. Start
2. Read Number of Data (n)

3. For i=1 to n:
     Read Xi and Yi
   Next i

4. Initialize:
     sumX = 0
     sumX2 = 0
     sumY = 0
     sumXY = 0

5. Calculate Required Sum
   For i=1 to n:
     sumX = sumX + Xi
     sumX2 = sumX2 + Xi * Xi
     sumY = sumY + Yi
     sumXY = sumXY + Xi * Yi
   Next i

6. Calculate Required Constant a and b of y = a + bx:
   b = (n * sumXY - sumX * sumY)/(n*sumX2 - sumX * sumX)
   a = (sumY - b*sumX)/n

7. Display value of a and b using y = a + bx
8. Stop

For me it was useful to smooth the chart.

De Morgan's laws

Those equations belong to boolean math's basics and it is used very commonly. Although boolean sentences wrote more straightforwardly are also easier to understand, you may find code using those laws.

De Morgan's laws - equations

!A && !B <=> !(A || B)
!A || !B <=> !(A && B)

!A && !B && !C <=> !(A || B || C)
!A || !B || !C) <=> !(A && B && C)

...

Code example successes

is_album_full = False
is_premium = False
# !(A || B)
if not (is_album_full or is_premium):
  print('Hello1')
# Hello1

# !A && !B 
if not is_album_full and not is_premium:
  print('Hello2')
# Hello2

is_album_full = True
is_premium = False
#!A || !B
if not is_album_full or not is_premium:
  print('Hello3')
# Hello3

# !(A && B)
if not is_album_full or not is_premium:
  print('Hello4')
# Hello4

Code example failures

is_album_full = True
is_premium = True

# !(A || B)
if not (is_album_full or is_premium):
  print('Hello1')

# !A && !B 
if not is_album_full and not is_premium:
  print('Hello2')

#!A || !B
if not is_album_full or not is_premium:
  print('Hello3')

# !(A && B)
if not is_album_full or not is_premium:
  print('Hello4')

I had to use it many times, more rather to understand the code than to write it. Thats why I believe it is the most crucial point out of three presented in the post.

Summary

As you can see it is worth to have in mind that, there are mathematical methods, which may be very effective in solving your problem. Just being aware of what you can achieve by utilizing them is helpful, especially that most of those tools are already implemented in certain libraries.