Skip to content

Commit 3630e23

Browse files
committed
Use LinearAlgebra library for norm calculations
Refactor approximation_error functions to use Julia's standard LinearAlgebra functions instead of manually implementing norms: - L2 norm: Use norm() with weighted scaling - L1 norm: Use dot() for weighted sum - L∞ norm: Use norm(diff, Inf) This makes the code more idiomatic and leverages optimized implementations from Julia's standard library.
1 parent 2fbd04b commit 3630e23

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

src/InfrastructureSystems.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import TOML
2727
using DataStructures: OrderedDict, SortedDict
2828
import SQLite
2929
import Tables
30+
using LinearAlgebra: norm, dot
3031

3132
using DocStringExtensions
3233

src/function_data.jl

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -672,11 +672,14 @@ function approximation_error(
672672
diff = y_orig .- y_approx
673673

674674
if metric === :L2
675-
return sqrt(sum(w .* diff .^ 2) / sum(w))
675+
# Weighted L2 norm (root mean square error)
676+
return norm(sqrt.(w) .* diff) / sqrt(sum(w))
676677
elseif metric === :L1
677-
return sum(w .* abs.(diff)) / sum(w)
678+
# Weighted L1 norm (mean absolute error)
679+
return dot(w, abs.(diff)) / sum(w)
678680
elseif metric === :Linf
679-
return maximum(abs.(diff))
681+
# L∞ norm (maximum absolute error)
682+
return norm(diff, Inf)
680683
else
681684
throw(ArgumentError("metric must be :L2, :L1, or :Linf"))
682685
end
@@ -696,11 +699,14 @@ function approximation_error(
696699
diff = slopes_orig .- slopes_approx
697700

698701
if metric === :L2
699-
return sqrt(sum(w .* diff .^ 2) / sum(w))
702+
# Weighted L2 norm (root mean square error)
703+
return norm(sqrt.(w) .* diff) / sqrt(sum(w))
700704
elseif metric === :L1
701-
return sum(w .* abs.(diff)) / sum(w)
705+
# Weighted L1 norm (mean absolute error)
706+
return dot(w, abs.(diff)) / sum(w)
702707
elseif metric === :Linf
703-
return maximum(abs.(diff))
708+
# L∞ norm (maximum absolute error)
709+
return norm(diff, Inf)
704710
else
705711
throw(ArgumentError("metric must be :L2, :L1, or :Linf"))
706712
end

0 commit comments

Comments
 (0)