在朱莉娅(Julia)中复制matlab

发布于 2025-01-31 03:36:56 字数 386 浏览 4 评论 0原文

众所周知,矩阵的特征分解不是唯一的。

但是我想知道是否可以在Julia中复制Matlab的eig函数。
也就是说,假设特征值的上升顺序,具有精确输出到数字误差。

假设我们将MATLAB的eig的选项设置为其默认值(BalanceOptionBalance,使用Choleskyky用于对称矩阵和qz作为其他算法)。

由于MATLAB正在使用Intel Mkl,因此在Julia中也可以假设MKL.JL(是否对eigen函数有影响? )。

It is known that the Eigen decomposition of a matrix isn't unique.

Yet I wonder if one could replicate MATLAB's eig function in Julia.
Namely have exact output up to numeric errors assuming ascending order of the eigen values.

Assuming we set the options of MATLAB's eig to their default (balanceOption, balance, Using cholesky for symmetric matrices and qz for others as algorithms).

Since MATLAB is using Intel MKL so assuming also MKL.jl is available in Julia (does it have an effect on Julia for the eigen function?).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寻找一个思念的角度 2025-02-07 03:36:56

这是确定标准化特征向量的食谱。这里有任意选择将向量正常化并将复数排序为(实际,复杂)的元素,并选择特征向量组件的符号,以便在归一化矢量中具有最大的实际值(请参阅注释):

using LinearAlgebra

""" compare two complex values as if tuples with (real portion, imaginary portion) """
complexlt(x, y) = (real(x), imag(x)) < (real(y), imag(y))

""" get a kind of canonical normalized form of an eigenvector of matrix A """
function sorted_normalized_eigen(A)
    # julia's eigen returns a struct, with eigenvector as values component of the struct
    m = eigen(A).values
    # sort the normalized form of the eigenvector from least to greatest
    normed1 = sort(normalize(m), lt = complexlt)
    # sort the additive inverse of normalized form of the eigenvector from least to greatest
    normed2 = sort(-1 .* normed1, lt = complexlt)
    # choose the normalized vector with the largest real component as our unique return value
    return complexlt(last(normed1), last(normed2)) ? normed2 : normed1
end

B =  [1 2 3; 3 1 2; 2 3 1]

@show sorted_normalized_eigen(B)

sorted_normalized_eigen(B) = ComplexF64[-0.2314550249431379 - 0.13363062095621225im, -0.2314550249431379 + 0.13363062095621225im, 0.9258200997725515 + 0.0im]

Here is a recipe for deciding on a standardized eigenvector. There are arbitrary choices here to normalize the vector and to sort complex numbers as tuples of (real, complex) and to choose the signs of the eigenvector components so as to have the largest real value in the normalized vector (see comments):

using LinearAlgebra

""" compare two complex values as if tuples with (real portion, imaginary portion) """
complexlt(x, y) = (real(x), imag(x)) < (real(y), imag(y))

""" get a kind of canonical normalized form of an eigenvector of matrix A """
function sorted_normalized_eigen(A)
    # julia's eigen returns a struct, with eigenvector as values component of the struct
    m = eigen(A).values
    # sort the normalized form of the eigenvector from least to greatest
    normed1 = sort(normalize(m), lt = complexlt)
    # sort the additive inverse of normalized form of the eigenvector from least to greatest
    normed2 = sort(-1 .* normed1, lt = complexlt)
    # choose the normalized vector with the largest real component as our unique return value
    return complexlt(last(normed1), last(normed2)) ? normed2 : normed1
end

B =  [1 2 3; 3 1 2; 2 3 1]

@show sorted_normalized_eigen(B)

sorted_normalized_eigen(B) = ComplexF64[-0.2314550249431379 - 0.13363062095621225im, -0.2314550249431379 + 0.13363062095621225im, 0.9258200997725515 + 0.0im]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文