Split NewVector into two functions

21 views
Skip to first unread message

Matthew Rankin

unread,
Mar 8, 2017, 9:44:47 AM3/8/17
to gonum-dev
Forgive me if this is a naive proposal. Currently the NewVector(n int, data []float64) *Vector function handles two cases—creating a vector with nil data and creating a vector the same size as the data provide with the size provide explicitly and implicitly in the length of the data slice. Should the NewVector function be split into two functions—NewVector(data []float64) *Vector and NewEmptyVector(n int) *Vector? This would allow the panic to be avoided/eliminated and simplify each function signature to a single argument.

func NewVector(data []float64) *Vector { return &Vector{ mat: blas64.Vector{ Inc: 1, Data: data, }, n: len(data), } } func NewEmptyVector(n int) *Vector { data = make([]float64, n) return &Vector{ mat: blas64.Vector{ Inc: 1, Data: data, }, n: n, } }


Brendan Tracey

unread,
Mar 8, 2017, 11:04:44 AM3/8/17
to gonum-dev
This makes the individual signatures better (at the cost of more functions), but if we do this, we should also do this for `NewDense`, etc. The `NewDense` function can't have only the data argument, because it needs to have the row and column data. This gives

`func NewDense(r, c int, v []float64) *Dense {}`

`func NewEmptyDense(r, c int) *Dense{}`

This doesn't seem to have any real improvement.

Dan Kortschak

unread,
Mar 8, 2017, 4:29:15 PM3/8/17
to Matthew Rankin, gonum-dev
As Brendan has already said, this is not possible in the general case
(it would be if n-dim slices were in the language).

The other side of it is that it's not actually necessary in the
majority of cases; for the most part the accesses to a vector or matrix
are via allocating methods (pretty much everything other than At and
Set), so a zero value is already usable.
Reply all
Reply to author
Forward
0 new messages