1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
//! Public API functions for cubic spline interpolation
//!
//! This module provides SciPy-compatible public functions for creating
//! and using cubic splines. These functions offer convenient interfaces
//! that match the behavior of SciPy's interpolation functions.
use crate;
use crateInterpolationFloat;
use ;
use CubicSpline;
use SplineBoundaryCondition;
/// Create a cubic spline with SciPy-compatible interface
///
/// This function provides the same interface as SciPy's `CubicSpline` constructor,
/// allowing for easy migration from Python-based workflows.
///
/// # Arguments
///
/// * `x` - The x coordinates (must be sorted in ascending order)
/// * `y` - The y coordinates (must have the same length as x)
/// * `bc_type` - Boundary condition type as string
/// - "natural": Zero second derivative at endpoints
/// - "not-a-knot": Continuous third derivative at second and second-to-last points
/// - "clamped": Specified first derivatives at endpoints (requires bc_values)
/// - "periodic": Function and derivatives match at endpoints
/// * `bc_values` - Boundary condition values for clamped splines (left_deriv, right_deriv)
/// * `_extrapolate` - Whether to allow extrapolation (currently unused for compatibility)
///
/// # Returns
///
/// A new `CubicSpline` object with the specified boundary conditions
///
/// # Errors
///
/// Returns an error if:
/// - Arrays have different lengths
/// - Insufficient points for the boundary condition
/// - x coordinates are not sorted
/// - Invalid boundary condition type
/// - Missing boundary condition values for clamped splines
///
/// # Examples
///
/// ```rust
/// use scirs2_core::ndarray::array;
/// use scirs2_interpolate::spline::cubic_spline_scipy;
///
/// let x = array![0.0, 1.0, 2.0, 3.0];
/// let y = array![0.0, 1.0, 4.0, 9.0];
///
/// // Natural boundary conditions
/// let spline1 = cubic_spline_scipy(&x.view(), &y.view(), "natural", None, false).expect("doc example: should succeed");
///
/// // Clamped boundary conditions
/// let spline2 = cubic_spline_scipy(&x.view(), &y.view(), "clamped", Some((0.0, 6.0)), false).expect("doc example: should succeed");
///
/// // Not-a-knot boundary conditions
/// let spline3 = cubic_spline_scipy(&x.view(), &y.view(), "not-a-knot", None, false).expect("doc example: should succeed");
/// ```
/// Create an interpolating spline with flexible boundary conditions
///
/// This function provides a more flexible interface for creating cubic splines
/// with various boundary conditions, similar to SciPy's `make_interp_spline`.
///
/// # Arguments
///
/// * `x` - The x coordinates (must be sorted in ascending order)
/// * `y` - The y coordinates (must have the same length as x)
/// * `bc_type` - Boundary condition type as string
/// - "natural": Zero second derivative at endpoints
/// - "not-a-knot": Continuous third derivative at second and second-to-last points
/// - "clamped": Specified first derivatives at endpoints (requires bc_params)
/// - "periodic": Function and derivatives match at endpoints
/// * `bc_params` - Optional boundary condition parameters
/// - For "clamped": Array of [left_derivative, right_derivative]
/// - For other types: unused
///
/// # Returns
///
/// A new `CubicSpline` object with the specified boundary conditions
///
/// # Errors
///
/// Returns an error if:
/// - Arrays have different lengths
/// - Insufficient points for the boundary condition
/// - x coordinates are not sorted
/// - Invalid boundary condition type
/// - Invalid boundary condition parameters
///
/// # Examples
///
/// ```rust
/// use scirs2_core::ndarray::array;
/// use scirs2_interpolate::spline::make_interp_spline;
///
/// let x = array![0.0, 1.0, 2.0, 3.0];
/// let y = array![0.0, 1.0, 4.0, 9.0];
///
/// // Natural boundary conditions
/// let spline1 = make_interp_spline(&x.view(), &y.view(), "natural", None).expect("doc example: should succeed");
///
/// // Clamped boundary conditions with derivatives
/// let bc_params = array![0.0, 6.0]; // [left_deriv, right_deriv]
/// let spline2 = make_interp_spline(&x.view(), &y.view(), "clamped", Some(&bc_params.view())).expect("doc example: should succeed");
/// ```
/// Create a SciPy-compatible interpolation function
///
/// This function provides a simplified interface similar to SciPy's `interp1d`
/// with cubic spline interpolation. It returns a closure that can be used
/// to interpolate values.
///
/// # Arguments
///
/// * `x` - Known x values (must be sorted)
/// * `y` - Known y values (must have same length as x)
/// * `kind` - Interpolation kind ("cubic" for cubic spline, others may be added)
/// * `bounds_error` - Whether to raise error for out-of-bounds points
/// * `fill_value` - Value to use for out-of-bounds points if bounds_error=false
///
/// # Returns
///
/// A closure that takes an array of x values and returns interpolated y values
///
/// # Errors
///
/// Returns an error if:
/// - Input validation fails
/// - Unsupported interpolation kind
/// - Spline construction fails
///
/// # Examples
///
/// ```rust
/// use scirs2_core::ndarray::array;
/// use scirs2_interpolate::spline::interp1d_scipy;
///
/// let x = array![0.0, 1.0, 2.0, 3.0];
/// let y = array![0.0, 1.0, 4.0, 9.0];
///
/// let interp_fn = interp1d_scipy(&x.view(), &y.view(), "cubic", true, None).expect("doc example: should succeed");
///
/// let x_new = array![0.5, 1.5, 2.5];
/// let y_interp = interp_fn(&x_new.view()).expect("doc example: should succeed");
/// ```
/// Create a spline with specified second derivatives at endpoints
///
/// This function creates a cubic spline with specified second derivative
/// boundary conditions, which is useful for certain physical constraints.
///
/// # Arguments
///
/// * `x` - The x coordinates (must be sorted in ascending order)
/// * `y` - The y coordinates (must have the same length as x)
/// * `d2y0` - Second derivative at the left endpoint
/// * `d2yn` - Second derivative at the right endpoint
///
/// # Returns
///
/// A new `CubicSpline` object with specified second derivative boundary conditions
///
/// # Examples
///
/// ```rust
/// use scirs2_core::ndarray::array;
/// use scirs2_interpolate::spline::cubic_spline_second_derivative;
///
/// let x = array![0.0, 1.0, 2.0, 3.0];
/// let y = array![0.0, 1.0, 4.0, 9.0];
///
/// // Specify curvature at endpoints
/// let spline = cubic_spline_second_derivative(&x.view(), &y.view(), 1.0, -1.0).expect("doc example: should succeed");
/// ```
/// Create a spline with parabolic runout boundary conditions
///
/// This creates a cubic spline with parabolic runout boundary conditions,
/// which is a specialized condition for certain applications.
///
/// # Arguments
///
/// * `x` - The x coordinates (must be sorted in ascending order)
/// * `y` - The y coordinates (must have the same length as x)
///
/// # Returns
///
/// A new `CubicSpline` object with parabolic runout boundary conditions
///
/// # Examples
///
/// ```rust
/// use scirs2_core::ndarray::array;
/// use scirs2_interpolate::spline::cubic_spline_parabolic_runout;
///
/// let x = array![0.0, 1.0, 2.0, 3.0];
/// let y = array![0.0, 1.0, 4.0, 9.0];
///
/// let spline = cubic_spline_parabolic_runout(&x.view(), &y.view()).expect("doc example: should succeed");
/// ```
/// Utility function to validate boundary condition parameters
///
/// This internal helper function validates that boundary condition parameters
/// are appropriate for the chosen boundary condition type.
///
/// # Arguments
///
/// * `bc_type` - The boundary condition type
/// * `bc_params` - The boundary condition parameters
/// * `n_points` - Number of data points
///
/// # Returns
///
/// `Ok(())` if parameters are valid, error otherwise