Skip to content

Commit 0300428

Browse files
committed
rename Dims()->SystemDims()
1 parent feed00f commit 0300428

File tree

16 files changed

+110
-108
lines changed

16 files changed

+110
-108
lines changed

filter.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import (
66

77
// Filter is a dynamical system filter.
88
type Filter interface {
9-
// Predict returns the expected change in internal state
9+
// Predict returns a prediction of which will be
10+
// next internal state
1011
Predict(x, u mat.Vector) (Estimate, error)
1112
// Update returns estimated system state based on external measurement ym.
1213
Update(x, u, ym mat.Vector) (Estimate, error)
@@ -32,15 +33,15 @@ type Model interface {
3233
Propagator
3334
// Observer is system observer
3435
Observer
35-
// Dims returns the dimension of state vector, input vector,
36+
// SystemDims returns the dimension of state vector, input vector,
3637
// output (measurements, written as y) vector and disturbance vector (only dynamical systems).
37-
// Below are dimension of matrices as returned by Dims() (row,column)
38-
// nx, nx = A.Dims()
39-
// nx, nu = B.Dims()
40-
// ny, nx = C.Dims()
41-
// ny, nu = D.Dims()
42-
// nx, nz = E.Dims()
43-
Dims() (nx, nu, ny, nz int)
38+
// Below are dimension of matrices as returned by SystemDims() (row,column)
39+
// nx, nx = A.SystemDims()
40+
// nx, nu = B.SystemDims()
41+
// ny, nx = C.SystemDims()
42+
// ny, nu = D.SystemDims()
43+
// nx, nz = E.SystemDims()
44+
SystemDims() (nx, nu, ny, nz int)
4445
}
4546

4647
// Smoother is a filter smoother
@@ -55,13 +56,13 @@ type DiscreteModel interface {
5556
// Model is a model of a dynamical system
5657
Model
5758
// SystemMatrix returns state propagation matrix
58-
SystemMatrix() (A mat.Matrix) // TODO rename to SystemMatrix
59+
SystemMatrix() (A mat.Matrix)
5960
// ControlMatrix returns state propagation control matrix
60-
ControlMatrix() (B mat.Matrix) // TODO rename to ControlMatrix
61+
ControlMatrix() (B mat.Matrix)
6162
// OutputMatrix returns observation matrix
6263
OutputMatrix() (C mat.Matrix)
6364
// FeedForwardMatrix returns observation control matrix
64-
FeedForwardMatrix() (D mat.Matrix) // TODO Rename to FeedMatrix/FeedForwardMatrix
65+
FeedForwardMatrix() (D mat.Matrix)
6566
// TODO DisturbanceMatrix
6667
}
6768

kalman/ekf/ekf.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ type EKF struct {
5050
// - invalid state or output noise is given: noise covariance must either be nil or match the model dimensions
5151
func New(m filter.Model, init filter.InitCond, q, r filter.Noise) (*EKF, error) {
5252
// size of the input and output vectors
53-
nx, _, ny, _ := m.Dims()
53+
nx, _, ny, _ := m.SystemDims()
5454
if nx <= 0 || ny <= 0 {
5555
return nil, fmt.Errorf("invalid model dimensions: [%d x %d]", nx, ny)
5656
}
@@ -174,7 +174,7 @@ func (k *EKF) Predict(x, u mat.Vector) (filter.Estimate, error) {
174174
// Update corrects state x using the measurement z, given control intput u and returns corrected estimate.
175175
// It returns error if either invalid state was supplied or if it fails to calculate system output estimate.
176176
func (k *EKF) Update(x, u, z mat.Vector) (filter.Estimate, error) {
177-
nx, _, ny, _ := k.m.Dims()
177+
nx, _, ny, _ := k.m.SystemDims()
178178

179179
if z.Len() != ny {
180180
return nil, fmt.Errorf("invalid measurement supplied: %v", z)

kalman/ekf/ekf_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ type invalidModel struct {
1515
filter.Model
1616
}
1717

18-
func (m *invalidModel) Dims() (nx, nu, ny, nz int) {
19-
return -10, 0, 8, 0
18+
func (m *invalidModel) SystemDims() (nx, nu, ny, nz int) {
19+
return -10, 0, 8, 0 // a system may have 0 inputs, this is not "invalid". Negative dimension is invalid
2020
}
2121

2222
var (

kalman/ekf/iekf.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ func NewIter(m filter.Model, ic filter.InitCond, q, r filter.Noise, n int) (*IEK
4949
// Update corrects state x using the measurement z, given control intput u and returns corrected estimate of x.
5050
// It returns error if either invalid state was supplied or if it fails to calculate system output estimate.
5151
func (k *IEKF) Update(x, u, z mat.Vector) (filter.Estimate, error) {
52-
_nx, _, _ny, _ := k.m.Dims()
52+
nx, _, ny, _ := k.m.SystemDims()
5353

54-
if z.Len() != _ny {
54+
if z.Len() != ny {
5555
return nil, fmt.Errorf("invalid measurement supplied: %v", z)
5656
}
5757

@@ -61,8 +61,8 @@ func (k *IEKF) Update(x, u, z mat.Vector) (filter.Estimate, error) {
6161
return nil, fmt.Errorf("failed to observe system output: %v", err)
6262
}
6363

64-
pxy := mat.NewDense(_nx, _ny, nil)
65-
pyy := mat.NewDense(_ny, _ny, nil)
64+
pxy := mat.NewDense(nx, ny, nil)
65+
pyy := mat.NewDense(ny, ny, nil)
6666

6767
// innovation vector
6868
inn := &mat.VecDense{}
@@ -139,8 +139,8 @@ func (k *IEKF) Update(x, u, z mat.Vector) (filter.Estimate, error) {
139139
k.inn.CopyVec(inn)
140140
k.k.Copy(gain)
141141
// update EKF covariance matrix
142-
for i := 0; i < _nx; i++ {
143-
for j := i; j < _nx; j++ {
142+
for i := 0; i < nx; i++ {
143+
for j := i; j < nx; j++ {
144144
k.p.SetSym(i, j, pCorr.At(i, j))
145145
}
146146
}

kalman/kf/kf.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -39,47 +39,47 @@ type KF struct {
3939
// - invalid state or output noise is given: noise covariance must either be nil or match the model dimensions
4040
func New(m filter.DiscreteModel, init filter.InitCond, z, wn filter.Noise) (*KF, error) {
4141
// size of the input and output vectors
42-
_nx, _, _ny, _ := m.Dims()
43-
if _nx <= 0 || _ny <= 0 {
44-
return nil, fmt.Errorf("invalid model dimensions: [%d x %d]", _nx, _ny)
42+
nx, _, ny, _ := m.SystemDims()
43+
if nx <= 0 || ny <= 0 {
44+
return nil, fmt.Errorf("invalid model dimensions: [%d x %d]", nx, ny)
4545
}
4646

4747
if z != nil {
48-
if z.Cov().Symmetric() != _nx {
49-
return nil, fmt.Errorf("invalid state noise dimension: %d != %d", z.Cov().Symmetric(), _ny)
48+
if z.Cov().Symmetric() != nx {
49+
return nil, fmt.Errorf("invalid state noise dimension: %d != %d", z.Cov().Symmetric(), ny)
5050
}
5151
} else {
5252
z, _ = noise.NewNone()
5353
}
5454

5555
if wn != nil {
56-
if wn.Cov().Symmetric() != _ny {
56+
if wn.Cov().Symmetric() != ny {
5757
return nil, fmt.Errorf("invalid output noise dimension: %d", wn.Cov().Symmetric())
5858
}
5959
} else {
6060
wn, _ = noise.NewNone()
6161
}
6262

6363
rows, cols := m.SystemMatrix().Dims()
64-
if rows != _nx || cols != _nx {
64+
if rows != nx || cols != nx {
6565
return nil, fmt.Errorf("invalid propagation matrix dimensions: [%d x %d]", rows, cols)
6666
}
6767

6868
if m.ControlMatrix() != nil && !m.ControlMatrix().(*mat.Dense).IsEmpty() {
6969
rows, cols := m.ControlMatrix().Dims()
70-
if rows != _nx {
70+
if rows != nx {
7171
return nil, fmt.Errorf("invalid ctl propagation matrix dimensions: [%d x %d]", rows, cols)
7272
}
7373
}
7474

7575
rows, cols = m.OutputMatrix().Dims()
76-
if rows != _ny || cols != _nx {
76+
if rows != ny || cols != nx {
7777
return nil, fmt.Errorf("invalid observation matrix dimensions: [%d x %d]", rows, cols)
7878
}
7979

8080
if m.FeedForwardMatrix() != nil && !m.FeedForwardMatrix().(*mat.Dense).IsEmpty() {
8181
rows, cols = m.FeedForwardMatrix().Dims()
82-
if rows != _ny {
82+
if rows != ny {
8383
return nil, fmt.Errorf("invalid ctl observation matrix dimensions: [%d x %d]", rows, cols)
8484
}
8585
}
@@ -92,10 +92,10 @@ func New(m filter.DiscreteModel, init filter.InitCond, z, wn filter.Noise) (*KF,
9292
pNext := mat.NewSymDense(init.Cov().Symmetric(), nil)
9393

9494
// innovation vector
95-
inn := mat.NewVecDense(_ny, nil)
95+
inn := mat.NewVecDense(ny, nil)
9696

9797
// kalman gain
98-
k := mat.NewDense(_nx, _ny, nil)
98+
k := mat.NewDense(nx, ny, nil)
9999

100100
return &KF{
101101
m: m,
@@ -140,9 +140,9 @@ func (k *KF) Predict(x, u mat.Vector) (filter.Estimate, error) {
140140
// Update corrects state x using the measurement z, given control intput u and returns corrected estimate.
141141
// It returns error if either invalid state was supplied or if it fails to calculate system output estimate.
142142
func (k *KF) Update(x, u, ym mat.Vector) (filter.Estimate, error) {
143-
_nx, _, _ny, _ := k.m.Dims()
143+
nx, _, ny, _ := k.m.SystemDims()
144144

145-
if ym.Len() != _ny {
145+
if ym.Len() != ny {
146146
return nil, fmt.Errorf("invalid measurement supplied: %v", ym)
147147
}
148148

@@ -152,8 +152,8 @@ func (k *KF) Update(x, u, ym mat.Vector) (filter.Estimate, error) {
152152
return nil, fmt.Errorf("failed to observe system output: %v", err)
153153
}
154154

155-
pxy := mat.NewDense(_nx, _ny, nil)
156-
pyy := mat.NewDense(_ny, _ny, nil)
155+
pxy := mat.NewDense(nx, ny, nil)
156+
pyy := mat.NewDense(ny, ny, nil)
157157

158158
// P*H'
159159
pxy.Mul(k.pNext, k.m.OutputMatrix().T())
@@ -217,8 +217,8 @@ func (k *KF) Update(x, u, ym mat.Vector) (filter.Estimate, error) {
217217
k.inn.CopyVec(inn)
218218
k.k.Copy(gain)
219219
// update KF covariance matrix
220-
for i := 0; i < _nx; i++ {
221-
for j := i; j < _nx; j++ {
220+
for i := 0; i < nx; i++ {
221+
for j := i; j < nx; j++ {
222222
k.p.SetSym(i, j, pCorr.At(i, j))
223223
}
224224
}

kalman/kf/kf_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@ import (
1313

1414
type invalidModel struct {
1515
filter.DiscreteModel
16-
r int
17-
c int
16+
nx int
17+
nu int
18+
ny int
1819
}
1920

20-
func (m *invalidModel) Dims() (nx, nu, ny, nz int) {
21-
return m.r, 0, m.c, 0
21+
func (m *invalidModel) SystemDims() (nx, nu, ny, nz int) {
22+
return m.nx, m.nu, m.ny, 0
2223
}
2324

2425
var (
@@ -50,7 +51,7 @@ func setup() {
5051
D := mat.NewDense(1, 1, []float64{0.0})
5152

5253
okModel = &sim.BaseModel{A: A, B: B, C: C, D: D}
53-
badModel = &invalidModel{DiscreteModel: okModel, r: 10, c: 10}
54+
badModel = &invalidModel{DiscreteModel: okModel, nx: 10, ny: 10}
5455
}
5556

5657
func TestMain(m *testing.M) {
@@ -70,7 +71,7 @@ func TestKFNew(t *testing.T) {
7071
assert.NotNil(f)
7172

7273
// invalid model: negative dimensions
73-
badModel.r, badModel.c = -10, 20
74+
badModel.nx, badModel.ny = -10, 20
7475
f, err = New(badModel, ic, q, r)
7576
assert.Nil(f)
7677
assert.Error(err)

0 commit comments

Comments
 (0)