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

4

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

为此,我复制了作为示例给出的矩阵wikipedia

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

那似乎是计算B的转置而不是其伴随。相反,我们应该得到这个(来自wikipedia):
并尝试使用adjoint()函数获取其伴随,尽管Julia文档here没有明确说明此函数的作用。
julia> adjoint(B)
3×3 Adjoint{Int64,Array{Int64,2}}:
 -3  -1   3
  2   0  -4
 -5  -2   1

我想要得到这个而不是那个:

enter image description here

在Matlab中我确实得到:
>> adjoint(B)

ans =

   -8.0000   18.0000   -4.0000
   -5.0000   12.0000   -1.0000
    4.0000   -6.0000    2.0000
1个回答

7

Julia的共轭伴随矩阵被定义为输入矩阵的复共轭转置。然而,您似乎想要求的是伴随矩阵

伴随矩阵有时被称为"伴随",但是今天矩阵的"伴随"通常指其相应的伴随算子,即其共轭转置。

您可以通过求逆并乘以行列式来计算伴随矩阵:

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

感谢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

然后,要找到伴随矩阵,只需要转置 (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

这与Matlab给出的结果等价。

编辑:在Julia的Slack上,Antoine Levitt指出这本质上是一个重新缩放的逆矩阵,因此如果您确定了缩放系数,您可以执行inv(B) * scaling_factor(对于这个矩阵,它是6)。


我遇到了错误ERROR: UndefVarError: Not not defined,其中Not来自哪里?我刚意识到它在InvertedIndex包中。 - kungfooman

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接