0

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?

tonytonov
  • 25,060
  • 16
  • 82
  • 98
Janak
  • 653
  • 7
  • 25
  • The good site to ask your question is [this](http://mathematica.stackexchange.com/) –  Dec 23 '14 at 08:52
  • I am using R. But I think the referred site is related to mathematica. I don't know how to use it. – Janak Dec 23 '14 at 08:55
  • This question in current state is off-topic since it asks for a package. Your task, however, is rather clear and can be formulated as simply "how to calculate L2 function norm", which is quite straightforward by using the definition and `integrate`. – tonytonov Dec 23 '14 at 09:03

1 Answers1

4

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.

tonytonov
  • 25,060
  • 16
  • 82
  • 98