向量范数与矩阵范数norm 向量范数与矩阵范数

向量范数与矩阵范数

要更好的理解范数,就要从函数、几何与矩阵的角度去理解。
我们都知道,函数与几何图形往往是有对应的关系,这个很好想象,特别是在三维以下的空间内,函数是几何图像的数学概括,而几何图像是函数的高度形象化,比如一个函数对应几何空间上若干点组成的图形。
但当函数与几何超出三维空间时,就难以获得较好的想象,于是就有了映射的概念,映射表达的就是一个集合通过某种关系转为另外一个集合。通常数学书是先说映射,然后再讨论函数,这是因为函数是映射的一个特例。
为了更好的在数学上表达这种映射关系,(这里特指线性关系)于是就引进了矩阵。这里的矩阵就是表征上述空间映射的线性关系。而通过向量来表示上述映射中所说的这个集合,而我们通常所说的基,就是这个集合的最一般关系。于是,我们可以这样理解,一个集合(向量),通过一种映射关系(矩阵),得到另外一个几何(另外一个向量)。
那么向量的范数,就是表示这个原有集合的大小。
而矩阵的范数,就是表示这个变化过程的大小的一个度量。

总结起来一句话,范数(norm),是具有“长度”概念的函数。

2.范数满足的三个特性

1.非负性: ||x||0||x||≥0

3.向量的范数

1-范数,计算方式为向量所有元素的绝对值之和。

||x||1=in|xi|||x||1=∑in|xi|

4.矩阵的范数

首先假设矩阵的大小为mnm∗n,即m行n列。

1-范数,又名列和范数。顾名思义,即矩阵列向量中绝对值之和的最大值。

||A||1=maxji=1m|aij|||A||1=maxj∑i=1m|aij|
的最大特征值。

F-范数,Frobenius范数,计算方式为矩阵元素的绝对值的平方和再开方。

||A||F=(i=1mj=1n|aij|2)12||A||F=(∑i=1m∑j=1n|aij|2)12

5.在python里计算范数

numpy包里的linalg模块,是专门处理基本线性代数问题的模块。借助该模块中的norm()函数可以轻松计算向量与矩阵的范数。

先看看norm()方法的原型:

def norm(x, ord=None, axis=None, keepdims=False):
    Matrix or vector norm.

    This function is able to return one of eight different matrix norms,
    or one of an infinite number of vector norms (described below), depending
    on the value of the ``ord`` parameter.

  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

再看看更为详细的计算说明:

    The following norms can be calculated:

    =====  ============================  ==========================
    ord    norm for matrices             norm for vectors
    =====  ============================  ==========================
    None   Frobenius norm                2-norm
    'fro'  Frobenius norm                --
    'nuc'  nuclear norm                  --
    inf    max(sum(abs(x), axis=1))      max(abs(x))
    -inf   min(sum(abs(x), axis=1))      min(abs(x))
    0      --                            sum(x != 0)
    1      max(sum(abs(x), axis=0))      as below
    -1     min(sum(abs(x), axis=0))      as below
    2      2-norm (largest sing. value)  as below
    -2     smallest singular value       as below
    other  --                            sum(abs(x)**ord)**(1./ord)
    =====  ============================  ==========================
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

看到上面这个表以后,同学们应该特别清楚了吧。

6.上代码

看了上面的说明以后,必须亲自动手写写代码,验证一下。前面全是理论,最后自然要实践一把。talk is cheap, show me the code!

#!/usr/bin/env python
#coding:utf-8

'''
Created on 2016年7月17日

@author: lei.wang
'''

import numpy as np
import numpy.linalg as LA

def compute_norm():
    mat = np.matrix([[1,2],[3,4]])
    inv_mat = np.linalg.inv(mat)
    print inv_mat

def vector_norm():
    a = np.arange(9) - 4
    print LA.norm(a,np.inf) #无穷范数
    print LA.norm(a,-np.inf)
    print LA.norm(a,1) #1范数
    print LA.norm(a,2) #2范数

def matrix_norm():
    a = np.arange(9) - 4
    b = a.reshape(3,3)
    b_t = np.transpose(b)
    b_new = np.dot(b_t,b) #b_new矩阵为b^t * b
    x = np.linalg.eigvals(b_new) #求b_new矩阵的特征值
    print x
    print LA.norm(b,1) #列范数
    print LA.norm(b,2) #谱范数,为x里最大值开平方
    print LA.norm(b,np.inf) #无穷范数,行范数
    print LA.norm(b,"fro") #F范数

vector_norm()
print 
matrix_norm()
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

让代码run起来:

4.0
0.0
20.0
7.74596669241

[  5.40000000e+01   6.00000000e+00   5.99792822e-16]
7.0
7.34846922835
9.0
7.74596669241
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

注释已经写得很详细,相信大家都已经看明白。

猜你喜欢

转载自blog.csdn.net/m0_37192554/article/details/85277437