Vector Operations¶
Overview¶
1D vector arithmetic operations — add, sub, mul, div, sqrt, dot product — with strided access support. Foundation level in TinyMath.
Header: #include "tiny_vec.h"
Platform Dispatch¶
ESP32: delegates to ESP-DSP library (dsps_* functions) for contiguous access (stride = 1). Generic: falls back to plain C loops.
Architecture¶
Functions are grouped into three tiers:
| Tier | Functions |
|---|---|
| Element-wise arithmetic | add, sub, mul, div (with and without constant, strided) |
| Square root | sqrt, sqrtf, inv_sqrt, inv_sqrtf |
| Dot product | dotprod, dotprode (extended stride) |
Common Parameters¶
These parameters appear in multiple functions:
| Param | Type | Description |
|---|---|---|
input1, input2 | const float * | Input vectors |
output | float * | Output vector (may alias inputs) |
len | int | Number of elements to process |
C | float | Scalar constant |
stride* | int | Element spacing. 1 = contiguous (fast path), > 1 = strided access |
allow_divide_by_zero | bool | If false, division by zero returns error; if true, returns inf |
dest | float * | Output scalar (for dot product) |
Element-wise Arithmetic¶
tiny_error_t tiny_vec_add_f32(const float *a, const float *b, float *c, int len,
int s1, int s2, int s_out);
tiny_error_t tiny_vec_sub_f32(/* same signature */);
tiny_error_t tiny_vec_mul_f32(/* same signature */);
Operation: \(c_i = a_i \pm b_i\) or \(c_i = a_i \cdot b_i\)
| Param | Description |
|---|---|
a, b, c | Input / output vectors |
len | Number of elements |
s1, s2, s_out | Stride for each vector (1 = contiguous) |
tiny_error_t tiny_vec_addc_f32(const float *in, float *out, int len, float C,
int s_in, int s_out);
tiny_error_t tiny_vec_subc_f32(/* same signature */);
tiny_error_t tiny_vec_mulc_f32(/* same signature */);
Operation: \(out_i = in_i \pm C\) or \(out_i = in_i \cdot C\)
tiny_error_t tiny_vec_div_f32(const float *a, const float *b, float *c, int len,
int s1, int s2, int s_out, bool allow_div0);
tiny_error_t tiny_vec_divc_f32(const float *in, float *out, int len, float C,
int s_in, int s_out, bool allow_div0);
Operation: \(c_i = a_i / b_i\) or \(out_i = in_i / C\)
| Param | Description |
|---|---|
allow_div0 | When false, division by zero returns TINY_ERR_MATH_ZERO_DIVISION. When true, returns inf silently |
Square Root¶
tiny_error_t tiny_vec_sqrt_f32(const float *in, float *out, int len);
tiny_error_t tiny_vec_inv_sqrt_f32(const float *in, float *out, int len);
Operation: \(out_i = \sqrt{in_i}\) or \(out_i = 1 / \sqrt{in_i}\)
tiny_error_t tiny_vec_sqrtf_f32(const float *in, float *out, int len);
tiny_error_t tiny_vec_inv_sqrtf_f32(const float *in, float *out, int len);
Operation: Same as above, but may use a faster approximation on some platforms. On ESP32, delegates to dsps_sqrtf32_f32.
When to use fast variants
Use sqrtf / inv_sqrtf when speed matters more than precision (e.g., normalisation in iterative algorithms). The difference is typically 2–3× faster with \(\sim 10^{-4}\) relative error.
Dot Product¶
Return Values¶
All functions return tiny_error_t:
| Code | Meaning |
|---|---|
TINY_OK | Success |
TINY_ERR_MATH_NULL_POINTER | Null pointer passed |
TINY_ERR_MATH_INVALID_LENGTH | len ≤ 0 |
TINY_ERR_MATH_ZERO_DIVISION | Division by zero (only when allow_div0 = false) |
TINY_ERR_MATH_NEGATIVE_SQRT | Negative input to sqrt or inv_sqrt |