Skip to content

Commit a20f274

Browse files
committed
Split eigenvalue test and eigenvector tests
1 parent 0ba1cb2 commit a20f274

1 file changed

Lines changed: 102 additions & 103 deletions

File tree

ndarray-linalg/tests/eig.rs

Lines changed: 102 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -79,112 +79,111 @@ fn fgeev() {
7979
}
8080
}
8181

82-
#[test]
83-
fn zgeev() {
84-
// https://software.intel.com/sites/products/documentation/doclib/mkl_sa/11/mkl_lapack_examples/zgeev_ex.f.htm
85-
let a: Array2<c64> = arr2(&[
86-
[
87-
c64::new(-3.84, 2.25),
88-
c64::new(-8.94, -4.75),
89-
c64::new(8.95, -6.53),
90-
c64::new(-9.87, 4.82),
91-
],
92-
[
93-
c64::new(-0.66, 0.83),
94-
c64::new(-4.40, -3.82),
95-
c64::new(-3.50, -4.26),
96-
c64::new(-3.15, 7.36),
97-
],
98-
[
99-
c64::new(-3.99, -4.73),
100-
c64::new(-5.88, -6.60),
101-
c64::new(-3.36, -0.40),
102-
c64::new(-0.75, 5.23),
103-
],
104-
[
105-
c64::new(7.74, 4.18),
106-
c64::new(3.66, -7.53),
107-
c64::new(2.58, 3.60),
108-
c64::new(4.59, 5.41),
109-
],
110-
]);
111-
let (e, vecs): (Array1<_>, Array2<_>) = (&a).eig().unwrap();
112-
assert_close_l2!(
113-
&e,
114-
&arr1(&[
115-
c64::new(-9.43, -12.98),
116-
c64::new(-3.44, 12.69),
117-
c64::new(0.11, -3.40),
118-
c64::new(5.76, 7.13)
119-
]),
120-
1.0e-3
121-
);
82+
// https://software.intel.com/sites/products/documentation/doclib/mkl_sa/11/mkl_lapack_examples/zgeev_ex.f.htm
83+
macro_rules! impl_test_complex {
84+
($complex:ty) => {
85+
paste::item! {
86+
fn [<$complex _matrix>]() -> Array2<$complex> {
87+
let mut a = Array2::zeros((4, 4));
12288

123-
/*
124-
let answer = &arr2(&[[c64::new( 0.43, 0.33), c64::new( 0.83, 0.00), c64::new( 0.60, 0.00), c64::new( -0.31, 0.03)],
125-
[c64::new( 0.51, -0.03), c64::new( 0.08, -0.25), c64::new( -0.40, -0.20), c64::new( 0.04, 0.34)],
126-
[c64::new( 0.62, 0.00), c64::new( -0.25, 0.28), c64::new( -0.09, -0.48), c64::new( 0.36, 0.06)],
127-
[c64::new( -0.23, 0.11), c64::new( -0.10, -0.32), c64::new( -0.43, 0.13), c64::new( 0.81, 0.00)]]);
128-
*/
89+
a[[0, 0]] = $complex::new(-3.84, 2.25);
90+
a[[0, 1]] = $complex::new(-8.94, -4.75);
91+
a[[0, 2]] = $complex::new(8.95, -6.53);
92+
a[[0, 3]] = $complex::new(-9.87, 4.82);
12993

130-
for (i, v) in vecs.axis_iter(Axis(1)).enumerate() {
131-
let av = a.dot(&v);
132-
let ev = v.mapv(|f| e[i] * f);
133-
assert_close_l2!(&av, &ev, 1.0e-7);
134-
}
135-
}
94+
a[[1, 0]] = $complex::new(-0.66, 0.83);
95+
a[[1, 1]] = $complex::new(-4.40, -3.82);
96+
a[[1, 2]] = $complex::new(-3.50, -4.26);
97+
a[[1, 3]] = $complex::new(-3.15, 7.36);
13698

137-
#[test]
138-
fn cgeev() {
139-
// https://software.intel.com/sites/products/documentation/doclib/mkl_sa/11/mkl_lapack_examples/zgeev_ex.f.htm
140-
let a: Array2<c32> = arr2(&[
141-
[
142-
c32::new(-3.84, 2.25),
143-
c32::new(-8.94, -4.75),
144-
c32::new(8.95, -6.53),
145-
c32::new(-9.87, 4.82),
146-
],
147-
[
148-
c32::new(-0.66, 0.83),
149-
c32::new(-4.40, -3.82),
150-
c32::new(-3.50, -4.26),
151-
c32::new(-3.15, 7.36),
152-
],
153-
[
154-
c32::new(-3.99, -4.73),
155-
c32::new(-5.88, -6.60),
156-
c32::new(-3.36, -0.40),
157-
c32::new(-0.75, 5.23),
158-
],
159-
[
160-
c32::new(7.74, 4.18),
161-
c32::new(3.66, -7.53),
162-
c32::new(2.58, 3.60),
163-
c32::new(4.59, 5.41),
164-
],
165-
]);
166-
let (e, vecs): (Array1<_>, Array2<_>) = (&a).eig().unwrap();
167-
assert_close_l2!(
168-
&e,
169-
&arr1(&[
170-
c32::new(-9.43, -12.98),
171-
c32::new(-3.44, 12.69),
172-
c32::new(0.11, -3.40),
173-
c32::new(5.76, 7.13)
174-
]),
175-
1.0e-3
176-
);
99+
a[[2, 0]] = $complex::new(-3.99, -4.73);
100+
a[[2, 1]] = $complex::new(-5.88, -6.60);
101+
a[[2, 2]] = $complex::new(-3.36, -0.40);
102+
a[[2, 3]] = $complex::new(-0.75, 5.23);
177103

178-
/*
179-
let answer = &arr2(&[[c32::new( 0.43, 0.33), c32::new( 0.83, 0.00), c32::new( 0.60, 0.00), c32::new( -0.31, 0.03)],
180-
[c32::new( 0.51, -0.03), c32::new( 0.08, -0.25), c32::new( -0.40, -0.20), c32::new( 0.04, 0.34)],
181-
[c32::new( 0.62, 0.00), c32::new( -0.25, 0.28), c32::new( -0.09, -0.48), c32::new( 0.36, 0.06)],
182-
[c32::new( -0.23, 0.11), c32::new( -0.10, -0.32), c32::new( -0.43, 0.13), c32::new( 0.81, 0.00)]]);
183-
*/
104+
a[[3, 0]] = $complex::new(7.74, 4.18);
105+
a[[3, 1]] = $complex::new(3.66, -7.53);
106+
a[[3, 2]] = $complex::new(2.58, 3.60);
107+
a[[3, 3]] = $complex::new(4.59, 5.41);
184108

185-
for (i, v) in vecs.axis_iter(Axis(1)).enumerate() {
186-
let av = a.dot(&v);
187-
let ev = v.mapv(|f| e[i] * f);
188-
assert_close_l2!(&av, &ev, 1.0e-5);
189-
}
109+
a
110+
}
111+
112+
fn [<$complex _matrix_t>]() -> Array2<$complex> {
113+
let mut a = Array2::zeros((4, 4).f());
114+
115+
a[[0, 0]] = $complex::new(-3.84, 2.25);
116+
a[[0, 1]] = $complex::new(-8.94, -4.75);
117+
a[[0, 2]] = $complex::new(8.95, -6.53);
118+
a[[0, 3]] = $complex::new(-9.87, 4.82);
119+
120+
a[[1, 0]] = $complex::new(-0.66, 0.83);
121+
a[[1, 1]] = $complex::new(-4.40, -3.82);
122+
a[[1, 2]] = $complex::new(-3.50, -4.26);
123+
a[[1, 3]] = $complex::new(-3.15, 7.36);
124+
125+
a[[2, 0]] = $complex::new(-3.99, -4.73);
126+
a[[2, 1]] = $complex::new(-5.88, -6.60);
127+
a[[2, 2]] = $complex::new(-3.36, -0.40);
128+
a[[2, 3]] = $complex::new(-0.75, 5.23);
129+
130+
a[[3, 0]] = $complex::new(7.74, 4.18);
131+
a[[3, 1]] = $complex::new(3.66, -7.53);
132+
a[[3, 2]] = $complex::new(2.58, 3.60);
133+
a[[3, 3]] = $complex::new(4.59, 5.41);
134+
135+
a
136+
}
137+
138+
fn [<$complex _eigvals_answer>]() -> Array1<$complex> {
139+
array![
140+
$complex::new(-9.43, -12.98),
141+
$complex::new(-3.44, 12.69),
142+
$complex::new(0.11, -3.40),
143+
$complex::new(5.76, 7.13)
144+
]
145+
}
146+
147+
fn [<$complex _eigvector_answer>]() -> Array2<$complex> {
148+
array![
149+
[$complex::new( 0.4308565200776108 , 0.32681273781262143),$complex::new( 0.8256820507672813 , 0. ), $complex::new( 0.5983959785539453 , 0. ),$complex::new(-0.30543190348437826, 0.03333164861799901)],
150+
[$complex::new( 0.5087414602970965 ,-0.02883342170692809),$complex::new( 0.07502916788141115,-0.2487285045091665 ), $complex::new(-0.40047616275207687 ,-0.2014492227625603 ),$complex::new( 0.03978282815783273, 0.34450765221546126)],
151+
[$complex::new( 0.6198496527657755 , 0. ),$complex::new(-0.24575578997801528, 0.27887240221169646), $complex::new(-0.09008001907594984 ,-0.4752646215391732 ),$complex::new( 0.3583254365159844 , 0.06064506988524665)],
152+
[$complex::new(-0.22692824331926856, 0.11043927846403584),$complex::new(-0.10343406372814358,-0.3192014653632327 ), $complex::new(-0.43484029549540404 , 0.13372491785816037),$complex::new( 0.8082432893178352 , 0. )]
153+
]
154+
}
155+
156+
#[test]
157+
fn [<$complex _eigvals >]() {
158+
let a = [<$complex _matrix>]();
159+
let (e, _vecs) = a.eig().unwrap();
160+
assert_close_l2!(&e, &[<$complex _eigvals_answer>](), 1.0e-3);
161+
}
162+
163+
#[test]
164+
fn [<$complex _eigvals_t>]() {
165+
let a = [<$complex _matrix_t>]();
166+
let (e, _vecs) = a.eig().unwrap();
167+
assert_close_l2!(&e, &[<$complex _eigvals_answer >](), 1.0e-3);
168+
}
169+
170+
#[test]
171+
fn [<$complex _eigvector>]() {
172+
let a = [<$complex _matrix>]();
173+
let (_e, vecs) = a.eig().unwrap();
174+
assert_close_l2!(&vecs, &[<$complex _eigvector_answer >](), 1.0e-3);
175+
}
176+
177+
178+
#[test]
179+
fn [<$complex _eigvector_t>]() {
180+
let a = [<$complex _matrix_t>]();
181+
let (_e, vecs) = a.eig().unwrap();
182+
assert_close_l2!(&vecs, &[<$complex _eigvector_answer >](), 1.0e-3);
183+
}
184+
} // paste::item!
185+
};
190186
}
187+
188+
impl_test_complex!(c32);
189+
impl_test_complex!(c64);

0 commit comments

Comments
 (0)