Skip to content

Commit 26e73eb

Browse files
committed
Add hamming weight for rationals
1 parent c491b6c commit 26e73eb

2 files changed

Lines changed: 91 additions & 1 deletion

File tree

src/rational/mat_q/norm.rs

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use super::MatQ;
1313
use crate::{
1414
rational::Q,
15-
traits::{MatrixDimensions, MatrixGetSubmatrix},
15+
traits::{MatrixDimensions, MatrixGetEntry, MatrixGetSubmatrix},
1616
};
1717

1818
impl MatQ {
@@ -88,6 +88,33 @@ impl MatQ {
8888
}
8989
max_norm
9090
}
91+
92+
/// Outputs the hamming weight of `self`, i.e. it returns the number of
93+
/// non-zero entries in the matrix.
94+
///
95+
/// # Examples
96+
/// ```
97+
/// use qfall_math::rational::MatQ;
98+
/// use std::str::FromStr;
99+
///
100+
/// let mat = MatQ::from_str("[[2, 3/2],[2/3, 0]]").unwrap();
101+
///
102+
/// let hamming_weight = mat.hamming_weight();
103+
///
104+
/// assert_eq!(3, hamming_weight);
105+
/// ```
106+
pub fn hamming_weight(&self) -> i64 {
107+
let mut hamming_weight = 0;
108+
for row in 0..self.get_num_rows() {
109+
for col in 0..self.get_num_columns() {
110+
let entry = unsafe { self.get_entry_unchecked(row, col) };
111+
if !entry.is_zero() {
112+
hamming_weight += 1;
113+
}
114+
}
115+
}
116+
hamming_weight
117+
}
91118
}
92119

93120
#[cfg(test)]
@@ -125,3 +152,22 @@ mod test_matrix_norms {
125152
assert_eq!(Q::from(5), infty_norm);
126153
}
127154
}
155+
156+
#[cfg(test)]
157+
mod test_hamming_weight {
158+
use super::MatQ;
159+
use std::str::FromStr;
160+
161+
/// Ensures that the hamming weight is computed correctly.
162+
#[test]
163+
fn hamming_weight() {
164+
let mat0 = MatQ::new(10, 8);
165+
let mat1 = MatQ::from_str("[[-2/5, 3/4],[2, -5/2],[-2, 0]]").unwrap();
166+
167+
let hw0 = mat0.hamming_weight();
168+
let hw1 = mat1.hamming_weight();
169+
170+
assert_eq!(0, hw0);
171+
assert_eq!(5, hw1);
172+
}
173+
}

src/rational/poly_over_q/norm.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,31 @@ impl PolyOverQ {
6868
}
6969
res
7070
}
71+
72+
/// Outputs the hamming weight of `self`, i.e. it returns the number of
73+
/// non-zero coefficients in the polynomial.
74+
///
75+
/// # Examples
76+
/// ```
77+
/// use qfall_math::rational::PolyOverQ;
78+
/// use std::str::FromStr;
79+
///
80+
/// let poly = PolyOverQ::from_str("5 1 2/3 3/2 0 4").unwrap();
81+
///
82+
/// let hamming_weight = poly.hamming_weight();
83+
///
84+
/// assert_eq!(4, hamming_weight);
85+
/// ```
86+
pub fn hamming_weight(&self) -> i64 {
87+
let mut hamming_weight = 0;
88+
for i in 0..=self.get_degree() {
89+
let coeff = unsafe { self.get_coeff_unchecked(i) };
90+
if !coeff.is_zero() {
91+
hamming_weight += 1;
92+
}
93+
}
94+
hamming_weight
95+
}
7196
}
7297

7398
#[cfg(test)]
@@ -142,3 +167,22 @@ mod test_norm_infty {
142167
assert_eq!(poly_2.norm_infty(), Q::from(i64::MAX));
143168
}
144169
}
170+
171+
#[cfg(test)]
172+
mod test_hamming_weight {
173+
use super::PolyOverQ;
174+
use std::str::FromStr;
175+
176+
/// Ensures that the hamming weight is computed correctly.
177+
#[test]
178+
fn hamming_weight() {
179+
let poly0 = PolyOverQ::default();
180+
let poly1 = PolyOverQ::from_str("6 0 0 2/2 3/2 4 5/7").unwrap();
181+
182+
let hw0 = poly0.hamming_weight();
183+
let hw1 = poly1.hamming_weight();
184+
185+
assert_eq!(0, hw0);
186+
assert_eq!(4, hw1);
187+
}
188+
}

0 commit comments

Comments
 (0)