Skip to content

Commit

Permalink
Rename ComplexKind instances
Browse files Browse the repository at this point in the history
  • Loading branch information
projekter committed Feb 21, 2024
1 parent da7fd62 commit f8d3214
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 45 deletions.
4 changes: 2 additions & 2 deletions src/DynamicPolynomials.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ function MP.similar_variable(
return MP.similar_variable(P, S)
end
function MP.similar_variable(p::PolyType{V,M}, s::Symbol) where {V,M}
return Variable(string(s), V, M, isreal(p) ? cpNone : cpFull)
return Variable(string(s), V, M, isreal(p) ? REAL : COMPLEX)
end
function MP.similar_variable(::Type{<:PolyType{V,M}}, s::Symbol) where {V,M}
return Variable(string(s), V, M, cpNone) # we cannot infer this from the type,
return Variable(string(s), V, M, REAL) # we cannot infer this from the type,
end

include("promote.jl")
Expand Down
28 changes: 14 additions & 14 deletions src/subs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function fillmap!(
# - The coefficients in vals might not be complex-valued; but to assign only a part of the variable, we necessarily need to
# introduce an explicit imaginary coefficient to the value.
# Currently, we don't do anything to catch these errors.
if s.first.kind == cpNone
if s.first.kind == REAL
for j in eachindex(vars)
if vars[j] == s.first
vals[j] = s.second
Expand All @@ -35,41 +35,41 @@ function fillmap!(
for j in eachindex(vars)
if vars[j].variable_order.order.id ==
s.first.variable_order.order.id
if s.first.kind == cpFull || s.first.kind == cpConj
value = s.first.kind == cpConj ? conj(s.second) : s.second
if vars[j].kind == cpFull
if s.first.kind == COMPLEX || s.first.kind == CONJ
value = s.first.kind == CONJ ? conj(s.second) : s.second
if vars[j].kind == COMPLEX
vals[j] = value
elseif vars[j].kind == cpConj
elseif vars[j].kind == CONJ
vals[j] = conj(value)
elseif vars[j].kind == cpReal
elseif vars[j].kind == REAL_PART
vals[j] = real(value)

Check warning on line 45 in src/subs.jl

View check run for this annotation

Codecov / codecov/patch

src/subs.jl#L44-L45

Added lines #L44 - L45 were not covered by tests
else
vals[j] = imag(value)

Check warning on line 47 in src/subs.jl

View check run for this annotation

Codecov / codecov/patch

src/subs.jl#L47

Added line #L47 was not covered by tests
end
elseif s.first.kind == cpReal
elseif s.first.kind == REAL_PART
isreal(s.second) || error(
"Cannot assign a complex value to the real part of an expression",
)
value = real(s.second) # just to make sure the type is correct
if vars[j].kind == cpFull
if vars[j].kind == COMPLEX
vals[j] = value + im * imag(vals[j])
elseif vars[j].kind == cpConj
elseif vars[j].kind == CONJ
vals[j] = value - im * imag(vals[j])
elseif vars[j].kind == cpReal
elseif vars[j].kind == REAL_PART

Check warning on line 58 in src/subs.jl

View check run for this annotation

Codecov / codecov/patch

src/subs.jl#L56-L58

Added lines #L56 - L58 were not covered by tests
vals[j] = value
end
# else we know the real part but use the imaginary part; do nothing
else
@assert(s.first.kind == cpImag)
@assert(s.first.kind == IMAG_PART)
isreal(s.second) || error(

Check warning on line 64 in src/subs.jl

View check run for this annotation

Codecov / codecov/patch

src/subs.jl#L63-L64

Added lines #L63 - L64 were not covered by tests
"Cannot assign a complex value to the imaginary part of an expression",
)
value = real(s.second) # just to make sure the type is correct
if vars[j].kind == cpFull
if vars[j].kind == COMPLEX
vals[j] = real(vals[j]) + im * value
elseif vars[j].kind == cpConj
elseif vars[j].kind == CONJ
vals[j] = real(vals[j]) - im * value
elseif vars[j].kind == cpImag
elseif vars[j].kind == IMAG_PART

Check warning on line 72 in src/subs.jl

View check run for this annotation

Codecov / codecov/patch

src/subs.jl#L67-L72

Added lines #L67 - L72 were not covered by tests
vals[j] = value
end
# else we know the imaginary part but use the real part; do nothing
Expand Down
58 changes: 29 additions & 29 deletions src/var.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ function _extract_kw_args(args, variable_order, complex_kind)
elseif arg.args[1] == :monomial_order
monomial_order = esc(arg.args[2])
elseif arg.args[1] == :complex
complex_kind = arg.args[2] == :true ? cpFull : cpNone
complex_kind = arg.args[2] == :true ? COMPLEX : REAL

Check warning on line 76 in src/var.jl

View check run for this annotation

Codecov / codecov/patch

src/var.jl#L75-L76

Added lines #L75 - L76 were not covered by tests
else
error("Unrecognized keyword argument `$(arg.args[1])`")
end
Expand All @@ -87,7 +87,7 @@ end
# Variable vector x returned garanteed to be sorted so that if p is built with x then vars(p) == x
macro polyvar(args...)
pos_args, variable_order, monomial_order, complex_kind =
_extract_kw_args(args, :($(Commutative{CreationOrder})), cpNone)
_extract_kw_args(args, :($(Commutative{CreationOrder})), REAL)
vars, exprs =
buildpolyvars(pos_args, variable_order, monomial_order, complex_kind)
return :($(foldl((x, y) -> :($x; $y), exprs, init = :()));
Expand All @@ -96,7 +96,7 @@ end

macro ncpolyvar(args...)
pos_args, variable_order, monomial_order, complex_kind =
_extract_kw_args(args, :($(NonCommutative{CreationOrder})), cpNone)
_extract_kw_args(args, :($(NonCommutative{CreationOrder})), REAL)
vars, exprs =
buildpolyvars(pos_args, variable_order, monomial_order, complex_kind)
return :($(foldl((x, y) -> :($x; $y), exprs, init = :()));
Expand All @@ -105,7 +105,7 @@ end

macro polycvar(args...)
pos_args, variable_order, monomial_order, complex_kind =
_extract_kw_args(args, :($(Commutative{CreationOrder})), cpFull)
_extract_kw_args(args, :($(Commutative{CreationOrder})), COMPLEX)
vars, exprs =
buildpolyvars(pos_args, variable_order, monomial_order, complex_kind)
return :($(foldl((x, y) -> :($x; $y), exprs, init = :()));
Expand Down Expand Up @@ -147,20 +147,20 @@ end
ComplexKind
The type used to identify whether a variable is complex-valued. It has the following instances:
- `cpNone`: the variable is real-valued, [`conj`](@ref) and [`real`](@ref) are identities, [`imag`](@ref) is zero.
- `cpFull`: the variable is a declared complex-valued variable with distinct conjugate, real, and imaginary part.
- `REAL`: the variable is real-valued, [`conj`](@ref) and [`real`](@ref) are identities, [`imag`](@ref) is zero.
- `COMPLEX`: the variable is a declared complex-valued variable with distinct conjugate, real, and imaginary part.
[`ordinary_variable`](@ref) is an identity.
- `cpConj`: the variable is the conjugate of a declared complex-valued variable. [`ordinary_variable`](@ref) is equal to
- `CONJ`: the variable is the conjugate of a declared complex-valued variable. [`ordinary_variable`](@ref) is equal to
[`conj`](@ref). It will print with an over-bar.
- `cpReal`: the variable is the real part of a complex-valued variable. It is real-valued itself: [`conj`](@ref) and
- `REAL_PART`: the variable is the real part of a complex-valued variable. It is real-valued itself: [`conj`](@ref) and
[`real`](@ref) are identities, [`imag`](@ref) is zero. [`ordinary_variable`](@ref) will give back the original complex-valued
_declared_ variable from which the real part was extracted. It will print with an `ᵣ` subscript.
- `cpImag`: the variable is the imaginary part of a declared complex-valued variable (or the negative imaginary part of the
- `IMAG_PART`: the variable is the imaginary part of a declared complex-valued variable (or the negative imaginary part of the
conjugate of a declared complex-valued variable). It is real-valued itself: [`conj`](@ref) and [`real`](@ref) are identities,
[`imag`](@ref) is zero. [`ordinary_variable`](@ref) will give back the original complex-valued _declared_ variable from which
the imaginary part was extracted. It will print with an `ᵢ` subscript.
"""
@enum ComplexKind cpNone cpFull cpConj cpReal cpImag
@enum ComplexKind REAL COMPLEX CONJ REAL_PART IMAG_PART

struct Variable{V,M} <: AbstractVariable
name::String
Expand All @@ -169,7 +169,7 @@ struct Variable{V,M} <: AbstractVariable

function Variable{V,M}(
name::AbstractString,
kind::ComplexKind = cpNone,
kind::ComplexKind = REAL,
) where {V<:AbstractVariableOrdering,M<:MP.AbstractMonomialOrdering}
return new{V,M}(convert(String, name), kind, instantiate(V))
end
Expand All @@ -178,7 +178,7 @@ struct Variable{V,M} <: AbstractVariable
name::AbstractString,
::Type{V},
::Type{M},
kind::ComplexKind = cpNone,
kind::ComplexKind = REAL,
) where {V<:AbstractVariableOrdering,M<:MP.AbstractMonomialOrdering}
return new{V,M}(convert(String, name), kind, instantiate(V))
end
Expand All @@ -187,8 +187,8 @@ struct Variable{V,M} <: AbstractVariable
from::Variable{V,M},
kind::ComplexKind,
) where {V<:AbstractVariableOrdering,M<:MP.AbstractMonomialOrdering}
(from.kind == cpNone && kind == cpNone) ||
(from.kind != cpNone && kind != cpNone) ||
(from.kind == REAL && kind == REAL) ||
(from.kind != REAL && kind != REAL) ||
error("Cannot change the complex type of an existing variable")
return new{V,M}(from.name, kind, from.variable_order)
end
Expand All @@ -214,36 +214,36 @@ MP.ordering(::Type{Variable{V,M}}) where {V,M} = M

iscomm(::Type{Variable{C}}) where {C} = C

Base.isreal(x::Variable{C}) where {C} = x.kind != cpFull && x.kind != cpConj
MP.isrealpart(x::Variable{C}) where {C} = x.kind == cpReal
MP.isimagpart(x::Variable{C}) where {C} = x.kind == cpImag
MP.isconj(x::Variable{C}) where {C} = x.kind == cpConj
Base.isreal(x::Variable{C}) where {C} = x.kind != COMPLEX && x.kind != CONJ
MP.isrealpart(x::Variable{C}) where {C} = x.kind == REAL_PART
MP.isimagpart(x::Variable{C}) where {C} = x.kind == IMAG_PART
MP.isconj(x::Variable{C}) where {C} = x.kind == CONJ
function MP.ordinary_variable(x::Variable)
return x.kind == cpNone || x.kind == cpFull ? x : Variable(x, cpFull)
return x.kind == REAL || x.kind == COMPLEX ? x : Variable(x, COMPLEX)
end

function Base.conj(x::Variable)
if x.kind == cpFull
return Variable(x, cpConj)
elseif x.kind == cpConj
return Variable(x, cpFull)
if x.kind == COMPLEX
return Variable(x, CONJ)
elseif x.kind == CONJ
return Variable(x, COMPLEX)
else
return x
end
end

function Base.real(x::Variable)
if x.kind == cpFull || x.kind == cpConj
return Variable(x, cpReal)
if x.kind == COMPLEX || x.kind == CONJ
return Variable(x, REAL_PART)
else
return x
end
end
function Base.imag(x::Variable{V,M}) where {V,M}
if x.kind == cpFull
return Variable(x, cpImag)
elseif x.kind == cpConj
return _Term{V,M,Int}(-1, Monomial(Variable(x, cpImag)))
if x.kind == COMPLEX
return Variable(x, IMAG_PART)
elseif x.kind == CONJ
return _Term{V,M,Int}(-1, Monomial(Variable(x, IMAG_PART)))
else
return MA.Zero()
end
Expand Down

0 comments on commit f8d3214

Please sign in to comment.