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

Source for file Complex.php

Documentation is available at Complex.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: Complex.php 304053 2010-10-05 00:43:46Z clockwerx $
  20. //
  21.  
  22. include_once "PEAR.php";
  23.  
  24. /**
  25.  * Package with classes to represent and manipulate complex number. Contain
  26.  * definitions for basic arithmetic functions, as well as trigonometric,
  27.  * inverse trigonometric, hyperbolic, inverse hyperbolic, exponential and
  28.  * logarithms of complex numbers.
  29.  * @package Math_Complex
  30.  */
  31.  
  32. /**
  33.  * Math_Complex: class to represent an manipulate complex numbers (z = a + b*i)
  34.  *
  35.  * Originally this class was part of NumPHP (Numeric PHP package)
  36.  *
  37.  * @author  Jesus M. Castagnetto <jmcastagnetto@php.net>
  38.  * @version 0.8
  39.  * @access  public
  40.  */
  41. class Math_Complex {/*{{{*/
  42.  
  43.     /**
  44.      * The real part of the complex number
  45.      *
  46.      * @var float 
  47.      * @access  private
  48.      */
  49.     var $_real;
  50.  
  51.     /**
  52.      * The imaginary part of the complex number
  53.      *
  54.      * @var float 
  55.      * @access  private
  56.      */
  57.     var $_im;
  58.     
  59.     /*{{{ Math_Complex() */
  60.     /**
  61.      * Constructor for Math_Complex
  62.      * 
  63.      * @param float $real Real part of the number
  64.      * @param float $im Imaginary part of the number
  65.      * @return object Math_Complex 
  66.      * @access public
  67.      */
  68.     function Math_Complex($real$im
  69.     {
  70.         $this->_real floatval($real);
  71.         $this->_im floatval($im);
  72.     }/*}}}*/
  73.     
  74.     /**
  75.      * Simple string representation of the number
  76.      *
  77.      * @return string 
  78.      * @access public
  79.      */
  80.     function toString(
  81.     {
  82.         $r $this->getReal();
  83.         $i $this->getIm();
  84.         $str $r;
  85.         $str .=  ($i < 0' - ' ' + ';
  86.         $str .= abs($i).'i';
  87.         return $str;
  88.     }/*}}}*/
  89.  
  90.     /*{{{ abs2() */
  91.     /**
  92.      * Returns the square of the magnitude of the number
  93.      *
  94.      * @return float 
  95.      * @access public
  96.      */
  97.     function abs2(
  98.     {
  99.         return ($this->_real $this->_real $this->_im $this->_im);
  100.     }/*}}}*/
  101.  
  102.     /*{{{ abs() */
  103.     /**
  104.      * Returns the magnitude (also referred as norm) of the number
  105.      *
  106.      * @return float 
  107.      * @access public
  108.      */
  109.     function abs(
  110.     {
  111.         return sqrt($this->abs2());
  112.     }/*}}}*/
  113.     
  114.     /*{{{ norm() */
  115.     /**
  116.      * Returns the norm of the number
  117.      * Alias of Math_Complex::abs()
  118.      *
  119.      * @return float 
  120.      * @access public
  121.      */
  122.     function norm(
  123.     {
  124.         return $this->abs();
  125.     }/*}}}*/
  126.  
  127.     /*{{{ arg() */
  128.     /**
  129.      * Returns the argument of the complex number
  130.      *
  131.      * @return float|PEAR_ErrorA floating point number on success, a PEAR_Error otherwise
  132.      * @access public
  133.      */
  134.     function arg(
  135.     {
  136.         $arg atan2($this->_im,$this->_real);
  137.         if (M_PI < $arg || $arg < -1*M_PI{
  138.             return PEAR::raiseError('Argument has an impossible value');
  139.         else {
  140.             return $arg;
  141.         }
  142.  
  143.     }/*}}}*/
  144.  
  145.     /*{{{ angle() */
  146.     /**
  147.      * Returns the angle (argument) associated with the complex number
  148.      * Alias of Math_Complex::arg()
  149.      *
  150.      * @return mixed A float on success, a PEAR_Error otherwise
  151.      * @access public
  152.      */
  153.     function angle({
  154.         return $this->arg();
  155.     }/*}}}*/
  156.  
  157.     /*{{{ getReal() */
  158.     /**
  159.      * Returns the real part of the complex number
  160.      *
  161.      * @return float 
  162.      * @access public
  163.      */
  164.     function getReal(
  165.     {
  166.         return $this->_real;
  167.     }/*}}}*/
  168.  
  169.     /*{{{ getIm() */
  170.     /**
  171.      * Returns the imaginary part of the complex number
  172.      * @return float 
  173.      * @access public
  174.      */
  175.     function getIm(
  176.     {
  177.         return $this->_im;
  178.     }/*}}}*/
  179.  
  180. /* end of Math_Complex class *//*}}}*/
  181.  
  182. ?>

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