0

I want to compute the functional norm of a function. My use case is the distance of two function but that is not as important as computing this in a correct manner in python. Looking at the docs I did:

import numpy as np
import scipy.integrate as integrate

def functional_diff_norm(f1, f2, lb, ub, p=2):
    """
    Computes norm:

    ||f||_p = (int_S |f|^p dmu)^1/p

    https://en.wikipedia.org/wiki/Lp_space
    """
    norm = integrate.quad(lambda x: abs(f1(x) - f2(x))**p, lb, ub)[0]**(1/p)
    return norm

but want to make sure it's correct.

Also, I'd like to generalize it to higher dimensions. Is that tractable? If yes up to what dimension (and accuracy?)


crossposted: https://discuss.pytorch.org/t/how-does-one-compute-the-norm-of-a-function-in-python/91527

Charlie Parker
  • 5,884
  • 57
  • 198
  • 323
  • For higher dimensions, this may take considerably more computational time if you use `quad`, depending on your function. If you want a faster the result, the logical question to ask is how accurate does it need to be, less accurate results are cheaper. – Peter Meisrimel Aug 04 '20 at 08:29
  • @PeterMeisrimel I think what would be optimal for me would be to give a time limit and then the integral is computed according to the budget I give. Or even better I give both a budget and a accuracy and whichever one is met first halts the computation. Is that available somewhere? (for any dimension of course) – Charlie Parker Aug 04 '20 at 15:17
  • I do not know too much about multidimensional numerical integration, but I suppose a Monte Carlo type approach might fit this budget concept. `quad` is adaptive and deterministic, you can pick tolerances, which will roughly correspond to the resulting accuracy. – Peter Meisrimel Aug 04 '20 at 15:37

1 Answers1

1

You should follow the formula in the doc string

norm = integrate.quad(lambda x: abs(f1(x) - f2(x))**p, lb, ub)[0]**(1/p)
Lutz Lehmann
  • 25,219
  • 2
  • 22
  • 51