HyperComplex

This gem provides a HyperComplex class highly compatible with other numeric classes.

The hypercomplex numbers form finite-dimensional algebra over the real numbers. These algebras are produced by the Cayley–Dickson construction. Examples of such algebras are complex numbers, quaternions, octonions, sedenions, etc.

The hypercomplex number can be represented as

i = 0 2 n - 1 a i e i    where   a i ,   e 0 = 1 ,   e 1 2 = ... = e 2 n - 1 2 = - 1 ,  n

The identity unit  ( e 0 )  and imaginary units  ( e i ,  i 0 )  form the basis for space of dimension   2 n   over   .

Requirements

Ruby >= 3.1

Installation

Add this line to your application's Gemfile:

gem 'mcalendar'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install hyper_complex

Usage

<span class='id identifier rubyid_require'>require</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>hyper_complex</span><span class='tstring_end'>'</span></span>

<span class='comment'># Creation from complex numbers
</span><span class='id identifier rubyid_q1'>q1</span> <span class='op'>=</span> <span class='const'><span class='object_link'><a href="HyperComplex.html" title="HyperComplex (class)">HyperComplex</a></span></span><span class='period'>.</span><span class='id identifier rubyid_rect'><span class='object_link'><a href="HyperComplex.html#rect-class_method" title="HyperComplex.rect (method)">rect</a></span></span><span class='lparen'>(</span><span class='lparen'>(</span><span class='int'>1</span><span class='op'>+</span><span class='imaginary'>2i</span><span class='rparen'>)</span><span class='comma'>,</span> <span class='lparen'>(</span><span class='int'>3</span><span class='op'>+</span><span class='imaginary'>4i</span><span class='rparen'>)</span><span class='rparen'>)</span> <span class='comment'>#=> HyperComplex[1, 2, 3, 4]
</span><span class='id identifier rubyid_q2'>q2</span> <span class='op'>=</span> <span class='const'><span class='object_link'><a href="HyperComplex.html" title="HyperComplex (class)">HyperComplex</a></span></span><span class='period'>.</span><span class='id identifier rubyid_rect'><span class='object_link'><a href="HyperComplex.html#rect-class_method" title="HyperComplex.rect (method)">rect</a></span></span><span class='lparen'>(</span><span class='lparen'>(</span><span class='int'>5</span><span class='op'>+</span><span class='imaginary'>6i</span><span class='rparen'>)</span><span class='comma'>,</span> <span class='lparen'>(</span><span class='int'>7</span><span class='op'>+</span><span class='imaginary'>8i</span><span class='rparen'>)</span><span class='rparen'>)</span> <span class='comment'>#=> HyperComplex[5, 6, 7, 8]
</span>
<span class='comment'># Creation from HyperComplex numbers
</span><span class='id identifier rubyid_o1'>o1</span> <span class='op'>=</span> <span class='const'><span class='object_link'><a href="HyperComplex.html" title="HyperComplex (class)">HyperComplex</a></span></span><span class='period'>.</span><span class='id identifier rubyid_rect'><span class='object_link'><a href="HyperComplex.html#rect-class_method" title="HyperComplex.rect (method)">rect</a></span></span><span class='lparen'>(</span><span class='id identifier rubyid_q1'>q1</span><span class='comma'>,</span> <span class='id identifier rubyid_q2'>q2</span><span class='rparen'>)</span> <span class='comment'>#=> HyperComplex[1, 2, 3, 4, 5, 6, 7, 8]
</span>
<span class='comment'># Creation from real numbers
</span><span class='id identifier rubyid_o2'>o2</span> <span class='op'>=</span> <span class='const'>HyperCoplex</span><span class='lbracket'>[</span><span class='int'>9</span><span class='comma'>,</span> <span class='int'>10</span><span class='comma'>,</span> <span class='int'>11</span><span class='comma'>,</span> <span class='int'>12</span><span class='comma'>,</span> <span class='int'>13</span><span class='rbracket'>]</span> <span class='comment'>#=> HyperComplex[9, 10, 11, 12, 13, 0, 0, 0]
</span>
<span class='comment'># Creation from polar form
</span><span class='id identifier rubyid_q3'>q3</span> <span class='op'>=</span> <span class='const'><span class='object_link'><a href="HyperComplex.html" title="HyperComplex (class)">HyperComplex</a></span></span><span class='period'>.</span><span class='id identifier rubyid_polar'><span class='object_link'><a href="HyperComplex.html#polar-class_method" title="HyperComplex.polar (method)">polar</a></span></span><span class='lparen'>(</span><span class='int'>1</span><span class='comma'>,</span> <span class='const'>Math</span><span class='op'>::</span><span class='const'>PI</span><span class='op'>/</span><span class='int'>3</span><span class='comma'>,</span> <span class='const'>Vector</span><span class='lbracket'>[</span><span class='int'>1</span><span class='comma'>,</span> <span class='int'>1</span><span class='comma'>,</span> <span class='int'>1</span><span class='rbracket'>]</span><span class='period'>.</span><span class='id identifier rubyid_normalize'>normalize</span><span class='rparen'>)</span> <span class='comment'>#=> HyperComplex[0.5, 0.5, 0.5, 0.5]
</span>
<span class='comment'># standard calculations between numeric instances
</span><span class='lparen'>(</span><span class='id identifier rubyid_q1'>q1</span><span class='op'>+</span><span class='id identifier rubyid_q2'>q2</span><span class='rparen'>)</span><span class='op'>*</span><span class='id identifier rubyid_o1'>o1</span> <span class='op'>/</span> <span class='const'>Complex</span><span class='op'>::</span><span class='const'>I</span> <span class='op'>-</span> <span class='int'>24</span> <span class='comment'>#=> HyperComplex[(0/1), (88/1), (-40/1), (20/1), (-80/1), (-184/1), (112/1), (-84/1)]
</span>

Contributing

Bug reports and pull requests are welcome on GitHub at github.com/bfifelin/hyper_complex.

License

This project is licensed under the terms of the MIT license.

Acknowledgments

This gem is based on the gem quaternion_c2 by Masahiro Nomoto.