Spenso Python API Documentation
A comprehensive Python tensor library for symbolic and numerical tensor computations, with a focus on physics applications.
Overview
The Spenso Python API provides powerful tools for:
- Tensor Algebra: Dense and sparse tensors with flexible data types
- Symbolic Computation: Integration with Symbolica for tensors with symbolic expressions
- Network Operations: Tensor networks for optimized computation graphs
- Physics Applications: Built-in support for HEP tensors (gamma matrices, color structures, etc.)
- Performance: Compiled evaluators for high-speed numerical computation
Installation
# Spenso is available as part of the symbolica.community.spenso module
Quick Start
# Create a 3D Minkowski representation
=
# Create tensor structure with indices
=
=
=
# Create dense tensor (3x3 identity matrix)
=
=
# Create symbolic tensors
, =
=
=
Core Classes
Tensor
The main tensor class supporting both dense and sparse storage with numerical or symbolic data.
# Dense tensor creation
=
= # 3x3 tensor
=
=
# Sparse tensor creation
=
= 1.0
= 2.0
# Symbolic tensors
, =
=
=
=
=
# Element access
= 10.0 # Set element
# Convert to sparse storage
Representations
Define the mathematical properties of tensor indices for group theory applications.
# Standard physics representations
= # 4D Euclidean space
= # 4D Minkowski space
= # SU(3) fundamental
= # SU(3) adjoint
= # Dirac bispinor
# Custom representations
=
# Create slots (representation + index)
=
# Metric tensors
= # Metric tensor g_μν
= # Flat metric η_μν
= # Identity δ_μν
TensorName
Named tensor functions with mathematical properties like symmetry.
# Create tensor names
= # Basic tensor
= # Symmetric tensor
= # Antisymmetric tensor
# Predefined physics tensors
= # Dirac gamma matrices
= # Pauli matrices
= # SU(N) structure constants
# Use with indices
=
=
=
= # Creates TensorIndices T(μ,ν)
TensorIndices and TensorStructure
Define tensor shapes and index structures.
# TensorIndices: For tensors with specific abstract indices
=
=
=
# TensorStructure: For tensor templates without specific indices
=
# Named structures
=
=
# Convert structure to indices
= # Assign indices a, b
# Create symbolic expressions
= # T(μ,ν)
# With additional arguments
=
= # T(x; μ,ν)
TensorNetwork
Computational graphs for optimized tensor operations.
# TensorIndices: For tensors with specific abstract indices
=
=
=
=
=
= *
=
# Controlled execution
# Only scalar operations
=
TensorLibrary
Registry for reusable tensor definitions.
# Create library
=
# Register tensors
=
=
=
=
# Access registered tensors
=
# HEP library with standard physics tensors
=
=
Evaluation and Performance
Symbolic Evaluation
# Create symbolic tensor
, =
=
=
# Create evaluator
=
# Evaluate for multiple parameter sets
= # x,y values
=
Compiled Evaluation
# Compile for maximum performance
=
# Use compiled evaluator for complex inputs
=
=
Physics Applications
High Energy Physics
# Load HEP library
=
# Standard HEP tensors
= # Dirac gamma matrices
= # γ₅ matrix
= # Pauli matrices
= # SU(N) structure constants
= # SU(N) generators
# Representations
= # Lorentz indices
= # Dirac spinor
# Build physics expressions
=
=
=
=
# Gamma matrix trace: Tr(γᵘγᵥ)
= *
Tensor Contractions
=
=
=
=
# Metric contraction: gᵘᵛ gᵤᵥ = 4
=
= * # Repeated indices contract
# Network evaluation
=
= # Should give 4 for 4D
Best Practices
- Use appropriate storage: Sparse for mostly-zero tensors, dense for general data
- Name your tensors: Required for symbolic manipulation and library registration
- Choose representations carefully: Self-dual vs dualizable affects contraction rules
- Optimize evaluation: Use compiled evaluators for performance-critical code
- Leverage libraries: Use
TensorLibrary.hep_lib()for standard physics tensors - Cook your indices: Indices have to be symbols or numbers. To that end, use the cook_indices function from idenso.