Skip to content

Commit

Permalink
fix!: Fixed comments on PR
Browse files Browse the repository at this point in the history
- Fixed result showing f64 instead of Complex<f64>
- Removed commented blocks in LinAlg trait with to-do comment
- Removed FPComplexMatrix added temporarily and used generics
to in FPMatrix to accomodate ComplexMatrix too
- Same as above for MutableMatrix
- Removed cgemm as dependency in matrixmultiply
in Cargo.toml
  • Loading branch information
soumyasen1809 committed Sep 27, 2024
1 parent b40f618 commit f277895
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 181 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ rand = { version = "0.8", features = ["small_rng"] }
rand_distr = "0.4"
order-stat = "0.1"
puruspe = "0.2"
matrixmultiply = { version = "0.3", features = ["threading", "cgemm"] }
matrixmultiply = { version = "0.3", features = ["threading"] }
peroxide-ad = "0.3"
peroxide-num = "0.1"
anyhow = "1.0"
Expand Down
37 changes: 15 additions & 22 deletions src/complex/matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::{
nearly_eq, tab, ConcatenateError, InnerProduct, LinearOp, MatrixProduct, Norm, Normed,
Shape, Vector,
},
traits::{fp::FPComplexMatrix, mutable::ComplexMutMatrix},
traits::{fp::FPMatrix, mutable::MutMatrix},
};

/// R-like complex matrix structure
Expand Down Expand Up @@ -375,9 +375,9 @@ impl ComplexMatrix {
/// );
/// println!("{}", a.spread()); // same as println!("{}", a);
/// // Result:
/// // c[0] c[1]
/// // r[0] 1 3
/// // r[1] 2 4
/// // c[0] c[1]
/// // r[0] 1+1i 3+3i
/// // r[1] 2+2i 4+4i
/// ```
pub fn spread(&self) -> String {
assert_eq!(self.row * self.col, self.data.len());
Expand Down Expand Up @@ -661,7 +661,7 @@ impl ComplexMatrix {
pub fn to_vec(&self) -> Vec<Vec<Complex<f64>>> {
let mut result = vec![vec![Complex::zero(); self.col]; self.row];
for i in 0..self.row {
result[i] = self.row(i); // ToDo: needs row implementation
result[i] = self.row(i);
}
result
}
Expand Down Expand Up @@ -1507,7 +1507,9 @@ impl IndexMut<(usize, usize)> for ComplexMatrix {
// Functional Programming Tools (Hand-written)
// =============================================================================

impl FPComplexMatrix for ComplexMatrix {
impl FPMatrix for ComplexMatrix {
type Scalar = Complex<f64>;

fn take_row(&self, n: usize) -> Self {
if n >= self.row {
return self.clone();
Expand Down Expand Up @@ -1595,7 +1597,7 @@ impl FPComplexMatrix for ComplexMatrix {
/// use peroxide::fuga::*;
/// use num_complex::Complex64;
/// use peroxide::complex::matrix::*;
/// use peroxide::traits::fp::FPComplexMatrix;
/// use peroxide::traits::fp::FPMatrix;
///
/// fn main() {
/// let x = complex_matrix(vec![Complex64::new(1f64, 1f64),
Expand Down Expand Up @@ -1641,7 +1643,7 @@ impl FPComplexMatrix for ComplexMatrix {
/// use peroxide::fuga::*;
/// use num_complex::Complex64;
/// use peroxide::complex::matrix::*;
/// use peroxide::traits::fp::FPComplexMatrix;
/// use peroxide::traits::fp::FPMatrix;
///
/// fn main() {
/// let x = complex_matrix(vec![Complex64::new(1f64, 1f64),
Expand Down Expand Up @@ -1767,20 +1769,9 @@ impl FPComplexMatrix for ComplexMatrix {
pub trait LinearAlgebra {
fn back_subs(&self, b: &Vec<Complex<f64>>) -> Vec<Complex<f64>>;
fn forward_subs(&self, b: &Vec<Complex<f64>>) -> Vec<Complex<f64>>;
// fn lu(&self) -> PQLU;
// fn waz(&self, d_form: Form) -> Option<WAZD>;
// fn qr(&self) -> QR;
// fn svd(&self) -> SVD;
// #[cfg(feature = "O3")]
// fn cholesky(&self, uplo: UPLO) -> Matrix;
// fn rref(&self) -> Matrix;
// fn det(&self) -> f64;
fn block(&self) -> (ComplexMatrix, ComplexMatrix, ComplexMatrix, ComplexMatrix);
// fn inv(&self) -> ComplexMatrix;
// fn pseudo_inv(&self) -> Matrix;
// fn solve(&self, b: &Vec<f64>, sk: SolveKind) -> Vec<f64>;
// fn solve_mat(&self, m: &Matrix, sk: SolveKind) -> Matrix;
fn is_symmetric(&self) -> bool;
// ToDo: Add other fn of this trait from src/structure/matrix.rs
}

impl LinearAlgebra for ComplexMatrix {
Expand Down Expand Up @@ -1892,7 +1883,9 @@ impl LinearAlgebra for ComplexMatrix {
}
}

impl ComplexMutMatrix for ComplexMatrix {
impl MutMatrix for ComplexMatrix {
type Scalar = Complex<f64>;

unsafe fn col_mut(&mut self, idx: usize) -> Vec<*mut Complex<f64>> {
assert!(idx < self.col, "Index out of range");
match self.shape {
Expand Down Expand Up @@ -1974,7 +1967,7 @@ pub unsafe fn swap_complex_vec_ptr(
/// use peroxide::fuga::*;
/// use num_complex::Complex64;
/// use peroxide::complex::matrix::*;
/// use peroxide::traits::fp::FPComplexMatrix;
/// use peroxide::traits::fp::FPMatrix;
///
/// fn main() {
/// let x1 = complex_matrix(vec![Complex64::new(1f64, 1f64)], 1, 1, Row);
Expand Down
35 changes: 22 additions & 13 deletions src/complex/vector.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use num_complex::Complex;
use crate::traits::fp::FPVector;
use crate::traits::math::{Vector, Normed, Norm, InnerProduct};
use crate::traits::math::{InnerProduct, Norm, Normed, Vector};
use crate::traits::sugar::VecOps;
use num_complex::Complex;

impl Vector for Complex<f64> {
type Scalar = Self;
Expand Down Expand Up @@ -31,7 +31,8 @@ impl Normed for Complex<f64> {

fn normalize(&self, kind: Norm) -> Self
where
Self: Sized {
Self: Sized,
{
let n = self.norm(kind);
self / n
}
Expand All @@ -48,26 +49,33 @@ impl FPVector for Vec<Complex<f64>> {

fn fmap<F>(&self, f: F) -> Self
where
F: Fn(Self::Scalar) -> Self::Scalar {
F: Fn(Self::Scalar) -> Self::Scalar,
{
self.iter().map(|&x| f(x)).collect()
}

fn zip_with<F>(&self, f: F, other: &Self) -> Self
where
F: Fn(Self::Scalar, Self::Scalar) -> Self::Scalar {
self.iter().zip(other.iter()).map(|(&x, &y)| f(x,y)).collect()
F: Fn(Self::Scalar, Self::Scalar) -> Self::Scalar,
{
self.iter()
.zip(other.iter())
.map(|(&x, &y)| f(x, y))
.collect()
}

fn reduce<F, T>(&self, init: T, f: F) -> Self::Scalar
where
F: Fn(Self::Scalar, Self::Scalar) -> Self::Scalar,
T: Into<Self::Scalar> {
self.iter().fold(init.into(), |x, &y| f(x,y))
F: Fn(Self::Scalar, Self::Scalar) -> Self::Scalar,
T: Into<Self::Scalar>,
{
self.iter().fold(init.into(), |x, &y| f(x, y))
}

fn filter<F>(&self, f: F) -> Self
where
F: Fn(Self::Scalar) -> bool {
F: Fn(Self::Scalar) -> bool,
{
self.iter().filter(|&x| f(*x)).cloned().collect()
}

Expand Down Expand Up @@ -110,13 +118,14 @@ impl Normed for Vec<Complex<f64>> {
fn norm(&self, kind: Norm) -> Self::UnsignedScalar {
match kind {
Norm::L1 => self.iter().map(|x| Complex::<f64>::norm(*x).abs()).sum(),
_ => unimplemented!()
_ => unimplemented!(),
}
}

fn normalize(&self, kind: Norm) -> Self
fn normalize(&self, _kind: Norm) -> Self
where
Self: Sized {
Self: Sized,
{
unimplemented!()
}
}
Expand Down
Loading

0 comments on commit f277895

Please sign in to comment.