Skip to content

Iteration is not usable #260

@rotu

Description

@rotu

I'm trying to loop over an NDArray coordinates, and it seems like iteration needs an overhaul.

  • NDArray is not iterable (so for (x of my_ndarray) and Array.from(myNdArray) throw errors)
  • NDIter's implementation of the iterable protocol seems to iterate over only the linearized indices (i.e. Array.from(new NDIter(my_ndarray)) gives [0,1,...], even if the ndarray is multidimensional)
  • NDIter has properties that are length 32 (V_MAXDIMS) even when the array it's iterating over has fewer dimensions.
  • NDIter has extraneous properties which obscure how to use it.
  • NDIter's string representation uses the name f (I think because it is derived from minified code) and is excessively verbose.
import {
  NDIter, range,
} from "./dist"

const myNdArray = range(100, 1, 130).reshape(2, 3, 5)
const myIter = new NDIter(x)
for (let i = 0; i < 17; ++i){ myIter.next() }
console.log(myIter)

prints:

f {
  x: array([
    [
      [ 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 ]
    ]
  ], dtype=float64),
  shape: [
    2, 3, 5, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0
  ],
  shapem1: [
    1, 2, 4, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0
  ],
  strides: [
    15, 5, 1, 0, 0, 0, 0, 0, 0,
     0, 0, 0, 0, 0, 0, 0, 0, 0,
     0, 0, 0, 0, 0, 0, 0, 0, 0,
     0, 0, 0, 0, 0
  ],
  backstrides: [
    15, 10, 4, 0, 0, 0, 0, 0, 0,
     0,  0, 0, 0, 0, 0, 0, 0, 0,
     0,  0, 0, 0, 0, 0, 0, 0, 0,
     0,  0, 0, 0, 0
  ],
  length: 30,
  lengthm1: 29,
  nd: 3,
  ndm1: 2,
  index: 17,
  coords: [
    1, 0, 2, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0
  ],
  pos: 17,
  factors: [
    15, 5, 1, 0, 0, 0, 0, 0, 0,
     0, 0, 0, 0, 0, 0, 0, 0, 0,
     0, 0, 0, 0, 0, 0, 0, 0, 0,
     0, 0, 0, 0, 0
  ]
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions