Skip to content

Commit 236ae42

Browse files
author
Kwesi Rutledge
committed
Added Simple Test of MonomialMatrix.Substitute and MonomialMatrix.Minus Functions
1 parent 32906ce commit 236ae42

File tree

2 files changed

+252
-10
lines changed

2 files changed

+252
-10
lines changed

Diff for: symbolic/monomial_matrix.go

-10
Original file line numberDiff line numberDiff line change
@@ -251,16 +251,6 @@ func (mm MonomialMatrix) Minus(e interface{}) Expression {
251251
return Minus(mm, eAsE)
252252
}
253253

254-
// Constants
255-
switch right := e.(type) {
256-
case float64:
257-
return mm.Minus(K(right))
258-
case mat.Dense:
259-
return mm.Minus(DenseToKMatrix(right))
260-
case *mat.Dense:
261-
return mm.Minus(DenseToKMatrix(*right))
262-
}
263-
264254
// If we've gotten this far, the input is not recognized
265255
panic(
266256
smErrors.UnsupportedInputError{

Diff for: testing/symbolic/monomial_matrix_test.go

+252
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,211 @@ func TestMonomialMatrix_Plus9(t *testing.T) {
776776
mm.Plus("a")
777777
}
778778

779+
/*
780+
TestMonomialMatrix_Minus1
781+
Description:
782+
783+
Verifies that the Minus() method panics if the monomial matrix
784+
that it is called on is not well formed.
785+
*/
786+
func TestMonomialMatrix_Minus1(t *testing.T) {
787+
// Constants
788+
v1 := symbolic.NewVariable()
789+
m1 := symbolic.Monomial{
790+
Coefficient: 1.0,
791+
VariableFactors: []symbolic.Variable{v1},
792+
Exponents: []int{1, 2},
793+
}
794+
var mm symbolic.MonomialMatrix = [][]symbolic.Monomial{
795+
{m1, m1},
796+
{m1},
797+
{m1, m1},
798+
}
799+
mm2 := mm
800+
801+
// Test
802+
defer func() {
803+
if r := recover(); r == nil {
804+
t.Errorf(
805+
"expected Minus() to panic; it did not",
806+
)
807+
}
808+
}()
809+
mm.Minus(mm2)
810+
}
811+
812+
/*
813+
TestMonomialMatrix_Minus2
814+
Description:
815+
816+
Verifies that the Minus() method panics if the second expression
817+
input to the method is not well formed.
818+
*/
819+
func TestMonomialMatrix_Minus2(t *testing.T) {
820+
// Constants
821+
v1 := symbolic.NewVariable()
822+
var mm symbolic.MonomialMatrix = [][]symbolic.Monomial{
823+
{v1.ToMonomial(), v1.ToMonomial()},
824+
{v1.ToMonomial(), v1.ToMonomial()},
825+
}
826+
var mm2 symbolic.MonomialMatrix
827+
828+
expectedError := mm2.Check()
829+
830+
// Test
831+
defer func() {
832+
r := recover()
833+
if r == nil {
834+
t.Errorf(
835+
"expected Minus() to panic; it did not",
836+
)
837+
}
838+
839+
// Check that the error is correct
840+
err, ok := r.(error)
841+
if !ok {
842+
t.Errorf(
843+
"expected Minus() to panic with an error; it panicked with %v",
844+
r,
845+
)
846+
}
847+
848+
if !strings.Contains(
849+
err.Error(),
850+
expectedError.Error(),
851+
) {
852+
t.Errorf(
853+
"expected Minus() to panic with error \"%v\"; it panicked with \"%v\"",
854+
expectedError,
855+
err,
856+
)
857+
}
858+
859+
}()
860+
mm.Minus(mm2)
861+
862+
}
863+
864+
/*
865+
TestMonomialMatrix_Minus3
866+
Description:
867+
868+
Verifies that the Minus() method panics if the two expressions
869+
are not the same size. (In this case the first expression
870+
is 3 x 2 and the second is 2 x 2).
871+
*/
872+
func TestMonomialMatrix_Minus3(t *testing.T) {
873+
// Constants
874+
v1 := symbolic.NewVariable()
875+
var mm symbolic.MonomialMatrix = [][]symbolic.Monomial{
876+
{v1.ToMonomial(), v1.ToMonomial()},
877+
{v1.ToMonomial(), v1.ToMonomial()},
878+
{v1.ToMonomial(), v1.ToMonomial()},
879+
}
880+
var mm2 symbolic.MonomialMatrix = [][]symbolic.Monomial{
881+
{v1.ToMonomial(), v1.ToMonomial()},
882+
{v1.ToMonomial(), v1.ToMonomial()},
883+
}
884+
expectedError := smErrors.DimensionError{
885+
Arg1: mm,
886+
Arg2: mm2,
887+
Operation: "Minus",
888+
}
889+
890+
// Test
891+
defer func() {
892+
r := recover()
893+
if r == nil {
894+
t.Errorf(
895+
"expected Minus() to panic; it did not",
896+
)
897+
}
898+
899+
// Check that the error is correct
900+
err, ok := r.(error)
901+
if !ok {
902+
t.Errorf(
903+
"expected Minus() to panic with an error; it panicked with %v",
904+
r,
905+
)
906+
}
907+
908+
if !strings.Contains(
909+
err.Error(),
910+
expectedError.Error(),
911+
) {
912+
t.Errorf(
913+
"expected Minus() to panic with error \"%v\"; it panicked with \"%v\"",
914+
expectedError,
915+
err,
916+
)
917+
}
918+
919+
}()
920+
mm.Minus(mm2)
921+
}
922+
923+
/*
924+
TestMonomialMatrix_Minus4
925+
Description:
926+
927+
Tests that the Minus() method properly subtracts a constant from a matrix
928+
of Monomials. (In this case, the monomial matrix is a 2 x2 matrix
929+
of single variables and the constant is 1.0).
930+
*/
931+
func TestMonomialMatrix_Minus4(t *testing.T) {
932+
// Constants
933+
v1 := symbolic.NewVariable()
934+
var mm symbolic.MonomialMatrix = [][]symbolic.Monomial{
935+
{v1.ToMonomial(), v1.ToMonomial()},
936+
{v1.ToMonomial(), v1.ToMonomial()},
937+
}
938+
f2 := 1.0
939+
940+
// Test
941+
difference := mm.Minus(f2)
942+
943+
// Check that the difference is of the PolynomialMatrix type
944+
_, ok := difference.(symbolic.PolynomialMatrix)
945+
if !ok {
946+
t.Errorf(
947+
"expected Minus() to return a PolynomialMatrix; received %v",
948+
difference,
949+
)
950+
}
951+
952+
// Check that each entry in the difference:
953+
// 1. Contains 2 monomials
954+
// 2. Contains the original monomial
955+
// 3. Contains the constant monomial
956+
for _, row := range difference.(symbolic.PolynomialMatrix) {
957+
for _, polynomial := range row {
958+
if len(polynomial.Monomials) != 2 {
959+
t.Errorf(
960+
"expected Minus() to return a PolynomialMatrix with 2 monomials; received %v",
961+
polynomial.Monomials,
962+
)
963+
}
964+
965+
// Check that the first monomial is the original monomial
966+
if v1Index := polynomial.VariableMonomialIndex(v1); v1Index == -1 {
967+
t.Errorf(
968+
"expected Minus() to return a PolynomialMatrix with the original monomial; received %v",
969+
polynomial,
970+
)
971+
}
972+
973+
// Check that the second monomial is the constant monomial
974+
if constIndex := polynomial.ConstantMonomialIndex(); polynomial.Monomials[constIndex].Coefficient != -1.0 {
975+
t.Errorf(
976+
"expected Minus() to return a PolynomialMatrix with the constant monomial; received %v",
977+
polynomial.Monomials[1],
978+
)
979+
}
980+
}
981+
}
982+
}
983+
779984
/*
780985
TestMonomialMatrix_Multiply1
781986
Description:
@@ -1632,3 +1837,50 @@ func TestMonomialMatrix_String2(t *testing.T) {
16321837

16331838
mm.String()
16341839
}
1840+
1841+
/*
1842+
TestMonomialMatrix_Substitute1
1843+
Description:
1844+
1845+
Tests that the Substitute() method properly substitutes a variable in a given
1846+
monomial matrix with a polynomial. This should lead to the matrix of monomials
1847+
becoming a matrix of polynomials. Each entry of the matrix should have the same number
1848+
of monomials as the first polynomial.
1849+
*/
1850+
func TestMonomialMatrix_Substitute1(t *testing.T) {
1851+
// Setup
1852+
v1 := symbolic.NewVariable()
1853+
v2 := symbolic.NewVariable()
1854+
p3 := v1.Plus(v2).(symbolic.Polynomial)
1855+
1856+
mm1 := symbolic.MonomialMatrix{
1857+
{v1.ToMonomial(), v1.ToMonomial()},
1858+
{v1.ToMonomial(), v1.ToMonomial()},
1859+
}
1860+
1861+
// Test
1862+
substituted := mm1.Substitute(v1, p3)
1863+
1864+
// Check that substituted is a PolynomialMatrix
1865+
sAsPM, ok := substituted.(symbolic.PolynomialMatrix)
1866+
if !ok {
1867+
t.Errorf(
1868+
"expected Substitute() to return a PolynomialMatrix; received %v",
1869+
substituted,
1870+
)
1871+
}
1872+
1873+
// Check that each entry in the substituted matrix has the same number of monomials
1874+
// as p3
1875+
for _, row := range sAsPM {
1876+
for _, polynomial := range row {
1877+
if len(polynomial.Monomials) != len(p3.Monomials) {
1878+
t.Errorf(
1879+
"expected Substitute() to return a PolynomialMatrix with %v monomials; received %v",
1880+
len(p3.Monomials),
1881+
len(polynomial.Monomials),
1882+
)
1883+
}
1884+
}
1885+
}
1886+
}

0 commit comments

Comments
 (0)