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

RFC: Matrix logarithm function (issue #5840) #12217

Merged
merged 2 commits into from
Jul 20, 2015

Conversation

mfasi
Copy link
Contributor

@mfasi mfasi commented Jul 19, 2015

I added the matrix logarithm function, with test cases. Although I tried to stick to the guidelines, it's my first contribution, please forgive any silly errors I might have committed.

@@ -278,9 +278,42 @@ function rcswap!{T<:Number}(i::Integer, j::Integer, X::StridedMatrix{T})
end
end

function logm{T}(A::StridedMatrix{T})
Copy link
Member

Choose a reason for hiding this comment

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

The type parameter T isn't used anywhere?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It looks like it isn't, should I remove it from the signature of the function?

Copy link
Member

Choose a reason for hiding this comment

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

Unless you're using it 1) in the function body, or 2) to filter the types for which this method applies, then yeah, it's not necessary.

Copy link
Member

Choose a reason for hiding this comment

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

As mentioned by @joehuchette, the T parameter should be removed here.

@jiahao
Copy link
Member

jiahao commented Jul 19, 2015

Ref #5840 - GitHub does not link issue numbers from titles

@tkelman tkelman added the linear algebra Linear algebra label Jul 19, 2015
@andreasnoack
Copy link
Member

Thanks for contributing this. It appears that (at least some of) the code is a translation of http://eprints.ma.man.ac.uk/1687/02/logm.zip. According to http://www.mathworks.com/matlabcentral/fileexchange/33393 it should be BSD licensed so there is no problem in using it, but I think you should link to the source in the initial comment.

# Use Schur decomposition
n = chksquare(A)
S,Q,d = schur(complex(A))
R = full(logm(UpperTriangular(S)))
Copy link
Member

Choose a reason for hiding this comment

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

I don't think full is needed here.

@tkelman
Copy link
Contributor

tkelman commented Jul 19, 2015

Note that there's the awful "Mathworks products only" clause in the File Exchange terms of use, see 2.a.iii. at http://www.mathworks.com/matlabcentral/termsofuse.html#content. So despite the BSD license on the code, unless it's available through a different medium or directly from the author (edit: with a clear license or permission to redistribute), having looked at any code hosted on File Exchange means we can't distribute a translation of it in Julia.

@andreasnoack
Copy link
Member

The first link above is to one of the authors' homepage so hopefully Mathworks can't claim this, but I cannot find any license statement in that source.

A = sqrtm(A)
end

AmI = A - UpperTriangular(eye(n))
Copy link
Member

Choose a reason for hiding this comment

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

You can do this more efficiently with A - I. I is a special constant of type UniformScaling.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It looks like it doesn't work if A is UpperTriangular, but

UpperTriangular(full(A) - I)

is even slower.

Copy link
Member

Choose a reason for hiding this comment

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

Okay. Now I understand. This is supposed to work so I'll fix this now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you. It looks like scale(A,x) has the same issue when A is UpperTriangular.

@ViralBShah
Copy link
Member

Perhaps we can write to the author and get clear documentation for the license.

end

AmI = A - UpperTriangular(eye(n))
d2 = norm(AmI^2, 1)^(1/2)
Copy link
Member

Choose a reason for hiding this comment

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

sqrt and cbrt are much faster

@mfasi
Copy link
Contributor Author

mfasi commented Jul 19, 2015

I received the matlab code form the author, I think I can ask him for the permission to redistribute. Anyway, thank you for all your useful comments.

@mfasi mfasi force-pushed the matrix_functions branch 2 times, most recently from 84090ed to d6add4a Compare July 20, 2015 10:21
@mfasi
Copy link
Contributor Author

mfasi commented Jul 20, 2015

The code is released under the terms of the BSD license. The author has just uploaded a new version of the .zip file containing the licence statement (http://eprints.ma.man.ac.uk/1851/02/logm.zip).

A = sqrtm(A)
end

AmI = UpperTriangular(full(A) - I)
Copy link
Member

Choose a reason for hiding this comment

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

This should be fixed now so you should be able to do AmI = A - I

@andreasnoack
Copy link
Member

Great that the license question was resolved so fast. I've added a few comments, but I think it should be ready soon.

@andreasnoack
Copy link
Member

@mfasi Do you think this pr is ready now? The AppVeyor failure is unrelated.

@mfasi
Copy link
Contributor Author

mfasi commented Jul 20, 2015

@andreasnoack All that I wanted to change is there and I'd be happy if you merged this pull request. Thank you for your feedback and support.

andreasnoack added a commit that referenced this pull request Jul 20, 2015
RFC: Matrix logarithm function (issue #5840)
@andreasnoack andreasnoack merged commit 7e7dfd3 into JuliaLang:master Jul 20, 2015
@andreasnoack
Copy link
Member

@mfasi Thank you for the contribution

@tkelman
Copy link
Contributor

tkelman commented Jul 20, 2015

We do actually need to reproduce the original license statement in full here, for derived code.

@Keno
Copy link
Member

Keno commented Jul 20, 2015

We can put it in License.md with a reference to which code falls under the license and just a note of This code is derived from code (c) The Author 2015, see License.md for full details.

@tkelman
Copy link
Contributor

tkelman commented Jul 20, 2015

There's a list of exceptions under LICENSE.md, and in contrib/add_license_to_files.jl, and I had modified a few of the license headers wherever files contained code derived from some other work. Or we could ask the author if they're willing to allow @mfasi to relicense his contribution under Julia's MIT license, then we could just put a comment indicating as much on this function.

@tkelman
Copy link
Contributor

tkelman commented Jul 20, 2015

This is also resulting in warnings which should be suppressed

     * linalg3              WARNING: Matrix with nonpositive real eigenvalues, a nonprimary matrix logarithm will be returned. 
WARNING: Matrix with nonpositive real eigenvalues, a nonprimary matrix logarithm will be returned. 
WARNING: Matrix with nonpositive real eigenvalues, a nonprimary matrix logarithm will be returned. 
WARNING: Matrix with nonpositive real eigenvalues, a nonprimary matrix logarithm will be returned.

@mfasi
Copy link
Contributor Author

mfasi commented Jul 20, 2015

@tkelman Sure, I can ask the author to relicense under Julia's MIT license. Is it enough if he just tells me that I can or should he add something to the original code?

I don't know how to suppress the warnings, nor I could find it anywhere. What should I do? The warnings are an expected behaviour in that case.

@tkelman
Copy link
Contributor

tkelman commented Jul 20, 2015

If you get written permission, we can add a comment indicating "Julia version relicensed with permission from original author" and I think it should be fine (though IANAL).

Do the warnings occur every time, or only some of the time depending on randomness in the input matrices? There are other cases in the tests that do redirect_stderr, or we could set a seed so the input values are the same every time. It is good to test warning code paths for the sake of code coverage, but we should do so in a way that suppresses expected warnings wherever possible.

@mfasi
Copy link
Contributor Author

mfasi commented Jul 20, 2015

I'll contact the author and let you know. There is no randomness involved, the warnings are produced every time you run the tests. Thus, I think that redirect_stderr would be fine in this case.

@ViralBShah
Copy link
Member

@mfasi Thanks for patiently following the process and getting logm in. @higham Thank you for providing logm under the BSD/MIT licenses.

Cc: @alanedelman

@tkelman
Copy link
Contributor

tkelman commented Jul 26, 2015

Just a hypothetical: how many tenured math, cs, and stats faculty around the world do you think we (+scipy, R, and octave communities) could get to co-sign a letter to the MathWorks complaining that the "may only be used with MathWorks products" clause is toxic (and hidden from users who otherwise think they're releasing code with a normal open source license) and asking them to drop it?

@ScottPJones
Copy link
Contributor

Good idea. Do you have a link to the toxic clause in the MathWorks license?

@mfasi
Copy link
Contributor Author

mfasi commented Jul 26, 2015

@ViralBShah You're very welcome. I'll add a link to this pull request in #5840.

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.

8 participants