From 763affb711eafc214b3237d84e81b9500a6381a0 Mon Sep 17 00:00:00 2001 From: Chunk Date: Sun, 21 Jun 2015 18:01:17 +0800 Subject: [PATCH] staged. --- chap1/1_3_infinite.py | 25 ++++++++++++++++++++----- chap1/common.py | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ chap1/common.pyc | Bin 0 -> 3080 bytes chap3/cholesky.py | 16 ++++++++-------- chap3/cholesky.pyc | Bin 4621 -> 0 bytes chap4/iterative.py | 3 ++- chap5/power.py | 13 +++++++------ common.py | 76 ---------------------------------------------------------------------------- common.pyc | Bin 3020 -> 0 bytes 9 files changed, 113 insertions(+), 96 deletions(-) create mode 100644 chap1/common.py create mode 100644 chap1/common.pyc delete mode 100644 common.py delete mode 100644 common.pyc diff --git a/chap1/1_3_infinite.py b/chap1/1_3_infinite.py index 048ba4b..7787255 100644 --- a/chap1/1_3_infinite.py +++ b/chap1/1_3_infinite.py @@ -2,6 +2,7 @@ __author__ = 'chunk' import struct import numpy as np +import math from common import * @@ -30,7 +31,7 @@ def double2bin(num): return ''.join(bits) -def infinite_float(): +def infinite_limit(): s = np.float32(0) tmp = np.float32(-1) i = 0 @@ -41,6 +42,11 @@ def infinite_float(): print i, s, tmp, float2bin(s) +def infinite_float(n): + s = np.float32(0) + for i in range(1, n + 1): + s += np.float32(1.0 / i) + return s def infinite_double(n): s = np.float64(0) @@ -58,14 +64,23 @@ def testn0(): if __name__ == '__main__': # testn0() - # infinite_float() + infinite_limit() + + for i in range(1,11): + print infinite_float(i) + for i in range(1,11): + print infinite_double(i) # i = 1 # while infinite_double(i) < 16: # i += 1 # print i, infinite_double(i) + timer = Timer() - timer.mark() - infinite_double(10000000) # 1125899906842624 - timer.report() + + for i in range(1,8): + n = math.pow(10,i) + timer.mark() + infinite_double(int(n)) # 1125899906842624 + timer.report() pass diff --git a/chap1/common.py b/chap1/common.py new file mode 100644 index 0000000..9a82dd0 --- /dev/null +++ b/chap1/common.py @@ -0,0 +1,76 @@ +""" +Common utils. + +@author: chunk +chunkplus@gmail.com +2014 Dec +""" +__author__ = 'hadoop' + +import os, sys +import time +import StringIO +import ConfigParser + +import numpy as np + +package_dir_imager = os.path.dirname(os.path.abspath(__file__)) + + +class Timer(): + def __init__(self): + self.__newtime = time.time() + self.__oldtime = self.__newtime + + def mark(self): + self.__oldtime = self.__newtime + self.__newtime = time.time() + return self.__newtime - self.__oldtime + + def report(self): + print "%-24s%fs" % ("time elapsed:", self.mark()) + + +def ttimer(): + newtime = time.time() + while True: + oldtime = newtime + newtime = time.time() + yield newtime - oldtime + + +def ctimer(): + newtime = time.clock() + while True: + oldtime = newtime + newtime = time.clock() + yield newtime - oldtime + + +def bytes2bits(arry_bytes): + """ + :param arry_bytes: 1-D np.unit8 array + :return: 1-D 0/1 array + """ + hid_data_bits = [map(int, '{0:08b}'.format(byte)) for byte in arry_bytes] + return np.array(hid_data_bits).ravel() + + +def bits2bytes(arry_bits): + """ + :param arry_bits: 1-D 0/1 array + :return: 1-D np.unit8 array + """ + str_bits = ''.join(map(str, arry_bits)) + arry_bytes = [int(str_bits[i:i + 8], 2) for i in range(0, len(str_bits), 8)] + return np.array(arry_bytes, dtype=np.uint8).ravel() + + +def test_grammer(): + a = 'fsaf' + b = ['dasf', 'dff'] + c = 'dgfsfdg' + # print a + b + print [a] + b # ['fsaf', 'dasf', 'dff'] + print [a] + [b] # ['fsaf', ['dasf', 'dff']] + print [a] + [c] # ['fsaf', 'dgfsfdg'] diff --git a/chap1/common.pyc b/chap1/common.pyc new file mode 100644 index 0000000..113c53d Binary files /dev/null and b/chap1/common.pyc differ diff --git a/chap3/cholesky.py b/chap3/cholesky.py index 356b27c..d047e42 100644 --- a/chap3/cholesky.py +++ b/chap3/cholesky.py @@ -108,23 +108,23 @@ def test(): def test1(): print "[origin(n=10)]" x, r, delta = calculate0(10) - print "x:%s\nr:%s\ndelta:%s\n" % (x, r, delta) - print "norm(r):%s\nnorm(delta):%s\n" % (norm(r), norm(delta)) + print "x:%s\nr:%s\ndelta:%s" % (x, r, delta) + print "norm(r):%s\nnorm(delta):%s" % (norm(r), norm(delta)) print "[disturbed(n=10)]" x, r, delta = calculate1(10) - print "x:%s\nr:%s\ndelta:%s\n" % (x, r, delta) - print "norm(r):%s\nnorm(delta):%s\n" % (norm(r), norm(delta)) + print "x:%s\nr:%s\ndelta:%s" % (x, r, delta) + print "norm(r):%s\nnorm(delta):%s" % (norm(r), norm(delta)) print "[n=8]" x, r, delta = calculate0(8) - print "x:%s\nr:%s\ndelta:%s\n" % (x, r, delta) - print "norm(r):%s\nnorm(delta):%s\n" % (norm(r), norm(delta)) + print "x:%s\nr:%s\ndelta:%s" % (x, r, delta) + print "norm(r):%s\nnorm(delta):%s" % (norm(r), norm(delta)) print "[n=12]" x, r, delta = calculate0(12) - print "x:%s\nr:%s\ndelta:%s\n" % (x, r, delta) - print "norm(r):%s\nnorm(delta):%s\n" % (norm(r), norm(delta)) + print "x:%s\nr:%s\ndelta:%s" % (x, r, delta) + print "norm(r):%s\nnorm(delta):%s" % (norm(r), norm(delta)) if __name__ == '__main__': diff --git a/chap3/cholesky.pyc b/chap3/cholesky.pyc index c3e3de2..9d28b85 100644 Binary files a/chap3/cholesky.pyc and b/chap3/cholesky.pyc differ diff --git a/chap4/iterative.py b/chap4/iterative.py index 4c16853..656fa86 100644 --- a/chap4/iterative.py +++ b/chap4/iterative.py @@ -73,7 +73,8 @@ def test(): A = gen_hilbert(n) b = gen_b(n) # print jacobi(A, b) - print SOR(A, b, 1) + # return + print SOR(A, b, 1.25) for w in np.linspace(0.5, 1.5, num=11): iter, x = SOR(A, b, w) diff --git a/chap5/power.py b/chap5/power.py index f7bc8bf..63fb5b4 100644 --- a/chap5/power.py +++ b/chap5/power.py @@ -44,17 +44,18 @@ def power_method(A, epsilon=0.00001, v=None): def test(): - ll = [0, 2, 0.5, 1, -3, 0.2] - ll2 = [i * 2 for i in ll] - print norm_vec(ll) - print norm_vec(ll2) + # ll = [0, 2, 0.5, 1, -3, 0.2] + # ll2 = [i * 2 for i in ll] + # print norm_vec(ll) + # print norm_vec(ll2) - A = [[3, 1], [1, 3]] - print power_method(A) + # A = [[3, 1], [1, 3]] + # print power_method(A) A = [[5, -4, 1], [-4, 6, -4], [1, -4, 7]] B = [[25, -41, 10, -6], [-41, 68, -17, 10], [10, -17, 5, -3], [-6, 10, -3, 2]] print power_method(A) + print "" print power_method(B) diff --git a/common.py b/common.py deleted file mode 100644 index 9a82dd0..0000000 --- a/common.py +++ /dev/null @@ -1,76 +0,0 @@ -""" -Common utils. - -@author: chunk -chunkplus@gmail.com -2014 Dec -""" -__author__ = 'hadoop' - -import os, sys -import time -import StringIO -import ConfigParser - -import numpy as np - -package_dir_imager = os.path.dirname(os.path.abspath(__file__)) - - -class Timer(): - def __init__(self): - self.__newtime = time.time() - self.__oldtime = self.__newtime - - def mark(self): - self.__oldtime = self.__newtime - self.__newtime = time.time() - return self.__newtime - self.__oldtime - - def report(self): - print "%-24s%fs" % ("time elapsed:", self.mark()) - - -def ttimer(): - newtime = time.time() - while True: - oldtime = newtime - newtime = time.time() - yield newtime - oldtime - - -def ctimer(): - newtime = time.clock() - while True: - oldtime = newtime - newtime = time.clock() - yield newtime - oldtime - - -def bytes2bits(arry_bytes): - """ - :param arry_bytes: 1-D np.unit8 array - :return: 1-D 0/1 array - """ - hid_data_bits = [map(int, '{0:08b}'.format(byte)) for byte in arry_bytes] - return np.array(hid_data_bits).ravel() - - -def bits2bytes(arry_bits): - """ - :param arry_bits: 1-D 0/1 array - :return: 1-D np.unit8 array - """ - str_bits = ''.join(map(str, arry_bits)) - arry_bytes = [int(str_bits[i:i + 8], 2) for i in range(0, len(str_bits), 8)] - return np.array(arry_bytes, dtype=np.uint8).ravel() - - -def test_grammer(): - a = 'fsaf' - b = ['dasf', 'dff'] - c = 'dgfsfdg' - # print a + b - print [a] + b # ['fsaf', 'dasf', 'dff'] - print [a] + [b] # ['fsaf', ['dasf', 'dff']] - print [a] + [c] # ['fsaf', 'dgfsfdg'] diff --git a/common.pyc b/common.pyc deleted file mode 100644 index 3f6cc80..0000000 Binary files a/common.pyc and /dev/null differ -- libgit2 0.21.2