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

Rewrite all @pure functions #105

Merged
merged 2 commits into from
Feb 20, 2017
Merged

Rewrite all @pure functions #105

merged 2 commits into from
Feb 20, 2017

Conversation

andyferris
Copy link
Member

  • Now we use Size more consistently and rely less on @pure implementations of similar_type.

  • similar and similar_type both use Size not integers to discuss size

  • similar and similar_type are now streamlined. Users only need to
    override one definition, just like in Base.

  • Associated documentation and test changes.

 * Now we use `Size` more consistently and rely less on `@pure` implementations of `similar_type`.

 * `similar` and `similar_type` both use `Size` not integers to discuss size

 * `similar` and `similar_type` are now streamlined. Users only need to
   override one definition, just like in Base.

 * Associated documentation and test changes.
@andyferris
Copy link
Member Author

This was a little tricky to get inference working well on v0.5. I had to use @generated in two places where on v0.6- @pure was sufficient - so it's good that the compiler is improving :)

But in general I think this reorganization is a very good thing.

@coveralls
Copy link

Coverage Status

Coverage increased (+1.06%) to 68.563% when pulling 5177a27 on ajf/nopure into 1be47cc on master.

@andyferris andyferris requested a review from c42f February 16, 2017 03:12
@andyferris
Copy link
Member Author

andyferris commented Feb 16, 2017

Test failure is caused by JuliaLang/julia#20620 on Julia master (v0.5 works).

(Ideally we would deal with this more fully but it seems orthogonal to this PR).

@coveralls
Copy link

Coverage Status

Coverage increased (+3.9%) to 71.389% when pulling e600e89 on ajf/nopure into 1be47cc on master.

Copy link
Member

@c42f c42f left a comment

Choose a reason for hiding this comment

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

Great effort Andy, this looks like a fair bit of work.

The result is a nice improvement, I think. I like how Size is fitting in here.

@pure Size{S}(::Type{MArray{S}}) = Size(S)
@pure Size{S,T}(::Type{MArray{S,T}}) = Size(S)
@pure Size{S,T,N}(::Type{MArray{S,T,N}}) = Size(S)
@pure Size{S,T,N,L}(::Type{MArray{S,T,N,L}}) = Size(S)
Copy link
Member

Choose a reason for hiding this comment

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

Do you know whether this kind of repetition go away with the new type system work in 0.6?

Copy link
Member Author

Choose a reason for hiding this comment

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

I think Size(::Type{T} where T <: MArray{S}) where S = Size(S) might be sufficient for all of the above, but I'd like to check. We could probably successfully move the @pure to the Size constructor and drop it from here, also.

@inline similar{SV <: StaticVector}(::SV) = MVector{length(SV),eltype(SV)}()
@inline similar{SV <: StaticVector, T}(::SV, ::Type{T}) = MVector{length(SV),T}()
similar{SA<:StaticArray,T}(::SA,::Type{T}) = similar(SA,T,Size(SA))
similar{SA<:StaticArray,T}(::Type{SA},::Type{T}) = similar(SA,T,Size(SA))
Copy link
Member

Choose a reason for hiding this comment

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

So now we've got similar based on input StaticArray type as well as value. I guess that makes sense as they all still return a value which can be written into. Did did we have it before anywhere?

Copy link
Member Author

Choose a reason for hiding this comment

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

I think so (or we should have) but it was all a bit dodgier...

src/core.jl Outdated
a `MVector`, `MMatrix` or `MArray`. You will benefit from overloading this if
your container is already mutable!
- `setindex!` on mutable arrays (e.g. those returned by `similar`)
- `setindex!` for a single elmenent (linear indexing).
Copy link
Member

Choose a reason for hiding this comment

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

elmenent

src/deque.jl Outdated
@generated function push(vec::StaticVector, x)
newtype = similar_type(vec, (length(vec) + 1 ,))
newtype = similar_type(vec, Size(length(vec) + 1 ,))
Copy link
Member

Choose a reason for hiding this comment

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

Minor tidy up: trailing comma no longer required here and in the rest of deque.jl.

@@ -54,7 +54,7 @@ end
error("Dimension mismatch")
end

exprs = [i == j ? :(a.λ + b[$(sub2ind(size(a), i, j))]) : :(b[$(sub2ind(size(a), i, j))]) for i = 1:n, j = 1:n]
exprs = [i == j ? :(a.λ + b[$(sub2ind(size(b), i, j))]) : :(b[$(sub2ind(size(b), i, j))]) for i = 1:n, j = 1:n]
Copy link
Member

Choose a reason for hiding this comment

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

I guess this never worked before?

Copy link
Member Author

Choose a reason for hiding this comment

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

No... we need to work to improve test coverage.

@@ -18,19 +18,21 @@
@test length(p) === 3
@test eltype(p) === Float64

@test (p + p)::Point3D ≈ Point3D(2.0, 4.0, 6.0)
Copy link
Member

Choose a reason for hiding this comment

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

Can use === here, I think, to check that both the elements and type are the same. (Elements should sum exactly, as the result is exactly representable in Float64.)

Copy link
Member Author

Choose a reason for hiding this comment

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

I never know when to trust floating point arithmetic

Copy link
Member

Choose a reason for hiding this comment

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

The rules say that basic floating point arithmetic operations should be correctly rounded. In particular, this means that if all intermediate results of a computation are exactly representable, then the final result is exact.

This certainly holds true when you're adding, subtracting and multiplying exact integers using floating point arithmetic, up until maxintfloat(Float64) == 9.007199254740992e15.

The reason I bring this up is that isapprox uses quite a generous default for rtol, a lot looser than you'd really want in many tests.

@test @SMatrix([1 2 3; 4 5 6])' === @SMatrix([1 4; 2 5; 3 6])
@test @inferred(ctranspose(@SVector([1, 2, 3]))) === @SMatrix([1 2 3])
@test @inferred(ctranspose(@SMatrix([1 2; 0 3]))) === @SMatrix([1 0; 2 3])
@test @inferred(ctranspose(@SMatrix([1 2 3; 4 5 6]))) === @SMatrix([1 4; 2 5; 3 6])
Copy link
Member

Choose a reason for hiding this comment

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

That's a lot of @inferred. You need a custom @testinferred macro or some kind of helper to make these easier to look at ;-)

Copy link
Member Author

Choose a reason for hiding this comment

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

:)

test/linalg.jl Outdated
@@ -82,4 +114,8 @@

@test normalize(SVector(1,2,3)) ≈ normalize([1,2,3])
end

@testset "trace" begin
@test trace(@SMatrix [1.0 2.0; 3.0 4.0]) ≈ 5.0
Copy link
Member

Choose a reason for hiding this comment

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

Can be exact equality, not \approx

@c42f
Copy link
Member

c42f commented Feb 20, 2017

I assume the CI failures in latest 0.6 are expected for now?

@coveralls
Copy link

Coverage Status

Coverage increased (+3.9%) to 71.389% when pulling 6344b4f on ajf/nopure into 1be47cc on master.

@andyferris andyferris merged commit e6d7387 into master Feb 20, 2017
@SimonDanisch SimonDanisch deleted the ajf/nopure branch May 17, 2018 10:57
oschulz pushed a commit to oschulz/StaticArrays.jl that referenced this pull request Apr 4, 2023
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.

3 participants