Skip to content

Introduced Some Basic Substitution Features #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 28 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
fdbab61
Adding the New Required Methods Substitute and SubstituteAccordingTo …
Mar 19, 2024
4ac71b3
Adding the Substitute and SubstituteAccordingTo methods to Constant-l…
Mar 30, 2024
bc032a0
Introduced Power Function (need tests!)
Mar 30, 2024
4cb5586
Introduced Substitution Functions
Apr 5, 2024
76291e6
Reverted substitution map to use map[Variable]Expression and introduc…
Apr 5, 2024
12364f9
Added Tests for Variable.Substitute and a few other functions
Apr 6, 2024
4572c41
Added Tests for KMatrix Substitute, Degree, SubstituteAccordingTo, an…
May 17, 2024
53c3262
Added Tests for Concretizing Vector Expressions
May 18, 2024
73ba89b
Added Tests for Checking Substitution Map
May 18, 2024
110fbe0
Added Tests for MonomialVector.Minus
May 18, 2024
1d58df5
Added Tests for MonomialVector.Degree and MonomialVector.Power
May 18, 2024
481ba92
Added Tests for MatrixExpression.ConcretizeMatrixExpression
May 19, 2024
bcb1529
Added Tests for Variable.Power, Variable.SubstituteAccordingTo
May 21, 2024
44db3b8
Added Tests for Monomial.Minus
May 26, 2024
6c67e5f
Added Tests for Monomial.Minus
May 26, 2024
861051b
Modified coverage file so that we don't consider the examples directory
May 26, 2024
9c1d346
Removed extraneous switch in Monomial.Minus + modified the IsVectorEx…
May 26, 2024
3e82a92
Modified ToVectorExpression as well
May 26, 2024
9002a6e
Added More Tests for Monomial.Multiply() and Monomial.DerivativeWrt()
Jun 1, 2024
f41ff0a
Added Tests for ScalarExpression.ScalarPowerTemplate
Jun 9, 2024
87250bd
Updated ConstantMatrix.Multiply to accept more types + added tests fo…
Jun 9, 2024
453fd97
Added Tests for MatrixSubstituteTemplate and MatrixPowerTemplate
Jun 9, 2024
51ed887
Simplified Some of VectorExpression functions and added tests for err…
Jun 9, 2024
d4d0666
Modified VariableVector.Minus() (to simplify it) and tested it
Jun 9, 2024
32906ce
Added Simple Test of Power Function
Jun 9, 2024
236ae42
Added Simple Test of MonomialMatrix.Substitute and MonomialMatrix.Min…
Jun 11, 2024
d22f117
Changed how we concretize matrix and vectors of monomials
Jun 28, 2024
299a125
Added tests for MonomialMatrix.SubstituteAccordingTo
Jun 28, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/coverage1.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:

- name: Test Coverage
run: |
go test -v -cover ./... -coverprofile coverage.out -coverpkg ./...
go test -v -cover $(go list ./... | grep -v /examples/) -coverprofile coverage.out -coverpkg ./...
go tool cover -func coverage.out -o coverage2.out

- name: Upload coverage reports to Codecov
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,10 @@ This project was motivated by the need for a symbolic math package for defining
optimization and control theory problems in Go, but symbolic mathematics is a topic that covers
a wide range of applications. If this tool is not useful for your purpose, then you might
find one of the following projects more helpful:

While other symbolic math libraries exist for Go, they typically focus on:
- Computer Algebra Systems that will help you get a final expression (in text) from
arbitrary math input (often in text) \[[expreduce](https://github.com/corywalker/expreduce),[sm](https://github.com/Konstantin8105/sm)\]
- Implementing Algorithms from [Domain-Specific Languages of Mathematics](https://github.com/DSLsofMath/DSLsofMath)
Course \[[gosymbol](https://github.com/victorbrun/gosymbol/tree/main)\]
- Machine Learning and algorithms needed to perform automatic differentiation \[[gorgonia](https://github.com/gorgonia/gorgonia)\]
20 changes: 20 additions & 0 deletions smErrors/negative_exponent.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package smErrors

import "fmt"

/*
negative_exponent.go
Description:

Functions related to the negative exponent error.
*/

// Type Definition
type NegativeExponentError struct {
Exponent int
}

// Error
func (e NegativeExponentError) Error() string {
return fmt.Sprintf("received negative exponent (%v); expected non-negative exponent", e.Exponent)
}
34 changes: 34 additions & 0 deletions symbolic/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ func (c K) Plus(rightIn interface{}) Expression {
// Convert to VectorExpression
ve, _ := ToVectorExpression(right)
return ve.Plus(c)
case mat.Dense:
return c.Plus(DenseToKMatrix(right))
case *mat.Dense:
return c.Plus(DenseToKMatrix(*right))
case KMatrix, VariableMatrix, MonomialMatrix, PolynomialMatrix:
// Convert to MatrixExpression
me, _ := ToMatrixExpression(right)
Expand Down Expand Up @@ -379,3 +383,33 @@ Description:
func (c K) String() string {
return fmt.Sprintf("%v", float64(c))
}

/*
Substitute
Description:

Substitutes the variable vIn with the expression eIn.
*/
func (c K) Substitute(vIn Variable, eIn ScalarExpression) Expression {
return c
}

/*
SubstituteAccordingTo
Description:

Substitutes the variables in the map with the corresponding expressions.
*/
func (c K) SubstituteAccordingTo(subMap map[Variable]Expression) Expression {
return c
}

/*
Power
Description:

Computes the power of the constant.
*/
func (c K) Power(exponent int) Expression {
return ScalarPowerTemplate(c, exponent)
}
73 changes: 73 additions & 0 deletions symbolic/constant_matrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,23 @@ func (km KMatrix) Plus(e interface{}) Expression {

return km.Plus(rightAsVM) // Reuse VariableMatrix case

case mat.Dense:
return km.Plus(DenseToKMatrix(right)) // Reuse KMatrix case

case *mat.Dense:
return km.Plus(*right) // Reuse mat.Dense case

case KMatrix:
// Create the result matrix
var result KMatrix = make([][]K, nR)
for rIndex := 0; rIndex < nR; rIndex++ {
result[rIndex] = make([]K, nC)
for cIndex := 0; cIndex < nC; cIndex++ {
result[rIndex][cIndex] = km[rIndex][cIndex] + right[rIndex][cIndex]
}
}
return result

case VariableMatrix:
// Create the result matrix
var result PolynomialMatrix = make([][]Polynomial, nR)
Expand Down Expand Up @@ -257,6 +274,32 @@ func (km KMatrix) Multiply(e interface{}) Expression {

case K:
return km.Multiply(float64(right)) // Reuse float64 case
case Polynomial:
// Choose the correct output type based on the size of km
nR, nC := km.Dims()[0], km.Dims()[1]
switch {
case (nR == 1) && (nC == 1):
// If the output is a scalar, return a scalar
return km[0][0].Multiply(right)
case nC == 1:
// If the output is a vector, return a vector
var outputVec PolynomialVector = make([]Polynomial, nR)
for rIndex := 0; rIndex < nR; rIndex++ {
outputVec[rIndex] = km[rIndex][0].Multiply(right.Copy()).(Polynomial)
}
return outputVec
default:
// If the output is a matrix, return a matrix
var outputMat PolynomialMatrix = make([][]Polynomial, nR)
for rIndex := 0; rIndex < nR; rIndex++ {
outputMat[rIndex] = make([]Polynomial, nC)
for cIndex := 0; cIndex < nC; cIndex++ {
outputMat[rIndex][cIndex] = km[rIndex][cIndex].Multiply(right.Copy()).(Polynomial)
}
}
return outputMat
}

case *mat.VecDense:
// Use gonum's built-in multiplication function
var product mat.VecDense
Expand Down Expand Up @@ -673,3 +716,33 @@ Description:
func (km KMatrix) Degree() int {
return 0
}

/*
Substitute
Description:

Substitutes all occurrences of variable vIn with the expression eIn.
*/
func (km KMatrix) Substitute(vIn Variable, eIn ScalarExpression) Expression {
return km
}

/*
SubstituteAccordingTo
Description:

Substitutes all occurrences of the variables in the map with the corresponding expressions.
*/
func (km KMatrix) SubstituteAccordingTo(subMap map[Variable]Expression) Expression {
return km
}

/*
Power
Description:

Raises the constant matrix to the power of the input integer.
*/
func (km KMatrix) Power(exponent int) Expression {
return MatrixPowerTemplate(km, exponent)
}
30 changes: 30 additions & 0 deletions symbolic/constant_vector.go
Original file line number Diff line number Diff line change
Expand Up @@ -591,3 +591,33 @@ Description:
func (kv KVector) Degree() int {
return 0
}

/*
Substitute
Description:

Substitutes all occurrences of variable vIn with the expression eIn.
*/
func (kv KVector) Substitute(vIn Variable, eIn ScalarExpression) Expression {
return kv
}

/*
SubstituteAccordingTo
Description:

Substitutes all occurrences of the variables in the map with the corresponding expressions.
*/
func (kv KVector) SubstituteAccordingTo(subMap map[Variable]Expression) Expression {
return kv
}

/*
Power
Description:

Raises the scalar expression to the power of the input integer.
*/
func (kv KVector) Power(exponent int) Expression {
return VectorPowerTemplate(kv, exponent)
}
1 change: 1 addition & 0 deletions symbolic/constraint.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type Constraint interface {
Right() Expression
ConstrSense() ConstrSense
Check() error
IsLinear() bool
}

func IsConstraint(c interface{}) bool {
Expand Down
10 changes: 10 additions & 0 deletions symbolic/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ type Expression interface {

// String returns a string representation of the expression
String() string

// Substitute returns the expression with the variable vIn replaced with the expression eIn
Substitute(vIn Variable, eIn ScalarExpression) Expression

// SubstituteAccordingToMap returns the expression with the variables in the map replaced with the corresponding expressions
SubstituteAccordingTo(subMap map[Variable]Expression) Expression

// Power
// Raises the scalar expression to the power of the input integer
Power(exponent int) Expression
}

/*
Expand Down
Loading
Loading