Skip to content

Commit

Permalink
Added tests for MonomialMatrix.SubstituteAccordingTo
Browse files Browse the repository at this point in the history
  • Loading branch information
Kwesi Rutledge committed Jun 28, 2024
1 parent d22f117 commit 299a125
Showing 1 changed file with 214 additions and 0 deletions.
214 changes: 214 additions & 0 deletions testing/symbolic/monomial_matrix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2007,6 +2007,79 @@ func TestMonomialMatrix_String2(t *testing.T) {
mm.String()
}

/*
TestMonomialMatrix_Degree1
Description:
Tests that the Degree() method properly panics when called with a monomial matrix
that is not well-defined.
*/
func TestMonomialMatrix_Degree1(t *testing.T) {
// Constants
var mm symbolic.MonomialMatrix

// Test
defer func() {
r := recover()
if r == nil {
t.Errorf(
"expected Degree() to panic; it did not",
)
}

rAsE, ok := r.(error)
if !ok {
t.Errorf(
"expected Degree() to panic with an error; it panicked with %v",
r,
)
}

expectedError := mm.Check()
if !strings.Contains(rAsE.Error(), expectedError.Error()) {
t.Errorf(
"expected Degree() to panic with error \"%v\"; it panicked with \"%v\"",
expectedError,
rAsE,
)
}
}()

mm.Degree()
}

/*
TestMonomialMatrix_Degree2
Description:
Tests that the Degree() method properly returns the degree of the monomial matrix
when called with a well-defined monomial matrix.
*/
func TestMonomialMatrix_Degree2(t *testing.T) {
// Constants
v1 := symbolic.NewVariable()
v2 := symbolic.NewVariable()
v3 := symbolic.NewVariable()
m1 := v1.ToMonomial()
m2 := v2.ToMonomial()
m3 := v3.ToMonomial()
var mm symbolic.MonomialMatrix = [][]symbolic.Monomial{
{m1, m2, m3},
{m1, symbolic.Monomial{Coefficient: 3.14, VariableFactors: []symbolic.Variable{v1, v2, v3}, Exponents: []int{1, 3, 5}}, m3},
}

// Test
degree := mm.Degree()

// Check that the degree is correct
if degree != 9 {
t.Errorf(
"expected Degree() to return 1; received %v",
degree,
)
}
}

/*
TestMonomialMatrix_Substitute1
Description:
Expand Down Expand Up @@ -2053,3 +2126,144 @@ func TestMonomialMatrix_Substitute1(t *testing.T) {
}
}
}

/*
TestMonomialMatrix_SubstituteWrt1
Description:
Tests that the SubstituteWrt() method properly substitutes a variable in a given
monomial matrix with a polynomial. This should lead to the matrix of monomials
becoming a matrix of polynomials. Each entry of the matrix should have the same number
of monomials as the test polynomial.
*/
func TestMonomialMatrix_SubstituteAccordingTo1(t *testing.T) {
// Setup
v1 := symbolic.NewVariable()
v2 := symbolic.NewVariable()
p3 := v1.Plus(v2).(symbolic.Polynomial)

mm1 := symbolic.MonomialMatrix{
{v1.ToMonomial(), v1.ToMonomial()},
{v1.ToMonomial(), v1.ToMonomial()},
}

// Test
substituted := mm1.SubstituteAccordingTo(
map[symbolic.Variable]symbolic.Expression{v1: p3},
)

// Check that substituted is a PolynomialMatrix
sAsPM, ok := substituted.(symbolic.PolynomialMatrix)
if !ok {
t.Errorf(
"expected SubstituteWrt() to return a PolynomialMatrix; received %v",
substituted,
)
}

// Check that each entry in the substituted matrix has the same number of monomials
// as p3
for _, row := range sAsPM {
for _, polynomial := range row {
if len(polynomial.Monomials) != len(p3.Monomials) {
t.Errorf(
"expected SubstituteWrt() to return a PolynomialMatrix with %v monomials; received %v",
len(p3.Monomials),
len(polynomial.Monomials),
)
}
}
}
}

/*
TestMonomialMatrix_SubstituteWrt2
Description:
Tests that the SubstituteWrt() method properly panics when called with a monomial matrix
that is not well-defined.
*/
func TestMonomialMatrix_SubstituteAccordingTo2(t *testing.T) {
// Constants
var mm symbolic.MonomialMatrix

// Test
defer func() {
r := recover()
if r == nil {
t.Errorf(
"expected SubstituteAccordingTo() to panic; it did not",
)
}

rAsE, ok := r.(error)
if !ok {
t.Errorf(
"expected SubstituteAccordingTo() to panic with an error; it panicked with %v",
r,
)
}

expectedError := mm.Check()
if !strings.Contains(rAsE.Error(), expectedError.Error()) {
t.Errorf(
"expected SubstituteAccordingTo() to panic with error \"%v\"; it panicked with \"%v\"",
expectedError,
rAsE,
)
}
}()

mm.SubstituteAccordingTo(map[symbolic.Variable]symbolic.Expression{})
}

/*
TestMonomialMatrix_SubstituteWrt3
Description:
Tests that the SubstituteWrt() method properly panics when called with a monomial matrix
that is well-defined and a substitution map that is not well-defined.
*/
func TestMonomialMatrix_SubstituteAccordingTo3(t *testing.T) {
// Constants
v1 := symbolic.NewVariable()
m1 := v1.ToMonomial()
var mm symbolic.MonomialMatrix = [][]symbolic.Monomial{
{m1, m1},
{m1, m1},
}

testMap := map[symbolic.Variable]symbolic.Expression{
v1: symbolic.Variable{},
}

// Test
defer func() {
r := recover()
if r == nil {
t.Errorf(
"expected SubstituteAccordingTo() to panic; it did not",
)
}

rAsE, ok := r.(error)
if !ok {
t.Errorf(
"expected SubstituteAccordingTo() to panic with an error; it panicked with %v",
r,
)
}

expectedError := symbolic.CheckSubstitutionMap(testMap)
if !strings.Contains(rAsE.Error(), expectedError.Error()) {
t.Errorf(
"expected SubstituteAccordingTo() to panic with error \"%v\"; it panicked with \"%v\"",
expectedError,
rAsE,
)
}
}()

mm.SubstituteAccordingTo(testMap)
t.Errorf("expected SubstituteAccordingTo() to panic; it did not")
}

0 comments on commit 299a125

Please sign in to comment.