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

optional sizehint for building matrix #199

Merged
merged 3 commits into from
Mar 30, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
init
  • Loading branch information
christofbradly committed Mar 30, 2023
commit 1ff8795f5e3c9b0c47d2553d0f7fc3ee1de53f30
10 changes: 7 additions & 3 deletions src/Hamiltonians/abstract.jl
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ momentum

"""
sm, basis = build_sparse_matrix_from_LO(
ham, address=starting_address(ham); cutoff, filter=nothing, nnzs, sort=false, kwargs...
ham, address=starting_address(ham); cutoff, filter=nothing, nnzs, col_hint, sort=false, kwargs...
)

Create a sparse matrix `sm` of all reachable matrix elements of a linear operator `ham`
Expand All @@ -155,6 +155,10 @@ configurations.
Providing the number `nnzs` of expected calculated matrix elements may improve performance.
The default estimates for `nnzs` is `dimension(ham)`.

Setting a custom `col_hint` may improve performance for cases where `num_offdiagonals` is
not a good estimate of the actual number of nonzero offdiagonal elements in each column.
Defaults to `num_offdiagonals(ham, address)`.

Providing an energy cutoff will skip the columns and rows with diagonal elements greater
than `cutoff`. Alternatively, an arbitrary `filter` function can be used instead. These are
not enabled by default.
Expand All @@ -169,7 +173,7 @@ function build_sparse_matrix_from_LO(
ham, address=starting_address(ham);
cutoff=nothing,
filter=isnothing(cutoff) ? nothing : (a -> diagonal_element(ham, a) ≤ cutoff),
nnzs=dimension(ham),
nnzs=dimension(ham), col_hint=num_offdiagonals(ham, address),
sort=false, kwargs...,
)
if !isnothing(filter) && !filter(address)
Expand All @@ -182,7 +186,7 @@ function build_sparse_matrix_from_LO(
adds = [address] # Queue of addresses. Also returned as the basis.
dict = Dict(address => 1) # Mapping from addresses to indices
col = Dict{Int,T}() # Temporary column storage
sizehint!(col, num_offdiagonals(ham, address))
sizehint!(col, col_hint)

is = Int[] # row indices
js = Int[] # column indice
Expand Down