https://kotlinlang.org logo
#mathematics
Title
# mathematics
b

Big Chungus

06/07/2022, 4:42 PM
Does anyone know if multik has something like np.roll() for d2Array?
p

Pavel Gorgulov

06/07/2022, 5:41 PM
Unfortunately, there is no such method in multik
b

Big Chungus

06/07/2022, 5:42 PM
Are you aware of any alternatives for kotlin?
Or any articles that would allow me to implement one myself?
p

Pavel Gorgulov

06/07/2022, 5:43 PM
For any dimension?
b

Big Chungus

06/07/2022, 5:43 PM
I'm working with d2Array
p

Pavel Gorgulov

06/07/2022, 6:16 PM
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

Big Chungus

06/07/2022, 6:17 PM
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

Pavel Gorgulov

06/07/2022, 6:20 PM
with axes are more complicated There are no functions from kotlin for this, here need to work with
strides
b

Big Chungus

06/07/2022, 6:25 PM
Might sound a bit dense, but what do axes mean in 2darray context here?
p

Pavel Gorgulov

06/07/2022, 6:36 PM
Same as in numpy, axis(1) by columns or axis(0) by rows
b

Big Chungus

06/07/2022, 6:36 PM
🤦‍♂️ so basically dimensions. Thought so...
a

altavir

06/07/2022, 8:11 PM
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

Big Chungus

06/07/2022, 8:20 PM
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

altavir

06/08/2022, 5:47 AM
Yeah, a lot of holes in it and too many experiments so far. For simple 2d manipulation Multik could be better so far.
41 Views