-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhadamard.py
More file actions
40 lines (30 loc) · 978 Bytes
/
hadamard.py
File metadata and controls
40 lines (30 loc) · 978 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# IMSL Lab - University of Notre Dame
# Author: Clemens JS Schaefer and Martin Schiemer
# Quantized training.
import math
import numpy as np
import scipy
def hadamard(n, dtype=int):
# from https://github.com/scipy/scipy/blob/v1.11.1/scipy/linalg/_special_matrices.py#L319-L373
if n < 1:
lg2 = 0
else:
lg2 = int(math.log(n, 2))
if 2 ** lg2 != n:
raise ValueError("n must be an positive integer, and n must be "
"a power of 2")
H = np.array([[1]], dtype=dtype)
# Sylvester's construction
for i in range(0, lg2):
H = np.vstack((np.hstack((H, H)), np.hstack((H, -H))))
return H
def biggest_power2_factor(n):
factors = []
for i in range(1, n + 1):
if n % i == 0:
if math.log(i, 2).is_integer():
factors.append(i)
return max(factors)
def make_hadamard(size_m):
biggest_pow2 = biggest_power2_factor(size_m)
return scipy.linalg.block_diag(*[hadamard(biggest_pow2)] * int(size_m / biggest_pow2))