Skip to content

Commit

Permalink
Merge c15e72a into acb646c
Browse files Browse the repository at this point in the history
  • Loading branch information
Expander committed Jan 27, 2024
2 parents acb646c + c15e72a commit 8e307fb
Show file tree
Hide file tree
Showing 6 changed files with 282 additions and 125 deletions.
9 changes: 6 additions & 3 deletions benches/li.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@ use criterion::*;

fn bench_real_li2(c: &mut Criterion) {
let mut group = c.benchmark_group("li2(x)");
group.bench_function("x=0.25", |b| b.iter(|| black_box(0.25_f64).li2()));
group.bench_function("x=0.25_f32", |b| b.iter(|| black_box(0.25_f32).li2()));
group.bench_function("x=0.25_f64", |b| b.iter(|| black_box(0.25_f64).li2()));
group.finish();
}


fn bench_complex_li2(c: &mut Criterion) {
let mut group = c.benchmark_group("li2(z)");
group.bench_function("z=0.25+0.25i", |b| b.iter(|| black_box(Complex::new(0.25_f64, 0.25_f64)).li2()));
group.bench_function("z=-0.7+0.7i" , |b| b.iter(|| black_box(Complex::new(-0.7_f64, 0.7_f64)).li2()));
group.bench_function("z=0.25_f32+0.25_f32i", |b| b.iter(|| black_box(Complex::new(0.25_f32, 0.25_f32)).li2()));
group.bench_function("z=-0.7_f32+0.7_f32i" , |b| b.iter(|| black_box(Complex::new(-0.7_f32, 0.7_f32)).li2()));
group.bench_function("z=0.25_f64+0.25_f64i", |b| b.iter(|| black_box(Complex::new(0.25_f64, 0.25_f64)).li2()));
group.bench_function("z=-0.7_f64+0.7_f64i" , |b| b.iter(|| black_box(Complex::new(-0.7_f64, 0.7_f64)).li2()));
group.finish();
}

Expand Down
16 changes: 10 additions & 6 deletions src/cln.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use num::complex::Complex;
use num::Float;

/// Provides an implementation of the complex logarithm `cln()` of a
/// number of type `T`, where the imaginary part of the logarithm is
Expand All @@ -11,21 +12,24 @@ pub trait CLn<T> {
fn cln(&self) -> T;
}

impl CLn<Complex<f64>> for Complex<f64> {
fn cln(&self) -> Complex<f64> {
impl<T: Float> CLn<Complex<T>> for Complex<T> {
fn cln(&self) -> Complex<T> {
let z = Complex::new(
self.re,
// convert -0.0 to 0.0
if self.im == 0.0 { 0.0 } else { self.im },
if self.im == T::zero() { T::zero() } else { self.im },
);
Complex::new(z.norm().ln(), z.arg())
}
}

#[test]
fn test_cln() {
let x = Complex::new(1.0, 0.0);
let x32 = Complex::new(1.0_f32, 0.0_f32);
let x64 = Complex::new(1.0_f64, 0.0_f64);

assert!((-x).cln() == Complex::new(0.0, std::f64::consts::PI));
assert!((-x).ln() == Complex::new(0.0, -std::f64::consts::PI));
assert!(((-x32).cln() - Complex::new(0.0_f32, std::f32::consts::PI)).norm() < 4.0_f32*std::f32::EPSILON);
assert!(((-x32).ln() - Complex::new(0.0_f32, -std::f32::consts::PI)).norm() < 4.0_f32*std::f32::EPSILON);
assert!((-x64).cln() == Complex::new(0.0_f64, std::f64::consts::PI));
assert!((-x64).ln() == Complex::new(0.0_f64, -std::f64::consts::PI));
}
Loading

0 comments on commit 8e307fb

Please sign in to comment.