[PYTHON] If you don't understand mathematical symbols, you can write a program.

A legend of a man who hates certain mathematics (or numbers)

So why is it now?

That's why the man who hates mathematics aiming for God steadily reviewed mathematics .... Isn't there a mathematical symbol? Somehow, that. I don't know.

I came up with that. If you don't understand mathematical symbols, you should try implementing them programmatically.

Try to implement Σ

For the time being, I forgot the meaning of Σ, so I decided to implement it myself.

What is the purpose of Σ in the first place?

Even if you study that the purpose is unclear, it doesn't come right at all. The meaning of Σ is summation, that is, creating a list (array) of numbers and calculating *** total value ***.

For example, the total value of [1,2,3,4,5,6,7,8,9,10] is 55, but it will be the symbol used to write this total value in one letter. No, I understand. But then you should write the total from 1 to 10.

Then, if the list of numbers is [2,4,6,8,10]. It will be "the sum of only even values from 1 to 10", but it is long. It seems that the more complicated the conditions, the harder it is to read. Personally, it's easier to read if you write it down.

Anyway, Σ appears here.

Σ is an iterative process

For example, the sum of [1,2,3,4,5,6,7,8,9,10] can be expressed like this:

image.png

In other words, it means that while repeating ** 1 to 10, the values from 1 to 10 are added to the array as it is for each repetition, and finally the sum of all the elements in the array is calculated **. In my case, if I try to find the total suddenly, I get an allergic reaction, so first calm down and make a sequence first. So, let's implement an array (list) from 1 to 10 in python.

array = []
for i in range(1, 11):
  array.append(i)

Yeah, no problem at all. After that, if you want to find the total value

sum(array)
 # => 55

You should be able to find the total value by writing.

If you drop it in the code, you can afford it, but if it is Σ, it seems meaningless for a moment. ~~ This is because mathematical symbols use math symbols that look good and are not readable. ~~ I think it's actually derived from it .... Let's fill in the difference between this general python code and the Σ symbol.

I tried to color-code variables and values that have the same role. This makes it a little easier to understand. "How far to repeat" is 11 due to the specifications of the range () function, but it can't be helped. image.png

In this case, k and i play the same role. Let's unify the variable names anyway.

array = []
for k in range(1, 11):
  array.append(k)

This makes it a little easier to understand.

Heart

I wish I could use variable names such as "start_from", "end", and "element" that are easy to understand without using k, n, or a. In the first place, Σ itself seems to be the Greek letter Σ of S in "Summation" which means sum in English. Then Summation is fine. Why do you abbreviate it? The etymology of summation is Latin in the first place, isn't it? Why do you use Greek letters? It's cool though!

Customization of processing part

Now let's get back to the story. The right side of Σ was written as "k" earlier, but another formula may come in like this.

image.png

This is pretty simple in code. It's a good idea to think of it as an indication of how the values you want to include in the list will eventually be processed.

array = []
for k in range(1, 11):
  array.append(k) #Corresponds to the k part here

So, in this example

array = []
for k in range(1, 11):
  array.append(2 * k) 

You can write it like this.

I'm only dealing with very simple expressions, but I think the rest can be applied to this.

Let's play a little (bonus)

Now that I know the basics, I'll adjust it from a programmatic perspective. First of all, it is troublesome to write every time, so let's make it a function.

Easy functioning

It will be like this if it is made into a function easily. The return value is trying to return the sum properly.

def sigma():
  array = []
  for k in range(1, 11):
    array.append(2 * k)

  return sum(array)

Receive value from outside

If this is left as it is, it will be inflexible, so be sure to receive the value from the outside.

def sigma(k, n):
  array = []
  for k in range(k, (n + 1)):
    array.append(2 * k)

  return sum(array)

#Function call example
print(sigma(1, 10))
 # =>110 because it is the sum of 1 to 10 multiplied by 2.

Receive the ceremony from the outside

Let's also receive the ceremony from the outside. I'll use a lambda expression here.

def sigma(k, n, func):
  array = []
  for k in range(k, (n + 1)):
    array.append(func(k, n))

  return sum(array)

#Function call example
print(sigma(1, 10, lambda k, n : 2 * k))
 # =>110 because it is the sum of 1 to 10 multiplied by 2.

I guessed here. Oh, maybe the list comprehension ...

For list comprehension

array = [2 * k for k in range(1, 11)]
print(sum(array))

I see......

print(sum([2 * k for k in range(1, 11)]))

It has shrunk to just one line. I knew that the execution speed was fast, but list comprehension is also easy in this sense. After this, you can also insert a conditional branch!

Conclusion

If you can write a program but can't do math, you may want to try writing a program! I didn't think Σ was useful, but at least I understood it, and I found that the list comprehension was wonderful (laughs).

Recommended Posts

If you don't understand mathematical symbols, you can write a program.
How can I write a good program?
If you write TinderBot in Python, she can do it
If you know Python, you can make a web application with Django
Don't write Python if you want to speed it up with Python
Don't you want to say that you made a face recognition program?
Check if you can connect to a TCP port in Python
Kalman filter that you can understand
Write a Caesar cipher program in Python
[Beginner] What happens if I write a program that runs in php in Python?
If you guys in the scope kitchen can do it with a margin ~ ♪
If you know this much, you can write Python test code! ~ Super introductory edition ~