第二章 向量与矩阵

本文详细介绍了MATLAB中的向量操作,包括创建、属性查询、运算及矩阵的基本操作,如转置、点乘、叉乘、行列式、矩阵乘法、秩计算、逆矩阵和解线性方程组等。此外,还讨论了向量的特殊类型和矩阵分解。内容深入浅出,适合MATLAB初学者和进阶者学习。

1.向量

向量(vector)一维数值数组。MATLAB 允许你创建列向量和行向量,列向量通过在方括号内把数值用分号(;)隔开来创建,对元素的个数没有限制。

a = [2; 1; 4]

a =

2

1

4

列向量的基本操作通过引用创建时使用的变量名来进行的。如果我们要把一个列向量乘上一一个数,这叫做数量乘法(scalar multiplication)。假设我们要创建一个新向量,它的元素是刚才我们创建的向量 a 的元素的三倍,我们可以先定义一个数量(注意命令行末的分号禁止了输出):

c = 3;

b = c*a

b =

6

3

12

要创建行向量,我们仍然是把一组数值用方括号括起来,不过这次使用的分隔符是空格(space)或逗号(,)。

v = [2 0 4]

v = 2 0 4

使用转置操作可以进行列向量与行向量之间的相互转换。

a = [2; 1; 4];

y = a’

y = 2 1 4

W = [3,0,3];

X = [2,1,1];

Y = W - X

Y = 1 -1 2

2.从已存变量创建大向量

A = [1; 4; 5];

B = [2; 3; 3];

D = [A;B]

D =

1

4

5

2

3

3

R = [12, 11, 9];

S = [1, 4];

T = [R, S]

T = 12 11 9 1 4

3.创建等差元素向量

有时需要创建带有等差元素的向量,差值为 q 为一个实数。创建一个首元素为 xi,末元

素为 xe的向量 x 的语法如下:x = [xi : q: xe]

x = [0:2:10]

x = 0 2 4 6 8 10

(1)linspace命令

向量含有 a 到 b 之间间隔相等(等差)的n 个元素。linspace(a, b)创建了 a、b 之间含有 100 个等差元素的向量,而 linspace(a, b, n)创建了 a、b 之间含有 n 个等差元素的向量。不管是哪种形式,MATLAB 自动确定元素之间的增量。

logspace(1, 2, 5)

ans = 10.0000 17.7828 31.6228 56.2341 100.0000

4.特征化向量(Characterizing a Vector)

(1)length命令

A = [2;3;3;4;5];

length(A)

ans = 5

(2)max或min命令

max 或 min 命令我们还可以找出向量中数值最大和最小的元素。

A = [8 4 4 1 7 11 2 0];

max(A)

ans = 11

min(A)

ans = 0

(3)向量的数量积(点乘)

J = [0; 3; 4];

J.*J

ans =

0

9

16

(4)共轭复数计算向量的和

u = [i; 1+2i; 4];

sum(u.*u)

ans = 12.0000 + 4.0000i

v = u’

v = 0 - 1.0000i 1.0000 - 2.0000i 4.0000

v = conj(u)

v =

0 - 1.0000i

1.0000 - 2.0000i

4.0000

b = sum(v.*u)

b = 22

magu = sqrt(b)

magu = 4.6904

(5)复数行向量的模

c = sqrt(sum(conj(u).*u))

c = 4.6904

(6)abs命令

A = [-2 0 -1 9]

A = -2 0 -1 9

B = abs(A)

B = 2 0 1 9

5.向量的点乘和叉乘(数量积和向量积)

(1)a、b 两向量的点乘 dot(a, b)命令计算

a = [1;4;7]; b = [2;-1;5];

c = dot(a,b)

c = 33

a = [1;4;7]; b = [2;-1;5];

c = dot(a,b)

c = 33

6.引用向量元素

1.MATLAB 有几种方法用来引用向量的一个或多个元素。

A = [12; 17; -2; 0; 4; 4; 11; 19; 27];

A(2)

ans = 17

A(8)

ans = 19

2.如果使用冒号——如 v(😃——来引用向量,等于告诉 MATLAB 列出向量的所有元素:

A(😃

ans =

12

17

-2

0

4

4

11

19

27

3.用 A(4:6)选出第 4 到第 6 个元素组成一个新的、含有 3 个元素的向量:

v = A(4:6)

v =

0

4

4

7.矩阵基本操作

矩阵是两维数字数组,要在 MATLAB 创建矩阵,输入的行各元素之间用空格或逗号分隔,行末使用分号标记。

A = [-1,6; 7, 11]

A = -1 6

 7  11

A = [5 1; 0 9];

B = [2 -2; 1 1];

A + B

ans = 7 -1

  1 10

A = [-1 2 0; 6 4 1]

A = -1 2 0

 6 4 1

B = A’//转置

B = -1 6

2 4

0 1

C = [1+i, 4-i; 5+2i, 3-3i]

C = 1.0000 + 1.0000i 4.0000 - 1.0000i

5.0000 + 2.0000i 3.0000 - 3.0000i

D = C’//如果矩阵包含有复数元素,那么转置操作会自动计算复数的共轭值:

D = 1.0000 - 1.0000i 5.0000 - 2.0000i

4.0000 + 1.0000i 3.0000 + 3.0000i

D = C.‘//如果要转置复数矩阵的而不计算它的共轭值,那么我们使用(.’):

D = 1.0000 + 1.0000i 5.0000 + 2.0000i

 4.0000 - 1.0000i 3.0000 - 3.0000i

8.矩阵相乘

A = [2 1; 1 2]; B = [3 4; 5 6];

A .* B//点乘

ans = 6 4

 5 12

A * B//矩阵相乘

ans =11 14

 13 16

9.更多基本操作

A = [1 2 3 4];

b = 2;

C = b + A//把数量值把数组的每个元素中

C = 3 4 5 6

A = [2 4 6 8]; B = [2 2 3 1];

C = A ./ B//数组上进行左除和右除

C =1 2 2 8

B = [2 4; -1 6]

B = 2 4

-1 6

B .^ 2//对每个元素进行平方:

ans =4 16

 1 36

10.特殊类型矩阵

1.单元矩阵是一个对角线为非零元素其它元素为零的方形矩阵。

eye(4)

ans =1 0 0 0

 0 1 0 0

 0 0 1 0

 0 0 0 1
2.要创建 n×n 的零矩阵,我们输入 zeros(n)。

zeros(4)

ans =0 0 0 0

 0 0 0 0

 0 0 0 0

 0 0 0 0

3.要创建整个元素都为 1 的矩阵,ones(n)或ones(m,n)即可分别创建 n×n 和 m×n 的矩阵。

ones(4)

ans =1 1 1 1

 1 1 1 1

 1 1 1 1

 1 1 1 1

11.引用矩阵元素

A = [1 2 3; 4 5 6; 7 8 9]

A = 1 2 3

4 5 6

7 8 9

A(2,3)//单个元素用

ans =6

A(:,2)//列引用

ans =

2

5

8

A(:,2:3)//多列引用

ans =2 3

 5 6

 8 9

F = A([1,2,1]😅//多次引用

F = -8 2 3

7 8 9

-8 2 3

12.行列式与线性系统求解

A = [1 3; 4 5];

det(A)//求解行列式的值

ans =-7

5x + 2y - 9z = -18

-9x - 2y + 2z = -7

6x + 7y + 3z = 29

1.列出系数矩阵 A 的行列式

A = [5 2 -9; -9 -2 2; 6 7 3]

A = 5 2 -9

-9 -2 2

6 7 3
2.判断行列式是否不为零,不为零那么解存在。

det(A)

ans = 437

3.MATLAB 允许我们使用左除容易地得到解。

b = [-18;-7;29];

A \ b求解方程组

ans =

1.0000

2.0000

3.0000

13.求矩阵的秩

矩阵的秩描述:

矩阵的秩是矩阵行或列的数值线性独立的度量。如果一个向量线性独立于另外一些向量组,那意味着这一个向量不能写成它们的线性组合。

A = [0 1 0 2; 0 2 0 4];

rank(A)//求解矩阵的秩

ans = 1

A = [1 -2 1; 3 4 5; -2 1 7]; b = [12; 20; 11];

C = [A b]//增广矩阵

C =

1 -2 1 12

3 4 5 20

-2 1 7 11

rank(A)

ans =3

rank©

ans =3

当且仅当 rank(A) = rank(A b)时系统有解。如果秩等于 n,那么系统有唯一解,但如果秩小于 n,那么系统有无数解。如果我们用 r 来表示秩,那么未知量的 r 就可以表示成其它变量的 n-r 的线性组合。

由于秩相同,因此解存在。这里有三个未知量,我们也注意到秩 r 满足 r = n。这意味着解唯一。我们用左除求得解:

x = A \ b

x = 4.3958

-2.2292

3.1458

14.求逆矩阵与伪逆矩阵

逆矩阵描述:

矩阵 A 的逆矩阵用 inv(A) 表示,逆矩阵并不总是存在。事实上我们可以用矩阵的行列式确定逆矩阵是否存在。如果 det(A)= 0,那么逆矩阵不存在,此时我们说此矩阵是一个奇异矩阵。

A = [2 3; 4 5]

A = 2 3

4 5

det(A)//检查矩阵行列式

ans =-2

det(A) ≠ 0,我们可以求得逆矩阵。

inv(A)

ans = -2.5000 1.5000

   2.0000 -1.0000

用系数矩阵的逆矩阵来相乘求得解

如果系数矩阵是方阵,这意味着方程组的方程数与未知数个数相同。如果方程数比未知数个数少,此时方程组称为欠定。这意味着方程组有无限解,因为此时只有一些未知数能够确定下来,不能确定的未知数可以赋予任意值。因此可以得到无限多组解。

x + 2y - z = 3

5y + z = 0

稍微处理可以得到:

z = -5y

x = 3 - 7y

在这个方程组中,我们可以为两个变量找到值(x 和 z),第三个变量 y 是不确定的。我们可以为 y 选择喜欢的值,此时方程组就有解了。

另外一种方程组存在无限解的情况是当 det(A) = 0 时。

解的办法是为未知数给出最小范数实数解,即是说,解向量 x 具有最小范数且元素都为实数。

举例:

3x + 2y - z = 7

4y + z = 2

很明显,这个方程组具无限解。我们输入数据:

A= [3 2 -1; 0 4 1]; b = [7; 2];

C = [A b]

C =

3 2 -1 7

0 4 1 2

现在计算秩:

rank(A)

ans = 2

rank©

ans = 2

由于这些秩相等,解存在。我们可以用 MATLAB 中的左除产生一组解:

x = A \ b

x =

2.0000

0.5000

0

MATLAB 通过把其中一个变量(本例中是 z)设为零产生一组解。如果要尝试用左除产生一组解,通常都是这样做的。当然,这解是有效的,不过记住它只是让 z = 0,而 z 可以是任何值。

我们也可以使用伪逆矩阵来解这个方程组。输入如下内容:

x = pinv(A) * b

x =

1.6667

0.6667

-0.6667

15.简化梯形矩阵

A = [1 2; 4 7]

A = 1 2

4 7

rref(A) 31

ans = 1 0

  0 1

16.矩阵分解

MATLAB 可以快速地对矩阵进行 LU、QR 或 SVD 分解。

MATLAB 中进行 LU 分解及如何使用它来求解线性方程组。

A = [-1 2 0; 4 1 8; 2 7 1];

[L, U] = lu(A)

L =

-0.2500 0.3462 1.0000

1.0000 0 0

0.5000 1.0000 0

U =

4.0000 1.0000 8.0000

0 6.5000 -3.0000

0 0 3.0385

b = [12;-8;6];

x = U(L\b)

x =

-6.9367

2.5316

2.1519

import cvxpy as cp import numpy as np import pandas as pd data = pd.read_excel('附件1.xlsx') X = data.iloc[1:, 1:2] Y = data.iloc[1:, 2:3] W = data.iloc[2:, 3:4] # 收集点垃圾量(i=1..n) xi = X.values.flatten() # 转换为一维数组 (n,) yi = Y.values.flatten() # 转换为一维数组 (n,) wi = W.values.flatten() # 转换为一维数组 (n,) # 定义参数 n = 30 # 收集点数量(i=1..n,0为处理厂) K = 30 # 车辆总数 Q = 5 # 最大载重 # 计算距离矩阵dij(i,j=0..n) dij = np.zeros((n+1, n+1)) for i in range(n+1): for j in range(n+1): dij[i, j] = np.sqrt((xi[i] - xi[j])**2 + (yi[i] - yi[j])**2) x = cp.Variable((n+1, n+1, K), boolean=True) # x_ijv: 车辆v从i到j y = cp.Variable((n, K), boolean=True) # y_iv: 车辆v服务收集点i(i=1..n,索引0..n-1) z = cp.Variable(K, boolean=True) # z_v: 使用车辆v u = cp.Variable((n+1, K), nonneg=True) # u_iv: 车辆v访问i后的累计载重(i=0..n,u[0,v]=0) objective = cp.Minimize(cp.sum(cp.sum(cp.sum(dij @ x, axis=2), axis=1), axis=0)) # 约束条件 constraints = [] # 1. 每个收集点被 exactly 一辆车服务(i=1..n,对应y的索引0..n-1) for i in range(n): constraints.append(cp.sum(y[i, :]) == 1) # 2. 流量守恒(i=1..n,处理厂i=0的约束在6中) for i in range(1, n+1): # 收集点i(1..n,y索引i-1) for v in range(K): constraints.append(cp.sum(x[i, :, v]) == y[i-1, v]) # 离开i的次数等于服务i的次数 constraints.append(cp.sum(x[:, i, v]) == y[i-1, v]) # 进入i的次数等于服务i的次数 # 3. 载重约束 for v in range(K): constraints.append(cp.sum(wi * y[:, v]) <= Q * z[v]) # 4. MTZ约束(子回路消除) for v in range(K): constraints.append(u[0, v] == 0) # 处理厂出发时载重为0 for i in range(1, n+1): # 收集点i(1..n,wi[i-1]) constraints.append(u[i, v] >= wi[i-1]) # 载重下限 constraints.append(u[i, v] <= Q) # 载重上限 for j in range(1, n+1): if i != j: constraints.append(u[i, v] - u[j, v] + Q * x[i, j, v] <= Q - wi[j-1]) # 5. 车辆使用约束:服务收集点则使用车辆 for i in range(n): for v in range(K): constraints.append(y[i, v] <= z[v]) # 6. 出发返回约束(处理厂i=0,出发到收集点1..n,返回从收集点1..n到0) for v in range(K): constraints.append(cp.sum(x[0, 1:, v]) == z[v]) # 出发次数等于车辆使用 constraints.append(cp.sum(x[1:, 0, v]) == z[v]) # 返回次数等于车辆使用 # 建立问题并求解 problem = cp.Problem(objective, constraints) problem.solve() # 输出结果(示例,需根据实际需求处理) print("最优目标值:", problem.value) print("使用的车辆:", z.value)D:\python\python.exe D:\35433\Documents\数模练习\电工杯\问题1.py D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning: This use of ``*`` has resulted in matrix multiplication. Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1. Use ``*`` for matrix-scalar and vector-scalar multiplication. Use ``@`` for matrix-matrix and matrix-vector multiplication. Use ``multiply`` for elementwise multiplication. This code path has been hit 1 times so far. D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning: This use of ``*`` has resulted in matrix multiplication. Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1. Use ``*`` for matrix-scalar and vector-scalar multiplication. Use ``@`` for matrix-matrix and matrix-vector multiplication. Use ``multiply`` for elementwise multiplication. This code path has been hit 2 times so far. D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning: This use of ``*`` has resulted in matrix multiplication. Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1. Use ``*`` for matrix-scalar and vector-scalar multiplication. Use ``@`` for matrix-matrix and matrix-vector multiplication. Use ``multiply`` for elementwise multiplication. This code path has been hit 3 times so far. D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning: This use of ``*`` has resulted in matrix multiplication. Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1. Use ``*`` for matrix-scalar and vector-scalar multiplication. Use ``@`` for matrix-matrix and matrix-vector multiplication. Use ``multiply`` for elementwise multiplication. This code path has been hit 4 times so far. D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning: This use of ``*`` has resulted in matrix multiplication. Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1. Use ``*`` for matrix-scalar and vector-scalar multiplication. Use ``@`` for matrix-matrix and matrix-vector multiplication. Use ``multiply`` for elementwise multiplication. This code path has been hit 5 times so far. D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning: This use of ``*`` has resulted in matrix multiplication. Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1. Use ``*`` for matrix-scalar and vector-scalar multiplication. Use ``@`` for matrix-matrix and matrix-vector multiplication. Use ``multiply`` for elementwise multiplication. This code path has been hit 6 times so far. D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning: This use of ``*`` has resulted in matrix multiplication. Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1. Use ``*`` for matrix-scalar and vector-scalar multiplication. Use ``@`` for matrix-matrix and matrix-vector multiplication. Use ``multiply`` for elementwise multiplication. This code path has been hit 7 times so far. D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning: This use of ``*`` has resulted in matrix multiplication. Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1. Use ``*`` for matrix-scalar and vector-scalar multiplication. Use ``@`` for matrix-matrix and matrix-vector multiplication. Use ``multiply`` for elementwise multiplication. This code path has been hit 8 times so far. D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning: This use of ``*`` has resulted in matrix multiplication. Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1. Use ``*`` for matrix-scalar and vector-scalar multiplication. Use ``@`` for matrix-matrix and matrix-vector multiplication. Use ``multiply`` for elementwise multiplication. This code path has been hit 9 times so far. D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning: This use of ``*`` has resulted in matrix multiplication. Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1. Use ``*`` for matrix-scalar and vector-scalar multiplication. Use ``@`` for matrix-matrix and matrix-vector multiplication. Use ``multiply`` for elementwise multiplication. This code path has been hit 10 times so far. D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning: This use of ``*`` has resulted in matrix multiplication. Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1. Use ``*`` for matrix-scalar and vector-scalar multiplication. Use ``@`` for matrix-matrix and matrix-vector multiplication. Use ``multiply`` for elementwise multiplication. This code path has been hit 11 times so far. D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning: This use of ``*`` has resulted in matrix multiplication. Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1. Use ``*`` for matrix-scalar and vector-scalar multiplication. Use ``@`` for matrix-matrix and matrix-vector multiplication. Use ``multiply`` for elementwise multiplication. This code path has been hit 12 times so far. D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning: This use of ``*`` has resulted in matrix multiplication. Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1. Use ``*`` for matrix-scalar and vector-scalar multiplication. Use ``@`` for matrix-matrix and matrix-vector multiplication. Use ``multiply`` for elementwise multiplication. This code path has been hit 13 times so far. D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning: This use of ``*`` has resulted in matrix multiplication. Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1. Use ``*`` for matrix-scalar and vector-scalar multiplication. Use ``@`` for matrix-matrix and matrix-vector multiplication. Use ``multiply`` for elementwise multiplication. This code path has been hit 14 times so far. D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning: This use of ``*`` has resulted in matrix multiplication. Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1. Use ``*`` for matrix-scalar and vector-scalar multiplication. Use ``@`` for matrix-matrix and matrix-vector multiplication. Use ``multiply`` for elementwise multiplication. This code path has been hit 15 times so far. D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning: This use of ``*`` has resulted in matrix multiplication. Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1. Use ``*`` for matrix-scalar and vector-scalar multiplication. Use ``@`` for matrix-matrix and matrix-vector multiplication. Use ``multiply`` for elementwise multiplication. This code path has been hit 16 times so far. D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning: This use of ``*`` has resulted in matrix multiplication. Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1. Use ``*`` for matrix-scalar and vector-scalar multiplication. Use ``@`` for matrix-matrix and matrix-vector multiplication. Use ``multiply`` for elementwise multiplication. This code path has been hit 17 times so far. D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning: This use of ``*`` has resulted in matrix multiplication. Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1. Use ``*`` for matrix-scalar and vector-scalar multiplication. Use ``@`` for matrix-matrix and matrix-vector multiplication. Use ``multiply`` for elementwise multiplication. This code path has been hit 18 times so far. D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning: This use of ``*`` has resulted in matrix multiplication. Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1. Use ``*`` for matrix-scalar and vector-scalar multiplication. Use ``@`` for matrix-matrix and matrix-vector multiplication. Use ``multiply`` for elementwise multiplication. This code path has been hit 19 times so far. D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning: This use of ``*`` has resulted in matrix multiplication. Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1. Use ``*`` for matrix-scalar and vector-scalar multiplication. Use ``@`` for matrix-matrix and matrix-vector multiplication. Use ``multiply`` for elementwise multiplication. This code path has been hit 20 times so far. D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning: This use of ``*`` has resulted in matrix multiplication. Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1. Use ``*`` for matrix-scalar and vector-scalar multiplication. Use ``@`` for matrix-matrix and matrix-vector multiplication. Use ``multiply`` for elementwise multiplication. This code path has been hit 21 times so far. D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning: This use of ``*`` has resulted in matrix multiplication. Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1. Use ``*`` for matrix-scalar and vector-scalar multiplication. Use ``@`` for matrix-matrix and matrix-vector multiplication. Use ``multiply`` for elementwise multiplication. This code path has been hit 22 times so far. D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning: This use of ``*`` has resulted in matrix multiplication. Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1. Use ``*`` for matrix-scalar and vector-scalar multiplication. Use ``@`` for matrix-matrix and matrix-vector multiplication. Use ``multiply`` for elementwise multiplication. This code path has been hit 23 times so far. D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning: This use of ``*`` has resulted in matrix multiplication. Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1. Use ``*`` for matrix-scalar and vector-scalar multiplication. Use ``@`` for matrix-matrix and matrix-vector multiplication. Use ``multiply`` for elementwise multiplication. This code path has been hit 24 times so far. D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning: This use of ``*`` has resulted in matrix multiplication. Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1. Use ``*`` for matrix-scalar and vector-scalar multiplication. Use ``@`` for matrix-matrix and matrix-vector multiplication. Use ``multiply`` for elementwise multiplication. This code path has been hit 25 times so far. D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning: This use of ``*`` has resulted in matrix multiplication. Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1. Use ``*`` for matrix-scalar and vector-scalar multiplication. Use ``@`` for matrix-matrix and matrix-vector multiplication. Use ``multiply`` for elementwise multiplication. This code path has been hit 26 times so far. D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning: This use of ``*`` has resulted in matrix multiplication. Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1. Use ``*`` for matrix-scalar and vector-scalar multiplication. Use ``@`` for matrix-matrix and matrix-vector multiplication. Use ``multiply`` for elementwise multiplication. This code path has been hit 27 times so far. D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning: This use of ``*`` has resulted in matrix multiplication. Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1. Use ``*`` for matrix-scalar and vector-scalar multiplication. Use ``@`` for matrix-matrix and matrix-vector multiplication. Use ``multiply`` for elementwise multiplication. This code path has been hit 28 times so far. D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning: This use of ``*`` has resulted in matrix multiplication. Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1. Use ``*`` for matrix-scalar and vector-scalar multiplication. Use ``@`` for matrix-matrix and matrix-vector multiplication. Use ``multiply`` for elementwise multiplication. This code path has been hit 29 times so far. D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning: This use of ``*`` has resulted in matrix multiplication. Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1. Use ``*`` for matrix-scalar and vector-scalar multiplication. Use ``@`` for matrix-matrix and matrix-vector multiplication. Use ``multiply`` for elementwise multiplication. This code path has been hit 30 times so far. D:\python\Lib\site-packages\cvxpy\reductions\solvers\solving_chain_utils.py:41: UserWarning: The problem has an expression with dimension greater than 2. Defaulting to the SCIPY backend for canonicalization.
最新发布
05-25
### CVXPY 中矩阵乘法的正确实现方式 在 CVXPY 1.1 及以上版本中,`*` 操作符被重新定义为逐元素乘法(element-wise multiplication),而不是传统的矩阵乘法。因此,在涉及矩阵乘法的操作时,直接使用 `*` 将引发警告或错误。为了消除这些警告并提高代码的兼容性和性能,建议采用以下方法: #### 使用 '@' 运算符 CVXPY 支持 Python 的标准矩阵乘法运算符 `@`,该运算符能够高效地完成矩阵间的乘法操作。这是推荐的方式之一,因为它不仅语义清晰,而且 NumPy 和 PyTorch 等库保持一致[^1]。 ```python import cvxpy as cp # 定义变量和参数 X = cp.Variable((3, 2)) A = cp.Parameter((2, 3), value=np.random.randn(2, 3)) # 正确使用 @ 实现矩阵乘法 expression = A @ X ``` 此代码片段展示了如何通过 `@` 来替代旧版中的 `*`,从而避免不必要的警告。 #### 使用 `cp.matmul` 除了 `@` 外,还可以显式调用 `cvxpy.matmul()` 函数来进行矩阵乘法。这种方式更加灵活,尤其适用于复杂表达式的构建场景[^1]。 ```python # 显式调用 matmul 方法 expression = cp.matmul(A, X) ``` 需要注意的是,无论是 `@` 还是 `cp.matmul`,都支持广播机制,这意味着当两个张量维度匹配时,它们会自动扩展以适应计算需求[^1]。 #### 避免误用 'multiply' 尽管 `np.multiply` 或者其对应的 CVXPY 版本可用于逐元素乘法,但它并不适合处理传统意义上的矩阵乘法问题。例如,下面的例子演示了两者的区别: ```python # 错误示范:试图用 multiply 替代矩阵乘法 C = np.array([[1, 2], [3, 4]]) D = np.array([[5, 6], [7, 8]]) wrong_result = np.multiply(C, D) # 结果是一个逐元素相乘后的数组 correct_result = C @ D # 执行真正的矩阵乘法 ``` 显然,只有后者才是正确的做法[^4]。 最后值得注意的一点在于输入数据结构的选择上——如果确认所有参运算的数据均为二维形式,则可以直接选用效率更高的专用函数;但如果存在高维情况或者不确定具体形状的话,则应优先考虑那些具备广泛适用性的解决方案,比如前述提到过的两种主流途径[^3]。 ### 性能考量 从性能角度来看,`@` 和 `cp.matmul` 均经过高度优化,能够在大多数情况下提供最佳表现。然而实际应用过程中还需关注以下几个方面: - **稀疏性**:若待求解模型含有大量零值项,则需特别指定相应属性以便充分利用内存空间减少冗余计算; - **批量化处理能力**:现代硬件架构往往偏好批量作业模式,故设计算法阶段也应当考虑到这一点进而合理规划数据流走向。 综上所述,在升级至最新版 CVXPY 后遇到此类提示信息时,请果断切换到现代化接口上来满足规范要求的同时兼顾执行速度上的优势。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

入世浮尘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值