Skip to content

Commit b6ee0f3

Browse files
chiyahnsglyon
authored andcommitted
Quickstart guide fix (#216)
* unit tests for convenience constructors with function call syntax * quickstart guide now uses function call syntax
1 parent 2633025 commit b6ee0f3

File tree

2 files changed

+72
-72
lines changed

2 files changed

+72
-72
lines changed

README.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,13 @@ A = [f(x) for x in xs]
110110

111111
# linear interpolation
112112
interp_linear = LinearInterpolation(xs, A)
113-
interp_linear[3] # exactly log(3)
114-
interp_linear[3.1] # approximately log(3.1)
113+
interp_linear(3) # exactly log(3)
114+
interp_linear(3.1) # approximately log(3.1)
115115

116116
# cubic spline interpolation
117117
interp_cubic = CubicSplineInterpolation(xs, A)
118-
interp_cubic[3] # exactly log(3)
119-
interp_cubic[3.1] # approximately log(3.1)
118+
interp_cubic(3) # exactly log(3)
119+
interp_cubic(3.1) # approximately log(3.1)
120120
```
121121
which support multidimensional data as well:
122122
```jl
@@ -127,13 +127,13 @@ A = [f(x+y) for x in xs, y in ys]
127127

128128
# linear interpolation
129129
interp_linear = LinearInterpolation((xs, ys), A)
130-
interp_linear[3, 2] # exactly log(3 + 2)
131-
interp_linear[3.1, 2.1] # approximately log(3.1 + 2.1)
130+
interp_linear(3, 2) # exactly log(3 + 2)
131+
interp_linear(3.1, 2.1) # approximately log(3.1 + 2.1)
132132

133133
# cubic spline interpolation
134134
interp_cubic = CubicSplineInterpolation((xs, ys), A)
135-
interp_cubic[3, 2] # exactly log(3 + 2)
136-
interp_cubic[3.1, 2.1] # approximately log(3.1 + 2.1)
135+
interp_cubic(3, 2) # exactly log(3 + 2)
136+
interp_cubic(3.1, 2.1) # approximately log(3.1 + 2.1)
137137
```
138138
For extrapolation, i.e., when interpolation objects are evaluated in coordinates outside of range provided in constructors, the default option for a boundary condition is `Throw` so that they will return an error.
139139
Interested users can specify boundary conditions by providing an extra parameter for `extrapolation_bc`:
@@ -145,8 +145,8 @@ A = [f(x) for x in xs]
145145
# extrapolation with linear boundary conditions
146146
extrap = LinearInterpolation(xs, A, extrapolation_bc = Interpolations.Linear())
147147

148-
@test extrap[1 - 0.2] # ≈ f(1) - (f(1.2) - f(1))
149-
@test extrap[5 + 0.2] # ≈ f(5) + (f(5) - f(4.8))
148+
@test extrap(1 - 0.2) # ≈ f(1) - (f(1.2) - f(1))
149+
@test extrap(5 + 0.2) # ≈ f(5) + (f(5) - f(4.8))
150150
```
151151
Irregular grids are supported as well; note that presently only `LinearInterpolation` supports irregular grids.
152152
```jl
@@ -155,8 +155,8 @@ A = [f(x) for x in xs]
155155

156156
# linear interpolation
157157
interp_linear = LinearInterpolation(xs, A)
158-
interp_linear[1] # exactly log(1)
159-
interp_linear[1.05] # approximately log(1.05)
158+
interp_linear(1) # exactly log(1)
159+
interp_linear(1.05) # approximately log(1.05)
160160
```
161161

162162
## Control of interpolation algorithm

test/convenience-constructors.jl

Lines changed: 60 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ YLEN = convert(Integer, floor((YMAX - YMIN)/ΔY) + 1)
2323
interp_full = extrapolate(scale(interpolate(A, BSpline(Linear()), OnGrid()), xs), Interpolations.Throw()) # using full constructor
2424

2525
@test typeof(interp) == typeof(interp_full)
26-
@test interp[XMIN] f(XMIN)
27-
@test interp[XMAX] f(XMAX)
28-
@test interp[XMIN + ΔX] f(XMIN + ΔX)
29-
@test interp[XMAX - ΔX] f(XMAX - ΔX)
30-
@test interp[XMIN + ΔX / 2] f(XMIN + ΔX / 2) atol=.1
31-
@test_throws BoundsError interp[XMIN - ΔX / 2]
32-
@test_throws BoundsError interp[XMAX + ΔX / 2]
26+
@test interp(XMIN) f(XMIN)
27+
@test interp(XMAX) f(XMAX)
28+
@test interp(XMIN + ΔX) f(XMIN + ΔX)
29+
@test interp(XMAX - ΔX) f(XMAX - ΔX)
30+
@test interp(XMIN + ΔX / 2) f(XMIN + ΔX / 2) atol=.1
31+
@test_throws BoundsError interp(XMIN - ΔX / 2)
32+
@test_throws BoundsError interp(XMAX + ΔX / 2)
3333
end
3434

3535
@testset "1d-regular-grids-cubic" begin
@@ -40,13 +40,13 @@ YLEN = convert(Integer, floor((YMAX - YMIN)/ΔY) + 1)
4040
interp_full = extrapolate(scale(interpolate(A, BSpline(Cubic(Line())), OnGrid()), xs), Interpolations.Throw())
4141

4242
@test typeof(interp) == typeof(interp_full)
43-
@test interp[XMIN] f(XMIN)
44-
@test interp[XMAX] f(XMAX)
45-
@test interp[XMIN + ΔX] f(XMIN + ΔX)
46-
@test interp[XMAX - ΔX] f(XMAX - ΔX)
47-
@test interp[XMIN + ΔX / 2] f(XMIN + ΔX / 2) atol=.1
48-
@test_throws BoundsError interp[XMIN - ΔX / 2]
49-
@test_throws BoundsError interp[XMAX + ΔX / 2]
43+
@test interp(XMIN) f(XMIN)
44+
@test interp(XMAX) f(XMAX)
45+
@test interp(XMIN + ΔX) f(XMIN + ΔX)
46+
@test interp(XMAX - ΔX) f(XMAX - ΔX)
47+
@test interp(XMIN + ΔX / 2) f(XMIN + ΔX / 2) atol=.1
48+
@test_throws BoundsError interp(XMIN - ΔX / 2)
49+
@test_throws BoundsError interp(XMAX + ΔX / 2)
5050
end
5151

5252
@testset "1d-irregular-grids" begin
@@ -59,12 +59,12 @@ YLEN = convert(Integer, floor((YMAX - YMIN)/ΔY) + 1)
5959
interp_full = extrapolate(interpolate((xs, ), A, Gridded(Linear())), Interpolations.Throw())
6060

6161
@test typeof(interp) == typeof(interp_full)
62-
@test interp[xmin] f(xmin)
63-
@test interp[xmax] f(xmax)
64-
@test interp[xs[2]] f(xs[2])
65-
@test interp[xmin + ΔX / 2] f(xmin + ΔX / 2) atol=.1
66-
@test_throws BoundsError interp[xmin - ΔX / 2]
67-
@test_throws BoundsError interp[xmax + ΔX / 2]
62+
@test interp(xmin) f(xmin)
63+
@test interp(xmax) f(xmax)
64+
@test interp(xs[2]) f(xs[2])
65+
@test interp(xmin + ΔX / 2) f(xmin + ΔX / 2) atol=.1
66+
@test_throws BoundsError interp(xmin - ΔX / 2)
67+
@test_throws BoundsError interp(xmax + ΔX / 2)
6868
end
6969

7070
@testset "1d-handling-extrapolation" begin
@@ -80,8 +80,8 @@ YLEN = convert(Integer, floor((YMAX - YMIN)/ΔY) + 1)
8080
extrap_full = extrapolate(scale(interpolate(A, BSpline(Linear()), OnGrid()), xs), Interpolations.Linear())
8181

8282
@test typeof(extrap) == typeof(extrap_full)
83-
@test extrap[x_lower] A[1] - ΔA_l
84-
@test extrap[x_higher] A[end] + ΔA_h
83+
@test extrap(x_lower) A[1] - ΔA_l
84+
@test extrap(x_higher) A[end] + ΔA_h
8585
end
8686
end
8787

@@ -95,18 +95,18 @@ end
9595
interp_full = extrapolate(scale(interpolate(A, BSpline(Linear()), OnGrid()), xs, ys), Interpolations.Throw())
9696

9797
@test typeof(interp) == typeof(interp_full)
98-
@test interp[XMIN,YMIN] f(XMIN,YMIN)
99-
@test interp[XMIN,YMAX] f(XMIN,YMAX)
100-
@test interp[XMAX,YMIN] f(XMAX,YMIN)
101-
@test interp[XMAX,YMAX] f(XMAX,YMAX)
102-
@test interp[XMIN + ΔX,YMIN] f(XMIN + ΔX,YMIN)
103-
@test interp[XMIN,YMIN + ΔY] f(XMIN,YMIN + ΔY)
104-
@test interp[XMIN + ΔX,YMIN + ΔY] f(XMIN + ΔX,YMIN + ΔY)
105-
@test interp[XMIN + ΔX / 2,YMIN + ΔY / 2] f(XMIN + ΔX / 2,YMIN + ΔY / 2) atol=.1
106-
@test_throws BoundsError interp[XMIN - ΔX / 2,YMIN - ΔY / 2]
107-
@test_throws BoundsError interp[XMIN - ΔX / 2,YMIN + ΔY / 2]
108-
@test_throws BoundsError interp[XMIN + ΔX / 2,YMIN - ΔY / 2]
109-
@test_throws BoundsError interp[XMAX + ΔX / 2,YMAX + ΔY / 2]
98+
@test interp(XMIN,YMIN) f(XMIN,YMIN)
99+
@test interp(XMIN,YMAX) f(XMIN,YMAX)
100+
@test interp(XMAX,YMIN) f(XMAX,YMIN)
101+
@test interp(XMAX,YMAX) f(XMAX,YMAX)
102+
@test interp(XMIN + ΔX,YMIN) f(XMIN + ΔX,YMIN)
103+
@test interp(XMIN,YMIN + ΔY) f(XMIN,YMIN + ΔY)
104+
@test interp(XMIN + ΔX,YMIN + ΔY) f(XMIN + ΔX,YMIN + ΔY)
105+
@test interp(XMIN + ΔX / 2,YMIN + ΔY / 2) f(XMIN + ΔX / 2,YMIN + ΔY / 2) atol=.1
106+
@test_throws BoundsError interp(XMIN - ΔX / 2,YMIN - ΔY / 2)
107+
@test_throws BoundsError interp(XMIN - ΔX / 2,YMIN + ΔY / 2)
108+
@test_throws BoundsError interp(XMIN + ΔX / 2,YMIN - ΔY / 2)
109+
@test_throws BoundsError interp(XMAX + ΔX / 2,YMAX + ΔY / 2)
110110
end
111111

112112
@testset "2d-regular-grids-cubic" begin
@@ -118,18 +118,18 @@ end
118118
interp_full = extrapolate(scale(interpolate(A, BSpline(Cubic(Line())), OnGrid()), xs, ys), Interpolations.Throw())
119119

120120
@test typeof(interp) == typeof(interp_full)
121-
@test interp[XMIN,YMIN] f(XMIN,YMIN)
122-
@test interp[XMIN,YMAX] f(XMIN,YMAX)
123-
@test interp[XMAX,YMIN] f(XMAX,YMIN)
124-
@test interp[XMAX,YMAX] f(XMAX,YMAX)
125-
@test interp[XMIN + ΔX,YMIN] f(XMIN + ΔX,YMIN)
126-
@test interp[XMIN,YMIN + ΔY] f(XMIN,YMIN + ΔY)
127-
@test interp[XMIN + ΔX,YMIN + ΔY] f(XMIN + ΔX,YMIN + ΔY)
128-
@test interp[XMIN + ΔX / 2,YMIN + ΔY / 2] f(XMIN + ΔX / 2,YMIN + ΔY / 2) atol=.1
129-
@test_throws BoundsError interp[XMIN - ΔX / 2,YMIN - ΔY / 2]
130-
@test_throws BoundsError interp[XMIN - ΔX / 2,YMIN + ΔY / 2]
131-
@test_throws BoundsError interp[XMIN + ΔX / 2,YMIN - ΔY / 2]
132-
@test_throws BoundsError interp[XMAX + ΔX / 2,YMAX + ΔY / 2]
121+
@test interp(XMIN,YMIN) f(XMIN,YMIN)
122+
@test interp(XMIN,YMAX) f(XMIN,YMAX)
123+
@test interp(XMAX,YMIN) f(XMAX,YMIN)
124+
@test interp(XMAX,YMAX) f(XMAX,YMAX)
125+
@test interp(XMIN + ΔX,YMIN) f(XMIN + ΔX,YMIN)
126+
@test interp(XMIN,YMIN + ΔY) f(XMIN,YMIN + ΔY)
127+
@test interp(XMIN + ΔX,YMIN + ΔY) f(XMIN + ΔX,YMIN + ΔY)
128+
@test interp(XMIN + ΔX / 2,YMIN + ΔY / 2) f(XMIN + ΔX / 2,YMIN + ΔY / 2) atol=.1
129+
@test_throws BoundsError interp(XMIN - ΔX / 2,YMIN - ΔY / 2)
130+
@test_throws BoundsError interp(XMIN - ΔX / 2,YMIN + ΔY / 2)
131+
@test_throws BoundsError interp(XMIN + ΔX / 2,YMIN - ΔY / 2)
132+
@test_throws BoundsError interp(XMAX + ΔX / 2,YMAX + ΔY / 2)
133133
end
134134

135135
@testset "2d-irregular-grids" begin
@@ -145,18 +145,18 @@ end
145145
interp_full = extrapolate(interpolate((xs, ys), A, Gridded(Linear())), Interpolations.Throw())
146146

147147
@test typeof(interp) == typeof(interp_full)
148-
@test interp[xmin,ymin] f(xmin,ymin)
149-
@test interp[xmin,ymax] f(xmin,ymax)
150-
@test interp[xmax,ymin] f(xmax,ymin)
151-
@test interp[xmax,ymax] f(xmax,ymax)
152-
@test interp[xs[2],ymin] f(xs[2],ymin)
153-
@test interp[xmin,ys[2]] f(xmin,ys[2])
154-
@test interp[xs[2],ys[2]] f(xs[2],ys[2])
155-
@test interp[xmin + ΔX / 2,ymin + ΔY / 2] f(xmin + ΔX / 2,ymin + ΔY / 2) atol=.1
156-
@test_throws BoundsError interp[xmin - ΔX / 2,ymin - ΔY / 2]
157-
@test_throws BoundsError interp[xmin - ΔX / 2,ymin + ΔY / 2]
158-
@test_throws BoundsError interp[xmin + ΔX / 2,ymin - ΔY / 2]
159-
@test_throws BoundsError interp[xmax + ΔX / 2,ymax + ΔY / 2]
148+
@test interp(xmin,ymin) f(xmin,ymin)
149+
@test interp(xmin,ymax) f(xmin,ymax)
150+
@test interp(xmax,ymin) f(xmax,ymin)
151+
@test interp(xmax,ymax) f(xmax,ymax)
152+
@test interp(xs[2],ymin) f(xs[2],ymin)
153+
@test interp(xmin,ys[2]) f(xmin,ys[2])
154+
@test interp(xs[2],ys[2]) f(xs[2],ys[2])
155+
@test interp(xmin + ΔX / 2,ymin + ΔY / 2) f(xmin + ΔX / 2,ymin + ΔY / 2) atol=.1
156+
@test_throws BoundsError interp(xmin - ΔX / 2,ymin - ΔY / 2)
157+
@test_throws BoundsError interp(xmin - ΔX / 2,ymin + ΔY / 2)
158+
@test_throws BoundsError interp(xmin + ΔX / 2,ymin - ΔY / 2)
159+
@test_throws BoundsError interp(xmax + ΔX / 2,ymax + ΔY / 2)
160160
end
161161

162162
@testset "2d-handling-extrapolation" begin
@@ -175,8 +175,8 @@ end
175175
extrap_full = extrapolate(scale(interpolate(A, BSpline(Linear()), OnGrid()), xs, ys), (Interpolations.Linear(), Interpolations.Flat()))
176176

177177
@test typeof(extrap) == typeof(extrap_full)
178-
@test extrap[x_lower, y_lower] A[1, 1] - ΔA_l
179-
@test extrap[x_higher, y_higher] A[end, end] + ΔA_h
178+
@test extrap(x_lower, y_lower) A[1, 1] - ΔA_l
179+
@test extrap(x_higher, y_higher) A[end, end] + ΔA_h
180180
end
181181
end
182182

0 commit comments

Comments
 (0)