# Linear Algebra

> multiply, maxsim and attend treat the index as a sparse operand rather than as a kNN store, and each folds its arithmetic into the graph walk instead of materializing a match list.

`#include <usearch/types.hpp>``#include <usearch/dense_index.hpp>` 3.0.0-rc.1 · cabb6603

### Sparse Multiplication

`multiply`, `maxsim` and `attend` treat the index as a sparse operand rather than as a kNN store, and each folds its arithmetic into the graph walk instead of materializing a match list. All three need logits rather than distances, so they run on an inner-product index alone.

`multiply` is a sparse matrix product: every left row is walked against the index, and every pair the walk scored lands in an arena as one ragged run, in traversal order. `wanted` keeps the first pairs of each row, where zero keeps every pair, and `required` counts how many there were. The run’s Apache Arrow offsets land in the arena beside the keys and products.

```cpp
auto [products, status] = index.on(executor).multiply<float>({left, rows, 768}, arena, {.wanted = 32});
for (std::size_t row = 0; row != rows; ++row)
    std::printf("%zu pairs\n", products.view.offsets[row + 1] - products.view.offsets[row]);
```

### Late Interaction

`maxsim` is late interaction: every query row takes its best match within a key, and those maxima sum to that key’s score. A document is one key holding its token rows, which is what a `values_per_key` above one exists for, and the wanted count is over documents rather than rows. Each query lands its best documents, largest score first, in the same ragged `dense_products_t` run `multiply` answers, and a `wanted` of zero keeps every document the query scored.

```cpp
auto [documents, status] = index.on(executor).maxsim<float>({tokens, rows, 768}, tokens_per_query, 10, arena);
for (std::size_t query = 0; query != rows / tokens_per_query; ++query)
    std::printf("%zu documents\n", documents.view.offsets[query + 1] - documents.view.offsets[query]);
```

### Attention

`attend` is Flash Attention folded into the walk: every key it scores folds into each head’s running maximum, partition and weighted value sum, with no top-k cut, and the outputs land in an arena already divided by the partition, as rows of the scalar type you name. The index stores no values, so a `value_lookup_t` handler resolves every scored key to its value row, as wide as the keys, stored in the index’s own quantization, and read in place from storage the caller keeps until the call is final. A null row, or rows of another scalar type or width than the library reads, refuses the call with `status_code_t::shape_mismatch_k`. It runs on the executor’s threads together. A VRAM walk resolves values on the device, where no host handler runs, so `value_lookup_t::device` names a device function and its state instead, and a host handler with an arena from a VRAM executor is refused with `status_code_t::device_k`. With a `head_count` of 8, row `t * 8 + h` of the queries is head `h` of token `t`, and the heads of one token share its walk. A `carry` of one `usearch_dense_softmax_state_t` per query row, owned by you, folds this branch into another over the same queries, such as a recent window kept out of the index. `score_scale` is conventionally the inverse square root of the head width, and zero derives it from the frozen geometry.

```cpp
auto value_lookup = [&](usearch_key_t const* keys, std::size_t count, usearch_scalar_t, std::size_t,
                        void const** rows) noexcept {
    for (std::size_t position = 0; position != count; ++position) rows[position] = cache.find(keys[position]);
};
std::vector<usearch_dense_softmax_state_t> carry(queries.count);
auto [context, status] = index.on(executor).attend<float, float>(
    queries, value_lookup, arena, {.head_count = 8, .carry = carry.data()});
std::printf("%zu rows of %zu\n", context.written, context.view.columns);
```

## API

### Types

#### `answer_value`

What a final `answer_type_` carries beside its status, `void` for a bare `status_t`.

[types.hpp:203 ↗](https://github.com/unum-cloud/USearch/blob/cabb660355b45ed0d6ade2dc86ca58be01d61e29/cpp/usearch/types.hpp#L203)

```cpp
template <typename answer_type_>
struct answer_value
```

#### `dense_products_t`

The ragged products of [`multiply`](https://www.unum.cloud/docs/usearch/cpp/metrics.md#multiply) or the matches of [`maxsim`](https://www.unum.cloud/docs/usearch/cpp/metrics.md#maxsim), laid out in the arena.

[dense_index.hpp:70 ↗](https://github.com/unum-cloud/USearch/blob/cabb660355b45ed0d6ade2dc86ca58be01d61e29/cpp/usearch/dense_index.hpp#L70)

```cpp
struct dense_products_t
```

#### Fields

| `view` | `usearch_matches_tape_view_t` | Keys, scores and offsets, end to end. |
| - | - | - |
| `written` | `std::size_t` | Pairs written across every row or query. |
| `required` | `std::size_t` | Pairs scored before `wanted` cut any. |

#### `value_lookup_t`

The value rows [`attend`](https://www.unum.cloud/docs/usearch/cpp/metrics.md#attend) gathers: a host handler read only during the call, or a device function.

[dense_index.hpp:93 ↗](https://github.com/unum-cloud/USearch/blob/cabb660355b45ed0d6ade2dc86ca58be01d61e29/cpp/usearch/dense_index.hpp#L93)

```cpp
class value_lookup_t
```

#### `dense_multiply_config_t`

The knobs of [`multiply`](https://www.unum.cloud/docs/usearch/cpp/metrics.md#multiply).

[dense_index.hpp:194 ↗](https://github.com/unum-cloud/USearch/blob/cabb660355b45ed0d6ade2dc86ca58be01d61e29/cpp/usearch/dense_index.hpp#L194)

```cpp
struct dense_multiply_config_t
```

#### Fields

| `wanted` | `std::size_t` | Pairs kept per left row, the first ones its walk scored; zero keeps every pair. |
| - | - | - |
| `expansion_factor` | `std::size_t` | The breadth of the candidates the walk explores; zero derives. |
| `traversal` | `usearch_traversal_t` | A walk or a scan; unknown derives. |
| `predicate` | `predicate_t` | Which candidate keys may be reported; empty admits every key. |
| `progress` | `progress_t` | Hears the rows answered, and may cancel. |
| `stats` | `usearch_traversal_stats_t *` | The traversal telemetry; `nullptr` skips reporting. |

#### `dense_attend_config_t`

The knobs of [`attend`](https://www.unum.cloud/docs/usearch/cpp/metrics.md#attend).

[dense_index.hpp:216 ↗](https://github.com/unum-cloud/USearch/blob/cabb660355b45ed0d6ade2dc86ca58be01d61e29/cpp/usearch/dense_index.hpp#L216)

```cpp
struct dense_attend_config_t
```

#### Fields

| `head_count` | `std::size_t` | Query heads sharing the index's key-value head, dividing the row count; zero means one. |
| - | - | - |
| `carry` | `usearch_dense_softmax_state_t *` | One online-softmax state per query row, passed by the caller; `nullptr` skips it. |
| `expansion_factor` | `std::size_t` | The breadth of the candidates the walk explores; zero derives. |
| `score_scale` | `float` | The logit scale; zero derives it from the frozen geometry. |
| `traversal` | `usearch_traversal_t` | A walk or a scan; unknown derives. |
| `predicate` | `predicate_t` | Which candidate keys may be attended; empty admits every key. |
| `progress` | `progress_t` | Hears the rows answered, and may cancel. |
| `stats` | `usearch_traversal_stats_t *` | The traversal telemetry; `nullptr` skips reporting. |

#### `dense_maxsim_config_t`

The knobs of [`maxsim`](https://www.unum.cloud/docs/usearch/cpp/metrics.md#maxsim).

[dense_index.hpp:244 ↗](https://github.com/unum-cloud/USearch/blob/cabb660355b45ed0d6ade2dc86ca58be01d61e29/cpp/usearch/dense_index.hpp#L244)

```cpp
struct dense_maxsim_config_t
```

#### Fields

| `expansion_factor` | `std::size_t` | The breadth of the candidates the walk explores; zero derives. |
| - | - | - |
| `traversal` | `usearch_traversal_t` | A walk or a scan; unknown derives. |
| `predicate` | `predicate_t` | Which candidate keys may be reported; empty admits every key. |
| `hints` | `usearch_matches_view_t const *` | Matches of an earlier search to start from; `nullptr` starts where searches start. |
| `progress` | `progress_t` | Hears the queries answered, and may cancel. |
| `stats` | `usearch_traversal_stats_t *` | The traversal telemetry; `nullptr` skips reporting. |
