Skip to content

Commit 481ba92

Browse files
author
Kwesi Rutledge
committed
Added Tests for MatrixExpression.ConcretizeMatrixExpression
1 parent 1d58df5 commit 481ba92

File tree

2 files changed

+149
-1
lines changed

2 files changed

+149
-1
lines changed

symbolic/matrix_expression.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ func ConcretizeMatrixExpression(sliceIn [][]ScalarExpression) MatrixExpression {
257257
var (
258258
containsConstant bool = false
259259
isAllVariables bool = true
260+
containsVariable bool = false
260261
containsMonomial bool = false
261262
containsPolynomial bool = false
262263
)
@@ -271,6 +272,8 @@ func ConcretizeMatrixExpression(sliceIn [][]ScalarExpression) MatrixExpression {
271272
switch elt.(type) {
272273
case K:
273274
containsConstant = true
275+
case Variable:
276+
containsVariable = true
274277
case Monomial:
275278
containsMonomial = true
276279
case Polynomial:
@@ -318,7 +321,7 @@ func ConcretizeMatrixExpression(sliceIn [][]ScalarExpression) MatrixExpression {
318321

319322
return out
320323

321-
case containsMonomial:
324+
case containsMonomial || (containsVariable && containsConstant):
322325
// Convert to a monomial vector
323326
var out MonomialMatrix
324327
for _, row_ii := range sliceIn {

testing/symbolic/matrix_expression_test.go

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,148 @@ func TestMatrixExpression_ToMatrixExpression1(t *testing.T) {
4646
}
4747
}
4848
}
49+
50+
/*
51+
TestMatrixExpression_ConcretizeMatrixExpression1
52+
Description:
53+
54+
Tests the conversion of a slice of slices of constants (K) to a KMatrix.
55+
*/
56+
func TestMatrixExpression_ConcretizeMatrixExpression1(t *testing.T) {
57+
// Setup
58+
x := [][]symbolic.ScalarExpression{
59+
{symbolic.K(1.0), symbolic.K(2), symbolic.K(3)},
60+
{symbolic.K(4), symbolic.K(5), symbolic.K(6)},
61+
{symbolic.K(7), symbolic.K(8), symbolic.K(9)},
62+
}
63+
64+
// Test
65+
me := symbolic.ConcretizeMatrixExpression(x)
66+
67+
// Check that me is a KMatrix
68+
if _, ok := me.(symbolic.KMatrix); !ok {
69+
t.Errorf("Expected a KMatrix; received %T", me)
70+
}
71+
72+
// Check that me is the correct KMatrix (i.e., it has the correct dimensions)
73+
if me.Dims()[0] != 3 || me.Dims()[1] != 3 {
74+
t.Errorf("Expected a 3x3 KMatrix; received %dx%d KMatrix", me.Dims()[0], me.Dims()[1])
75+
}
76+
77+
}
78+
79+
/*
80+
TestMatrixExpression_ConcretizeMatrixExpression2
81+
Description:
82+
83+
Tests the conversion of a slice of slices of Monomials to a MonomialMatrix.
84+
*/
85+
func TestMatrixExpression_ConcretizeMatrixExpression2(t *testing.T) {
86+
// Setup
87+
m := symbolic.NewVariable().ToMonomial()
88+
x := [][]symbolic.ScalarExpression{
89+
{m, m},
90+
{m, m},
91+
}
92+
93+
// Test
94+
me := symbolic.ConcretizeMatrixExpression(x)
95+
96+
// Check that me is a MonomialMatrix
97+
if _, ok := me.(symbolic.MonomialMatrix); !ok {
98+
t.Errorf("Expected a MonomialMatrix; received %T", me)
99+
}
100+
101+
// Check that me is the correct MonomialMatrix (i.e., it has the correct dimensions)
102+
if me.Dims()[0] != 2 || me.Dims()[1] != 2 {
103+
t.Errorf("Expected a 2x2 MonomialMatrix; received %dx%d MonomialMatrix", me.Dims()[0], me.Dims()[1])
104+
}
105+
}
106+
107+
/*
108+
TestMatrixExpression_ConcretizeMatrixExpression3
109+
Description:
110+
111+
Tests the conversion of a slice of slices of Polynomials to a PolynomialMatrix.
112+
*/
113+
func TestMatrixExpression_ConcretizeMatrixExpression3(t *testing.T) {
114+
// Setup
115+
p := symbolic.NewVariable().ToPolynomial()
116+
x := [][]symbolic.ScalarExpression{
117+
{p, p},
118+
{p, p},
119+
}
120+
121+
// Test
122+
me := symbolic.ConcretizeMatrixExpression(x)
123+
124+
// Check that me is a PolynomialMatrix
125+
if _, ok := me.(symbolic.PolynomialMatrix); !ok {
126+
t.Errorf("Expected a PolynomialMatrix; received %T", me)
127+
}
128+
129+
// Check that me is the correct PolynomialMatrix (i.e., it has the correct dimensions)
130+
if me.Dims()[0] != 2 || me.Dims()[1] != 2 {
131+
t.Errorf("Expected a 2x2 PolynomialMatrix; received %dx%d PolynomialMatrix", me.Dims()[0], me.Dims()[1])
132+
}
133+
}
134+
135+
/*
136+
TestMatrixExpression_ConcretizeMatrixExpression4
137+
Description:
138+
139+
Tests the conversion of a slice of slices of Variable objects to a VariableMatrix.
140+
*/
141+
func TestMatrixExpression_ConcretizeMatrixExpression4(t *testing.T) {
142+
// Setup
143+
v := symbolic.NewVariable()
144+
x := [][]symbolic.ScalarExpression{
145+
{v, v},
146+
{v, v},
147+
{v, v},
148+
}
149+
150+
// Test
151+
me := symbolic.ConcretizeMatrixExpression(x)
152+
153+
// Check that me is a VariableMatrix
154+
if _, ok := me.(symbolic.VariableMatrix); !ok {
155+
t.Errorf("Expected a VariableMatrix; received %T", me)
156+
}
157+
158+
// Check that me is the correct VariableMatrix (i.e., it has the correct dimensions)
159+
if me.Dims()[0] != 3 || me.Dims()[1] != 2 {
160+
t.Errorf("Expected a 3x2 VariableMatrix; received %dx%d VariableMatrix", me.Dims()[0], me.Dims()[1])
161+
}
162+
163+
}
164+
165+
/*
166+
TestMatrixExpression_ConcretizeMatrixExpression5
167+
Description:
168+
169+
Tests the conversion of a slice of slices constaining constants (K) and variables (symbolic.variable) expressions
170+
to a MonomialMatrix.
171+
*/
172+
func TestMatrixExpression_ConcretizeMatrixExpression5(t *testing.T) {
173+
// Setup
174+
k := symbolic.K(2)
175+
v := symbolic.NewVariable()
176+
x := [][]symbolic.ScalarExpression{
177+
{k, v},
178+
{v, k},
179+
}
180+
181+
// Test
182+
me := symbolic.ConcretizeMatrixExpression(x)
183+
184+
// Check that me is a MonomialMatrix
185+
if _, ok := me.(symbolic.MonomialMatrix); !ok {
186+
t.Errorf("Expected a MonomialMatrix; received %T", me)
187+
}
188+
189+
// Check that me is the correct MonomialMatrix (i.e., it has the correct dimensions)
190+
if me.Dims()[0] != 2 || me.Dims()[1] != 2 {
191+
t.Errorf("Expected a 2x2 MonomialMatrix; received %dx%d MonomialMatrix", me.Dims()[0], me.Dims()[1])
192+
}
193+
}

0 commit comments

Comments
 (0)