1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
|
package tensor
import (
"reflect"
"sort"
"github.com/pkg/errors"
)
var (
_ Sparse = &CS{}
)
// Sparse is a sparse tensor.
type Sparse interface {
Tensor
Densor
NonZeroes() int // NonZeroes returns the number of nonzero values
}
// coo is an internal representation of the Coordinate type sparse matrix.
// It's not exported because you probably shouldn't be using it.
// Instead, constructors for the *CS type supports using a coordinate as an input.
type coo struct {
o DataOrder
xs, ys []int
data array
}
func (c *coo) Len() int { return c.data.Len() }
func (c *coo) Less(i, j int) bool {
if c.o.IsColMajor() {
return c.colMajorLess(i, j)
}
return c.rowMajorLess(i, j)
}
func (c *coo) Swap(i, j int) {
c.xs[i], c.xs[j] = c.xs[j], c.xs[i]
c.ys[i], c.ys[j] = c.ys[j], c.ys[i]
c.data.swap(i, j)
}
func (c *coo) colMajorLess(i, j int) bool {
if c.ys[i] < c.ys[j] {
return true
}
if c.ys[i] == c.ys[j] {
// check xs
if c.xs[i] <= c.xs[j] {
return true
}
}
return false
}
func (c *coo) rowMajorLess(i, j int) bool {
if c.xs[i] < c.xs[j] {
return true
}
if c.xs[i] == c.xs[j] {
// check ys
if c.ys[i] <= c.ys[j] {
return true
}
}
return false
}
// CS is a compressed sparse data structure. It can be used to represent both CSC and CSR sparse matrices.
// Refer to the individual creation functions for more information.
type CS struct {
s Shape
o DataOrder
e Engine
f MemoryFlag
z interface{} // z is the "zero" value. Typically it's not used.
indices []int
indptr []int
array
}
// NewCSR creates a new Compressed Sparse Row matrix. The data has to be a slice or it panics.
func NewCSR(indices, indptr []int, data interface{}, opts ...ConsOpt) *CS {
t := new(CS)
t.indices = indices
t.indptr = indptr
t.array = arrayFromSlice(data)
t.o = NonContiguous
t.e = StdEng{}
for _, opt := range opts {
opt(t)
}
return t
}
// NewCSC creates a new Compressed Sparse Column matrix. The data has to be a slice, or it panics.
func NewCSC(indices, indptr []int, data interface{}, opts ...ConsOpt) *CS {
t := new(CS)
t.indices = indices
t.indptr = indptr
t.array = arrayFromSlice(data)
t.o = MakeDataOrder(ColMajor, NonContiguous)
t.e = StdEng{}
for _, opt := range opts {
opt(t)
}
return t
}
// CSRFromCoord creates a new Compressed Sparse Row matrix given the coordinates. The data has to be a slice or it panics.
func CSRFromCoord(shape Shape, xs, ys []int, data interface{}) *CS {
t := new(CS)
t.s = shape
t.o = NonContiguous
t.array = arrayFromSlice(data)
t.e = StdEng{}
// coord matrix
cm := &coo{t.o, xs, ys, t.array}
sort.Sort(cm)
r := shape[0]
c := shape[1]
if r <= cm.xs[len(cm.xs)-1] || c <= MaxInts(cm.ys...) {
panic("Cannot create sparse matrix where provided shape is smaller than the implied shape of the data")
}
indptr := make([]int, r+1)
var i, j, tmp int
for i = 1; i < r+1; i++ {
for j = tmp; j < len(xs) && xs[j] < i; j++ {
}
tmp = j
indptr[i] = j
}
t.indices = ys
t.indptr = indptr
return t
}
// CSRFromCoord creates a new Compressed Sparse Column matrix given the coordinates. The data has to be a slice or it panics.
func CSCFromCoord(shape Shape, xs, ys []int, data interface{}) *CS {
t := new(CS)
t.s = shape
t.o = MakeDataOrder(NonContiguous, ColMajor)
t.array = arrayFromSlice(data)
t.e = StdEng{}
// coord matrix
cm := &coo{t.o, xs, ys, t.array}
sort.Sort(cm)
r := shape[0]
c := shape[1]
// check shape
if r <= MaxInts(cm.xs...) || c <= cm.ys[len(cm.ys)-1] {
panic("Cannot create sparse matrix where provided shape is smaller than the implied shape of the data")
}
indptr := make([]int, c+1)
var i, j, tmp int
for i = 1; i < c+1; i++ {
for j = tmp; j < len(ys) && ys[j] < i; j++ {
}
tmp = j
indptr[i] = j
}
t.indices = xs
t.indptr = indptr
return t
}
func (t *CS) Shape() Shape { return t.s }
func (t *CS) Strides() []int { return nil }
func (t *CS) Dtype() Dtype { return t.t }
func (t *CS) Dims() int { return 2 }
func (t *CS) Size() int { return t.s.TotalSize() }
func (t *CS) DataSize() int { return t.Len() }
func (t *CS) Engine() Engine { return t.e }
func (t *CS) DataOrder() DataOrder { return t.o }
func (t *CS) Slice(...Slice) (View, error) {
return nil, errors.Errorf("Slice for sparse tensors not implemented yet")
}
func (t *CS) At(coord ...int) (interface{}, error) {
if len(coord) != t.Dims() {
return nil, errors.Errorf("Expected coordinates to be of %d-dimensions. Got %v instead", t.Dims(), coord)
}
if i, ok := t.at(coord...); ok {
return t.Get(i), nil
}
if t.z == nil {
return reflect.Zero(t.t.Type).Interface(), nil
}
return t.z, nil
}
func (t *CS) SetAt(v interface{}, coord ...int) error {
if i, ok := t.at(coord...); ok {
t.Set(i, v)
return nil
}
return errors.Errorf("Cannot set value in a compressed sparse matrix: Coordinate %v not found", coord)
}
func (t *CS) Reshape(...int) error { return errors.New("compressed sparse matrix cannot be reshaped") }
// T transposes the matrix. Concretely, it just changes a bit - the state goes from CSC to CSR, and vice versa.
func (t *CS) T(axes ...int) error {
dims := t.Dims()
if len(axes) != dims && len(axes) != 0 {
return errors.Errorf("Cannot transpose along axes %v", axes)
}
if len(axes) == 0 || axes == nil {
axes = make([]int, dims)
for i := 0; i < dims; i++ {
axes[i] = dims - 1 - i
}
}
UnsafePermute(axes, []int(t.s))
t.o = t.o.toggleColMajor()
t.o = MakeDataOrder(t.o, Transposed)
return errors.Errorf(methodNYI, "T", t)
}
// UT untransposes the CS
func (t *CS) UT() { t.T(); t.o = t.o.clearTransposed() }
// Transpose is a no-op. The data does not move
func (t *CS) Transpose() error { return nil }
func (t *CS) Apply(fn interface{}, opts ...FuncOpt) (Tensor, error) {
return nil, errors.Errorf(methodNYI, "Apply", t)
}
func (t *CS) Eq(other interface{}) bool {
if ot, ok := other.(*CS); ok {
if t == ot {
return true
}
if len(ot.indices) != len(t.indices) {
return false
}
if len(ot.indptr) != len(t.indptr) {
return false
}
if !t.s.Eq(ot.s) {
return false
}
if ot.o != t.o {
return false
}
for i, ind := range t.indices {
if ot.indices[i] != ind {
return false
}
}
for i, ind := range t.indptr {
if ot.indptr[i] != ind {
return false
}
}
return t.array.Eq(&ot.array)
}
return false
}
func (t *CS) Clone() interface{} {
retVal := new(CS)
retVal.s = t.s.Clone()
retVal.o = t.o
retVal.e = t.e
retVal.indices = make([]int, len(t.indices))
retVal.indptr = make([]int, len(t.indptr))
copy(retVal.indices, t.indices)
copy(retVal.indptr, t.indptr)
retVal.array = makeArray(t.t, t.array.Len())
copyArray(&retVal.array, &t.array)
retVal.e = t.e
return retVal
}
func (t *CS) IsScalar() bool { return false }
func (t *CS) ScalarValue() interface{} { panic("Sparse Matrices cannot represent Scalar Values") }
func (t *CS) MemSize() uintptr { return uintptr(calcMemSize(t.t, t.array.Len())) }
func (t *CS) Uintptr() uintptr { return t.array.Uintptr() }
// NonZeroes returns the nonzeroes. In academic literature this is often written as NNZ.
func (t *CS) NonZeroes() int { return t.Len() }
func (t *CS) RequiresIterator() bool { return true }
func (t *CS) Iterator() Iterator { return NewFlatSparseIterator(t) }
func (t *CS) at(coord ...int) (int, bool) {
var r, c int
if t.o.IsColMajor() {
r = coord[1]
c = coord[0]
} else {
r = coord[0]
c = coord[1]
}
for i := t.indptr[r]; i < t.indptr[r+1]; i++ {
if t.indices[i] == c {
return i, true
}
}
return -1, false
}
// Dense creates a Dense tensor from the compressed one.
func (t *CS) Dense() *Dense {
if t.e != nil && t.e != (StdEng{}) {
// use
}
d := recycledDense(t.t, t.Shape().Clone(), WithEngine(t.e))
if t.o.IsColMajor() {
for i := 0; i < len(t.indptr)-1; i++ {
for j := t.indptr[i]; j < t.indptr[i+1]; j++ {
d.SetAt(t.Get(j), t.indices[j], i)
}
}
} else {
for i := 0; i < len(t.indptr)-1; i++ {
for j := t.indptr[i]; j < t.indptr[i+1]; j++ {
d.SetAt(t.Get(j), i, t.indices[j])
}
}
}
return d
}
// Other Accessors
func (t *CS) Indptr() []int {
retVal := BorrowInts(len(t.indptr))
copy(retVal, t.indptr)
return retVal
}
func (t *CS) Indices() []int {
retVal := BorrowInts(len(t.indices))
copy(retVal, t.indices)
return retVal
}
func (t *CS) AsCSR() {
if t.o.IsRowMajor() {
return
}
t.o.toggleColMajor()
}
func (t *CS) AsCSC() {
if t.o.IsColMajor() {
return
}
t.o.toggleColMajor()
}
func (t *CS) IsNativelyAccessible() bool { return t.f.nativelyAccessible() }
func (t *CS) IsManuallyManaged() bool { return t.f.manuallyManaged() }
func (t *CS) arr() array { return t.array }
func (t *CS) arrPtr() *array { return &t.array }
func (t *CS) standardEngine() standardEngine { return nil }
|