How to calculate the functional norm L_2 using R

Suppose that fand gare two bounded functions defined on an interval [0,1].

How to calculate L_2 distance between fand g?

0
source share
1 answer

Extension of my comment:

f <- function(x) x^2
g <- function(x) sqrt(cos(x))
z <- function(x) 0

l2_norm <- function(fun) {
  f_sq <- function(x) fun(x)^2
  sqrt(integrate(f = f_sq, lower = 0, upper = 1)$value)
}

l2_dist <- function(f, g) {
  f_diff <- function(x) (f(x) - g(x))^2
  sqrt(integrate(f = f_diff, lower = 0, upper = 1)$value)
}

l2_norm(f) # = sqrt(1/5)
l2_norm(g) # = sqrt(sin(1))
l2_dist(f, z) # = l2_norm(f)
l2_dist(f, g)

A similar approach can be used to determine, for example, an internal product in L2.

+4
source

All Articles