pandrs 0.4.0

A high-performance DataFrame library for Rust, providing pandas-like API with advanced features including SIMD optimization, parallel processing, and distributed computing capabilities
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
//! Linear algebra bridge using SciRS2's implementations.
//!
//! All types and functions in this module are gated behind the `scirs2` feature flag.

#[cfg(feature = "scirs2")]
use crate::core::error::{Error, Result};
#[cfg(feature = "scirs2")]
use crate::dataframe::DataFrame;
#[cfg(feature = "scirs2")]
use crate::scirs2_integration::conversion::{array2_to_dataframe, dataframe_to_array2};
#[cfg(feature = "scirs2")]
use crate::series::Series;

/// Result of an eigenvalue decomposition.
#[cfg(feature = "scirs2")]
#[derive(Debug, Clone)]
pub struct EigResult {
    /// Eigenvalues (real parts for symmetric/Hermitian matrices)
    pub values: Vec<f64>,
    /// Eigenvectors as a DataFrame (each column is an eigenvector)
    pub vectors: DataFrame,
}

/// Result of a Singular Value Decomposition.
#[cfg(feature = "scirs2")]
#[derive(Debug, Clone)]
pub struct SvdResult {
    /// Left singular vectors as a DataFrame
    pub u: DataFrame,
    /// Singular values
    pub s: Vec<f64>,
    /// Right singular vectors (transposed) as a DataFrame
    pub vt: DataFrame,
}

/// Result of a QR decomposition.
#[cfg(feature = "scirs2")]
#[derive(Debug, Clone)]
pub struct QrResult {
    /// Orthogonal matrix Q as a DataFrame
    pub q: DataFrame,
    /// Upper triangular matrix R as a DataFrame
    pub r: DataFrame,
}

/// Result of an LU decomposition.
#[cfg(feature = "scirs2")]
#[derive(Debug, Clone)]
pub struct LuResult {
    /// Lower triangular matrix L (with unit diagonal) as a DataFrame
    pub l: DataFrame,
    /// Upper triangular matrix U as a DataFrame
    pub u: DataFrame,
}

/// Result of a least-squares solve.
#[cfg(feature = "scirs2")]
#[derive(Debug, Clone)]
pub struct LstsqDataFrameResult {
    /// Solution matrix as a DataFrame (one column per right-hand side column in b)
    pub solution: DataFrame,
    /// Sum of squared residuals for each RHS column
    pub residuals: Vec<f64>,
    /// Effective rank of matrix A
    pub rank: usize,
}

/// Linear algebra operations on DataFrames using SciRS2's implementations.
///
/// All methods treat DataFrames as numeric matrices. Only f64 numeric columns
/// are supported. Column names in the result DataFrames follow the convention
/// described in each method's documentation.
///
/// # Examples
///
/// ```rust
/// # #[cfg(feature = "scirs2")]
/// # {
/// use pandrs::{DataFrame, Series};
/// use pandrs::scirs2_integration::linalg::SciRS2LinAlg;
///
/// let mut a = DataFrame::new();
/// a.add_column("c0".to_string(),
///     Series::new(vec![2.0f64, 1.0], Some("c0".to_string())).expect("ok")).expect("ok");
/// a.add_column("c1".to_string(),
///     Series::new(vec![1.0f64, 2.0], Some("c1".to_string())).expect("ok")).expect("ok");
///
/// let det = SciRS2LinAlg::det(&a).expect("det ok");
/// // det of [[2, 1], [1, 2]] = 3.0
/// assert!((det - 3.0).abs() < 1e-10);
/// # }
/// ```
#[cfg(feature = "scirs2")]
pub struct SciRS2LinAlg;

#[cfg(feature = "scirs2")]
impl SciRS2LinAlg {
    /// Perform matrix multiplication of two DataFrames.
    ///
    /// Both DataFrames are treated as numeric matrices. The result has columns
    /// named `c0`, `c1`, ..., `c{n-1}` where n is the number of columns in `b`.
    ///
    /// # Arguments
    ///
    /// * `a` - Left matrix DataFrame with shape (m, k)
    /// * `b` - Right matrix DataFrame with shape (k, n)
    ///
    /// # Errors
    ///
    /// Returns an error if the inner dimensions do not match.
    pub fn matmul(a: &DataFrame, b: &DataFrame) -> Result<DataFrame> {
        let a_col_names = a.column_names();
        let a_cols: Vec<&str> = a_col_names.iter().map(|s| s.as_str()).collect();
        let b_col_names = b.column_names();
        let b_cols: Vec<&str> = b_col_names.iter().map(|s| s.as_str()).collect();

        let arr_a = dataframe_to_array2(a, &a_cols)?;
        let arr_b = dataframe_to_array2(b, &b_cols)?;

        let (m, k_a) = arr_a.dim();
        let (k_b, n) = arr_b.dim();

        if k_a != k_b {
            return Err(Error::InvalidInput(format!(
                "Matrix dimensions incompatible for multiplication: ({}, {}) x ({}, {})",
                m, k_a, k_b, n
            )));
        }

        let result = arr_a.dot(&arr_b);
        let col_names: Vec<String> = (0..n).map(|i| format!("c{}", i)).collect();
        array2_to_dataframe(&result, col_names)
    }

    /// Compute eigenvalues and eigenvectors of a symmetric matrix DataFrame.
    ///
    /// Uses SciRS2's `eigh` function which is optimised for symmetric matrices.
    /// Eigenvectors are returned as columns named `ev0`, `ev1`, ..., `ev{n-1}`.
    ///
    /// # Arguments
    ///
    /// * `df` - A square symmetric numeric DataFrame
    ///
    /// # Errors
    ///
    /// Returns an error if the DataFrame is not square or the decomposition fails.
    pub fn eig(df: &DataFrame) -> Result<EigResult> {
        use scirs2_linalg::eigh;

        let cols: Vec<String> = df.column_names();
        let col_refs: Vec<&str> = cols.iter().map(|s| s.as_str()).collect();
        let arr = dataframe_to_array2(df, &col_refs)?;

        let (n_rows, n_cols) = arr.dim();
        if n_rows != n_cols {
            return Err(Error::InvalidInput(format!(
                "Eigendecomposition requires a square matrix, got ({}, {})",
                n_rows, n_cols
            )));
        }

        let (eigenvalues, eigenvectors) = eigh(&arr.view(), None)
            .map_err(|e| Error::OperationFailed(format!("SciRS2 eigh failed: {}", e)))?;

        let values: Vec<f64> = eigenvalues.iter().copied().collect();

        let ev_col_names: Vec<String> = (0..n_cols).map(|i| format!("ev{}", i)).collect();
        let vectors_df = array2_to_dataframe(&eigenvectors, ev_col_names)?;

        Ok(EigResult {
            values,
            vectors: vectors_df,
        })
    }

    /// Compute the Singular Value Decomposition (SVD) of a DataFrame.
    ///
    /// Returns a triple (U, S, Vt) where U and Vt are DataFrames and S is a vector
    /// of singular values.
    ///
    /// Column naming:
    /// - `u`: columns named `u0`, `u1`, ..., `u{m-1}`
    /// - `vt`: columns named `v0`, `v1`, ..., `v{n-1}`
    ///
    /// # Arguments
    ///
    /// * `df` - The numeric DataFrame with shape (m, n)
    ///
    /// # Errors
    ///
    /// Returns an error if the DataFrame is empty or the SVD fails.
    pub fn svd(df: &DataFrame) -> Result<SvdResult> {
        use scirs2_linalg::svd;

        let cols: Vec<String> = df.column_names();
        let col_refs: Vec<&str> = cols.iter().map(|s| s.as_str()).collect();
        let arr = dataframe_to_array2(df, &col_refs)?;

        let (m, n) = arr.dim();

        let (u, s, vt) = svd(&arr.view(), false, None)
            .map_err(|e| Error::OperationFailed(format!("SciRS2 svd failed: {}", e)))?;

        let k = s.len();
        let singular_values: Vec<f64> = s.iter().copied().collect();

        let u_col_names: Vec<String> = (0..u.ncols()).map(|i| format!("u{}", i)).collect();
        let vt_col_names: Vec<String> = (0..vt.ncols()).map(|i| format!("v{}", i)).collect();

        let u_df = array2_to_dataframe(&u, u_col_names)?;
        let vt_df = array2_to_dataframe(&vt, vt_col_names)?;

        Ok(SvdResult {
            u: u_df,
            s: singular_values,
            vt: vt_df,
        })
    }

    /// Solve the linear system Ax = b for x.
    ///
    /// Both `a` and `b` are DataFrames. `a` must be square. `b` can have
    /// multiple columns (each represents a right-hand side vector).
    ///
    /// Result columns are named `x0`, `x1`, ..., `x{k-1}`.
    ///
    /// # Arguments
    ///
    /// * `a` - The coefficient matrix (square, n×n)
    /// * `b` - The right-hand side (n×k)
    ///
    /// # Errors
    ///
    /// Returns an error if `a` is not square or the system is singular.
    pub fn solve(a: &DataFrame, b: &DataFrame) -> Result<DataFrame> {
        use scirs2_linalg::solve_multiple;

        let a_cols: Vec<String> = a.column_names();
        let b_cols: Vec<String> = b.column_names();
        let a_col_refs: Vec<&str> = a_cols.iter().map(|s| s.as_str()).collect();
        let b_col_refs: Vec<&str> = b_cols.iter().map(|s| s.as_str()).collect();

        let arr_a = dataframe_to_array2(a, &a_col_refs)?;
        let arr_b = dataframe_to_array2(b, &b_col_refs)?;

        let (n_rows, n_cols) = arr_a.dim();
        if n_rows != n_cols {
            return Err(Error::InvalidInput(format!(
                "Coefficient matrix must be square, got ({}, {})",
                n_rows, n_cols
            )));
        }

        let x = solve_multiple(&arr_a.view(), &arr_b.view(), None)
            .map_err(|e| Error::OperationFailed(format!("SciRS2 solve failed: {}", e)))?;

        let k = x.ncols();
        let x_col_names: Vec<String> = (0..k).map(|i| format!("x{}", i)).collect();
        array2_to_dataframe(&x, x_col_names)
    }

    /// Compute the matrix inverse of a square numeric DataFrame.
    ///
    /// Result columns retain the same names as the input DataFrame.
    ///
    /// # Arguments
    ///
    /// * `df` - A square numeric DataFrame
    ///
    /// # Errors
    ///
    /// Returns an error if the DataFrame is not square or the matrix is singular.
    pub fn inv(df: &DataFrame) -> Result<DataFrame> {
        use scirs2_linalg::inv;

        let cols: Vec<String> = df.column_names();
        let col_refs: Vec<&str> = cols.iter().map(|s| s.as_str()).collect();
        let arr = dataframe_to_array2(df, &col_refs)?;

        let (n_rows, n_cols) = arr.dim();
        if n_rows != n_cols {
            return Err(Error::InvalidInput(format!(
                "Matrix inverse requires a square matrix, got ({}, {})",
                n_rows, n_cols
            )));
        }

        let inv_arr = inv(&arr.view(), None)
            .map_err(|e| Error::OperationFailed(format!("SciRS2 inv failed: {}", e)))?;

        array2_to_dataframe(&inv_arr, cols)
    }

    /// Compute the determinant of a square numeric DataFrame.
    ///
    /// # Arguments
    ///
    /// * `df` - A square numeric DataFrame
    ///
    /// # Errors
    ///
    /// Returns an error if the DataFrame is not square or the computation fails.
    pub fn det(df: &DataFrame) -> Result<f64> {
        use scirs2_linalg::det;

        let cols: Vec<String> = df.column_names();
        let col_refs: Vec<&str> = cols.iter().map(|s| s.as_str()).collect();
        let arr = dataframe_to_array2(df, &col_refs)?;

        let (n_rows, n_cols) = arr.dim();
        if n_rows != n_cols {
            return Err(Error::InvalidInput(format!(
                "Determinant requires a square matrix, got ({}, {})",
                n_rows, n_cols
            )));
        }

        det(&arr.view(), None)
            .map_err(|e| Error::OperationFailed(format!("SciRS2 det failed: {}", e)))
    }

    /// Compute the QR decomposition of a DataFrame.
    ///
    /// Returns a `QrResult` containing orthogonal Q and upper-triangular R such that
    /// `A = Q * R`.  Column names follow the conventions `q0, q1, ...` and `r0, r1, ...`.
    ///
    /// # Arguments
    ///
    /// * `df` - The input numeric DataFrame
    ///
    /// # Errors
    ///
    /// Returns an error if the DataFrame is empty or the decomposition fails.
    pub fn qr(df: &DataFrame) -> Result<QrResult> {
        use scirs2_linalg::qr;

        let cols: Vec<String> = df.column_names();
        let col_refs: Vec<&str> = cols.iter().map(|s| s.as_str()).collect();
        let arr = dataframe_to_array2(df, &col_refs)?;

        let (q_arr, r_arr) = qr(&arr.view(), None)
            .map_err(|e| Error::OperationFailed(format!("SciRS2 qr failed: {}", e)))?;

        let q_col_names: Vec<String> = (0..q_arr.ncols()).map(|i| format!("q{}", i)).collect();
        let r_col_names: Vec<String> = (0..r_arr.ncols()).map(|i| format!("r{}", i)).collect();

        let q_df = array2_to_dataframe(&q_arr, q_col_names)?;
        let r_df = array2_to_dataframe(&r_arr, r_col_names)?;

        Ok(QrResult { q: q_df, r: r_df })
    }

    /// Compute the Cholesky decomposition of a symmetric positive-definite DataFrame.
    ///
    /// Returns the lower-triangular factor L such that `A = L * L.T`.
    /// Result columns retain the same names as the input DataFrame.
    ///
    /// # Arguments
    ///
    /// * `df` - A square symmetric positive-definite numeric DataFrame
    ///
    /// # Errors
    ///
    /// Returns an error if the matrix is not square, not positive-definite, or the
    /// decomposition fails.
    pub fn cholesky(df: &DataFrame) -> Result<DataFrame> {
        use scirs2_linalg::cholesky;

        let cols: Vec<String> = df.column_names();
        let col_refs: Vec<&str> = cols.iter().map(|s| s.as_str()).collect();
        let arr = dataframe_to_array2(df, &col_refs)?;

        let (n_rows, n_cols) = arr.dim();
        if n_rows != n_cols {
            return Err(Error::InvalidInput(format!(
                "Cholesky decomposition requires a square matrix, got ({}, {})",
                n_rows, n_cols
            )));
        }

        let l = cholesky(&arr.view(), None)
            .map_err(|e| Error::OperationFailed(format!("SciRS2 cholesky failed: {}", e)))?;

        array2_to_dataframe(&l, cols)
    }

    /// Compute the LU decomposition of a DataFrame.
    ///
    /// Returns an `LuResult` with lower-triangular L (unit diagonal) and
    /// upper-triangular U such that `P * A = L * U` (pivot matrix P is absorbed).
    /// Column names follow `l0, l1, ...` and `u0, u1, ...`.
    ///
    /// # Arguments
    ///
    /// * `df` - The input numeric DataFrame
    ///
    /// # Errors
    ///
    /// Returns an error if the DataFrame is empty, singular, or the decomposition fails.
    pub fn lu(df: &DataFrame) -> Result<LuResult> {
        use scirs2_linalg::lu;

        let cols: Vec<String> = df.column_names();
        let col_refs: Vec<&str> = cols.iter().map(|s| s.as_str()).collect();
        let arr = dataframe_to_array2(df, &col_refs)?;

        let (_p_arr, l_arr, u_arr) = lu(&arr.view(), None)
            .map_err(|e| Error::OperationFailed(format!("SciRS2 lu failed: {}", e)))?;

        let l_col_names: Vec<String> = (0..l_arr.ncols()).map(|i| format!("l{}", i)).collect();
        let u_col_names: Vec<String> = (0..u_arr.ncols()).map(|i| format!("u{}", i)).collect();

        let l_df = array2_to_dataframe(&l_arr, l_col_names)?;
        let u_df = array2_to_dataframe(&u_arr, u_col_names)?;

        Ok(LuResult { l: l_df, u: u_df })
    }

    /// Solve the least-squares problem min ||Ax - b||² for each column of b.
    ///
    /// Uses SciRS2's `lstsq` function which supports over-determined and
    /// under-determined systems.  Solution columns are named `x0, x1, ...`.
    ///
    /// # Arguments
    ///
    /// * `a` - Coefficient matrix (m × n)
    /// * `b` - Right-hand side matrix (m × k); each column is an independent RHS
    ///
    /// # Errors
    ///
    /// Returns an error if the dimensions are incompatible or the solve fails.
    pub fn lstsq(a: &DataFrame, b: &DataFrame) -> Result<LstsqDataFrameResult> {
        use scirs2_linalg::lstsq;

        let a_cols: Vec<String> = a.column_names();
        let b_cols: Vec<String> = b.column_names();
        let a_col_refs: Vec<&str> = a_cols.iter().map(|s| s.as_str()).collect();
        let b_col_refs: Vec<&str> = b_cols.iter().map(|s| s.as_str()).collect();

        let arr_a = dataframe_to_array2(a, &a_col_refs)?;
        let arr_b = dataframe_to_array2(b, &b_col_refs)?;

        let (m_a, _n) = arr_a.dim();
        let (m_b, k) = arr_b.dim();

        if m_a != m_b {
            return Err(Error::InvalidInput(format!(
                "lstsq: A has {} rows but b has {} rows",
                m_a, m_b
            )));
        }

        let mut solution_cols: Vec<Vec<f64>> = Vec::with_capacity(k);
        let mut residuals: Vec<f64> = Vec::with_capacity(k);
        let mut final_rank = 0usize;

        for j in 0..k {
            let col_b: scirs2_core::ndarray::Array1<f64> = arr_b.column(j).to_owned();
            let res = lstsq(&arr_a.view(), &col_b.view(), None)
                .map_err(|e| Error::OperationFailed(format!("SciRS2 lstsq failed: {}", e)))?;

            solution_cols.push(res.x.iter().copied().collect());
            residuals.push(res.residuals);
            final_rank = res.rank;
        }

        // Build solution DataFrame
        let n_sol = if solution_cols.is_empty() {
            0
        } else {
            solution_cols[0].len()
        };
        let col_names: Vec<String> = (0..k).map(|i| format!("x{}", i)).collect();

        let mut sol_df = DataFrame::new();
        for (j, col_name) in col_names.iter().enumerate() {
            let vals = solution_cols[j].clone();
            let series = Series::new(vals, Some(col_name.clone()))?;
            sol_df.add_column(col_name.clone(), series)?;
        }
        let _ = n_sol; // silence unused warning

        Ok(LstsqDataFrameResult {
            solution: sol_df,
            residuals,
            rank: final_rank,
        })
    }

    /// Compute a matrix norm of a DataFrame.
    ///
    /// Supported `ord` values:
    /// - `"fro"` — Frobenius norm (default if unrecognised)
    /// - `"1"` — maximum column sum (1-norm)
    /// - `"inf"` — maximum row sum (infinity-norm)
    ///
    /// # Arguments
    ///
    /// * `df` - The numeric DataFrame
    /// * `ord` - Norm order string (`"fro"`, `"1"`, or `"inf"`)
    ///
    /// # Errors
    ///
    /// Returns an error if the DataFrame is empty or the computation fails.
    pub fn matrix_norm(df: &DataFrame, ord: &str) -> Result<f64> {
        use scirs2_linalg::matrix_norm;

        let cols: Vec<String> = df.column_names();
        let col_refs: Vec<&str> = cols.iter().map(|s| s.as_str()).collect();
        let arr = dataframe_to_array2(df, &col_refs)?;

        matrix_norm(&arr.view(), ord, None)
            .map_err(|e| Error::OperationFailed(format!("SciRS2 matrix_norm failed: {}", e)))
    }

    /// Compute the numerical rank of a DataFrame using SVD.
    ///
    /// Uses a tolerance of `max(m, n) * eps * sigma_max` to determine which
    /// singular values are significant.
    ///
    /// # Arguments
    ///
    /// * `df` - The numeric DataFrame
    ///
    /// # Errors
    ///
    /// Returns an error if the DataFrame is empty or the SVD fails.
    pub fn matrix_rank(df: &DataFrame) -> Result<usize> {
        use scirs2_linalg::matrix_rank;

        let cols: Vec<String> = df.column_names();
        let col_refs: Vec<&str> = cols.iter().map(|s| s.as_str()).collect();
        let arr = dataframe_to_array2(df, &col_refs)?;

        matrix_rank(&arr.view(), None, None)
            .map_err(|e| Error::OperationFailed(format!("SciRS2 matrix_rank failed: {}", e)))
    }

    /// Compute the 2-norm condition number of a square numeric DataFrame.
    ///
    /// Returns `sigma_max / sigma_min` where sigma values are the singular values.
    /// Returns `f64::INFINITY` for singular matrices.
    ///
    /// # Arguments
    ///
    /// * `df` - A square numeric DataFrame
    ///
    /// # Errors
    ///
    /// Returns an error if the DataFrame is not square or the computation fails.
    pub fn condition_number(df: &DataFrame) -> Result<f64> {
        use scirs2_linalg::cond;

        let cols: Vec<String> = df.column_names();
        let col_refs: Vec<&str> = cols.iter().map(|s| s.as_str()).collect();
        let arr = dataframe_to_array2(df, &col_refs)?;

        let (n_rows, n_cols) = arr.dim();
        if n_rows != n_cols {
            return Err(Error::InvalidInput(format!(
                "Condition number requires a square matrix, got ({}, {})",
                n_rows, n_cols
            )));
        }

        cond(&arr.view(), Some("2"), None)
            .map_err(|e| Error::OperationFailed(format!("SciRS2 cond failed: {}", e)))
    }

    /// Compute the pseudoinverse of a numeric DataFrame using least-squares.
    ///
    /// Computes `A_pinv = lstsq(A, I)` where `I` is the m×m identity matrix.
    /// Result columns are named `pi0, pi1, ...`.
    ///
    /// # Arguments
    ///
    /// * `df` - The numeric DataFrame with shape (m, n)
    ///
    /// # Errors
    ///
    /// Returns an error if the DataFrame is empty or the solve fails.
    pub fn pinv(df: &DataFrame) -> Result<DataFrame> {
        use scirs2_core::ndarray::Array2;
        use scirs2_linalg::lstsq;

        let cols: Vec<String> = df.column_names();
        let col_refs: Vec<&str> = cols.iter().map(|s| s.as_str()).collect();
        let arr_a = dataframe_to_array2(df, &col_refs)?;

        let (m, n) = arr_a.dim();
        if m == 0 || n == 0 {
            return Err(Error::EmptyData("pinv: input matrix is empty".to_string()));
        }

        // Identity matrix of size m
        let eye = Array2::<f64>::eye(m);

        // Solve A * X = I column by column and collect the n×m pseudoinverse
        let mut pinv_cols: Vec<Vec<f64>> = Vec::with_capacity(m);
        for j in 0..m {
            let col_b: scirs2_core::ndarray::Array1<f64> = eye.column(j).to_owned();
            let res = lstsq(&arr_a.view(), &col_b.view(), None).map_err(|e| {
                Error::OperationFailed(format!("SciRS2 lstsq (pinv) failed: {}", e))
            })?;
            pinv_cols.push(res.x.iter().copied().collect());
        }

        // pinv_cols[j] is the j-th column of A_pinv (length n)
        // Build result DataFrame with m columns
        let col_names: Vec<String> = (0..m).map(|i| format!("pi{}", i)).collect();
        let mut result_df = DataFrame::new();
        for (j, col_name) in col_names.iter().enumerate() {
            let vals = pinv_cols[j].clone();
            let series = Series::new(vals, Some(col_name.clone()))?;
            result_df.add_column(col_name.clone(), series)?;
        }

        Ok(result_df)
    }
}