github.com/wzzhu/tensor@v0.9.24/dense_mapreduce.go (about)

     1  package tensor
     2  
     3  import "github.com/pkg/errors"
     4  
     5  // Apply applies a function to all the values in the tensor.
     6  func (t *Dense) Apply(fn interface{}, opts ...FuncOpt) (retVal Tensor, err error) {
     7  	var e Engine = t.e
     8  	if e == nil {
     9  		e = StdEng{}
    10  	}
    11  	if m, ok := e.(Mapper); ok {
    12  		return m.Map(fn, t, opts...)
    13  	}
    14  	return nil, errors.Errorf("Execution engine %T for %v not a mapper", e, t)
    15  }
    16  
    17  // Reduce applies a reduction function and reduces the values along the given axis.
    18  func (t *Dense) Reduce(fn interface{}, axis int, defaultValue interface{}) (retVal *Dense, err error) {
    19  	var e Engine = t.e
    20  	if e == nil {
    21  		e = StdEng{}
    22  	}
    23  
    24  	if rd, ok := e.(Reducer); ok {
    25  		var val Tensor
    26  		if val, err = rd.Reduce(fn, t, axis, defaultValue); err != nil {
    27  			err = errors.Wrapf(err, opFail, "Dense.Reduce")
    28  			return
    29  		}
    30  		retVal = val.(*Dense)
    31  		return
    32  	}
    33  	return nil, errors.Errorf("Engine %v is not a Reducer", e)
    34  }