渲染zh-Hans

sparsa

Translation status: 简体中文 reader-locale proof. Code fences render through the zh-Hans pipeline; prose is canonical Latin.

Sparse tensor width sugar mirrors dense tensor sugar with an s prefix.

Syntax: sf32[2, 3] | si64[N] | su32[4]

Category#

collection

Examples#

radix/corpus/sparsa/decl.fab (canonical · type)#

sparsa<T, Figura> declaration shell with all-zero vacua.

# =============================================================================
# sparsa — sparsa<T, Figura> declaration shell with all-zero vacua.
# =============================================================================
#
# What this teaches:
#   • Sparse tensor declaration — the `sparsa<T, Figura>` type syntax and `vacua` initializer
#   • Identity functions — passing sparsa values through functions preserves shape and rank
#
# Common mistakes:
#   • confusing sparsa<T, Figura> with tensor<T, Figura> — sparse tensors use the same shape syntax but different storage semantics
#
# See also: sparsa, tensor, vacua
# =============================================================================


# sparsa — declaration shell for sparsa<T, Figura>
#
# sparsa<T, Figura> name ← vacua
# functio f(sparsa<T, Figura> value) → sparsa<T, Figura>
#
# GRAMMAR:
#   type :← 'sparsa' '<' type ',' shape '>'
#
# EXPECTED OUTPUT:
#   Rank and stored non-zero count after round-trip identity call.

fn identity(sparsa<float<f32>, [2, 3]> value)  sparsa<float<f32>, [2, 3]> {
    return value
}

main {
    const sparsa<float<f32>, [2, 3]> empty  vacua
    const sparsa<float<f32>, [2, 3]> roundtrip  identity(empty)

    print roundtrip.longitudo(), roundtrip.nonnihil()
}

Expected output:

2 0

radix/corpus/sparsa/sugar.fab (canonical · type)#

Sparse tensor width sugar mirrors dense tensor sugar with an s prefix.

# =============================================================================
# sparsa — Sparse tensor width sugar mirrors dense tensor sugar with an s prefix.
# =============================================================================
#
# What this teaches:
#   • Sugar syntax — `sf32[2, 3]` as shorthand for `sparsa<fractus<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 {
    var sf32[2, 3] grid  vacua
    grid.ponde([0, 1], 4.0)

    const _ value  grid.accipe([0, 1])
    const _ absent  grid.accipe([1, 2])
    const _ count  grid.nonnihil()

    const tf32[2, 3] dense  grid.densata()
    print value, absent, count, dense.longitudo()
}

Expected output:

4.0 0.0 1 2