def sliceArr(N): for num in range(1, N): for start in range(-num, num + 1): for end in range(-num, num + 1): for stride in (-3, -2, -1, 1, 2, 3): l = list(range(num)) a = np.array(l, dtype=np.int8) sl = l[start:end:stride] ll = len(sl) try: sa = list(a[start:end:stride]) except IndexError as e: sa = str(e) a[start:end:stride] = np.ones(len(sl)) * -1 return 1
def spatial_filter(img, kernel, dxy=None): """ Convolves `img` with `kernel` assuming a stride of 1. If `img` is linear, `dxy` needs to hold the dimensions of the image. """ # Make sure that the image is 2D _img = np.array(img) if dxy is None: dx, dy = _img.shape() isInt = type(img[0:0]) isFlat = False else: dx, dy = dxy if dx * dy != _img.size(): raise TypeError("Dimensions do not match number of `img` elements") isInt = type(img[0]) isFlat = True img2d = _img.reshape((dx, dy)) # Check if kernel is a square matrix with an odd number of elements per row # and column krn = np.array(kernel) dk, dk2 = krn.shape() if dk != dk2 or dk % 2 == 0 or dk <= 1: raise TypeError( "`kernel` is not a square 2D matrix or does not have an " "odd number of row / column elements") # Make a padded copy of the image; pad with mean of image to reduce edge # effects padd = dk // 2 img2dp = np.ones((dx + padd * 2, dy + padd * 2)) * numerical.mean(img2d) img2dp[padd:dx + padd, padd:dy + padd] = img2d imgRes = np.zeros(img2dp.shape()) # Convolve padded image with kernel for x in range(0, dx): for y in range(0, dy): imgRes[x + padd, y + padd] = numerical.sum( img2dp[x:x + dk, y:y + dk] * krn[:, :]) # Remove padding, flatten and restore value type if needed _img = imgRes[padd:dx + padd, padd:dy + padd] if isFlat: _img = _img.flatten() if isInt: _img = list(np.array(_img, dtype=np.int16)) return _img
def __call__(self, tree): numStrings = len(tree) length = tree.length trailEnd = 2 * self.length probThreshold = 1.0 / numStrings location = np.ones(numStrings, dtype=int) * length # Location of trail colors = [None] * numStrings # Color of trail while True: tree.down() location += 1 for ii, string in enumerate(tree): if location[ii] <= self.length: # Propagate a trail string["TOP"] = colors[ii] elif location[ii] > trailEnd and randFloat() < probThreshold: # Start a trail location[ii] = 0 colors[ii] = randomColor() yield
try: import ulab as np except: import numpy as np for num in range(1, 4): for start in range(-num, num + 1): for end in range(-num, num + 1): for stride in (-3, -2, -1, 1, 2, 3): l = list(range(num)) a = np.array(l, dtype=np.int8) sl = l[start:end:stride] ll = len(sl) try: sa = list(a[start:end:stride]) except IndexError as e: sa = str(e) print("%2d [% d:% d:% d] %-24r %-24r%s" % (num, start, end, stride, sl, sa, " ***" if sa != sl else "")) a[start:end:stride] = np.ones(len(sl)) * -1 print("%2d [% d:% d:% d] %r" % (num, start, end, stride, list(a)))
from ulab import linalg import ulab print(ulab.ones(3)) print(ulab.ones((2,3))) print(ulab.zeros(3)) print(ulab.zeros((2,3))) print(ulab.eye(3)) print(ulab.ones(1, dtype=ulab.int8)) print(ulab.ones(2, dtype=ulab.uint8)) print(ulab.ones(3, dtype=ulab.int16)) print(ulab.ones(4, dtype=ulab.uint16)) print(ulab.ones(5, dtype=ulab.float))
import ulab as np a = np.array([1, 2, 3, 4, 5], dtype=np.uint8) b = np.array([5, 4, 3, 2, 1], dtype=np.float) print(np.minimum(a, b)) print(np.maximum(a, b)) print(np.maximum(1, 5.5)) a = np.array(range(9), dtype=np.uint8) print(np.clip(a, 3, 7)) b = 3 * np.ones(len(a), dtype=np.float) print(np.clip(a, b, 7))
#PYCLOUDIOT : LIBRARY,2,10,compare_library.py, import ulab as np def getMinArrayFrom2(a, b): return np.minimum(a, b) def getMaxArrayFrom2(a, b): return np.maximum(a, b) def MaxMinusMin(max, min): return max - min #PYCLOUDIOT : MAIN,13,17,compare_main.py, #IMPORTS :compare_library.py ;ulab, import ulab as np N = 10 a = np.array(range(N), dtype=np.uint8) b = (N / 2) * np.ones(len(a), dtype=np.float) MaxMinusMin(getMaxArrayFrom2(a, b), getMinArrayFrom2(a, b))
#PYCLOUDIOT : LIBRARY,2,4,operators_library.py, import ulab as np def matrixtimesmatrix(arrayToMultiply): return arrayToMultiply * arrayToMultiply #PYCLOUDIOT : MAIN,7,8,operators_main.py, #IMPORTS :operators_library.py ;ulab, N = 10 a = np.ones(N) matrixtimesmatrix(a)
import ulab from ulab import compare a = ulab.array([1, 2, 3, 4, 5], dtype=ulab.uint8) b = ulab.array([5, 4, 3, 2, 1], dtype=ulab.float) print(compare.minimum(a, b)) print(compare.maximum(a, b)) print(compare.maximum(1, 5.5)) a = ulab.array(range(9), dtype=ulab.uint8) print(compare.clip(a, 3, 7)) b = 3 * ulab.ones(len(a), dtype=ulab.float) print(compare.clip(a, b, 7))
import ulab a = ulab.ones(3) print(a + a) print(a - a) print(a * a) print(a / a) print(a + 2) print(a - 2) print(a * 2) print(a / 2) print(a < 1) print(a < 2) print(a <= 0) print(a <= 1) print(a > 1) print(a > 2) print(a >= 0) print(a >= 1) #print(a==0) # These print just true or false. Is it right? is it a micropython limitation? #print(a==1)
print(np.linspace(0, 10, num=5, endpoint=False, dtype=np.uint8)) print(np.linspace(0, 10, num=5, endpoint=False, dtype=np.uint16)) print(np.linspace(0, 10, num=5, endpoint=False, dtype=np.int8)) print(np.linspace(0, 10, num=5, endpoint=False, dtype=np.int16)) print("Array creation using LOGSPACE:") print(np.logspace(0, 10, num=5)) print(np.logspace(0, 10, num=5, endpoint=False)) print(np.logspace(0, 10, num=5, endpoint=True)) print(np.logspace(0, 10, num=5, endpoint=False, dtype=np.uint8)) print(np.logspace(0, 10, num=5, endpoint=False, dtype=np.uint16)) print(np.logspace(0, 10, num=5, endpoint=False, dtype=np.int8)) print(np.logspace(0, 10, num=5, endpoint=False, dtype=np.int16)) print("Array creation using ZEROS:") print(np.zeros(3)) print(np.zeros((3, 3))) print(np.zeros((3, 3), dtype=np.uint8)) print(np.zeros((3, 3), dtype=np.uint16)) print(np.zeros((3, 3), dtype=np.int8)) print(np.zeros((3, 3), dtype=np.int16)) print(np.zeros((4, 3), dtype=np.float)) print(np.zeros((3, 4), dtype=np.float)) print("Array creation using ONES:") print(np.ones(3)) print(np.ones((3, 3))) print(np.ones((3, 3), dtype=np.uint8)) print(np.ones((3, 3), dtype=np.uint16)) print(np.ones((3, 3), dtype=np.int8)) print(np.ones((3, 3), dtype=np.int16)) print(np.ones((4, 3), dtype=np.float)) print(np.ones((3, 4), dtype=np.float))
def mandelbrot_ulab(WIDTH, HEIGHT, N=256, group=None, CENTER=(-0.5, 0), DIAMX=2.3): """ mandelbrot() return a group with drawing of mandelbrot fractal in it. Parameters : WIDTH : width of the bitmap HEIGHT : height of the bitmap N : number of iterations, and number of colors (default 256) group : reuse a group instead of creating a new one CENTER : tuple containing the center coordinates DIAMX : "zoom" level, from 2.3 to 0.000027 """ DIAMY = DIAMX*HEIGHT/WIDTH XMIN=CENTER[0]-DIAMX/2 XMAX=CENTER[0]+DIAMX/2 YMIN=CENTER[1]-DIAMY/2 YMAX=CENTER[1]+DIAMY/2 r1 = ulab.linspace(XMIN, XMAX, num=WIDTH) r2 = ulab.linspace(YMIN, YMAX, num=HEIGHT) if group is None: group = displayio.Group() elif group: group.pop() bitmap = displayio.Bitmap(WIDTH, HEIGHT, N) def ColorMap(p): sr = sg = sb = 0 if (p < 64): sr=0 ; sg=p*4 ; sb=255 elif (p < 128): sr=(p-64)*4 ; sg=255 ; sb=(255-(p-64)*4) elif (p < 192): sr=255 ; sg=255 ;sb = (p-128)*4 elif (p < 256): sr=255 ; sg=(255-(p-191)*4) ; sb= (255-(p-191)*4) return (sr,sg,sb) palette = displayio.Palette(N) for i in range(N): r=255*i//(N-1) s=ColorMap(r) palette[i]=s[0]*2**16+s[1]*2**8+s[2] palette[0]=0 palette[1]=0xffffff tile_grid = displayio.TileGrid(bitmap, pixel_shader=palette) group.append(tile_grid) ############################# # Mandelbrot Drawing # ############################# before = 0 loop = 0 after = 0 print("Start drawing mandelbrot fractal :", CENTER, DIAMX, N) start = time.monotonic() for xp in range(WIDTH): s_before = time.monotonic() col_z_imag = ulab.array(r2) x = ulab.ones(HEIGHT)*r1[xp] x2 = x * x z2 = col_z_imag * col_z_imag output = ulab.zeros(HEIGHT, dtype=ulab.int16) notdone = [True]*HEIGHT before += time.monotonic() - s_before s_loop = time.monotonic() for i in range(N): z2 = col_z_imag * col_z_imag x2 = x * x try: output[(x2 + z2 ) < 4] = i except IndexError: break #col_z_imag[notdone] = ( ( x * col_z_imag )[notdone] * 2 ) + r2[notdone] col_z_imag = ( ( x * col_z_imag ) * 2 ) + r2 #x[notdone] = (x2 - z2)[notdone] + r1[xp] x = (x2 - z2) + r1[xp] loop += time.monotonic() - s_loop s_after = time.monotonic() limit = output >= N-1 if limit.count(True) > 0: output[limit] = -1 output = output + 1 for yp in range(HEIGHT): bitmap[xp,yp]= output[yp] after += time.monotonic() - s_after print("before :", before) print("loop :", loop) print("after :", after) print("total :", time.monotonic() - start) return group