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

Add normalize and normalize! #253

Merged
merged 12 commits into from
Aug 11, 2016
Merged

Conversation

ranjanan
Copy link
Contributor

Add normalize and normalize!, which normalizes a vector according to the p-norm. Fixes #246 .

@ranjanan
Copy link
Contributor Author

Failing on v0.3 :( I added docstrings to the functions, but there's no @doc defined for v0.3.

@TotalVerb
Copy link
Contributor

@ranjanan I think the standard way to deal with docstrings is to use the syntax without @doc, that is:

"""
Docstring
"""
function x() end

This degrades to a no-op on v0.3, and will produce the correct docstring on v0.4.

@ranjanan
Copy link
Contributor Author

Yes, I tried that first, but the docs won't show up at ?help. @yuyichao told me this was by design. That was why I shifted to using @doc.

@TotalVerb
Copy link
Contributor

It is really no surprise that docs won't show up on v0.3, as docstrings were not implemented until v0.4. The @doc macro won't really help, as that was also implemented in v0.4.

The docs should show up on v0.4 without issue using the docstring syntax.

@ranjanan
Copy link
Contributor Author

ranjanan commented Jul 16, 2016

@TotalVerb I meant that on v0.4, the normal docstring syntax within an if block does not work (that is, show up at the REPL) by design. (But using @doc on v0.4 would make it show up at the REPL.) On v0.3 @doc wasn't part of Base Julia so it's not going to work. That is why, at @tkelman 's suggestion, I removed the docstrings and linked to it in the README.

@TotalVerb
Copy link
Contributor

@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)

Copy link
Contributor

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

@tkelman
Copy link
Contributor

tkelman commented Jul 16, 2016

needs rebasing

@TotalVerb
Copy link
Contributor

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`
Copy link
Contributor

Choose a reason for hiding this comment

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

rebasing, not merging

@ranjanan
Copy link
Contributor Author

@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.

@tkelman
Copy link
Contributor

tkelman commented Jul 18, 2016

when you rebase you need to force push over your branch

@ranjanan
Copy link
Contributor Author

ranjanan commented Jul 18, 2016

So the norm test is failing. Which is kinda weird, I copied this test from the Julia tests on master.

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?

δ = inv(prevfloat(typemax(Float64)))
v = [δ, -δ]

@test norm(v) === 7.866824069956793e-309
Copy link
Contributor Author

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

Copy link
Contributor Author

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!

Copy link
Contributor Author

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.

@ranjanan
Copy link
Contributor Author

ranjanan commented Aug 5, 2016

Okay, the function norm behaves differently in v0.3 as compared to v0.4 and v0.5. It seems to me that to support normalize and normalize!, we drop v0.3 support, or warn against flaky behaviour.

cc: @tkelman

@stevengj
Copy link
Member

stevengj commented Aug 5, 2016

At some point the norm function's behavior was improved to reduce overflow/underflow problems. I don't think it's a big deal if Compat.normalize has the same limitations as norm in a given Julia version.

@@ -1301,9 +1301,13 @@ end
let
δ = inv(prevfloat(typemax(Float64)))
v = [δ, -δ]

if VERSION > v"0.4.0-"
Copy link
Member

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
Copy link
Member

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()
Copy link
Contributor

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.

Copy link
Contributor Author

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?

Copy link
Sponsor Member

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

Copy link
Contributor Author

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

@TotalVerb
Copy link
Contributor

@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)

@TotalVerb
Copy link
Contributor

LGTM. Thanks for the contribution!

@blakejohnson blakejohnson merged commit 8912b94 into JuliaLang:master Aug 11, 2016
@ranjanan ranjanan deleted the RA/norm branch August 11, 2016 03:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants