The plus-operator is a node in the tree. When you iteratively add 1, the constant 1 will always be the right sub-tree, the previous goes to the left. So the tree has depth n. We can visualize the tree in ngsolve by printing the coefficient funciton. The depth of indention shows the tree level. Here are 3 examples (with 16 = 4*4 = 2**4 terms). Last one is the best.
If you have a list of N terms, make a recursive function which adds the sum of the first half of terms, with the sum of the second half of terms.
from ngsolve import *
mesh = Mesh(unit_square.GenerateMesh(maxh=0.2))
mip = mesh(0.2,0.4)
# version 1: summing n terms
cf = CoefficientFunction(0)
for i in range(16):
cf += 1
print(cf)
print(cf(mip))
# version 2: summing n*n terms:
cf = CF(0)
for i in range(4):
partsum = CF(0)
for j in range(4):
partsum += 1
cf += partsum
print (cf)
print (cf(mip))
# version 3: perfectly balanced tree:
cf = CF(1)
for i in range(4):
cf = cf+cf
print (cf)
print (cf(mip))
the outcome is:
coef binary operation '+', real
coef binary operation '+', real
coef binary operation '+', real
coef binary operation '+', real
coef binary operation '+', real
coef binary operation '+', real
coef binary operation '+', real
coef binary operation '+', real
coef binary operation '+', real
coef binary operation '+', real
coef binary operation '+', real
coef binary operation '+', real
coef binary operation '+', real
coef binary operation '+', real
coef binary operation '+', real
coef 1, real
coef 1, real
coef 1, real
coef 1, real
coef 1, real
coef 1, real
coef 1, real
coef 1, real
coef 1, real
coef 1, real
coef 1, real
coef 1, real
coef 1, real
coef 1, real
coef 1, real
coef 1, real
16.0
coef binary operation '+', real
coef binary operation '+', real
coef binary operation '+', real
coef binary operation '+', real
coef binary operation '+', real
coef binary operation '+', real
coef 1, real
coef 1, real
coef 1, real
coef 1, real
coef binary operation '+', real
coef binary operation '+', real
coef binary operation '+', real
coef 1, real
coef 1, real
coef 1, real
coef 1, real
coef binary operation '+', real
coef binary operation '+', real
coef binary operation '+', real
coef 1, real
coef 1, real
coef 1, real
coef 1, real
coef binary operation '+', real
coef binary operation '+', real
coef binary operation '+', real
coef 1, real
coef 1, real
coef 1, real
coef 1, real
16.0
coef binary operation '+', real
coef binary operation '+', real
coef binary operation '+', real
coef binary operation '+', real
coef unary operation ' ', real
coef 1, real
coef unary operation ' ', real
coef 1, real
coef binary operation '+', real
coef unary operation ' ', real
coef 1, real
coef unary operation ' ', real
coef 1, real
coef binary operation '+', real
coef binary operation '+', real
coef unary operation ' ', real
coef 1, real
coef unary operation ' ', real
coef 1, real
coef binary operation '+', real
coef unary operation ' ', real
coef 1, real
coef unary operation ' ', real
coef 1, real
coef binary operation '+', real
coef binary operation '+', real
coef binary operation '+', real
coef unary operation ' ', real
coef 1, real
coef unary operation ' ', real
coef 1, real
coef binary operation '+', real
coef unary operation ' ', real
coef 1, real
coef unary operation ' ', real
coef 1, real
coef binary operation '+', real
coef binary operation '+', real
coef unary operation ' ', real
coef 1, real
coef unary operation ' ', real
coef 1, real
coef binary operation '+', real
coef unary operation ' ', real
coef 1, real
coef unary operation ' ', real
coef 1, real
16.0