Python wrapper for OpenCL enabling GPU and parallel computing with comprehensive array operations and mathematical functions
86
Comprehensive set of mathematical functions optimized for GPU execution, including trigonometric, exponential, logarithmic, and special functions that operate element-wise on arrays with high performance and numerical accuracy.
Standard trigonometric functions with high accuracy and performance on GPU arrays.
def sin(x, queue=None):
"""
Element-wise sine function.
Parameters:
- x (Array): Input array
- queue (CommandQueue, optional): Command queue
Returns:
Array: Sine of each element
"""
def cos(x, queue=None):
"""
Element-wise cosine function.
Parameters:
- x (Array): Input array
- queue (CommandQueue, optional): Command queue
Returns:
Array: Cosine of each element
"""
def tan(x, queue=None):
"""
Element-wise tangent function.
Parameters:
- x (Array): Input array
- queue (CommandQueue, optional): Command queue
Returns:
Array: Tangent of each element
"""
def asin(x, queue=None):
"""
Element-wise arcsine function.
Parameters:
- x (Array): Input array (values in [-1, 1])
- queue (CommandQueue, optional): Command queue
Returns:
Array: Arcsine of each element in radians
"""
def acos(x, queue=None):
"""
Element-wise arccosine function.
Parameters:
- x (Array): Input array (values in [-1, 1])
- queue (CommandQueue, optional): Command queue
Returns:
Array: Arccosine of each element in radians
"""
def atan(x, queue=None):
"""
Element-wise arctangent function.
Parameters:
- x (Array): Input array
- queue (CommandQueue, optional): Command queue
Returns:
Array: Arctangent of each element in radians
"""Hyperbolic trigonometric functions and their inverses.
def sinh(x, queue=None):
"""
Element-wise hyperbolic sine function.
Parameters:
- x (Array): Input array
- queue (CommandQueue, optional): Command queue
Returns:
Array: Hyperbolic sine of each element
"""
def cosh(x, queue=None):
"""
Element-wise hyperbolic cosine function.
Parameters:
- x (Array): Input array
- queue (CommandQueue, optional): Command queue
Returns:
Array: Hyperbolic cosine of each element
"""
def tanh(x, queue=None):
"""
Element-wise hyperbolic tangent function.
Parameters:
- x (Array): Input array
- queue (CommandQueue, optional): Command queue
Returns:
Array: Hyperbolic tangent of each element
"""
def asinh(x, queue=None):
"""
Element-wise inverse hyperbolic sine function.
Parameters:
- x (Array): Input array
- queue (CommandQueue, optional): Command queue
Returns:
Array: Inverse hyperbolic sine of each element
"""
def acosh(x, queue=None):
"""
Element-wise inverse hyperbolic cosine function.
Parameters:
- x (Array): Input array (values >= 1)
- queue (CommandQueue, optional): Command queue
Returns:
Array: Inverse hyperbolic cosine of each element
"""
def atanh(x, queue=None):
"""
Element-wise inverse hyperbolic tangent function.
Parameters:
- x (Array): Input array (values in (-1, 1))
- queue (CommandQueue, optional): Command queue
Returns:
Array: Inverse hyperbolic tangent of each element
"""Trigonometric functions using π as the base unit for improved accuracy with π multiples.
def sinpi(x, queue=None):
"""
Element-wise sin(π*x) function.
Parameters:
- x (Array): Input array
- queue (CommandQueue, optional): Command queue
Returns:
Array: sin(π*x) for each element
"""
def cospi(x, queue=None):
"""
Element-wise cos(π*x) function.
Parameters:
- x (Array): Input array
- queue (CommandQueue, optional): Command queue
Returns:
Array: cos(π*x) for each element
"""
def tanpi(x, queue=None):
"""
Element-wise tan(π*x) function.
Parameters:
- x (Array): Input array
- queue (CommandQueue, optional): Command queue
Returns:
Array: tan(π*x) for each element
"""
def asinpi(x, queue=None):
"""
Element-wise asin(x)/π function.
Parameters:
- x (Array): Input array (values in [-1, 1])
- queue (CommandQueue, optional): Command queue
Returns:
Array: asin(x)/π for each element
"""
def acospi(x, queue=None):
"""
Element-wise acos(x)/π function.
Parameters:
- x (Array): Input array (values in [-1, 1])
- queue (CommandQueue, optional): Command queue
Returns:
Array: acos(x)/π for each element
"""
def atanpi(x, queue=None):
"""
Element-wise atan(x)/π function.
Parameters:
- x (Array): Input array
- queue (CommandQueue, optional): Command queue
Returns:
Array: atan(x)/π for each element
"""Exponential functions with different bases for scientific computation.
def exp(x, queue=None):
"""
Element-wise natural exponential function (e^x).
Parameters:
- x (Array): Input array
- queue (CommandQueue, optional): Command queue
Returns:
Array: e^x for each element
"""
def exp2(x, queue=None):
"""
Element-wise base-2 exponential function (2^x).
Parameters:
- x (Array): Input array
- queue (CommandQueue, optional): Command queue
Returns:
Array: 2^x for each element
"""
def exp10(x, queue=None):
"""
Element-wise base-10 exponential function (10^x).
Parameters:
- x (Array): Input array
- queue (CommandQueue, optional): Command queue
Returns:
Array: 10^x for each element
"""
def expm1(x, queue=None):
"""
Element-wise exp(x) - 1 function with improved accuracy for small x.
Parameters:
- x (Array): Input array
- queue (CommandQueue, optional): Command queue
Returns:
Array: exp(x) - 1 for each element
"""Logarithmic functions with different bases and specialized variants.
def log(x, queue=None):
"""
Element-wise natural logarithm function.
Parameters:
- x (Array): Input array (values > 0)
- queue (CommandQueue, optional): Command queue
Returns:
Array: Natural logarithm of each element
"""
def log2(x, queue=None):
"""
Element-wise base-2 logarithm function.
Parameters:
- x (Array): Input array (values > 0)
- queue (CommandQueue, optional): Command queue
Returns:
Array: Base-2 logarithm of each element
"""
def log10(x, queue=None):
"""
Element-wise base-10 logarithm function.
Parameters:
- x (Array): Input array (values > 0)
- queue (CommandQueue, optional): Command queue
Returns:
Array: Base-10 logarithm of each element
"""
def log1p(x, queue=None):
"""
Element-wise log(1 + x) function with improved accuracy for small x.
Parameters:
- x (Array): Input array (values > -1)
- queue (CommandQueue, optional): Command queue
Returns:
Array: log(1 + x) for each element
"""
def logb(x, queue=None):
"""
Element-wise extract exponent function.
Parameters:
- x (Array): Input array (finite non-zero values)
- queue (CommandQueue, optional): Command queue
Returns:
Array: Exponent of each element
"""
def ilogb(x, queue=None):
"""
Element-wise extract exponent as integer.
Parameters:
- x (Array): Input array (finite non-zero values)
- queue (CommandQueue, optional): Command queue
Returns:
Array: Integer exponent of each element
"""Square root, cube root, and related functions.
def sqrt(x, queue=None):
"""
Element-wise square root function.
Parameters:
- x (Array): Input array (values >= 0)
- queue (CommandQueue, optional): Command queue
Returns:
Array: Square root of each element
"""
def cbrt(x, queue=None):
"""
Element-wise cube root function.
Parameters:
- x (Array): Input array
- queue (CommandQueue, optional): Command queue
Returns:
Array: Cube root of each element
"""Various rounding and truncation operations.
def ceil(x, queue=None):
"""
Element-wise ceiling function (round up to nearest integer).
Parameters:
- x (Array): Input array
- queue (CommandQueue, optional): Command queue
Returns:
Array: Ceiling of each element
"""
def floor(x, queue=None):
"""
Element-wise floor function (round down to nearest integer).
Parameters:
- x (Array): Input array
- queue (CommandQueue, optional): Command queue
Returns:
Array: Floor of each element
"""
def round(x, queue=None):
"""
Element-wise round to nearest integer function.
Parameters:
- x (Array): Input array
- queue (CommandQueue, optional): Command queue
Returns:
Array: Nearest integer for each element
"""
def rint(x, queue=None):
"""
Element-wise round to nearest integer function.
Parameters:
- x (Array): Input array
- queue (CommandQueue, optional): Command queue
Returns:
Array: Nearest integer for each element
"""
def trunc(x, queue=None):
"""
Element-wise truncate to integer function (toward zero).
Parameters:
- x (Array): Input array
- queue (CommandQueue, optional): Command queue
Returns:
Array: Truncated integer for each element
"""Functions for magnitude and sign operations.
def fabs(x, queue=None):
"""
Element-wise absolute value function.
Parameters:
- x (Array): Input array
- queue (CommandQueue, optional): Command queue
Returns:
Array: Absolute value of each element
"""Error functions, gamma functions, and other special mathematical functions.
def erf(x, queue=None):
"""
Element-wise error function.
Parameters:
- x (Array): Input array
- queue (CommandQueue, optional): Command queue
Returns:
Array: Error function of each element
"""
def erfc(x, queue=None):
"""
Element-wise complementary error function.
Parameters:
- x (Array): Input array
- queue (CommandQueue, optional): Command queue
Returns:
Array: Complementary error function (1 - erf(x))
"""
def tgamma(x, queue=None):
"""
Element-wise true gamma function.
Parameters:
- x (Array): Input array
- queue (CommandQueue, optional): Command queue
Returns:
Array: Gamma function of each element
"""
def lgamma(x, queue=None):
"""
Element-wise natural logarithm of absolute value of gamma function.
Parameters:
- x (Array): Input array
- queue (CommandQueue, optional): Command queue
Returns:
Array: log(|gamma(x)|) for each element
"""Utility functions for special values and numerical operations.
def nan(x, queue=None):
"""
Generate NaN (Not a Number) values.
Parameters:
- x (Array): Input array (used for shape and type)
- queue (CommandQueue, optional): Command queue
Returns:
Array: Array filled with NaN values
"""import pyopencl as cl
import pyopencl.array as cl_array
import pyopencl.clmath as clmath
import numpy as np
# Setup
ctx = cl.create_some_context()
queue = cl.CommandQueue(ctx)
# Create array with angles
angles = cl_array.arange(queue, 0, 2*np.pi, 0.1, dtype=np.float32)
# Trigonometric functions
sin_values = clmath.sin(angles)
cos_values = clmath.cos(angles)
tan_values = clmath.tan(angles)
print(f"Angles shape: {angles.shape}")
print(f"Sin values: {sin_values.get()[:5]}")
print(f"Cos values: {cos_values.get()[:5]}")import pyopencl as cl
import pyopencl.array as cl_array
import pyopencl.clmath as clmath
import numpy as np
# Setup
ctx = cl.create_some_context()
queue = cl.CommandQueue(ctx)
# Create array
x = cl_array.arange(queue, 0.1, 10, 0.1, dtype=np.float32)
# Exponential functions
exp_x = clmath.exp(x)
exp2_x = clmath.exp2(x)
exp10_x = clmath.exp10(x)
# Logarithmic functions
log_x = clmath.log(x)
log2_x = clmath.log2(x)
log10_x = clmath.log10(x)
print(f"Original: {x.get()[:5]}")
print(f"exp(x): {exp_x.get()[:5]}")
print(f"log(x): {log_x.get()[:5]}")import pyopencl as cl
import pyopencl.array as cl_array
import pyopencl.clmath as clmath
import numpy as np
# Setup
ctx = cl.create_some_context()
queue = cl.CommandQueue(ctx)
# Create array for statistical/probability calculations
x = cl_array.arange(queue, -3, 3, 0.1, dtype=np.float32)
# Error function (used in statistics)
erf_x = clmath.erf(x)
erfc_x = clmath.erfc(x)
# Gamma function (used in probability distributions)
positive_x = cl_array.arange(queue, 0.1, 5, 0.1, dtype=np.float32)
gamma_x = clmath.tgamma(positive_x)
lgamma_x = clmath.lgamma(positive_x)
print(f"Error function: {erf_x.get()[:5]}")
print(f"Gamma function: {gamma_x.get()[:5]}")import pyopencl as cl
import pyopencl.array as cl_array
import pyopencl.clmath as clmath
import numpy as np
# Setup
ctx = cl.create_some_context()
queue = cl.CommandQueue(ctx)
# Create array with decimal values
x = cl_array.to_device(queue, np.array([-2.7, -1.3, 0.5, 1.8, 2.9], dtype=np.float32))
# Different rounding operations
ceil_x = clmath.ceil(x)
floor_x = clmath.floor(x)
round_x = clmath.round(x)
trunc_x = clmath.trunc(x)
print(f"Original: {x.get()}")
print(f"Ceiling: {ceil_x.get()}")
print(f"Floor: {floor_x.get()}")
print(f"Round: {round_x.get()}")
print(f"Truncate: {trunc_x.get()}")import pyopencl as cl
import pyopencl.array as cl_array
import pyopencl.clmath as clmath
import numpy as np
# Setup
ctx = cl.create_some_context()
queue = cl.CommandQueue(ctx)
# Create input array
x = cl_array.arange(queue, 0, 4*np.pi, 0.1, dtype=np.float32)
# Complex mathematical expression: exp(-x/10) * sin(x)
damped_sine = clmath.exp(-x/10) * clmath.sin(x)
# Another example: sqrt(x^2 + 1)
x_vals = cl_array.arange(queue, -10, 10, 0.1, dtype=np.float32)
hypot_like = clmath.sqrt(x_vals*x_vals + 1)
print(f"Damped sine wave: {damped_sine.get()[:10]}")
print(f"Hypot-like function: {hypot_like.get()[:10]}")Install with Tessl CLI
npx tessl i tessl/pypi-pyopencldocs
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10