faer calculates the eigenvalues of a nontrivial adjacency matrix as `0` and `+/- inf`
ariasanovsky opened this issue · 8 comments
ariasanovsky commented
Describe the bug
The Vec<c64>
output of eigenvalues()
for the adjacency matrix of the following
Note: the path graph on
To Reproduce
/* use the smaller unit test linked above
#[test]
fn this_other_tree_has_correct_maximum_eigenvalue() {
let edges = [(3, 2), (6, 1), (7, 4), (7, 6), (8, 5), (9, 4), (11, 2), (12, 2), (13, 2), (15, 6), (16, 2), (16, 4), (17, 8), (18, 0), (18, 8), (18, 2), (19, 6), (19, 10), (19, 14)];
let mut a = faer::Mat::zeros(20, 20);
for (v, u) in edges.iter() {
a[(*v, *u)] = 1.0;
a[(*u, *v)] = 1.0;
}
let eigs_complex: Vec<faer::complex_native::c64> = a.eigenvalues();
println!("{eigs_complex:?}");
let eigs_real = eigs_complex
.iter()
.map(|e| e.re)
.collect::<Vec<_>>();
println!("{eigs_real:?}");
let lambda_1 = *eigs_real
.iter()
.max_by(|a, b| a.partial_cmp(&b).unwrap())
.unwrap();
let correct_lamba_1 = 2.6148611139728866;
assert!(
(lambda_1 - correct_lamba_1).abs() < 1e-10,
"lambda_1 = {lambda_1}, correct_lamba_1 = {correct_lamba_1}",
);
}
*/
Expected behavior
name: graphenv
# channels:
# - defaults
# dependencies:
# - python
# - networkx
# - matplotlib
# - imageio
# - msgpack-python
# - scipy
import networkx as nx
import numpy as np
import matplotlib.pyplot as plt
# Create the graph
edges = [[3, 2], [6, 1], [7, 4], [7, 6], [8, 5], [9, 4], [11, 2], [12, 2], [13, 2], [15, 6], [16, 2], [16, 4], [17, 8], [18, 0], [18, 8], [18, 2], [19, 6], [19, 10], [19, 14]]
graph = nx.Graph()
graph.add_edges_from(edges)
# Get the adjacency matrix eigenvalues
adjacency_matrix = nx.adjacency_matrix(graph).todense()
eigenvalues = np.linalg.eigvals(adjacency_matrix)
# Print the eigenvalues in decreasing order
print("Eigenvalues in decreasing order:")
for eigenvalue in sorted(eigenvalues, reverse=True):
print(eigenvalue)
# Plot the graph
nx.draw(graph, with_labels=True)
plt.show()
ariasanovsky commented
Smaller bug witness ($N = 15$ )
let edges = [[2, 0], [6, 4], [7, 0], [8, 4], [9, 8], [10, 0], [11, 0], [12, 4], [13, 5], [13, 11], [13, 3], [14, 1], [14, 3], [14, 4]]
// ...
let correct_lambda_1 = 2.2950716903444595;
ariasanovsky commented
Smaller bug witness ($N = 8$ )
let edges = [[1, 0], [3, 0], [5, 2], [5, 0], [6, 0], [6, 4], [7, 6]]
// ...
let correct_lambda_1 = 2.2215832939527633;
ariasanovsky commented
Smaller bug witness ($N = 4$ )
let edges = [[1, 0], [2, 0], [3, 0]];
// [0.0 + 0.0 * I, -inf + 0.0 * I, 0.0 + 0.0 * I, 0.0 + 0.0 * I]
// [0.0, -inf, 0.0, 0.0]
ariasanovsky commented
Smaller bug witness ($N = 3$ )
let edges = [[1, 0], [0, 2]];
/*
[
[0.0, 1.0, 1.0],
[1.0, 0.0, 0.0],
[1.0, 0.0, 0.0],
]
[0.0 + 0.0 * I, -inf + 0.0 * I, 0.0 + 0.0 * I]
[0.0, -inf, 0.0]
*/
However, after permutation, the values are correct
let edges = [[1, 0], [2, 1]];
/*
[
[0.0, 1.0, 0.0],
[1.0, 0.0, 1.0],
[0.0, 1.0, 0.0],
]
[-1.414213562373094 + 0.0 * I, 5.160802341031001e-17 + 0.0 * I, 1.414213562373095 + 0.0 * I]
[-1.414213562373094, 5.160802341031001e-17, 1.414213562373095]
*/
ariasanovsky commented
Unit test
#[test]
fn path_on_three_vertices_has_correct_eigenvalues_regardless_of_permutation() {
let all_edges = [
[(1, 0), (2, 0)],
[(2, 1), (1, 0)],
[(2, 1), (2, 0)],
];
let correct_eigenvalues = [2.0f64.sqrt(), 0., -2.0f64.sqrt()];
for edges in all_edges {
let mut a = faer::Mat::zeros(3, 3);
for (v, u) in edges.iter() {
a[(*v, *u)] = 1.0;
a[(*u, *v)] = 1.0;
}
let eigs_complex: Vec<faer::complex_native::c64> = a.eigenvalues();
let mut eigs_real = eigs_complex
.iter()
.map(|e| e.re)
.collect::<Vec<_>>();
eigs_real.sort_by(|a, b| b.partial_cmp(a).unwrap());
let error = correct_eigenvalues.iter().zip(eigs_real.iter()).map(|(a, b)| (a - b).powi(2)).sum::<f64>();
assert!(
error < 1e-10,
"error = {error}\nedges = {edges:?}\na = {a:?}\neigs = {eigs_complex:?}\neigs = {eigs_real:?}",
);
}
}
ariasanovsky commented
Patching with
const ZERO: f64 = 0.0000001;
let mut a = Mat::from_fn(N, N, |_, _| ZERO);
solve my problem.
If this is user error, feel free to close.
sarah-quinones commented
it's not a user error. i forgot i should be detecting zeros at the head/tail of the matrix during the eigenvalue computation xP
sarah-quinones commented
fixed in 0.15.0
thanks a bunch!