def problem4(): n = 50 H = splin.hilbert(n) Hi = splin.invhilbert(n) U, S, V = splin.svd(H) b = U[:, 0] * S[0] db = U[:, -1] * S[-1] print(S[-1]) x = Hi.dot(b) dx = Hi.dot(db) k = S[0] / S[-1] print('Condition number of H50: {}'.format(k)) dxx = np.linalg.norm(dx) / np.linalg.norm(x) dbb = np.linalg.norm(db) / np.linalg.norm(b) q = dxx / dbb print('Quotient of pertubations: {}'.format(q)) print('Quotient: {}'.format(q / k)) maxq = 0 for j in range(0, 100): num_vecs = 1000 b_collection = np.random.rand(n, num_vecs) db_collection = np.random.rand(n, num_vecs) * 1e-16 x_collection = Hi.dot(b_collection) dx_collection = Hi.dot(db_collection) q_collection = np.zeros((num_vecs, 1)) for k in range(0, num_vecs): q_collection[k] = np.linalg.norm( dx_collection[:, k]) * np.linalg.norm(b_collection[:, k]) q_collection[k] /= np.linalg.norm( db_collection[:, k]) * np.linalg.norm(x_collection[:, k]) maxq = max(maxq, max(q_collection)) print(j) print(max(maxq))
def test_inverse(self): for n in xrange(1, 10): a = hilbert(n) b = invhilbert(n) # The Hilbert matrix is increasingly badly conditioned, # so take that into account in the test c = cond(a) assert_allclose(a.dot(b), eye(n), atol=1e-15 * c, rtol=1e-15 * c)
def test_inverse(self): for n in xrange(1, 10): a = hilbert(n) b = invhilbert(n) # The Hilbert matrix is increasingly badly conditioned, # so take that into account in the test c = cond(a) assert_allclose(a.dot(b), eye(n), atol=1e-15*c, rtol=1e-15*c)
def __init__(self, r, s=1): dim = len(r) self.invH = linalg.invhilbert(dim) # self.invH = self.invH @ self.invH self.s = s self.r = np.array(r) self.r.shape = (1, -1) self.norm = (s / np.sqrt(np.pi))**dim * np.sqrt(linalg.det(self.invH))
def __init__(self, dim, r_s=None): """ Initialisiert ein neues Hilbert-Objekt. Input: dim (int): Dimension der Hilbertmatrix. Return: - """ self.dim = dim self.hil_matr = lina.hilbert(self.dim) self.inv_hil_matr = lina.invhilbert(self.dim) self.r_s = r_s
def test_basic(self): invh1 = array([[1]]) assert_array_equal(invhilbert(1, exact=True), invh1) assert_array_equal(invhilbert(1), invh1) invh2 = array([[4, -6], [-6, 12]]) assert_array_equal(invhilbert(2, exact=True), invh2) assert_array_almost_equal(invhilbert(2), invh2) invh3 = array([[9, -36, 30], [-36, 192, -180], [30, -180, 180]]) assert_array_equal(invhilbert(3, exact=True), invh3) assert_array_almost_equal(invhilbert(3), invh3) invh4 = array([[16, -120, 240, -140], [-120, 1200, -2700, 1680], [240, -2700, 6480, -4200], [-140, 1680, -4200, 2800]]) assert_array_equal(invhilbert(4, exact=True), invh4) assert_array_almost_equal(invhilbert(4), invh4) invh5 = array([[25, -300, 1050, -1400, 630], [-300, 4800, -18900, 26880, -12600], [1050, -18900, 79380, -117600, 56700], [-1400, 26880, -117600, 179200, -88200], [630, -12600, 56700, -88200, 44100]]) assert_array_equal(invhilbert(5, exact=True), invh5) assert_array_almost_equal(invhilbert(5), invh5) invh17 = array([ [ 289, -41616, 1976760, -46124400, 629598060, -5540462928, 33374693352, -143034400080, 446982500250, -1033026222800, 1774926873720, -2258997839280, 2099709530100, -1384423866000, 613101997800, -163493866080, 19835652870 ], [ -41616, 7990272, -426980160, 10627061760, -151103534400, 1367702848512, -8410422724704, 36616806420480, -115857864064800, 270465047424000, -468580694662080, 600545887119360, -561522320049600, 372133135180800, -165537539406000, 44316454993920, -5395297580640 ], [ 1976760, -426980160, 24337869120, -630981792000, 9228108708000, -85267724461920, 532660105897920, -2348052711713280, 7504429831470000, -17664748409880000, 30818191841236800, -39732544853164800, 37341234283298400, -24857330514030000, 11100752642520000, -2982128117299200, 364182586693200 ], [ -46124400, 10627061760, -630981792000, 16826181120000, -251209625940000, 2358021022156800, -14914482965141760, 66409571644416000, -214015221119700000, 507295338950400000, -890303319857952000, 1153715376477081600, -1089119333262870000, 727848632044800000, -326170262829600000, 87894302404608000, -10763618673376800 ], [ 629598060, -151103534400, 9228108708000, -251209625940000, 3810012660090000, -36210360321495360, 231343968720664800, -1038687206500944000, 3370739732635275000, -8037460526495400000, 14178080368737885600, -18454939322943942000, 17489975175339030000, -11728977435138600000, 5272370630081100000, -1424711708039692800, 174908803442373000 ], [ -5540462928, 1367702848512, -85267724461920, 2358021022156800, -36210360321495360, 347619459086355456, -2239409617216035264, 10124803292907663360, -33052510749726468000, 79217210949138662400, -140362995650505067440, 183420385176741672960, -174433352415381259200, 117339159519533952000, -52892422160973595200, 14328529177999196160, -1763080738699119840 ], [ 33374693352, -8410422724704, 532660105897920, -14914482965141760, 231343968720664800, -2239409617216035264, 14527452132196331328, -66072377044391477760, 216799987176909536400, -521925895055522958000, 928414062734059661760, -1217424500995626443520, 1161358898976091015200, -783401860847777371200, 354015418167362952000, -96120549902411274240, 11851820521255194480 ], [ -143034400080, 36616806420480, -2348052711713280, 66409571644416000, -1038687206500944000, 10124803292907663360, -66072377044391477760, 302045152202932469760, -995510145200094810000, 2405996923185123840000, -4294704507885446054400, 5649058909023744614400, -5403874060541811254400, 3654352703663101440000, -1655137020003255360000, 450325202737117593600, -55630994283442749600 ], [ 446982500250, -115857864064800, 7504429831470000, -214015221119700000, 3370739732635275000, -33052510749726468000, 216799987176909536400, -995510145200094810000, 3293967392206196062500, -7988661659013106500000, 14303908928401362270000, -18866974090684772052000, 18093328327706957325000, -12263364009096700500000, 5565847995255512250000, -1517208935002984080000, 187754605706619279900 ], [ -1033026222800, 270465047424000, -17664748409880000, 507295338950400000, -8037460526495400000, 79217210949138662400, -521925895055522958000, 2405996923185123840000, -7988661659013106500000, 19434404971634224000000, -34894474126569249192000, 46141453390504792320000, -44349976506971935800000, 30121928988527376000000, -13697025107665828500000, 3740200989399948902400, -463591619028689580000 ], [ 1774926873720, -468580694662080, 30818191841236800, -890303319857952000, 14178080368737885600, -140362995650505067440, 928414062734059661760, -4294704507885446054400, 14303908928401362270000, -34894474126569249192000, 62810053427824648545600, -83243376594051600326400, 80177044485212743068000, -54558343880470209780000, 24851882355348879230400, -6797096028813368678400, 843736746632215035600 ], [ -2258997839280, 600545887119360, -39732544853164800, 1153715376477081600, -18454939322943942000, 183420385176741672960, -1217424500995626443520, 5649058909023744614400, -18866974090684772052000, 46141453390504792320000, -83243376594051600326400, 110552468520163390156800, -106681852579497947388000, 72720410752415168870400, -33177973900974346080000, 9087761081682520473600, -1129631016152221783200 ], [ 2099709530100, -561522320049600, 37341234283298400, -1089119333262870000, 17489975175339030000, -174433352415381259200, 1161358898976091015200, -5403874060541811254400, 18093328327706957325000, -44349976506971935800000, 80177044485212743068000, -106681852579497947388000, 103125790826848015808400, -70409051543137015800000, 32171029219823375700000, -8824053728865840192000, 1098252376814660067000 ], [ -1384423866000, 372133135180800, -24857330514030000, 727848632044800000, -11728977435138600000, 117339159519533952000, -783401860847777371200, 3654352703663101440000, -12263364009096700500000, 30121928988527376000000, -54558343880470209780000, 72720410752415168870400, -70409051543137015800000, 48142941226076592000000, -22027500987368499000000, 6049545098753157120000, -753830033789944188000 ], [ 613101997800, -165537539406000, 11100752642520000, -326170262829600000, 5272370630081100000, -52892422160973595200, 354015418167362952000, -1655137020003255360000, 5565847995255512250000, -13697025107665828500000, 24851882355348879230400, -33177973900974346080000, 32171029219823375700000, -22027500987368499000000, 10091416708498869000000, -2774765838662800128000, 346146444087219270000 ], [ -163493866080, 44316454993920, -2982128117299200, 87894302404608000, -1424711708039692800, 14328529177999196160, -96120549902411274240, 450325202737117593600, -1517208935002984080000, 3740200989399948902400, -6797096028813368678400, 9087761081682520473600, -8824053728865840192000, 6049545098753157120000, -2774765838662800128000, 763806510427609497600, -95382575704033754400 ], [ 19835652870, -5395297580640, 364182586693200, -10763618673376800, 174908803442373000, -1763080738699119840, 11851820521255194480, -55630994283442749600, 187754605706619279900, -463591619028689580000, 843736746632215035600, -1129631016152221783200, 1098252376814660067000, -753830033789944188000, 346146444087219270000, -95382575704033754400, 11922821963004219300 ] ]) assert_array_equal(invhilbert(17, exact=True), invh17) assert_allclose(invhilbert(17), invh17.astype(float), rtol=1e-12)
def test_basic(self): invh1 = array([[1]]) assert_array_equal(invhilbert(1, exact=True), invh1) assert_array_equal(invhilbert(1), invh1) invh2 = array([[4, -6], [-6, 12]]) assert_array_equal(invhilbert(2, exact=True), invh2) assert_array_almost_equal(invhilbert(2), invh2) invh3 = array([[9, -36, 30], [-36, 192, -180], [30, -180, 180]]) assert_array_equal(invhilbert(3, exact=True), invh3) assert_array_almost_equal(invhilbert(3), invh3) invh4 = array([[16, -120, 240, -140], [-120, 1200, -2700, 1680], [240, -2700, 6480, -4200], [-140, 1680, -4200, 2800]]) assert_array_equal(invhilbert(4, exact=True), invh4) assert_array_almost_equal(invhilbert(4), invh4) invh5 = array([[25, -300, 1050, -1400, 630], [-300, 4800, -18900, 26880, -12600], [1050, -18900, 79380, -117600, 56700], [-1400, 26880, -117600, 179200, -88200], [630, -12600, 56700, -88200, 44100]]) assert_array_equal(invhilbert(5, exact=True), invh5) assert_array_almost_equal(invhilbert(5), invh5) invh17 = array([ [289, -41616, 1976760, -46124400, 629598060, -5540462928, 33374693352, -143034400080, 446982500250, -1033026222800, 1774926873720, -2258997839280, 2099709530100, -1384423866000, 613101997800, -163493866080, 19835652870], [-41616, 7990272, -426980160, 10627061760, -151103534400, 1367702848512, -8410422724704, 36616806420480, -115857864064800, 270465047424000, -468580694662080, 600545887119360, -561522320049600, 372133135180800, -165537539406000, 44316454993920, -5395297580640], [1976760, -426980160, 24337869120, -630981792000, 9228108708000, -85267724461920, 532660105897920, -2348052711713280, 7504429831470000, -17664748409880000, 30818191841236800, -39732544853164800, 37341234283298400, -24857330514030000, 11100752642520000, -2982128117299200, 364182586693200], [-46124400, 10627061760, -630981792000, 16826181120000, -251209625940000, 2358021022156800, -14914482965141760, 66409571644416000, -214015221119700000, 507295338950400000, -890303319857952000, 1153715376477081600, -1089119333262870000, 727848632044800000, -326170262829600000, 87894302404608000, -10763618673376800], [629598060, -151103534400, 9228108708000, -251209625940000, 3810012660090000, -36210360321495360, 231343968720664800, -1038687206500944000, 3370739732635275000, -8037460526495400000, 14178080368737885600, -18454939322943942000, 17489975175339030000, -11728977435138600000, 5272370630081100000, -1424711708039692800, 174908803442373000], [-5540462928, 1367702848512, -85267724461920, 2358021022156800, -36210360321495360, 347619459086355456, -2239409617216035264, 10124803292907663360, -33052510749726468000, 79217210949138662400, -140362995650505067440, 183420385176741672960, -174433352415381259200, 117339159519533952000, -52892422160973595200, 14328529177999196160, -1763080738699119840], [33374693352, -8410422724704, 532660105897920, -14914482965141760, 231343968720664800, -2239409617216035264, 14527452132196331328, -66072377044391477760, 216799987176909536400, -521925895055522958000, 928414062734059661760, -1217424500995626443520, 1161358898976091015200, -783401860847777371200, 354015418167362952000, -96120549902411274240, 11851820521255194480], [-143034400080, 36616806420480, -2348052711713280, 66409571644416000, -1038687206500944000, 10124803292907663360, -66072377044391477760, 302045152202932469760, -995510145200094810000, 2405996923185123840000, -4294704507885446054400, 5649058909023744614400, -5403874060541811254400, 3654352703663101440000, -1655137020003255360000, 450325202737117593600, -55630994283442749600], [446982500250, -115857864064800, 7504429831470000, -214015221119700000, 3370739732635275000, -33052510749726468000, 216799987176909536400, -995510145200094810000, 3293967392206196062500, -7988661659013106500000, 14303908928401362270000, -18866974090684772052000, 18093328327706957325000, -12263364009096700500000, 5565847995255512250000, -1517208935002984080000, 187754605706619279900], [-1033026222800, 270465047424000, -17664748409880000, 507295338950400000, -8037460526495400000, 79217210949138662400, -521925895055522958000, 2405996923185123840000, -7988661659013106500000, 19434404971634224000000, -34894474126569249192000, 46141453390504792320000, -44349976506971935800000, 30121928988527376000000, -13697025107665828500000, 3740200989399948902400, -463591619028689580000], [1774926873720, -468580694662080, 30818191841236800, -890303319857952000, 14178080368737885600, -140362995650505067440, 928414062734059661760, -4294704507885446054400, 14303908928401362270000, -34894474126569249192000, 62810053427824648545600, -83243376594051600326400, 80177044485212743068000, -54558343880470209780000, 24851882355348879230400, -6797096028813368678400, 843736746632215035600], [-2258997839280, 600545887119360, -39732544853164800, 1153715376477081600, -18454939322943942000, 183420385176741672960, -1217424500995626443520, 5649058909023744614400, -18866974090684772052000, 46141453390504792320000, -83243376594051600326400, 110552468520163390156800, -106681852579497947388000, 72720410752415168870400, -33177973900974346080000, 9087761081682520473600, -1129631016152221783200], [2099709530100, -561522320049600, 37341234283298400, -1089119333262870000, 17489975175339030000, -174433352415381259200, 1161358898976091015200, -5403874060541811254400, 18093328327706957325000, -44349976506971935800000, 80177044485212743068000, -106681852579497947388000, 103125790826848015808400, -70409051543137015800000, 32171029219823375700000, -8824053728865840192000, 1098252376814660067000], [-1384423866000, 372133135180800, -24857330514030000, 727848632044800000, -11728977435138600000, 117339159519533952000, -783401860847777371200, 3654352703663101440000, -12263364009096700500000, 30121928988527376000000, -54558343880470209780000, 72720410752415168870400, -70409051543137015800000, 48142941226076592000000, -22027500987368499000000, 6049545098753157120000, -753830033789944188000], [613101997800, -165537539406000, 11100752642520000, -326170262829600000, 5272370630081100000, -52892422160973595200, 354015418167362952000, -1655137020003255360000, 5565847995255512250000, -13697025107665828500000, 24851882355348879230400, -33177973900974346080000, 32171029219823375700000, -22027500987368499000000, 10091416708498869000000, -2774765838662800128000, 346146444087219270000], [-163493866080, 44316454993920, -2982128117299200, 87894302404608000, -1424711708039692800, 14328529177999196160, -96120549902411274240, 450325202737117593600, -1517208935002984080000, 3740200989399948902400, -6797096028813368678400, 9087761081682520473600, -8824053728865840192000, 6049545098753157120000, -2774765838662800128000, 763806510427609497600, -95382575704033754400], [19835652870, -5395297580640, 364182586693200, -10763618673376800, 174908803442373000, -1763080738699119840, 11851820521255194480, -55630994283442749600, 187754605706619279900, -463591619028689580000, 843736746632215035600, -1129631016152221783200, 1098252376814660067000, -753830033789944188000, 346146444087219270000, -95382575704033754400, 11922821963004219300] ]) assert_array_equal(invhilbert(17, exact=True), invh17) assert_allclose(invhilbert(17), invh17.astype(float), rtol=1e-12)
def time_invhilbert(self, size): sl.invhilbert(size)
K_fro = np.linalg.cond(A, p='fro') Norm_froB = np.linalg.cond((I - np.dot(A, Be)), p='fro') A_inv = sla.inv(A) Norm_fro = np.linalg.cond((I - np.dot(A, A_inv)), p='fro') results = np.zeros((4, 3)) results[0][0] = K_fro results[0][1] = Norm_froB results[0][2] = Norm_fro A = sla.hilbert(5) K_fro = np.linalg.cond(A, p='fro') Q, R = np.linalg.qr(A) Be = B(A, I, Q, R) Norm_froB = np.linalg.cond((I - np.dot(A, Be)), p='fro') A_inv = sla.invhilbert(5) Norm_fro = np.linalg.cond((I - np.dot(A, A_inv)), p='fro') results[1][0] = K_fro results[1][1] = Norm_froB results[1][2] = Norm_fro I = np.identity(20) A = np.random.rand(20, 20) K_fro = np.linalg.cond(A, p='fro') Q, R = np.linalg.qr(A) Be = B(A, I, Q, R) Norm_froB = np.linalg.cond((I - np.dot(A, Be)), p='fro') A_inv = sla.inv(A) Norm_fro = np.linalg.cond((I - np.dot(A, A_inv)), p='fro')
# -*- coding: utf-8 -*- """ Created on Thu Oct 4 18:24:19 2018 @author: David_000 """ import numpy.linalg as nl import scipy.linalg as sl import numpy as np A = sl.hilbert(5) Ainv = sl.invhilbert(5) u, s, vh = nl.svd(A) Anorm = nl.norm(A, 2) Ainvnorm = nl.norm(Ainv, 2) smat = np.diag(s) #Choose b in the direction corresponding to the maximum eigenval for A #Choose db in the direction corresponding to the minimum eigenval for A #np.zeros(50) #b[0] = 1 #np.zeros(50) b = u[:, 0] db = u[:, -1] print() condnbr = Anorm * Ainvnorm condnbrS = s[0] / s[-1]
print('|| A^(-1) * A - I ||_F = ', '{:25.17e}'.format(sclinalg.norm(ret, 'fro'))) print('|| A^(-1) * A - I ||_1 = ', '{:25.17e}'.format(np.linalg.norm(ret, 1))) print('|| A^(-1) * A - I ||_1 = ', '{:25.17e}'.format(sclinalg.norm(ret, 1))) print('|| A^(-1) * A - I ||_1 = ', '{:25.17e}'.format(np.linalg.norm(ret, np.inf))) print('|| A^(-1) * A - I ||_inf = ', '{:25.17e}'.format(sclinalg.norm(ret, np.inf))) # rank(A) : numpyのみ rank_a = np.linalg.matrix_rank(mat_a) print('rank(A) = ', '{:25.17e}'.format(rank_a)) # cond(A) : numpyのみ h_mat = sclinalg.hilbert(3) inv_h_mat = sclinalg.invhilbert(3) print('Hilbert matrix = \n', h_mat) print('Inverse Hilbert matrix = \n', inv_h_mat) print('||H * H^(-1) - I3 = \n', sclinalg.norm(h_mat @ inv_h_mat - I3)) print('cond(H) = ', np.linalg.cond(h_mat)) print('||H||_2 * ||H^(-1)||_2 = ', np.linalg.norm(h_mat) * np.linalg.norm(inv_h_mat)) print('cond(H, 1) = ', np.linalg.cond(h_mat, 1)) print('||H||_1 * ||H^(-1)||_1 = ', np.linalg.norm(h_mat, 1) * np.linalg.norm(inv_h_mat, 1)) print('cond(H, np.inf) = ', np.linalg.cond(h_mat, 1)) print('||H||_inf * ||H^(-1)||_inf = ', np.linalg.norm(h_mat, np.inf) * np.linalg.norm(inv_h_mat, np.inf)) # det(A) p_mat = sclinalg.pascal(3)
import scipy.linalg as sci x = sci.hilbert(4) y = sci.hilbert(8) ix = sci.invhilbert(4) iy = sci.invhilbert(8) print(x, y, ix, iy) n = 5 for i in range(15): print(sci.det(sci.hilbert(n))) n = n + 1
# -*- coding: utf-8 -*- """ Created on Wed Sep 20 10:02:50 2017 @author: edadagasan """ import scipy.linalg as sl import numpy as np H = sl.hilbert(50) H_inv = sl.invhilbert(50) U,s,V = sl.svd(H) #U unitary matrix having left sing vectors as columns #s vector with sing values ordered (greatest first) S = np.diag(s) #constructing a diag matrix b = U[:,0] #first column delta_b = U[:,len(s)-1] #last column x = H_inv@b delta_x = H_inv@delta_b K = (sl.norm(delta_x, ord=2)/sl.norm(x, ord=2))/(sl.norm(delta_b, ord=2)/sl.norm(b, ord=2)) K_A = sl.norm(H, ord=2)*sl.norm(H_inv, ord=2) print(K, K_A)
from __future__ import division from scipy import * from scipy import linalg from matplotlib.pyplot import * import numpy as np beta = 1e-5 alpha = 1 size = 50 #A = random.rand(size,size) #Ainv = np.linalg.inv(A) A = linalg.hilbert(size) Ainv = linalg.invhilbert(size) u, s, v = np.linalg.svd(A) b = u[:, 0] * alpha db = u[:, -1] * beta #x = np.linalg.solve(A,b) #xh = np.linalg.solve(A,b + db) x = np.dot(Ainv, b) xh = np.dot(Ainv, b + db) #dx = np.dot(Ainv, db) dx = xh - x k = np.linalg.norm(Ainv, ord=2) * np.linalg.norm(A, ord=2)