Status of exp and sqrt

Now that exp. and sqrt. exist, will expm and sqrtm be deprecated and replaced by exp and sqrt on matrices? Is the answer no because it would be too error-prone?

1 Like

The expm and sqrtm functions are matrix operations, not elementwise operations. I expect they’ll be sticking around.

1 Like

Edit: Below is likely not what OP meant.

To be explicit:

julia> A = rand(2,2)
2×2 Array{Float64,2}:
 0.0692582  0.167208
 0.223925   0.360005

julia> expm(A'A)
2×2 Array{Float64,2}:
 1.06113  0.10272
 0.10272  1.17547

julia> exp.(A'A)
2×2 Array{Float64,2}:
 1.05648  1.09658
 1.09658  1.17065

julia> expm(A'A) ≈ exp.(A'A)
false

But having exp. to mean elementwise operations, wouldn’t it be correct for exp to mean matrix exponentiation (although possibly against all other languages)?

4 Likes

Ah, that makes more sense. Hm, I think that would perhaps be error prone. On the other hand, it might enable generic code that will both work with matrices and scalars?

1 Like

Some prior discussion here: https://github.com/JuliaLang/julia/issues/5840#issuecomment-237077416

Thanks. Sorry for being unclear. Yes, I meant using exp for matrix exponentiation

Yes, exponential integrators, integration factor methods, and exponential time differencing methods would be nicer to write with this. You can get around this with a simple if <: AbstractArray, but if exp “just worked” then the code would be much nicer. So there’s at least a use case in DiffEqs (that I ran into quite recently).

3 Likes

It sounds tempting, but then what about fast vectorised versions of exp etc, see e.g. AppleAccelerate.jl

Also note that expm and sqrtm can be applied to scalars, so the canonical way to write exponential integrators would probably be to use expm from the start?

They can overload broadcast and broadcast!

I did not know that… you make a great point sir.

Interesting- then maybe this could be a nice (if not crucial) change.

I think all these types of functions should work on matrices, on the guiding principal that if a Taylor (or similar) expansion works for both Number and square Matrix, then why wouldn’t the same function apply to both?? Julia is meant to support generic mathematical programming, after all…

5 Likes

But then expm is merely a superset of exp?

Not really. Exp applied to a matrix is not the same as expm.

@cortner If the element-wise exp([1., 2.]) becomes deprecated, then expm will be a superset of exp.

I guess you are right then - I am still thinking 0.5; maybe another argument in favour deprecating expm

IMHO it would be confusing for new users. People will frequently expect exp(::AbstractMatrix) to apply exp to each entry in the matrix. Better keep separate functions.

1 Like

At least while only a subset of functions have a “proper” matrix version, distinguishing the latter is a reasonable alternative, even if it comes at the cost of elegance.

Conversely, expm should only be merged to exp when cos(::AbstractMatrix) and similar throw an error.

1 Like

So 0.7 then (when the deprecations are removed)?