From fadcd6dd7f7044e07e35c3f3ecb4a6271df977c0 Mon Sep 17 00:00:00 2001 From: kortschak Date: Fri, 27 Dec 2013 19:40:43 +1030 Subject: [PATCH] Avoid confusion with the name realloc Fixes issue 1. --- mat64/dense.go | 18 +++++++++--------- mat64/matrix.go | 4 +++- mat64/vec.go | 2 +- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/mat64/dense.go b/mat64/dense.go index 9511b5da..d0d7621a 100644 --- a/mat64/dense.go +++ b/mat64/dense.go @@ -344,7 +344,7 @@ func (m *Dense) Add(a, b Matrix) { Rows: ar, Cols: ac, Stride: ac, - Data: realloc(m.mat.Data, ar*ac), + Data: use(m.mat.Data, ar*ac), } } else if ar != m.mat.Rows || ac != m.mat.Cols { panic(ErrShape) @@ -398,7 +398,7 @@ func (m *Dense) Sub(a, b Matrix) { Rows: ar, Cols: ac, Stride: ac, - Data: realloc(m.mat.Data, ar*ac), + Data: use(m.mat.Data, ar*ac), } } else if ar != m.mat.Rows || ac != m.mat.Cols { panic(ErrShape) @@ -452,7 +452,7 @@ func (m *Dense) MulElem(a, b Matrix) { Rows: ar, Cols: ac, Stride: ac, - Data: realloc(m.mat.Data, ar*ac), + Data: use(m.mat.Data, ar*ac), } } else if ar != m.mat.Rows || ac != m.mat.Cols { panic(ErrShape) @@ -551,7 +551,7 @@ func (m *Dense) Mul(a, b Matrix) { Rows: ar, Cols: bc, Stride: bc, - Data: realloc(w.mat.Data, ar*bc), + Data: use(w.mat.Data, ar*bc), } } else if ar != w.mat.Rows || bc != w.mat.Cols { panic(ErrShape) @@ -619,7 +619,7 @@ func (m *Dense) Scale(f float64, a Matrix) { Rows: ar, Cols: ac, Stride: ac, - Data: realloc(m.mat.Data, ar*ac), + Data: use(m.mat.Data, ar*ac), } } else if ar != m.mat.Rows || ac != m.mat.Cols { panic(ErrShape) @@ -662,7 +662,7 @@ func (m *Dense) Apply(f ApplyFunc, a Matrix) { Rows: ar, Cols: ac, Stride: ac, - Data: realloc(m.mat.Data, ar*ac), + Data: use(m.mat.Data, ar*ac), } } else if ar != m.mat.Rows || ac != m.mat.Cols { panic(ErrShape) @@ -719,7 +719,7 @@ func (m *Dense) U(a Matrix) { Rows: ar, Cols: ac, Stride: ac, - Data: realloc(m.mat.Data, ar*ac), + Data: use(m.mat.Data, ar*ac), } case ar != m.mat.Rows || ac != m.mat.Cols: panic(ErrShape) @@ -775,7 +775,7 @@ func (m *Dense) L(a Matrix) { Rows: ar, Cols: ac, Stride: ac, - Data: realloc(m.mat.Data, ar*ac), + Data: use(m.mat.Data, ar*ac), } case ar != m.mat.Rows || ac != m.mat.Cols: panic(ErrShape) @@ -826,7 +826,7 @@ func (m *Dense) TCopy(a Matrix) { Order: BlasOrder, Rows: ac, Cols: ar, - Data: realloc(w.mat.Data, ar*ac), + Data: use(w.mat.Data, ar*ac), } w.mat.Stride = ar } else if ar != m.mat.Cols || ac != m.mat.Rows { diff --git a/mat64/matrix.go b/mat64/matrix.go index 879a239c..2e6b5df2 100644 --- a/mat64/matrix.go +++ b/mat64/matrix.go @@ -381,7 +381,9 @@ func max(a, b int) int { return b } -func realloc(f []float64, l int) []float64 { +// use returns a float64 slice with l elements, using f if it +// has the necessary capacity, otherwise creating a new slice. +func use(f []float64, l int) []float64 { if l < cap(f) { return f[:l] } diff --git a/mat64/vec.go b/mat64/vec.go index ae2b6951..f235d293 100644 --- a/mat64/vec.go +++ b/mat64/vec.go @@ -71,7 +71,7 @@ func (m *Vec) Mul(a, b Matrix) { w = *m } if len(w) == 0 { - w = realloc(w, ar) + w = use(w, ar) } else if ar != len(w) || bc != 1 { panic(ErrShape) }