> ## Documentation Index
> Fetch the complete documentation index at: https://numpyts.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Arithmetic

> Element-wise arithmetic operations: add, subtract, multiply, divide, power, absolute value, and more.

All arithmetic functions operate element-wise. Binary functions accept an `NDArray` or a scalar as the second argument and broadcast as needed.

### add

Add arguments element-wise.

```typescript theme={null}
function add(x1: NDArray, x2: NDArray | number): NDArray
```

| Name | Type | Default | Description |
| - | - | - | - |
| `x1` | `NDArray` | - | First input array. |
| `x2` | `NDArray \| number` | - | Second input array or scalar. |

**Returns:** `NDArray` -- Element-wise sum of the inputs.

```typescript Full Library theme={null}
import * as np from 'numpy-ts';

const a = np.array([1, 2, 3]);
const b = np.add(a, np.array([10, 20, 30]));
// array([11, 22, 33])

const c = np.add(a, 100);
// array([101, 102, 103])
```

```typescript Core theme={null}
import { add, array } from 'numpy-ts/core';

const a = array([1, 2, 3]);
const b = add(a, 100);
```

***

### subtract

Subtract arguments element-wise.

```typescript theme={null}
function subtract(x1: NDArray, x2: NDArray | number): NDArray
```

| Name | Type | Default | Description |
| - | - | - | - |
| `x1` | `NDArray` | - | First input array. |
| `x2` | `NDArray \| number` | - | Second input array or scalar. |

**Returns:** `NDArray` -- Element-wise difference `x1 - x2`.

```typescript Full Library theme={null}
import * as np from 'numpy-ts';

const a = np.subtract(np.array([10, 20, 30]), np.array([1, 2, 3]));
// array([9, 18, 27])
```

```typescript Core theme={null}
import { subtract, array } from 'numpy-ts/core';

const a = subtract(array([10, 20, 30]), 5);
// array([5, 15, 25])
```

***

### multiply

Multiply arguments element-wise.

```typescript theme={null}
function multiply(x1: NDArray, x2: NDArray | number): NDArray
```

| Name | Type | Default | Description |
| - | - | - | - |
| `x1` | `NDArray` | - | First input array. |
| `x2` | `NDArray \| number` | - | Second input array or scalar. |

**Returns:** `NDArray` -- Element-wise product.

```typescript Full Library theme={null}
import * as np from 'numpy-ts';

const a = np.multiply(np.array([1, 2, 3]), np.array([4, 5, 6]));
// array([4, 10, 18])
```

```typescript Core theme={null}
import { multiply, array } from 'numpy-ts/core';

const a = multiply(array([2, 4, 6]), 0.5);
// array([1, 2, 3])
```

***

### divide

Divide arguments element-wise (true division).

```typescript theme={null}
function divide(x: NDArray, divisor: NDArray | number): NDArray
```

| Name | Type | Default | Description |
| - | - | - | - |
| `x` | `NDArray` | - | Dividend array. |
| `divisor` | `NDArray \| number` | - | Divisor array or scalar. |

**Returns:** `NDArray` -- Element-wise quotient `x / divisor`.

```typescript Full Library theme={null}
import * as np from 'numpy-ts';

const a = np.divide(np.array([10, 20, 30]), 10);
// array([1, 2, 3])
```

```typescript Core theme={null}
import { divide, array } from 'numpy-ts/core';

const a = divide(array([10, 20, 30]), 10);
```

***

### true\_divide

Alias for [`divide`](#divide). Returns true (floating-point) division.

```typescript theme={null}
function true_divide(x: NDArray, divisor: NDArray | number): NDArray
```

***

### floor\_divide

Element-wise floor division. Returns the largest integer less than or equal to the division.

```typescript theme={null}
function floor_divide(x: NDArray, divisor: NDArray | number): NDArray
```

| Name | Type | Default | Description |
| - | - | - | - |
| `x` | `NDArray` | - | Dividend array. |
| `divisor` | `NDArray \| number` | - | Divisor array or scalar. |

**Returns:** `NDArray` -- Element-wise floor of `x / divisor`.

```typescript Full Library theme={null}
import * as np from 'numpy-ts';

const a = np.floor_divide(np.array([7, 8, 9]), 3);
// array([2, 2, 3])
```

```typescript Core theme={null}
import { floor_divide, array } from 'numpy-ts/core';

const a = floor_divide(array([7, 8, 9]), 3);
```

***

### mod

Element-wise modulo (remainder of floor division). Equivalent to `remainder`.

```typescript theme={null}
function mod(x: NDArray, divisor: NDArray | number): NDArray
```

| Name | Type | Default | Description |
| - | - | - | - |
| `x` | `NDArray` | - | Dividend array. |
| `divisor` | `NDArray \| number` | - | Divisor array or scalar. |

**Returns:** `NDArray` -- Element-wise remainder.

```typescript Full Library theme={null}
import * as np from 'numpy-ts';

const a = np.mod(np.array([7, 8, 9]), 3);
// array([1, 2, 0])
```

```typescript Core theme={null}
import { mod, array } from 'numpy-ts/core';

const a = mod(array([7, 8, 9]), 3);
```

***

### remainder

Element-wise remainder of division. Equivalent to `mod`.

```typescript theme={null}
function remainder(x: NDArray, y: NDArray | number): NDArray
```

| Name | Type | Default | Description |
| - | - | - | - |
| `x` | `NDArray` | - | Dividend array. |
| `y` | `NDArray \| number` | - | Divisor. |

**Returns:** `NDArray` -- Element-wise remainder of `x1 / x2`.

***

### fmod

Element-wise remainder using C-library `fmod`. The result has the same sign as the dividend.

```typescript theme={null}
function fmod(x1: NDArray, x2: NDArray | number): NDArray
```

| Name | Type | Default | Description |
| - | - | - | - |
| `x1` | `NDArray` | - | Dividend. |
| `x2` | `NDArray \| number` | - | Divisor. |

**Returns:** `NDArray` -- Element-wise C-style remainder.

```typescript Full Library theme={null}
import * as np from 'numpy-ts';

const a = np.fmod(np.array([-3, -2, -1, 1, 2, 3]), 2);
// array([-1, 0, -1, 1, 0, 1])
```

```typescript Core theme={null}
import { fmod, array } from 'numpy-ts/core';

const a = fmod(array([-3, -2, -1, 1, 2, 3]), 2);
```

***

### divmod

Return element-wise quotient and remainder simultaneously.

```typescript theme={null}
function divmod(x: NDArray, y: NDArray | number): [NDArray, NDArray]
```

| Name | Type | Default | Description |
| - | - | - | - |
| `x` | `NDArray` | - | Dividend. |
| `y` | `NDArray \| number` | - | Divisor. |

**Returns:** `[NDArray, NDArray]` -- Tuple of `[quotient, remainder]`.

```typescript Full Library theme={null}
import * as np from 'numpy-ts';

const [q, r] = np.divmod(np.array([7, 8, 9]), 3);
// q = array([2, 2, 3])
// r = array([1, 2, 0])
```

```typescript Core theme={null}
import { divmod, array } from 'numpy-ts/core';

const [q, r] = divmod(array([7, 8, 9]), 3);
```

***

### power

Element-wise exponentiation.

```typescript theme={null}
function power(x: NDArray, exponent: NDArray | number): NDArray
```

| Name | Type | Default | Description |
| - | - | - | - |
| `x` | `NDArray` | - | Base array. |
| `exponent` | `NDArray \| number` | - | Exponent array or scalar. |

**Returns:** `NDArray` -- Element-wise `x1 ** x2`.

```typescript Full Library theme={null}
import * as np from 'numpy-ts';

const a = np.power(np.array([2, 3, 4]), 2);
// array([4, 9, 16])
```

```typescript Core theme={null}
import { power, array } from 'numpy-ts/core';

const a = power(array([2, 3, 4]), 2);
```

***

### pow

Alias for [`power`](#power).

```typescript theme={null}
function pow(x: NDArray, exponent: NDArray | number): NDArray
```

***

### float\_power

Element-wise power, always returning float64 results.

```typescript theme={null}
function float_power(x1: NDArray, x2: NDArray | number): NDArray
```

| Name | Type | Default | Description |
| - | - | - | - |
| `x1` | `NDArray` | - | Base array. |
| `x2` | `NDArray \| number` | - | Exponent. |

**Returns:** `NDArray` -- Element-wise `x1 ** x2` as float64.

***

### sqrt

Return the non-negative square root of each element.

```typescript theme={null}
function sqrt(x: NDArray): NDArray
```

| Name | Type | Default | Description |
| - | - | - | - |
| `x` | `NDArray` | - | Input array. |

**Returns:** `NDArray` -- Element-wise square root.

```typescript Full Library theme={null}
import * as np from 'numpy-ts';

const a = np.sqrt(np.array([1, 4, 9, 16]));
// array([1, 2, 3, 4])
```

```typescript Core theme={null}
import { sqrt, array } from 'numpy-ts/core';

const a = sqrt(array([1, 4, 9, 16]));
```

***

### square

Return the element-wise square.

```typescript theme={null}
function square(x: NDArray): NDArray
```

| Name | Type | Default | Description |
| - | - | - | - |
| `x` | `NDArray` | - | Input array. |

**Returns:** `NDArray` -- Element-wise `x * x`.

```typescript Full Library theme={null}
import * as np from 'numpy-ts';

const a = np.square(np.array([1, 2, 3, 4]));
// array([1, 4, 9, 16])
```

```typescript Core theme={null}
import { square, array } from 'numpy-ts/core';

const a = square(array([1, 2, 3]));
```

***

### cbrt

Return the cube root of each element.

```typescript theme={null}
function cbrt(x: NDArray): NDArray
```

| Name | Type | Default | Description |
| - | - | - | - |
| `x` | `NDArray` | - | Input array. |

**Returns:** `NDArray` -- Element-wise cube root.

```typescript Full Library theme={null}
import * as np from 'numpy-ts';

const a = np.cbrt(np.array([1, 8, 27]));
// array([1, 2, 3])
```

```typescript Core theme={null}
import { cbrt, array } from 'numpy-ts/core';

const a = cbrt(array([1, 8, 27]));
```

***

### absolute

Compute the absolute value element-wise.

```typescript theme={null}
function absolute(x: NDArray): NDArray
```

| Name | Type | Default | Description |
| - | - | - | - |
| `x` | `NDArray` | - | Input array. |

**Returns:** `NDArray` -- Element-wise absolute value.

```typescript Full Library theme={null}
import * as np from 'numpy-ts';

const a = np.absolute(np.array([-1, -2, 3, -4]));
// array([1, 2, 3, 4])
```

```typescript Core theme={null}
import { absolute, array } from 'numpy-ts/core';

const a = absolute(array([-1, -2, 3]));
```

***

### abs

Alias for [`absolute`](#absolute).

```typescript theme={null}
function abs(x: NDArray): NDArray
```

***

### fabs

Compute the absolute value element-wise (float version). Identical to `absolute` for real-valued arrays.

```typescript theme={null}
function fabs(x: NDArray): NDArray
```

| Name | Type | Default | Description |
| - | - | - | - |
| `x` | `NDArray` | - | Input array. |

**Returns:** `NDArray` -- Element-wise absolute value as float.

***

### sign

Return the element-wise sign: `-1` for negative, `0` for zero, `1` for positive.

```typescript theme={null}
function sign(x: NDArray): NDArray
```

| Name | Type | Default | Description |
| - | - | - | - |
| `x` | `NDArray` | - | Input array. |

**Returns:** `NDArray` -- Element-wise sign indication.

```typescript Full Library theme={null}
import * as np from 'numpy-ts';

const a = np.sign(np.array([-5, 0, 3]));
// array([-1, 0, 1])
```

```typescript Core theme={null}
import { sign, array } from 'numpy-ts/core';

const a = sign(array([-5, 0, 3]));
```

***

### negative

Numerical negative, element-wise.

```typescript theme={null}
function negative(x: NDArray): NDArray
```

| Name | Type | Default | Description |
| - | - | - | - |
| `x` | `NDArray` | - | Input array. |

**Returns:** `NDArray` -- Element-wise `-x`.

```typescript Full Library theme={null}
import * as np from 'numpy-ts';

const a = np.negative(np.array([1, -2, 3]));
// array([-1, 2, -3])
```

```typescript Core theme={null}
import { negative, array } from 'numpy-ts/core';

const a = negative(array([1, -2, 3]));
```

***

### positive

Numerical positive, element-wise. Returns a copy.

```typescript theme={null}
function positive(x: NDArray): NDArray
```

| Name | Type | Default | Description |
| - | - | - | - |
| `x` | `NDArray` | - | Input array. |

**Returns:** `NDArray` -- Copy of the input array.

***

### reciprocal

Return the reciprocal (1/x) of each element.

```typescript theme={null}
function reciprocal(x: NDArray): NDArray
```

| Name | Type | Default | Description |
| - | - | - | - |
| `x` | `NDArray` | - | Input array. |

**Returns:** `NDArray` -- Element-wise `1 / x`.

```typescript Full Library theme={null}
import * as np from 'numpy-ts';

const a = np.reciprocal(np.array([1, 2, 4, 10]));
// array([1, 0.5, 0.25, 0.1])
```

```typescript Core theme={null}
import { reciprocal, array } from 'numpy-ts/core';

const a = reciprocal(array([1, 2, 4]));
```

***

### heaviside

Compute the Heaviside step function. Returns `0` where `x1 < 0`, `x2` where `x1 == 0`, and `1` where `x1 > 0`.

```typescript theme={null}
function heaviside(x1: NDArray, x2: NDArray | number): NDArray
```

| Name | Type | Default | Description |
| - | - | - | - |
| `x1` | `NDArray` | - | Input values. |
| `x2` | `NDArray \| number` | - | Value to use where `x1 == 0`. |

**Returns:** `NDArray` -- Element-wise Heaviside step function.

```typescript Full Library theme={null}
import * as np from 'numpy-ts';

const a = np.heaviside(np.array([-1, 0, 1]), 0.5);
// array([0, 0.5, 1])
```

```typescript Core theme={null}
import { heaviside, array } from 'numpy-ts/core';

const a = heaviside(array([-1, 0, 1]), 0.5);
```

***

### gcd

Return the greatest common divisor of `|x1|` and `|x2|`, element-wise.

```typescript theme={null}
function gcd(x1: NDArray, x2: NDArray | number): NDArray
```

| Name | Type | Default | Description |
| - | - | - | - |
| `x1` | `NDArray` | - | First input array. |
| `x2` | `NDArray \| number` | - | Second input array or scalar. |

**Returns:** `NDArray` -- Element-wise GCD.

```typescript Full Library theme={null}
import * as np from 'numpy-ts';

const a = np.gcd(np.array([12, 18, 24], 'int32'), 6);
// array([6, 6, 6])
```

```typescript Core theme={null}
import { gcd, array } from 'numpy-ts/core';

const a = gcd(array([12, 18, 24], 'int32'), 6);
```

***

### lcm

Return the least common multiple of `|x1|` and `|x2|`, element-wise.

```typescript theme={null}
function lcm(x1: NDArray, x2: NDArray | number): NDArray
```

| Name | Type | Default | Description |
| - | - | - | - |
| `x1` | `NDArray` | - | First input array. |
| `x2` | `NDArray \| number` | - | Second input array or scalar. |

**Returns:** `NDArray` -- Element-wise LCM.

```typescript Full Library theme={null}
import * as np from 'numpy-ts';

const a = np.lcm(np.array([4, 6], 'int32'), np.array([6, 8], 'int32'));
// array([12, 24])
```

```typescript Core theme={null}
import { lcm, array } from 'numpy-ts/core';

const a = lcm(array([4, 6], 'int32'), array([6, 8], 'int32'));
```

***

### frexp

Decompose each element into mantissa and exponent, such that `x = mantissa * 2**exponent`.

```typescript theme={null}
function frexp(x: NDArray): [NDArray, NDArray]
```

| Name | Type | Default | Description |
| - | - | - | - |
| `x` | `NDArray` | - | Input array. |

**Returns:** `[NDArray, NDArray]` -- Tuple of `[mantissa, exponent]` arrays.

```typescript Full Library theme={null}
import * as np from 'numpy-ts';

const [m, e] = np.frexp(np.array([1, 2, 4, 8]));
// m = array([0.5, 0.5, 0.5, 0.5])
// e = array([1, 2, 3, 4])
```

```typescript Core theme={null}
import { frexp, array } from 'numpy-ts/core';

const [m, e] = frexp(array([1, 2, 4, 8]));
```

***

### ldexp

Compute `x1 * 2**x2`, element-wise. The inverse of `frexp`.

```typescript theme={null}
function ldexp(x1: NDArray, x2: NDArray | number): NDArray
```

| Name | Type | Default | Description |
| - | - | - | - |
| `x1` | `NDArray` | - | Mantissa array. |
| `x2` | `NDArray \| number` | - | Exponent array or scalar. |

**Returns:** `NDArray` -- Element-wise `x1 * 2**x2`.

```typescript Full Library theme={null}
import * as np from 'numpy-ts';

const a = np.ldexp(np.array([0.5, 0.5, 0.5]), np.array([1, 2, 3]));
// array([1, 2, 4])
```

```typescript Core theme={null}
import { ldexp, array } from 'numpy-ts/core';

const a = ldexp(array([0.5, 0.5, 0.5]), array([1, 2, 3]));
```

***

### modf

Return the fractional and integer parts of each element.

```typescript theme={null}
function modf(x: NDArray): [NDArray, NDArray]
```

| Name | Type | Default | Description |
| - | - | - | - |
| `x` | `NDArray` | - | Input array. |

**Returns:** `[NDArray, NDArray]` -- Tuple of `[fractional_part, integer_part]`.

```typescript Full Library theme={null}
import * as np from 'numpy-ts';

const [frac, intg] = np.modf(np.array([1.5, 2.7, -3.2]));
// frac = array([0.5, 0.7, -0.2])
// intg = array([1, 2, -3])
```

```typescript Core theme={null}
import { modf, array } from 'numpy-ts/core';

const [frac, intg] = modf(array([1.5, 2.7, -3.2]));
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.