Skip to content

Commit

Permalink
Merge pull request #69 from yha/spline-eq
Browse files Browse the repository at this point in the history
Spline equality (==)
  • Loading branch information
kbarbary authored Sep 5, 2020
2 parents 5ac701e + 23cec27 commit e961ed9
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/Dierckx.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export Spline1D,
get_coeffs,
get_residual

import Base: show
import Base: show, ==

# ----------------------------------------------------------------------------
# 1-d splines
Expand Down Expand Up @@ -100,6 +100,9 @@ function reallycompact(a::Vector)
return read(io, String)
end

function ==(s1::Spline1D, s2::Spline1D)
s1.t == s2.t && s1.c == s2.c && s1.k == s2.k && s1.bc == s2.bc && s1.fp == s2.fp
end

function show(io::IO, spl::Spline1D)
print(io, """Spline1D(knots=$(reallycompact(get_knots(spl))), k=$(spl.k), extrapolation=\"$(_translate_bc(spl.bc))\", residual=$(spl.fp))""")
Expand Down Expand Up @@ -414,6 +417,11 @@ function show(io::IO, spl::ParametricSpline)
print(io, """ParametricSpline(knots=$(reallycompact(get_knots(spl))), k=$(spl.k), extrapolation=\"$(_translate_bc(spl.bc))\", residual=$(spl.fp))""")
end

function ==(s1::ParametricSpline, s2::ParametricSpline)
s1.t == s2.t && s1.c == s2.c && s1.k == s2.k && s1.bc == s2.bc && s1.fp == s2.fp
end


function ParametricSpline(x::AbstractMatrix;
w::AbstractVector=ones(size(x, 2)),
k::Int=3, s::Real=0.0, bc::AbstractString="nearest",
Expand Down Expand Up @@ -687,6 +695,9 @@ get_knots(spl::Spline2D) = (spl.tx[spl.kx+1:end-spl.kx],
spl.ty[spl.ky+1:end-spl.ky])
get_residual(spl::Spline2D) = spl.fp

function ==(s1::Spline2D, s2::Spline2D)
s1.tx == s2.tx && s1.ty == s2.ty && s1.c == s2.c && s1.kx == s2.kx && s1.ky == s2.ky && s1.fp == s2.fp
end

# Helper functions for calculating required size of work arrays in surfit.
# Note that x and y here are as in the Fortran documentation.
Expand Down
40 changes: 40 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,18 @@ seek(io, 0)
s = read(io, String)
@test s[1:9] == "Spline1D("

# test equality
seed!(0)
x = sort(rand(10))
y = rand(10)
sp1 = Spline1D(x, y)
sp2 = Spline1D(x, y)
sp3 = Spline1D(x.+1, y)
sp4 = Spline1D(x, y.+1)
@test sp1 == sp2
@test allunique([sp1,sp3,sp4])


# -----------------------------------------------------------------------------
# ParametricSpline

Expand Down Expand Up @@ -184,6 +196,20 @@ seek(io, 0)
s = read(io, String)
@test s[1:17] == "ParametricSpline("


# test equality
seed!(0)
x = sort(rand(10))
y = rand(3, 10)
sp1 = ParametricSpline(x, y)
sp2 = ParametricSpline(x, y)
sp3 = ParametricSpline(x.+1, y)
sp4 = ParametricSpline(x, y.+1)
@test sp1 == sp2
@test allunique([sp1,sp3,sp4])



# -----------------------------------------------------------------------------
# Spline2D

Expand Down Expand Up @@ -282,4 +308,18 @@ for (f, domain, exact) in [(test2d_1, (0.0, 1.0, 0.0, 1.0), 1.0/3.0),
@test isapprox(integrate(spl1, x0, x1, y0, y1), exact, atol=1e-6)
end

# test equality
seed!(0)
x = sort(rand(10))
y = sort(rand(10))
z = rand(10,10)
sp1 = Spline2D(x, y, z)
sp2 = Spline2D(x, y, z)
sp3 = Spline2D(x.+1, y, z)
sp4 = Spline2D(x, y.+1, z)
sp5 = Spline2D(x, y, z.+1)
@test sp1 == sp2
@test allunique([sp1, sp3, sp4, sp5])


println("All tests passed.")

0 comments on commit e961ed9

Please sign in to comment.