def sample_function(x, y, z): return cupy.square(cupy.add(x, y))
def __radd__(self, other): return cupy.add(other, self)
def g(x, y, z): x += z cupy.add(x, y, z) return z
def g(x, y, z): a = x a += y cupy.add(x, y, z)
def map_coordinates(input, coordinates, output=None, order=None, mode='constant', cval=0.0, prefilter=True): """Map the input array to new coordinates by interpolation. The array of coordinates is used to find, for each point in the output, the corresponding coordinates in the input. The value of the input at those coordinates is determined by spline interpolation of the requested order. The shape of the output is derived from that of the coordinate array by dropping the first axis. The values of the array along the first axis are the coordinates in the input array at which the output value is found. Args: input (cupy.ndarray): The input array. coordinates (array_like): The coordinates at which ``input`` is evaluated. output (cupy.ndarray or ~cupy.dtype): The array in which to place the output, or the dtype of the returned array. order (int): The order of the spline interpolation. If it is not given, order 1 is used. It is different from :mod:`scipy.ndimage` and can change in the future. Currently it supports only order 0 and 1. mode (str): Points outside the boundaries of the input are filled according to the given mode (``'constant'``, ``'nearest'``, ``'mirror'`` or ``'opencv'``). Default is ``'constant'``. cval (scalar): Value used for points outside the boundaries of the input if ``mode='constant'`` or ``mode='opencv'``. Default is 0.0 prefilter (bool): It is not used yet. It just exists for compatibility with :mod:`scipy.ndimage`. Returns: cupy.ndarray: The result of transforming the input. The shape of the output is derived from that of ``coordinates`` by dropping the first axis. .. seealso:: :func:`scipy.ndimage.map_coordinates` """ _check_parameter('map_coordinates', order, mode) if mode == 'opencv' or mode == '_opencv_edge': input = cupy.pad(input, [(1, 1)] * input.ndim, 'constant', constant_values=cval) coordinates = cupy.add(coordinates, 1) mode = 'constant' ret = _get_output(output, input, coordinates.shape[1:]) if mode == 'nearest': for i in range(input.ndim): coordinates[i] = coordinates[i].clip(0, input.shape[i] - 1) elif mode == 'mirror': for i in range(input.ndim): length = input.shape[i] - 1 if length == 0: coordinates[i] = 0 else: coordinates[i] = cupy.remainder(coordinates[i], 2 * length) coordinates[i] = 2 * cupy.minimum( coordinates[i], length) - coordinates[i] if input.dtype.kind in 'iu': input = input.astype(cupy.float32) if order == 0: out = input[tuple(cupy.rint(coordinates).astype(cupy.int32))] else: coordinates_floor = cupy.floor(coordinates).astype(cupy.int32) coordinates_ceil = coordinates_floor + 1 sides = [] for i in range(input.ndim): # TODO(mizuno): Use array_equal after it is implemented if cupy.all(coordinates[i] == coordinates_floor[i]): sides.append([0]) else: sides.append([0, 1]) out = cupy.zeros(coordinates.shape[1:], dtype=input.dtype) if input.dtype in (cupy.float64, cupy.complex128): weight = cupy.empty(coordinates.shape[1:], dtype=cupy.float64) else: weight = cupy.empty(coordinates.shape[1:], dtype=cupy.float32) for side in itertools.product(*sides): weight.fill(1) ind = [] for i in range(input.ndim): if side[i] == 0: ind.append(coordinates_floor[i]) weight *= coordinates_ceil[i] - coordinates[i] else: ind.append(coordinates_ceil[i]) weight *= coordinates[i] - coordinates_floor[i] out += input[ind] * weight del weight if mode == 'constant': mask = cupy.zeros(coordinates.shape[1:], dtype=cupy.bool_) for i in range(input.ndim): mask += coordinates[i] < 0 mask += coordinates[i] > input.shape[i] - 1 out[mask] = cval del mask if ret.dtype.kind in 'iu': out = cupy.rint(out) ret[:] = out return ret
def gradient_function(x, y, w): return (1.0 / M) * (cp.add( cp.add(cp.dot(cp.dot(x.T, x), w), -1 * cp.dot(x.T, y)), 0.001 * w))
def make_blobs(n_samples=100, n_features=2, centers=None, cluster_std=1.0, center_box=(-10.0, 10.0), shuffle=True, random_state=None, return_centers=False, order='F', dtype='float32'): """Generate isotropic Gaussian blobs for clustering. Parameters ---------- n_samples : int or array-like, optional (default=100) If int, it is the total number of points equally divided among clusters. If array-like, each element of the sequence indicates the number of samples per cluster. n_features : int, optional (default=2) The number of features for each sample. centers : int or array of shape [n_centers, n_features], optional (default=None) The number of centers to generate, or the fixed center locations. If n_samples is an int and centers is None, 3 centers are generated. If n_samples is array-like, centers must be either None or an array of length equal to the length of n_samples. cluster_std : float or sequence of floats, optional (default=1.0) The standard deviation of the clusters. center_box : pair of floats (min, max), optional (default=(-10.0, 10.0)) The bounding box for each cluster center when centers are generated at random. shuffle : boolean, optional (default=True) Shuffle the samples. random_state : int, RandomState instance, default=None Determines random number generation for dataset creation. Pass an int for reproducible output across multiple function calls. return_centers : bool, optional (default=False) If True, then return the centers of each cluster order: str, optional (default='F') The order of the generated samples dtype : str, optional (default='float32') Dtype of the generated samples Returns ------- X : device array of shape [n_samples, n_features] The generated samples. y : device array of shape [n_samples] The integer labels for cluster membership of each sample. centers : device array, shape [n_centers, n_features] The centers of each cluster. Only returned if ``return_centers=True``. Examples -------- >>> from sklearn.datasets import make_blobs >>> X, y = make_blobs(n_samples=10, centers=3, n_features=2, ... random_state=0) >>> print(X.shape) (10, 2) >>> y array([0, 0, 1, 0, 2, 2, 2, 1, 1, 0]) >>> X, y = make_blobs(n_samples=[3, 3, 4], centers=None, n_features=2, ... random_state=0) >>> print(X.shape) (10, 2) >>> y array([0, 1, 2, 0, 2, 2, 2, 1, 1, 0]) See also -------- make_classification: a more intricate variant """ generator = _create_rs_generator(random_state=random_state) centers, n_centers = _get_centers(generator, centers, center_box, n_samples, n_features, dtype) # stds: if cluster_std is given as list, it must be consistent # with the n_centers if (hasattr(cluster_std, "__len__") and len(cluster_std) != n_centers): raise ValueError("Length of `clusters_std` not consistent with " "number of centers. Got centers = {} " "and cluster_std = {}".format(centers, cluster_std)) if isinstance(cluster_std, numbers.Real): cluster_std = cp.full(len(centers), cluster_std) if isinstance(n_samples, Iterable): n_samples_per_center = n_samples else: n_samples_per_center = [int(n_samples // n_centers)] * n_centers for i in range(n_samples % n_centers): n_samples_per_center[i] += 1 X = cp.zeros(n_samples * n_features, dtype=dtype) X = X.reshape((n_samples, n_features), order=order) y = cp.zeros(n_samples, dtype=dtype) if shuffle: proba_samples_per_center = np.array(n_samples_per_center) / np.sum( n_samples_per_center) np_seed = int(generator.randint(n_samples, size=1)) np.random.seed(np_seed) shuffled_sample_indices = cp.array( np.random.choice(n_centers, n_samples, replace=True, p=proba_samples_per_center)) for i, (n, std) in enumerate(zip(n_samples_per_center, cluster_std)): center_indices = cp.where(shuffled_sample_indices == i) y[center_indices[0]] = i X_k = generator.normal(scale=std, size=(len(center_indices[0]), n_features), dtype=dtype) # NOTE: Adding the loc explicitly as cupy has a bug # when calling generator.normal with an array for loc. # cupy.random.normal, however, works with the same # arguments cp.add(X_k, centers[i], out=X_k) X[center_indices[0], :] = X_k else: stop = 0 for i, (n, std) in enumerate(zip(n_samples_per_center, cluster_std)): start, stop = stop, stop + n_samples_per_center[i] y[start:stop] = i X_k = generator.normal(scale=std, size=(n, n_features), dtype=dtype) cp.add(X_k, centers[i], out=X_k) X[start:stop, :] = X_k if return_centers: return X, y, centers else: return X, y
def least_square_regular(X, Y): return cp.dot( cp.dot(cp.linalg.inv(cp.add(cp.dot(X.T, X), lambd * cp.eye(M, k=0))), X.T), Y)
def loss(x, y, w): diff = cp.add(cp.dot(x, w), -1 * y) loss = 1.0 / (2 * data_size) * cp.dot(diff.T, diff) return loss[0, 0]
def srrf( images: np.array, mag: float = 5, radius: float = 0.4, sample_n: int = 12, do_gw: bool = False, do_iw: bool = True, sofi_delay: int = 1, sofi_order: int = 2, avg_every_n: int = 1, do_drift_correction: bool = False, signal=None, ) -> cp.array: # designating memory pool for async data loading (deprecated) # ban memory pool for memory safety cp.cuda.set_allocator(None) cp.cuda.set_pinned_memory_allocator(None) # clean memory pool # cp.get_default_memory_pool().free_all_blocks() # pinned_memory_pool.free_all_blocks() # preparing frame_n, h, w = shape = images.shape[:3] batch_size = get_batch_size(frame_n, shape, mag) if batch_size is 0: raise ValueError("Image shape is too large to process!") if batch_size * avg_every_n > frame_n: if avg_every_n < frame_n: batch_size = frame_n // avg_every_n * avg_every_n else: avg_every_n = frame_n batch_size = frame_n warnings.warn( "avg_every_n is greater than total number of frames. All frames will be averaged." ) else: batch_size = batch_size * avg_every_n result = cp.zeros((int(h * mag), int(w * mag)), dtype=cp.float64) gradientX = cp.zeros((ceil(batch_size / avg_every_n), h, w), dtype=cp.float32) gradientY = cp.zeros((ceil(batch_size / avg_every_n), h, w), dtype=cp.float32) gradientX_zoomed = cp.zeros( (ceil(batch_size / avg_every_n), int(h * mag), int(w * mag)), dtype=cp.float32) gradientY_zoomed = cp.zeros( (ceil(batch_size / avg_every_n), int(h * mag), int(w * mag)), dtype=cp.float32) image_mat_zoomed = cp.empty( (ceil(batch_size / avg_every_n), int(h * mag), int(w * mag)), dtype=cp.float32) radiality = cp.empty( (ceil(batch_size / avg_every_n), int(h * mag), int(w * mag)), dtype=cp.float32) trac_mat = cp.empty((int(h * mag), int(w * mag)), dtype=cp.float64) drift_correction = drift_correction_wrapper( images) if do_drift_correction else None catmullrom_zoom = catmullrom_zoom_wrapper(images, mag, ceil(batch_size / avg_every_n)) calculate_radiality = calculate_radiality_wrapper(images, sample_n, mag, radius, do_gw, do_iw) # init CUDA streams sm_n = 2 # number of stream managers used in graphic card. streams = [cp.cuda.Stream(non_blocking=True) for _ in range(sm_n)] mats = [ cp.empty(images[:batch_size].shape, images[:batch_size].dtype) for _ in range(sm_n) ] for i, image_mat in enumerate(split_image_sequence(images, batch_size)): # send signal for gui progress bar if signal is not None: signal.emit(i * batch_size * 100 / frame_n) # dispatch the task of copying image mat from CPU to GPU asynchronously # select 2 streams index_next = i % sm_n index_current = (i - 1) % sm_n # Use next stream to load image data in advance if image_mat is not None: np_mat = to_pinned_memory(image_mat) mats[index_next].set(np_mat, stream=streams[index_next]) # skip the first time(the copy is not finished yet) if i is 0: continue # Use the current stream to process the image mat on GPU image_mat = mats[index_current] with streams[index_current]: print("dispatching %d/%d" % (i * batch_size, frame_n)) # average every n images to reduce the calculation. Optional if avg_every_n != 1: image_mat = avg_every_n_frames(image_mat, avg_every_n) calculate_gradient(image_mat, gradientX, gradientY) # 这里梯度也必须是原始图像的梯度插值得到。如果直接使用放大后的图像计算梯度,会受到插值算法的影响出现方格状图案 drift = drift_correction( image_mat ) if do_drift_correction and image_mat.shape[0] > 1 else None catmullrom_zoom(image_mat, image_mat_zoomed, drift) catmullrom_zoom(gradientX, gradientX_zoomed, drift) catmullrom_zoom(gradientY, gradientY_zoomed, drift) # calculate srrf radiality in every frame calculate_radiality(image_mat_zoomed, gradientX_zoomed, gradientY_zoomed, radiality) # do TRAC calculation (sofi) calculate_trac(radiality, trac_mat, sofi_delay, sofi_order) cp.add(result, trac_mat, out=result) streams[index_current].synchronize() # The next stream will be started only if the current stream is done # This is to protect the data on the next stream, otherwise the data will be # overwritten and the result will be wrong stop_event = streams[index_current].record() streams[index_next].wait_event(stop_event) [stream.synchronize() for stream in streams] return cp.asnumpy(result)
import numexpr as ne import scipy as sp import pyculib as pc import cupy as cp #init N = 1000 A = np.ones((N, N), dtype=np.float32) B = np.ones((N, N), dtype=np.float32) C = np.ones((N, N), dtype=np.float32) del A2, B2 A2 = cp.array(A) B2 = cp.array(B) C_list = [] C2 = cp.add(A2, B2) C_list.append(C2) #sigmoid #注:使用exp(n)比e**n快 #numpy def sigmoid0(a): return 1. / (1. + np.exp(-1. * a)) #numexpr def sigmoid3(a): return ne.evaluate("1./(1.+exp(-1.*a))")
def logisticmap_calc(x, p, mu): return cp.mod(cp.multiply(mu, cp.multiply(x, cp.add(x, 1))), p)
def func(x, y, z): return cupy.add(x, y, out=z)
def map_coordinates( input, coordinates, output=None, order=3, mode="constant", cval=0.0, prefilter=True, *, allow_float32=True, ): """Map the input array to new coordinates by interpolation. The array of coordinates is used to find, for each point in the output, the corresponding coordinates in the input. The value of the input at those coordinates is determined by spline interpolation of the requested order. The shape of the output is derived from that of the coordinate array by dropping the first axis. The values of the array along the first axis are the coordinates in the input array at which the output value is found. Args: input (cupy.ndarray): The input array. coordinates (array_like): The coordinates at which ``input`` is evaluated. output (cupy.ndarray or ~cupy.dtype): The array in which to place the output, or the dtype of the returned array. order (int): The order of the spline interpolation. Must be between 0 and 5. mode (str): Points outside the boundaries of the input are filled according to the given mode (``'constant'``, ``'nearest'``, ``'mirror'`` or ``'opencv'``). Default is ``'constant'``. cval (scalar): Value used for points outside the boundaries of the input if ``mode='constant'`` or ``mode='opencv'``. Default is 0.0 prefilter (bool): It is not used yet. It just exists for compatibility with :mod:`scipy.ndimage`. Returns: cupy.ndarray: The result of transforming the input. The shape of the output is derived from that of ``coordinates`` by dropping the first axis. Notes ----- This implementation handles boundary modes 'wrap' and 'reflect' correctly, while SciPy does not (at least as of release 1.4.0). So, if comparing to SciPy, some disagreement near the borders may occur unless ``mode == 'mirror'``. For ``order > 1`` with ``prefilter == True``, the spline prefilter boundary conditions are implemented correctly only for modes 'mirror', 'reflect' and 'wrap'. For the other modes ('constant' and 'nearest'), there is some innacuracy near the boundary of the array. .. seealso:: :func:`scipy.ndimage.map_coordinates` """ _check_parameter("map_coordinates", order, mode) if mode == "opencv" or mode == "_opencv_edge": input = cupy.pad(input, [(1, 1)] * input.ndim, "constant", constant_values=cval) coordinates = cupy.add(coordinates, 1) mode = "constant" ret = _get_output(output, input, coordinates.shape[1:]) integer_output = ret.dtype.kind in "iu" if input.dtype.kind in "iu": input = input.astype(cupy.float32) if coordinates.dtype.kind in "iu": if order > 1: # order > 1 (spline) kernels require floating-point coordinates if allow_float32: coord_dtype = cupy.promote_types(coordinates.dtype, cupy.float32) else: coord_dtype = cupy.promote_types(coordinates.dtype, cupy.float64) coordinates = coordinates.astype(coord_dtype) elif coordinates.dtype.kind != "f": raise ValueError("coordinates should have floating point dtype") else: if allow_float32: coord_dtype = cupy.promote_types(coordinates.dtype, cupy.float32) else: coord_dtype = cupy.promote_types(coordinates.dtype, cupy.float64) coordinates = coordinates.astype(coord_dtype, copy=False) if prefilter and order > 1: padded, npad = _prepad_for_spline_filter(input, mode, cval) filtered = spline_filter( padded, order, output=input.dtype, mode=mode, allow_float32=allow_float32, ) else: npad = 0 filtered = input large_int = max(_misc._prod(input.shape), coordinates.shape[0]) > 1 << 31 kern = _get_map_kernel( filtered.ndim, large_int, yshape=coordinates.shape, mode=mode, cval=cval, order=order, integer_output=integer_output, nprepad=npad, ) # kernel assumes C-contiguous arrays if not filtered.flags.c_contiguous: filtered = cupy.ascontiguousarray(filtered) if not coordinates.flags.c_contiguous: coordinates = cupy.ascontiguousarray(coordinates) kern(filtered, coordinates, ret) return ret
def func(x, y, z, u, v): cupy.add(x, y, out=z) cupy.subtract(z, x, out=u) cupy.multiply(z, x, out=v) return u
def _convert(image, dtype, force_copy=False, uniform=False): """ Convert an image to the requested data-type. Warnings are issued in case of precision loss, or when negative values are clipped during conversion to unsigned integer types (sign loss). Floating point values are expected to be normalized and will be clipped to the range [0.0, 1.0] or [-1.0, 1.0] when converting to unsigned or signed integers respectively. Numbers are not shifted to the negative side when converting from unsigned to signed integer types. Negative values will be clipped when converting to unsigned integers. Parameters ---------- image : ndarray Input image. dtype : dtype Target data-type. force_copy : bool, optional Force a copy of the data, irrespective of its current dtype. uniform : bool, optional Uniformly quantize the floating point range to the integer range. By default (uniform=False) floating point values are scaled and rounded to the nearest integers, which minimizes back and forth conversion errors. .. versionchanged :: 0.15 ``_convert`` no longer warns about possible precision or sign information loss. See discussions on these warnings at: https://github.com/scikit-image/scikit-image/issues/2602 https://github.com/scikit-image/scikit-image/issues/543#issuecomment-208202228 https://github.com/scikit-image/scikit-image/pull/3575 References ---------- .. [1] DirectX data conversion rules. https://msdn.microsoft.com/en-us/library/windows/desktop/dd607323%28v=vs.85%29.aspx .. [2] Data Conversions. In "OpenGL ES 2.0 Specification v2.0.25", pp 7-8. Khronos Group, 2010. .. [3] Proper treatment of pixels as integers. A.W. Paeth. In "Graphics Gems I", pp 249-256. Morgan Kaufmann, 1990. .. [4] Dirty Pixels. J. Blinn. In "Jim Blinn's corner: Dirty Pixels", pp 47-57. Morgan Kaufmann, 1998. """ image = cp.asarray(image) dtypeobj_in = image.dtype if dtype is cp.floating: dtypeobj_out = cp.dtype("float64") else: dtypeobj_out = cp.dtype(dtype) dtype_in = dtypeobj_in.type dtype_out = dtypeobj_out.type kind_in = dtypeobj_in.kind kind_out = dtypeobj_out.kind itemsize_in = dtypeobj_in.itemsize itemsize_out = dtypeobj_out.itemsize # Below, we do an `issubdtype` check. Its purpose is to find out # whether we can get away without doing any image conversion. This happens # when: # # - the output and input dtypes are the same or # - when the output is specified as a type, and the input dtype # is a subclass of that type (e.g. `cp.floating` will allow # `float32` and `float64` arrays through) if cp.issubdtype(dtype_in, cp.obj2sctype(dtype)): if force_copy: image = image.copy() return image if not (dtype_in in _supported_types and dtype_out in _supported_types): raise ValueError("Can not convert from {} to {}.".format( dtypeobj_in, dtypeobj_out)) if kind_in in "ui": imin_in = cp.iinfo(dtype_in).min imax_in = cp.iinfo(dtype_in).max if kind_out in "ui": imin_out = cp.iinfo(dtype_out).min imax_out = cp.iinfo(dtype_out).max # any -> binary if kind_out == "b": return image > dtype_in(dtype_range[dtype_in][1] / 2) # binary -> any if kind_in == "b": result = image.astype(dtype_out) if kind_out != "f": result *= dtype_out(dtype_range[dtype_out][1]) return result # float -> any if kind_in == "f": if kind_out == "f": # float -> float return image.astype(dtype_out) if cp.min(image) < -1.0 or cp.max(image) > 1.0: raise ValueError("Images of type float must be between -1 and 1.") # floating point -> integer # use float type that can represent output integer type computation_type = _dtype_itemsize(itemsize_out, dtype_in, cp.float32, cp.float64) if not uniform: if kind_out == "u": image_out = cp.multiply(image, imax_out, dtype=computation_type) else: image_out = cp.multiply(image, (imax_out - imin_out) / 2, dtype=computation_type) image_out -= 1.0 / 2.0 cp.rint(image_out, out=image_out) cp.clip(image_out, imin_out, imax_out, out=image_out) elif kind_out == "u": image_out = cp.multiply(image, imax_out + 1, dtype=computation_type) cp.clip(image_out, 0, imax_out, out=image_out) else: image_out = cp.multiply(image, (imax_out - imin_out + 1.0) / 2.0, dtype=computation_type) cp.floor(image_out, out=image_out) cp.clip(image_out, imin_out, imax_out, out=image_out) return image_out.astype(dtype_out) # signed/unsigned int -> float if kind_out == "f": # use float type that can exactly represent input integers computation_type = _dtype_itemsize(itemsize_in, dtype_out, cp.float32, cp.float64) if kind_in == "u": # using cp.divide or cp.multiply doesn't copy the data # until the computation time image = cp.multiply(image, 1.0 / imax_in, dtype=computation_type) # DirectX uses this conversion also for signed ints # if imin_in: # cp.maximum(image, -1.0, out=image) else: image = cp.add(image, 0.5, dtype=computation_type) image *= 2 / (imax_in - imin_in) return cp.asarray(image, dtype_out) # unsigned int -> signed/unsigned int if kind_in == "u": if kind_out == "i": # unsigned int -> signed int image = _scale(image, 8 * itemsize_in, 8 * itemsize_out - 1) return image.view(dtype_out) else: # unsigned int -> unsigned int return _scale(image, 8 * itemsize_in, 8 * itemsize_out) # signed int -> unsigned int if kind_out == "u": image = _scale(image, 8 * itemsize_in - 1, 8 * itemsize_out) result = cp.empty(image.shape, dtype_out) cp.maximum(image, 0, out=result, dtype=image.dtype, casting="unsafe") return result # signed int -> signed int if itemsize_in > itemsize_out: return _scale(image, 8 * itemsize_in - 1, 8 * itemsize_out - 1) image = image.astype(_dtype_bits("i", itemsize_out * 8)) image -= imin_in image = _scale(image, 8 * itemsize_in, 8 * itemsize_out, copy=False) image += imin_out return image.astype(dtype_out)
def g(x, y, z): a = x a += y cupy.add(x, y, z) return y
def run_cupy(price, strike, t, rate, vol): import cupy as cp # Allocate temporary arrays size = len(price) tmp = cp.empty(size, dtype='float64') vol_sqrt = cp.empty(size, dtype='float64') rsig = cp.empty(size, dtype='float64') d1 = cp.empty(size, dtype='float64') d2 = cp.empty(size, dtype='float64') # Outputs call = cp.empty(size, dtype='float64') put = cp.empty(size, dtype='float64') # Transfer inputs to the GPU price = cp.array(price) strike = cp.array(strike) t = cp.array(t) rate = cp.array(rate) vol = cp.array(vol) # Create an erf function that doesn't exist cp_erf = cp.core.create_ufunc('cupyx_scipy_erf', ('f->f', 'd->d'), 'out0 = erf(in0)', doc='''Error function. .. seealso:: :meth:`scipy.special.erf` ''') # Begin computation c05 = 3.0 c10 = 1.5 invsqrt2 = 1.0 / math.sqrt(2.0) cp.multiply(vol, vol, out=rsig) cp.multiply(rsig, c05, out=rsig) cp.add(rsig, rate, out=rsig) cp.sqrt(t, out=vol_sqrt) cp.multiply(vol_sqrt, vol, out=vol_sqrt) cp.multiply(rsig, t, out=tmp) cp.divide(price, strike, out=d1) cp.log2(d1, out=d1) cp.add(d1, tmp, out=d1) cp.divide(d1, vol_sqrt, out=d1) cp.subtract(d1, vol_sqrt, out=d2) # d1 = c05 + c05 * erf(d1 * invsqrt2) cp.multiply(d1, invsqrt2, out=d1) cp_erf(d1, out=d1) cp.multiply(d1, c05, out=d1) cp.add(d1, c05, out=d1) # d2 = c05 + c05 * erf(d2 * invsqrt2) cp.multiply(d2, invsqrt2, out=d2) cp_erf(d2, out=d2) cp.multiply(d2, c05, out=d2) cp.add(d2, c05, out=d2) # Reuse existing buffers e_rt = vol_sqrt tmp2 = rsig # e_rt = exp(-rate * t) cp.multiply(rate, -1.0, out=e_rt) cp.multiply(e_rt, t, out=e_rt) cp.exp(e_rt, out=e_rt) # call = price * d1 - e_rt * strike * d2 # # tmp = price * d1 # tmp2 = e_rt * strike * d2 # call = tmp - tmp2 cp.multiply(price, d1, out=tmp) cp.multiply(e_rt, strike, out=tmp2) cp.multiply(tmp2, d2, out=tmp2) cp.subtract(tmp, tmp2, out=call) # put = e_rt * strike * (c10 - d2) - price * (c10 - d1) # tmp = e_rt * strike # tmp2 = (c10 - d2) # put = tmp - tmp2 # tmp = c10 - d1 # tmp = price * tmp # put = put - tmp cp.multiply(e_rt, strike, out=tmp) cp.subtract(c10, d2, out=tmp2) cp.multiply(tmp, tmp2, out=put) cp.subtract(c10, d1, out=tmp) cp.multiply(price, tmp, out=tmp) cp.subtract(put, tmp, out=put) # Transfer outputs back to CPU call = cp.asnumpy(call) put = cp.asnumpy(put) return call, put
def __add__(self, other): return cupy.add(self, other)
def map_coordinates(input, coordinates, output=None, order=3, mode='constant', cval=0.0, prefilter=True): """Map the input array to new coordinates by interpolation. The array of coordinates is used to find, for each point in the output, the corresponding coordinates in the input. The value of the input at those coordinates is determined by spline interpolation of the requested order. The shape of the output is derived from that of the coordinate array by dropping the first axis. The values of the array along the first axis are the coordinates in the input array at which the output value is found. Args: input (cupy.ndarray): The input array. coordinates (array_like): The coordinates at which ``input`` is evaluated. output (cupy.ndarray or ~cupy.dtype): The array in which to place the output, or the dtype of the returned array. order (int): The order of the spline interpolation, default is 3. Must be in the range 0-5. mode (str): Points outside the boundaries of the input are filled according to the given mode (``'constant'``, ``'nearest'``, ``'mirror'``, ``'reflect'``, ``'wrap'``, ``'grid-mirror'``, ``'grid-wrap'``, ``'grid-constant'`` or ``'opencv'``). cval (scalar): Value used for points outside the boundaries of the input if ``mode='constant'`` or ``mode='opencv'``. Default is 0.0 prefilter (bool): It is not used yet. It just exists for compatibility with :mod:`scipy.ndimage`. Returns: cupy.ndarray: The result of transforming the input. The shape of the output is derived from that of ``coordinates`` by dropping the first axis. .. seealso:: :func:`scipy.ndimage.map_coordinates` """ _check_parameter('map_coordinates', order, mode) if mode == 'opencv' or mode == '_opencv_edge': input = cupy.pad(input, [(1, 1)] * input.ndim, 'constant', constant_values=cval) coordinates = cupy.add(coordinates, 1) mode = 'constant' ret = _util._get_output(output, input, coordinates.shape[1:]) integer_output = ret.dtype.kind in 'iu' _util._check_cval(mode, cval, integer_output) if input.dtype.kind in 'iu': input = input.astype(cupy.float32) coordinates = _check_coordinates(coordinates, order) filtered, nprepad = _filter_input(input, prefilter, mode, cval, order) large_int = max(_prod(input.shape), coordinates.shape[0]) > 1 << 31 kern = _interp_kernels._get_map_kernel(input.ndim, large_int, yshape=coordinates.shape, mode=mode, cval=cval, order=order, integer_output=integer_output, nprepad=nprepad) kern(filtered, coordinates, ret) return ret