math4610

Routine Name: lp

Author: David Merkley

Language: Python

Description/Purpose: Gives the lp norm

Input: a vector

Output: the norm

Implementation/Code:

def lp(x, p):
    if p == 1:
        return l1(x)
    elif p == 2:
        return l2(x)
    elif isinf(p):
        return linfty(x)
    else:
        return sum([abs(xi)**p for xi in x])**(1/p)