Skip to content

Commit 8407cc3

Browse files
authored
Change @assert to ArgumentError (#53)
1 parent 27868a1 commit 8407cc3

File tree

4 files changed

+22
-25
lines changed

4 files changed

+22
-25
lines changed

src/bases.jl

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,8 @@ For a permutation vector `[2,1,3]` and a given object with basis `[b1, b2, b3]`
255255
this function results in `[b2, b1, b3]`.
256256
"""
257257
function permutesystems(b::CompositeBasis, perm)
258-
@assert length(b.bases) == length(perm)
259-
@assert isperm(perm)
258+
(nsubsystems(b) == length(perm)) || throw(ArgumentError("Must have nsubsystems(b) == length(perm) in permutesystems"))
259+
isperm(perm) || throw(ArgumentError("Must pass actual permeutation to permutesystems"))
260260
CompositeBasis(b.shape[perm], b.bases[perm])
261261
end
262262

@@ -278,7 +278,7 @@ struct FockBasis{T} <: Basis
278278
offset::T
279279
function FockBasis(N::T,offset::T=0) where T
280280
if N < 0 || offset < 0 || N <= offset
281-
throw(DimensionMismatch())
281+
throw(ArgumentError("Fock cutoff and offset must be positive and cutoff must be less than offset"))
282282
end
283283
new{T}([N-offset+1], N, offset)
284284
end
@@ -296,9 +296,7 @@ struct NLevelBasis{T} <: Basis
296296
shape::Vector{T}
297297
N::T
298298
function NLevelBasis(N::T) where T<:Integer
299-
if N < 1
300-
throw(DimensionMismatch())
301-
end
299+
N > 0 || throw(ArgumentError("N must be greater than 0"))
302300
new{T}([N], N)
303301
end
304302
end
@@ -340,8 +338,8 @@ struct SpinBasis{S,T} <: Basis
340338
function SpinBasis{S}(spinnumber::Rational{T}) where {S,T<:Integer}
341339
n = numerator(spinnumber)
342340
d = denominator(spinnumber)
343-
@assert d==2 || d==1
344-
@assert n >= 0
341+
d==2 || d==1 || throw(ArgumentError("Can only construct integer or half-integer spin basis"))
342+
n >= 0 || throw(ArgumentError("Can only construct positive spin basis"))
345343
N = numerator(spinnumber*2 + 1)
346344
new{spinnumber,T}([N], spinnumber)
347345
end

src/embed_permute.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ specifies in which subsystems the corresponding operator is defined.
77
"""
88
function embed(basis_l::CompositeBasis, basis_r::CompositeBasis,
99
operators::Dict{<:Vector{<:Integer}, T}) where T<:AbstractOperator
10-
@assert length(basis_l.bases) == length(basis_r.bases)
10+
(nsubsystems(basis_l) == nsubsystems(basis_r)) || throw(ArgumentError("Must have nsubsystems(bl) == nsubsystems(br) in embed"))
1111
N = length(basis_l.bases)::Int # type assertion to help type inference
1212
if length(operators) == 0
1313
return identityoperator(T, basis_l, basis_r)
@@ -55,11 +55,11 @@ Tensor product of operators where missing indices are filled up with identity op
5555
function embed(basis_l::CompositeBasis, basis_r::CompositeBasis,
5656
indices, operators::Vector{T}) where T<:AbstractOperator
5757

58-
@assert check_embed_indices(indices)
58+
check_embed_indices(indices) || throw(ArgumentError("Must have unique indices in embed"))
59+
(nsubsystems(basis_l) == nsubsystems(basis_r)) || throw(ArgumentError("Must have nsubsystems(bl) == nsubsystems(br) in embed"))
60+
(length(indices) == length(operators)) || throw(ArgumentError("Must have length(indices) == length(operators) in embed"))
5961

60-
N = length(basis_l.bases)
61-
@assert length(basis_r.bases) == N
62-
@assert length(indices) == length(operators)
62+
N = nsubsystems(basis_l)
6363

6464
# Embed all single-subspace operators.
6565
idxop_sb = [x for x in zip(indices, operators) if x[1] isa Integer]

src/sortedindices.jl

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ function check_indices(imax, indices)
7474
N = length(indices)
7575
for n=1:N
7676
i = indices[n]
77-
@assert 0 < i <= imax
77+
(0 < i <= imax) || throw(ArgumentError("indices exceed allowable range"))
7878
for m in n+1:N
79-
@assert i != indices[m]
79+
(i != indices[m]) || throw(ArgumentError("indices not unique"))
8080
end
8181
end
8282
end
@@ -90,10 +90,10 @@ function check_sortedindices(imax, indices)
9090
return nothing
9191
end
9292
i_ = indices[1]
93-
@assert 0 < i_ <= imax
93+
(0 < i_ <= imax) || throw(ArgumentError("indices exceed allowable range"))
9494
for i in indices[2:end]
95-
@assert 0 < i <= imax
96-
@assert i > i_
95+
(0 < i <= imax) || throw(ArgumentError("indices exceed allowable range"))
96+
(i > i_) || throw(ArgumentError("indices not sorted"))
9797
end
9898
end
9999

@@ -107,8 +107,7 @@ function check_embed_indices(indices)
107107
# short circuit return when `indices` is empty.
108108
length(indices) == 0 && return true
109109

110-
err_str = "Variable `indices` comes in an unexpected form. Expecting `Array{Union{Int, Array{Int, 1}}, 1}`"
111-
@assert all(x isa Array || x isa Int for x in indices) err_str
110+
all(x isa Array || x isa Int for x in indices) || throw(ArgumentError("Variable `indices` comes in an unexpected form. Expecting `Array{Union{Int, Array{Int, 1}}, 1}`"))
112111

113112
# flatten the indices and check for uniqueness
114113
# use a custom flatten because it's ≈ 4x faster than Base.Iterators.flatten

test/test_sortedindices.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ x = [3, 5]
1818
s.reducedindices!(x, [2, 3, 5, 6])
1919
@test x == [2, 3]
2020

21-
@test_throws AssertionError s.check_indices(5, [1, 6])
22-
@test_throws AssertionError s.check_indices(5, [0, 2])
21+
@test_throws ArgumentError s.check_indices(5, [1, 6])
22+
@test_throws ArgumentError s.check_indices(5, [0, 2])
2323
@test s.check_indices(5, Int[]) == nothing
2424
@test s.check_indices(5, [1, 3]) == nothing
2525
@test s.check_indices(5, [3, 1]) == nothing
2626

27-
@test_throws AssertionError s.check_sortedindices(5, [1, 6])
28-
@test_throws AssertionError s.check_sortedindices(5, [3, 1])
29-
@test_throws AssertionError s.check_sortedindices(5, [0, 2])
27+
@test_throws ArgumentError s.check_sortedindices(5, [1, 6])
28+
@test_throws ArgumentError s.check_sortedindices(5, [3, 1])
29+
@test_throws ArgumentError s.check_sortedindices(5, [0, 2])
3030
@test s.check_sortedindices(5, Int[]) == nothing
3131
@test s.check_sortedindices(5, [1, 3]) == nothing
3232

0 commit comments

Comments
 (0)