Comments (5)
I've added the hash
method in #104. However, due to the fact isequal(x, y)
must imply hash(x) == hash(y)
we have this weird situation:
julia> x = 3 ± 0.1
3.0 ± 0.1
julia> a = 3 ± 0.1
3.0 ± 0.1
julia> x === a
false
julia> d = Dict(x => 42)
Dict{Measurement{Float64}, Int64} with 1 entry:
3.0±0.1 => 42
julia> d[x]
42
julia> d[a]
42
julia> d[a] = 0
0
julia> d[a]
0
julia> d[x]
0
I don't think this can be avoided given the requirement on the relation between isequal
and hash
from measurements.jl.
Frankly, I've never implemented hashing because
- I don't understand how it's supposed to work for non-standard numbers (and lack of documentation about this doesn't help much)
- so far I never had to implement them in practice.
I'm happy to do it if someone can help in particular with point 1, or if they open a pull request 🙂
from measurements.jl.
Taking inspiration from other packages:
- AutoHashEquals.jl hashes all the contents together like in my first example.
- DualNumbers.jl uses
bitmix
, but I'm not sure wherebitmix
comes from.
from measurements.jl.
Out of curiosity, did you find a case in practice where the hash
method was needed? I'm aware the method isn't implemented, but I couldn't find a real-world case in my applications where this would be needed.
from measurements.jl.
One instance it using a Measurement as a key in a Dict:
julia> using Measurements
julia> m = 1.0 ± 0.1
1.0 ± 0.1
julia> Dict(m=>"anything else here")
ERROR: MethodError: no method matching decompose(::Measurement{Float64})
Closest candidates are:
decompose(::Integer) at float.jl:555
decompose(::Float16) at float.jl:557
decompose(::Float32) at float.jl:568
...
Stacktrace:
[1] hash(x::Measurement{Float64}, h::UInt64)
@ Base ./float.jl:491
[2] hash(x::Measurement{Float64})
@ Base ./hashing.jl:18
[3] hashindex(key::Measurement{Float64}, sz::Int64)
@ Base ./dict.jl:169
[4] ht_keyindex2!(h::Dict{Measurement{Float64}, String}, key::Measurement{Float64})
@ Base ./dict.jl:310
[5] setindex!(h::Dict{Measurement{Float64}, String}, v0::String, key::Measurement{Float64})
@ Base ./dict.jl:383
[6] Dict{Measurement{Float64}, String}(kv::Tuple{Pair{Measurement{Float64}, String}})
@ Base ./dict.jl:104
[7] Dict(ps::Pair{Measurement{Float64}, String})
@ Base ./dict.jl:124
[8] top-level scope
@ REPL[3]:1
julia> Base.hash(m::Measurement) = foldr(hash, (m.val, m.err, m.der, m.tag))
julia> Dict(m=>"anything else here")
Dict{Measurement{Float64}, String} with 1 entry:
1.0±0.1 => "anything else here"
from measurements.jl.
Related Issues (20)
- making measurements work with Printf
- Adding measurement components back to a measurement after iteratively solving for a value HOT 7
- [FR] Plot recipe: ribbon plots option beside error bars HOT 11
- tryparse for Measurement type
- Can't use unique with measurements HOT 1
- Measurements with missing errors HOT 4
- Measurements.value(x::Missing) = missing HOT 1
- Integration with Zygote
- `weightedmean()` returns `NaN ± 0`? HOT 1
- Use auto-differentiation engine
- Bad integration with Plots' boxplot HOT 2
- Move to pkgextensions for Julia v1.9+
- one(measurement) should return 1, not 1 ± 0 HOT 8
- `Symbolics.jl` support? HOT 7
- Plotting mixture of measurements and missing data HOT 1
- Trying to use Measurements to differentiate respect to a unitful quantity. HOT 12
- Is there an autodiff package that is compatible? HOT 3
- Broken `MeasurementsJunoExt.jl` HOT 1
- Julia 1.6 incompatibility from stdlib compat bounds HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from measurements.jl.