Skip to content

Commit bd3f281

Browse files
committed
Add more tests for linear_terms
1 parent 465c63a commit bd3f281

2 files changed

Lines changed: 58 additions & 18 deletions

File tree

src/utils.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,11 @@ function linear_terms(exprs::AbstractVector, vars)
7676
if expr isa Number # just a number, e.g. Int, Float64
7777
const_terms[i] = expr
7878
elseif expr in vars # expr is a variables in vars
79-
push_sparse_coeff(i, term, 1)
79+
push_sparse_coeff(i, expr, 1)
8080
elseif SymbolicUtils.ismul(expr) && length(expr.dict) == 1
8181
base, exp = first(expr.dict)
8282
if base in vars && exp == 1 # a var with a coeff
83-
push_sparse_coeff(i, term, expr.coeff)
83+
push_sparse_coeff(i, base, expr.coeff)
8484
else
8585
const_nonlinear(i, expr)
8686
end

test/utils.jl

Lines changed: 56 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,61 @@
11
using Test, ModelOrderReduction
22
using Symbolics
33

4+
@variables t w(t) x(t) y(t) z(t)
5+
46
@testset "linear_terms" begin
5-
@variables t w(t) x(t) y(t) z(t)
6-
vars = [x, y, z]
7-
exprs = [3.0x + 4.5y + 6.0
8-
2.0z + 3.4w + 7.0 + sin(x)
9-
9.8 + x * (1.0 - y)
10-
5.6y + 1.3z^2]
11-
A, c, n = ModelOrderReduction.linear_terms(exprs, vars)
12-
@test size(A) == (length(exprs), length(vars))
13-
@test A == [3.0 4.5 0.0
14-
0.0 0.0 2.0
15-
0.0 0.0 0.0
16-
0.0 5.6 0.0]
17-
@test length(c) == length(exprs)
18-
@test isequal(c, [6.0, 3.4w + 7.0, 9.8, 0.0])
19-
@test length(n) == length(exprs)
20-
@test isequal(n, [0.0, sin(x), x * (1.0 - y), 1.3z^2])
7+
@testset "linear_terms full" begin
8+
vars = [x, y, z]
9+
exprs = [3.0x + 4.5y + 6.0
10+
2.0z + 3.4w + 7.0 + sin(x)
11+
9.8 + x * (1.0 - y)
12+
5.6y + 1.3z^2]
13+
A, c, n = ModelOrderReduction.linear_terms(exprs, vars)
14+
@test size(A) == (length(exprs), length(vars))
15+
@test A == [3.0 4.5 0.0
16+
0.0 0.0 2.0
17+
0.0 0.0 0.0
18+
0.0 5.6 0.0]
19+
@test length(c) == length(exprs)
20+
@test isequal(c, [6.0, 3.4w + 7.0, 9.8, 0.0])
21+
@test length(n) == length(exprs)
22+
@test isequal(n, [0.0, sin(x), x * (1.0 - y), 1.3z^2])
23+
end
24+
25+
@testset "linear_terms empty exprs" begin
26+
vars = [x, y, z]
27+
exprs = Vector{Num}(undef, 4)
28+
fill!(exprs, false)
29+
A, c, n = ModelOrderReduction.linear_terms(exprs, vars)
30+
@test size(A) == (length(exprs), length(vars))
31+
@test iszero(A)
32+
@test length(c) == length(exprs)
33+
@test iszero(c)
34+
@test length(n) == length(exprs)
35+
@test iszero(n)
36+
end
37+
38+
@testset "linear_terms diagonal" begin
39+
vars = [x, y, z]
40+
exprs = [x, 2y, 3z, 4w]
41+
A, c, n = ModelOrderReduction.linear_terms(exprs, vars)
42+
@test size(A) == (length(exprs), length(vars))
43+
@test A == [1.0 0.0 0.0
44+
0.0 2.0 0.0
45+
0.0 0.0 3.0
46+
0.0 0.0 0.0]
47+
@test length(c) == length(exprs)
48+
@test isequal(c, [0.0, 0.0, 0.0, 4w])
49+
@test length(n) == length(exprs)
50+
@test iszero(n)
51+
end
52+
53+
@testset "linear_terms nonunique vars" begin
54+
vars = [x, y, y]
55+
exprs = [3.0x + 4.5y + 6.0
56+
2.0z + 3.4w + 7.0 + sin(x)
57+
9.8 + x * (1.0 - y)
58+
5.6y + 1.3z^2]
59+
@test_throws ArgumentError ModelOrderReduction.linear_terms(exprs, vars)
60+
end
2161
end

0 commit comments

Comments
 (0)