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

Fix degree_complex #292

Merged
merged 3 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
81 changes: 48 additions & 33 deletions src/complex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -214,26 +214,64 @@ end
# Also give complex-valued degree definitions. We choose not to overwrite degree, as this will lead to issues in monovecs
# and their sorting. So now there are two ways to calculate degrees: strictly by considering all variables independently,
# and also by looking at their complex structure.
for fn in (:degree_complex, :halfdegree)
@eval function $fn(t::AbstractTermLike)
realdeg = 0
cpdeg = 0
conjdeg = 0
for (var, exp) in powers(t)
if isreal(var)
realdeg += exp
(isrealpart(var) || isimagpart(var)) && error(
"Cannot calculate complex degrees when real or imaginary parts are present",
)
else
if isconj(var)
conjdeg += exp
else
cpdeg += exp
end
end
end
return $(
fn === :degree_complex ? :(realdeg) : :(div(realdeg, 2, RoundUp))
) + max(cpdeg, conjdeg)
end
end

"""
degree_complex(t::AbstractTermLike)

Return the _total complex degree_ of the monomial of the term `t`, i.e., the maximum of the total degree of the declared
variables in `t` and the total degree of the conjugate variables in `t`.
To be well-defined, the monomial must not contain real parts or imaginary parts of variables.
If `x` is a real-valued variable and `z` is complex-valued,
- `degree_complex(x^5) = 5`
- `degree_complex(z^3 * conj(z)^4) = 4` and `degree_complex(z^4 * conj(z)^3) = 4`
Copy link
Member

Choose a reason for hiding this comment

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

I meant also an example with two different complex-valued where one has a larger degree for the conjugate and not the other one

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, now I use two real and two complex variables for the example, I hope this covers what you thought of.

- `degree_complex(x^5 * z^3 * conj(z^4)) = 5 + 4 = 9`
"""
Copy link
Member

Choose a reason for hiding this comment

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

The reader might not know what happens to this example: degree_complex(x^2 * conj(x) * y * conj(y)^2)). Is it 3 or 4 ? It might be useful as example to clarify.

degree_complex(t::AbstractTermLike)

"""
halfdegree(t::AbstractTermLike)

Return the equivalent of `ceil(degree(t)/2)`` for real-valued terms or `degree_complex(t)` for terms with only complex
variables; however, respect any mixing between complex and real-valued variables.
Copy link
Member

Choose a reason for hiding this comment

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

Could you add an example section in the docstring to help the reader understand ?

To be well-defined, the monomial must not contain real parts or imaginary parts of variables.
If `x` is a real-valued variable and `z` is complex-valued,
- `halfdegree(x^5) = 3`
- `halfdegree(z^3 * conj(z)^4) = 4` and `halfdegree(z^4 * conj(z)^3) = 4`
- `halfdegree(x^5 * z^3 * conj(z^4)) = 3 + 4 = 7`
"""
halfdegree(t::AbstractTermLike)

"""
degree_complex(t::AbstractTermLike, v::AbstractVariable)

Returns the exponent of the variable `v` or its conjugate in the monomial of the term `t`, whatever is larger.

See also [`isconj`](@ref).
"""
function degree_complex(t::AbstractTermLike)
vars = variables(t)
@assert(!any(isrealpart, vars) && !any(isimagpart, vars))
grouping = isconj.(vars)
exps = exponents(t)
return max(sum(exps[grouping]), sum(exps[map(!, grouping)]))
end
function degree_complex(t::AbstractTermLike, var::AbstractVariable)
return degree_complex(monomial(t), var)
end
Expand All @@ -243,7 +281,9 @@ function degree_complex(m::AbstractMonomial, v::AbstractVariable)
deg_c = 0
c_v = conj(v)
for (var, exp) in powers(m)
@assert(!isrealpart(var) && !isimagpart(var))
(isrealpart(var) || isimagpart(var)) && error(
"Cannot calculate complex degrees when real or imaginary parts are present",
)
if var == v
deg += exp
elseif var == c_v
Expand All @@ -253,31 +293,6 @@ function degree_complex(m::AbstractMonomial, v::AbstractVariable)
return max(deg, deg_c)
end

"""
halfdegree(t::AbstractTermLike)

Return the equivalent of `ceil(degree(t)/2)`` for real-valued terms or `degree_complex(t)` for terms with only complex
variables; however, respect any mixing between complex and real-valued variables.
"""
function halfdegree(t::AbstractTermLike)
realdeg = 0
cpdeg = 0
conjdeg = 0
for (var, exp) in powers(t)
if isreal(var)
realdeg += exp
else
if isconj(var)
conjdeg += exp
else
@assert(!isrealpart(var) && !isimagpart(var))
cpdeg += exp
end
end
end
return ((realdeg + 1) >> 1) + max(cpdeg, conjdeg)
end

"""
mindegree_complex(p::Union{AbstractPolynomialLike, AbstractVector{<:AbstractTermLike}})

Expand Down
5 changes: 4 additions & 1 deletion test/complex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,11 @@
@test degree_complex(x * y^2 * conj(y)^3) == 3
@test degree_complex(x * y^2 * conj(y)^3, x) == 1
@test degree_complex(x * y^2 * conj(y)^3, y) == 3
@test degree_complex(a^5 * x * y^2 * conj(y)^4) == 9
@test degree_complex(a^5 * x * y^2 * conj(y)^2) == 8
@test halfdegree(x * y^2 * conj(y^3)) == 3
@test halfdegree(x * a^5 * conj(y)) == 4
@test halfdegree(x * a^5 * conj(y^2)) == 5
@test halfdegree(x^2 * a^5 * conj(y^2)) == 5
@test ordinary_variable([x, y, conj(x), a, real(x), imag(y)]) == [x, y, a]

@test subs(4x + 8y^2 - 6x^3, [x, y] => [2 + 4im, 9 - im]) ==
Expand Down
Loading