Skip to content

Commit bc6a145

Browse files
authored
Fix substitution with noncommutative variables (#74)
1 parent 566f08b commit bc6a145

File tree

2 files changed

+14
-23
lines changed

2 files changed

+14
-23
lines changed

src/subs.jl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,14 @@ function Base.setindex!(sv::SafeValues, v, i::Int)
77
sv.values[i] = v
88
end
99

10-
function fillmap!(vals, vars, s::MP.Substitution)
10+
function fillmap!(vals, vars::Vector{PolyVar{false}}, s::MP.Substitution)
11+
for j in eachindex(vars)
12+
if vars[j] == s.first
13+
vals[j] = s.second
14+
end
15+
end
16+
end
17+
function fillmap!(vals, vars::Vector{PolyVar{true}}, s::MP.Substitution)
1118
j = findfirst(isequal(s.first), vars)
1219
if j !== nothing
1320
vals[j] = s.second

test/runtests.jl

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,19 @@ using Test
44
using LinearAlgebra
55

66
# TODO move to MP
7-
@testset "Noncommutative quadratic" begin
8-
@ncpolyvar x[1:2]
9-
Q = Hermitian([1 2 + 3im; 2 - 3im 4])
10-
p = 1x[1]^2 + (2 + 3im) * x[1] * x[2] + (2 - 3im) * x[2] * x[1] + 4x[2]^2
11-
@test polynomial(Q, x) == p
12-
@test polynomial(Q, monovec(x)) == p
13-
end
14-
15-
@testset "Issue #71" begin
16-
@ncpolyvar x y
17-
@test x^0 * y == y * x^0
7+
@testset "Issue #70" begin
8+
@ncpolyvar y0 y1 x0 x1
9+
p = x1 * x0 * x1
10+
@test subs(p, x0 => y0, x1 => y1) == y1 * y0 * y1
11+
@test subs(p, x0 => 1) == x1^2
12+
@test p(x0 => y0, x1 => y1) == y1 * y0 * y1
1813
end
1914

2015
include("mono.jl")
2116
include("poly.jl")
2217
include("comp.jl")
2318
include("mutable_arithmetics.jl")
2419

25-
# TODO move to MultivariatePolynomials.jl
26-
@testset "Subs with no variables" begin
27-
@polyvar x
28-
t = convert(termtype(x, Int), 3)
29-
@test t == @inferred subs(t, x => x + 1)
30-
@test t == @inferred subs(t, x => x + 1.0)
31-
@test t == @inferred subs(t, x => 1x)
32-
@test t == @inferred subs(t, x => 1.0x)
33-
@test t == @inferred subs(t, x => 1.0)
34-
end
35-
3620
module newmodule
3721
using Test
3822
import DynamicPolynomials

0 commit comments

Comments
 (0)