Skip to content

Commit 6ebb81b

Browse files
committed
Simplified VarVector.ToSymbolic and tested it
1 parent 7d2b6b2 commit 6ebb81b

File tree

2 files changed

+65
-14
lines changed

2 files changed

+65
-14
lines changed

optim/var_vector.go

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -454,20 +454,8 @@ func (vv VarVector) ToSymbolic() (symbolic.Expression, error) {
454454

455455
// Add each variable to the vector
456456
for _, elt := range vv.Elements {
457-
eltAsSymExpr, err := elt.ToSymbolic()
458-
if err != nil {
459-
return nil, fmt.Errorf(
460-
"could not convert variable %v to symbolic variable",
461-
elt,
462-
)
463-
}
464-
eltAsSymVar, ok := eltAsSymExpr.(symbolic.Variable)
465-
if !ok {
466-
return nil, fmt.Errorf(
467-
"could not convert variable %v to symbolic variable",
468-
elt,
469-
)
470-
}
457+
eltAsSymExpr, _ := elt.ToSymbolic() // No errors should occur because elts were checked above
458+
eltAsSymVar, _ := eltAsSymExpr.(symbolic.Variable) // No errors should occur because elt must be a variable
471459
symVVec = append(symVVec, eltAsSymVar)
472460
}
473461

testing/optim/var_vector_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package optim_test
33
import (
44
"fmt"
55
"github.com/MatProGo-dev/MatProInterface.go/optim"
6+
"github.com/MatProGo-dev/SymbolicMath.go/symbolic"
67
"gonum.org/v1/gonum/mat"
78
"strings"
89
"testing"
@@ -1478,3 +1479,65 @@ func TestVarVector_Multiply8(t *testing.T) {
14781479
}
14791480

14801481
}
1482+
1483+
/*
1484+
TestVarVector_ToSymbolic1
1485+
Description:
1486+
1487+
Tests that the ToSymbolic() produces an error when the VarVector has one
1488+
variable that is not well-defined.
1489+
*/
1490+
func TestVarVector_ToSymbolic1(t *testing.T) {
1491+
// Constants
1492+
m := optim.NewModel("TestVarVector_ToSymbolic1")
1493+
v1 := m.AddVariable()
1494+
1495+
vv2 := optim.VarVector{}
1496+
for i := 0; i < 10; i++ {
1497+
if i == 5 {
1498+
vv2.Elements = append(vv2.Elements, optim.Variable{Lower: -1, Upper: -2})
1499+
} else {
1500+
vv2.Elements = append(vv2.Elements, v1)
1501+
}
1502+
}
1503+
1504+
// Run ToSymbolic
1505+
_, err := vv2.ToSymbolic()
1506+
if err == nil {
1507+
t.Errorf("expected error, but received none!")
1508+
} else {
1509+
if !strings.Contains(
1510+
err.Error(),
1511+
fmt.Sprintf("element %v has an issue: %v", 5, "lower bound (-1) of variable is above upper bound (-2)."),
1512+
) {
1513+
t.Errorf("unexpected error: %v", err)
1514+
}
1515+
}
1516+
1517+
}
1518+
1519+
/*
1520+
TestVarVector_ToSymbolic2
1521+
Description:
1522+
1523+
Tests that the ToSymbolic() does not produce an error when the VarVector
1524+
is well-defined. In addition, the output should be of type symbolic.VariableVector
1525+
*/
1526+
func TestVarVector_ToSymbolic2(t *testing.T) {
1527+
// Constants
1528+
m := optim.NewModel("TestVarVector_ToSymbolic2")
1529+
N := 10
1530+
1531+
vv2 := m.AddVariableVector(N)
1532+
1533+
// Run ToSymbolic
1534+
symVv2, err := vv2.ToSymbolic()
1535+
if err != nil {
1536+
t.Errorf("unexpected error: %v", err)
1537+
}
1538+
1539+
_, tf := symVv2.(symbolic.VariableVector)
1540+
if !tf {
1541+
t.Errorf("expected output to be of type symbolic.VariableVector; received %T instead", symVv2)
1542+
}
1543+
}

0 commit comments

Comments
 (0)