Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

Add PInv Moore-Penrose pseudoinverse

Đang mở
#292 9 bình luận 1 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
35/100
Loại issue
Tính năng
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
rust
Lĩnh vực
data

Hướng nghiên cứu

Start in numrs/src/linalg.rs at the proposed Pinv traits and the existing self.svd(true, true) call. Check how ndarray-linalg represents real and complex Scalar values and how matrix multiplication is supported across those types. Done means a generic Moore-Penrose pseudoinverse with optional rcond that avoids the reported multiplication error and matches the NumPy and MATLAB references.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

I was porting some code from NumPy, and needed the pinv[1][2] function (Moore-Penrose pseudoinverse). I have started implementing it but have run into some issues making it generic over real and complex numbers.

pub trait Pinv {
    type B;
    type E;
    type C;
    fn pinv(&self, rcond: Option<Self::E>) -> Result<Self::B, Error>;
}

pub trait PInvVInto {
    type B;
    type E;
    fn pinv(self, rcond: Option<Self::E>) -> Result<Self::B, Error>;
}

pub trait PInvInplace {
    type B;
    type E;
    fn pinv(&mut self, rcond: Option<Self::E>) -> Result<Self::B, Error>;
}

impl<A, S> Pinv for ArrayBase<S, Ix2>
where
    A: Scalar + Lapack + PartialOrd + Zero,
    S: Data<Elem = A>,
{
    type E = A::Real;
    type B = Array2<A>;
    type C = Array1<A::Real>;

    fn pinv(&self, rconf: Option<Self::E>) -> Result<Self::B, Error> {
        let result: (Option<Self::B>, Self::C, Option<Self::B>) =
            self.svd(true, true).map_err(|_| Error::SvdFailed)?;
        if let (Some(u), s, Some(vt)) = result {
            // discard small singular values
            let s: Self::C = if let Some(rconf) = rconf {
                s.mapv(|v| if v > rconf { v } else { Zero::zero() })
            } else {
                s
            };

            let u = u.reversed_axes();
            let s = s.insert_axis(Axis(1));
            let x1 = s * u;
            Ok(&vt.reversed_axes() * x1)
        } else {
            Err(Error::SvdFailed)
        }
    }
}

At the moment I am getting this error

error[E0277]: cannot multiply `ndarray::ArrayBase<OwnedRepr<<A as ndarray_linalg::Scalar>::Real>, Dim<[usize; 2]>>` by `ndarray::ArrayBase<OwnedRepr<A>, Dim<[usize; 2]>>`
  --> numrs/src/linalg.rs:58:24
   |
58 |             let x1 = s * u;
   |                        ^ no implementation for `ndarray::ArrayBase<OwnedRepr<<A as ndarray_linalg::Scalar>::Real>, Dim<[usize; 2]>> * ndarray::ArrayBase<OwnedRepr<A>, Dim<[usize; 2]>>`
   |
   = help: the trait `Mul<ndarray::ArrayBase<OwnedRepr<A>, Dim<[usize; 2]>>>` is not implemented for `ndarray::ArrayBase<OwnedRepr<<A as ndarray_linalg::Scalar>::Real>, Dim<[usize; 2]>>`
help: consider extending the `where` bound, but there might be an alternative better way to express this requirement
   |
38 |     S: Data<Elem = A>, ndarray::ArrayBase<OwnedRepr<<A as ndarray_linalg::Scalar>::Real>, Dim<[usize; 2]>>: Mul<ndarray::ArrayBase<OwnedRepr<A>, Dim<[usize; 2]>>>

My understanding this is because I am trying to multiple Scalar::Real with a Scalar, but I am not sure how this should be fixed.

  1. https://numpy.org/doc/stable/reference/generated/numpy.linalg.pinv.html
  2. https://www.mathworks.com/help/matlab/ref/pinv.html
Ngôn ngữ chính
Rust
Star
452
Fork
95
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Chuẩn bị môi trường

Chúng tôi chưa kiểm tra các tệp thiết lập môi trường của dự án này. Hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của rust-ndarray/ndarray-linalg

Tất cả issue của rust-ndarray/ndarray-linalg

Issue tương tự

Thêm issue về Rust

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.