# Complex numbers

As was seen in the preceding chapter, a complex number is an object comprising 2 real numbers (called real and imag by Ruby). This is the Cayley-Dickson construction of the complex numbers. In a very similar manner, a quaternion can be considered as made of 2 complex numbers.
In all the following, cmath will be used as it handles fractions automatically. This chapter is in some way different from the preceding ones, as it shows how to create brand new objects in Ruby, and not how to use already available objects.

# Quaternions
## Definition and display
### Definition

The definition of a quaternion finds its shelter in a class which is called Quaternion:
The first method of a quaternion will be its instantiation:

#### Instantiation

From now on, a and b (which will be complex numbers) will be the 2 quaternion's attributes

#### Attributes a and b

As the two numbers which define a quaternion are complex, it is not appropriate to call them the real and imaginary parts. Besides, an other stage will be necessary with the octonions later on. So the shortest names have been chosen, and they will be called the a of the quaternion, and its b.
From now on it is possible to access to the a and b part of a quaternion q with q.a and q.b.

### Display

In order that it be easy to display a quaternion q with puts(q) it is necessary to redefine a method to_s for it (a case of polymorphism). There are several choices but this one works OK:
To read it loud it is better to read from right to left. For example, a.real denotes the real part of a and q.a.real denotes the real part of the a part of q.

## Functions
### Modulus

The absolute value of a quaternion is a (positive) real number.

### Conjugate

The conjugate of a quaternion is another quaternion, having the same modulus.

## Operations
### Addition

To add two quaternions, just add their as together, and their bs together:

### Subtraction

The use of the - symbol is an other case of polymorphism, which allows to write rather simply the subtraction.

### Multiplication

Multiplication of the quaternions is more complex (!):
This multiplication is not commutative, as can be checked by the following examples:

### Division

The division can be defined as this:
As they have the same modulus, the quotient of a quaternion by its conjugate has modulus one:
This last example digs that $\left(-{\frac {22}{30}}\right)^{2}+\left({\frac {4}{30}}\right)^{2}+\left({\frac {12}{30}}\right)^{2}+\left({\frac {16}{30}}\right)^{2}=1$, or $22^{2}+4^{2}+12^{2}+16^{2}=484+16+144+256=900=30^{2}$, which is a decomposition of $30^{2}$ as a sum of 4 squares.

## Quaternion class in Ruby

The complete class is here:
If this content is saved in a text file called quaternion.rb, after require 'quaternion' one can make computations on quaternions.

# Octonions

One interesting fact about the Cayley-Dickson which has been used for the quaternions above, is that it can be generalized, for example for the octonions.

## Definition and display
### Definition

All the following methods will be enclosed in a class called Octonion:
At this point, there is not much difference from the quaternion object. Only, for an octonion, a and b will be quaternions, not complex numbers. Ruby will know it when a and b will be instantiated.

### Display

The to_s method of an octonion (converting it to a string object so that it can be displayed) is very similar to the quaternion equivalent, only there are 8 real numbers to display now:
The first of these numbers is the real part of the a part of the first quaternion, which is the octonions's a! Accessing to this real part of the a part of the octonion's a part, requires to go through a binary tree which depth is 3.

## Functions

Thanks to Cayley and Dickson, the methods needed for octonions computing are similar to the quaternion's.

### Modulus

Same than for the quaternions:

### Conjugate
## Operations
### Addition

Like for the quaternions, one has just to add the as and the bs separately (only now the a and b part are quaternions):

### Multiplication

This multiplication is still not commutative, but it is even not associative either!

### Division

Here again, the division of an octonion by its conjugate has modulus 1:

## The octonion class in Ruby

The file is not much heavier than the quaternion's one:
Saving it as octonions.rb, any script beginning by
allows computing on octonions.

# Bibliography

* Actually, there is already a quaternion support for Ruby, but it is not (yet) native: ; on the same site, there is also a file for the octonions, which is interesting to compare to the above one.
* A "best-downloader" book on octonions is John Baez's one: