Skip to content

Commit 3781668

Browse files
author
Kwesi Rutledge
committed
Added another example of how to use mat.Dense in SymbolicMath.go
1 parent 84582f3 commit 3781668

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Some key features include:
1212
in as few lines as you wish (e.g., `x.Transpose().Multiply(Q).Multiply(x)`)
1313
- Simple API for defining constants and variables
1414
- Recognition of [gonum](https://www.gonum.org/) matrices and vectors
15-
in most operations
15+
in most operations (e.g., `vv1.Plus(mat.NewVecDense(N, []float64{1, 2, 3}))`)
1616

1717
Some documentation can be found by clicking the "reference" badge above.
1818

examples/gonum2/gonum2.go

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"github.com/MatProGo-dev/SymbolicMath.go/symbolic"
6+
"gonum.org/v1/gonum/mat"
7+
)
8+
9+
/*
10+
gonum2.go
11+
Description:
12+
13+
This script is meant to demonstrate the use of the gonum package's
14+
matrices in an operation with the symbolic package.
15+
*/
16+
func main() {
17+
// Constants
18+
N := 3
19+
vv1 := symbolic.NewVariableVector(N)
20+
Q := mat.NewDense(N, N, []float64{1, 0, 0, 0, 2, 0, 0, 0, 3})
21+
22+
// Sum vv1 with a mat.VecDense object from the gonum package
23+
sum2 := vv1.Transpose().Multiply(Q)
24+
25+
// Print the result
26+
fmt.Println(sum2.String())
27+
}

symbolic/variable_matrix.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,12 @@ func (vm VariableMatrix) Multiply(e interface{}) Expression {
248248
}
249249
return result
250250
}
251-
251+
case *mat.Dense:
252+
// Use the mat.Dense case
253+
return vm.Multiply(DenseToKMatrix(*right))
254+
case mat.Dense:
255+
// Use the KMatrix case
256+
return vm.Multiply(DenseToKMatrix(right))
252257
case KMatrix:
253258
// Collect dimensions
254259
nResultRows, nResultCols := vm.Dims()[0], right.Dims()[1]

0 commit comments

Comments
 (0)