{"id":51470674,"url":"https://github.com/iraikov/array-morphisms","last_synced_at":"2026-07-06T17:01:25.328Z","repository":{"id":367780361,"uuid":"1282288669","full_name":"iraikov/array-morphisms","owner":"iraikov","description":"Structure-preserving transformations between arrays","archived":false,"fork":false,"pushed_at":"2026-06-27T15:19:07.000Z","size":186,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2026-06-27T17:12:56.202Z","etag":null,"topics":["array-fusion","array-manipulation","array-methods","chicken-scheme","chicken-scheme-eggs","gradients","mathematics-of-arrays","moa","multi-dimensional-arrays","numerical-computation","scheme-language","single-static-assignment","ssa"],"latest_commit_sha":null,"homepage":"","language":"Scheme","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/iraikov.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2026-06-27T15:17:47.000Z","updated_at":"2026-06-27T15:45:47.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/iraikov/array-morphisms","commit_stats":null,"previous_names":["iraikov/array-morphisms"],"tags_count":null,"template":false,"template_full_name":null,"purl":"pkg:github/iraikov/array-morphisms","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iraikov%2Farray-morphisms","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iraikov%2Farray-morphisms/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iraikov%2Farray-morphisms/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iraikov%2Farray-morphisms/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/iraikov","download_url":"https://codeload.github.com/iraikov/array-morphisms/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iraikov%2Farray-morphisms/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":35199360,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-07-06T02:00:07.184Z","response_time":106,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["array-fusion","array-manipulation","array-methods","chicken-scheme","chicken-scheme-eggs","gradients","mathematics-of-arrays","moa","multi-dimensional-arrays","numerical-computation","scheme-language","single-static-assignment","ssa"],"created_at":"2026-07-06T17:01:24.527Z","updated_at":"2026-07-06T17:01:25.309Z","avatar_url":"https://github.com/iraikov.png","language":"Scheme","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Array Morphisms\n\n[![Chicken Scheme](https://img.shields.io/badge/Chicken-Scheme-orange.svg)](https://call-cc.org/)\n\nA unified backend for numerical computing in Chicken Scheme, combining fusion-based lazy evaluation, Mathematics of Arrays (MoA) index transformations, and automatic memory reuse.\n\n## Features\n\n- **Lazy Evaluation**: Operations build expression trees that are materialized on demand\n- **Zero-Copy Views**: Structural operations (reshape, transpose, slice) via MoA affine index functions\n- **Memory Reuse**: Automatic buffer planning with graph coloring for optimal allocation\n- **BLAS Integration**: Transparent dispatch to optimized linear algebra kernels\n- **Type Safety**: Multiple element types (f64, f32, s64, s32, u32, u64)\n- **Category-Theoretic Foundation**: Array morphisms as structure-preserving transformations\n\n## Installation\n\n```bash\n# Install array-morphisms\nchicken-install array-morphisms\n```\n\nOr clone from GitHub:\n\n```bash\ngit clone https://github.com/iraikov/array-morphisms.git\ncd array-morphisms\nchicken-install .\n```\n\n## Quick Start\n\n```scheme\n(import array-morphisms-core\n        array-morphisms-basic-ops\n        array-morphisms-structural-ops\n        array-morphisms-realization)\n\n;; Create concrete arrays\n(define x (morph-from-list '(1.0 2.0 3.0 4.0 5.0) '(5) 'f64))\n(define y (morph-from-list '(2.0 4.0 6.0 8.0 10.0) '(5) 'f64))\n\n;; Build lazy computation (no allocation yet!)\n(define z (morph+ (morph-map (lambda (a) (* a a)) x) y))\n\n;; Materialize when needed\n(define result (realize z))  ; Returns concrete array\n(morph-\u003elist result)          ; Convert to Scheme list\n\n;; Chain structural operations (all zero-copy views)\n(define matrix (morph-reshape x #(2 3)))   ; Reshape to 2x3\n(define transposed (morph-transpose matrix)) ; Transpose\n(define slice (morph-slice transposed '(0 0) '(2 2))) ; Extract submatrix\n```\n\n## Core Concepts\n\n### Morphisms vs Arrays\n\nIn array-morphisms, computation is represented as **morphisms** - structure-preserving transformations between arrays. There are two types:\n\n- **Concrete Arrays**: Materialized data with shape, dtype, and strides\n- **Abstract Morphisms**: Deferred computations represented as expression trees\n\n```scheme\n;; Concrete array - data is stored\n(define concrete (morph-from-list '(1.0 2.0 3.0) '(3) 'f64))\n\n;; Abstract morphism - represents computation\n(define abstract (morph+ concrete concrete))\n\n;; Realization materializes the morphism\n(define result (realize abstract))  ; Now concrete\n```\n\n### Index Functions\n\nArray morphisms use **index functions** to describe transformations algebraically:\n\n- **Affine Index Functions**: Pure transformations (reshape, transpose, slice)\n- **Compute Index Functions**: Element-wise arithmetic operations\n- **Window Index Functions**: Convolution and pooling operations\n- **Reduction Index Functions**: Aggregate operations (sum, mean, max)\n\n```scheme\n;; Affine: stride-2 slice\n(define downsampled (morph-slice x '(0) '(16) 2))\n\n;; Compute: element-wise multiplication\n(define scaled (morph* x (morph-from-list '(2.0) '(1) 'f64)))\n\n;; Reduction: sum all elements\n(define total (morph-reduce 'sum x))\n```\n\n### Zero-Copy Structural Operations\n\nStructural operations manipulate array views without copying data:\n\n```scheme\n(define x (morph-from-list '(0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0) '(8) 'f64))\n\n;; Non-contiguous slice (stride 2)\n(define strided (morph-slice x '(0) '(8) 2))  ; (0.0 2.0 4.0 6.0)\n\n;; Reshape works even on non-contiguous views\n(define as-2x2 (morph-reshape strided #(2 2)))  ; ((0.0 2.0) (4.0 6.0))\n\n;; Transpose\n(define transposed (morph-transpose as-2x2 '(1 0)))  ; ((0.0 4.0) (2.0 6.0))\n```\n\n## Basic Operations\n\n### Array Creation\n\n```scheme\n(morph-from-list '(1.0 2.0 3.0) '(3) 'f64)  ; From list\n(make-morphism data-vector shape 'f64)        ; From typed vector\n```\n\n### Arithmetic\n\n```scheme\n(morph+ a b)    (morph- a b)    (morph* a b)    (morph/ a b)\n(morph-pow a b)\n\n;; Unary operations\n(morph-negate a)  (morph-abs a)  (morph-sqrt a)\n(morph-exp a)     (morph-log a)  (morph-sin a)   (morph-cos a)\n```\n\n### Comparison\n\n```scheme\n(morph\u003e a b)  (morph\u003c a b)  (morph= a b)  (morph\u003e= a b)  (morph\u003c= a b)\n;; Returns 1.0 for true, 0.0 for false\n```\n\n### Structural Operations\n\n```scheme\n;; Reshape (supports -1 for automatic dimension inference)\n(morph-reshape m #(2 3))      ; Reshape to 2x3\n(morph-reshape m '(2 -1))     ; Infer second dimension\n\n;; Transpose\n(morph-transpose m)           ; Reverse all axes\n(morph-transpose m '(1 0))    ; 2D transpose\n(morph-transpose m '(0 2 1))  ; Swap last two axes\n\n;; Slice\n(morph-slice m '(0) '(10))      ; Elements 0 to 9\n(morph-slice m '(0) '(10) 2)    ; Every other element\n\n;; Stack/Concat\n(morph-stack (list m1 m2 m3) 0)   ; Stack along new axis\n(morph-concat (list m1 m2) 0)      ; Concatenate along existing axis\n```\n\n### Functional Operations\n\n```scheme\n;; Map applies function element-wise\n(morph-map (lambda (x) (* x x)) arr)\n\n;; Reduce aggregates over specified axes\n(morph-reduce 'sum arr)           ; Sum all elements\n(morph-reduce 'mean arr '(0))     ; Mean along axis 0\n(morph-reduce 'max arr '(1 2))    ; Max along axes 1 and 2\n\n;; Fold and scan (batch operations)\n(batch-fold fn init batched-m)\n(batch-scan fn init batched-m)\n```\n\n## Memory Reuse with Execution Context\n\nFor repeated computations, use execution contexts to enable buffer reuse:\n\n```scheme\n(import array-morphisms-context)\n\n;; Create context for memory planning\n(define ctx (make-morphism-context))\n\n;; Trace phase: record allocations\n(realize/ctx ctx morphism)\n(finalize-context! ctx)\n\n;; Replay phase: reuse buffers\n(reset-context! ctx)\n(realize/ctx ctx morphism)  ; Uses pre-allocated buffers\n```\n\n## Type System\n\n| Type    | Description           | Size     |\n|---------|-----------------------|----------|\n| `'f64`  | Double float          | 64-bit   |\n| `'f32`  | Single float          | 32-bit   |\n| `'s64`  | 64-bit signed int     | 64-bit   |\n| `'s32`  | 32-bit signed int     | 32-bit   |\n| `'u64`  | 64-bit unsigned int   | 64-bit   |\n| `'u32`  | 32-bit unsigned int   | 32-bit   |\n\nType promotion rules:\n- Mixed operations promote to the higher precision type\n- Transcendental functions promote integers to floating point\n- Reductions preserve dtype (mean promotes to float)\n\n## Performance Tips\n\n1. **Laziness is your friend** - Build expression trees, materialize once\n2. **Zero-copy views** - Structural operations are essentially free\n3. **Use contexts** - For repeated computations, enable buffer reuse\n4. **Batch operations** - Process multiple arrays together efficiently\n\n```scheme\n;; Good: Chain operations, materialize once\n(define result (realize (morph-sqrt (morph+ (morph* a b) c))))\n\n;; Good: Use contexts for repeated inference\n(define ctx (make-morphism-context))\n(realize/ctx ctx model-output)  ; First run traces\n(finalize-context! ctx)\n;; ... later ...\n(realize/ctx ctx model-output)  ; Reuses buffers\n\n;; Bad: Materializing intermediate results\n(define temp1 (realize (morph* a b)))\n(define temp2 (realize (morph+ temp1 c)))\n(define result (realize (morph-sqrt temp2)))\n```\n\n## Comparison with Fusion Arrays\n\n| Feature              | Fusion Arrays        | Array Morphisms                 |\n|----------------------|----------------------|----------------------------------|\n| Core abstraction     | Fusion arrays        | Array morphisms                  |\n| Structural ops       | Copy on non-contiguous | Zero-copy via MoA              |\n| Memory reuse         | Manual               | Automatic (context-based)        |\n| Index functions      | Hidden               | First-class, composable          |\n| Batch operations     | Limited              | First-class combinators          |\n| BLAS integration     | No                   | Yes (GEMM, GEMV, DOT)            |\n\n## Examples\n\n### Layer Normalization\n\n```scheme\n(define (layer-norm x eps)\n  (let* ((mean (morph-reduce 'mean x '(0)))\n         (centered (morph- x mean))\n         (variance (morph-reduce 'mean \n                               (morph* centered centered) \n                               '(0)))\n         (std (morph-sqrt (morph+ variance \n                                  (morph-from-list \n                                    (make-list (vector-ref \n                                                (get-morphism-shape x) 1) \n                                               eps)\n                                    (list (vector-ref \n                                           (get-morphism-shape x) 1))\n                                    'f64)))))\n    (morph/ centered std)))\n```\n\n### Signal Downsampling Pipeline\n\n```scheme\n(define (downsample-pipeline signal)\n  ;; Polyphase downsampling via composed slices\n  (let* ((even (morph-slice signal '(0) (get-morphism-shape signal) 2))\n         (quarter (morph-slice even '(0) (get-morphism-shape even) 2)))\n    ;; Both slices are zero-copy views\n    ;; Final realization computes in single pass\n    (realize quarter)))\n```\n\n### Batched Matrix Operations\n\n```scheme\n(import array-morphisms-batch-ops)\n\n;; Stack matrices into batch\n(define batch (morph-stack (list m1 m2 m3) 0))\n\n;; Apply operation to each batch element\n(define doubled (batch-map \n                   (lambda (m) (morph-map (lambda (x) (* x 2)) m))\n                   batch))\n\n;; Reduce across batch dimension\n(define summed (batch-reduce 'sum batch))\n```\n\n## Requirements\n\n- CHICKEN Scheme 5.0+\n- Dependencies: datatype, matchable, srfi-1, srfi-4, srfi-69\n- Optional: BLAS library for accelerated linear algebra\n\n## API Reference\n\nSee [CHICKEN Scheme Wiki](https://wiki.call-cc.org/) for full documentation.\n\nKey modules:\n- `array-morphisms-core` - Core data types and utilities\n- `array-morphisms-basic-ops` - Arithmetic and transcendental operations\n- `array-morphisms-structural-ops` - Reshape, transpose, slice, stack\n- `array-morphisms-realization` - Materialization and execution\n- `array-morphisms-context` - Memory reuse contexts\n- `array-morphisms-batch-ops` - Batch operations and combinators\n\n## License\n\nLGPL-3\n\n## Acknowledgments\n\n- Inspired by the Mathematics of Arrays (MoA) formalism by Lenore Mullin\n- Category-theoretic foundation from functional programming research\n- Memory reuse patterns from stream fusion and buffer optimization literature\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Firaikov%2Farray-morphisms","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Firaikov%2Farray-morphisms","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Firaikov%2Farray-morphisms/lists"}