Source for file Quaternion.php
Documentation is available at Quaternion.php
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2003 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors: Jesus M. Castagnetto <jmcastagnetto@php.net> |
// +----------------------------------------------------------------------+
// $Id: Quaternion.php 303949 2010-10-02 16:11:40Z clockwerx $
* Math_Quaternion: class to represent an manipulate quaternions (q = a + b*i + c*j + d*k)
* A quaternion is an extension of the idea of complex numbers
* In 1844 Hamilton described a system in which numbers were composed of
* a real part and 3 imaginary and independent parts (i,j,k), such that:
* i^2 = j^2 = k^2 = -1 and
* ij = k, jk = i, ki = j and
* ji = -k, kj = -i, ik = -j
* The above are known as "Hamilton's rules"
* Interesting references on quaternions:
* - Sir William Rowan Hamilton "On Quaternions", Proceedings of the Royal Irish Academy,
* Nov. 11, 1844, vol. 3 (1847), 1-16
* (http://www.maths.tcd.ie/pub/HistMath/People/Hamilton/Quatern2/Quatern2.html)
* - Quaternion (from MathWorld): http://mathworld.wolfram.com/Quaternion.html
* Originally this class was part of NumPHP (Numeric PHP package)
* require_once 'Math/Quaternion.php';
* $a = new Math_Quaternion(2,4,2,-0.5);
* $b = new Math_Quaternion(1,2,3,0.5);
* echo "a: ".$a->toString()."\n";
* echo "b: ".$b->toString()."\n";
* $t = Math_QuaternionOp::conjugate($a);
* echo "a': ".$t->toString()."\n";
* $t = Math_QuaternionOp::conjugate($b);
* echo "b': ".$t->toString()."\n";
* echo "length(a): ".$a->length()." length2(a): ".$a->length2()."\n";
* echo "real(a): ".$a->getReal()."\nimag(a): ";
* print_r($a->getAllIm());
* a': 2 + -4i + -2j + 0.5k
* b': 1 + -2i + -3j + -0.5k
* length(a): 4.9244289008981 length2(a): 24.25
* @author Jesus M. Castagnetto <jmcastagnetto@php.net>
* @package Math_Quaternion
* The real part of the quaternion
* Coefficient of the first imaginary root
* Coefficient of the second imaginary root
* Coefficient of the third imaginary root
* Constructor for Math_Quaternion
* @return object Math_Quaternion
* Simple string representation of the quaternion
function toString ($fmt = 'number') {/*{{{*/
. ' '. $this->getJ(). ' '. $this->getK(). ' ]';
$this->getJ(). "j + ". $this->getK(). "k");
* Returns the square of the norm (length)
return ($r* $r + $i* $i + $j* $j + $k* $k);
* Returns the norm of the quaternion
* Returns the length (norm). Alias of Math_Quaternion:norm()
* Normalizes the quaternion
* @return mixed True on success, PEAR_Error object otherwise
return PEAR ::raiseError ('Quaternion cannot be normalized, norm = 0');
if ($this->norm() != 1.0 ) {
return PEAR_Error ('Computation error while normalizing, norm != 1');
* Conjugates the quaternion
* @return object Math_Quaternion
function setI($i) {/*{{{*/
function setJ($j) {/*{{{*/
function setK($k) {/*{{{*/
* Returns an associative array of I, J, K
return array ( 'i' => $this->getI(), 'j' => $this->getJ(), 'k' => $this->getK() );
}/*}}} end of Math_Quaternion */
Documentation generated on Mon, 11 Mar 2019 15:39:16 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|