Skip to content

Commit 5f70fcc

Browse files
committed
Cleanup some tests
1 parent c57e330 commit 5f70fcc

File tree

2 files changed

+79
-95
lines changed

2 files changed

+79
-95
lines changed

src/test.rs

Lines changed: 51 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,98 +1,91 @@
11
#[cfg(test)]
22
mod test {
3-
use crate::algebra::ClBasis;
3+
use crate::algebra::{ClBasis, NonDegenerate};
4+
use crate::types::{DivRing, FromComplex, IntoComplex64};
45
use core::f64;
6+
use std::fmt::Debug;
57
use std::hint::black_box;
68
use std::time;
79

810
use crate::algebra::ClAlgebra;
911
use crate::declare_algebra;
1012
use crate::{Multivector, SparseMultivector};
1113
use ndarray::Array2;
12-
use num::complex::Complex64;
14+
use num::complex::{Complex32, Complex64};
1315
use num::One;
1416
use num::Zero;
1517

1618
#[test]
1719
fn basis_test() {
1820
declare_algebra!(Cl44, [+,+,+,+,-,-,-,-], ["e1", "e2", "e3", "e4", "g1", "g2", "g3", "g4"]);
21+
// Check unpacking
1922
let [e1, e2, e3, e4, g1, g2, g3, g4] = Cl44::basis::<f64>();
2023
println!("{}", e1 + e2 + e3 + e4 + g1 + g2 + g3 + g4);
2124

25+
// Check basis multiplication
26+
let sig = Cl44::signaturef();
2227
let b = Cl44::basis_sparse::<Complex64>();
23-
println!("{}", &b[1] * &b[2]);
2428
for i in 0..b.len() {
29+
assert_eq!(
30+
&b[i] * &b[i],
31+
SparseMultivector::<Complex64, Cl44>::from_scalar(sig[i].into())
32+
);
2533
for j in 0..i {
2634
assert_eq!(&b[i] * &b[j], -(&b[j] * &b[i]))
2735
}
28-
//println!("{}", b[i].to_dense().fft().unwrap());
29-
}
30-
31-
declare_algebra!(Cl4, [+,+,+,+], ["a", "b", "c", "d"]);
32-
let e = Cl4::basis::<f64>();
33-
for ei in e {
34-
println!("{}", ei.fft());
3536
}
3637
}
3738

3839
#[test]
3940
fn fft_test() {
40-
declare_algebra!(Oct, [-,-,-,-,-,-], ["e1", "e2", "e3", "e4", "e5", "e6"]);
41-
// Associative map of real octonions
42-
let e = Oct::basis::<f64>();
43-
for i in 0..e.len() {
44-
let fei = e[i].fft();
45-
// Check the the square of fft square is negative identity
46-
assert_eq!(
47-
(&fei * &fei).into_array2(),
48-
Array2::from_diag_elem(8, -Complex64::one())
49-
);
50-
for j in 0..i {
51-
let eij = &fei * &e[j].fft();
52-
let eji = &e[j].fft() * &fei;
53-
// Check anticommutativity
54-
assert_eq!(eij, -&eji);
55-
56-
let prod = eij.ifft();
57-
// Check that naive and fft products agree
58-
assert_eq!(prod, e[i].naive_wedge_impl(&e[j]));
59-
// And that the fft product is correct at all
60-
assert!(prod.get_by_idx((1 << i) | (1 << j)).is_one());
61-
assert!(prod.set_by_idx((1 << i) | (1 << j), 0.).is_zero());
62-
}
63-
}
64-
// Associative map of complex octonions
65-
let e = Oct::basis::<Complex64>();
66-
for i in 0..e.len() {
67-
let fei = e[i].fft();
68-
// Check the the square of fft square is negative identity
69-
assert_eq!(
70-
(&fei * &fei).into_array2(),
71-
Array2::from_diag_elem(8, -Complex64::one())
72-
);
73-
for j in 0..i {
74-
let eij = &fei * &e[j].fft();
75-
let eji = &e[j].fft() * &fei;
76-
// Check anticommutativity
77-
assert_eq!(eij, -&eji);
78-
79-
let prod = eij.ifft();
80-
// Check that naive and fft products agree
81-
assert_eq!(prod, e[i].naive_wedge_impl(&e[j]));
82-
// And that the fft product is correct at all
83-
assert!(prod.get_by_idx((1 << i) | (1 << j)).is_one());
84-
assert!(prod
85-
.set_by_idx((1 << i) | (1 << j), Complex64::zero())
86-
.is_zero());
41+
fn fft_test_case<
42+
const DIM: usize,
43+
const REPR_DIM: usize,
44+
T: DivRing + Clone + IntoComplex64 + FromComplex + Debug,
45+
A: ClAlgebra + ClBasis<DIM> + NonDegenerate + Debug,
46+
>() {
47+
let e = A::basis::<T>();
48+
for i in 0..e.len() {
49+
let fei = e[i].fft();
50+
// Check the the square of fft square is negative identity
51+
assert_eq!(
52+
(&fei * &fei).into_array2(),
53+
Array2::from_diag_elem(REPR_DIM, Complex64::one() * A::signaturef()[i])
54+
);
55+
for j in 0..i {
56+
let eij = &fei * &e[j].fft();
57+
let eji = &e[j].fft() * &fei;
58+
// Check anticommutativity
59+
assert_eq!(eij, -&eji);
60+
61+
let prod = eij.ifft();
62+
// Check that naive and fft products agree
63+
assert_eq!(prod, e[i].naive_wedge_impl(&e[j]));
64+
// And that the fft product is correct at all
65+
assert!(prod.get_by_idx((1 << i) | (1 << j)).is_one());
66+
assert!(prod.set_by_idx((1 << i) | (1 << j), T::zero()).is_zero());
67+
}
8768
}
8869
}
70+
71+
declare_algebra!(Oct, [-,-,-,-,-,-]);
72+
fft_test_case::<6, 8, f32, Oct>();
73+
fft_test_case::<6, 8, f64, Oct>();
74+
fft_test_case::<6, 8, Complex32, Oct>();
75+
fft_test_case::<6, 8, Complex64, Oct>();
76+
declare_algebra!(ClOdd, [-,-,-,+,-,+,-]);
77+
fft_test_case::<7, 16, f32, ClOdd>();
78+
fft_test_case::<7, 16, f64, ClOdd>();
79+
fft_test_case::<7, 16, Complex32, ClOdd>();
80+
fft_test_case::<7, 16, Complex64, ClOdd>();
8981
}
9082

9183
#[test]
9284
fn ops_test() {
93-
declare_algebra!(Oct, [-,-,-,-,-,-], ["e1", "e2", "e3", "e4", "e5", "e6"]);
85+
declare_algebra!(Oct, [-,-,-,-,-,-]);
9486
type MV = SparseMultivector<f64, Oct>;
9587

88+
// Check values of blade exponents
9689
let mut theta = 0.0;
9790
while theta < f64::consts::TAU {
9891
let b = MV::zero().set_by_idx(0b11, theta);

tests/unit.rs

Lines changed: 28 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use ndarray::Array2;
77
//use ndarray_linalg::Determinant;
88
use num::complex::{Complex32, Complex64, ComplexFloat};
99
use num::{One, Zero};
10+
use tclifford::algebra::NonDegenerate;
1011
use tclifford::pga::PGAMV;
1112
use tclifford::types::DivRing;
1213
use tclifford::types::WedgeProduct;
@@ -754,31 +755,36 @@ fn trace_test() {
754755

755756
#[test]
756757
fn irrep_test() {
757-
declare_algebra!(Cl8, [+,+,+,+,+,+,+,+]);
758-
759-
let blade = random_unitary_blade::<f64, Cl8>(2);
758+
fn irrep_test_internal<const RDIM: usize, A: ClAlgebra + NonDegenerate>() {
759+
let blade = random_unitary_blade::<f64, A>(2);
760760

761-
let angle = PI / 10.;
762-
let rot: FFTRepr<Cl8> = (blade * angle).fft().exp();
761+
let angle = PI / 10.;
762+
let rot: FFTRepr<A> = (blade * angle).fft().exp();
763763

764-
let (even_spin_repr, odd_spin_repr) = rot.into_half_spin_repr();
765-
// Check that the even representation has the expected order (A^10 == -I)
766-
let mut actual = Array2::eye(8);
767-
for _ in 0..10 {
768-
actual = actual.dot(&even_spin_repr);
764+
let (even_spin_repr, odd_spin_repr) = rot.into_half_spin_repr();
765+
// Check that the even representation has the expected order (A^10 == -I)
766+
let mut actual = Array2::eye(RDIM);
767+
for _ in 0..10 {
768+
actual = actual.dot(&even_spin_repr);
769+
}
770+
assert!((actual - (-Array2::<Complex64>::eye(RDIM)))
771+
.iter()
772+
.all(|c| c.abs() < 1e-12));
773+
774+
// Check that the odd representation has the expected order (A^10 == -I)
775+
let mut actual = Array2::eye(RDIM);
776+
for _ in 0..10 {
777+
actual = actual.dot(&odd_spin_repr);
778+
}
779+
assert!((actual - (-Array2::<Complex64>::eye(RDIM)))
780+
.iter()
781+
.all(|c| c.abs() < 1e-12));
769782
}
770-
assert!((actual - (-Array2::<Complex64>::eye(8)))
771-
.iter()
772-
.all(|c| c.abs() < 1e-12));
773783

774-
// Check that the odd representation has the expected order (A^10 == -I)
775-
let mut actual = Array2::eye(8);
776-
for _ in 0..10 {
777-
actual = actual.dot(&odd_spin_repr);
778-
}
779-
assert!((actual - (-Array2::<Complex64>::eye(8)))
780-
.iter()
781-
.all(|c| c.abs() < 1e-12));
784+
declare_algebra!(Cl8, [+,+,+,+,+,+,+,+]);
785+
irrep_test_internal::<8, Cl8>();
786+
declare_algebra!(Cl10, [+,+,+,+,+,+,+,+,+,+]);
787+
irrep_test_internal::<16, Cl10>();
782788
}
783789

784790
/// Test for examples in README.md
@@ -877,8 +883,8 @@ mod benchmarks {
877883
fn fft_bench() {
878884
declare_algebra!(Cl8, [+,+,+,+,+,+,+,+]);
879885

886+
// Check overhead of FFTRepr compared to the raw clifft
880887
let b = random_mv_real::<Cl8>();
881-
882888
let ts = time::Instant::now();
883889
for _ in 0..10000 {
884890
let _ = black_box(
@@ -918,21 +924,6 @@ mod benchmarks {
918924
println!("mul {:?}", ts.elapsed());
919925
}
920926

921-
#[test]
922-
fn wfft_bench() {
923-
declare_algebra!(A, [+,+,+,+,+,-,0,0,0], ["e0","e1","e2","e3","e4","e5","n0","n1","n2"]);
924-
//type MV = Multivector<f64, A>;
925-
926-
let a = random_mv_real::<A>().fft();
927-
let b = random_mv_real::<A>().fft();
928-
929-
let ts = time::Instant::now();
930-
for _ in 0..10000 {
931-
let _ = black_box(&a * &b);
932-
}
933-
println!("mul: {:?}", ts.elapsed());
934-
}
935-
936927
#[test]
937928
fn low_dim_bench() {
938929
fn bench_mul<A: ClAlgebra>(count: u32) -> time::Duration {

0 commit comments

Comments
 (0)