def test_gaussel3(self) : # matrix dari vektor baris X = array([ # kasus j = k : solusi unik [1,2,3], [4,5,6], [7,8,9] ]) Y = array([ [10,11,12] ]) print "kasus unik normal" print solve_linear( (X.astype(float)).T, Y.astype(float) )
def test_gaussel0(self) : # matrix dari vektor baris X = array([ # kasus j = k : solusi unik [1,2,3], [4,5,6] ]) Y = array([ [5,7,10] ]) print "linear independen (1)" print "=================" print solve_linear( (X.astype(float)).T, Y.astype(float) )
def test_gaussel2(self) : # matrix dari vektor baris X = array([ # kasus j = k : solusi unik [1,2,3], [4,5,6], [5,7,10] ]) Y = array([ [0,0,0] ]) print "sistem homogen" print "==============" print solve_linear( (X.astype(float)).T, Y.astype(float) )
def shortest(B) : (j,k) = B.shape if j==1 : return B b = LLL(B).getBasis() # representasi basis dalam array 2d while True : # aproximasi reduced basis b_ = orthoproject(b[0], b[1:]) # rekursif b__ = shortest(b_) # lifting dg transformasi linear, catatan 13/12 T = solve_linear( b_.T,b__ ) # b_ perlu ditransform, liat spek # transpose T karena msh kolom v = matrix( T.T )*matrix( b[1:] ) # ubah ke matrix dulu b[1:] = np.asarray(v) # override array lagi b0n = norm(b[0]) b1n = norm(b[1]) if b1n**2 >= (3.0/4)*b0n**2 : break # swap t = np.copy(b[0]) b[0] = b[1] b[1] = t # temukan nilai batas j0 g = Gram(b) # cari vektor yg norm-nya lebih besar dari b0 i = 1 while i < j : v = g.getBstar(i) if norm(v) >= b0n : break i += 1 # enumerate v1 = enumerate(b[:i], g) # select-basis B = select_basis( np.append([v1], b, axis=0) ) # ulangi lagi step spt yg didalam loop B_ = orthoproject( B[0], B[1:] ) B__ = shortest(B_) # rekursif T = solve_linear( B_.T, B__ ) # tangani kondisi ketika pengenolan # apakah dalam enumerate alpha diubah2 secara share V = matrix( T.T )*matrix( B[1:] ) # perlu bentuk matrix B[1:] = np.asarray(V) return B
def test_gaussel0(self) : X = array( [[1,2,3]] ) Y = array( [[1,2,3]] ) print "kasus paling dasar" print "==================" T = solve_linear( (X.astype(float)).T, Y.astype(float) ) # input kolom perlu transp print T
def test_gaussel7(self) : X = array([ # kasus j = k : solusi unik [0,-1,-1], [4,7,8], [-3,-5,-6] ]) Y = array([ [3, 4, 5] ]) print "kasus unik full rank" T = solve_linear( (X.astype(float)).T, Y.astype(float) ) # input kolom perlu transp print T
def test_gaussel6(self) : X = array([ # kasus j = k : solusi unik [2,-1,0], [-1,2,-1], [0,-1,1] ]) Y = array([ [0, 0, 1] ]) print "kasus unik full rank" T = solve_linear( (X.astype(float)).T, Y.astype(float) ) # input kolom perlu transp print T
def test_gaussel5(self) : X = array([ # kasus j = k : solusi unik [1,1,1], [1,2,2], [1,2,3] ]) Y = array([ [1, 1, 1] ]) print "kasus unik full rank" T = solve_linear( (X.astype(float)).T, Y.astype(float) ) # input kolom perlu transp print T
def test_gaussel4(self) : X = array([ # kasus j = k : solusi unik [0,-2,-2], [1,4,5], [-1,-1,-4] ]) Y = array([ [3, 1, -2] ]) print "kasus unik full rank" T = solve_linear( (X.astype(float)).T, Y.astype(float) ) # input kolom perlu transp print T
def test_gaussel2(self): X = array([ # kasus j = k : solusi unik [1,2,3], [7,8,9] ]) Y = array([ [4,5,6], [10, 11, 12] ]) print "kasus unik not full rank" T = solve_linear( (X.astype(float)).T, Y.astype(float) ) # input kolom perlu transp print T
def select_basis(b) : # b is numpy array (j,k) = b.shape # j = k+1 global Z if Z is None : Z = np.zeros(k) # TODO : cek apakah rekursif berpengaruh b[0] = map(lambda x: round(x,14), b[0]) # cek pembulatan nol pd b0 if all( b[0]==Z ) : if len(b) > 1 : basis = b[1:] else : basis = [] else : # tes apakah b[0]= kombinasi linear dari b[1]..b[j-1] # menggunakan eliminasi Gauss c = solve_linear( (b[1:j].astype(float)).T , [ b[0] ] ) a = np.empty([j-1,k], dtype=object) if c==[] : # b[0] bebas linear thd b lain a[0] = np.copy(b[0]) else : xs = c.T[0] # c msh dalam represnt kolom # khusus bagian ini lihat buku Bremner p.185 # kita tidak perlu y dan z karena sdh ditangani oleh Fraction # tidak perlu menangani x_i = 0 i = findNZ1( xs ) f = Fraction( str(xs[i]) ) m = f.denominator i2 = i+1 while i2 < len(xs) : if xs[i2] : f = Fraction( str(xs[i2]) ) m = lcm( f.denominator, m ) i2 += 1 d = long( round( m*xs[i] ) ) i += 1 while i < len(xs) : if xs[i] : x = long( round(m*xs[i]) ) d = gcd( x,d ) i += 1 # python perlu denominator tetap positif if d < 0 : m *= -1 d *= -1 # f adalah bentuk rasional m/d yg jika relative prima = p/q # yg berarti faktor bersama nya dikeluarkan dan diambil q nya f = Fraction( '%d/%d' % (m,d) ) if m < 0 : a[0] = ( -1.0/f.denominator )*b[0] else : a[0] = ( 1.0/f.denominator )*b[0] # proyeksi b ke a[0]. karena b[0] sdh diproses, maka # b yg diproyeksi berikutnya adalah 1,2..j-1 b_ = orthoproject( a[0], b[1:] ) # rekursif v = select_basis(b_) # hasil dari rekursif digunakan utk lifting dari c ke a i = 1 # a[0] sudah diproses for c in v : a[i] = lifting(b, c) i += 1 basis = a return basis
def test_step_4dim(self) : b_ = np.array([ [2.46632124352332, 0.25906735751295, -2.11917098445596, -3.59067357512953], [3.4786437692646395, -3.6177895200352275, 4.313518273888157, -0.41743725231175377]], dtype=object) b = np.array([[ 2.46632124, 0.25906736, -2.11917098, -3.59067358], [ 3.24352332, -3.64248705, 4.51554404, -0.07512953]]) T = solve_linear(b.T,b_)