-
-
Notifications
You must be signed in to change notification settings - Fork 117
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
Add normalize
and normalize!
#253
Conversation
Failing on v0.3 :( I added docstrings to the functions, but there's no |
@ranjanan I think the standard way to deal with docstrings is to use the syntax without """
Docstring
"""
function x() end This degrades to a no-op on v0.3, and will produce the correct docstring on v0.4. |
Yes, I tried that first, but the docs won't show up at |
It is really no surprise that docs won't show up on v0.3, as docstrings were not implemented until v0.4. The The docs should show up on v0.4 without issue using the docstring syntax. |
@TotalVerb I meant that on v0.4, the normal docstring syntax within an |
@ranjanan Ah, you are correct. I apologize for the misunderstanding. |
@@ -1085,6 +1085,43 @@ if !isdefined(Base, :Threads) | |||
export Threads | |||
end | |||
|
|||
if !isdefined(Base, :normalize) | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
don't put empty lines at the beginning or end of blocks like this
needs rebasing |
You might need to provide copy_oftype too. |
@@ -1286,3 +1286,12 @@ let | |||
@test norm(w) === 1.0 | |||
@test norm(normalize!(v) - w, Inf) < eps() | |||
end | |||
|
|||
# 0.5 style single argument `@boundscheck` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rebasing, not merging
@tkelman , yes, but I screwed up. I rebased on my local branch and my push got rejected, so I ended merging stuff from my remote branch on my local branch. I need to get better at this. |
when you rebase you need to force push over your branch |
So the The same test passes on master but doesn't pass over here, even though I used the same implementations and same tests. @tkelman am I doing anything obviously wrong? |
Present in LinAlg module in v0.4 and v0.5 but not v0.3, so defined instead of importing
δ = inv(prevfloat(typemax(Float64))) | ||
v = [δ, -δ] | ||
|
||
@test norm(v) === 7.866824069956793e-309 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is where the v0.3 test fails. norm(v)
gives Inf
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You know what, I'm not sure why I'm adding this test for norm. I'm testing normalize and normalize!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, actually it seems that norm
itself behaves differently on v0.3 as compared to v0.4 and v0.5.
Okay, the function cc: @tkelman |
At some point the |
@@ -1301,9 +1301,13 @@ end | |||
let | |||
δ = inv(prevfloat(typemax(Float64))) | |||
v = [δ, -δ] | |||
|
|||
if VERSION > v"0.4.0-" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
VERSION
>= v"0.4.0-pre+7164", for JuliaLang/julia#1861
w = normalize(v) | ||
@test w ≈ [1/√2, -1/√2] | ||
@test norm(w) === 1.0 | ||
if VERSION > v"0.4.0-" | ||
@test norm(w) === 1.0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems too strict. Should be @test norm(w) ≈ 1.0
, and this should work on any version of Julia.
w = normalize(v) | ||
@test w ≈ [1/√2, -1/√2] | ||
@test_approx_eq norm(w) 1.0 | ||
@test norm(normalize!(v) - w, Inf) < eps() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't matter in this case, but ≈ is preferred to @test_approx_eq
which is broken on some numeric types.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@TotalVerb You're right. I guess I'll use isapprox
instead from now on. Would you like me to change it in this PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Better to use @test norm(w) ≈ 1.0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@TotalVerb @KristofferC it's weird, but the test fails on Julia v0.4 using isapprox
but passes with @test_approx_eq
@ranjanan The problem you are seeing on v0.4 is not caused by this change, merely exposed by it. It is rather a leaked rounding mode introduced by a different test. Here, try adding the following line to reset the rounding mode, so setrounding(T, RoundDown)
@test rounding(T) == RoundDown becomes setrounding(T, RoundDown)
@test rounding(T) == RoundDown
setrounding(T, RoundNearest) |
LGTM. Thanks for the contribution! |
Add
normalize
andnormalize!
, which normalizes a vector according to the p-norm. Fixes #246 .