solvr 0.2.0

Advanced computing library for real-world problem solving - optimization, differential equations, interpolation, statistics, and more
Documentation
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
//! Radial Basis Function interpolation generic implementation.
//!
//! Constructs the RBF kernel matrix from pairwise distances, optionally
//! augments with polynomial terms, solves the system, and evaluates at
//! query points.

use crate::interpolate::error::{InterpolateError, InterpolateResult};
use crate::interpolate::traits::rbf::{RbfKernel, RbfModel};
use numr::algorithm::linalg::LinearAlgebraAlgorithms;
use numr::ops::{CompareOps, MatmulOps, ScalarOps, ShapeOps, TensorOps};
use numr::prelude::DType;
use numr::runtime::{Runtime, RuntimeClient};
use numr::tensor::Tensor;

/// Fit an RBF interpolant.
pub fn rbf_fit_impl<R, C>(
    client: &C,
    points: &Tensor<R>,
    values: &Tensor<R>,
    kernel: RbfKernel,
    epsilon: Option<f64>,
    smoothing: f64,
) -> InterpolateResult<RbfModel<R>>
where
    R: Runtime<DType = DType>,
    C: TensorOps<R>
        + ScalarOps<R>
        + CompareOps<R>
        + MatmulOps<R>
        + ShapeOps<R>
        + LinearAlgebraAlgorithms<R>
        + RuntimeClient<R>,
{
    let device = client.device();
    let shape = points.shape().to_vec();
    if shape.len() != 2 {
        return Err(InterpolateError::InvalidParameter {
            parameter: "points".to_string(),
            message: "points must be 2D [n, d]".to_string(),
        });
    }
    let n = shape[0];
    let d = shape[1];

    // Auto-select epsilon if not provided: mean nearest-neighbor distance
    let eps = match epsilon {
        Some(e) => e,
        None => auto_epsilon(client, points, n)?,
    };

    // Compute pairwise distance matrix [n, n]
    let dist = cdist_euclidean(client, points, points)?;

    // Apply kernel function
    let mut kernel_mat = apply_kernel(client, &dist, kernel, eps)?;

    // Add smoothing to diagonal
    if smoothing > 0.0 {
        let eye = Tensor::from_slice(
            &(0..n)
                .flat_map(|i| (0..n).map(move |j| if i == j { smoothing } else { 0.0 }))
                .collect::<Vec<_>>(),
            &[n, n],
            device,
        );
        kernel_mat = client.add(&kernel_mat, &eye)?;
    }

    // Determine polynomial degree based on kernel
    let poly_degree = match kernel {
        RbfKernel::ThinPlateSpline | RbfKernel::Linear | RbfKernel::Cubic | RbfKernel::Quintic => {
            1 // linear polynomial augmentation
        }
        _ => 0, // no augmentation needed for positive definite kernels
    };

    if poly_degree == 0 {
        // Simple system: K * w = v
        let vals_col = if values.shape().len() == 1 {
            values.reshape(&[n, 1])?
        } else {
            values.clone()
        };

        let weights_col =
            LinearAlgebraAlgorithms::solve(client, &kernel_mat, &vals_col).map_err(|e| {
                InterpolateError::NumericalError {
                    message: format!("RBF solve failed: {}", e),
                }
            })?;
        let weights = if values.shape().len() == 1 {
            weights_col.reshape(&[n])?
        } else {
            weights_col
        };

        Ok(RbfModel {
            centers: points.clone(),
            weights,
            kernel,
            epsilon: eps,
            poly_coeffs: None,
            dim: d,
        })
    } else {
        // Augmented system with polynomial terms:
        // [ K   P ] [ w ]   [ v ]
        // [ P^T 0 ] [ c ] = [ 0 ]
        let p_cols = 1 + d; // constant + linear terms
        // Build polynomial matrix P [n, 1+d]: [1, x1, x2, ..., xd]
        let ones_col = Tensor::full_scalar(&[n, 1], DType::F64, 1.0, device);
        let p_mat = client.cat(&[&ones_col, points], -1)?;

        // Build augmented system
        let zeros_pp = Tensor::zeros(&[p_cols, p_cols], DType::F64, device);

        // Top row: [K, P]
        let kernel_mat = kernel_mat.contiguous()?;
        let p_mat = p_mat.contiguous()?;
        let top = client.cat(&[&kernel_mat, &p_mat], 1)?;
        // Bottom row: [P^T, 0]
        let p_t = p_mat.transpose(0, 1)?.contiguous()?;
        let bottom = client.cat(&[&p_t, &zeros_pp], 1)?;
        // Full: [[K, P], [P^T, 0]]
        let aug_mat = client.cat(&[&top, &bottom], 0)?;

        // Augmented rhs
        let vals_col = if values.shape().len() == 1 {
            values.reshape(&[n, 1])?
        } else {
            values.clone()
        };
        let n_out = vals_col.shape().get(1).copied().unwrap_or(1);
        let zeros_rhs = Tensor::zeros(&[p_cols, n_out], DType::F64, device);
        let aug_rhs = client.cat(&[&vals_col, &zeros_rhs], 0)?;

        let aug_mat = aug_mat.contiguous()?;
        let aug_rhs = aug_rhs.contiguous()?;
        let solution = LinearAlgebraAlgorithms::solve(client, &aug_mat, &aug_rhs).map_err(|e| {
            InterpolateError::NumericalError {
                message: format!("RBF augmented solve failed: {}", e),
            }
        })?;

        // Split solution into weights and polynomial coefficients
        let weights = solution.narrow(0, 0, n)?.contiguous()?;
        let poly_coeffs = solution.narrow(0, n, p_cols)?.contiguous()?;

        let weights = if values.shape().len() == 1 {
            weights.reshape(&[n])?
        } else {
            weights
        };
        let poly_coeffs = if values.shape().len() == 1 {
            poly_coeffs.reshape(&[p_cols])?
        } else {
            poly_coeffs
        };

        Ok(RbfModel {
            centers: points.clone(),
            weights,
            kernel,
            epsilon: eps,
            poly_coeffs: Some(poly_coeffs),
            dim: d,
        })
    }
}

/// Evaluate an RBF model at query points.
pub fn rbf_evaluate_impl<R, C>(
    client: &C,
    model: &RbfModel<R>,
    query: &Tensor<R>,
) -> InterpolateResult<Tensor<R>>
where
    R: Runtime<DType = DType>,
    C: TensorOps<R>
        + ScalarOps<R>
        + CompareOps<R>
        + MatmulOps<R>
        + ShapeOps<R>
        + LinearAlgebraAlgorithms<R>
        + RuntimeClient<R>,
{
    let device = client.device();
    let q_shape = query.shape().to_vec();
    let m = q_shape[0];

    // Compute distances from query points to centers
    let dist = cdist_euclidean(client, query, &model.centers)?;

    // Apply kernel
    let kernel_vals = apply_kernel(client, &dist, model.kernel, model.epsilon)?;

    // Compute weighted sum: result = kernel_vals @ weights
    let weights = if model.weights.shape().len() == 1 {
        model.weights.reshape(&[model.weights.shape()[0], 1])?
    } else {
        model.weights.clone()
    };
    let mut result = client.matmul(&kernel_vals, &weights)?;

    // Add polynomial contribution if present
    if let Some(ref poly_coeffs) = model.poly_coeffs {
        let ones_col: Tensor<R> = Tensor::full_scalar(&[m, 1], DType::F64, 1.0, device);
        let p_query = client.cat(&[&ones_col, query], -1)?;
        let pc = if poly_coeffs.shape().len() == 1 {
            poly_coeffs.reshape(&[poly_coeffs.shape()[0], 1])?
        } else {
            poly_coeffs.clone()
        };
        let poly_term = client.matmul(&p_query, &pc)?;
        result = client.add(&result, &poly_term)?;
    }

    // Squeeze if single output
    if result.shape().len() == 2 && result.shape()[1] == 1 {
        result = result.reshape(&[m])?;
    }

    Ok(result)
}

/// Compute Euclidean distance matrix between two point sets.
/// a: [n, d], b: [m, d] -> result: [n, m]
fn cdist_euclidean<R, C>(client: &C, a: &Tensor<R>, b: &Tensor<R>) -> InterpolateResult<Tensor<R>>
where
    R: Runtime<DType = DType>,
    C: TensorOps<R> + ScalarOps<R> + MatmulOps<R> + RuntimeClient<R>,
{
    let device = client.device();
    let n = a.shape()[0];
    let m = b.shape()[0];
    let d = a.shape()[1];

    // ||a_i - b_j||^2 = ||a_i||^2 + ||b_j||^2 - 2 * a_i . b_j
    // a_sq: [n, 1], b_sq: [1, m]
    let a_sq = {
        let a2 = client.mul(a, a)?;
        // Sum over dim 1
        let mut s = a2.narrow(1, 0, 1)?;
        for col in 1..d {
            let c = a2.narrow(1, col, 1)?;
            s = client.add(&s, &c)?;
        }
        s // [n, 1]
    };

    let b_sq = {
        let b2 = client.mul(b, b)?;
        let mut s = b2.narrow(1, 0, 1)?;
        for col in 1..d {
            let c = b2.narrow(1, col, 1)?;
            s = client.add(&s, &c)?;
        }
        s.transpose(0, 1)?.contiguous()? // [1, m]
    };

    let b_t = b.transpose(0, 1)?.contiguous()?;
    let ab = client.matmul(a, &b_t)?; // [n, m]
    let two_ab = client.mul_scalar(&ab, 2.0)?;

    // Broadcast a_sq [n,1] + b_sq [1,m] -> [n,m]
    let a_sq_b = a_sq.broadcast_to(&[n, m])?.contiguous()?;
    let b_sq_b = b_sq.broadcast_to(&[n, m])?.contiguous()?;
    let sum_sq = client.add(&a_sq_b, &b_sq_b)?;
    let dist_sq = client.sub(&sum_sq, &two_ab)?;

    // Clamp to non-negative before sqrt (numerical safety)
    let zero = Tensor::zeros(&[n, m], DType::F64, device);
    let dist_sq_safe = client.maximum(&dist_sq, &zero)?;
    let dist = client.sqrt(&dist_sq_safe)?;

    Ok(dist)
}

/// Apply RBF kernel function element-wise to distance matrix.
fn apply_kernel<R, C>(
    client: &C,
    dist: &Tensor<R>,
    kernel: RbfKernel,
    epsilon: f64,
) -> InterpolateResult<Tensor<R>>
where
    R: Runtime<DType = DType>,
    C: TensorOps<R> + ScalarOps<R> + CompareOps<R> + RuntimeClient<R>,
{
    let device = client.device();
    let shape = dist.shape().to_vec();

    match kernel {
        RbfKernel::ThinPlateSpline => {
            // r^2 * ln(r), with 0*ln(0) = 0
            let r2 = client.mul(dist, dist)?;
            let ln_r = client.log(dist)?;
            let result = client.mul(&r2, &ln_r)?;
            // Replace NaN (from 0*ln(0)) with 0
            let zero = Tensor::zeros(&shape, DType::F64, device);
            let is_zero = client.eq(dist, &zero)?;
            Ok(client.where_cond(&is_zero, &zero, &result)?)
        }
        RbfKernel::Multiquadric => {
            // sqrt(1 + (r/eps)^2)
            let r_eps = client.mul_scalar(dist, 1.0 / epsilon)?;
            let r_eps2 = client.mul(&r_eps, &r_eps)?;
            let one_plus = client.add_scalar(&r_eps2, 1.0)?;
            Ok(client.sqrt(&one_plus)?)
        }
        RbfKernel::InverseMultiquadric => {
            // 1/sqrt(1 + (r/eps)^2)
            let r_eps = client.mul_scalar(dist, 1.0 / epsilon)?;
            let r_eps2 = client.mul(&r_eps, &r_eps)?;
            let one_plus = client.add_scalar(&r_eps2, 1.0)?;
            let sq = client.sqrt(&one_plus)?;
            let one = Tensor::full_scalar(&shape, DType::F64, 1.0, device);
            Ok(client.div(&one, &sq)?)
        }
        RbfKernel::Gaussian => {
            // exp(-(r/eps)^2)
            let r_eps = client.mul_scalar(dist, 1.0 / epsilon)?;
            let r_eps2 = client.mul(&r_eps, &r_eps)?;
            let neg = client.mul_scalar(&r_eps2, -1.0)?;
            Ok(client.exp(&neg)?)
        }
        RbfKernel::Linear => {
            // r
            Ok(dist.clone())
        }
        RbfKernel::Cubic => {
            // r^3
            let r2 = client.mul(dist, dist)?;
            Ok(client.mul(&r2, dist)?)
        }
        RbfKernel::Quintic => {
            // r^5
            let r2 = client.mul(dist, dist)?;
            let r4 = client.mul(&r2, &r2)?;
            Ok(client.mul(&r4, dist)?)
        }
    }
}

/// Auto-select epsilon as mean nearest-neighbor distance (fully on-device).
fn auto_epsilon<R, C>(client: &C, points: &Tensor<R>, n: usize) -> InterpolateResult<f64>
where
    R: Runtime<DType = DType>,
    C: TensorOps<R> + ScalarOps<R> + MatmulOps<R> + RuntimeClient<R>,
{
    if n < 2 {
        return Ok(1.0);
    }

    // Compute pairwise distance matrix [n, n] on-device
    let dist = cdist_euclidean(client, points, points)?;

    // Add large value to diagonal so self-distance isn't the minimum
    let big = Tensor::full_scalar(&[n], DType::F64, 1e30, client.device());
    let big_diag = client.diagflat(&big)?;
    let dist_masked = client.add(&dist, &big_diag)?;

    // Min along rows → [n] nearest-neighbor distances
    let nn_dists = client.min(&dist_masked, &[1], false)?;

    // Mean → scalar
    let mean_dist = client.mean(&nn_dists, &[0], false)?;

    // Single scalar extraction (acceptable: returns config parameter)
    Ok(mean_dist.item::<f64>().unwrap_or(1.0))
}

#[cfg(test)]
mod tests {
    use super::*;
    use numr::runtime::cpu::{CpuClient, CpuDevice, CpuRuntime};

    fn setup() -> (CpuDevice, CpuClient) {
        let device = CpuDevice::new();
        let client = CpuClient::new(device.clone());
        (device, client)
    }

    #[test]
    fn test_rbf_gaussian_1d() {
        let (device, client) = setup();
        // 1D points as [n, 1]
        let points = Tensor::<CpuRuntime>::from_slice(&[0.0, 1.0, 2.0, 3.0], &[4, 1], &device);
        let values = Tensor::<CpuRuntime>::from_slice(&[0.0, 1.0, 4.0, 9.0], &[4], &device);

        let model = rbf_fit_impl(
            &client,
            &points,
            &values,
            RbfKernel::Gaussian,
            Some(1.0),
            0.0,
        )
        .unwrap();

        // Evaluate at known points — should recover values
        let result = rbf_evaluate_impl(&client, &model, &points).unwrap();
        let vals: Vec<f64> = result.to_vec();
        let expected = [0.0, 1.0, 4.0, 9.0];

        for i in 0..4 {
            assert!(
                (vals[i] - expected[i]).abs() < 1e-6,
                "point {}: {} vs {}",
                i,
                vals[i],
                expected[i]
            );
        }
    }

    #[test]
    fn test_rbf_thin_plate_2d() {
        let (device, client) = setup();
        // 2D points
        let points = Tensor::<CpuRuntime>::from_slice(
            &[0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0],
            &[4, 2],
            &device,
        );
        let values = Tensor::<CpuRuntime>::from_slice(&[0.0, 1.0, 1.0, 2.0], &[4], &device);

        let model = rbf_fit_impl(
            &client,
            &points,
            &values,
            RbfKernel::ThinPlateSpline,
            None,
            0.0,
        )
        .unwrap();

        // Evaluate at known points
        let result = rbf_evaluate_impl(&client, &model, &points).unwrap();
        let vals: Vec<f64> = result.to_vec();
        let expected = [0.0, 1.0, 1.0, 2.0];

        for i in 0..4 {
            assert!(
                (vals[i] - expected[i]).abs() < 1e-6,
                "point {}: {} vs {}",
                i,
                vals[i],
                expected[i]
            );
        }
    }

    #[test]
    fn test_rbf_multiquadric() {
        let (device, client) = setup();
        let points = Tensor::<CpuRuntime>::from_slice(&[0.0, 1.0, 2.0, 3.0], &[4, 1], &device);
        let values = Tensor::<CpuRuntime>::from_slice(&[1.0, 0.0, 1.0, 0.0], &[4], &device);

        let model = rbf_fit_impl(
            &client,
            &points,
            &values,
            RbfKernel::Multiquadric,
            Some(1.0),
            0.0,
        )
        .unwrap();

        let result = rbf_evaluate_impl(&client, &model, &points).unwrap();
        let vals: Vec<f64> = result.to_vec();
        let expected = [1.0, 0.0, 1.0, 0.0];

        for i in 0..4 {
            assert!(
                (vals[i] - expected[i]).abs() < 1e-6,
                "point {}: {} vs {}",
                i,
                vals[i],
                expected[i]
            );
        }
    }
}