Commit 763affb711eafc214b3237d84e81b9500a6381a0

Authored by Chunk
1 parent 7d0297f3
Exists in master

staged.

chap1/1_3_infinite.py
... ... @@ -2,6 +2,7 @@ __author__ = 'chunk'
2 2  
3 3 import struct
4 4 import numpy as np
  5 +import math
5 6  
6 7 from common import *
7 8  
... ... @@ -30,7 +31,7 @@ def double2bin(num):
30 31 return ''.join(bits)
31 32  
32 33  
33   -def infinite_float():
  34 +def infinite_limit():
34 35 s = np.float32(0)
35 36 tmp = np.float32(-1)
36 37 i = 0
... ... @@ -41,6 +42,11 @@ def infinite_float():
41 42  
42 43 print i, s, tmp, float2bin(s)
43 44  
  45 +def infinite_float(n):
  46 + s = np.float32(0)
  47 + for i in range(1, n + 1):
  48 + s += np.float32(1.0 / i)
  49 + return s
44 50  
45 51 def infinite_double(n):
46 52 s = np.float64(0)
... ... @@ -58,14 +64,23 @@ def testn0():
58 64  
59 65 if __name__ == '__main__':
60 66 # testn0()
61   - # infinite_float()
  67 + infinite_limit()
  68 +
  69 + for i in range(1,11):
  70 + print infinite_float(i)
  71 + for i in range(1,11):
  72 + print infinite_double(i)
62 73  
63 74 # i = 1
64 75 # while infinite_double(i) < 16:
65 76 # i += 1
66 77 # print i, infinite_double(i)
  78 +
67 79 timer = Timer()
68   - timer.mark()
69   - infinite_double(10000000) # 1125899906842624
70   - timer.report()
  80 +
  81 + for i in range(1,8):
  82 + n = math.pow(10,i)
  83 + timer.mark()
  84 + infinite_double(int(n)) # 1125899906842624
  85 + timer.report()
71 86 pass
... ...
chap1/common.py 0 → 100644
... ... @@ -0,0 +1,76 @@
  1 +"""
  2 +Common utils.
  3 +
  4 +@author: chunk
  5 +chunkplus@gmail.com
  6 +2014 Dec
  7 +"""
  8 +__author__ = 'hadoop'
  9 +
  10 +import os, sys
  11 +import time
  12 +import StringIO
  13 +import ConfigParser
  14 +
  15 +import numpy as np
  16 +
  17 +package_dir_imager = os.path.dirname(os.path.abspath(__file__))
  18 +
  19 +
  20 +class Timer():
  21 + def __init__(self):
  22 + self.__newtime = time.time()
  23 + self.__oldtime = self.__newtime
  24 +
  25 + def mark(self):
  26 + self.__oldtime = self.__newtime
  27 + self.__newtime = time.time()
  28 + return self.__newtime - self.__oldtime
  29 +
  30 + def report(self):
  31 + print "%-24s%fs" % ("time elapsed:", self.mark())
  32 +
  33 +
  34 +def ttimer():
  35 + newtime = time.time()
  36 + while True:
  37 + oldtime = newtime
  38 + newtime = time.time()
  39 + yield newtime - oldtime
  40 +
  41 +
  42 +def ctimer():
  43 + newtime = time.clock()
  44 + while True:
  45 + oldtime = newtime
  46 + newtime = time.clock()
  47 + yield newtime - oldtime
  48 +
  49 +
  50 +def bytes2bits(arry_bytes):
  51 + """
  52 + :param arry_bytes: 1-D np.unit8 array
  53 + :return: 1-D 0/1 array
  54 + """
  55 + hid_data_bits = [map(int, '{0:08b}'.format(byte)) for byte in arry_bytes]
  56 + return np.array(hid_data_bits).ravel()
  57 +
  58 +
  59 +def bits2bytes(arry_bits):
  60 + """
  61 + :param arry_bits: 1-D 0/1 array
  62 + :return: 1-D np.unit8 array
  63 + """
  64 + str_bits = ''.join(map(str, arry_bits))
  65 + arry_bytes = [int(str_bits[i:i + 8], 2) for i in range(0, len(str_bits), 8)]
  66 + return np.array(arry_bytes, dtype=np.uint8).ravel()
  67 +
  68 +
  69 +def test_grammer():
  70 + a = 'fsaf'
  71 + b = ['dasf', 'dff']
  72 + c = 'dgfsfdg'
  73 + # print a + b
  74 + print [a] + b # ['fsaf', 'dasf', 'dff']
  75 + print [a] + [b] # ['fsaf', ['dasf', 'dff']]
  76 + print [a] + [c] # ['fsaf', 'dgfsfdg']
... ...
chap1/common.pyc 0 → 100644
No preview for this file type
chap3/cholesky.py
... ... @@ -108,23 +108,23 @@ def test():
108 108 def test1():
109 109 print "[origin(n=10)]"
110 110 x, r, delta = calculate0(10)
111   - print "x:%s\nr:%s\ndelta:%s\n" % (x, r, delta)
112   - print "norm(r):%s\nnorm(delta):%s\n" % (norm(r), norm(delta))
  111 + print "x:%s\nr:%s\ndelta:%s" % (x, r, delta)
  112 + print "norm(r):%s\nnorm(delta):%s" % (norm(r), norm(delta))
113 113  
114 114 print "[disturbed(n=10)]"
115 115 x, r, delta = calculate1(10)
116   - print "x:%s\nr:%s\ndelta:%s\n" % (x, r, delta)
117   - print "norm(r):%s\nnorm(delta):%s\n" % (norm(r), norm(delta))
  116 + print "x:%s\nr:%s\ndelta:%s" % (x, r, delta)
  117 + print "norm(r):%s\nnorm(delta):%s" % (norm(r), norm(delta))
118 118  
119 119 print "[n=8]"
120 120 x, r, delta = calculate0(8)
121   - print "x:%s\nr:%s\ndelta:%s\n" % (x, r, delta)
122   - print "norm(r):%s\nnorm(delta):%s\n" % (norm(r), norm(delta))
  121 + print "x:%s\nr:%s\ndelta:%s" % (x, r, delta)
  122 + print "norm(r):%s\nnorm(delta):%s" % (norm(r), norm(delta))
123 123  
124 124 print "[n=12]"
125 125 x, r, delta = calculate0(12)
126   - print "x:%s\nr:%s\ndelta:%s\n" % (x, r, delta)
127   - print "norm(r):%s\nnorm(delta):%s\n" % (norm(r), norm(delta))
  126 + print "x:%s\nr:%s\ndelta:%s" % (x, r, delta)
  127 + print "norm(r):%s\nnorm(delta):%s" % (norm(r), norm(delta))
128 128  
129 129  
130 130 if __name__ == '__main__':
... ...
chap3/cholesky.pyc
No preview for this file type
chap4/iterative.py
... ... @@ -73,7 +73,8 @@ def test():
73 73 A = gen_hilbert(n)
74 74 b = gen_b(n)
75 75 # print jacobi(A, b)
76   - print SOR(A, b, 1)
  76 + # return
  77 + print SOR(A, b, 1.25)
77 78  
78 79 for w in np.linspace(0.5, 1.5, num=11):
79 80 iter, x = SOR(A, b, w)
... ...
chap5/power.py
... ... @@ -44,17 +44,18 @@ def power_method(A, epsilon=0.00001, v=None):
44 44  
45 45  
46 46 def test():
47   - ll = [0, 2, 0.5, 1, -3, 0.2]
48   - ll2 = [i * 2 for i in ll]
49   - print norm_vec(ll)
50   - print norm_vec(ll2)
  47 + # ll = [0, 2, 0.5, 1, -3, 0.2]
  48 + # ll2 = [i * 2 for i in ll]
  49 + # print norm_vec(ll)
  50 + # print norm_vec(ll2)
51 51  
52   - A = [[3, 1], [1, 3]]
53   - print power_method(A)
  52 + # A = [[3, 1], [1, 3]]
  53 + # print power_method(A)
54 54  
55 55 A = [[5, -4, 1], [-4, 6, -4], [1, -4, 7]]
56 56 B = [[25, -41, 10, -6], [-41, 68, -17, 10], [10, -17, 5, -3], [-6, 10, -3, 2]]
57 57 print power_method(A)
  58 + print ""
58 59 print power_method(B)
59 60  
60 61  
... ...
common.py
... ... @@ -1,76 +0,0 @@
1   -"""
2   -Common utils.
3   -
4   -@author: chunk
5   -chunkplus@gmail.com
6   -2014 Dec
7   -"""
8   -__author__ = 'hadoop'
9   -
10   -import os, sys
11   -import time
12   -import StringIO
13   -import ConfigParser
14   -
15   -import numpy as np
16   -
17   -package_dir_imager = os.path.dirname(os.path.abspath(__file__))
18   -
19   -
20   -class Timer():
21   - def __init__(self):
22   - self.__newtime = time.time()
23   - self.__oldtime = self.__newtime
24   -
25   - def mark(self):
26   - self.__oldtime = self.__newtime
27   - self.__newtime = time.time()
28   - return self.__newtime - self.__oldtime
29   -
30   - def report(self):
31   - print "%-24s%fs" % ("time elapsed:", self.mark())
32   -
33   -
34   -def ttimer():
35   - newtime = time.time()
36   - while True:
37   - oldtime = newtime
38   - newtime = time.time()
39   - yield newtime - oldtime
40   -
41   -
42   -def ctimer():
43   - newtime = time.clock()
44   - while True:
45   - oldtime = newtime
46   - newtime = time.clock()
47   - yield newtime - oldtime
48   -
49   -
50   -def bytes2bits(arry_bytes):
51   - """
52   - :param arry_bytes: 1-D np.unit8 array
53   - :return: 1-D 0/1 array
54   - """
55   - hid_data_bits = [map(int, '{0:08b}'.format(byte)) for byte in arry_bytes]
56   - return np.array(hid_data_bits).ravel()
57   -
58   -
59   -def bits2bytes(arry_bits):
60   - """
61   - :param arry_bits: 1-D 0/1 array
62   - :return: 1-D np.unit8 array
63   - """
64   - str_bits = ''.join(map(str, arry_bits))
65   - arry_bytes = [int(str_bits[i:i + 8], 2) for i in range(0, len(str_bits), 8)]
66   - return np.array(arry_bytes, dtype=np.uint8).ravel()
67   -
68   -
69   -def test_grammer():
70   - a = 'fsaf'
71   - b = ['dasf', 'dff']
72   - c = 'dgfsfdg'
73   - # print a + b
74   - print [a] + b # ['fsaf', 'dasf', 'dff']
75   - print [a] + [b] # ['fsaf', ['dasf', 'dff']]
76   - print [a] + [c] # ['fsaf', 'dgfsfdg']
common.pyc
No preview for this file type