suppose I need to define functions that when the input is numpy array, it returns the numpy version of the function. And when the input is a cupy array, it returns cupy version of the function.
import numpy as np
import cupy as cp
def tan(arr):
return cp.tan(arr) if arr.__class__ is cp.ndarray else np.tan(arr)
I also need to create functions such as sin, cos,tanh etc. Is there a way to create those in a loop to avoid typos, or are there better ways? Thanks!