Does anyone know if multik has something like np.r...
# mathematics
b
Does anyone know if multik has something like np.roll() for d2Array?
p
Unfortunately, there is no such method in multik
b
Are you aware of any alternatives for kotlin?
Or any articles that would allow me to implement one myself?
p
For any dimension?
b
I'm working with d2Array
p
roll
without specifying an axis, it’s just a shift. You can use `Collections.rotate`:
Copy code
fun D2Array<Int>.roll(n: Int): D2Array<Int> {
    val data = this.toList().also { Collections.rotate(it, n) }
    return D2Array(MemoryViewIntArray(data.toIntArray()), shape = this.shape.clone(), dim = this.dim)
}
or use
sliceArray
Copy code
fun D2Array<Int>.roll(n: Int): D2Array<Int> {
    val newData = this.data.getIntArray().let { it.sliceArray(size - n until size) + it.sliceArray(0 until size - n) }
    return D2Array(MemoryViewIntArray(newData), shape = this.shape.clone(), dim = this.dim)
}
b
I do need an axis though 😄 Here's what I'm trying to do
Copy code
img = np.roll(img, dy, axis=0)
 img = np.roll(img, dx, axis=1)
img is a 2d array
p
with axes are more complicated There are no functions from kotlin for this, here need to work with
strides
b
Might sound a bit dense, but what do axes mean in 2darray context here?
p
Same as in numpy, axis(1) by columns or axis(0) by rows
b
🤦‍♂️ so basically dimensions. Thought so...
a
In KMathe it is planed to do that with virtual buffers. But manipulating strides is a more performant solution. I wonder if roll have a lot of uses outside numpy paradigm (I meant when you can't work with elements in a cycle). @Big Chungus could you explain the problem you are solving.
b
Not sure I can. I'm basically getting my feet wet with OpenCV and image transformations. Here's a PY algorithm I'm trying to reproduce to shift image https://stackoverflow.com/a/53140617
Also kmath seems massive, but the missing docs makes it extremey hard to get started (I've been trying for the entire day today 😄 )
a
Yeah, a lot of holes in it and too many experiments so far. For simple 2d manipulation Multik could be better so far.