Skip to content

Commit

Permalink
More strict substitution rules: only allow ordinary variable
Browse files Browse the repository at this point in the history
  • Loading branch information
projekter committed May 9, 2024
1 parent e0ad44d commit 0845707
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 50 deletions.
47 changes: 12 additions & 35 deletions src/subs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,45 +30,22 @@ function fillmap!(
end
end
else
s.first.kind == COMPLEX || throw(
ArgumentError(
"Substitution with complex variables requires the ordinary_variable in the substitution specification",
),
)
for j in eachindex(vars)
if vars[j].variable_order.order.id ==
s.first.variable_order.order.id
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 == CONJ
vals[j] = conj(value)
elseif vars[j].kind == REAL_PART
vals[j] = real(value)
else
vals[j] = imag(value)
end
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 == REAL_PART
vals[j] = value
elseif vars[j].kind != IMAG_PART
error(
"Found complex variable with substitution of real part - not implemented",
)
end
if vars[j].kind == COMPLEX
vals[j] = s.second
elseif vars[j].kind == CONJ
vals[j] = conj(s.second)
elseif vars[j].kind == REAL_PART
vals[j] = real(s.second)
else
@assert(s.first.kind == IMAG_PART)
isreal(s.second) || error(
"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 == IMAG_PART
vals[j] = value
elseif vars[j].kind != REAL_PART
error(
"Found complex variable with substitution of imaginary part - not implemented",
)
end
vals[j] = imag(s.second)
end
end
end
Expand Down
20 changes: 5 additions & 15 deletions test/poly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -112,22 +112,12 @@
@complex_polyvar z
pc = z^3 + 2real(z) - 7imag(z^4)
@test pc(z => 2 + 3im) == 798 + 9im
@test pc(conj(z) => 2 - 3im) == 798 + 9im
@test real(pc)(z => 2 + 3im) == 798
err = ArgumentError(
"Variable `zᵢ` was not assigned a value. Use `subs` to substitute only a subset of the variables.",
)
@test_throws err real(pc)(real(z) => 2)
@test subs(real(pc), real(z) => 2) ==
12 - 224imag(z) - 6imag(z)^2 + 56imag(z)^3
@test real(pc)([real(z), imag(z)] => [2, 3]) == 798
err = ErrorException(
"Found complex variable with substitution of real part - not implemented",
"Substitution with complex variables requires the ordinary_variable in the substitution specification"
)
@test_throws err subs(pc, real(z) => 2)
err = ErrorException(
"Found complex variable with substitution of imaginary part - not implemented",
)
@test_throws err subs(pc, imag(z) => 3)
@test_throws err pc(conj(z) => 2 - 3im)
@test_throws err pc(real(z) => 2)
@test_throws err pc(imag(z) => 3)
@test real(pc)(z => 2 + 3im) == 798
end
end

0 comments on commit 0845707

Please sign in to comment.