# `Spatex.Input`
[🔗](https://github.com/CountlinkX-Solutions/spatex/blob/v0.2.1/lib/spatex/input.ex#L1)

The input contract shared by every Spatex function.

User data is normalized in Elixir, before any NIF call, into flat native-endian
binaries (`f64` for numeric vectors and points, `u32` for index vectors). All
validation errors raise `ArgumentError` naming the argument and the offending index.

## Accepted inputs

| Input | `to_f64_binary/2` | `to_points_binary/1` |
|---|---|---|
| List of numbers (integers are coerced) | yes | `[{x, y}]` or `[[x, y]]` |
| `Nx.Tensor` (optional dep) | rank 1, any numeric type | `{n, 2}` tensor |
| `Explorer.Series` (optional dep) | numeric dtypes | `{xs, ys}` pair |
| Packed `{:f64, binary}` | passed through (validated) | - |

Labels (regimes, partitions) go through `to_labels/2`: a list of any terms, a rank-1 `Nx`
tensor or an `Explorer.Series` of any dtype. Panels and other `n x t` matrices go through
`to_matrix/2`: a list of numeric rows, a rank-2 `Nx` tensor or an `Explorer.DataFrame` (its
numeric columns in order).

`Nx` and `Explorer` are optional dependencies; they are only touched when the user
passes one of their structs, so Spatex compiles and runs without them.

## Options (`to_f64_binary/2`)

  * `:name` - argument name used in error messages (default `"values"`).
  * `:allow_nan` - accept NaN and infinities (default `false`).
  * `:allow_nil` - for `Explorer.Series`, map `nil` to NaN instead of raising
    (default `false`). The resulting NaN is still subject to `:allow_nan`.
  * `:length` - expected length; a mismatch raises.

## NaN and infinity

Elixir floats cannot hold NaN or infinity, so those values are written to binaries
from raw bit patterns and decoded by `from_f64_binary/1` as the atoms `:nan`,
`:infinity` and `:neg_infinity`. Lists may contain those atoms when `allow_nan: true`.

# `f64_result`

```elixir
@type f64_result() :: {binary(), non_neg_integer()}
```

# `special`

```elixir
@type special() :: :nan | :infinity | :neg_infinity
```

# `add_simulations`

```elixir
@spec add_simulations(list() | Nx.Tensor.t(), list() | Nx.Tensor.t()) ::
  list() | Nx.Tensor.t()
```

Element-wise sum of two simulation vectors of the same form (two lists or two tensors).

# `from_f64_binary`

```elixir
@spec from_f64_binary(binary()) :: [float() | special()]
```

Decodes a native-endian `f64` binary to a list.

NaN and infinity bit patterns decode to `:nan`, `:infinity` and `:neg_infinity`
because Elixir floats cannot represent them.

## Examples

    iex> Spatex.Input.from_f64_binary(<<1.5::float-64-native>>)
    [1.5]

# `keep_flag`

```elixir
@spec keep_flag(boolean() | :tensor) :: boolean()
```

The boolean the native side takes for a validated `:keep_simulations` value: `:tensor` keeps
the draws like `true`.

# `keep_simulations!`

```elixir
@spec keep_simulations!(term(), boolean()) :: boolean() | :tensor
```

Validates a `:keep_simulations` option value: `true`, `false` or `:tensor`.

`:tensor` needs the optional `:nx` dependency; the second argument (default: whether `Nx` is
loaded) lets that check be exercised without it. Returns the value.

# `require_nx!`

```elixir
@spec require_nx!(String.t(), boolean()) :: :ok
```

Raises an `ArgumentError` naming the optional `:nx` dependency unless `Nx` is loaded.

`what` names the function that needs it; the second argument (default: whether `Nx` is
loaded) lets the failure be exercised without removing `Nx`.

# `simulation_vectors`

```elixir
@spec simulation_vectors(binary(), true | :tensor, pos_integer(), non_neg_integer()) ::
  [
    list() | Nx.Tensor.t()
  ]
```

Splits a binary of `count` equal simulation vectors of `len` draws into `count` values, each
a list (`true`) or a `{len}` tensor (`:tensor`).

# `simulations`

```elixir
@spec simulations(binary(), true | :tensor, tuple()) :: list() | Nx.Tensor.t()
```

Decodes a simulation binary into the form `keep` asks for: nested lists (`true`) or an
`Nx.Tensor` of type `{:f, 64}` built straight from the binary (`:tensor`), both of `shape`.

The list form nests by `shape`, so `{n, p}` is `n` lists of `p` values.

# `to_f64_binary`

```elixir
@spec to_f64_binary(term(), keyword()) :: f64_result()
```

Normalizes a numeric vector to a native-endian `f64` binary, returning `{binary, n}`.

## Examples

    iex> {bin, 2} = Spatex.Input.to_f64_binary([1, 2.5])
    iex> Spatex.Input.from_f64_binary(bin)
    [1.0, 2.5]

# `to_index_binary`

```elixir
@spec to_index_binary(term(), :u32, keyword()) :: {binary(), non_neg_integer()}
```

Normalizes a vector of non-negative integer indices to a native-endian `u32` binary.

## Examples

    iex> Spatex.Input.to_index_binary([0, 2], :u32) |> elem(1)
    2

# `to_labels`

```elixir
@spec to_labels(term(), keyword()) :: [term()]
```

Normalizes per-observation labels (regimes, partitions, groups) to a list of terms.

Accepts a list of any terms, a rank-1 `Nx.Tensor` or an `Explorer.Series` of any dtype
(`nil` raises). Options: `:name` (default `"labels"`) and `:length`, the expected count.

# `to_matrix`

```elixir
@spec to_matrix(term(), keyword()) :: [[float()]]
```

Normalizes an `n x t` matrix (one row per observation) to a list of float rows.

Accepts a list of numeric rows, a rank-2 `Nx.Tensor` or an `Explorer.DataFrame` whose numeric
columns are taken in order (one column per period for panels). Options: `:name` (default
`"matrix"`) and `:rows`, the expected row count.

# `to_points_binary`

```elixir
@spec to_points_binary(term()) :: f64_result()
```

Normalizes points to an interleaved `x0, y0, x1, y1, ...` `f64` binary, returning `{binary, n}`.

Accepts `[{x, y}]`, `[[x, y]]`, an `{n, 2}` `Nx.Tensor`, or a pair `{xs, ys}` of
anything `to_f64_binary/2` accepts. Non-finite coordinates are rejected.

## Examples

    iex> {bin, 2} = Spatex.Input.to_points_binary([{0, 1}, {2, 3}])
    iex> Spatex.Input.from_f64_binary(bin)
    [0.0, 1.0, 2.0, 3.0]

---

*Consult [api-reference.md](api-reference.md) for complete listing*
