ComplexNumber   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 121
rs 10
c 0
b 0
f 0
wmc 12

9 Methods

Rating   Name   Duplication   Size   Complexity  
A angle() 0 3 1
A subtract() 0 5 1
A getReal() 0 3 1
A abs2() 0 3 1
A toString() 0 8 2
A __construct() 0 4 1
A abs() 0 3 1
A getIm() 0 3 1
A arg() 0 7 3
1
<?php
2
3
namespace Hotrush\AngularSweep;
4
5
class ComplexNumber
6
{
7
    /**
8
     * The real part of the complex number
9
     *
10
     * @var float
11
     * @access  private
12
     */
13
    private $_real;
14
    /**
15
     * The imaginary part of the complex number
16
     *
17
     * @var float
18
     * @access  private
19
     */
20
    private $_im;
21
22
    /**
23
     * Constructor for ComplexNumberPoint
24
     *
25
     * @param float $real Real part of the number
26
     * @param float $im Imaginary part of the number
27
     */
28
    public function __construct($real, $im)
29
    {
30
        $this->_real = floatval($real);
31
        $this->_im = floatval($im);
32
    }
33
34
    /**
35
     * Simple string representation of the number.
36
     *
37
     * @return string
38
     */
39
    public function toString()
40
    {
41
        $r = $this->getReal();
42
        $i = $this->getIm();
43
        $str = $r;
44
        $str .= ($i < 0) ? ' - ' : ' + ';
45
        $str .= abs($i) . 'i';
46
        return $str;
47
    }
48
49
    /**
50
     * Returns the square of the magnitude of the number.
51
     *
52
     * @return float
53
     */
54
    public function abs2()
55
    {
56
        return ($this->_real * $this->_real + $this->_im * $this->_im);
57
    }
58
59
    /**
60
     * Returns the magnitude (also referred as norm) of the number.
61
     *
62
     * @return float
63
     */
64
    public function abs()
65
    {
66
        return sqrt($this->abs2());
67
    }
68
69
    /**
70
     * Returns the argument of the complex number.
71
     *
72
     * @return float A floating point number on success
73
     * @throws \Exception
74
     */
75
    public function arg()
76
    {
77
        $arg = atan2($this->_im, $this->_real);
78
        if (M_PI < $arg || $arg < -1 * M_PI) {
79
            throw new \Exception('Argument has an impossible value');
80
        } else {
81
            return $arg;
82
        }
83
    }
84
85
    /**
86
     * Returns the angle (argument) associated with the complex number.
87
     *
88
     * @return mixed A float on success
89
     * @throws \Exception
90
     */
91
    public function angle()
92
    {
93
        return $this->arg();
94
    }
95
96
    /**
97
     * Returns the real part of the complex number.
98
     *
99
     * @return float
100
     */
101
    public function getReal()
102
    {
103
        return $this->_real;
104
    }
105
106
    /**
107
     * Returns the imaginary part of the complex number.
108
     * @return float
109
     */
110
    public function getIm()
111
    {
112
        return $this->_im;
113
    }
114
115
    /**
116
     * Subtract complex number.
117
     *
118
     * @param ComplexNumber $number
119
     * @return ComplexNumber
120
     */
121
    public function subtract(ComplexNumber $number)
122
    {
123
        return new ComplexNumber(
124
            $this->getReal() - $number->getReal(),
125
            $this->getIm() - $number->getIm()
126
        );
127
    }
128
}