Math_Quaternion
[ class tree: Math_Quaternion ] [ index: Math_Quaternion ] [ all elements ]

Source for file Quaternion.php

Documentation is available at Quaternion.php

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Jesus M. Castagnetto <jmcastagnetto@php.net>                |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: Quaternion.php 303949 2010-10-02 16:11:40Z clockwerx $
  20. //
  21.  
  22. include_once "PEAR.php";
  23.  
  24. /**
  25.  * Math_Quaternion: class to represent an manipulate quaternions (q = a + b*i + c*j + d*k)
  26.  *
  27.  * A quaternion is an extension of the idea of complex numbers
  28.  * In 1844 Hamilton described a system in which numbers were composed of
  29.  * a real part and 3 imaginary and independent parts (i,j,k), such that:
  30.  *
  31.  *     i^2 = j^2 = k^2 = -1       and
  32.  *     ij = k, jk = i, ki = j     and
  33.  *     ji = -k, kj = -i, ik = -j
  34.  *
  35.  * The above are known as "Hamilton's rules"
  36.  *
  37.  * Interesting references on quaternions:
  38.  *
  39.  * - Sir William Rowan Hamilton "On Quaternions", Proceedings of the Royal Irish Academy,
  40.  *   Nov. 11, 1844, vol. 3 (1847), 1-16
  41.  *   (http://www.maths.tcd.ie/pub/HistMath/People/Hamilton/Quatern2/Quatern2.html)
  42.  *
  43.  * - Quaternion (from MathWorld): http://mathworld.wolfram.com/Quaternion.html
  44.  * 
  45.  * Originally this class was part of NumPHP (Numeric PHP package)
  46.  *
  47.  * Example:
  48.  * <pre>
  49.  * require_once 'Math/Quaternion.php';
  50.  * 
  51.  * $a = new Math_Quaternion(2,4,2,-0.5);
  52.  * $b = new Math_Quaternion(1,2,3,0.5);
  53.  * 
  54.  * echo "a: ".$a->toString()."\n";
  55.  * echo "b: ".$b->toString()."\n";
  56.  * $t = Math_QuaternionOp::conjugate($a);
  57.  * echo "a': ".$t->toString()."\n";
  58.  * $t = Math_QuaternionOp::conjugate($b);
  59.  * echo "b': ".$t->toString()."\n";
  60.  * echo "length(a): ".$a->length()."  length2(a): ".$a->length2()."\n";
  61.  * echo "real(a): ".$a->getReal()."\nimag(a): ";
  62.  * 
  63.  * print_r($a->getAllIm());
  64.  * </pre>
  65.  * 
  66.  * Output from example:
  67.  * <pre>
  68.  * a: 2 + 4i + 2j + -0.5k
  69.  * b: 1 + 2i + 3j + 0.5k
  70.  * a': 2 + -4i + -2j + 0.5k
  71.  * b': 1 + -2i + -3j + -0.5k
  72.  * length(a): 4.9244289008981  length2(a): 24.25
  73.  * real(a): 2
  74.  * imag(a): Array
  75.  * (
  76.  *     [i] => 4
  77.  *     [j] => 2
  78.  *     [k] => -0.5
  79.  * )
  80.  * </pre>
  81.  * 
  82.  * @author  Jesus M. Castagnetto <jmcastagnetto@php.net>
  83.  * @version 0.7
  84.  * @access  public
  85.  * @package Math_Quaternion
  86.  */
  87. class Math_Quaternion {/*{{{*/
  88.  
  89.     /**
  90.      * The real part of the quaternion
  91.      *
  92.      * @var    float 
  93.      * @access private
  94.      */
  95.     var $real;
  96.  
  97.     /**
  98.      * Coefficient of the first imaginary root
  99.      *
  100.      * @var float 
  101.      * @access private
  102.      */
  103.     var $i;
  104.  
  105.     /**
  106.      * Coefficient of the second imaginary root
  107.      *
  108.      * @var float 
  109.      * @access private
  110.      */
  111.     var $j;
  112.  
  113.     /**
  114.      * Coefficient of the third imaginary root
  115.      *
  116.      * @var float 
  117.      * @access private
  118.      */
  119.     var $k;
  120.     
  121.     /**
  122.      * Constructor for Math_Quaternion
  123.      *
  124.      * @param float $real 
  125.      * @param float $i 
  126.      * @param float $j 
  127.      * @param float $k 
  128.      * @return object Math_Quaternion 
  129.      * @access public
  130.      */
  131.     function Math_Quaternion ($real$i$j$k{/*{{{*/
  132.         $this->setReal($real);
  133.         $this->setI($i);
  134.         $this->setJ($j);
  135.         $this->setK($k);
  136.     }/*}}}*/
  137.     
  138.     /**
  139.      * Simple string representation of the quaternion
  140.      *
  141.      * @return string 
  142.      * @access public
  143.      */
  144.     function toString ($fmt 'number'{/*{{{*/
  145.         if ($fmt == 'vector'{
  146.             return '[ '.$this->getReal().' '.$this->getI()
  147.                     .' '.$this->getJ().' '.$this->getK().' ]';
  148.         else {
  149.             return $this->getReal()." + ".$this->getI()."i + ".
  150.                      $this->getJ()."j + ".$this->getK()."k");
  151.         }
  152.     }/*}}}*/
  153.  
  154.     /**
  155.      * Returns the square of the norm (length)
  156.      *
  157.      * @return float 
  158.      * @access public
  159.      */
  160.     function length2({/*{{{*/
  161.             $r $this->getReal()
  162.             $i $this->getI();
  163.             $j $this->getJ()
  164.             $k $this->getK();
  165.             return ($r*$r $i*$i $j*$j $k*$k);
  166.     }/*}}}*/
  167.  
  168.     /**
  169.      * Returns the norm of the quaternion
  170.      *
  171.      * @return float 
  172.      * @access public
  173.      */
  174.     function norm({
  175.         return sqrt($this->length2());
  176.     }
  177.     
  178.     /**
  179.      * Returns the length (norm). Alias of Math_Quaternion:norm()
  180.      *
  181.      * @return float 
  182.      * @access public
  183.      */
  184.     function length({
  185.         return $this->norm();
  186.     }
  187.  
  188.     /**
  189.      * Normalizes the quaternion
  190.      *
  191.      * @return mixed True on success, PEAR_Error object otherwise
  192.      * @access public
  193.      */
  194.     function normalize({/*{{{*/
  195.         $n $this->norm();
  196.         if ($n == 0.0{
  197.             return PEAR::raiseError('Quaternion cannot be normalized, norm = 0');
  198.         else {
  199.             $this->setReal($this->getReal($n);
  200.             $this->setI($this->getI($n);
  201.             $this->setJ($this->getJ($n);
  202.             $this->setK($this->getK($n);
  203.             if ($this->norm(!= 1.0{
  204.                 return PEAR_Error('Computation error while normalizing, norm != 1');
  205.             else {
  206.                 return true;
  207.             }
  208.         }
  209.     }/*}}}*/
  210.  
  211.     /**
  212.      * Conjugates the quaternion
  213.      *
  214.      * @return void 
  215.      * @access public
  216.      */
  217.     function conjugate({
  218.         $this->setI(-1 * $this->getI());
  219.         $this->setJ(-1 * $this->getJ());
  220.         $this->setK(-1 * $this->getK());
  221.     }
  222.  
  223.     /**
  224.      * Negates the quaternion
  225.      *
  226.      * @return void 
  227.      * @access public
  228.      */
  229.     function negate({
  230.         $this->setReal(-1 * $this->getReal());
  231.         $this->setI(-1 * $this->getI());
  232.         $this->setJ(-1 * $this->getJ());
  233.         $this->setK(-1 * $this->getK());
  234.     }
  235.  
  236.     /**
  237.      * Clones the quaternion
  238.      *
  239.      * @return object Math_Quaternion 
  240.      * @access public
  241.      */
  242.     function &makeClone({
  243.         $obj = new Math_Quaternion($this->getReal()$this->getI()$this->getJ()$this->getK());
  244.         return $obj;
  245.     }
  246.  
  247.     /**
  248.      * Sets the real part
  249.      *
  250.      * @param float $real 
  251.      * @return void 
  252.      * @access public
  253.      */
  254.     function setReal($real{/*{{{*/
  255.         $this->real floatval($real);
  256.     }/*}}}*/
  257.     
  258.     /**
  259.      * Returns the real part
  260.      *
  261.      * @return float 
  262.      * @access public
  263.      */
  264.     function getReal({/*{{{*/
  265.         return $this->real;
  266.     }/*}}}*/
  267.  
  268.     /**
  269.      * Sets I
  270.      *
  271.      * @param float $i 
  272.      * @return void 
  273.      * @access public
  274.      */
  275.     function setI($i{/*{{{*/
  276.         $this->floatval($i);
  277.     }/*}}}*/
  278.     
  279.     /**
  280.      * Returns I
  281.      *
  282.      * @return float 
  283.      * @access public
  284.      */
  285.     function getI({/*{{{*/
  286.         return $this->i;
  287.     }/*}}}*/
  288.  
  289.     /**
  290.      * Sets J
  291.      *
  292.      * @param float $j 
  293.      * @return void 
  294.      * @access public
  295.      */
  296.     function setJ($j{/*{{{*/
  297.         $this->floatval($j);
  298.     }/*}}}*/
  299.     
  300.     /**
  301.      * Returns J
  302.      *
  303.      * @return float 
  304.      * @access public
  305.      */
  306.     function getJ({/*{{{*/
  307.         return $this->j;
  308.     }/*}}}*/
  309.  
  310.     /**
  311.      * Sets K
  312.      *
  313.      * @param float $k 
  314.      * @return void 
  315.      * @access public
  316.      */
  317.     function setK($k{/*{{{*/
  318.         $this->floatval($k);
  319.     }/*}}}*/
  320.     
  321.     /**
  322.      * Returns K
  323.      *
  324.      * @return float 
  325.      * @access public
  326.      */
  327.     function getK({/*{{{*/
  328.         return $this->k;
  329.     }/*}}}*/
  330.  
  331.     /**
  332.      * Sets I, J, K
  333.      * @return void 
  334.      * @access public
  335.      */
  336.     function setAllIm($i$j$k{/*{{{*/
  337.         $this->setI($i);
  338.         $this->setJ($j);
  339.         $this->setK($k);
  340.     }/*}}}*/
  341.     
  342.     /**
  343.      * Returns an associative array of I, J, K
  344.      *
  345.      * @return array 
  346.      * @access public
  347.      */
  348.     function getAllIm({/*{{{*/
  349.         return array 'i' => $this->getI()'j' => $this->getJ()'k' => $this->getK() );
  350.     }/*}}}*/
  351.  
  352. }/*}}} end of Math_Quaternion */
  353.  
  354. ?>

Documentation generated on Mon, 11 Mar 2019 15:39:16 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.