pythondefinition

Defining function in Python whose one argument is list


There is a part of mathematics called the theory of multiple zeta values (MZVs) introduced around 1992. In this theory, we study the properties of parametric nested infinite series (see, e.g., here for the definition of one particular variant) whose numeric evaluation is, therefore, of high computational complexity. Several days ago, I started using Python to calculate the basic partial sums of specific MZVs.

The following code allows me to calculate the 20th partial sum of the so-called multiple zeta-star value zeta*(10,2,1,1,1) or any other similar instance by changing the inputs S, n.

S = (10, 2, 1, 1, 1)
n = 20

l = len(S)

def F(d, N):
  if d == 0:
    return 1
  else:
    return sum(F(d-1, k)/(k**S[-d]) for k in range(1, N+1))

print(F(l, n))

Of course, the partial sums of the d-dimensional MZVs essentially depend on their arguments forming a d-tuple (s_1, ..., s_d) and on the upper summation bound n.

My question is whether we can define a function typed in a user-friendly way F([s_1, ..., s_d], n) with the first argument being a list with integer entries and the second argument being the summation bound n. Thus, instead of typing the two inputs separately, S and n, I am looking for a way to directly type F([10, 2, 1, 1, 1], 20) or F([2, 1], 100), etc., with the given delimiters of the two arguments.


Solution

  • It looks like you are looking for a function wrapper, which takes the arguments as you really want them, and which has the function F as a local function inside of it, and calls it:

    def zeta(S, n):  # wrapper around F
    
        def F(d, n):
            if d == 0:
                return 1
            return sum(F(d-1, k)/(k**S[-d]) for k in range(1, n+1))
    
        return F(len(S), n)  # Call F and return what it returns
    
    # Example call
    result = zeta((10, 2, 1, 1, 1), 20)
    print(result)