欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

线性代数(3)——矩阵基础

程序员文章站 2022-07-12 14:11:26
...

概述

向量是对数的拓展,一个向量表示一组数;而矩阵则可以视为对向量的拓展,一个矩阵表示一组向量。

看待一个矩阵有两个视角,行向量视角和列向量视角。

当行数和列数相等时候,称为方阵,方阵有很多特殊的性质。有很多特殊的性质的矩阵,是方阵。

实现矩阵类

from vector import Vector


class Matrix:
	def __init__(self, list2d):
		self._value = list2d.copy()

	def __repr__(self):
		return "Matrix({})".format(self._value)

	# 此处简单设置交互式与print模式打印的内容相同
	__str__ = __repr__

	def shape(self):
		return len(self._value), len(self._value[0])

	def row_num(self):
		"""返回矩阵行数"""
		return self.shape()[0]

	def col_num(self):
		"""返回矩阵列数"""
		return self.shape()[1]

	def size(self):
		"""矩阵元素个数"""
		r, c = self.shape()
		return r * c

	def __getitem__(self, pos):
		"""返回指定未知数的元素,pos的形式是元组"""
		assert pos[0] < self.row_num() and pos[1] < self.col_num
		r, c = pos
		return self._value[r][c]

	def row_vector(self, idx):
		"""返回第idx个行向量"""
		return Vector(self._value[idx])

	def col_vector(self, idx):
		"""返回第idx个列向量"""
		return Vector([row[idx] for row in self._value)
		

矩阵基本运算和性质

矩阵加法

两个同形矩阵加法,
A=(a11a12...a1ca21a22...a2c............ar1ar2...arc)B=(b11b12...b1cb21b22...b2c............br1br2...brc) A=\begin{pmatrix}a_{11}&amp;a_{12}&amp;...&amp;a_{1c}\\a_{21}&amp;a_{22}&amp;...&amp;a_{2c}\\...&amp;...&amp;...&amp;...\\a_{r1}&amp;a_{r2}&amp;...&amp;a_{rc}\end{pmatrix}B=\begin{pmatrix}b_{11}&amp;b_{12}&amp;...&amp;b_{1c}\\b_{21}&amp;b_{22}&amp;...&amp;b_{2c}\\...&amp;...&amp;...&amp;...\\b_{r1}&amp;b_{r2}&amp;...&amp;b_{rc}\end{pmatrix}
A+B=(a11+b11a12+b12...a1c+b1ca21+b21a22+b22...a2c+b2c............ar1+br1ar2+br2...arc+brc) A+B=\begin{pmatrix}a_{11}+b_{11}&amp;a_{12}+b_{12}&amp;...&amp;a_{1c}+b_{1c}\\a_{21}+b_{21}&amp;a_{22}+b_{22}&amp;...&amp;a_{2c}+b_{2c}\\...&amp;...&amp;...&amp;...\\a_{r1}+b_{r1}&amp;a_{r2}+b_{r2}&amp;...&amp;a_{rc}+b_{rc}\end{pmatrix}

矩阵数乘

一个实数与一个矩阵的乘法运算,
A=(a11a12...a1ca21a22...a2c............ar1ar2...arc) A=\begin{pmatrix}a_{11}&amp;a_{12}&amp;...&amp;a_{1c}\\a_{21}&amp;a_{22}&amp;...&amp;a_{2c}\\...&amp;...&amp;...&amp;...\\a_{r1}&amp;a_{r2}&amp;...&amp;a_{rc}\end{pmatrix}
kA=(ka11ka12...ka1cka21ka22...ka2c............kar1kar2...karc) k\cdot A=\begin{pmatrix}k\cdot a_{11}&amp;k\cdot a_{12}&amp;...&amp;k\cdot a_{1c}\\k\cdot a_{21}&amp;k\cdot a_{22}&amp;...&amp;k\cdot a_{2c}\\...&amp;...&amp;...&amp;...\\k\cdot a_{r1}&amp;k\cdot a_{r2}&amp;...&amp;k\cdot a_{rc}\end{pmatrix}

矩阵运算性质

  1. 交换律
    A+B=B+AA + B = B + A

  2. 结合律
    (A+B)+C=A+(B+C)(A + B) + C = A + (B + C)
    (ck)A=c(kA)(c\cdot k) \cdot A = c\cdot(k\cdot A) 。其中c和k是实数
    k(A+B)=kA+kBk\cdot(A + B) = k\cdot A + k\cdot B
    (c+k)A=cA+kA(c + k) \cdot A = c\cdot A + k\cdot A

  3. 任何一个矩阵AA,都存在一个相同形状的矩阵OO,满足
    A+O=AA + O = A

矩阵基本运算代码实现

接之前Matrix类代码,

	def __add__(self, another):
		assert self.shape() == another.shape()
		return Matrix([[a+b for a, b in zip(self.row_vector(i), another.row_vector(i))] for i in range(self.row_num()])

	def __sub__(self, another):
		assert self.shape() == another.shape()
		return Matrix([[a-b for a, b in zip(self.row_vector(i), another.row_vector(i))] for i in range(self.row_num()])

	def __mul__(self, k):
		return Matrix([[k*a for a in self.row_vector(i)] for i in range(self.row_num()])

	def __rmul__(self, k):
		return Matrix([[k*a for a in self.row_vector(i)] for i in range(self.row_num()])

	def __truediv__(self, k):
		return Matrix([[a/k for a in self.row_vector(i)] for i in range(self.row_num()])

	def __pos__(self):
		return self
	
	def __neg__(self):
		return -1 * self
	
	@classmethod
	def __zero__(cls, r, c):
		return cls([[0] * c] for _ in range(r))