Browse Source

Move complex stuff to a separate crate

master
Ivan Ukhov 9 years ago
parent
commit
b1c7f9f25c
  1. 7
      Cargo.toml
  2. 42
      src/complex.rs
  3. 27
      src/lib.rs

7
Cargo.toml

@ -3,11 +3,14 @@ name = "fft"
version = "0.0.1"
authors = ["Ivan Ukhov <ivan.ukhov@gmail.com>"]
license = "MIT"
repository = "https://github.com/hilbert-space/fft"
homepage = "https://github.com/hilbert-space/fft"
repository = "https://github.com/stainless-steel/fft"
homepage = "https://github.com/stainless-steel/fft"
description = """
The package provides an algorithm to compute the discrete Fourier transform and
its inverse."""
[dependencies]
complex = "*"
[dev-dependencies]
assert = "*"

42
src/complex.rs

@ -1,42 +0,0 @@
use std::ops::{Add, Mul, Sub};
/// A complex number.
#[allow(non_camel_case_types)]
#[derive(Clone, Copy, Debug)]
pub struct c64(pub f64, pub f64);
impl Add for c64 {
type Output = Self;
#[inline(always)]
fn add(self, rhs: c64) -> c64 {
c64(self.0 + rhs.0, self.1 + rhs.1)
}
}
impl Mul for c64 {
type Output = Self;
#[inline(always)]
fn mul(self, rhs: c64) -> c64 {
c64(self.0 * rhs.0 - self.1 * rhs.1, self.0 * rhs.1 + self.1 * rhs.0)
}
}
impl Mul<f64> for c64 {
type Output = Self;
#[inline(always)]
fn mul(self, rhs: f64) -> c64 {
c64(self.0 * rhs, self.1 * rhs)
}
}
impl Sub for c64 {
type Output = Self;
#[inline(always)]
fn sub(self, rhs: c64) -> c64 {
c64(self.0 - rhs.0, self.1 - rhs.1)
}
}

27
src/lib.rs

@ -1,7 +1,16 @@
use std::slice;
//! [Algorithm][1] to compute the [discrete Fourier transform][2] and its
//! inverse.
//!
//! [1]: https://en.wikipedia.org/wiki/Fast_Fourier_transform
//! [2]: https://en.wikipedia.org/wiki/Discrete_Fourier_transform
// The implementation is based on:
// http://www.librow.com/articles/article-10
mod complex;
pub use complex::c64;
extern crate complex;
use complex::c64;
use std::slice;
/// A means of obtaining a slice of mutable complex numbers.
pub trait AsMutComplex<'l> {
@ -125,15 +134,3 @@ fn scale(data: &mut [c64], n: usize) {
data[position] = data[position] * factor;
}
}
#[cfg(test)]
mod tests {
use c64;
use std::mem;
#[test]
fn size_of() {
assert_eq!(mem::size_of::<c64>(), 2 * mem::size_of::<f64>());
assert_eq!(mem::size_of::<[c64; 42]>(), 2 * mem::size_of::<[f64; 42]>());
}
}
Loading…
Cancel
Save