-
Notifications
You must be signed in to change notification settings - Fork 30
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
Fix degree_complex #292
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -214,26 +214,68 @@ 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₁` and `x₂` are real-valued variables and `z₁` and `z₂` are complex-valued, | ||
- `degree_complex(x₁^2 * x₂^3) = 5` | ||
- `degree_complex(z₁^3 * conj(z₁)^4) = max(3, 4) = 4` and `degree_complex(z₁^4 * conj(z₁)^3) = max(4, 3) = 4` | ||
- `degree_complex(z₁^3 * z₂ * conj(z₁)^2 * conj(z₂)^4) = max(4, 6) = 6` and | ||
`degree_complex(z₁^4 * z₂ * conj(z₁) * conj(z₂)^3) = max(5, 4) = 5` | ||
- `degree_complex(x₁^2 * x₂^3 * z₁^3 * z₂ * conj(z₁)^2 * conj(z₂)^4) = 5 + max(4, 6) = 11` | ||
""" | ||
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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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₁` and `x₂` are real-valued variables and `z₁` and `z₂` are complex-valued, | ||
- `halfdegree(x₁^2 * x₂^3) = ⌈5/2⌉ = 3` | ||
- `halfdegree(z₁^3 * conj(z₁)^4) = max(3, 4) = 4` and `halfdegree(z₁^4 * conj(z₁)^3) = max(4, 3) = 4` | ||
- `halfdegree(z₁^3 * z₂ * conj(z₁)^2 * conj(z₂)^4) = max(4, 6) = 6` and | ||
`halfdegree(z₁^4 * z₂ * conj(z₁) * conj(z₂)^3) = max(5, 4) = 5` | ||
- `halfdegree(x₁^2 * x₂^3 * z₁^3 * z₂ * conj(z₁)^2 * conj(z₂)^4) = ⌈5/2⌉ + max(4, 6) = 9` | ||
""" | ||
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 | ||
|
@@ -243,7 +285,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 | ||
|
@@ -253,31 +297,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}}) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.