Jutho / KrylovKit.jl

Krylov methods for linear problems, eigenvalues, singular values and matrix functions
Other
284 stars 37 forks source link

Weird behavior of exponentiate when trying to use inplace behavior #89

Closed DavidWellnitz closed 4 months ago

DavidWellnitz commented 4 months ago

I am trying to use exponentiate to compute a matrix exponential of an inplace defined function. The simplest way to reproduce the problem which I found is

exponentiate(x -> x, 1, ones(2))

which returns [3.95..., 3.95...] instead of [2.71..., 2.71...]. In contrast

exponentiate(x -> deepcopy(x), 1, ones(2))

works fine. Is there a reason why method 1 should not work?

Jutho commented 4 months ago

Yes, KrylovKit currently assumes that applying the operator creates a result that is memorywise independent from the input. If at some point inplace operators would be supported, it would be with two-argument versions f!(y, x) where the result is stored iny. I might be mistaken, but in my typical applications, there would be very few linear operators where it would make sense to apply them inplace in the sense of overwriting the input.

DavidWellnitz commented 4 months ago

There is probably a way around it. I had originally written an inplace function to use with DifferentialEquations, but when I realized my problem is linear, I thought KrylovKit should be more efficient (and it seems to be). I was wondering if there is a way to pre-allocate the output vector (in the way you suggested), in order to improve efficiency, but from your comment that seems to not be the case.

Jutho commented 4 months ago

Can I close this? Supporting in place functions and reducing memory allocation is certainly on the TODO list so I don't think this needs to stay open as an extra reminder.

DavidWellnitz commented 4 months ago

Yes, go ahead!