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

# Arrays

> Constructors and the SFArray class: creating, placing, indexing, and reading codes back.

## `sf.array(obj, dtype=None, device="host")`

Build an array by quantizing `obj`. Always copies.

<ParamField path="obj" type="list | nested list | numpy array | SF scalar | SFArray">The data. A 0-d input returns a scalar.</ParamField>
<ParamField path="dtype" type="dtype spec" default="None">Required unless `obj` already holds SF values, in which case it defaults to theirs.</ParamField>
<ParamField path="device" type="str" default="&#x22;host&#x22;">Where to place the result. Only sf16 is accepted on Atreides.</ParamField>

**Events:** overflow, underflow and invalid per element, reported with counts (`overflow in 2 of 100 element(s)`), in catalogue order.

```python theme={null}
sf.array([0.1, -0.25, 0.5], dtype=sf.sf16)
# sfarray([ 0.1000061, -0.25     ,  0.5      ], dtype=sf16)
sf.array([0.5, 0.25])
# SFTypeError: sf.array needs dtype= unless the input already holds SF values
```

## `sf.asarray(obj, dtype=None, device="host")`

Like `sf.array`, but returns `obj` itself when it is already an `SFArray` of that dtype on that device, and otherwise converts (a change of device uploads or downloads; a change of dtype casts).

## `sf.zeros(shape, dtype="sf16", device="host")`

An array of zeros. No events.

## `sf.full(shape, value, dtype=None, device="host")`

An array filled with `value`, quantized through the policy. `dtype` is required unless `value` is an SF scalar.

```python theme={null}
sf.full((3,), 0.25, sf.sf16)     # sfarray([0.25, 0.25, 0.25], dtype=sf16)
```

## `sf.ones(shape, dtype="sf16", device="host")`

Filled with the literal 1, which no width can hold: raises `SFOverflowError` under the default policy and saturates to max under `over="ignore"`. Use `sf.full(shape, sf.info(dt).max, dt)` for the largest value.

## `sf.from_codes(codes, dt, device="host")`

An array from integer codes, validated in the width of `dt`. No quantization, no events.

```python theme={null}
sf.from_codes([16384, -8192], sf.sf16)     # sfarray([ 0.5 , -0.25], dtype=sf16)
```

## `sf.cast(x, dt)` and `arr.astype(dt)`

Convert to another width. Widening is exact; narrowing re-quantizes through the policy (overflow and underflow with counts). Scalars and arrays.

## The `SFArray` class

### Attributes

<ResponseField name="codes" type="numpy int64 array">The integer codes. Read-only.</ResponseField>
<ResponseField name="bits" type="numpy array">The sign-magnitude patterns.</ResponseField>
<ResponseField name="dtype" type="type">The scalar class, e.g. `sf.sf16`.</ResponseField>
<ResponseField name="shape, ndim, size" type="tuple, int, int">As in NumPy.</ResponseField>
<ResponseField name="device" type="str">`"host"` or a device name.</ResponseField>
<ResponseField name="status" type="SFStatus | None">The status of the heavy operation that produced this array; `None` for host arrays.</ResponseField>
<ResponseField name="T" type="SFArray">Transpose (a view).</ResponseField>

### Methods

| Method | Returns |
| - | - |
| `numpy()` | float64 NumPy array of the values (downloads first if on a device) |
| `tolist()` | nested Python lists of floats |
| `astype(dt)` | cast |
| `to(device)` | the array on `device` (`"host"` or a name) |
| `copy()` | a copy |
| `reshape(*shape)`, `flatten()`, `transpose(*axes)` | reshaped views or copies as NumPy would |
| `sum(axis=None)` | saturating sequential sum |
| `max(axis=None)`, `min(axis=None)` | exact selection |
| `argmax(axis=None)`, `argmin(axis=None)` | NumPy int indices |
| `any()`, `all()` | on the codes |

### Indexing

`a[idx]` accepts everything NumPy accepts for reading: integers, slices, boolean and integer arrays, `None`. A scalar index returns an SF scalar; anything else returns an `SFArray` of the same dtype and device. Assignment is not supported: arrays are immutable.

### Operators

The scalar operators, element by element, with NumPy broadcasting; comparisons return NumPy boolean arrays. `@` is `sf.matmul`. See [Elementwise operations](/arrays/elementwise).


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