https://github.com/ogeagla/clj-hypercomplex
A library for hypercomplex algebras in Clojure
https://github.com/ogeagla/clj-hypercomplex
algebraic-structures cayley-dickson clojure complex-numbers hypercomplex-number octonion quaternions
Last synced: 3 months ago
JSON representation
A library for hypercomplex algebras in Clojure
- Host: GitHub
- URL: https://github.com/ogeagla/clj-hypercomplex
- Owner: ogeagla
- License: epl-1.0
- Created: 2018-03-27T20:32:06.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2024-02-06T13:54:14.000Z (almost 2 years ago)
- Last Synced: 2025-10-22T00:02:13.381Z (3 months ago)
- Topics: algebraic-structures, cayley-dickson, clojure, complex-numbers, hypercomplex-number, octonion, quaternions
- Language: Clojure
- Homepage:
- Size: 62.3 MB
- Stars: 4
- Watchers: 1
- Forks: 3
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# hypercomplex
[Changelog](CHANGELOG.md)
[](https://travis-ci.com/ogeagla/clj-hypercomplex)
[](https://clojars.org/hypercomplex)
```clojure
[hypercomplex "0.0.4"]
```
A hypercomplex number library written in Clojure.
Includes Cayley-Dickson construction for generating and working with hypercomplex algebras.
https://en.wikipedia.org/wiki/Cayley%E2%80%93Dickson_construction

## Usage
- `hypercomplex.core` for operators on or between hypercomplex numbers: `times`, `plus`, `minus`, `neg`, `c` (conjugate), `scale`, `norm`, `inv`, and `mag`.
- `hypercomplex.cayley-dickson-construction` for constructing hypercomplex numbers: `complex`, `quaternion`, `octonion`, `sedenion`, `pathion`, and `n-hypercomplex` for constructing higher-order algebras of arbitrary order, provided it is a power of 2.
- All constructions can take an `:impl`, which can be either `:plain`, or `:apache`, which represent either pure Clojure or `org.apache.commons.math3.complex.Complex` implementations to be used under the hood for constructing the hypercomplex numbers. Hypercomplex numbers with different underlying implementations are not interoperable (TODO).
An example of creating complex numbers, `quaternion`s, performing multiplication, and checking equality as a result of associativity:
```clojure
(:require [hypercomplex.core :refer :all]
[hypercomplex.cayley-dickson-construction :refer
[complex quaternion octonion sedenion pathion n-hypercomplex]])
;create a complex number using pure Clojure impl:
(complex {:a 1.1 :b 2.2 :impl :plain})
; => #hypercomplex.core.Complex2{:a 1.1, :b 2.2, :order 2}
;create a complex number using Apache Commons Complex2 impl:
(complex {:a 1.1 :b 2.2 :impl :apache})
; => #hypercomplex.core.Complex2Apache{:a 1.1, :b 2.2, :order 2, :obj #object[org.apache.commons.math3.complex.Complex 0x3249a1ce (1.1, 2.2)]}
;similar for other hypercomplex numbers:
(quaternion {:a 1 :b 2 :c 3 :d 4 :impl :plain})
; => #hypercomplex.core.Construction{:a #hypercomplex.core.Complex2{:a 1, :b 2, :order 2}, :b #hypercomplex.core.Complex2{:a 3, :b 4, :order 2}, :order 4}
;example passing test case:
(is
(= (times
(quaternion {:a 1 :b 2 :c 3 :d 4})
(times (quaternion {:a 8 :b 7 :c 6 :d 5})
(quaternion {:a 9 :b 10 :c 11 :d 12})))
(times
(times
(quaternion {:a 1 :b 2 :c 3 :d 4})
(quaternion {:a 8 :b 7 :c 6 :d 5}))
(quaternion {:a 9 :b 10 :c 11 :d 12}))))
```
An example of several other operators and constructions:
```clojure
(is
(= (quaternion {:a 1.5 :b 1.5 :c 1.5 :d 1.5})
(scale
(quaternion {:a 1 :b 1 :c 1 :d 1})
1.5)))
(is (= 32
(norm (pathion {:a 1 :b 1 :c 1 :d 1 :e 1 :f 1 :g 1 :h 1
:i 1 :j 1 :k 1 :l 1 :m 1 :n 1 :o 1 :p 1
:q 1 :r 1 :s 1 :t 1 :u 1 :v 1 :w 1 :x 1
:y 1 :z 1 :aa 1 :bb 1 :cc 1 :dd 1 :ee 1 :ff 1}))))
```
An example of creating much higher order algebras using pure Clojure implementation, `:plain`:
```clojure
(n-hypercomplex [0 1 2 3 4 5 6 7] :plain)
=>
#hypercomplex.core.Construction
{:a #hypercomplex.core.Construction
{:a #hypercomplex.core.Complex2
{:a 0
:b 1
:order 2}
:b #hypercomplex.core.Complex2
{:a 2
:b 3
:order 2}
:order 4}
:b #hypercomplex.core.Construction
{:a #hypercomplex.core.Complex2
{:a 4
:b 5
:order 2}
:b #hypercomplex.core.Complex2
{:a 6
:b 7
:order 2}
:order 4}
:order 8}
```
And a _very_ high order hypercomplex number:
```clojure
(n-hypercomplex (range 4096) :plain)
=>
#hypercomplex.core.Construction
{:a #hypercomplex.core.Construction
{:a #hypercomplex.core.Construction
{:a #hypercomplex.core.Construction
{:a #hypercomplex.core.Construction
{:a #hypercomplex.core.Construction
{:a #hypercomplex.core.Construction
{:a #hypercomplex.core.Construction
{:a #hypercomplex.core.Construction
{:a #hypercomplex.core.Construction
{:a #hypercomplex.core.Construction
{:a #hypercomplex.core.Complex2
{:a 0
:b 1
:order 2}
:b #hypercomplex.core.Complex2
{:a 2
:b 3
:order 2}
:order 4}
...
:order 2}
:order 4}
:order 8}
:order 16}
:order 32}
:order 64}
:order 128}
:order 256}
:order 512}
:order 1024}
:order 2048}
:order 4096}
```
## Tests
```bash
lein test
```
## License
Credits to:
- https://github.com/hamiltron/py-cayleydickson (MIT License)
- https://nakkaya.com/2009/09/29/fractals-in-clojure-mandelbrot-fractal/
- https://github.com/clojure-numerics/image-matrix/blob/master/src/main/clojure/mikera/image_matrix/colours.clj (EPL License)
Copyright © 2018 Octavian Geagla
Distributed under the Eclipse Public License either version 1.0 or (at
your option) any later version.