Big Chungus
12/01/2021, 10:48 AMBig Chungus
12/01/2021, 10:48 AMdef shift_img(img, dx, dy):
img = np.roll(img, dy, axis=0)
img = np.roll(img, dx, axis=1)
if dy>0:
img[:dy, :] = 0
elif dy<0:
img[dy:, :] = 0
if dx>0:
img[:, :dx] = 0
elif dx<0:
img[:, dx:] = 0
return img
Big Chungus
12/01/2021, 10:48 AMdef halftone():
# for every bandLength rows darken to 10-30% brightness,
# then don't touch for bandGap rows.
bandLength, bandGap = 2, 3
for y in range(holo.shape[0]):
if y % (bandLength+bandGap) < bandLength:
holo[y,:,:] = holo[y,:,:] * np.random.uniform(0.1, 0.3)
Big Chungus
12/01/2021, 10:48 AMSerVB
12/01/2021, 12:36 PMgeneral case:
x[y:z:s] # python
x[y until z step s] # kotlin
without step:
x[y:z] # python
x[y until z] # kotlin
without some element:
x[:z] # python
x[null until z] # kotlin
But need of course define `operator get/set(PyRange)`for the type and operator rangeTo
for nullable ints.
And regarding multiple dimensions, Kotlin supports commas in brackets too: https://kotlinlang.org/docs/operator-overloading.html#indexed-access-operator
So if we define such methods, we can easily write img[:dy, :]
as img[null until dy, null until null]
in Kotlin.
Of course something else can be used instead of nulls and untils, but the idea is that it looks implementable in KotlinSerVB
12/01/2021, 12:37 PMBig Chungus
12/01/2021, 1:10 PMBig Chungus
12/01/2021, 1:12 PMSerVB
12/01/2021, 2:16 PMfun shift_img(img: Tensor, dx: Int, dy: Int): Tensor {
var img = img
img = np.roll(img, dy, axis=0)
img = np.roll(img, dx, axis=1)
if (dy>0) {
img[null until dy, null until null] = 0
} else if (dy<0) {
img[dy until null, null until] = 0
}
if (dx>0) {
img[null until null, null until dx] = 0
} else if (dx<0) {
img[null until null, dx until null] = 0
}
return img
}
SerVB
12/01/2021, 2:19 PMBig Chungus
12/01/2021, 2:24 PMimg[x until y]
accessorBig Chungus
12/01/2021, 2:25 PMimg[:,:,:] = other[:,:,:]
kind of syntax)SerVB
12/01/2021, 2:39 PMAlso kotlin/jvm does not support this img[x until y] accessorWhy not?
x until y
simply returns PyRange
object (if you define operator fun Int?.rangeTo(end: Int?) = PyRange(this, end)
). And img[x until y]
should be accessible if you define operator fun Tensor.get(range: PyRange) = ...
Big Chungus
12/01/2021, 2:43 PMIt still uses list slice operator assignment that I'm unable to graspMy problem here is mainly seeing how the code would look like without those operators so I could (hopefully) understand them.
SerVB
12/01/2021, 2:53 PMBig Chungus
12/01/2021, 3:00 PMBig Chungus
12/01/2021, 3:07 PMarr[:]
syntax is called slicing, so I can goole it. Do you know how arr[:] = other[:]
syntax is called so I could google that too?SerVB
12/01/2021, 3:20 PMarr[:] = other[:]
, I don't know a term... In terms of AST, it's just an assignment of two things created via slicing. But I'm not sure how it's called in real lifeBig Chungus
12/01/2021, 3:26 PMBig Chungus
12/01/2021, 4:15 PMarr[:] = other[:]
, the processing is as follows:
1. Create a view of slice viewImg = arr[:]
that maps to relevant elements in original arr
matrix.
2. Create a view of viewOther = other[:]
that maps to relevant elements in original other
matrix.
3. Loop through elements of viewOther
and assign them to viewImg
. Errors if the shapes differ between the two.
4. viewImg
assignments are translated to reassignments of each element in its original place in img
Big Chungus
12/01/2021, 4:17 PM