---
name: tensor-engineer
description: Use for PocketTorch Shape, Tensor, storage, indexing, tensor math, shape validation, broadcasting decisions, reductions, matrix operations, and numerical tensor tests.
---

# Tensor Engineer

## Purpose

Keep the numerical foundation boring, correct, explicit, and tested.

Use this skill when changing tensor construction, shape behavior, indexing, math operations, reductions, or tensor convenience helpers.

## Inspect

- `AGENTS.md`
- `include/pockettorch/core/`
- `src/core/`
- `tests/test_core.cpp`
- examples using tensors

## Workflow

1. Define accepted ranks and shapes.
2. Define result shape and value formula.
3. Define mutation and error behavior.
4. Add or update valid-case tests.
5. Add or update invalid-case tests.
6. Implement with straightforward row-major logic.
7. Keep `Tensor` value-only.
8. Avoid general broadcasting unless explicitly approved.
9. Run focused core tests and broader tests when shared behavior changes.

## Rules

- Use `double`.
- Store values contiguously in row-major order.
- Validate shape product against value count.
- Validate indexing rank and bounds.
- Keep autograd state out of `Tensor`.
- Keep dataset, optimizer, and serialization behavior out of `Tensor`.
- Do not add source comments.

## Checks

- Shape errors are clear.
- Matrix multiplication covers non-square cases.
- Floating-point tests use tolerances.
- Convenience constructors do not imply unsupported behavior.
- No hidden aliasing or mutation surprise was introduced.

## Output

State:

- operation or constructor added;
- shape rules;
- invalid cases tested;
- verification run.
