
常用的矩阵命令(2)”
欢迎您的访问。
Share interest, spread happiness,
increase knowledge,and leave beautiful.
Dear, this is LearningYard Academy!
Today, the editor brings you the article
“Zhexue (45): Introduction to Mathematica:
Commonly used matrix commands (2)”
Welcome to visit!
本期推文,小编将从思维导图和学习内容两个方面来介绍常用的矩阵命令的入门学习。
In this post, the editor will introduce the introductory learning of commonly used matrix commands from two aspects: mind mapping and learning content.
一、思维导图(Mind Maps)

二、学习内容(Learning content)
(1)计算矩阵特征值(Compute matrix eigenvalues)
当我们有一个矩阵A和一个非零向量p时,如果矩阵A作用在向量p上,结果是一个与p共线的向量,但这个向量的长度可能发生了变化(变长或变短,甚至可能变为零向量),那么我们就说p是矩阵A的一个特征向量,而这个长度变化的比例(即缩放因子)就是矩阵A的一个特征值。简单来说,特征值描述了矩阵对特定向量(即特征向量)进行变换时的缩放情况。
When we have a matrix A and a non-zero vector p, if the matrix A acts on the vector p, the result is a vector that is collinear with p, but the length of this vector may change (become longer or shorter, or even become a zero vector), then we say that p is an eigenvector of the matrix A, and the ratio of this length change (i.e., the scaling factor) is an eigenvalue of the matrix A. Simply put, the eigenvalue describes the scaling of a specific vector (i.e., the eigenvector) when the matrix transforms it.
首先,在Mathematica中定义一个矩阵。矩阵可以通过列表来定义,然后,使用Eigenvalues函数来计算该矩阵的特征值。最后,按Enter+Shift键后,Mathematica将返回矩阵A的所有特征值。
First, define a matrix in Mathematica. Matrices can be defined using lists. Then, use the Eigenvalues function to calculate the eigenvalues of the matrix. Finally, after pressing Enter+Shift, Mathematica will return all the eigenvalues of the matrix A.

(2)计算矩阵的迹(Compute the trace of a matrix)
矩阵的迹,用通俗易懂的话来说,就是矩阵主对角线上所有元素的总和。这里的“主对角线”指的是从矩阵的左上角到右下角的对角线。当我们面对一个方阵(即行数和列数相等的矩阵)时,只需要将这条对角线上的所有元素加起来,得到的结果就是该矩阵的迹。
The trace of a matrix, in layman's terms, is the sum of all the elements on the main diagonal of the matrix. The "main diagonal" here refers to the diagonal from the upper left corner to the lower right corner of the matrix. When we are faced with a square matrix (that is, a matrix with equal number of rows and columns), we only need to add up all the elements on this diagonal, and the result is the trace of the matrix.
首先,你需要在Mathematica中定义一个矩阵。矩阵可以通过列表来定义,每个内部列表代表矩阵的一行,所有内部列表被一个外部列表包围起来形成整个矩阵。在定义了矩阵之后,你可以使用Mathematica内置的Tr函数来计算矩阵的迹。将你想要计算迹的矩阵作为Tr函数的参数传入即可。按Enter键后,Mathematica将自动计算并返回矩阵A的迹,即主对角线上元素1、5和9的总和15。
First, you need to define a matrix in Mathematica. A matrix can be defined using lists, where each inner list represents a row of the matrix, and all inner lists are surrounded by an outer list to form the entire matrix. After defining the matrix, you can use Mathematica's built-in Tr function to calculate the trace of the matrix. Just pass the matrix you want to calculate the trace of as the argument to the Tr function. After pressing the Enter key, Mathematica will automatically calculate and return the trace of the matrix A, which is the sum of the elements 1, 5, and 9 on the main diagonal, 15.

(3)输入海塞矩阵、雅可比矩阵(Input Hessian matrix, Jacobian matrix)
海塞矩阵是一个方阵,其元素由函数f对自变量x₁, x₂, …, xₙ的二阶偏导数组成。具体来说,矩阵中的元素Hᵢⱼ(i行j列)表示函数f对自变量xᵢ和xⱼ的二阶偏导数。
The Hessian matrix is a square matrix whose elements consist of the second-order partial derivatives of a function f with respect to the independent variables x₁, x₂, …, xₙ. Specifically, the element Hᵢⱼ (i row and j column) in the matrix represents the second-order partial derivatives of the function f with respect to the independent variables xᵢ and xⱼ.
雅可比矩阵是一个一阶偏导数以一定方式排列成的矩阵。具体来说,假设有一个m维向量函数f(x) = [f₁(x), f₂(x),…, fₘ(x)]T,其中x = [x₁, x₂, …, xₙ]T是一个n维输入向量。雅可比矩阵J是一个m×n的矩阵,其元素由函数的偏导数组成,即J的(i,j)元素是∂fᵢ/∂xⱼ,表示函数fᵢ对变量xⱼ的偏导数。
The Jacobian matrix is a matrix of first-order partial derivatives arranged in a certain way. Specifically, suppose there is an m-dimensional vector function f(x) = [f₁(x), f₂(x),…, fₘ(x)]T, where x = [x₁, x₂, …, xₙ]T is an n-dimensional input vector. The Jacobian matrix J is an m×n matrix whose elements consist of the partial derivatives of the function, that is, the (i,j) element of J is ∂fᵢ/∂xⱼ, which represents the partial derivative of the function fᵢwith respect to the variable xⱼ.
首先,你需要在Mathematica中定义你想要计算海塞矩阵的多元函数。接下来,你需要计算函数f(x, y)关于其各个变量的二阶偏导数。在Mathematica中,你可以使用D函数来执行这一操作。D函数的一般形式是D[f, {{var},n}],其中f是函数,var是变量,n是次数。
First, you need to define in Mathematica the multivariate function for which you want to compute the Hessian matrix. Next, you need to compute the second-order partial derivatives of the function f(x, y) with respect to each of its variables. In Mathematica, you can use the D function to perform this operation. The general form of the D function is D[f, {{var},n}], where f is the function, var is the variable, and n is the degree.

而雅可比矩阵是一阶偏导数,因此具体函数表达为D[f,{var}],其中f是函数,var是变量。
The Jacobian matrix is a first-order partial derivative, so the specific function is expressed as D[f,{var}], where f is a function and var is a variable.

(4)提取矩阵元素(Extracting Matrix Elements)
在Mathematica中,你可以通过直接指定矩阵的行和列索引来提取元素。索引通常使用方括号[]进行。索引的起始值为1,而不是在其他一些编程语言中常见的0。
In Mathematica, you can extract elements by specifying the row and column indices of a matrix directly. Indexing is usually done using square brackets []. Indices start at 1, not 0 as is common in some other programming languages.
提取单个元素,可以使用A[[i, j]]来提取第i行第j列的元素。如果你想要提取整行或整列,可以使用A[[i]]来提取第i行(这会返回一个行向量),或者使用A[[All, j]]来提取第j列(返回一个列向量)。注意,All是一个特殊的索引,表示所有行或列。
To extract a single element, you can use A[[i, j]] to extract the element in the i-th row and j-th column. If you want to extract an entire row or column, you can use A[[i]] to extract the i-th row (which returns a row vector), or use A[[All, j]] to extract the j-th column (which returns a column vector). Note that All is a special index that means all rows or columns.
提取矩阵中的元素,一定要另起一行,单独使用一次MatricForm函数,否则无法提取所需要的元素。
To extract elements from a matrix, you must start a new line and use the MatricForm function once alone; otherwise, the required elements cannot be extracted.
