tenzr::numpy¶
Classes¶
| Name | |
|---|---|
| struct | tenzr::numpy::BroadcastResult Pair of arrays broadcast to a common shape. |
Functions¶
| Name | |
|---|---|
| TENZR_APIDType | PromoteTypes(DType a, DType b) |
| TENZR_APIShape | BroadcastShapes(const Shape & a, const Shape & b) |
| TENZR_APIBroadcastResult | BroadcastArrays(const Array & a, const Array & b) |
| TENZR_APIBroadcastResult | PromoteAndBroadcast(const Array & a, const Array & b) |
| TENZR_APIArray | Zeros(Shape shape, DType dtype =DType::kFloat32) |
| TENZR_APIArray | Ones(Shape shape, DType dtype =DType::kFloat32) |
| TENZR_APIArray | Full(Shape shape, double fill_value, DType dtype =DType::kFloat32) |
| TENZR_APIArray | Arange(double start, double stop, double step =1.0, DType dtype =DType::kFloat32) |
| TENZR_APIArray | Arange(double stop, DType dtype =DType::kFloat32) |
| TENZR_APIArray | Linspace(double start, double stop, int64_t num =50, DType dtype =DType::kFloat32) |
| TENZR_APIArray | Eye(int64_t N, int64_t M =-1, int64_t k =0, DType dtype =DType::kFloat32) |
| template <typename T > Array |
Asarray(const std::vector< T > & data, Shape shape) |
| template <typename T > Array |
array(const std::vector< T > & data, Shape shape) |
| TENZR_APIArray | Add(const Array & a, const Array & b) Adds two arrays elementwise. |
| TENZR_APIArray | Subtract(const Array & a, const Array & b) Subtracts b from a elementwise. |
| TENZR_APIArray | Multiply(const Array & a, const Array & b) Multiplies two arrays elementwise. |
| TENZR_APIArray | Divide(const Array & a, const Array & b) Divides a by b elementwise. |
| TENZR_APIArray | Power(const Array & a, const Array & b) Raises a to the power b elementwise. |
| TENZR_APIArray | Maximum(const Array & a, const Array & b) Takes the elementwise maximum of two arrays. |
| TENZR_APIArray | Minimum(const Array & a, const Array & b) Takes the elementwise minimum of two arrays. |
| TENZR_APIArray | Remainder(const Array & a, const Array & b) Computes the elementwise remainder of a divided by b. |
| TENZR_APIArray | Atan2(const Array & a, const Array & b) Computes the elementwise two-argument arctangent. |
| TENZR_APIArray | Equal(const Array & a, const Array & b) Compares two arrays for elementwise equality. |
| TENZR_APIArray | NotEqual(const Array & a, const Array & b) Compares two arrays for elementwise inequality. |
| TENZR_APIArray | Less(const Array & a, const Array & b) Tests whether a is elementwise less than b. |
| TENZR_APIArray | LessEqual(const Array & a, const Array & b) Tests whether a is elementwise less than or equal to b. |
| TENZR_APIArray | Greater(const Array & a, const Array & b) Tests whether a is elementwise greater than b. |
| TENZR_APIArray | GreaterEqual(const Array & a, const Array & b) Tests whether a is elementwise greater than or equal to b. |
| TENZR_APIArray | Sin(const Array & a) Computes the elementwise sine. |
| TENZR_APIArray | Cos(const Array & a) Computes the elementwise cosine. |
| TENZR_APIArray | Tan(const Array & a) Computes the elementwise tangent. |
| TENZR_APIArray | Exp(const Array & a) Computes the elementwise exponential. |
| TENZR_APIArray | Log(const Array & a) Computes the elementwise natural logarithm. |
| TENZR_APIArray | Sqrt(const Array & a) Computes the elementwise square root. |
| TENZR_APIArray | Rsqrt(const Array & a) Computes the elementwise reciprocal square root. |
| TENZR_APIArray | Abs(const Array & a) Computes the elementwise absolute value. |
| TENZR_APIArray | tanh(const Array & a) Computes the elementwise hyperbolic tangent. |
| TENZR_APIArray | Ceil(const Array & a) Rounds each element up to the nearest integer. |
| TENZR_APIArray | Floor(const Array & a) Rounds each element down to the nearest integer. |
| TENZR_APIArray | Round(const Array & a) Rounds each element to the nearest integer. |
| TENZR_APIArray | Sign(const Array & a) Returns the elementwise sign (-1, 0, or 1). |
| TENZR_APIArray | Negative(const Array & a) Negates each element. |
| TENZR_APIArray | Clip(const Array & a, const Array & a_min, const Array & a_max) Clamps each element to the closed interval [a_min, a_max]. |
| TENZR_APIArray | Matmul(const Array & a, const Array & b) Matrix-multiplies two arrays, broadcasting batch dimensions. |
| TENZR_APIArray | Dot(const Array & a, const Array & b) Computes the dot product of two arrays. |
| TENZR_APIArray | Transpose(const Array & a, const std::vector< int64_t > & axes ={}) Permutes the array axes, reversing them when axes is empty. |
| TENZR_APIArray | Reshape(const Array & a, const Shape & new_shape) Returns an array with the requested shape. |
| TENZR_APIArray | Squeeze(const Array & a, std::optional< int64_t > axis =std::nullopt) Removes size-1 dimensions, either at axis or all of them. |
| TENZR_APIArray | ExpandDims(const Array & a, int64_t axis) Inserts a new size-1 dimension at axis. |
| TENZR_APIArray | Concatenate(const std::vector< Array > & arrays, int64_t axis =0) Concatenates arrays along an existing axis. |
| TENZR_APIArray | Stack(const std::vector< Array > & arrays, int64_t axis =0) Stacks arrays along a new axis. |
| TENZR_APIArray | Sum(const Array & a, std::optional< std::vector< int64_t > > axis =std::nullopt, bool keepdims =false) Sums elements, optionally over the given axes. |
| TENZR_APIArray | Mean(const Array & a, std::optional< std::vector< int64_t > > axis =std::nullopt, bool keepdims =false) Computes the arithmetic mean, optionally over the given axes. |
| TENZR_APIArray | Prod(const Array & a, std::optional< std::vector< int64_t > > axis =std::nullopt, bool keepdims =false) Multiplies elements, optionally over the given axes. |
| TENZR_APIArray | Max(const Array & a, std::optional< std::vector< int64_t > > axis =std::nullopt, bool keepdims =false) Computes the maximum, optionally over the given axes. |
| TENZR_APIArray | Min(const Array & a, std::optional< std::vector< int64_t > > axis =std::nullopt, bool keepdims =false) Computes the minimum, optionally over the given axes. |
| TENZR_APIArray | Var(const Array & a, std::optional< std::vector< int64_t > > axis =std::nullopt, bool keepdims =false) Computes the variance, optionally over the given axes. |
| TENZR_APIArray | StdDev(const Array & a, std::optional< std::vector< int64_t > > axis =std::nullopt, bool keepdims =false) Computes the standard deviation, optionally over the given axes. |
| TENZR_APIArray | Where(const Array & condition, const Array & x, const Array & y) Selects elements from x or y according to condition. |
| TENZR_APIArray | Take(const Array & a, const Array & indices, int64_t axis =0) Takes elements from a along axis using the given indices. |
Functions Documentation¶
function PromoteTypes¶
Parameters:
- a The first dtype.
- b The second dtype.
- a Left operand type.
- b Right operand type.
Return:
- The promoted dtype.
- The promoted type.
User-friendly multidimensional array processing layer (tenzr::numpy / tnp). Unlike tenzr::plax, this layer implements standard NumPy ergonomics: automatic type promotion, implicit broadcasting, canonical constructors, and the usual ufuncs, linear algebra, and reductions. Promotes two dtypes to their common NumPy result type. NumPy-style result type of a binary operation.
function BroadcastShapes¶
Parameters:
- a The first shape.
- b The second shape.
- a Left operand shape.
- b Right operand shape.
Return:
- The broadcasted shape.
- The broadcast result shape.
Computes the broadcasted shape of two shapes. Broadcast two shapes together.
function BroadcastArrays¶
Parameters:
- a The first array.
- b The second array.
- a Left operand.
- b Right operand.
Return:
- The two broadcasted arrays.
- Materialized, shape-matched arrays.
Broadcasts two arrays to a common shape. Broadcast two arrays to a shared contiguous shape.
function PromoteAndBroadcast¶
Parameters:
- a The first array.
- b The second array.
- a Left operand.
- b Right operand.
Return:
- The two promoted and broadcasted arrays.
- Materialized, dtype- and shape-matched arrays.
Promotes two arrays to a common dtype and broadcasts them to a common shape. Promote two arrays to a common dtype and broadcast them to a shared shape.
function Zeros¶
Parameters:
- shape The output shape.
- dtype The output dtype.
- shape Output shape.
- dtype Element type.
Return:
- The zero-filled array.
- The new array.
Creates an array of zeros with the given shape. Allocate a zero-filled array.
function Ones¶
Parameters:
- shape The output shape.
- dtype The output dtype.
- shape Output shape.
- dtype Element type.
Return:
- The one-filled array.
- The new array.
Creates an array of ones with the given shape. Allocate a one-filled array.
function Full¶
Parameters:
- shape The output shape.
- fill_value The value to fill.
- dtype The output dtype.
- shape Output shape.
- fill_value Value to fill with.
- dtype Element type.
Return:
- The filled array.
- The new array.
Creates an array filled with a constant value. Allocate an array filled with fill_value.
function Arange¶
Parameters:
- start The start of the interval.
- stop The end of the interval (exclusive).
- step The spacing between values.
- dtype The output dtype.
- start Range start.
- stop Range end (exclusive).
- step Spacing; must be non-zero.
- dtype Element type.
Return:
- The generated array.
- The new array.
Creates evenly spaced values over [start, stop). Evenly spaced values in [start, stop).
function Arange¶
Parameters:
- stop The end of the interval (exclusive).
- dtype The output dtype.
- stop Range end (exclusive).
- dtype Element type.
Return:
- The generated array.
- The new array.
Creates evenly spaced values over [0, stop) with a step of 1. Values 0, 1, ..., stop - 1.
function Linspace¶
Parameters:
- start The start of the interval.
- stop The end of the interval (inclusive).
- num The number of samples.
- dtype The output dtype.
- start Range start.
- stop Range end (inclusive).
- num Number of samples; must be positive.
- dtype Element type.
Return:
- The generated array.
- The new array.
Creates num evenly spaced values over the closed interval [start, stop]. num evenly spaced values from start to stop inclusive.
function Eye¶
Parameters:
- N Number of rows.
- M Number of columns, defaulting to N when negative.
- k Diagonal index (0 is the main diagonal).
- dtype The output dtype.
- N Row count.
- M Column count; defaults to N when negative.
- k Diagonal offset.
- dtype Element type.
Return:
- The identity-like array.
- The new array.
Creates a 2D array with ones on the k-th diagonal and zeros elsewhere. 2-D identity-like matrix with ones on the k-th diagonal.
function Asarray¶
Parameters:
- data The host element values.
- shape The shape of the resulting array.
Template Parameters:
- T The element type.
Return: The array holding data.
Creates an array from a host vector of the given shape.
function array¶
Parameters:
- data The host element values.
- shape The shape of the resulting array.
Template Parameters:
- T The element type.
Return: The array holding data.
Alias of asarray, creating an array from a host vector of the given shape.
function Add¶
Adds two arrays elementwise.
Parameters:
- a Left operand.
- b Right operand.
Return: a + b.
Elementwise addition with promotion and broadcasting.
function Subtract¶
Subtracts b from a elementwise.
Parameters:
- a Left operand.
- b Right operand.
Return: a - b.
Elementwise subtraction with promotion and broadcasting.
function Multiply¶
Multiplies two arrays elementwise.
Parameters:
- a Left operand.
- b Right operand.
Return: a * b.
Elementwise multiplication with promotion and broadcasting.
function Divide¶
Divides a by b elementwise.
Parameters:
- a Dividend.
- b Divisor.
Return: a / b.
Elementwise division with promotion and broadcasting.
function Power¶
Raises a to the power b elementwise.
Parameters:
- a Base.
- b Exponent.
Return: a ** b.
Elementwise exponentiation with promotion and broadcasting.
function Maximum¶
Takes the elementwise maximum of two arrays.
Parameters:
- a Left operand.
- b Right operand.
Return: Elementwise maximum.
Elementwise maximum with promotion and broadcasting.
function Minimum¶
Takes the elementwise minimum of two arrays.
Parameters:
- a Left operand.
- b Right operand.
Return: Elementwise minimum.
Elementwise minimum with promotion and broadcasting.
function Remainder¶
Computes the elementwise remainder of a divided by b.
Parameters:
- a Dividend.
- b Divisor.
Return: a % b.
Elementwise remainder with promotion and broadcasting.
function Atan2¶
Computes the elementwise two-argument arctangent.
Parameters:
- a First operand.
- b Second operand.
Return: atan2(a, b).
Elementwise two-argument arctangent with promotion and broadcasting.
function Equal¶
Compares two arrays for elementwise equality.
Parameters:
- a Left operand.
- b Right operand.
Return: Boolean array.
Elementwise equality comparison.
function NotEqual¶
Compares two arrays for elementwise inequality.
Parameters:
- a Left operand.
- b Right operand.
Return: Boolean array.
Elementwise inequality comparison.
function Less¶
Tests whether a is elementwise less than b.
Parameters:
- a Left operand.
- b Right operand.
Return: Boolean array.
Elementwise less-than comparison.
function LessEqual¶
Tests whether a is elementwise less than or equal to b.
Parameters:
- a Left operand.
- b Right operand.
Return: Boolean array.
Elementwise less-than-or-equal comparison.
function Greater¶
Tests whether a is elementwise greater than b.
Parameters:
- a Left operand.
- b Right operand.
Return: Boolean array.
Elementwise greater-than comparison.
function GreaterEqual¶
Tests whether a is elementwise greater than or equal to b.
Parameters:
- a Left operand.
- b Right operand.
Return: Boolean array.
Elementwise greater-than-or-equal comparison.
function Sin¶
Computes the elementwise sine.
Parameters:
- a Input array.
Return: sin(a).
Elementwise sine.
function Cos¶
Computes the elementwise cosine.
Parameters:
- a Input array.
Return: cos(a).
Elementwise cosine.
function Tan¶
Computes the elementwise tangent.
Parameters:
- a Input array.
Return: tan(a).
Elementwise tangent.
function Exp¶
Computes the elementwise exponential.
Parameters:
- a Input array.
Return: exp(a).
Elementwise exponential.
function Log¶
Computes the elementwise natural logarithm.
Parameters:
- a Input array.
Return: log(a).
Elementwise natural logarithm.
function Sqrt¶
Computes the elementwise square root.
Parameters:
- a Input array.
Return: sqrt(a).
Elementwise square root.
function Rsqrt¶
Computes the elementwise reciprocal square root.
Parameters:
- a Input array.
Return: 1 / sqrt(a).
Elementwise reciprocal square root.
function Abs¶
Computes the elementwise absolute value.
Parameters:
- a Input array.
Return: |a|.
Elementwise absolute value.
function tanh¶
Computes the elementwise hyperbolic tangent.
Parameters:
- a Input array.
Return: tanh(a).
Elementwise hyperbolic tangent.
function Ceil¶
Rounds each element up to the nearest integer.
Parameters:
- a Input array.
Return: ceil(a).
Elementwise ceiling.
function Floor¶
Rounds each element down to the nearest integer.
Parameters:
- a Input array.
Return: floor(a).
Elementwise floor.
function Round¶
Rounds each element to the nearest integer.
Parameters:
- a Input array.
Return: round(a).
Elementwise round-to-nearest.
function Sign¶
Returns the elementwise sign (-1, 0, or 1).
Parameters:
- a Input array.
Return: sign(a).
Elementwise sign.
function Negative¶
Negates each element.
Parameters:
- a Input array.
Return: -a.
Elementwise negation.
function Clip¶
Clamps each element to the closed interval [a_min, a_max].
Parameters:
- a Input array.
- a_min Lower bound.
- a_max Upper bound.
Return: The clamped array.
Clamp elements into [a_min, a_max] with promotion and broadcasting across all three operands.
function Matmul¶
Matrix-multiplies two arrays, broadcasting batch dimensions.
Parameters:
- a Left operand.
- b Right operand.
Return: a @ b.
Matrix multiplication.
function Dot¶
Computes the dot product of two arrays.
Parameters:
- a Left operand.
- b Right operand.
Return: The dot product or matmul result.
Vector dot product for rank-1 operands, otherwise matrix multiplication.
function Transpose¶
Permutes the array axes, reversing them when axes is empty.
Parameters:
- a Input array.
- axes Optional target axis order; reverses when empty.
Return: The transposed view.
Reverse the axes, or apply an explicit permutation.
function Reshape¶
Returns an array with the requested shape.
Parameters:
- a Input array.
- new_shape Target shape.
Return: The reshaped array.
Reshape without changing the element count.
function Squeeze¶
Removes size-1 dimensions, either at axis or all of them.
Parameters:
- a Input array.
- axis Optional axis to squeeze; all unit dimensions when empty.
Return: The squeezed view.
Remove unit dimensions.
function ExpandDims¶
Inserts a new size-1 dimension at axis.
Parameters:
- a Input array.
- axis Insertion position.
Return: The expanded view.
Insert a unit dimension at axis.
function Concatenate¶
Concatenates arrays along an existing axis.
Parameters:
- arrays Arrays to join, all with the same rank and matching non-axis dimensions.
- axis Concatenation axis.
Return: The concatenated array.
Join arrays along an existing axis, promoting to a common dtype.
function Stack¶
Stacks arrays along a new axis.
Parameters:
- arrays Arrays to stack, all with the same shape.
- axis Position of the new axis.
Return: The stacked array.
Join arrays along a new axis.
function Sum¶
TENZR_APIArray Sum(
const Array & a,
std::optional< std::vector< int64_t > > axis =std::nullopt,
bool keepdims =false
)
Sums elements, optionally over the given axes.
Parameters:
- a Input array.
- axis Axes to reduce; all when empty.
- keepdims Whether reduced axes are retained.
Return: The reduced array.
Sum over the given axes.
function Mean¶
TENZR_APIArray Mean(
const Array & a,
std::optional< std::vector< int64_t > > axis =std::nullopt,
bool keepdims =false
)
Computes the arithmetic mean, optionally over the given axes.
Parameters:
- a Input array.
- axis Axes to reduce; all when empty.
- keepdims Whether reduced axes are retained.
Return: The reduced array.
Arithmetic mean over the given axes.
function Prod¶
TENZR_APIArray Prod(
const Array & a,
std::optional< std::vector< int64_t > > axis =std::nullopt,
bool keepdims =false
)
Multiplies elements, optionally over the given axes.
Parameters:
- a Input array.
- axis Axes to reduce; all when empty.
- keepdims Whether reduced axes are retained.
Return: The reduced array.
Product over the given axes.
function Max¶
TENZR_APIArray Max(
const Array & a,
std::optional< std::vector< int64_t > > axis =std::nullopt,
bool keepdims =false
)
Computes the maximum, optionally over the given axes.
Parameters:
- a Input array.
- axis Axes to reduce; all when empty.
- keepdims Whether reduced axes are retained.
Return: The reduced array.
Maximum over the given axes.
function Min¶
TENZR_APIArray Min(
const Array & a,
std::optional< std::vector< int64_t > > axis =std::nullopt,
bool keepdims =false
)
Computes the minimum, optionally over the given axes.
Parameters:
- a Input array.
- axis Axes to reduce; all when empty.
- keepdims Whether reduced axes are retained.
Return: The reduced array.
Minimum over the given axes.
function Var¶
TENZR_APIArray Var(
const Array & a,
std::optional< std::vector< int64_t > > axis =std::nullopt,
bool keepdims =false
)
Computes the variance, optionally over the given axes.
Parameters:
- a Input array.
- axis Axes to reduce; all when empty.
- keepdims Whether reduced axes are retained.
Return: The reduced array.
Variance over the given axes.
function StdDev¶
TENZR_APIArray StdDev(
const Array & a,
std::optional< std::vector< int64_t > > axis =std::nullopt,
bool keepdims =false
)
Computes the standard deviation, optionally over the given axes.
Parameters:
- a Input array.
- axis Axes to reduce; all when empty.
- keepdims Whether reduced axes are retained.
Return: The reduced array.
Standard deviation over the given axes.
function Where¶
Selects elements from x or y according to condition.
Parameters:
- condition Boolean selector.
- x Value where true.
- y Value where false.
Return: The selected array.
Select x where condition is true and y otherwise.
function Take¶
Takes elements from a along axis using the given indices.
Parameters:
- a Source array.
- indices Index array.
- axis Axis to gather along.
Return: The gathered array.
Gather entries of a along axis at the given indices.