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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
//! Sparse stencil assembly for finite difference methods.
//!
//! Builds sparse Laplacian matrices from grid specifications.
//! Stencil assembly is a one-time setup cost at API boundary.
use crate::DType;

use numr::error::Result;
use numr::runtime::Runtime;
use numr::sparse::CsrData;
use numr::tensor::Tensor;

use crate::pde::types::{Grid2D, Grid3D};

/// Assemble a 2D negative Laplacian (-nabla^2) with Dirichlet BCs as CSR.
///
/// Boundary rows become identity rows. Interior neighbors that are boundary
/// nodes have their contribution moved to the RHS vector.
///
/// This solves: -nabla^2 u = f, so the matrix is SPD for interior nodes.
pub fn assemble_neg_laplacian_2d_dirichlet<R>(
    grid: &Grid2D,
    boundary_values: &[f64],
    rhs_data: &mut [f64],
    device: &R::Device,
) -> Result<CsrData<R>>
where
    R: Runtime<DType = DType>,
{
    let nx = grid.nx;
    let ny = grid.ny;
    let n = nx * ny;
    let dx2 = grid.dx * grid.dx;
    let dy2 = grid.dy * grid.dy;

    let is_boundary =
        |i: usize, j: usize| -> bool { i == 0 || i == nx - 1 || j == 0 || j == ny - 1 };

    let bval = |idx: usize| -> f64 {
        if idx < boundary_values.len() {
            boundary_values[idx]
        } else {
            0.0
        }
    };

    let mut rows = Vec::with_capacity(5 * n);
    let mut cols = Vec::with_capacity(5 * n);
    let mut vals: Vec<f64> = Vec::with_capacity(5 * n);

    for i in 0..nx {
        for j in 0..ny {
            let idx = i * ny + j;

            if is_boundary(i, j) {
                rows.push(idx as i64);
                cols.push(idx as i64);
                vals.push(1.0);
                rhs_data[idx] = bval(idx);
            } else {
                let mut center = 0.0;

                // Left (i-1, j)
                let left_idx = (i - 1) * ny + j;
                if is_boundary(i - 1, j) {
                    rhs_data[idx] += bval(left_idx) / dx2;
                } else {
                    rows.push(idx as i64);
                    cols.push(left_idx as i64);
                    vals.push(-1.0 / dx2);
                }
                center += 1.0 / dx2;

                // Right (i+1, j)
                let right_idx = (i + 1) * ny + j;
                if is_boundary(i + 1, j) {
                    rhs_data[idx] += bval(right_idx) / dx2;
                } else {
                    rows.push(idx as i64);
                    cols.push(right_idx as i64);
                    vals.push(-1.0 / dx2);
                }
                center += 1.0 / dx2;

                // Bottom (i, j-1)
                let bottom_idx = i * ny + (j - 1);
                if is_boundary(i, j - 1) {
                    rhs_data[idx] += bval(bottom_idx) / dy2;
                } else {
                    rows.push(idx as i64);
                    cols.push(bottom_idx as i64);
                    vals.push(-1.0 / dy2);
                }
                center += 1.0 / dy2;

                // Top (i, j+1)
                let top_idx = i * ny + (j + 1);
                if is_boundary(i, j + 1) {
                    rhs_data[idx] += bval(top_idx) / dy2;
                } else {
                    rows.push(idx as i64);
                    cols.push(top_idx as i64);
                    vals.push(-1.0 / dy2);
                }
                center += 1.0 / dy2;

                rows.push(idx as i64);
                cols.push(idx as i64);
                vals.push(center);
            }
        }
    }

    let nnz = rows.len();
    let row_t = Tensor::<R>::from_slice(&rows, &[nnz], device);
    let col_t = Tensor::<R>::from_slice(&cols, &[nnz], device);
    let val_t = Tensor::<R>::from_slice(&vals, &[nnz], device);

    // COO to CSR: sort by row, build row_ptrs
    coo_to_csr_sorted::<R>(&row_t, &col_t, &val_t, [n, n], device)
}

/// Assemble 2D Laplacian (not negated) as CSR for time-dependent solvers.
///
/// This returns L such that L*u approximates nabla^2 u.
/// Used by heat/wave equation solvers where the sign is handled externally.
pub fn assemble_laplacian_2d<R>(grid: &Grid2D, device: &R::Device) -> Result<CsrData<R>>
where
    R: Runtime<DType = DType>,
{
    let nx = grid.nx;
    let ny = grid.ny;
    let n = nx * ny;
    let dx2 = grid.dx * grid.dx;
    let dy2 = grid.dy * grid.dy;

    let mut rows = Vec::with_capacity(5 * n);
    let mut cols = Vec::with_capacity(5 * n);
    let mut vals: Vec<f64> = Vec::with_capacity(5 * n);

    for i in 0..nx {
        for j in 0..ny {
            let idx = i * ny + j;
            let mut center = 0.0;

            if i > 0 {
                rows.push(idx as i64);
                cols.push((idx - ny) as i64);
                vals.push(1.0 / dx2);
                center -= 1.0 / dx2;
            }
            if i < nx - 1 {
                rows.push(idx as i64);
                cols.push((idx + ny) as i64);
                vals.push(1.0 / dx2);
                center -= 1.0 / dx2;
            }
            if j > 0 {
                rows.push(idx as i64);
                cols.push((idx - 1) as i64);
                vals.push(1.0 / dy2);
                center -= 1.0 / dy2;
            }
            if j < ny - 1 {
                rows.push(idx as i64);
                cols.push((idx + 1) as i64);
                vals.push(1.0 / dy2);
                center -= 1.0 / dy2;
            }

            rows.push(idx as i64);
            cols.push(idx as i64);
            vals.push(center);
        }
    }

    let nnz = rows.len();
    let row_t = Tensor::<R>::from_slice(&rows, &[nnz], device);
    let col_t = Tensor::<R>::from_slice(&cols, &[nnz], device);
    let val_t = Tensor::<R>::from_slice(&vals, &[nnz], device);

    coo_to_csr_sorted::<R>(&row_t, &col_t, &val_t, [n, n], device)
}

/// Assemble 3D Laplacian as CSR (7-point stencil).
pub fn assemble_laplacian_3d<R>(grid: &Grid3D, device: &R::Device) -> Result<CsrData<R>>
where
    R: Runtime<DType = DType>,
{
    let nx = grid.nx;
    let ny = grid.ny;
    let nz = grid.nz;
    let n = nx * ny * nz;
    let nyz = ny * nz;
    let dx2 = grid.dx * grid.dx;
    let dy2 = grid.dy * grid.dy;
    let dz2 = grid.dz * grid.dz;

    let mut rows = Vec::with_capacity(7 * n);
    let mut cols = Vec::with_capacity(7 * n);
    let mut vals: Vec<f64> = Vec::with_capacity(7 * n);

    for i in 0..nx {
        for j in 0..ny {
            for k in 0..nz {
                let idx = i * nyz + j * nz + k;
                let mut center = 0.0;

                if i > 0 {
                    rows.push(idx as i64);
                    cols.push((idx - nyz) as i64);
                    vals.push(1.0 / dx2);
                    center -= 1.0 / dx2;
                }
                if i < nx - 1 {
                    rows.push(idx as i64);
                    cols.push((idx + nyz) as i64);
                    vals.push(1.0 / dx2);
                    center -= 1.0 / dx2;
                }
                if j > 0 {
                    rows.push(idx as i64);
                    cols.push((idx - nz) as i64);
                    vals.push(1.0 / dy2);
                    center -= 1.0 / dy2;
                }
                if j < ny - 1 {
                    rows.push(idx as i64);
                    cols.push((idx + nz) as i64);
                    vals.push(1.0 / dy2);
                    center -= 1.0 / dy2;
                }
                if k > 0 {
                    rows.push(idx as i64);
                    cols.push((idx - 1) as i64);
                    vals.push(1.0 / dz2);
                    center -= 1.0 / dz2;
                }
                if k < nz - 1 {
                    rows.push(idx as i64);
                    cols.push((idx + 1) as i64);
                    vals.push(1.0 / dz2);
                    center -= 1.0 / dz2;
                }

                rows.push(idx as i64);
                cols.push(idx as i64);
                vals.push(center);
            }
        }
    }

    let nnz = rows.len();
    let row_t = Tensor::<R>::from_slice(&rows, &[nnz], device);
    let col_t = Tensor::<R>::from_slice(&cols, &[nnz], device);
    let val_t = Tensor::<R>::from_slice(&vals, &[nnz], device);

    coo_to_csr_sorted::<R>(&row_t, &col_t, &val_t, [n, n], device)
}

// ============================================================================
// General (per-side) BC stencil
// ============================================================================

/// Per-side boundary condition for the general Poisson assembler.
///
/// Used by [`assemble_neg_laplacian_2d_mixed`] to express any combination of
/// Dirichlet, Neumann, and Periodic conditions across the four grid sides.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum SideBc {
    /// Fixed value (supplied per node via `dirichlet_values`).
    Dirichlet,
    /// Fixed outward normal derivative `du/dn = g`.
    Neumann(f64),
    /// Periodic wrap-around (must be paired with the opposite side).
    Periodic,
}

/// Assemble a 2D negative Laplacian (-nabla^2) with arbitrary per-side BCs.
///
/// This is the single general assembler underpinning every non-pure-Dirichlet
/// Poisson configuration: uniform Neumann, uniform Periodic, and any
/// heterogeneous (mixed) per-side combination are all handled here.
///
/// `sides` = `[left, right, bottom, top]`. `dirichlet_values` is a flat
/// `[nx*ny]` row-major array giving the fixed value at each node lying on a
/// Dirichlet side (entries for non-Dirichlet nodes are ignored).
///
/// Per-node discretisation of `(i, j)`:
/// - **Dirichlet dominance**: a node on any Dirichlet side becomes an identity
///   row `u = dirichlet_values[idx]`. A fixed value overrides flux/periodicity,
///   so corners shared between a Dirichlet side and another side resolve
///   deterministically to Dirichlet.
/// - **Neumann** side (`du/dn = g`, second-order ghost-node elimination): the
///   boundary row gets `+2/h^2` on the diagonal and `-2/h^2` toward its single
///   interior neighbour, with `+2g/h` added to the RHS.
/// - **Periodic** side: the missing neighbour wraps to the opposite edge with
///   the standard `-1/h^2` coefficient.
/// - A neighbour that is itself a Dirichlet node contributes its known value to
///   the RHS instead of producing a matrix entry.
///
/// Periodic sides MUST be paired (left&right, or bottom&top); the caller is
/// responsible for validating this.
///
/// A configuration with no Dirichlet side (all Neumann/Periodic) yields a
/// singular system (solution defined up to an additive constant); the caller
/// must fix the gauge before solving.
pub fn assemble_neg_laplacian_2d_mixed<R>(
    grid: &Grid2D,
    sides: &[SideBc; 4],
    dirichlet_values: &[f64],
    rhs_data: &mut [f64],
    device: &R::Device,
) -> Result<CsrData<R>>
where
    R: Runtime<DType = DType>,
{
    let nx = grid.nx;
    let ny = grid.ny;
    let n = nx * ny;
    let dx = grid.dx;
    let dy = grid.dy;
    let dx2 = dx * dx;
    let dy2 = dy * dy;
    let [s_left, s_right, s_bottom, s_top] = *sides;

    // A node is a fixed-value (Dirichlet) node iff it lies on any Dirichlet side.
    let is_dir = |i: usize, j: usize| -> bool {
        (i == 0 && s_left == SideBc::Dirichlet)
            || (i == nx - 1 && s_right == SideBc::Dirichlet)
            || (j == 0 && s_bottom == SideBc::Dirichlet)
            || (j == ny - 1 && s_top == SideBc::Dirichlet)
    };
    let dval = |idx: usize| -> f64 { dirichlet_values.get(idx).copied().unwrap_or(0.0) };

    let mut rows: Vec<i64> = Vec::with_capacity(5 * n);
    let mut cols: Vec<i64> = Vec::with_capacity(5 * n);
    let mut vals: Vec<f64> = Vec::with_capacity(5 * n);

    // Add a standard (coefficient -1/h^2) neighbour at grid position `(ni, nj)`.
    // If that neighbour is a Dirichlet node its known value is folded into the
    // RHS; otherwise a matrix entry is emitted. The centre coefficient grows by
    // 1/h^2 either way.
    #[allow(clippy::too_many_arguments)]
    fn add_neighbour(
        ni: usize,
        nj: usize,
        ny: usize,
        inv_h2: f64,
        idx: usize,
        is_dir: &impl Fn(usize, usize) -> bool,
        dval: &impl Fn(usize) -> f64,
        rows: &mut Vec<i64>,
        cols: &mut Vec<i64>,
        vals: &mut Vec<f64>,
        rhs_data: &mut [f64],
        center: &mut f64,
    ) {
        let nb = ni * ny + nj;
        if is_dir(ni, nj) {
            rhs_data[idx] += dval(nb) * inv_h2;
        } else {
            rows.push(idx as i64);
            cols.push(nb as i64);
            vals.push(-inv_h2);
        }
        *center += inv_h2;
    }

    let inv_dx2 = 1.0 / dx2;
    let inv_dy2 = 1.0 / dy2;

    for i in 0..nx {
        for j in 0..ny {
            let idx = i * ny + j;

            // Dirichlet dominance: identity row u = dirichlet_values[idx].
            if is_dir(i, j) {
                rows.push(idx as i64);
                cols.push(idx as i64);
                vals.push(1.0);
                rhs_data[idx] = dval(idx);
                continue;
            }

            let on_left = i == 0;
            let on_right = i == nx - 1;
            let on_bottom = j == 0;
            let on_top = j == ny - 1;
            let mut center = 0.0f64;

            // ---- x-axis ----
            if on_left {
                if let SideBc::Neumann(g) = s_left {
                    // Ghost-node fully accounts for the x-direction at this node.
                    center += 2.0 / dx2;
                    let nb = (i + 1) * ny + j;
                    if is_dir(i + 1, j) {
                        rhs_data[idx] += (2.0 / dx2) * dval(nb);
                    } else {
                        rows.push(idx as i64);
                        cols.push(nb as i64);
                        vals.push(-2.0 / dx2);
                    }
                    rhs_data[idx] += 2.0 * g / dx;
                } else {
                    // Periodic left edge: wrap to i = nx-1, plus the normal right neighbour.
                    add_neighbour(
                        nx - 1,
                        j,
                        ny,
                        inv_dx2,
                        idx,
                        &is_dir,
                        &dval,
                        &mut rows,
                        &mut cols,
                        &mut vals,
                        rhs_data,
                        &mut center,
                    );
                    add_neighbour(
                        i + 1,
                        j,
                        ny,
                        inv_dx2,
                        idx,
                        &is_dir,
                        &dval,
                        &mut rows,
                        &mut cols,
                        &mut vals,
                        rhs_data,
                        &mut center,
                    );
                }
            } else if on_right {
                if let SideBc::Neumann(g) = s_right {
                    center += 2.0 / dx2;
                    let nb = (i - 1) * ny + j;
                    if is_dir(i - 1, j) {
                        rhs_data[idx] += (2.0 / dx2) * dval(nb);
                    } else {
                        rows.push(idx as i64);
                        cols.push(nb as i64);
                        vals.push(-2.0 / dx2);
                    }
                    rhs_data[idx] += 2.0 * g / dx;
                } else {
                    // Periodic right edge: normal left neighbour, plus wrap to i = 0.
                    add_neighbour(
                        i - 1,
                        j,
                        ny,
                        inv_dx2,
                        idx,
                        &is_dir,
                        &dval,
                        &mut rows,
                        &mut cols,
                        &mut vals,
                        rhs_data,
                        &mut center,
                    );
                    add_neighbour(
                        0,
                        j,
                        ny,
                        inv_dx2,
                        idx,
                        &is_dir,
                        &dval,
                        &mut rows,
                        &mut cols,
                        &mut vals,
                        rhs_data,
                        &mut center,
                    );
                }
            } else {
                // Interior in x: both neighbours.
                add_neighbour(
                    i - 1,
                    j,
                    ny,
                    inv_dx2,
                    idx,
                    &is_dir,
                    &dval,
                    &mut rows,
                    &mut cols,
                    &mut vals,
                    rhs_data,
                    &mut center,
                );
                add_neighbour(
                    i + 1,
                    j,
                    ny,
                    inv_dx2,
                    idx,
                    &is_dir,
                    &dval,
                    &mut rows,
                    &mut cols,
                    &mut vals,
                    rhs_data,
                    &mut center,
                );
            }

            // ---- y-axis ----
            if on_bottom {
                if let SideBc::Neumann(g) = s_bottom {
                    center += 2.0 / dy2;
                    let nb = i * ny + (j + 1);
                    if is_dir(i, j + 1) {
                        rhs_data[idx] += (2.0 / dy2) * dval(nb);
                    } else {
                        rows.push(idx as i64);
                        cols.push(nb as i64);
                        vals.push(-2.0 / dy2);
                    }
                    rhs_data[idx] += 2.0 * g / dy;
                } else {
                    add_neighbour(
                        i,
                        ny - 1,
                        ny,
                        inv_dy2,
                        idx,
                        &is_dir,
                        &dval,
                        &mut rows,
                        &mut cols,
                        &mut vals,
                        rhs_data,
                        &mut center,
                    );
                    add_neighbour(
                        i,
                        j + 1,
                        ny,
                        inv_dy2,
                        idx,
                        &is_dir,
                        &dval,
                        &mut rows,
                        &mut cols,
                        &mut vals,
                        rhs_data,
                        &mut center,
                    );
                }
            } else if on_top {
                if let SideBc::Neumann(g) = s_top {
                    center += 2.0 / dy2;
                    let nb = i * ny + (j - 1);
                    if is_dir(i, j - 1) {
                        rhs_data[idx] += (2.0 / dy2) * dval(nb);
                    } else {
                        rows.push(idx as i64);
                        cols.push(nb as i64);
                        vals.push(-2.0 / dy2);
                    }
                    rhs_data[idx] += 2.0 * g / dy;
                } else {
                    add_neighbour(
                        i,
                        j - 1,
                        ny,
                        inv_dy2,
                        idx,
                        &is_dir,
                        &dval,
                        &mut rows,
                        &mut cols,
                        &mut vals,
                        rhs_data,
                        &mut center,
                    );
                    add_neighbour(
                        i,
                        0,
                        ny,
                        inv_dy2,
                        idx,
                        &is_dir,
                        &dval,
                        &mut rows,
                        &mut cols,
                        &mut vals,
                        rhs_data,
                        &mut center,
                    );
                }
            } else {
                add_neighbour(
                    i,
                    j - 1,
                    ny,
                    inv_dy2,
                    idx,
                    &is_dir,
                    &dval,
                    &mut rows,
                    &mut cols,
                    &mut vals,
                    rhs_data,
                    &mut center,
                );
                add_neighbour(
                    i,
                    j + 1,
                    ny,
                    inv_dy2,
                    idx,
                    &is_dir,
                    &dval,
                    &mut rows,
                    &mut cols,
                    &mut vals,
                    rhs_data,
                    &mut center,
                );
            }

            rows.push(idx as i64);
            cols.push(idx as i64);
            vals.push(center);
        }
    }

    coo_to_csr_unsorted::<R>(&rows, &cols, &vals, [n, n], device)
}

/// Convert unsorted COO data to CSR format (sorts by row then column).
fn coo_to_csr_unsorted<R: Runtime<DType = DType>>(
    row_vec: &[i64],
    col_vec: &[i64],
    val_vec: &[f64],
    shape: [usize; 2],
    device: &R::Device,
) -> Result<CsrData<R>> {
    let [nrows, _ncols] = shape;
    let nnz = row_vec.len();

    // Sort by (row, col)
    let mut order: Vec<usize> = (0..nnz).collect();
    order.sort_by_key(|&k| (row_vec[k], col_vec[k]));

    let mut sorted_rows = vec![0i64; nnz];
    let mut sorted_cols = vec![0i64; nnz];
    let mut sorted_vals = vec![0.0f64; nnz];
    for (dst, &src) in order.iter().enumerate() {
        sorted_rows[dst] = row_vec[src];
        sorted_cols[dst] = col_vec[src];
        sorted_vals[dst] = val_vec[src];
    }

    let mut row_ptrs = vec![0i64; nrows + 1];
    for &r in &sorted_rows {
        row_ptrs[r as usize + 1] += 1;
    }
    for i in 1..=nrows {
        row_ptrs[i] += row_ptrs[i - 1];
    }
    debug_assert_eq!(row_ptrs[nrows] as usize, nnz);

    let rp_tensor = Tensor::<R>::from_slice(&row_ptrs, &[nrows + 1], device);
    let col_tensor = Tensor::<R>::from_slice(&sorted_cols, &[nnz], device);
    let val_tensor = Tensor::<R>::from_slice(&sorted_vals, &[nnz], device);

    CsrData::new(rp_tensor, col_tensor, val_tensor, shape)
}

/// Convert already-sorted COO data to CSR format.
///
/// The stencil assembly produces entries sorted by row (and within each row by column),
/// so we can build row_ptrs directly without a general sort.
fn coo_to_csr_sorted<R: Runtime<DType = DType>>(
    row_indices: &Tensor<R>,
    col_indices: &Tensor<R>,
    values: &Tensor<R>,
    shape: [usize; 2],
    device: &R::Device,
) -> Result<CsrData<R>> {
    let [nrows, _ncols] = shape;
    let rows_vec: Vec<i64> = row_indices.to_vec();
    let nnz = rows_vec.len();

    // Build row_ptrs
    let mut row_ptrs = vec![0i64; nrows + 1];
    for &r in &rows_vec {
        row_ptrs[r as usize + 1] += 1;
    }
    // Cumulative sum
    for i in 1..=nrows {
        row_ptrs[i] += row_ptrs[i - 1];
    }
    debug_assert_eq!(row_ptrs[nrows] as usize, nnz);

    let rp_tensor = Tensor::<R>::from_slice(&row_ptrs, &[nrows + 1], device);
    CsrData::new(rp_tensor, col_indices.clone(), values.clone(), shape)
}