朱莉娅:我们如何计算伴随或古典伴随(线性代数)?

ecj*_*cjb 3 linear-algebra julia

我想在中计算经典伴随 Julia 1.0

为此,我复制了维基百科中作为示例给出的矩阵

julia> B = [-3 2 -5; -1 0 -2; 3 -4 1]
3×3 Array{Int64,2}:
 -3   2  -5
 -1   0  -2
  3  -4   1
Run Code Online (Sandbox Code Playgroud)

在我看来,这似乎是在计算其转置B而不是其伴随。相反,我们应该(从Wikipedia)得到这个:

并尝试使用此处adjoint()茱莉亚文档中提到的函数来获得其伴随,尽管该文档没有具体说明该函数的作用

julia> adjoint(B)
3×3 Adjoint{Int64,Array{Int64,2}}:
 -3  -1   3
  2   0  -4
 -5  -2   1
Run Code Online (Sandbox Code Playgroud)

相反,我想得到这个:

在此处输入图片说明

在Matlab中,我确实得到了:

>> adjoint(B)

ans =

   -8.0000   18.0000   -4.0000
   -5.0000   12.0000   -1.0000
    4.0000   -6.0000    2.0000
Run Code Online (Sandbox Code Playgroud)

Ans*_*hvi 6

朱莉娅的伴随被定义为输入矩阵的复共轭的转置。但是,您似乎希望使用辅助矩阵:

佐剂有时被称为“伴随”,但是今天,矩阵的“伴随”通常是指其对应的伴随运算符,即其共轭转置。

您可以通过求反,然后乘以行列式来计算辅助矩阵:

julia> det(B) * inv(B)
3×3 Array{Float64,2}:
 -8.0  18.0  -4.0
 -5.0  12.0  -1.0
  4.0  -6.0   2.0
Run Code Online (Sandbox Code Playgroud)

感谢Julia Slack上的@Antoine Levitt和@Syx Pek提供了行列式求逆和乘积的建议。


原始答案:

佐剂矩阵似乎是辅因子矩阵的转置。以下是发现辅因子的简单实施:

# import Pkg; Pkg.add("InvertedIndices")
using InvertedIndices # for cleaner code, you can remove this if you really want to.
function cofactor(A::AbstractMatrix, T = Float64)
           ax = axes(A)
           out = similar(A, T, ax)
           for col in ax[1]
               for row in ax[2]
                   out[col, row] = (-1)^(col + row) * det(A[Not(col), Not(row)])
               end
           end
           return out
       end
Run Code Online (Sandbox Code Playgroud)

然后,要找到辅助词,只需要转置(transpose(cofactor(B)))。

答案是:

julia> cofactor(B, Float64) |> transpose
3×3 Transpose{Float64,Array{Float64,2}}:
 -8.0  18.0  -4.0
 -5.0  12.0  -1.0
  4.0  -6.0   2.0
Run Code Online (Sandbox Code Playgroud)

这与Matlab所提供的等效。

编辑:@Antoine Levitt在Julia松弛度上指出,这实际上是一个重新缩放的逆矩阵,因此,如果您知道缩放比例因子,则可以执行inv(B) * scaling_factor此操作(在此矩阵的情况下为6)。