-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement more matrix functions #5840
Comments
It would be amazing to include all of these algorithms. Thanks for putting this issue together, @jiahao. |
It's not clear to me what interface should be implemented for the very generic things like "function of a symmetric or Hermitian matrix". We could define methods for elementary functions until the cows come home, but that is both tedious and namespace polluting (we'd need Maybe we want something like |
This is one of the reasons why having all these functions be vectorized is kind of shitty. I suspect that @JeffBezanson will vehemently agree. There could be a |
Sure, but that won't help with the fact that a complicated matrix function like |
Yes, that's a very good point. |
Trefethen's contour-integration method is also neat for mostly analytic functions (with known singularities), though I don't know how it compares to the state of the art (in performance and generality). |
@stevengj it looks like that paper is Ref. 32 of the Higham and Deadman report, and isn't currently available in any major package (although it has Matlab code). |
This seems like a good candidate for a GSOC project. As far as an interface goes, I would suggest a macro
A naive approach could simply replace the functions (e.g. |
The item "[sqrtm] real version for real matrices (Higham, 1987)" is checked. I can't see that algorithm in base. I made an attempt on it about a year ago but concluded that the complexity just wasn't worth it. I've put up the code as a gist if someone wants to have a look. |
Thanks for catching that. I must have ticked it by accident when writing up this issue. |
Just tried to call logm, but wasn't implemented. Had to go to Mathematica |
Whoever tackles this issue might find this paper helpful for testing: |
Great reference – thanks, @fabianlischka. |
RFC: Matrix logarithm function (issue #5840)
The |
Also cc: @higham @alanedelman |
Would |
I think dropping the m is more consistent with the Taylor series definition of exp(dual(1.,2.)) returns the dual corresponding to the Taylor series definition (which in this case is equivalent to That's ignoring the Matlab and Mathematica precedents. |
Whether or not exp(A) replaces expm(A), I think down the line exp.(A) should replace exp(A), as the current exp(A) is confusing for non-MATLAB users |
@dlfivefifty sooner than you think, there's already an open PR for it: https://github.com/JuliaLang/julia/pull/17302/files#diff-8278b779f2ea681192ba5b020a2c3e2bL137 |
Version 2 of the Deadman-Higham list has been out for a year and Julia is the same as it was first time. Let's get better before version 3 comes out. I'm going to look at Schur-Pade -- that together with the top-4 tasks "General function evaluations" in the OP would cover most of this. Anyone else want to join? P.S. we already beat R in the DH list, with built-in Matlab and SciPy looking vulnerable as well! |
Is the "Deadman-Higham ranking" our Weissman Score? |
Could there be a function |
Even for diagonalizable A, the possibility of nearly defective A would be an issue. Of course, it could be restricted to Hermitian A, but that case is so easy that we would hardly add much value by implementing a function for it in Base. |
There is a pretty good chance that matrix derivatives would work with ForwardDiff.jl, at least |
Should this all be moved out to a MatrixFunctions.jl? |
Seems like a good idea -- I think everything on the list other than @ararslan You did the great work moving SpecialFunctions.jl. What do you think about MatrixFunctions.jl? Other people that were involved there IIRC but are not in this thread: @musm, @andreasnoack |
Should we close this, and perhaps there ought to be a better place for this in a new repo? |
If it's extending |
we could do something like this: for f in (:sqrt, ...)
@eval $f(A::AbstractArray) = matrixfunction($f, A)
end
matrixfunction(f, A) = error(“Install MatrixFunctions.jl”)
end Then MatrixFunctions.jl can safely override |
I have implemented a couple of algorithms for computing matrix functions. You can check them out here https://github.com/robzan8/MatFun.jl |
I was planning to add the Fréchet derivatives for matrix exponential and logarithm to ChainRules to support pushforwards and pullbacks of |
Possibly but isn't it possible to define these in terms of existing matrix functions? |
Unless I'm missing something, not really: differentials of matrix functions are tricky due to non-commutativity. @sethaxen it would be strange to have this live in LinearAlgebra, can you maybe refactor LinearAlgebra to expose the functions that you need and use them in ChainRules? |
I think @antoine-levitt is right. Looks like differentiating f(A(t)) = ∮ f(ζ) inv(ζ*I - A(t)) dζ then we get d/dt f(A(t)) = ∮ f(ζ) inv(ζ*I - A(t)) * dA/dt * inv(ζ*I - A(t)) dζ which I don't believe can be reduced to matrix factorisations. Really interested to know if anyone has a reference for differentiating matrix functions. Don't have a copy of Higham handy but nothing pops out of the table of contents. |
Sort of. For any matrix function function frechet(f, A, ΔA)
n = checksquare(A)
Ablock = [A ΔA; zero(A) A]
Yblock = f(Ablock)
Y = Yblock[1:n,1:n]
ΔY = Yblock[1:n,n+1:2n]
return Y, ΔY
end But this is ~8x the cost of
I agree, I only ask because given this issue it seemed there was (old) interest in them being in base, and the Frechet derivative of
Perhaps. While the most efficient way to implement will be completely in parallel, we can get close by reusing all matrix products. So a nice refactor might be to move the work of |
Basically continue what you were doing with the integral formula, expand in an eigenbasis and integrate the poles explicitly, and you get divided differences (f(lambda_n) - f(lambda_m)) / (lambda_n - lambda_m), with the convention (f(x)-f(y))/(x-y) = f'(x) when x==y. It's probably discussed in Higham somewhere, I think it's called Daleskii-Krein there (I know it from quantum mechanics, where it's known under the generic name of "perturbation theory"). It's a bit tricky to implement in the case of close eigenvalues for generic functions It was suggested above to split matrix functions (more than exp/log/sqrt) to a separate MatrixFunctions.jl package; I think that would be a very natural place to put derivatives. |
@sethaxen's method is probably a bad idea for normal matrices as |
Section 3.2 of Higham's Functions of Matrices gives several approaches for computing Frechet derivative:
For automatic differentiation, we indeed only want to define a rule if it works for all matrices, and the latter approach should be fine. In fact, the Frechet derivative of the matrix exponential by Higham is precisely exactly this, just checked for accuracy. I don't think we can autodiff
The issue with that, raised above, is type piracy. I guess that could be worked around by re-appending the |
I see. In the applications I'm interested in the function f is often the Heaviside function, so that's not an option. Regarding a separate package, clearly exp/sin/log/sqrt are special because they're in base, but there are other functions one might want to use, so a general |
This is best done in external packages. Moving to a discussion so that we have the list. |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
Higham and Deadman have published a list of software implementing algorithms for matrix functions, showing that Julia's library for these functions could be improved greatly relative to the state of the art.
The purpose of this issue is to discuss and track the implementation of matrix functions.
From Higham and Deadman's list:
General function evaluations
cond(f,A)
f(A) * b
Specific functions
expm
: scaling and squaring algorithm (Al-Mohy and Higham, 2009)logm
: inverse scaling and squaring algorithm (Al-Mohy, Higham, and Relton, 2012,2013)
sqrtm
:A^t
for real matrix powers: Schur– Padé algorithm (Higham and Lin, 2013) (Fixed the algorithm for powers of a matrix. #21184)Fréchet derivatives
A^t
for real matrix powers: Schur– Padé algorithm (Higham and Lin, 2013)Miscellaneous functions
inv
anddet
forTridiagonal
andSymTridiagonal
matrices (Usmani, 1994) (Provide inv and det of Tridiagonal and SymTridiagonal matrices #5358)The text was updated successfully, but these errors were encountered: