use super::super::jacobi::LinalgElement;
use super::super::{CpuClient, CpuRuntime};
use super::svd::svd_decompose_impl;
use crate::algorithm::linalg::impl_generic::{resolve_rcond, svd_cutoff};
use crate::algorithm::linalg::{linalg_demote, linalg_promote, validate_matrix_2d};
use crate::dtype::{DType, Element};
use crate::error::{Error, Result};
use crate::ops::{BinaryOps, MatmulOps};
use crate::runtime::RuntimeClient;
use crate::tensor::Tensor;
pub fn pinverse_impl(
client: &CpuClient,
a: &Tensor<CpuRuntime>,
rcond: Option<f64>,
) -> Result<Tensor<CpuRuntime>> {
if !a.dtype().is_float() {
return Err(Error::UnsupportedDType {
dtype: a.dtype(),
op: "pinverse",
});
}
let (a, original_dtype) = linalg_promote(client, a)?;
let (m, n) = validate_matrix_2d(a.shape())?;
let rcond = resolve_rcond(rcond, m, n, a.dtype())?;
if m == 0 || n == 0 {
let empty = Tensor::<CpuRuntime>::zeros(&[n, m], a.dtype(), client.device())?;
return linalg_demote(client, empty, original_dtype);
}
let svd = svd_decompose_impl(client, &a)?;
let (s_inv, _rank) = svd_cutoff(client, &svd.s, rcond)?;
let v = svd.vt.transpose(0, 1)?.contiguous()?;
let ut = svd.u.transpose(0, 1)?.contiguous()?;
let v_sinv = BinaryOps::mul(client, &v, &s_inv)?;
let pinv = MatmulOps::matmul(client, &v_sinv, &ut)?;
linalg_demote(client, pinv, original_dtype)
}
pub fn cond_impl(client: &CpuClient, a: &Tensor<CpuRuntime>) -> Result<Tensor<CpuRuntime>> {
if !a.dtype().is_float() {
return Err(Error::UnsupportedDType {
dtype: a.dtype(),
op: "cond",
});
}
let (a, original_dtype) = linalg_promote(client, a)?;
let (m, n) = validate_matrix_2d(a.shape())?;
let result = match a.dtype() {
DType::F32 => cond_typed::<f32>(client, &a, m, n),
DType::F64 => cond_typed::<f64>(client, &a, m, n),
dtype => Err(Error::UnsupportedDType { dtype, op: "cond" }),
}?;
linalg_demote(client, result, original_dtype)
}
fn cond_typed<T: Element + LinalgElement>(
client: &CpuClient,
a: &Tensor<CpuRuntime>,
m: usize,
n: usize,
) -> Result<Tensor<CpuRuntime>> {
let device = client.device();
if m == 0 || n == 0 {
return Tensor::<CpuRuntime>::from_slice(&[T::from_f64(f64::INFINITY)], &[], device);
}
let svd = svd_decompose_impl(client, a)?;
let s_data: Vec<T> = svd.s.to_vec();
if s_data.is_empty() {
return Tensor::<CpuRuntime>::from_slice(&[T::from_f64(f64::INFINITY)], &[], device);
}
let s_max = s_data[0].to_f64();
let s_min = s_data[s_data.len() - 1].to_f64();
let eps = T::epsilon_val();
let cond = if s_min.abs() < eps {
T::from_f64(f64::INFINITY)
} else {
T::from_f64(s_max / s_min)
};
Tensor::<CpuRuntime>::from_slice(&[cond], &[], device)
}
pub fn cov_impl(
client: &CpuClient,
a: &Tensor<CpuRuntime>,
ddof: Option<usize>,
) -> Result<Tensor<CpuRuntime>> {
if !a.dtype().is_float() {
return Err(Error::UnsupportedDType {
dtype: a.dtype(),
op: "cov",
});
}
let (a, original_dtype) = linalg_promote(client, a)?;
let (n_samples, n_features) = validate_matrix_2d(a.shape())?;
let result = match a.dtype() {
DType::F32 => cov_typed::<f32>(client, &a, n_samples, n_features, ddof),
DType::F64 => cov_typed::<f64>(client, &a, n_samples, n_features, ddof),
dtype => Err(Error::UnsupportedDType { dtype, op: "cov" }),
}?;
linalg_demote(client, result, original_dtype)
}
fn cov_typed<T: Element + LinalgElement>(
client: &CpuClient,
a: &Tensor<CpuRuntime>,
n_samples: usize,
n_features: usize,
ddof: Option<usize>,
) -> Result<Tensor<CpuRuntime>> {
let device = client.device();
let ddof = ddof.unwrap_or(1);
if n_samples <= ddof {
return Err(Error::Internal(format!(
"cov: n_samples ({}) must be greater than ddof ({})",
n_samples, ddof
)));
}
if n_features == 0 {
return Tensor::<CpuRuntime>::from_slice::<T>(&[], &[0, 0], device);
}
let a_data: Vec<T> = a.to_vec();
let n_f64 = n_samples as f64;
let mut means: Vec<T> = vec![T::zero(); n_features];
for j in 0..n_features {
let mut sum = T::zero();
for i in 0..n_samples {
sum = sum + a_data[i * n_features + j];
}
means[j] = T::from_f64(sum.to_f64() / n_f64);
}
let divisor = (n_samples - ddof) as f64;
let mut cov: Vec<T> = vec![T::zero(); n_features * n_features];
for i in 0..n_features {
for j in i..n_features {
let mut sum = T::zero();
for k in 0..n_samples {
let xi = a_data[k * n_features + i] - means[i];
let xj = a_data[k * n_features + j] - means[j];
sum = sum + xi * xj;
}
let cov_val = T::from_f64(sum.to_f64() / divisor);
cov[i * n_features + j] = cov_val;
cov[j * n_features + i] = cov_val; }
}
Tensor::<CpuRuntime>::from_slice(&cov, &[n_features, n_features], device)
}
pub fn corrcoef_impl(client: &CpuClient, a: &Tensor<CpuRuntime>) -> Result<Tensor<CpuRuntime>> {
if !a.dtype().is_float() {
return Err(Error::UnsupportedDType {
dtype: a.dtype(),
op: "corrcoef",
});
}
let (a, original_dtype) = linalg_promote(client, a)?;
let (n_samples, n_features) = validate_matrix_2d(a.shape())?;
let result = match a.dtype() {
DType::F32 => corrcoef_typed::<f32>(client, &a, n_samples, n_features),
DType::F64 => corrcoef_typed::<f64>(client, &a, n_samples, n_features),
dtype => Err(Error::UnsupportedDType {
dtype,
op: "corrcoef",
}),
}?;
linalg_demote(client, result, original_dtype)
}
fn corrcoef_typed<T: Element + LinalgElement>(
client: &CpuClient,
a: &Tensor<CpuRuntime>,
n_samples: usize,
n_features: usize,
) -> Result<Tensor<CpuRuntime>> {
let device = client.device();
if n_samples <= 1 {
return Err(Error::Internal(format!(
"corrcoef: n_samples ({}) must be greater than 1",
n_samples
)));
}
if n_features == 0 {
return Tensor::<CpuRuntime>::from_slice::<T>(&[], &[0, 0], device);
}
let cov_tensor = cov_typed::<T>(client, a, n_samples, n_features, Some(1))?;
let cov_data: Vec<T> = cov_tensor.to_vec();
let mut stds: Vec<f64> = vec![0.0; n_features];
for i in 0..n_features {
let var = cov_data[i * n_features + i].to_f64();
stds[i] = if var > 0.0 { var.sqrt() } else { 0.0 };
}
let mut corr: Vec<T> = vec![T::zero(); n_features * n_features];
for i in 0..n_features {
for j in 0..n_features {
if i == j {
corr[i * n_features + j] = if stds[i] > 0.0 { T::one() } else { T::zero() };
} else {
let std_prod = stds[i] * stds[j];
let corr_val = if std_prod > 0.0 {
T::from_f64(cov_data[i * n_features + j].to_f64() / std_prod)
} else {
T::zero() };
corr[i * n_features + j] = corr_val;
}
}
}
Tensor::<CpuRuntime>::from_slice(&corr, &[n_features, n_features], device)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::runtime::cpu::CpuDevice;
fn pinv_f64(data: &[f64], shape: &[usize], rcond: Option<f64>) -> (Vec<usize>, Vec<f64>) {
let device = CpuDevice::new();
let client = CpuClient::new(device.clone());
let a = Tensor::<CpuRuntime>::from_slice(data, shape, &device).unwrap();
let p = pinverse_impl(&client, &a, rcond).unwrap();
(p.shape().to_vec(), p.to_vec::<f64>())
}
fn assert_close(got: &[f64], want: &[f64], tol: f64) {
assert_eq!(got.len(), want.len());
for (i, (g, w)) in got.iter().zip(want).enumerate() {
assert!((g - w).abs() <= tol, "element {i}: got {g}, want {w}");
}
}
#[test]
fn full_rank_square_matches_inverse() {
let (shape, p) = pinv_f64(&[4.0, 7.0, 2.0, 6.0], &[2, 2], None);
assert_eq!(shape, vec![2, 2]);
assert_close(&p, &[0.6, -0.7, -0.2, 0.4], 1e-12);
}
#[test]
fn rank_deficient_drops_the_null_direction() {
let (shape, p) = pinv_f64(&[1.0, 2.0, 2.0, 4.0], &[2, 2], None);
assert_eq!(shape, vec![2, 2]);
assert_close(&p, &[0.04, 0.08, 0.08, 0.16], 1e-12);
}
#[test]
fn rcond_sets_the_cutoff_relative_to_max_singular_value() {
let a = [1.0, 0.0, 0.0, 1e-8, 0.0, 0.0];
let (shape, p) = pinv_f64(&a, &[3, 2], None);
assert_eq!(shape, vec![2, 3]);
assert_close(&p, &[1.0, 0.0, 0.0, 0.0, 1e8, 0.0], 1e-4);
let (_, p) = pinv_f64(&a, &[3, 2], Some(1e-4));
assert_close(&p, &[1.0, 0.0, 0.0, 0.0, 0.0, 0.0], 1e-12);
let scaled: Vec<f64> = a.iter().map(|v| v * 1000.0).collect();
let (_, p) = pinv_f64(&scaled, &[3, 2], Some(1e-4));
assert_close(&p, &[1e-3, 0.0, 0.0, 0.0, 0.0, 0.0], 1e-15);
}
#[test]
fn all_zero_matrix_gives_all_zero_pinverse() {
let (shape, p) = pinv_f64(&[0.0; 6], &[2, 3], None);
assert_eq!(shape, vec![3, 2]);
assert_eq!(p, vec![0.0; 6]);
assert!(p.iter().all(|v| v.is_finite()));
}
#[test]
fn f32_rank_deficient_and_zero() {
let device = CpuDevice::new();
let client = CpuClient::new(device.clone());
let a =
Tensor::<CpuRuntime>::from_slice(&[1.0f32, 2.0, 2.0, 4.0], &[2, 2], &device).unwrap();
let p = pinverse_impl(&client, &a, None).unwrap();
assert_eq!(p.dtype(), DType::F32);
let want = [0.04f32, 0.08, 0.08, 0.16];
for (g, w) in p.to_vec::<f32>().iter().zip(want) {
assert!((g - w).abs() <= 1e-5, "got {g}, want {w}");
}
let z = Tensor::<CpuRuntime>::zeros(&[3, 2], DType::F32, &device).unwrap();
let p = pinverse_impl(&client, &z, None).unwrap();
assert_eq!(p.shape(), &[2, 3]);
assert_eq!(p.to_vec::<f32>(), vec![0.0f32; 6]);
}
#[test]
fn empty_matrix_gives_transposed_empty_shape() {
let (shape, p) = pinv_f64(&[], &[0, 3], None);
assert_eq!(shape, vec![3, 0]);
assert!(p.is_empty());
}
#[test]
fn batched_input_is_rejected() {
let device = CpuDevice::new();
let client = CpuClient::new(device.clone());
let a = Tensor::<CpuRuntime>::zeros(&[2, 2, 2], DType::F64, &device).unwrap();
assert!(pinverse_impl(&client, &a, None).is_err());
}
}