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