Exponential#94
Conversation
Codecov Report❌ Patch coverage is
... and 36 files with indirect coverage changes 🚀 New features to boost your workflow:
|
|
|
||
| function exponential!(A::AbstractMatrix, expA::AbstractMatrix, alg::ExponentialViaEigh) | ||
| D, V = eigh_full(A, alg.eigh_alg) | ||
| copyto!(expA, V * Diagonal(exp.(diagview(D))) * inv(V)) |
There was a problem hiding this comment.
Reduced allocation strategy:
| copyto!(expA, V * Diagonal(exp.(diagview(D))) * inv(V)) | |
| iV = inv(V) | |
| map!(exp, diagview(D)) | |
| mul!(expA, rmul!(V, D), iV) |
There was a problem hiding this comment.
It has to be map!(exp, diagview(D), diagview(D)) instead of map!(exp, diagview(D)), but good suggestion otherwise. I have also added it for the ExponentialViaEig.
EDIT: the suggested change works only for Julia 1.12 onwards. That's why I will keep the version with
3 arguments.
There was a problem hiding this comment.
Why not just diagview(D) .= exp.(diagview(D))?
There was a problem hiding this comment.
Is that more efficient than the current code? If not, I'd prefer to keep it that way, since it feels a bit more natural to me.
lkdvos
left a comment
There was a problem hiding this comment.
Overall I'm not fully convinced by the interface of exponential(!), especially in its current form and implementation this looks slightly strange.
LinearAlgebra uses an in-place version, i.e. it reuses the input array to return exp!, and looking at the different implementations you have here, it is not obvious that trying to fit this into a exponentiate!(A, expA, alg) signature is really helping us - on the contrary, all this is really doing is creating an additional copy at the end just to make sure that it is allocated in the provided output.
As we discussed for your previous PR, this really is not the purpose of being able to provide the output argument.
For the algorithms, thinking a bit ahead, it might be appropriate to just call these something along the lines of matrix functions via eig, since presumably these approaches are actually generic for all of these implementations.
|
|
||
| function exponential!(A::AbstractMatrix, expA::AbstractMatrix, alg::ExponentialViaEigh) | ||
| D, V = eigh_full(A, alg.eigh_alg) | ||
| copyto!(expA, V * Diagonal(exp.(diagview(D))) * inv(V)) |
There was a problem hiding this comment.
Why not just diagview(D) .= exp.(diagview(D))?
The idea of putting it in this framework is to allow for different sorts of algorithms, i.e. ones that would also work for BigFloats. I get that in the BLASFloat case, we should avoid extra allocations, but could you elaborate on your suggestion for
I may be missing your point here, but that was the idea behind the |
I am indeed referring to the preallocated output, not the algorithms part. It really only makes sense to have the option of giving a preallocated output if we are actually able to use this, and for the current implementations you have this is not saving us any work, rather it is increasing it because you add an extra allocation at the beginning and an extra copy at the end. While it is definitely possible to have
Sorry I should have explained that better, I meant that I would like to avoid having to also define |
Is your suggestion then to just skip the whole
Okay, I see. I agree and will change this. |
|
Regarding the algorithm names, I agree with Lukas and also think we want to have a general Regarding the role of the output arguments, I only partially agree. The whole point of why we started MatrixAlgebraKit.jl, is because in TensorKit we first want to define the output tensor, and then compute block per block the result, where we want to store the result in the corresponding block of the output tensor. Ideally, yes, the computation is such that we also use that output data as storage during the computation, in such a way that the end result "naturally" ends up there, but if that is difficult, a final Note that the LinearAlgebra |
|
Regardless of the comment about general matrix functions, it is a fact that the exponential is by far the most useful and common one that we need, so I am also not opposed to first thinking carefully about this one, and having some part of the implementation be specific for matrix exponentials. In particular, one important consideration that we might want to include in this design, that is specific to our use case, is that we also might be interested in computing |
|
To comment on the TensorKit interaction, I definitely agree with the purpose, but this is not actually currently the design we ended up with. So basically there are two comments I have: On the one hand, there is the question about whether or not there are implementations that benefit from providing an additional output array. On the other hand, given that interface, if there is no way of naturally making the output end up in the provided destination, I would really like to avoid ending up with a final |
|
I guess I am a bit confused, because most of the implementations now do actually perform the final step in the calculation in such a way that the result is directly stored in the output array, no? It is only the algorithm that goes via Base/LinearAlgebra that requires the extra But it is also true that, by the time the final step of the calculation is reached; the memory of |
change name to `MatrixFunctionViaEig` etc change `decompositions` to `matrixfunctions` add default algorithm for Diagonal matrices add input checks add @testthrows to catch non-hermitian matrices being given to MatrixFunctionViaEigh change default exponential algorithm to e.g. `MatrixFunctionViaEig` of the default `eig_alg`
lkdvos
left a comment
There was a problem hiding this comment.
Overall this is looking quite nice! Apologies if this is a premature review, just showed up in my notifications and figured I'd have a look.
The only thing I'm still wondering is if we want to just replace exponentialr(t, A) = exponential((t, A)) and not have the two functions. I know we had this discussion before, I'm not sure what came out of that to be honest.
| # -------------- | ||
| function exponential!(A, expA, alg::MatrixFunctionViaLA) | ||
| check_input(exponential!, A, expA, alg) | ||
| return LinearAlgebra.exp!(A) |
There was a problem hiding this comment.
I know we had a lot of discussions about this in the past, but I think we now settled on actually copying over the data (see also the GLA extension, I think), so this should probably become:
| return LinearAlgebra.exp!(A) | |
| LinearAlgebra.exp!(A) | |
| A === expA || copy!(expA, A) | |
| return expA |
There was a problem hiding this comment.
I know we had a lot of discussions about this in the past, but I think we now settled on actually copying over the data (see also the GLA extension, I think), so this should probably become:
This does not work, for the very bizarre reason that LinearAlgebra.exp!(A) does not guarantee that the result is directly stored in the matrix A (at least when I test it locally with version 1.12.0). Everything is fine if I replace line 74 with A = LinearAlgebra.exp!(A), but probably there is a better way to get around this?
Co-authored-by: Lukas Devos <ldevos98@gmail.com>
Co-authored-by: Lukas Devos <ldevos98@gmail.com>
Co-authored-by: Lukas Devos <ldevos98@gmail.com>
I also don't know whether there was a consensus. I would be in favour of indeed just using |
|
Yes, let's do this and then push this PR over the edge 🎉 |
| if scalar_check | ||
| @check_scalar(expA, A) | ||
| end |
There was a problem hiding this comment.
I added these lines to be able to differentiate the check_input test between exponential(A) and exponential(tau,A), since the latter does not require @check_scalar. Feel free to make suggestions to do this in a different way.
There was a problem hiding this comment.
Why does the latter not require that? If A is real and tau is complex, expA has to be complex right?
There was a problem hiding this comment.
You're right, I forgot about that. I added a method check_input for exponential((tau,A)), where I dispatch on whether tau is real.
| end | ||
|
|
||
| function check_input(type::typeof(exponential!), τ, A::AbstractMatrix, expA::AbstractMatrix, alg) | ||
| return check_input(type, complex(A), expA, alg) |
There was a problem hiding this comment.
This is probably not entirely how we should handle this, as we don't want to copy A to make the check. I think the easiest is just to copy some of the code, and I think @check_scalar has the option to give complex as an extra parameter. See e.g. the eig_ implementations, where we have a similar thing happening
There was a problem hiding this comment.
Now, I copy some of the code, and changed @check_scalar(expA, A) to (τ isa Real) ? @check_scalar(expA, A) : @check_scalar(expA, A, complex)
lkdvos
left a comment
There was a problem hiding this comment.
I think I left a final set of comments, otherwise this is good to go for me. You'll see it's quite nitpicky, so feel free to share your opinions as well!
| return nothing | ||
| end | ||
|
|
||
| function check_input(::typeof(exponential!), τ::Number, A::AbstractMatrix, expA::AbstractMatrix, alg::AbstractAlgorithm) |
There was a problem hiding this comment.
Another question that is kind of irrelevant, but just want to check if you have a specific reasoning for choosing check_input(exponential!, (τ, A), expA, alg) vs check_input(exponential!, τ, A, expA, alg). I guess there is some annoyance with the tuple and dispatch, but this is somewhat less in line with the other single-argument functions where f!(in, out, alg) calls check_input(f!, in, out, alg), instead of here check_input(f!, in..., out, alg). I assume this is a remnant of using the 2-arg @functiondef from before?
There was a problem hiding this comment.
I agree that check_input(exponential!, (τ, A), expA, alg) is the more logical choice. I've changed that now.
Co-authored-by: Lukas Devos <ldevos98@gmail.com>
Co-authored-by: Lukas Devos <ldevos98@gmail.com>
Co-authored-by: Lukas Devos <ldevos98@gmail.com>
Co-authored-by: Lukas Devos <ldevos98@gmail.com>
Co-authored-by: Lukas Devos <ldevos98@gmail.com>
Co-authored-by: Lukas Devos <ldevos98@gmail.com>
Co-authored-by: Lukas Devos <ldevos98@gmail.com>
Thanks for the final comments. I agreed with all of them! |
lkdvos
left a comment
There was a problem hiding this comment.
Thanks for all the work @sanderdemeyer, this looks like a great addition!
@Jutho, @kshyatt I'll leave this open in case you still want to have a look, and at the latest I'll merge this Monday evening to keep things moving, while not imposing on weekends 😉
This implements the exponential of a matrix for both
BLASFloatsandBigFloats.I have named these functions
exponentialandexponential!, instead of the usualexpandexp!fromLinearAlgebra. Extending these methods while keeping the current structure using @algdef and @ functiondef results in some naming conflicts. The default for BLASFloats is to useLinearAlgebra.exp!. InTensorKit, we can still stick to theexpnaming convention.