Suppose f
and g
are two bounded functions defined on the interval [0,1]
.
How to calculate the L_2 distance between f
and g
?
Suppose f
and g
are two bounded functions defined on the interval [0,1]
.
How to calculate the L_2 distance between f
and g
?
Expanding 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 define e.g. inner product in L2.