Skip to content

plurigrid/asi / ies/music-topos/.codex/skills/sheaf-theoretic-coordination

sheaf-theoretic-coordination

Verified

FileKindSize
SKILL.mdskill md2.6 KB
cellular_sheaf.jlother4.9 KB
sheaf_laplacian.jlother3.9 KB

cellular_sheaf.jl

View on GitHub
"""
Cellular Sheaf Construction and Cohomology

Implements cellular sheaves over cell complexes with restriction maps,
sheaf cohomology computation, and obstruction detection.
"""

module CellularSheafModule

export CellularSheaf, add_restriction!, compute_coboundary
export sheaf_cohomology, check_global_consistency

using LinearAlgebra
using SparseArrays

"""
Cellular Sheaf over a cell complex
"""
mutable struct CellularSheaf
    base_complex::Any  # Underlying cell complex (graph, simplicial complex, etc.)
    stalk_dim::Int  # Dimension of vector space at each cell

    # Restriction maps: edge -> linear map between stalks
    restrictions::Dict{Tuple{Int, Int}, Matrix{Float64}}

    # Stalks: cell_id -> vector space dimension
    stalks::Dict{Int, Int}

    function CellularSheaf(base_complex, stalk_dim::Int)
        new(base_complex, stalk_dim, Dict{Tuple{Int, Int}, Matrix{Float64}}(), Dict{Int, Int}())
    end
end

"""
Add restriction map for edge (source, target)
"""
function add_restriction!(sheaf::CellularSheaf, source::Int, target::Int, restriction::Matrix{Float64})
    @assert size(restriction) == (sheaf.stalk_dim, sheaf.stalk_dim) "Restriction map dimension mismatch"

    # Store restriction map
    sheaf.restrictions[(source, target)] = restriction

    # Register stalks
    sheaf.stalks[source] = sheaf.stalk_dim
    sheaf.stalks[target] = sheaf.stalk_dim

    return sheaf
end

"""
Compute coboundary map δ^k: C^k(sheaf) -> C^{k+1}(sheaf)
"""
function compute_coboundary(sheaf::CellularSheaf, dimension::Int)
    # For simplicity, implement for dimension 0 (vertices -> edges)
    if dimension == 0
        return vertex_to_edge_coboundary(sheaf)
    elseif dimension == 1
        return edge_to_face_coboundary(sheaf)
    else
        error("Higher-dimensional coboundary not yet implemented")
    end
end

"""
Coboundary map from vertex cochains to edge cochains
"""
function vertex_to_edge_coboundary(sheaf::CellularSheaf)
    n_vertices = length(sheaf.stalks)
    n_edges = length(sheaf.restrictions)
    d = sheaf.stalk_dim

    # Each vertex contributes d dimensions, each edge contributes d dimensions
    n_vertex_cochain = n_vertices * d
    n_edge_cochain = n_edges * d

    I = Int[]
    J = Int[]
    V = Float64[]

    edge_idx = 0
    for ((source, target), restriction) in sheaf.restrictions
        edge_idx += 1

        # Edge cochain = restriction(target) - restriction(source)
        # δ^0(c)(e) = F_e(c(target)) - c(source)

        for i in 1:d
            for j in 1:d
                # Contribution from source (negative)
                push!(I, (edge_idx - 1) * d + i)
                push!(J, (source - 1) * d + j)
                push!(V, -restriction[i, j])

                # Contribution from target (positive, identity)
                if i == j
                    push!(I, (edge_idx - 1) * d + i)
                    push!(J, (target - 1) * d + j)
                    push!(V, 1.0)
                end
            end
        end
    end

    return sparse(I, J, V, n_edge_cochain, n_vertex_cochain)
end

"""
Coboundary map from edge cochains to face cochains (stub)
"""
function edge_to_face_coboundary(sheaf::CellularSheaf)
    # Would implement for 2-cells in simplicial/CW complex
    # Requires knowledge of face structure
    return spzeros(0, 0)
end

"""
Compute sheaf cohomology groups H^k(sheaf)
"""
function sheaf_cohomology(sheaf::CellularSheaf, dimension::Int)
    if dimension == 0
        # H^0 = ker(δ^0)
        δ0 = compute_coboundary(sheaf, 0)
        return nullspace(Matrix(δ0))
    elseif dimension == 1
        # H^1 = ker(δ^1) / im(δ^0)
        δ0 = compute_coboundary(sheaf, 0)
        δ1 = compute_coboundary(sheaf, 1)

        ker_δ1 = nullspace(Matrix(δ1))
        im_δ0 = Matrix(δ0)

        # Quotient space (simplified computation)
        # Full implementation would use Smith normal form
        return ker_δ1
    else
        error("Higher cohomology not yet implemented")
    end
end

"""
Check global consistency of sheaf section
"""
function check_global_consistency(sheaf::CellularSheaf, section::Dict{Int, Vector{Float64}})
    inconsistency = 0.0

    for ((source, target), restriction) in sheaf.restrictions
        if haskey(section, source) && haskey(section, target)
            # Check if F_e(s(target)) ≈ s(source)
            restricted = restriction * section[target]
            diff = norm(restricted - section[source])
            inconsistency += diff^2
        end
    end

    return sqrt(inconsistency)
end

"""
Create random orthogonal restriction map
"""
function random_orthogonal_matrix(n::Int)
    # QR decomposition of random matrix
    Q, _ = qr(randn(n, n))
    return Matrix(Q)
end

"""
Create identity restriction (trivial sheaf)
"""
function identity_restriction(n::Int)
    return Matrix{Float64}(I, n, n)
end

"""
Create rotation restriction (for circular/angular data)
"""
function rotation_restriction(angle::Float64)
    return [cos(angle) -sin(angle); sin(angle) cos(angle)]
end

end  # module
slack-gif-creatoranthropics/skillsKnowledge and utilities for creating animated GIFs optimized for Slack. Provides constraints, validation tools, and animation concepts. Use when users request animated GIFs for Slack like "make me a GIF of X doing Y for Slack."Stars: 198kUpdated Claude CodeCodexVerifiedInstall
ponytailDietrichGebert/ponytailLazy senior dev mode for any coding task: the smallest change that fully solves it, and a reply a busy human understands in one read.Stars: 194kUpdated Claude CodeCodexVerifiedInstall
ponytail-debtDietrichGebert/ponytailHarvest every shortcut: comment into one debt ledger, so deferrals get tracked instead of forgotten. One-shot report.Stars: 194kUpdated Claude CodeCodexVerifiedInstall
ponytail-debtDietrichGebert/ponytailList every `shortcut:` comment (and older `ponytail:` ones) as a debt ledger. One-shot report, changes nothing. Use for "ponytail debt", "what did ponytail defer", "list the shortcuts", /ponytail-debt.Stars: 194kUpdated Claude CodeCodexVerifiedInstall