Skip to content
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

New logm implementation #21571

Closed
wants to merge 9 commits into from
Closed

New logm implementation #21571

wants to merge 9 commits into from

Conversation

iamnapo
Copy link
Contributor

@iamnapo iamnapo commented Apr 26, 2017

Fixed 21179

Original algorithm can be found here.

for j = 3:tmax
if alpha3 <= theta[j]
break
m = 0; m += (alpha2 <= theta[1] ? 1 : 2)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why the = 0 then immediately += ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my mistake

# computes the principal square root X
# of the matrix A using the product form of the Denman-Beavers
# iteration. The matrix M tends to I.
# Copyright (c) 2008 by N. J. Higham
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

he's been very nice before about granting permission to relicense derived translations of his implementations, did a previous instance of that also cover this bit of code?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wellsqrtm_dbp is internally called by logm, for which he has given permission to use. So yes, I guess.

cc @higham

@@ -473,7 +473,7 @@ for (funm, func) in ([:logm,:log], [:sqrtm,:sqrt])
@eval begin
function ($funm){T<:Real}(A::Symmetric{T})
F = eigfact(A)
if isposdef(F)
if all(λ -> λ ≥ 0, F.values)
Copy link
Member

@fredrikekre fredrikekre Apr 26, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this change? isposdef(F) is more clear IMO and does the same thing? Same just below too.

Edit: Nevermind, isposdef checks λ > 0

@tkelman
Copy link
Contributor

tkelman commented Apr 26, 2017

Would be good to add the cases from JuliaLang/LinearAlgebra.jl#415 as tests, and maybe a handful more that are wrong or broken on master but fixed here.

@kshyatt kshyatt added the linear algebra Linear algebra label Apr 26, 2017
@@ -551,40 +551,26 @@ julia> logm(A)
0.0 1.0
```
"""
function logm(A::StridedMatrix{T}) where T
function logm{T}(A::StridedMatrix{T})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should go back the way it was

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For future reference, is there something wrong with this, or is it just the preferred syntax?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the logm{T} version is likely to be deprecated for 0.7/1.0

@@ -454,31 +454,55 @@ end
A5 = convert(Matrix{elty}, [1 1 0 1; 0 1 1 0; 0 0 1 1; 1 0 0 1])
@test expm(logm(A5)) ≈ A5

A6 = convert(Matrix{elty}, [-5 2 0 0 ; 1/2 -7 3 0; 0 1/3 -9 4; 0 0 1/4 -11])
@test expm(logm(A6)) ≈ A6
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why remove this one?
(likewise with @test (Ab^im)^(-im) ≈ Ab)

Copy link
Contributor Author

@iamnapo iamnapo Apr 28, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because A6, Ab have negative eigenvalues so logm is not unique and the logarithm returned on these two occasions isn't one that expm(logm(A)) == A.
(Ab^im = expm(im*logm(A)))

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so the new algorithm returns a different root? should we test the new result instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure how to test that

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

compare the result against its expected value written out? is the root this algorithm now returns consistent with what any other libraries return, like wolfram or matlab or octave, or would we be making a new outlier choice?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now I'm not following. Even if logm(A) isn't unique, any such solution should satisfy expm(logm(A)) == A, shouldn't it? How is it a valid logm(A) otherwise?

Copy link
Contributor Author

@iamnapo iamnapo Apr 28, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes yes, my previous comment was wrong. If A has negative eigenvalues, logm is not unique and this algorithm's results may be inaccurate.

I'll work on it more.

@iamnapo
Copy link
Contributor Author

iamnapo commented Apr 28, 2017

It appears that the previous implementation was better and the problem was elsewhere.
Closing this PR and open the correct one shortly.

@iamnapo iamnapo closed this Apr 28, 2017
@YingboMa
Copy link
Contributor

YingboMa commented May 6, 2017

@iamnapo If you are using MATLAB as a comparison, be aware that this algorithm is not as robust as the MATLAB build-in algorithm for non-principal matrix.

@iamnapo
Copy link
Contributor Author

iamnapo commented May 6, 2017

Yeap, that's why I closed this PR.

@YingboMa
Copy link
Contributor

YingboMa commented May 6, 2017

Yeah, I tried to write a PR, and I cannot figure it out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
linear algebra Linear algebra
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants