Math::Quaternion - Hyper-complex numbers as objects with operators
use Math::Quaternion;
This distribution implements an OO class for Quaternions, which are the simplest of the hyper-complex numbers. It adds the operators needed for basic math on the Qs, and should be a good base class for future modules to support the other hyper-complex numbers.
I intend this module to be a high-quality example of Raku numeric OO, usable as a reference for future module authors.
This module is a mostly-from-scratch re-implementation of Quaternions; its API differs from the excellent Perl Math::Quaternion module.
Wikipedia | Raku | Boost | dwh | RAW |
---|---|---|---|---|
a | $.r | R_component_1 | real | Real____________ part of a Q (as accessor) |
b | $.i | R_component_2 | imag_i | First_ imaginary part of a Q (as accessor) |
c | $.j | R_component_3 | imag_j | Second imaginary part of a Q (as accessor) |
d | $.k | R_component_4 | imag_k | Third_ imaginary part of a Q (as accessor) |
.coeffs | (r, i, j, k) # List, not arrayref nor vector | |||
.v | (i, j, k) # List, not arrayref nor vector | |||
. | ||||
norm | abs | abs | sum_of_squares( r,i,j,k ).sqrt | |
norm | norm | sum_of_squares( r,i,j,k ) | ||
abs(unreal) | abs_imag | sum_of_squares( i,j,k ).sqrt | ||
norm(unreal) | norm_imag | sum_of_squares( i,j,k ) | ||
arg | atan2( sum_of_squares( i,j,k ).sqrt, r ); | |||
distance | . | |||
. | ||||
unreal | imag | self.( 0.0, i, j, k ); | ||
conj | self.( r, -i, -j, -k ); | |||
operator * | self.( r, -i, -j, -k ); | |||
signum | absq = sum_of_squares( r,i,j,k ).sqrt; return ( absq == 0.0 ) ?? self !! self.new( r/absq, i/absq, j/absq, k/absq ); | |||
sqr | self.( r*r - i*i - j*j - k*k, 2*r*i, 2*r*j, 2*r*k ); | |||
inverse | self.new: (r,-i,-j,-k) / sum_of_squares( r,i,j,k ) | |||
versor | self / sum_of_squares( r,i,j,k ).sqrt | |||
. | ||||
is_nan | r != r or i != i or j != j or k != k; | |||
is_inf | any(r,i,j,k) == any(PosInf,NegInf) | |||
is_neg_inf | all(_,i,j,k) == 0 and r == NEG_INF; | |||
is_pos_inf | all(_,i,j,k) == 0 and r == POS_INF; | |||
is_real_inf | all(_,i,j,k) == 0 and r == any(POS_INF,NEG_INF); | |||
is_real | is_real | all(_,i,j,k) == 0 | ||
is_zero | is_zero | all(r,i,j,k) == 0 | ||
is_complex | all(_,_,j,k) == 0 | |||
is_imaginary | is_imaginary | r == 0 | ||
. | ||||
rotate | When |q| == 1 | |||
sqrt | . |
Thanks to:
-
Jonathan Chin for the original Perl Math::Quaternion module.
-
Solomon "colomon" Foster for encouraging this module's release.
-
Will "Coke" Coleda for fixes to match changes in Rakudo.
-
RosettaCode for the challenge that provoked the initial code.
Bruce Gray
Copyright 2010 - 2017 Bruce Gray
Copyright 2024 Raku Community
This library is free software; you can redistribute it and/or modify it under the Artistic License 2.0.