Skip to content

Commit

Permalink
Use next-odd basis to construct hash tables (#316)
Browse files Browse the repository at this point in the history
* Use next-odd basis to construct hash tables

and revert #314

* Bump patch version
  • Loading branch information
lbenet authored Feb 23, 2023
1 parent 1daed03 commit c4b474d
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 129 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "TaylorSeries"
uuid = "6aa5eb33-94cf-58f4-a9d0-e4b2c4fc25ea"
repo = "https://github.com/JuliaDiff/TaylorSeries.jl.git"
version = "0.13.1"
version = "0.13.2"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand Down
1 change: 0 additions & 1 deletion docs/src/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ _InternalMutFuncs
generate_tables
generate_index_vectors
in_base
in_base_safe
make_inverse_dict
resize_coeffs1!
resize_coeffsHP!
Expand Down
42 changes: 15 additions & 27 deletions src/hash_tables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,19 @@ of the corresponding monomial in `coeffs_table`.
function generate_tables(num_vars, order)
coeff_table = [generate_index_vectors(num_vars, i) for i in 0:order]

index_table = Vector{Int}[map(x->in_base_safe(order, x), coeffs) for coeffs in coeff_table]
index_table = Vector{Int}[map(x->in_base(order, x), coeffs) for coeffs in coeff_table]

# Check uniqueness of labels as "non-collision" test
@assert all(allunique.(index_table))

pos_table = map(make_inverse_dict, index_table)
size_table = map(length, index_table)

coeff_table, index_table, size_table, pos_table
# The next line tests the consistency of the number of monomials,
# but it's commented because it may not pass due to the `binomial`
# @assert sum(size_table) == binomial(num_vars+order, min(num_vars,order))

return (coeff_table, index_table, size_table, pos_table)
end

"""
Expand All @@ -61,7 +68,7 @@ function generate_index_vectors(num_vars, degree)

end

indices
return indices
end


Expand All @@ -77,44 +84,25 @@ the corresponding index.
It is used to construct `pos_table` from `index_table`.
"""
function make_inverse_dict(v::Vector)
Dict(Dict(x=>i for (i,x) in enumerate(v)))
end
make_inverse_dict(v::Vector) = Dict(Dict(x=>i for (i,x) in enumerate(v)))

"""
in_base(order, v)
Convert vector `v` of non-negative integers to base `order+1`.
Convert vector `v` of non-negative integers to base `oorder`, where
`oorder` is the next odd integer of `order`.
"""
function in_base(order, v)
order = order+1
oorder = iseven(order) ? order+1 : order+2 # `oorder` is the next odd integer to `order`

result = 0

all(iszero.(v)) && return result

for i in v
result = result*order + i
result = result*oorder + i
end

result
end

"""
in_base_safe(order, v)
Same as `in_base` assuring positivity of the result;
used for constructing `index_table`.
"""
function in_base_safe(order, v)
result = in_base(order, v)

iszero(v) && return result

(result <= 0) && throw(OverflowError("""
Using numvars=$(length(v)) at order=$(order) produces
a non-positive index_table entry: $result."""))

return result
end

Expand Down
Loading

2 comments on commit c4b474d

@lbenet
Copy link
Member Author

@lbenet lbenet commented on c4b474d Feb 23, 2023

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/78297

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.13.2 -m "<description of version>" c4b474d5cac3d44b95144c42ad4b740fa1d9dbbe
git push origin v0.13.2

Please sign in to comment.