Beispiel #1
0
def method_jacobi_rotations(a, eps):
    n_iter = 0
    a_k = copy.copy(a)
    v = Matrix(a.size, single=True)

    while True:
        i_max, j_max = find_max(a_k)
        fi = 0.5 * math.atan(2 * a_k[i_max][j_max] /
                             (a_k[i_max][i_max] - a_k[j_max][j_max]))

        u = Matrix(a.size, single=True)
        u[i_max][i_max] = math.cos(fi)
        u[i_max][j_max] = -math.sin(fi)
        u[j_max][i_max] = math.sin(fi)
        u[j_max][j_max] = math.cos(fi)

        u_t = copy.copy(u)
        u_t.transpose()

        a_k = u_t * a_k * u
        v = v * u

        n_iter += 1

        if t(a_k) < eps:
            eigenvalue = Vector(a_k.size)
            eigenvalue.vector = [a_k[i][i] for i in range(0, a_k.size)]

            print('Итераций: ', n_iter)
            return eigenvalue, v
 def feed_forward(self,x):
     output_data = Matrix(x).transpose()
     self.activations =[]
     for i in range(len(self.theta)):
         self.activations.append(output_data)
         input_data = Matrix(output_data.matrix.copy()).transpose()
         for j in range(len(input_data.matrix)):
             input_data.matrix[j] = [1] + input_data.matrix[j]
         output_data = self.sigmoid(self.theta[i] * input_data.transpose())
     self.activations.append(output_data)
     return output_data
Beispiel #3
0
def lu(a):
    size = a.size

    l = Matrix(size, single=True)
    u = copy.deepcopy(a)

    for k in range(0, size - 1):
        m_k = Matrix(size, single=True)
        for i in range(k + 1, size):
            mu_i = -u[i][k] / u[k][k]

            m_k[i][k] = mu_i
            l[i][k] = -mu_i
        u = m_k * u
    return l, u
def t(a, b, n):
    a11 = (2**(-n - 1)) * (a * (b**n) + b * ((-a)**n)) / sqrt(5)
    a12 = (2**(-n)) * (b**n - ((-a)**n)) / sqrt(5)
    a21 = ((2**(-n - 2)) * a * b) * (b**n - ((-a)**n)) / sqrt(5)
    a22 = (2**(-n - 1)) * ((b**(n + 1)) + ((-a)**(n + 1))) / sqrt(5)

    T = Matrix([[int(a11), int(a12)], [int(a21), int(a22)]])
    return T
Beispiel #5
0
def lup(matrix):
    size = matrix.size

    p = get_matrix_permutations(matrix)
    pa = p * matrix
    l = Matrix(size, single=True)
    u = copy.deepcopy(pa)

    for k in range(0, size - 1):
        m_k = Matrix(size, single=True)
        for i in range(k + 1, size):
            mu_i = -u[i][k] / u[k][k]

            m_k[i][k] = mu_i
            l[i][k] = -mu_i
        u = m_k * u
    return l, u, p
Beispiel #6
0
def get_matrix_permutations(matrix):
    size = matrix.size
    p = Matrix(size, single=True)
    for i in range(0, size):
        column = [matrix[j][i] for j in range(i, size)]
        row_idx = column.index(max(column, key=abs)) + i
        if i != row_idx:
            p[i], p[row_idx] = p[row_idx], p[i]
    return p
Beispiel #7
0
def main(argv):
    assert len(argv) == 2, "Vous devez donner le nombre de points"
    points = int(argv[1])
    assert points > 0, "Le nombre de points doit être > 0"

    delta = time.perf_counter()
    m = Matrix()
    m.fill_matrix(points)
    delta = time.perf_counter() - delta
    perf_stats(m.approx_pi(), points, delta)
Beispiel #8
0
def get_matrix_householder(matrix, col):
    v = Vector(matrix.size)
    e = Matrix(matrix.size, single=True)

    sign = -1 if matrix[col][col] < 0 else 1 if matrix[col][col] > 0 else 0
    v[col] = matrix[col][col] + sign * math.sqrt(
        sum([matrix[j][col]**2 for j in range(col, matrix.size)]))

    for i in range(col + 1, matrix.size):
        v[i] = matrix[i][col]

    v_vt = Matrix(matrix.size)
    for i in range(0, v_vt.size):
        for j in range(0, v_vt.size):
            v_vt[i][j] = v[i] * v[j]

    vt_v = sum([v[i]**2 for i in range(0, v.size)])

    h = e - v_vt * (2 / vt_v)
    return h
Beispiel #9
0
def get_alpha_beta(a, b):
    size = a.size
    alpha = Matrix(size)
    beta = Vector(size)

    for i in range(0, size):
        beta[i] = b[i] / a[i][i]
        for j in range(0, size):
            alpha[i][j] = -a[i][j] / a[i][i] if i != j else 0

    return alpha, beta
Beispiel #10
0
def qr(matrix):
    q = Matrix(matrix.size, single=True)
    r = copy.copy(matrix)

    for i in range(0, matrix.size - 1):
        h = get_matrix_householder(r, i)

        q = q * h
        r = h * r

    return q, r
Beispiel #11
0
def inverse_matrix(l, u, p):
    size = l.size
    inv = Matrix(size)

    for i in range(0, size):
        e = Vector(size)
        e[i] = 1
        column = lup_solve(l, u, p, e)

        for j in range(0, size):
            inv[j][i] = column[j]
    return inv
    def back_propagation(self, x , y , lambda_r=0):
        Delta = [[[0 for i in range(len(self.activations[l].matrix))] for j in range(len(self.activations[l+1].matrix))] for l in range(len(self.activations)-1)]
        for m in range(len(x)):
            deltas = []
            self.feed_forward([x[m]])
            deltas.append((self.activations[-1]-Matrix([y[m]]))) 

            for l in range(len(self.theta)-1, 0, -1):
                deltas.insert(0,Matrix((self.theta[l].transpose()*deltas[0]).matrix[1:][:]).pw_prod(self.activations[l]).pw_prod(Matrix([1]*len(self.activations[l].matrix))-self.activations[l]))
         
            for l in range(len(self.activations)-1):
                for j in range(len(self.activations[l+1].matrix)):
                    for i in range(len(self.activations[l].matrix)):
                        Delta[l][j][i] += (self.activations[l].matrix[i][0]*deltas[l].matrix[j][0])
        
        D = [[[0 for i in range(len(self.activations[l].matrix))] for j in range(len(self.activations[l+1].matrix))] for l in range(len(self.activations)-1)]
        for l in range(len(self.activations)-1):
            for j in range(len(self.activations[l+1].matrix)):
                for i in range(len(self.activations[l].matrix)):
                    if j==0:
                        D[l][j][i] = (1/len(x))*Delta[l][j][i]
                    else:
                        D[l][j][i] = (1/len(x))*Delta[l][j][i] +lambda_r * self.theta[l].matrix[j][i]
        return D
Beispiel #13
0
def get_poly_1_deg(points, values):
    sum_points = sum(points)
    sum_points_2 = sum([point**2 for point in points])
    sum_values = sum(values)
    sum_v_p = sum([values[i] * points[i] for i in range(0, len(points))])

    a = Matrix(2)
    a.matrix = [[len(points), sum_points], [sum_points, sum_points_2]]
    b = Vector(2)
    b.vector = [sum_values, sum_v_p]

    l, u, p = lup(a)
    coefficients = lup_solve(l, u, p, b).vector

    return parse_expr(f'{coefficients[0]} + {coefficients[1]} * x')
Beispiel #14
0
def get_matrix_system(points, h, lambda_, k):
    size_p = len(points)
    A = Matrix(size_p)

    for i in range(size_p):
        for j in range(size_p):
            if i == j:
                if j == 0 or j == size_p - 1:
                    A[i][j] = 1 - lambda_ * h / 2 * k(points[i], points[j])
                else:
                    A[i][j] = 1 - lambda_ * h * k(points[i], points[j])
            elif j == 0 or j == size_p - 1:
                A[i][j] = -lambda_ * h / 2 * k(points[i], points[j])
            else:
                A[i][j] = -lambda_ * h * k(points[i], points[j])
    return A
Beispiel #15
0
def main(argv):
    width = int(argv[1])
    points = int(argv[2])
    rounding = int(argv[3])

    m = Matrix(width)
    steps = points // 10

    delta = time.perf_counter()

    fnames = []
    for n, p in enumerate(range(steps, points + steps, steps)):
        m.fill_matrix(steps)
        fname = generate_ppm_file(m, rounding, n)
        fnames.append(fname)

    print("Generating GIF pi.gif")
    subprocess.call(["python", "convert.py"] + fnames + ["pi.gif"])

    delta = time.perf_counter() - delta
    perf_stats(m.approx_pi(), points, delta)
Beispiel #16
0
def get_poly_2_deg(points, values):
    sum_points = sum(points)
    sum_points_2 = sum([point**2 for point in points])
    sum_points_3 = sum([point**3 for point in points])
    sum_points_4 = sum([point**4 for point in points])
    sum_values = sum(values)
    sum_v_p = sum([values[i] * points[i] for i in range(0, len(points))])
    sum_v_p2 = sum([values[i] * points[i]**2 for i in range(0, len(points))])

    a = Matrix(3)
    a.matrix = [[len(points), sum_points, sum_points_2],
                [sum_points, sum_points_2, sum_points_3],
                [sum_points_2, sum_points_3, sum_points_4]]
    b = Vector(3)
    b.vector = [sum_values, sum_v_p, sum_v_p2]

    l, u, p = lup(a)
    coefficients = lup_solve(l, u, p, b).vector

    return parse_expr(
        f'{coefficients[0]} + {coefficients[1]} * x + {coefficients[2]} * x ** 2'
    )
 def init_weights(self):
     self.theta = []
     layers_range = [self.size[0]]+ self.hidden_size + [self.size[-1]]
     for i in range(len(layers_range)-1):
         self.theta.append(Matrix([[random() for m in range(layers_range[i] + 1)] for n in range(layers_range[i+1])]))
Beispiel #18
0
        vectors[i].print_vector()
        print('A * x =  ', end='')
        (matrix * vectors[i]).print_vector()
        print('a_k * x =  ', end='')
        (vectors[i] * eigenvalue[i]).print_vector()
        print('-------------------------------------------------------------')


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--input', required=True)
    args = parser.parse_args()

    with open(args.input, 'r') as f:
        data = json.load(f)
        n = int(data['size'])
        Eps = float(data['eps'])

    A = Matrix(n)
    A.matrix_read_file(args.input, 'matrix')

    Eigenvalue, V = method_jacobi_rotations(A, Eps)

    print('Собственные значения: ')
    for i in range(0, n):
        print('a_{0} = {1:8.5f}'.format(i + 1, Eigenvalue[i]))
    print('\nМатрица собственных векторов: ')
    V.print_matrix()

    test(Eigenvalue, V, A)
Beispiel #19
0
handler = logging.StreamHandler()
logger.addHandler(handler)

cf_m = analysis.recognition.CharacterClassifierManager(lbls.lbls)
cf = cf_m.get_classifier()

img = PixelMatrix(Image.open('tmp/1-0_f.bmp').convert('1'))

liste_taux = cf.predire(img)
for taux in liste_taux:
    print(taux)

# [DEBUG] Image de correpondance des points

for i in range(3):
    matriceCorrespondances = Matrix(28, 28)
    taux, lettre = liste_taux[i]

    matricePoids = cf.matricesPoids[lettre]
    threshold = -math.ceil(matricePoids.sample_count * 0.95)
    for index, poids in enumerate(matricePoids):
        if img[index] == 0:
            matriceCorrespondances[index] = poids
        elif poids > threshold:
            matriceCorrespondances[index] = -poids
        else:
            matriceCorrespondances[index] = 0

    matriceCorrespondances_np = np.array(matriceCorrespondances._matrix)
    fig, ax = plt.subplots()