Skip to content

Instantly share code, notes, and snippets.

@JarnaChao09
Last active February 26, 2024 04:45
Show Gist options
  • Save JarnaChao09/7fb2f223474d5e5b9eedb74b89830101 to your computer and use it in GitHub Desktop.
Save JarnaChao09/7fb2f223474d5e5b9eedb74b89830101 to your computer and use it in GitHub Desktop.
convolution.py
from ulab import numpy as np
def meshgrid(input_rows, input_cols):
output_shape = (input_rows.shape[0], input_cols.shape[0])
ret_rows = np.zeros(output_shape, dtype=np.int64)
ret_cols = np.zeros(output_shape, dtype=np.int64)
r, c = output_shape
for i in range(0, r):
for j in range(0, c):
ret_rows[i, j] = input_rows[i]
ret_cols[i, j] = input_cols[j]
return (ret_rows, ret_cols)
def take_with_default(arr, indices, default):
"""
Indexes an ndarray following the indices and uses a default value for all indexes that are out of bounds
"""
# will be broadcasted up into np.array[bool]
mask = True
for i, index in enumerate(indices):
mask = mask & (index >= 0) & (index < arr.shape[i])
# result with the shape of the mask filled with the default value
result = default * np.ones(mask.shape, dtype=arr.dtype)
# replacing all in bounds indices with their corresponding data
result[mask] = arr[tuple(ind[mask] for ind in indices)]
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment