The cap fields allow us to resize back to the original size (just the
same as a cap in a Go slice). This is not necessary for any case where
only slicing to a smaller size is needed. The allocation that we do in
making a new *Dense would not be needed if the mat.Matrix returned did
not modify its own dimensions. So a minimal implementation of what we
have would be (untested and with no bounds checks):

import "gonum.org/v1/gonum/blas64"

type general blas64.General

func (g general) Dims() (r, c int)    { return g.Rows, g.Cols }
func (g general) At(i, j int) float64 { return g.Data[i*g.Stride+j] }
func (g general) T() mat.Matrix       { return mat.Transpose{g} }

func (g general) Slice(i, k, j, l int) Matrix {
        // This is on a non-pointer receiver,
        // so the original g is not mutated.
        g.Data = g.Data[i*g.Stride+j : (k-1)*g.Stride+l]
        g.Rows = k - i
        g.Cols = l - j
        return g
}

On Sat, 2017-08-12 at 17:08 -0700, chatrapathi.ak...@gmail.com wrote:
> Hi kortschak, thanks for replying. Please correct me if I'm not
> following 
> this right. So on Line 290 
> <https://github.com/gonum/gonum/blob/620965de6d2b527d48f4f32daec0a38f
> e341da1c/mat/dense.go#L285> of 
> the Dense.Slice() method, the receiver is copied into the variable
> 't' by 
> dereferencing the pointer, then the underlying data slice is sliced
> (easy 
> to slice rows vs cols) with the method you have used. I do not
> understand 
> what is happening with the capRows and capCols. Seems to me that
> there are 
> two types of dimensions being maintained here. One for the
> underlying 
> General datatype and another for the Dense type. It would be great if
> you 
> could help me understand this so that I may be able to adapt it to
> my 
> <https://play.golang.org/p/TQMdNLBbCi>implementation linked to in
> the 
> question. Here <https://play.golang.org/p/vTvlYpWL40> is my attempt
> at 
> adapting the Slice method you pointed. I'm not getting the right
> answer, 
> meaning I'm missing something. Could you please point it out?

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to