|
1 | 1 | using Test, ModelOrderReduction |
2 | 2 | using Symbolics |
3 | 3 |
|
| 4 | +@variables t w(t) x(t) y(t) z(t) |
| 5 | + |
4 | 6 | @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 |
21 | 61 | end |
0 commit comments