Initial commit of functions from github.com/btracey/optimize

s/len/cap/

The condition can be condensed into

    if b.invHess == nil || cap(b.invHess.RawMatrix().Data) < dim*dim {
        b.invHess = mat64.NewDense(dim, dim, nil)
    } else {
        b.invHess = mat64.NewDense(dim, dim, b.invHess.RawMatrix().Data[:b.nDim*b.nDim])
    }

(or the conjunction equivalent) unless you want to save the second call to RawMatrix(), which you are not doing here.