From d8ae262583e84d0e33877992bb87e9165bd9631e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Vigil-V=C3=A1squez?= Date: Mon, 15 Dec 2025 00:01:01 +0100 Subject: [PATCH] refactor: rename some arguments for consistency --- src/types.jl | 84 +++++++++++++++++++++++----------------------- test/operations.jl | 10 +++--- test/types.jl | 17 +++++----- 3 files changed, 55 insertions(+), 56 deletions(-) diff --git a/src/types.jl b/src/types.jl index 9c6a61f..4d76fd7 100644 --- a/src/types.jl +++ b/src/types.jl @@ -4,6 +4,7 @@ types.jl Implements the basic types for the different hypervectors (wrappers for ordinary vectors) Contains: +- AbstractHV - BinaryHV - BipolarHV - TernaryHV @@ -17,8 +18,8 @@ Every hypervector HV has the following basic functionality TODO: - [ ] SparseHV -- [ ] support for different types - [ ] complex HDC +- [ ] support for different types =# # ----------------------------------------------------------------------------------- AbstractHV @@ -27,7 +28,7 @@ abstract type AbstractHV{T} <: AbstractVector{T} end Base.copy(hv::HV) where {HV <: AbstractHV} = HV(copy(hv.v)) Base.getindex(hv::AbstractHV, i) = hv.v[i] Base.hash(hv::AbstractHV) = hash(hv.v) -Base.similar(hv::T) where {T <: AbstractHV} = T(; dims = length(hv)) +Base.similar(hv::T) where {T <: AbstractHV} = T(; D = length(hv)) Base.size(hv::AbstractHV) = size(hv.v) Base.sum(hv::AbstractHV) = sum(hv.v) @@ -41,26 +42,25 @@ empty_vector(hv::AbstractHV) = zero(hv.v) # ------------------------------------------------------------------------------------ BipolarHV struct BipolarHV <: AbstractHV{Int} v::BitVector - BipolarHV(v::AbstractVector{Bool}) = new(v) end function BipolarHV(; - dims::Integer = 10_000, + D::Integer = 10_000, seed::Union{Number, Nothing} = nothing, rng = MersenneTwister ) rng_instance = isnothing(seed) ? rng() : rng(seed) - return BipolarHV(bitrand(rng_instance, dims)) + return BipolarHV(bitrand(rng_instance, D)) end function BipolarHV( - s::Any; - dims::Integer = 10_000, + this::Any; + D::Integer = 10_000, rng = MersenneTwister ) - rng_instance = rng(hash(s)) - return BipolarHV(bitrand(rng_instance, dims)) + rng_instance = rng(hash(this)) + return BipolarHV(bitrand(rng_instance, D)) end BipolarHV(v::AbstractVector{<:Integer}) = BipolarHV(v .> 0) @@ -95,21 +95,21 @@ struct TernaryHV <: AbstractHV{Int} end function TernaryHV(; - dims::Integer = 10_000, + D::Integer = 10_000, seed::Union{Integer, Nothing} = nothing, rng = Random.MersenneTwister ) rng_instance = isnothing(seed) ? rng() : rng(seed) - return TernaryHV(rand(rng_instance, (-1, 1), dims)) + return TernaryHV(rand(rng_instance, (-1, 1), D)) end function TernaryHV( - s::Any; - dims::Integer = 10_000, + this::Any; + D::Integer = 10_000, rng = Random.MersenneTwister ) - rng_instance = rng(hash(s)) - return TernaryHV(rand(rng_instance, (-1, 1), dims)) + rng_instance = rng(hash(this)) + return TernaryHV(rand(rng_instance, (-1, 1), D)) end # Helpers @@ -141,21 +141,21 @@ struct BinaryHV <: AbstractHV{Bool} end function BinaryHV(; - dims::Integer = 10_000, + D::Integer = 10_000, seed::Union{Integer, Nothing} = nothing, rng = Random.MersenneTwister ) rng_instance = isnothing(seed) ? rng() : rng(seed) - return BinaryHV(bitrand(rng_instance, dims)) + return BinaryHV(bitrand(rng_instance, D)) end function BinaryHV( - s::Any; - dims::Integer = 10_000, + this::Any; + D::Integer = 10_000, rng = Random.MersenneTwister ) - rng_instance = rng(hash(s)) - return BinaryHV(bitrand(rng_instance, dims)) + rng_instance = rng(hash(this)) + return BinaryHV(bitrand(rng_instance, D)) end # Helpers @@ -176,27 +176,27 @@ end function RealHV(; distr::Distribution = eldist(RealHV), - dims::Integer = 10_000, + D::Integer = 10_000, seed::Union{Integer, Nothing} = nothing, rng = Random.MersenneTwister ) rng_instance = isnothing(seed) ? rng() : rng(seed) - return RealHV(rand(rng_instance, distr, dims), distr) + return RealHV(rand(rng_instance, distr, D), distr) end function RealHV( - s::Any; - dims::Integer = 10_000, + this::Any; + D::Integer = 10_000, distr::Distribution = eldist(RealHV), rng = Random.MersenneTwister ) - rng_instance = rng(hash(s)) - return RealHV(rand(rng_instance, distr, dims), distr) + rng_instance = rng(hash(this)) + return RealHV(rand(rng_instance, distr, D), distr) end # Helpers Base.copy(hv::RealHV) = RealHV(copy(hv.v), hv.distr) -Base.similar(hv::RealHV) = RealHV(; dims = length(hv), distr = hv.distr) +Base.similar(hv::RealHV) = RealHV(; D = length(hv), distr = hv.distr) function normalize!(hv::RealHV) hv.v .*= std(hv.distr) / std(hv.v) return hv @@ -216,30 +216,30 @@ struct GradedHV{T <: Real} <: AbstractHV{T} end function GradedHV(; - dims::Integer = 10_000, + D::Integer = 10_000, distr::Distribution = eldist(GradedHV), seed::Union{Integer, Nothing} = nothing, rng = Random.MersenneTwister ) @assert 0 ≤ minimum(distr) < maximum(distr) ≤ 1 "Provide `distr` with support in [0,1]" rng_instance = isnothing(seed) ? rng() : rng(seed) - return GradedHV(rand(rng_instance, distr, dims), distr) + return GradedHV(rand(rng_instance, distr, D), distr) end function GradedHV( - s::Any; - dims::Integer = 10_000, + this::Any; + D::Integer = 10_000, distr::Distribution = eldist(GradedHV), rng = Random.MersenneTwister ) @assert 0 ≤ minimum(distr) < maximum(distr) ≤ 1 "Provide `distr` with support in [0,1]" - rng_instance = rng(hash(s)) - return GradedHV(rand(rng_instance, distr, dims), distr) + rng_instance = rng(hash(this)) + return GradedHV(rand(rng_instance, distr, D), distr) end # Helpers Base.copy(hv::GradedHV) = GradedHV(copy(hv.v), hv.distr) -Base.similar(hv::GradedHV) = GradedHV(; dims = length(hv), distr = eldist(GradedHV)) +Base.similar(hv::GradedHV) = GradedHV(; D = length(hv), distr = eldist(GradedHV)) Base.zeros(hv::GradedHV) = fill!(similar(hv.v), one(eltype(hv.v)) / 2) LinearAlgebra.normalize!(hv::GradedHV) = clamp!(hv.v, 0, 1) eldist(::Type{<:GradedHV}) = Beta(1, 1) @@ -258,30 +258,30 @@ struct GradedBipolarHV{T <: Real} <: AbstractHV{T} end function GradedBipolarHV(; - dims::Integer = 10_000, + D::Integer = 10_000, distr::Distribution = eldist(GradedBipolarHV), seed::Union{Integer, Nothing} = nothing, rng = Random.MersenneTwister ) @assert -1 ≤ minimum(distr) < maximum(distr) ≤ 1 "Provide `distr` with support in [-1,1]" rng_instance = isnothing(seed) ? rng() : rng(seed) - return GradedBipolarHV(rand(rng_instance, distr, dims), distr) + return GradedBipolarHV(rand(rng_instance, distr, D), distr) end function GradedBipolarHV( - s::Any; - dims::Integer = 10_000, + this::Any; + D::Integer = 10_000, distr::Distribution = eldist(GradedBipolarHV), rng = Random.MersenneTwister ) @assert -1 ≤ minimum(distr) < maximum(distr) ≤ 1 "Provide `distr` with support in [-1,1]" - rng_instance = rng(hash(s)) - return GradedBipolarHV(rand(rng_instance, distr, dims), distr) + rng_instance = rng(hash(this)) + return GradedBipolarHV(rand(rng_instance, distr, D), distr) end # Helpers Base.copy(hv::GradedBipolarHV) = GradedBipolarHV(copy(hv.v), hv.distr) -Base.similar(hv::GradedBipolarHV) = GradedBipolarHV(; dims = length(hv), distr = hv.distr) +Base.similar(hv::GradedBipolarHV) = GradedBipolarHV(; D = length(hv), distr = hv.distr) LinearAlgebra.normalize!(hv::GradedBipolarHV) = clamp!(hv.v, -1, 1) eldist(::Type{<:GradedBipolarHV}) = 2Beta(1, 1) - 1 diff --git a/test/operations.jl b/test/operations.jl index 52032c3..7e6e271 100644 --- a/test/operations.jl +++ b/test/operations.jl @@ -13,8 +13,8 @@ using LinearAlgebra, Random @testset "operations $HV" begin - hv1 = HV(; dims = N) - hv2 = HV(; dims = N) + hv1 = HV(; D = N) + hv2 = HV(; D = N) v1 = collect(hv1) v2 = collect(hv2) @@ -56,9 +56,9 @@ using LinearAlgebra, Random @testset "similarity $HV" begin N = 10_000 - hv1 = HV(; dims = N) - hv2 = HV(; dims = N) - hv3 = HV(; dims = N) + hv1 # similar to 1 but not to 2 + hv1 = HV(; D = N) + hv2 = HV(; D = N) + hv3 = HV(; D = N) + hv1 # similar to 1 but not to 2 normalize!(hv3) @test !(hv1 ≈ hv2) diff --git a/test/types.jl b/test/types.jl index 90cc834..4489193 100644 --- a/test/types.jl +++ b/test/types.jl @@ -6,7 +6,7 @@ using Distributions, LinearAlgebra @testset "types" begin @testset "BipolarHV" begin - hdv = BipolarHV(; dims = n) + hdv = BipolarHV(; D = n) @test length(hdv) == n @test eltype(hdv) <: Int @@ -20,7 +20,7 @@ using Distributions, LinearAlgebra end @testset "BinaryHV" begin - hdv = BinaryHV(; dims = n) + hdv = BinaryHV(; D = n) @test length(hdv) == n @test eltype(hdv) <: Bool @@ -32,7 +32,7 @@ using Distributions, LinearAlgebra end @testset "TernaryHV" begin - hdv = TernaryHV(; dims = n) + hdv = TernaryHV(; D = n) @test length(hdv) == n @test eltype(hdv) <: Int @@ -44,7 +44,7 @@ using Distributions, LinearAlgebra end @testset "GradedBipolarHV" begin - hdv = GradedBipolarHV(; dims = n) + hdv = GradedBipolarHV(; D = n) @test length(hdv) == n @test eltype(hdv) <: Real @@ -56,7 +56,7 @@ using Distributions, LinearAlgebra #@test eltype(GradedBipolarHV(Float32, n)) <: Float32 @test GradedBipolarHV(s) == GradedBipolarHV(; seed = hash_s) - @test GradedBipolarHV(; dims = n, distr = 2Beta(10, 2) - 1) isa GradedBipolarHV + @test GradedBipolarHV(; D = n, distr = 2Beta(10, 2) - 1) isa GradedBipolarHV hv2 = GradedBipolarHV([0.1, 1.12, -0.2, -3.0]) normalize!(hv2) @@ -65,7 +65,7 @@ using Distributions, LinearAlgebra end @testset "GradedHV" begin - hdv = GradedHV(; dims = n) + hdv = GradedHV(; D = n) @test length(hdv) == n @test eltype(hdv) <: Real @@ -77,16 +77,15 @@ using Distributions, LinearAlgebra #@test eltype(GradedHV(Float32, n)) <: Float32 @test GradedHV(s) == GradedHV(; seed = hash_s) - @test GradedHV(; dims = n, distr = Beta(10, 2)) isa GradedHV + @test GradedHV(; D = n, distr = Beta(10, 2)) isa GradedHV hv2 = GradedHV([0.1, 1.12, -0.2, -3.0]) normalize!(hv2) @test all(0 .≤ hv2 .≤ 1) end - @testset "RealHV" begin - hdv = RealHV(; dims = n) + hdv = RealHV(; D = n) @test length(hdv) == n @test eltype(hdv) <: Real