Skip to content

Commit

Permalink
Make N a named argument to spin_matrices
Browse files Browse the repository at this point in the history
  • Loading branch information
kbarros committed Jul 14, 2023
1 parent 614ee56 commit d3768d7
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/Integrators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ end

# Returns (Λ + B⋅S) Z
@generated function mul_spin_matrices(Λ, B::Sunny.Vec3, Z::Sunny.CVec{N}) where N
S = Sunny.spin_matrices(N)
S = spin_matrices(; N)
out = map(1:N) do i
out_i = map(1:N) do j
terms = Any[:(Λ[$i,$j])]
Expand Down
2 changes: 1 addition & 1 deletion src/Operators/Rotation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ end
function unitary_for_rotation(R::Mat3; N::Int)
!(R'*R I) && error("Not an orthogonal matrix, R = $R.")
!(det(R) 1) && error("Matrix includes a reflection, R = $R.")
S = spin_matrices(N)
S = spin_matrices(; N)
n, θ = axis_angle(R)
return exp(-im*θ*(n'*S))
end
Expand Down
8 changes: 4 additions & 4 deletions src/Operators/Spin.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
"""
spin_matrices(N)
spin_matrices(; N)
Constructs the three spin operators, i.e. the generators of SU(2), in the
`N`-dimensional irrep. See also [`spin_operators`](@ref), which determines the
appropriate value of `N` for a given site index.
"""
function spin_matrices(N::Int)
function spin_matrices(; N::Int)
if N == 0
return fill(zeros(ComplexF64,0,0), 3)
end
Expand All @@ -23,7 +23,7 @@ end

# Returns ⟨Z|Sᵅ|Z⟩
@generated function expected_spin(Z::CVec{N}) where N
S = spin_matrices(N)
S = spin_matrices(; N)
elems_x = SVector{N-1}(diag(S[1], 1))
elems_z = SVector{N}(diag(S[3], 0))
lo_ind = SVector{N-1}(1:N-1)
Expand All @@ -45,7 +45,7 @@ end
# http://www.emis.de/journals/SIGMA/2014/084/
ket_from_dipole(_::Vec3, ::Val{0}) :: CVec{0} = zero(CVec{0})
function ket_from_dipole(dip::Vec3, ::Val{N}) :: CVec{N} where N
S = spin_matrices(N)
S = spin_matrices(; N)
λs, vs = eigen(dip' * S)
return CVec{N}(vs[:, argmax(real.(λs))])
end
Expand Down
4 changes: 2 additions & 2 deletions src/Operators/Stevens.jl
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ function stevens_matrices(k::Int; N::Int)
if k >= N
return fill(zeros(ComplexF64, N, N), 2k+1)
else
return stevens_abstract_polynomials(; J=spin_matrices(N), k)
return stevens_abstract_polynomials(; J=spin_matrices(; N), k)
end
end

Expand Down Expand Up @@ -202,7 +202,7 @@ in the large-``S`` classical limit is
# Examples
```julia
S = spin_matrices(5)
S = spin_matrices(N=5)
print_stevens_expansion(S[1]^4 + S[2]^4 + S[3]^4)
# Prints: (1/20)𝒪₄₀ + (1/4)𝒪₄₄ + 102/5
```
Expand Down
2 changes: 1 addition & 1 deletion src/Operators/Symbolic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ const 𝒮 = spin_operator_symbols
# Construct explicit N-dimensional matrix representation of operator
function operator_to_matrix(p::DP.AbstractPolynomialLike; N)
rep = p(
𝒮 => spin_matrices(N),
𝒮 => spin_matrices(; N),
[stevens_operator_symbols[k] => stevens_matrices(k; N) for k=1:6]...
)
if !(rep rep')
Expand Down
4 changes: 2 additions & 2 deletions src/Operators/TensorOperators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ end
# Returns the spin operators for two sites, with Hilbert space dimensions N₁ and
# N₂, respectively.
function spin_pair(N1, N2)
S1 = Sunny.spin_matrices(N1)
S2 = Sunny.spin_matrices(N2)
S1 = spin_matrices(N=N1)
S2 = spin_matrices(N=N2)
return local_quantum_operators(S1, S2)
end
Expand Down
6 changes: 3 additions & 3 deletions src/SpinWaveTheory/SpinWaveTheory.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function generate_local_sun_gens(sys :: System)
Nₘ, N = length(sys.dipoles), sys.Ns[1] # number of magnetic atoms and dimension of Hilbert
S = (N-1)/2

s_mat_N = spin_matrices(N)
s_mat_N = spin_matrices(; N)

# we support the biquad interactions now in the :dipole mode
# we choose a particular basis of the nematic operators listed in Appendix B of *Phys. Rev. B 104, 104409*
Expand All @@ -76,7 +76,7 @@ function generate_local_sun_gens(sys :: System)
Q_mat_N[5] = 3 * s_mat_N[3] * s_mat_N[3] - 1/√3 * S * (S+1) * I

if sys.mode == :SUN
s_mat_N = spin_matrices(N)
s_mat_N = spin_matrices(; N)

s̃_mat = Array{ComplexF64, 4}(undef, N, N, 3, Nₘ)
T̃_mat = Array{ComplexF64, 3}(undef, N, N, Nₘ)
Expand All @@ -98,7 +98,7 @@ function generate_local_sun_gens(sys :: System)
end

elseif sys.mode == :dipole
s_mat_2 = spin_matrices(2)
s_mat_2 = spin_matrices(N=2)

s̃_mat = Array{ComplexF64, 4}(undef, 2, 2, 3, Nₘ)

Expand Down
8 changes: 4 additions & 4 deletions src/System/System.jl
Original file line number Diff line number Diff line change
Expand Up @@ -429,8 +429,8 @@ Each is an ``N×N`` matrix of appropriate dimension ``N``.
See also [`print_stevens_expansion`](@ref).
"""
spin_operators(sys::System{N}, i::Int) where N = spin_matrices(sys.Ns[i])
spin_operators(sys::System{N}, site::Site) where N = spin_matrices(sys.Ns[to_atom(site)])
spin_operators(sys::System{N}, i::Int) where N = spin_matrices(N=sys.Ns[i])
spin_operators(sys::System{N}, site::Site) where N = spin_matrices(N=sys.Ns[to_atom(site)])

"""
stevens_operators(sys, i::Int)
Expand All @@ -446,7 +446,7 @@ stevens_operators(sys::System{N}, site::Site) where N = StevensMatrices(sys.Ns[t


function spin_operators(sys::System{N}, b::Bond) where N
Si = spin_matrices(sys.Ns[b.i])
Sj = spin_matrices(sys.Ns[b.j])
Si = spin_matrices(N=sys.Ns[b.i])
Sj = spin_matrices(N=sys.Ns[b.j])
return local_quantum_operators(Si, Sj)
end
12 changes: 6 additions & 6 deletions test/test_operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
### Verify 𝔰𝔲(2) irreps
for N = 2:5
S₀ = (N-1)/2
S = Sunny.spin_matrices(N)
S = spin_matrices(; N)

for i in 1:3, j in 1:3
# Test commutation relations
Expand Down Expand Up @@ -32,7 +32,7 @@
Λ = randn(ComplexF64, N, N)
B = randn(Sunny.Vec3)
Z = randn(Sunny.CVec{N})
@test Sunny.mul_spin_matrices(Λ, B, Z) + B'*Sunny.spin_matrices(N)) * Z
@test Sunny.mul_spin_matrices(Λ, B, Z) + B'*spin_matrices(; N)) * Z
end
end

Expand Down Expand Up @@ -94,7 +94,7 @@ end

# Check transformation properties of spherical tensors
for N in 2:7
S = Sunny.spin_matrices(N)
S = spin_matrices(; N)
Sp = S[1] + im*S[2]
Sm = S[1] - im*S[2]

Expand All @@ -103,7 +103,7 @@ end
T = spherical_tensors(k; N)

# Generators of rotations in the spin-k representation
K = Sunny.spin_matrices(2k+1)
K = spin_matrices(N=2k+1)

# The selected basis is q ∈ [|k⟩, |k-1⟩, ... |-k⟩]. This function
# converts from a q value to a 1-based index.
Expand Down Expand Up @@ -186,7 +186,7 @@ end

# Test that spin matrices rotate as vectors
let
S = Sunny.spin_matrices(N)
S = spin_matrices(; N)
@test R * S rotate_operator.(S, Ref(R))
end

Expand Down Expand Up @@ -258,7 +258,7 @@ end
@test capt.output == "(1/20)𝒪₄₀ + (1/4)𝒪₄₄ + (3/5)X²\n"

capt = IOCapture.capture() do
S = spin_matrices(5)
S = spin_matrices(N=5)
print_stevens_expansion(S[1]^4 + S[2]^4 + S[3]^4)
end
@test capt.output == "(1/20)𝒪₄₀ + (1/4)𝒪₄₄ + 102/5\n"
Expand Down
2 changes: 1 addition & 1 deletion test/test_structure_factors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
# Test sum rule with custom observables
sys = simple_model_sf(; mode=:SUN)
thermalize_simple_model!(sys; kT=0.1)
S = Sunny.spin_matrices(2)
S = spin_matrices(N=2)
observables = cat(S...; dims=3)
sf = DynamicStructureFactor(sys; nω=100, ωmax=10.0, Δt=0.1, apply_g=false, observables)
qgrid = all_exact_wave_vectors(sf)
Expand Down

0 comments on commit d3768d7

Please sign in to comment.