You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: book/external_language_integration/julia/basics.nim
+2-2Lines changed: 2 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -5,7 +5,7 @@ nbInit(theme = useNimibook)
5
5
nbText: """
6
6
# Using Julia with Nim
7
7
8
-
In this tutorial, we explore how to use [Nimjl](https://github.com/Clokk/nimjl) to integrate [Julia](https://julialang.org/) code with Nim.
8
+
In this tutorial, we explore how to use [Nimjl](https://github.com/Clonkk/nimjl) to integrate [Julia](https://julialang.org/) code with Nim.
9
9
10
10
## What is Julia ?
11
11
@@ -20,7 +20,7 @@ Most notably, it has a strong emphasis on scientific computing and Julia's Array
20
20
21
21
# Tutorial
22
22
23
-
[Nimjl](https://github.com/Clokk/nimjl) already has some [examples](https://github.com/Clonkk/nimjl/examples/) that explains the basics, make sure to go through them in order.
23
+
[Nimjl](https://github.com/Clonkk/nimjl) already has some [examples](https://github.com/Clonkk/nimjl/examples/) that explains the basics, make sure to go through them in order.
Copy file name to clipboardExpand all lines: book/numerical_methods/interpolation.nim
+49Lines changed: 49 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -92,6 +92,55 @@ We can now evaluate it on a denser set of points and compare it to the original
92
92
As we can see, the interpolant does a decent job of approximating the function.
93
93
It is worse where the function is changing a lot and closer to the original
94
94
in the middle where there is less happening.
95
+
96
+
### Extrapolation
97
+
For 1D interpolators, extrapolation is supported. The available methods are:
98
+
- `Constant`: Set all points outside the range of the interpolator to a specified value.
99
+
- `Edge`: Use the value of the left/right edge.
100
+
- `Linear`: Uses linear extrapolation using the two points closest to the edge.
101
+
- `Native` (default): Uses the native method of the interpolator to extrapolate. For Linear1D it will be a linear extrapolation, and for Cubic and Hermite splines it will be cubic extrapolation.
102
+
- `Error`: Raises an `ValueError` if `x` is outside the range.
103
+
104
+
The extrapolation method is optionally supplied as an argument to `eval` and `derivEval`:
105
+
"""
106
+
107
+
nbCode:
108
+
echo"Edge: ", interp.eval(-1.0, Edge) # will use the value at x=0
109
+
echo"Constant: ", interp.eval(-1.0, Constant, NaN) # will return NaN
echo"Native: ", interp.eval(-1.0) # will use Native by default
112
+
113
+
nbText: hlMd"""
114
+
As you can see, the choice of extrapolation method affects the values considerably.
115
+
Keep in mind though that `-1.0` is quite a big extrapolation and the further away we go, the worse the approximation gets.
116
+
117
+
Here is a visual example of how the different methods behave. I have removed the two outermost points on each side from the example before (ignore the point at `(0, 0)`, it's a bug):
118
+
"""
119
+
block:
120
+
let t =linspace(0.0, 4.2, 100)
121
+
let yOriginal = t.map(f)
122
+
let x = x[2..^3]
123
+
let y = y[2..^3]
124
+
let interp =newHermiteSpline(x, y)
125
+
let yLinear = interp.eval(t, ExtrapolateKind.Linear)
126
+
let yConstant = interp.eval(t, ExtrapolateKind.Constant, 0.0)
127
+
let yEdge = interp.eval(t, ExtrapolateKind.Edge)
128
+
let yNative = interp.eval(t, ExtrapolateKind.Native)
129
+
var df =toDf({"x": x, "y": y, "t": t, "f(t)": yOriginal, "Linear": yLinear, "Constant": yConstant, "Edge": yEdge, "Native": yNative})
nbImage("images/compare_interp_extrapolate.png", caption="Showcase of the extrapolation methods.")
138
+
139
+
nbText: hlMd"""
140
+
As we can see, the `Edge` and `Constant` are just horizontal lines while `Linear` has the same slope as the edge of the interpolant.
141
+
`Native` on the other hand is smooth but quite quickly starts to take off towards $\pm \infty$ (for the linear interpolator it would behave the same as `Linear` though).
142
+
Which method you should use is very dependent on the application and what behavior you want it to exhibit.
143
+
Of course, you should try to avoid extrapolation whenever possible.
0 commit comments