渲染zh-Hant

稀疏

Translation status: 繁體中文 reader-locale proof. Term names and code fences follow the zh-Hant 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

Examples#

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

稀疏<T, Figura> declaration shell with all-zero 空集.

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


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

fn identity(稀疏<f32, [2, 3]> value) → 稀疏<f32, [2, 3]> {
    return value
}

main {
    const 稀疏<f32, [2, 3]> empty ← empty
    const 稀疏<f32, [2, 3]> roundtrip ← identity(empty)

    print roundtrip.length(), roundtrip.nonzero_count()
}

Expected output:

2 0

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

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

# =============================================================================
# 稀疏 — Sparse 張量 width sugar mirrors dense 張量 sugar with an s prefix.
# =============================================================================
#
# What this teaches:
#   • Sugar syntax — `sf32[2, 3]` as shorthand for `稀疏<f32, [2, 3]>`
#   • Naming convention — the `s` prefix distinguishes sparse from dense 張量 sugar
#
# Common mistakes:
#   • forgetting the s prefix when declaring a sparse 張量 with sugar syntax — sf32, si64, su32
#
# See also: 張量, numeric type sugar
# =============================================================================

# STYLE: sparse 張量 sugar (sf32, si64[N]) — see docs/design/numeric-type-sugar.md

main {
    var sf32[2, 3] grid ← empty
    grid.set([0, 1], 4.0)

    const _ value ← grid.get([0, 1])
    const _ absent ← grid.get([1, 2])
    const _ count ← grid.nonzero_count()

    const tf32[2, 3] dense ← grid.densify()
    print value, absent, count, dense.length()
}

Expected output:

4.0 0.0 1 2