-
Notifications
You must be signed in to change notification settings - Fork 6
/
spectral_norm_cython.pyx
67 lines (54 loc) · 1.64 KB
/
spectral_norm_cython.pyx
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
# The Computer Language Benchmarks Game
# http://shootout.alioth.debian.org/
#
# Contributed by Sebastien Loisel
# Fixed by Isaac Gouy
# Sped up by Josh Goldfoot
# Dirtily sped up by Simon Descarpentries
# Used list comprehension by Vadim Zelenin
from libc.stdlib cimport malloc
from libc.math cimport sqrt
from sys import argv
cdef double eval_A(long i, long j):
cdef long ij = i + j
return 1.0 / (((ij * (ij + 1)) >> 1) + i + 1)
cdef double *eval_A_times_u(double* u, long n):
cdef long i, j
#cdef double u_j
cdef double* tmp = <double *> malloc(n*sizeof(double))
for i in xrange(n):
tmp[i] = 0.0
for j in xrange(n):
tmp[i] += eval_A(i, j) * u[j]
#free(u)
return tmp
cdef double *eval_At_times_u(double* u, long n):
cdef long i,j
#cdef double u_j
cdef double* tmp = <double *> malloc(n*sizeof(double))
for i in xrange(n):
tmp[i] = 0.0
for j in xrange(n):
tmp[i] += eval_A(j,i) * u[j]
#free(u)
return tmp
cdef double *eval_AtA_times_u(double* u, long n):
return eval_At_times_u(eval_A_times_u(u, n), n)
def main():
cdef long n = int(argv[1])
cdef double* u = <double *> malloc(n*sizeof(double))
cdef double* v = <double *> malloc(n*sizeof(double))
cdef double* tmp = <double *> malloc(n*sizeof(double))
cdef double vBv, vv
cdef long i
for i in xrange(n):
u[i] = 1.0
for dummy in xrange(10):
v = eval_AtA_times_u(u, n)
u = eval_AtA_times_u(v, n)
vBv = vv = 0.0
for i in xrange(n):
vBv += u[i] * v[i]
vv += v[i] * v[i]
print "%0.9f" % (sqrt(vBv/vv))
main()