# Mathematical notation for Summing with if's

I'm trying to write a mathematical notation for the following logic

total = 0
Co = 10

for element in elementlist:
if element contains A:
Ci = A
elif element contains B:
Ci = B
else:
Ci = Po
total += Co - Ci


Here is what I have come up with but I'd like some extra eyes.

Thanks so much!

• One thing you can do is separate out $(n+1)C_0$, then deal with the subtracting the rest, perhaps as three separate sums over $A$, $B$ and $C$. Oct 11, 2016 at 17:05
• Could you explain that a bit more - although I'm not sure that's what I'm after? Oct 11, 2016 at 17:09
• Your computer code total += Co - Ci sums $n+1$ instances of $C_0$ (once for each time through the loop) and subtracts some values that happen to occur in one of the three lists. If that's not what you're after then you will have to explain it a bit more - I can't. Oct 11, 2016 at 17:13
• Think of this calculating total (time) for all of the elements in a list. Co is the End Time (for example right now). Ci is the Start Time. If the element contains A then use A as Ci otherwise B etc. Once you determine Ci add Co-Ci to the total. In english take the end time - start time and add it to the total time Oct 11, 2016 at 17:17

$$total = \sum_{i\in A}\left( C_0-C_i\right)+ \sum_{i\in B}\left( C_0-C_i\right)+ \sum_{i\in C}\left( C_0-C_i\right)$$ $$= C_0|A|-\sum_{i\in A}C_i+ C_0|B|-\sum_{i\in B}C_i+ C_0|C|-\sum_{i\in C}C_i$$ $$= C_0|A|+ C_0|B|+ C_0|C|-\sum_{i\in A}C_i-\sum_{i\in B}C_i-\sum_{i\in C}C_i$$ $$= C_0\left(|A|+ |B|+ |C|\right)-\left(\sum_{i\in A}C_i+\sum_{i\in B}C_i+\sum_{i\in C}C_i\right)$$ $$= C_0\left(|A|+ |B|+ |C|\right)-\sum_{i\in A\cup B\cup C}C_i$$