sparse
Translation status: English reader-locale proof. Term names and code fences follow the en pack; supporting prose may still be English.
Sparse tensor width sugar mirrors dense tensor sugar with an s prefix.
Syntax: sf32[2, 3] | si64[N] | su32[4]
Category#
collection
Related#
Examples#
radix/corpus/sparsa/decl.fab (canonical · type)#
sparse<T, Figura> declaration shell with all-zero empty.
# =============================================================================
# sparse — sparse<T, Figura> declaration shell with all-zero empty.
# =============================================================================
#
# What this teaches:
# • Sparse tensor declaration — the `sparse<T, Figura>` type syntax and `empty` initializer
# • Identity functions — passing sparse values through functions preserves shape and rank
#
# Common mistakes:
# • confusing sparse<T, Figura> with tensor<T, Figura> — sparse tensors use the same shape syntax but different storage semantics
#
# See also: sparse, tensor, empty
# =============================================================================
# sparse — declaration shell for sparse<T, Figura>
#
# sparse<T, Figura> name ← empty
# fn f(sparse<T, Figura> value) → sparse<T, Figura>
#
# GRAMMAR:
# type :← 'sparse' '<' type ',' shape '>'
#
# EXPECTED OUTPUT:
# Rank and stored non-zero count after round-trip identity call.
fn identity(sparse<f32, [2, 3]> value) → sparse<f32, [2, 3]> {
return value
}
main {
let sparse<f32, [2, 3]> empty ← empty
let sparse<f32, [2, 3]> roundtrip ← identity(empty)
print roundtrip.length(), roundtrip.count_nonzero()
}Expected output:
2 0
radix/corpus/sparsa/sugar.fab (canonical · type)#
Sparse tensor width sugar mirrors dense tensor sugar with an s prefix.
# =============================================================================
# sparse — Sparse tensor width sugar mirrors dense tensor sugar with an s prefix.
# =============================================================================
#
# What this teaches:
# • Sugar syntax — `sf32[2, 3]` as shorthand for `sparse<f32, [2, 3]>`
# • Naming convention — the `s` prefix distinguishes sparse from dense tensor sugar
#
# Common mistakes:
# • forgetting the s prefix when declaring a sparse tensor with sugar syntax — sf32, si64, su32
#
# See also: tensor, numeric type sugar
# =============================================================================
# STYLE: sparse tensor sugar (sf32, si64[N]) — see docs/design/numeric-type-sugar.md
main {
mut sf32[2, 3] grid ← empty
grid.set([0, 1], 4.0)
let _ value ← grid.get([0, 1])
let _ absent ← grid.get([1, 2])
let _ count ← grid.count_nonzero()
let tf32[2, 3] dense ← grid.densify()
print value, absent, count, dense.length()
}Expected output:
4.0 0.0 1 2
radix/corpus/sparsa/la-spelling-reject.fab (supporting · reject)#
The Latin type word sparse in an en file is a wrong-locale error (expect parse error).
# =============================================================================
# sparse — Latin sparse type word in an en file (expect parse error).
# =============================================================================
#
# What this teaches:
# • Locale surfaces are sealed — the en spelling of the sparse tensor type is `sparse`, so the Latin `sparse` is refused in an en file
# • Parse error — `PARSE040:wrong_locale_type` names the word written and the en spelling to use instead
#
# Common mistakes:
# • writing `sparse<f32, [2, 3]>` in an en file — write `sparse<f32, [2, 3]>`
#
# See also: sparse
# =============================================================================
# Latin sparse type word in an en file (expect parse error)
#
# WHY: en and la spellings never mix in one file; the en pack owns `sparse`.
#
# DELIBERATE: this exemplum keeps the Latin spelling (`sparse`) on purpose, to
# pin the PARSE040 `wrong_locale_type` diagnostic for the sparse tensor type.
main {
let sparse<f32, [2, 3]> grid ← empty
print grid.count_nonzero()
}Expected: compilation rejects this example.