Math

Gael Gil · February 1, 2019

In this blogpost I will be talking about the how we can represent math in code. The similarities and differences and how code can make summnation easier to understand Also in this blog post I will also be refering to arithmetic mean i’ll be refering to it as just mean for shorts.

The average of an array, \(x\) can be written as \(\bar{x}\) and is defined as:

\[\bar{x} = \frac{1}{n} \left( \sum_{i=1}^{n} x_i \right)\]

This is sumation notation and it can be very confusing at first but \(\left( \sum_{i=1}^{n} x_i \right)\) simply just tells us to add up all the elements of n starting from 1 (the sum of the array). Once we have the sum we multiply \(\frac{1}{n}\) to it. Which will leave us with the sum of n over n.

We can also write it in detail as:

\[\bar{x} = \frac{x_1+x_2+....+x_n}{n}\]

Written out in detail here, we are adding up every element of the array in the top which will get us the sum of the array, we have the sum over n which represents the ammount of elements/numbers.

All together we have this

The prevoius examples showed different ways we can represent the mean and all of put together we have this

$\bar{x} = \frac{1}{n} \left( \sum_{i=1}^{n} x_i \right) = \frac{x_1+x_2+….+x_n}{x} $

This confusing but this just shows us the levels of abstraction. From being supper deatiled on the right and getting simpler the more. We can represent summation notation in programing with a for loop that can make understanding it easier. Here are some examples of getting the mean using c++, python, and python libraries.

Summation in Code

Now I will be talking about how we can represent summnation in code and how it looks like across some languages but first I will go over a small code rule. In coding we always start counting from zero unless specified otherwise.

C++

We start a for loop and set int i = 0. This will help us access all the elements of the array. We select an element of the array by doing n[i] and we add that to the sum (sum += n[i];). We go until the end of the array and once the loop is done we set the mean equal to the sum over the number of elements in the array. mean = (sum/3)

#include <iostream>
using namespace std;

int main(){

    int n[] = {10, 20, 30};
    int sum = 0;
    int mean;
    for(int i = 0; i < 3; i++){
        sum += n[i];
    }

    mean = (sum/3);

    cout << mean << endl;

    return 0;
}

Writing everything out in python.

Similar to C++ in Python we can use a loop to get a mean as well but we could also do it in one line as well. Here is an example of a loop similar to c++ in python. As you can see it is very similar but much more simpler.

n = [10, 20, 30]
sum = 0

for i in range(len(n)):
    sum += n[i]

mean =  (sum/len(n))

Python in one line

In python we have many built in functions such as min() (this will give us the minum value of something). The sum() methond gives us the sum of a array and len() gives of the length of a array(how many elements are in the array) We can use these two methods then divied those and this will give us the mean.

n = [10, 20, 30]

mean = (sum(n)/len(n))

Python using statistics library

We can also use of Pythons built in libraries to make things even more easier. Here I imported the the statistics library and imported the mean() function which takes in a array as its argument and returns the mean.

from statistics import mean 

n = [11, 3, 4, 5, 7, 9, 2]

mean_of_n = mean(n)

Differences

If you didn’t know the notation of this or what it all means looking at this could be very confusing and may know know what it means. Especially seeing how summnation is taught at higher levels (at least in my expirence). But because I had a code background when I first leanred summation it was pretty straight forward.

\[\bar{x} = \frac{1}{n} \left( \sum_{i=1}^{n} x_i \right)\]

We can easily represent sumation notation in code with a loop that not ony makes it easier to understand but gives us directions as to what to do.

What the loop tells us to do

n = [11, 3, 4, 5, 7, 9, 2] 
sum = 0

for i in range(len(n)):
    sum += n[i]

mean =  (sum/len(n))

These instructions here tell us to:

  • First we loop through all the elements (this is given by len(n))
  • After every loop we add the current element to the sum
  • Once the loop is over we will have our sum of the array
  • The final step is to simply divide the sum by the array size

In this blogpost I have talked about summation notation, how it represented in code, and how it can be benficial. I used a simple summation problem and demonstrated it in code to see how we can easily represent a summation formula in code. Summnation can often look intimidating at first glance but if we use code to break it down it can be much easier to learn.

Twitter, Facebook