python--实现任意向量和矩阵的范数计算

由矩阵范数计算公式:

python--实现任意向量和矩阵的范数计算_第1张图片

并计算满足以下不等式的n值:

python--实现任意向量和矩阵的范数计算_第2张图片

代码如下:

import numpy as np
import math

# calculate norm of Vector X , p=1,2,∞
def norm_x(x,p):
    n = x.shape[0] #维数
    if(p==np.inf):
        nvinf = max(abs(x))
        res = nvinf

    else:
        ssum = 0
        for i in range(n):
            ssum = ssum + pow(abs(x[i]), p)
        res = pow(ssum, 1 / p)
    return res



#A--matrix
def norm1_A(A):
    row = A.shape[0]
    column = A.shape[1]
    tmp = A[1,1]
    for j in range(column):
        s =0
        for i in range(row):
            s += abs(A[i,j])
        if(s>tmp):
            tmp = s

    return tmp

def normE_A(A):
    row = A.shape[0]
    column = A.shape[1]
    s=0
    for i in range(row):
        for j in range(column):
            s += pow(A[i,j],2)

    return np.sqrt(s)

def normInf_A(A):
    row = A.shape[0]
    column = A.shape[1]
    tmp = A[1, 1]
    for i in range(row):
        s = 0
        for j in range(column):
            s += abs(A[i,j])
        if (s > tmp):
            tmp = s
    return tmp

#......Чтобы найти n, удовлетворяющее условию, я написал три функции, которые передают аргумент n.
def norm_1(A, n):
    tmp_1 = 0
    max_1 = np.sum(abs(A[0:n, 0]))
    for j in range(1, n):
        # print(max_1)
        tmp_1 = np.sum(abs(A[0:n, j]))
        if max_1 < tmp_1:
            max_1 = tmp_1
        tmp_1 = 0
    return max_1


def norm_E(A, n):
    A = np.multiply(A, A)
    return math.sqrt(np.sum(abs(A[0:n, 0:n])))


def norm_inf(A, n):
    tmp_inf = 0
    max_inf = np.sum(abs(A[0, 0:n]))
    for i in range(1, n):
        # print(max_inf)
        tmp_inf = np.sum(abs(A[i, 0:n]))
        if max_inf < tmp_inf:
            max_inf = tmp_inf
        tmp_inf = 0
    return max_inf


def findout(A):
    n = A.shape[0]
    N = []
    #Проверьте, что для каждого n неравенство выполняется.
    for i in range(1, n+1):
        Norm_1 = norm_1(A, i)
        Norm_E = norm_E(A, i)
        Norm_inf = norm_inf(A, i)
        # print("Norm_1: %.4f" % Norm_1)
        # print("Norm_E: %.4f" % Norm_E)
        # print("Norm_inf: %.4f" % Norm_inf)
        # print("Norm_1/sqrt({}): %.4f".format(i) % (Norm_1 / math.sqrt(i)))
        # print("Norm_1*sqrt({}): %.4f".format(i) % (Norm_1 * math.sqrt(i)))
        # print("Norm_E: %.4f" % Norm_E)
        # print("Norm_inf/sqrt({}): %.4f".format(i) % (Norm_inf / math.sqrt(i)))
        # print("Norm_inf*sqrt({}): %.4f".format(i) % (Norm_inf * math.sqrt(i)))
        if (Norm_1 / math.sqrt(i) <= Norm_E and Norm_1 * math.sqrt(i) >= Norm_E) and (Norm_inf / math.sqrt(i) <= Norm_E and Norm_inf * math.sqrt(i) >= Norm_E):
            N.append(i)
    return N




if __name__ == '__main__' :
    x = np.array([3, 0, -4])
    #Используйте библиотечные функции для проверки правильности вычисления результатов пользовательских функций
    nv1 = np.linalg.norm(x, ord = 1)
    nv2 = np.linalg.norm(x, ord = 2)
    nvinf = np.linalg.norm(x, ord = np.inf)

    print("||x||1 = ", norm_x(x, 1))
    print("||x||2 = ", norm_x(x, 2))
    print("||x||∞ = ", norm_x(x,np.inf))
    print("checkX : ", nv1,nv2,nvinf)

    A = np.mat([[1, 2, 3], [-4, -5, -6], [7, 8, 9]])
    n1 = np.linalg.norm(A, ord=1)
    nE = np.linalg.norm(A, ord='fro')
    ninf = np.linalg.norm(A, ord=np.inf)
    print("||A||1 = ", norm1_A(A))
    print("check: ", n1)
    print("||A||E = ", normE_A(A))
    print("check: ", nE)
    print("||A||∞ = ", normInf_A(A))
    print("check: ", ninf)

#Выведите все значения n, удовлетворяющие неравенству
    print("n = ", findout(A))




 

 

你可能感兴趣的:(python,python,矩阵,开发语言)