Minimum Distance in Julia
#Normanddistancefunction
norm(L, x) = sqrt(inner(L, x, x))
dist(L, x, y) = norm(L, x-y)
#ExamplefortheEuclideanp2-Norm
inner(::Val{:P2}, x, y) = x' * y
dist(Val(:P2), [0,0], [1,1]) #1.4142
functiongram_schmidt(L, b)
e = copy(b)
fori = 1:length(b)
forj=1:i-1
e[i] -= e[j] * inner(L, b[i], e[j])
end
nn = norm(L, e[i])
ifnn > 0.0001#Normalizeifnon-zero
e[i] = e[i] / nn
end
end
returne
end
#Projectiononthesubspacedefinedbya(not
#necessarilyorthogonal)basis
functionproj(L, x, basis)
ob = gram_schmidt(L, basis) #orthonormalbasis
returnsum([ob[i] * inner(L, x, ob[i])
fori=1:length(ob)])
end
#Returnstheprojectionanditscoefficients
#forthebasiselements
functionproj_normal(L, x, basis)
nb = length(basis)
G = zeros(nb,nb) #Grammatrix,alwayssymmetric
fori=1:nb, j=1:i
G[i,j] = inner(L, basis[i], basis[j])
G[j,i] = G[i,j]
end
c = [inner(L, x, basis[i]) fori=1:nb]
alpha = G \ c#G*alpha=c
returnsum(basis .* alpha), alpha
end
18/24