-
Notifications
You must be signed in to change notification settings - Fork 0
/
kll-compression.py
108 lines (79 loc) · 2.48 KB
/
kll-compression.py
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/bin/python
import numpy as np
from numpy.linalg import svd
import givens
import itertools
import pickle, sys
from itertools import combinations
ANGS_TABLE = {}
LTABLE = {}
ANGS_TABLE_FILE = ".kll-angs-table-file"
LTABLE_FILE = ".kll-ltable-file"
def generate_data_lookup(NUMBER_OF_ANGLES = 10, MATRIX_SIZE = 5):
global ANGS_TABLE, LTABLE
ANGLES = np.arange(0,2*np.pi,2*np.pi/NUMBER_OF_ANGLES)
ANGS_TABLE = {}
print "generating angles table"
for ang in ANGLES:
ANGS_TABLE[ang] = np.identity(MATRIX_SIZE)
for i,j in combinations(xrange(MATRIX_SIZE),2):
ANGS_TABLE[ang] = np.dot(ANGS_TABLE[ang],givens.givens(i,j,ang,MATRIX_SIZE))
#print ang,ANGS_TABLE[ang]
LTABLE = {}
print "generating ltable"
number_of_entries = NUMBER_OF_ANGLES**(MATRIX_SIZE-1)
print "number of entries: %u"%(number_of_entries)
i = 0
for angs in itertools.product(ANGS_TABLE,repeat=MATRIX_SIZE-1):
i+=1
if (i%(number_of_entries/100) == 0):
sys.stdout.write('.')
sys.stdout.flush()
MAT = np.identity(MATRIX_SIZE)
for ang in angs:
MAT = np.dot(MAT,ANGS_TABLE[ang])
LTABLE[angs] = MAT
pickle.dump(ANGS_TABLE,open(ANGS_TABLE_FILE,"wb"), protocol = 2)
pickle.dump(ANGS_TABLE,open(LTABLE_FILE,"wb"), protocol = 2)
print len(ANGS_TABLE), len(LTABLE)
def read_data_lookup_file():
global ANGS_TABLE, LTABLE
generate_data_lookup()
int_vec = np.vectorize(int)
def find_angles(M):
less = ((0,0,0,0),25)
for angles in LTABLE:
mat = LTABLE[angles]
D = np.dot(mat,M.transpose())
abs_vec = np.vectorize(abs)
s = abs_vec(D).sum()
if s <= less[1]:
less = (angles,s)
#if less[1] < 25:
#return less[0]
return less[0]
def mat_recuperate(au,av,s):
U = LTABLE[au]#.transpose()
V = LTABLE[av]#.transpose()
S = np.diag(s)
return np.dot(U,np.dot(S,V))
def compact_data(data):
x = np.array(data)
A = np.matrix(np.array_split(x,5))
print A
U, s, V = svd(A)
print U
print V
au = find_angles(U)
av = find_angles(V)
print LTABLE[au].transpose()
print LTABLE[av].transpose()
#print au,av,s
Ar = mat_recuperate(au,av,int_vec(s))
Ar2 = np.dot(U,np.dot(np.diag(int_vec(s)),V))
print int_vec(Ar)
print int_vec(Ar2)
#print A - Ar
import random
data = [random.randint(0,2**8) for _ in range(25)]
compact_data(data)