Skip to content

Commit

Permalink
Fix minor things (#20)
Browse files Browse the repository at this point in the history
* Fix showcoeff for non-numeric coefficients

* Convenience constructor for NBodyMatrixElement from one NBodyTerm

* Allow rectangular energy expressions

* Bump version
  • Loading branch information
jagot authored Oct 14, 2022
1 parent a299d73 commit 2d2fa1a
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "EnergyExpressions"
uuid = "f4b57a2e-27c7-11e9-0cb0-edd185b706f6"
authors = ["Stefanos Carlström <[email protected]>"]
version = "0.1.1"
version = "0.1.2"

[deps]
AtomicLevels = "10933b4c-d60f-11e8-1fc6-bd9035a249a1"
Expand Down
6 changes: 3 additions & 3 deletions src/bit_configurations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -536,9 +536,9 @@ function Base.Matrix(bcs::BitConfigurations, rows, op::QuantumOperator, cols; ve
reduce(vcat, V), m, n)
end

function Base.Matrix(bcs::BitConfigurations, op::QuantumOperator; kwargs...)
m = length(bcs)
Matrix(bcs, 1:m, op, 1:m; kwargs...)
function Base.Matrix(bcs::BitConfigurations, op::QuantumOperator; left=Colon(), right=Colon(), kwargs...)
ms = 1:length(bcs)
Matrix(bcs, ms[left], op, ms[right]; kwargs...)
end

export BitConfigurations
4 changes: 3 additions & 1 deletion src/nbody_matrix_elements.jl
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,8 @@ struct NBodyMatrixElement
terms::Vector{NBodyTerm}
end

NBodyMatrixElement(term::NBodyTerm) = NBodyMatrixElement([term])

Base.zero(::Type{NBodyMatrixElement}) =
NBodyMatrixElement(NBodyTerm[])

Expand Down Expand Up @@ -830,7 +832,7 @@ Base.Matrix(op::QuantumOperator, cfgs::CFGs,
overlaps::Vector{<:OrbitalOverlap}=OrbitalOverlap[]; kwargs...) where {CFGs<:AbstractVector} =
Matrix(cfgs, op, cfgs, overlaps; kwargs...)

function transform(fun::Function, E::EnergyExpression; verbosity=0)
function transform(fun::Function, E::EnergyExpression; verbosity=0, kwargs...)
m,n = size(E)

I = Int[]
Expand Down
5 changes: 5 additions & 0 deletions src/show_coefficients.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,8 @@ function showcoeff(io::IO, n::Number, show_sign::Bool, show_one::Bool=false)
write(io, "1")
end
end

function showcoeff(io::IO, n, show_sign::Bool, _)
show_sign && write(io, "+ ")
print(io, n)
end
3 changes: 3 additions & 0 deletions test/bit_configurations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -602,5 +602,8 @@ end
@test val == [ome(1, h, 1) + ome(2, h, 2) + ome(3, h, 3) ome(3, h, 6)*s
ome(6, h, 3)*s' ome(1, h, 1) + ome(5, h, 5) + ome(6, h, 6)]
@test occursin("[ Info: Generating energy expression\n", err)

@test Matrix(bcs, h, left=1, right=2) == reshape([ome(3, h, 6)*s],1,1)
@test Matrix(bcs, h, left=1, right=1:2) == reshape([ome(1, h, 1) + ome(2, h, 2) + ome(3, h, 3),ome(3, h, 6)*s],1,2)
end
end
5 changes: 5 additions & 0 deletions test/nbody_matrix_elements.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@
n = nbt(m, s)
@test n == nbt(m, s)
@test n == convert(NBodyTerm, m)*convert(NBodyTerm, s)

# This is rather contrived, but just to make sure the printing
# does not crash if NBodyTerm::coeff is not a number.
@test string(NBodyTerm(Vector{EnergyExpressions.NBodyTermFactor}(),OrbitalOverlap(:a,:b))) == "⟨a|b⟩"
end

@testset "NBodyMatrixElement" begin
Expand All @@ -90,6 +94,7 @@

@test o == one(z)
@test isone(o)
@test o == NBodyMatrixElement(one(NBodyTerm))

@test z == zero(o)
@test iszero(z)
Expand Down

2 comments on commit 2d2fa1a

@jagot
Copy link
Member Author

@jagot jagot commented on 2d2fa1a Oct 14, 2022

Choose a reason for hiding this comment

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

@JuliaRegistrator register()

@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/70214

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.1.2 -m "<description of version>" 2d2fa1a018bf115618990ed63e91fc23c030287f
git push origin v0.1.2

Please sign in to comment.