def jacobi_method(A, b, iterations=5, startvector=None, w=1, threshold=0.1, full_output=False): if isinstance(A, SparseMatrix): Dinvers_content = {key: 1/A[key] for key in A.content.keys() if key[0] == key[1]} D_invers = SparseMatrix([], [], [], shape=A.shape) D_invers.content = Dinvers_content Diw = D_invers * w else: Dinvers = np.zeros(A.shape) np.fill_diagonal(Dinvers, 1/4) Diw = Dinvers * w if startvector is not None: x_old = startvector else: x_old = np.zeros(b.shape) if full_output: residua = [] for k in range(iterations): # Vorlesung 21.04 S. 4 residuum = (b - (A @ x_old)) if full_output: residua.append(np.linalg.norm(residuum)) x_new = x_old + Diw @ residuum if residuum.max() < threshold and threshold > 0: print(residuum.max(), k) break x_old = x_new if full_output: return x_new, residua return x_new
def test_transposed(self): a = SparseMatrix([1, 1, 1], [0, 1, 2], [0, 1, 2]) self.assertEqual(a, a.T) b = SparseMatrix([1, 1, 1], [0, 0, 0], [0, 1, 2]) c = SparseMatrix([1, 1, 1], [0, 1, 2], [0, 0, 0]) self.assertEqual(b, c.T)
def test_mul(self): a = SparseMatrix([1, 2, 3], [0, 1, 0], [1, 2, 0]) b = 2 c = SparseMatrix([2, 4, 6], [0, 1, 0], [1, 2, 0]) self.assertEqual(a * b, c) print(b * a) self.assertEqual(b * a, c)
def test_radd(self): a = SparseMatrix([1, 1, 1], [0, 1, 2], [0, 1, 2], shape=(3, 3)) b = SparseMatrix([1, 1, 1], [1, 1, 1], [0, 1, 2], shape=(3, 3)) c = SparseMatrix([1, 1, 2, 1, 1], [0, 1, 1, 1, 2], [0, 0, 1, 2, 2]) self.assertEqual(a, 0 + a) d = sum([a, b]) self.assertEqual(c, d)
def test_jacobi(self): A = SparseMatrix([4, -2, 1, -1, 5, -2, 1, 1, 3], [0, 0, 0, 1, 1, 1, 2, 2, 2], [0, 1, 2, 0, 1, 2, 0, 1, 2]) b = SparseMatrix([2, 4, 6], [0, 1, 2], [0, 0, 0]) x0 = SparseMatrix([1, 2, 1], [0, 1, 2], [0, 0, 0]) x1 = SparseMatrix([0.25, 1.4, 1], [0, 1, 2], [0, 0, 0]) res = jacobi_method(A, b, iterations=1, startvector=x0, w=1) print(res) self.assertEqual(x1, res)
def question2(): first_image1 = np.array(MNIST_DATASET[0].tolist()) n, m, coord = array_to_coord(first_image1) mat = SparseMatrix(fromiter=coord, shape=(n, m)) first_image2 = mat.todense() for i in range(n): for j in range(m): if not first_image1[i, j] == first_image2[i, j]: print('Erreur, les images sont differentes') return None print('Les deux images sont identiques') return None
def SOR(A, b, iterations=5, startvector=None, w=1/2, threshold=0.1, full_output=False): # D = {key: A[key] for key in A.content.keys() if key[0] == key[1]} # R = {key: A[key] for key in A.content.keys() if key[0] < key[1]} # L = {key: A[key] for key in A.content.keys() if key[0] > key[1]} # I = {key: 1 for key in A.content.keys() if key[0] == key[1]} # for i in 1 ... laenge x # berechne si # berechne xi # Gauß-Seidel: -> berechne die si mit den geupdateten xi (in jacobi würde man ERST ALLE si berechnen) # für k+1: s_i = A_zeile_i * x^k # für k+1 für die i-te komponente: x^k+1_i = x^k_i - w/(A_i,i) * (s_i - b_i) # Jacobi si, si, si, ..., xi, xi, xi, ... # Gauß-Seidel si, xi, si, xi, ... # SOR -> 1/(A-i,i) wird zu w/(A_i,i) # aus dem video x_old = startvector or SparseMatrix([], [], [], shape=b.shape) x_new = startvector or SparseMatrix([], [], [], shape=b.shape) if full_output: residua = [] for k in range(iterations): for i in range(b.shape[0]): #A_i_content = {key: A[key] for key in A.content.keys() if key[0] == i} A_i_content = {(0, key[1]): A[key] for key in A.content.keys() if key[0] == i}# hier war der fehler A_i = SparseMatrix([], [], [], shape=(1, A.shape[1])) A_i.content = A_i_content # print("Ai ", A_i) s_i = (A_i @ x_old) wert = s_i[0, 0] # print(A_i, " @ ", x_old, "=", s_i) # print("s", i, " = ", wert) x_new[i, 0] = x_old[i, 0] - w/(A[i,i]) * (wert - b[i, 0]) x_old = 1 * x_new # TODO: copy methode implementieren residuum = (b - (A @ x_old)) if full_output: residua.append(max(residuum.content.values())) if max(residuum.content.values()) < threshold: break # eine Iteration, ein mal den Lösungsvektor # if ...: # break # k Iterations if full_output: return x_new, residua return x_old
def test_matmul(self): # test shape a = SparseMatrix([], [], [], shape=(3, 5)) b = SparseMatrix([], [], [], shape=(5, 7)) self.assertEqual((3, 7), (a @ b).shape) #example from Wikipedia a = SparseMatrix([3, 2, 1, 1, 2], [0, 0, 0, 1, 1], [0, 1, 2, 0, 2]) b = SparseMatrix([1, 2, 1, 4], [0, 0, 1, 2], [0, 1, 1, 0]) c = SparseMatrix([7, 8, 9, 2], [0, 0, 1, 1], [0, 1, 0, 1]) self.assertEqual(c, a @ b)
def test_add(self): a = SparseMatrix([1, 1, 1], [0, 1, 2], [0, 1, 2], shape=(3, 3)) b = SparseMatrix([1, 1, 1], [1, 1, 1], [0, 1, 2], shape=(3, 3)) c = SparseMatrix([1, 1, 2, 1, 1], [0, 1, 1, 1, 2], [0, 0, 1, 2, 2]) d = SparseMatrix([1, -1, -1, 1], [0, 1, 1, 2], [0, 0, 2, 2], shape=(3, 3)) self.assertEqual(c, a + b) self.assertEqual(d, a - b) e = SparseMatrix([], [], [], shape=(10, 10)) self.assertRaises(ArithmeticError, a.__add__, e)
def test_shape_parameter(self): values = [1, 2] row = [0, 1] col = [0, 1] a = SparseMatrix(values, row, col) b = SparseMatrix(values, row, col, shape=(3, 4)) c = SparseMatrix([1, 1], [12, 23], [7, 9]) self.assertEqual(a.shape, (2, 2)) self.assertEqual(b.shape, (3, 4)) self.assertRaises(IndexError, a.__getitem__, (2, 3)) self.assertEqual(b[2, 3], 0) self.assertEqual(c.shape, (24, 10))
def test_equals(self): a = SparseMatrix([1, 1, 1], [0, 1, 2], [0, 1, 2]) b = SparseMatrix([1, 1, 1], [2, 1, 0], [2, 1, 0]) c = SparseMatrix([1, 1, 1], [2, 0, 1], [2, 0, 1]) d = SparseMatrix([1, 1, 1, 0], [0, 1, 2, 1], [0, 1, 2, 2]) e = SparseMatrix([1, 1, 1], [0, 1, 2], [0, 1, 2], shape=(4, 4)) self.assertEqual(a, b) self.assertEqual(a, c) self.assertEqual(b, c) self.assertEqual(a, d) self.assertNotEqual(a, e)
def test_non_zero_indexing(self): values = [1, 2, 3, 4] row = [1, 2, 3, 4] col = [1, 2, 3, 4] mat = SparseMatrix(values, row, col) self.assertEqual(mat[1, 1], 1) self.assertEqual(mat[2, 2], 2) self.assertEqual(mat[3, 3], 3) self.assertEqual(mat[4, 4], 4)
def test_zero_indexing(self): values = [1, 2, 3, 4] row = [1, 2, 3, 4] col = [1, 2, 3, 4] mat = SparseMatrix(values, row, col) self.assertEqual(mat[0, 0], 0) self.assertEqual(mat[1, 2], 0) self.assertEqual(mat[3, 4], 0) self.assertEqual(mat[4, 3], 0)
def test_out_of_range_indexing(self): values = [1, 2, 3, 4] row = [1, 2, 3, 4] col = [1, 2, 3, 4] mat = SparseMatrix(values, row, col) self.assertRaises(IndexError, mat.__getitem__, (0, -1)) self.assertRaises(IndexError, mat.__getitem__, (-1, 0)) self.assertRaises(IndexError, mat.__getitem__, (0, 5)) self.assertRaises(IndexError, mat.__getitem__, (5, 0))
def test_setitem(self): a = SparseMatrix([1, 1, 1], [0, 1, 2], [0, 1, 2]) self.assertEqual(a[0, 0], 1) a[0, 0] = 5 self.assertEqual(a[0, 0], 5) self.assertEqual(a[0, 2], 0) a[0, 2] = 5 self.assertEqual(a[0, 2], 5) self.assertRaises(IndexError, a.__setitem__, (0, 3), 5)
def calc_pmi(X_Y_C): P = SparseMatrix() X_C, Y_C = Counter(), Counter() x_y_sum = 0 for x, x_Y_C in X_Y_C.items(): for y, c in x_Y_C.items(): X_C[x] += c Y_C[y] += c x_y_sum += c for x, x_Y_C in X_Y_C.items(): for y, c in x_Y_C.items(): P[x, y] = log2((c * x_y_sum) / (X_C[x] * Y_C[y])) return P
# k Iterations if full_output: return x_new, residua return x_old if __name__ == '__main__': ## example from the english wikipedia # https://en.wikipedia.org/wiki/Jacobi_method # A = SparseMatrix([2, 1, 5, 7], [0, 0, 1, 1], [0, 1, 0, 1]) # b = SparseMatrix([11, 13], [0, 1], [0, 0]) # x0 = SparseMatrix([1, 1], [0, 1], [0, 0]) A = SparseMatrix([4, -2, 1, -1, 5, -2, 1, 1, 3], [0, 0, 0, 1, 1, 1, 2, 2, 2], [0, 1, 2, 0, 1, 2, 0, 1, 2]) b = SparseMatrix([2, 4, 6], [0, 1, 2], [0, 0, 0]) x0 = SparseMatrix([1, 2, 1], [0, 1, 2], [0, 0, 0]) x_jac1 = SparseMatrix([1.25, 1.4, 1], [0, 1, 2], [0, 0, 0]) x_gasei1 = SparseMatrix([1.25, 1.45, 1.1], [0, 1, 2], [0, 0, 0]) print("--- JACOBI ---") # res = jacobi_method(A, b, iterations=1, w=1) # print("1 iterations: ", res) # # print("EXPECTED: 1 iterations: ", x_jac1) # res = jacobi_method(A, b, iterations=2, w=1) # print("2 iterations: ", res) res, residua = jacobi_method(A, b, iterations=25, w=1, full_output=True)
def _new(cls, *args, **kwargs): s = SparseMatrix(*args) rows = Integer(s.rows) cols = Integer(s.cols) mat = Dict(s._smat) return Basic.__new__(cls, rows, cols, mat)
from matplotlib import pyplot as plt #%% load data # a_ij = (np.loadtxt("./test_data/a_ij.dat")).tolist() # i_index = (np.loadtxt("./test_data/i.dat", dtype=int)).tolist() # j_index = (np.loadtxt("./test_data/j.dat", dtype=int)).tolist() # b_vector = (np.loadtxt("./test_data/b.dat")).tolist() # %% generate sparse matrizes # A = SparseMatrix(a_ij, i_index, j_index) # b = SparseMatrix(b_vector, list(range(A.shape[0])), [0 for i in range(A.shape[1])]) #%% A = SparseMatrix([4, -2, 1, -1, 5, -2, 1, 1, 3], [0, 0, 0, 1, 1, 1, 2, 2, 2], [0, 1, 2, 0, 1, 2, 0, 1, 2]) b = SparseMatrix([2, 4, 6], [0, 1, 2], [0, 0, 0]) #x0 = SparseMatrix([1, 2, 1], [0, 1, 2], [0, 0, 0]) # %% run linsolve Methods th = 0.0001 max_its = 10 print('jacobi') jacobi_res, jacobi_resids = jacobi_method(A, b, threshold=th, iterations=max_its, full_output=True) print('gaus seidel')
import numpy as np import math as math import matplotlib.pyplot as plt from sparse import SparseMatrix %matplotlib inline # Importation des donnes MNIST mnist_dataset = np.memmap('train-images-idx3-ubyte', offset=16, shape=(60000, 28, 28)) premiere_image = mnist_dataset[0].tolist() # first_image est de taille (28, 28) encodage_premiere_image = SparseMatrix(premiere_image,(28,28)) premiere_image_encode_decode = encodage_premiere_image.todense() plt.imshow(premiere_image, cmap='gray_r') plt.show() plt.imshow(encodage_premiere_image.todense(), cmap='gray_r') plt.show() # Comparaison Pixel par Pixel pas_pareil = False for i in range(len(premiere_image)): for j in range(len(premiere_image[i])): if premiere_image[i][j] != premiere_image_encode_decode[i][j]: print(premiere_image[i][j]) print(premiere_image_encode_decode[i][j]) print(i) print(j) print("\n") pas_pareil = True