Skip to content

Commit

Permalink
update to mat32 1.0.9 math functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Randall C. O'Reilly committed Apr 29, 2021
1 parent 4835368 commit e86310e
Show file tree
Hide file tree
Showing 15 changed files with 133 additions and 98 deletions.
4 changes: 2 additions & 2 deletions emer/layer.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ type Layer interface {
// useful when there are multiple projections between two layers.
// Returns error on invalid var name.
// If the receiving neuron is not connected to the given sending layer or neuron
// then the value is set to math32.NaN().
// then the value is set to mat32.NaN().
// Returns error on invalid var name or lack of recv prjn (vals always set to nan on prjn err).
RecvPrjnVals(vals *[]float32, varNm string, sendLay Layer, sendIdx1D int, prjnType string) error

Expand All @@ -195,7 +195,7 @@ type Layer interface {
// useful when there are multiple projections between two layers.
// Returns error on invalid var name.
// If the sending neuron is not connected to the given receiving layer or neuron
// then the value is set to math32.NaN().
// then the value is set to mat32.NaN().
// Returns error on invalid var name or lack of recv prjn (vals always set to nan on prjn err).
SendPrjnVals(vals *[]float32, varNm string, recvLay Layer, recvIdx1D int, prjnType string) error

Expand Down
4 changes: 2 additions & 2 deletions emer/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ type Network interface {

// UnitVarNames returns a list of variable names available on the units in this network.
// This list determines what is shown in the NetView (and the order of vars list).
// Not all layers need to support all variables, but must safely return math32.NaN() for
// Not all layers need to support all variables, but must safely return mat32.NaN() for
// unsupported ones.
// This is typically a global list so do not modify!
UnitVarNames() []string
Expand All @@ -84,7 +84,7 @@ type Network interface {

// SynVarNames returns the names of all the variables on the synapses in this network.
// This list determines what is shown in the NetView (and the order of vars list).
// Not all projections need to support all variables, but must safely return math32.NaN() for
// Not all projections need to support all variables, but must safely return mat32.NaN() for
// unsupported ones.
// This is typically a global list so do not modify!
SynVarNames() []string
Expand Down
2 changes: 1 addition & 1 deletion emer/prjn.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ type Prjn interface {

// SynVal returns value of given variable name on the synapse
// between given send, recv unit indexes (1D, flat indexes).
// Returns math32.NaN() for access errors.
// Returns mat32.NaN() for access errors.
SynVal(varNm string, sidx, ridx int) float32

// SetSynVal sets value of given variable name on the synapse
Expand Down
10 changes: 5 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ go 1.13

require (
github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883
github.com/chewxy/math32 v1.0.6
github.com/emer/etable v1.0.26
github.com/goki/gi v1.2.6
github.com/emer/etable v1.0.27
github.com/goki/gi v1.2.7
github.com/goki/ki v1.1.3
github.com/goki/mat32 v1.0.8
gonum.org/v1/gonum v0.9.0
github.com/goki/mat32 v1.0.9
golang.org/x/exp v0.0.0-20210429022752-aa422307df1f // indirect
gonum.org/v1/gonum v0.9.1
)
122 changes: 81 additions & 41 deletions go.sum

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions netview/netdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ import (
"path/filepath"
"strings"

"github.com/chewxy/math32"
"github.com/emer/emergent/emer"
"github.com/emer/emergent/ringidx"
"github.com/goki/gi/gi"
"github.com/goki/ki/ki"
"github.com/goki/ki/kit"
"github.com/goki/mat32"
)

// LayData maintains a record of all the data for a given layer
Expand Down Expand Up @@ -161,9 +161,9 @@ func (nd *NetData) Record(ctrs string) {
}
for ui := range dvals {
vl := dvals[ui]
if !math32.IsNaN(vl) {
*mn = math32.Min(*mn, vl)
*mx = math32.Max(*mx, vl)
if !mat32.IsNaN(vl) {
*mn = mat32.Min(*mn, vl)
*mx = mat32.Max(*mx, vl)
}
}
}
Expand All @@ -185,8 +185,8 @@ func (nd *NetData) UpdateVarRange() {
mmidx := ri * vlen
mn := nd.MinPer[mmidx+vi]
mx := nd.MaxPer[mmidx+vi]
*vmn = math32.Min(*vmn, mn)
*vmx = math32.Max(*vmx, mx)
*vmn = mat32.Min(*vmn, mn)
*vmx = mat32.Max(*vmx, mx)
}
}
}
Expand Down Expand Up @@ -245,7 +245,7 @@ func (nd *NetData) UnitVal(laynm string, vnm string, uidx1d int, recno int) (flo
nvu := vlen * nu
idx := ridx*nvu + vi*nu + uidx1d
val := ld.Data[idx]
if math32.IsNaN(val) {
if mat32.IsNaN(val) {
return 0, false
}
return val, true
Expand Down Expand Up @@ -301,7 +301,7 @@ func (nd *NetData) SaveJSON(filename gi.FileName) error {
func (nd *NetData) ReadJSON(r io.Reader) error {
dec := json.NewDecoder(r)
err := dec.Decode(nd) // this is way to do it on reader instead of bytes
nan := math32.NaN()
nan := mat32.NaN()
for _, ld := range nd.LayData {
for i := range ld.Data {
if ld.Data[i] == NaNSub {
Expand All @@ -323,7 +323,7 @@ const NaNSub = -1.11e-37
func (nd *NetData) WriteJSON(w io.Writer) error {
for _, ld := range nd.LayData {
for i := range ld.Data {
if math32.IsNaN(ld.Data[i]) {
if mat32.IsNaN(ld.Data[i]) {
ld.Data[i] = NaNSub
}
}
Expand Down
3 changes: 1 addition & 2 deletions netview/netview.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"strings"
"sync"

"github.com/chewxy/math32"
"github.com/emer/emergent/emer"
"github.com/emer/etable/minmax"
"github.com/goki/gi/gi"
Expand Down Expand Up @@ -172,7 +171,7 @@ func (nv *NetView) UpdateImpl() {
}
}
if vp.ZeroCtr && !vp.Range.FixMin && !vp.Range.FixMax {
bmax := math32.Max(math32.Abs(vp.Range.Max), math32.Abs(vp.Range.Min))
bmax := mat32.Max(mat32.Abs(vp.Range.Max), mat32.Abs(vp.Range.Min))
if !needUpdt {
if vp.Range.Max != bmax || vp.Range.Min != -bmax {
needUpdt = true
Expand Down
6 changes: 3 additions & 3 deletions patgen/permuted.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import (
"math"
"math/rand"

"github.com/chewxy/math32"
"github.com/emer/emergent/erand"
"github.com/emer/etable/etensor"
"github.com/emer/etable/metric"
"github.com/goki/ki/ints"
"github.com/goki/mat32"
)

// PermutedBinary sets the given tensor to contain nOn onVal values and the
Expand Down Expand Up @@ -142,8 +142,8 @@ func RowVsPrevDist32(tsr *etensor.Float32, row int, fun metric.Func32) (min, max
for i := 0; i <= row-1; i++ {
crow := tsr.SubSpace([]int{i}).(*etensor.Float32)
dst := fun(lrow.Values, crow.Values)
min = math32.Min(min, dst)
max = math32.Max(max, dst)
min = mat32.Min(min, dst)
max = mat32.Max(max, dst)
}
return
}
9 changes: 4 additions & 5 deletions popcode/popcode1d.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package popcode
import (
"sort"

"github.com/chewxy/math32"
"github.com/goki/mat32"
)

Expand Down Expand Up @@ -84,9 +83,9 @@ func (pc *OneD) Encode(pat *[]float32, val float32, n int, add bool) {
switch pc.Code {
case GaussBump:
dist := gnrm * (trg - val)
act = math32.Exp(-(dist * dist))
act = mat32.Exp(-(dist * dist))
case Localist:
dist := math32.Abs(trg - val)
dist := mat32.Abs(trg - val)
if dist > incr {
act = 0
} else {
Expand Down Expand Up @@ -122,7 +121,7 @@ func (pc *OneD) Decode(pat []float32) float32 {
avg += trg * act
sum += act
}
sum = math32.Max(sum, pc.MinSum)
sum = mat32.Max(sum, pc.MinSum)
avg /= sum
return avg
}
Expand Down Expand Up @@ -205,7 +204,7 @@ func (pc *OneD) DecodeNPeaks(pat []float32, nvals, width int) []float32 {
avg += trg * act
sum += act
}
sum = math32.Max(sum, pc.MinSum)
sum = mat32.Max(sum, pc.MinSum)
vals[i] = avg / sum
}

Expand Down
11 changes: 5 additions & 6 deletions popcode/popcode2d.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"log"
"sort"

"github.com/chewxy/math32"
"github.com/emer/etable/etensor"
"github.com/goki/mat32"
)
Expand Down Expand Up @@ -74,11 +73,11 @@ func (pc *TwoD) Encode(pat etensor.Tensor, val mat32.Vec2, add bool) error {
switch pc.Code {
case GaussBump:
dist := trg.Sub(val).Mul(gnrm)
act = math32.Exp(-dist.LengthSq())
act = mat32.Exp(-dist.LengthSq())
case Localist:
dist := trg.Sub(val)
dist.X = math32.Abs(dist.X)
dist.Y = math32.Abs(dist.Y)
dist.X = mat32.Abs(dist.X)
dist.Y = mat32.Abs(dist.Y)
if dist.X > incr.X || dist.Y > incr.Y {
act = 0
} else {
Expand Down Expand Up @@ -127,7 +126,7 @@ func (pc *TwoD) Decode(pat etensor.Tensor) (mat32.Vec2, error) {
sum += act
}
}
sum = math32.Max(sum, pc.MinSum)
sum = mat32.Max(sum, pc.MinSum)
return avg.DivScalar(sum), nil
}

Expand Down Expand Up @@ -243,7 +242,7 @@ func (pc *TwoD) DecodeNPeaks(pat etensor.Tensor, nvals, width int) ([]mat32.Vec2
sum += act
}
}
sum = math32.Max(sum, pc.MinSum)
sum = mat32.Max(sum, pc.MinSum)
vals[i] = avg.DivScalar(sum)
}

Expand Down
23 changes: 11 additions & 12 deletions popcode/popcode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package popcode
import (
"testing"

"github.com/chewxy/math32"
"github.com/emer/etable/etensor"
"github.com/goki/mat32"
)
Expand All @@ -19,7 +18,7 @@ const difTolMulti = float32(1.0e-2)

func CmprFloats(out, cor []float32, msg string, t *testing.T) {
for i := range out {
dif := math32.Abs(out[i] - cor[i])
dif := mat32.Abs(out[i] - cor[i])
if dif > difTol { // allow for small numerical diffs
t.Errorf("%v err: out: %v, cor: %v, dif: %v\n", msg, out[i], cor[i], dif)
}
Expand Down Expand Up @@ -47,7 +46,7 @@ func TestPopCode1D(t *testing.T) {

val := pc.Decode(pat)
//fmt.Printf("decode pat for 0.5: %v\n", val)
if math32.Abs(val-0.5) > difTol {
if mat32.Abs(val-0.5) > difTol {
t.Errorf("did not decode properly: val: %v != 0.5", val)
}
}
Expand All @@ -69,11 +68,11 @@ func TestPopCode1DMulti(t *testing.T) {
// fmt.Printf("decode pat for 0.25, 0.75: %v\n", vals)
for _, val := range vals {
if val > 0.5 {
if math32.Abs(val-0.9) > difTolMulti {
if mat32.Abs(val-0.9) > difTolMulti {
t.Errorf("did not decode properly: val: %v != 0.9", val)
}
} else {
if math32.Abs(val-0.1) > difTolMulti {
if mat32.Abs(val-0.1) > difTolMulti {
t.Errorf("did not decode properly: val: %v != 0.1", val)
}
}
Expand Down Expand Up @@ -106,10 +105,10 @@ func TestPopCode2D(t *testing.T) {
if err != nil {
t.Error(err)
}
if math32.Abs(val.X-0.3) > difTol {
if mat32.Abs(val.X-0.3) > difTol {
t.Errorf("did not decode properly: val: %v != 0.3", val)
}
if math32.Abs(val.Y-0.9) > difTol {
if mat32.Abs(val.Y-0.9) > difTol {
t.Errorf("did not decode properly: val: %v != 0.9", val)
}
}
Expand Down Expand Up @@ -140,11 +139,11 @@ func TestPopCode2DMulti(t *testing.T) {
for d := 0; d < 2; d++ {
val := valv.Dim(mat32.Dims(d))
if val > 0.5 {
if math32.Abs(val-0.9) > difTolMulti {
if mat32.Abs(val-0.9) > difTolMulti {
t.Errorf("did not decode properly: val: %v != 0.9", val)
}
} else {
if math32.Abs(val-0.1) > difTolMulti {
if mat32.Abs(val-0.1) > difTolMulti {
t.Errorf("did not decode properly: val: %v != 0.1", val)
}
}
Expand Down Expand Up @@ -176,7 +175,7 @@ func TestRing(t *testing.T) {

val := pc.Decode(pat)
// fmt.Printf("decode pat for 180: %v\n", val)
if math32.Abs(val-180) > difTolWeak {
if mat32.Abs(val-180) > difTolWeak {
t.Errorf("did not decode properly: val: %v != 180", val)
}

Expand All @@ -189,7 +188,7 @@ func TestRing(t *testing.T) {

val = pc.Decode(pat)
// fmt.Printf("decode pat for 330: %v\n", val)
if math32.Abs(val-330) > difTolWeak {
if mat32.Abs(val-330) > difTolWeak {
t.Errorf("did not decode properly: val: %v != 330", val)
}

Expand All @@ -202,7 +201,7 @@ func TestRing(t *testing.T) {

val = pc.Decode(pat)
// fmt.Printf("decode pat for 30: %v\n", val)
if math32.Abs(val-30) > difTolWeak {
if mat32.Abs(val-30) > difTolWeak {
t.Errorf("did not decode properly: val: %v != 30", val)
}
}
7 changes: 3 additions & 4 deletions popcode/ring.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package popcode

import (
"github.com/chewxy/math32"
"github.com/goki/mat32"
)

Expand Down Expand Up @@ -75,9 +74,9 @@ func (pc *Ring) EncodeImpl(pat *[]float32, val float32, n int) {
switch pc.Code {
case GaussBump:
dist := gnrm * (trg - val)
act = math32.Exp(-(dist * dist))
act = mat32.Exp(-(dist * dist))
case Localist:
dist := math32.Abs(trg - val)
dist := mat32.Abs(trg - val)
if dist > incr {
act = 0
} else {
Expand Down Expand Up @@ -166,7 +165,7 @@ func (pc *Ring) Decode(pat []float32) float32 {
sum += act
}
}
sum = math32.Max(sum, pc.MinSum)
sum = mat32.Max(sum, pc.MinSum)
avg /= sum
return avg
}
Expand Down
8 changes: 4 additions & 4 deletions prjn/edge.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

package prjn

import "github.com/chewxy/math32"
import "github.com/goki/mat32"

// Edge returns coordinate value based on either wrapping or clipping at the edge
// and if not wrapping, if it should be clipped (ignored)
Expand All @@ -28,11 +28,11 @@ func Edge(ci, max int, wrap bool) (int, bool) {
// i.e., if going out beyond max is closer, then returns that coordinate
// else if going below 0 is closer than not, then returns that coord
func WrapMinDist(ci, max, ctr float32) float32 {
nwd := math32.Abs(ci - ctr) // no-wrap dist
if math32.Abs((ci+max)-ctr) < nwd {
nwd := mat32.Abs(ci - ctr) // no-wrap dist
if mat32.Abs((ci+max)-ctr) < nwd {
return ci + max
}
if math32.Abs((ci-max)-ctr) < nwd {
if mat32.Abs((ci-max)-ctr) < nwd {
return ci - max
}
return ci
Expand Down
Loading

0 comments on commit e86310e

Please sign in to comment.