-
Notifications
You must be signed in to change notification settings - Fork 12
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
Implement rand() #112
Implement rand() #112
Conversation
Codecov Report
@@ Coverage Diff @@
## master #112 +/- ##
==========================================
+ Coverage 85.80% 86.01% +0.21%
==========================================
Files 30 31 +1
Lines 2479 2496 +17
==========================================
+ Hits 2127 2147 +20
+ Misses 352 349 -3
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. |
It took me like 3 hours to understand the type system, but I think this whole implementation is going to be < 50 lines when done. 😂 |
Yes, that sounds very Julian: the ratio of time it takes to write code versus length of code can be pretty high (especially with lots of types flying around working together in undocumented ways as is the case here...sorry about that). On the one hand, one expects there to be an implementation of point_in_domain(d::CompositeDomain) = toexternalpoint(d, map(point_in_domain, components(d))) The julia> using DomainSets
julia> using DomainSets: ×
julia> d = (1..2.0) × UnitDisk()
(1.0..2.0) × UnitDisk()
julia> eltype(d)
SVector{3, Float64} (alias for StaticArrays.SArray{Tuple{3}, Float64, 1, 3})
julia> x = point_in_domain(d)
3-element StaticArrays.SVector{3, Float64} with indices SOneTo(3):
1.5
0.0
0.0
julia> DomainSets.tointernalpoint(d, x)
(1.5, [0.0, 0.0])
julia> typeof(ans)
Tuple{Float64, StaticArrays.SVector{2, Float64}}
julia> map(DomainSets.point_in_domain, components(d))
(1.5, [0.0, 0.0])
julia> DomainSets.toexternalpoint(d, ans)
3-element StaticArrays.SVector{3, Float64} with indices SOneTo(3):
1.5
0.0
0.0 The internal representation works will with Probably rand only makes sense for product domains. It would be difficult to define properly for unions, intersections and differences of domains (the other composite domains), because there is no guarantee about whether or not the composing domains have any overlap - the distribution could be skewed because of that. Same for mapped domains: some kind of random point could be defined but, unless the map is affine, what is its distribution? |
src/applications/random.jl
Outdated
Random.gentype(::Type{<:Domain{T}}) where T = T | ||
|
||
# Random.gentype(::Type{<:ProductDomain{T}}) where T = T | ||
Base.rand(rng::AbstractRNG, s::Random.SamplerTrivial{<:ProductDomain}) = map(i->(rand(rng, i)), factors(s[])) |
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.
I think here you'd want to put a toexternalpoint(d, outcome_of_map)
around it, where d
is the product domain
@daanhb I implemented the easy ones. I think this is a good enough minimal implementation to be merged, but I would be willing to take a stab at it if you think others should be included. |
Just the easy cases is fine! If it is isn't easy, then perhaps it should not be in this package after all. |
src/applications/random.jl
Outdated
# for low-dimensional balls, use rejection sampling - acceptance rate is at least 52% | ||
if dimension(b) <= 3 | ||
while true | ||
r = rand(rng, boundingbox(b)) |
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.
boundingbox(d)
allocates memory and is not very optimized, perhaps it is better to define box=boundingbox(b)
once before the while loop?
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.
boundingbox(d) allocates memory and is not very optimized
allocates memory even with StaticArrays? that is unfortunate.
This looks good. Could you try to add my suggestions in the issues you opened and see if that fixes the broken tests? |
Ok, everything is fixed - I think it's ready to go! |
I just noticed that julia> using DomainSets
julia> rand(ChebyshevInterval())
ERROR: ArgumentError: Sampler for this object is not defined which also causes julia> rand(UnitBall())
ERROR: ArgumentError: Sampler for this object is not defined The reason is that |
FYI @daanhb I fixed the issue you mentioned above at JuliaMath/IntervalSets.jl#116 |
Fix #109