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.
For the time being, I forgot the meaning of Σ, so I decided to implement it myself.
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.
For example, the sum of [1,2,3,4,5,6,7,8,9,10] can be expressed like this:
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.
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.
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!
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.
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.
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.
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)
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.
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 ...
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!
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