1 | # Complex Numbers
|
2 |
|
3 | Math.js supports the creation, manipulation, and calculations with complex numbers.
|
4 | Support of complex numbers is powered by the library [complex.js](https://github.com/infusion/Complex.js).
|
5 |
|
6 | In mathematics, a complex number is an expression of the form `a + bi`,
|
7 | where `a` and `b` are real numbers and `i` represents the imaginary number
|
8 | defined as `i^2 = -1`. (In other words, `i` is the square root of `-1`.)
|
9 | The real number `a` is called the real part of the complex number,
|
10 | and the real number `b` is the imaginary part. For example, `3 + 2i` is a
|
11 | complex number, having real part `3` and imaginary part `2`.
|
12 | Complex numbers are often used in applied mathematics, control theory,
|
13 | signal analysis, fluid dynamics and other fields.
|
14 |
|
15 | ## Usage
|
16 |
|
17 | A complex number is created using the function `math.complex`. This function
|
18 | accepts:
|
19 |
|
20 | - two numbers representing the real and imaginary part of the value,
|
21 | - a single string containing a complex value in the form `a + bi` where `a`
|
22 | and `b` respectively represent the real and imaginary part of the complex number.
|
23 | - an object with either properties `re` and `im` for the real and imaginary
|
24 | part of the value, or two properties `r` and `phi` containing the polar
|
25 | coordinates of a complex value.
|
26 | The function returns a `Complex` object.
|
27 |
|
28 | Syntax:
|
29 |
|
30 | ```js
|
31 | math.complex(re: number) : Complex
|
32 | math.complex(re: number, im: number) : Complex
|
33 | math.complex(complex: Complex) : Complex
|
34 | math.complex({re: Number, im: Number}) : Complex
|
35 | math.complex({r: number, phi: number}) : Complex
|
36 | math.complex({abs: number, arg: number}) : Complex
|
37 | math.complex(str: string) : Complex
|
38 | ```
|
39 |
|
40 | Examples:
|
41 |
|
42 | ```js
|
43 | const a = math.complex(2, 3) // Complex 2 + 3i
|
44 | a.re // Number 2
|
45 | a.im // Number 3
|
46 |
|
47 | const b = math.complex('4 - 2i') // Complex 4 - 2i
|
48 | b.re = 5 // Number 5
|
49 | b // Complex 5 - 2i
|
50 | ```
|
51 |
|
52 | ## Calculations
|
53 |
|
54 | Most functions of math.js support complex numbers. Complex and real numbers
|
55 | can be used together.
|
56 |
|
57 | ```js
|
58 | const a = math.complex(2, 3) // Complex 2 + 3i
|
59 | const b = math.complex('4 - 2i') // Complex 4 - 2i
|
60 |
|
61 | math.re(a) // Number 2
|
62 | math.im(a) // Number 3
|
63 | math.conj(a) // Complex 2 - 3i
|
64 |
|
65 | math.add(a, b) // Complex 6 + i
|
66 | math.multiply(a, 2) // Complex 4 + 6i
|
67 | math.sqrt(-4) // Complex 2i
|
68 | ```
|
69 |
|
70 | ## API
|
71 | A `Complex` object created by `math.complex` contains the following properties and functions:
|
72 |
|
73 | ### complex.re
|
74 |
|
75 | A number containing the real part of the complex number. Can be read and replaced.
|
76 |
|
77 | ### complex.im
|
78 |
|
79 | A number containing the imaginary part of the complex number. Can be read and replaced.
|
80 |
|
81 | ### complex.clone()
|
82 |
|
83 | Create a clone of the complex number.
|
84 |
|
85 | ### complex.equals(other)
|
86 |
|
87 | Test whether a complex number equals another complex value.
|
88 |
|
89 | Two complex numbers are equal when both their real and imaginary parts are
|
90 | equal.
|
91 |
|
92 | ### complex.neg()
|
93 |
|
94 | Returns a complex number with a real part and an imaginary part equal in magnitude but opposite in sign to the current complex number.
|
95 |
|
96 | ### complex.conjugate()
|
97 |
|
98 | Returns a complex number with an equal real part and an imaginary part equal in magnitude but opposite in sign to the current complex number.
|
99 |
|
100 | ### complex.inverse()
|
101 |
|
102 | Returns a complex number that is inverse of the current complex number.
|
103 |
|
104 | ### complex.toVector()
|
105 |
|
106 | Get the vector representation of the current complex number. Returns an array of size 2.
|
107 |
|
108 | ### complex.toJSON()
|
109 |
|
110 | Returns a JSON representation of the complex number, with signature
|
111 | `{mathjs: 'Complex', re: number, im: number}`.
|
112 | Used when serializing a complex number, see [Serialization](../core/serialization.md).
|
113 |
|
114 | ### complex.toPolar()
|
115 |
|
116 | Get the polar coordinates of the complex number, returns
|
117 | an object with properties `r` and `phi`.
|
118 |
|
119 | ### complex.toString()
|
120 |
|
121 | Returns a string representation of the complex number, formatted
|
122 | as `a + bi` where `a` is the real part and `b` the imaginary part.
|
123 |
|
124 |
|
125 | ### complex.format([precision: number])
|
126 |
|
127 | Get a string representation of the complex number,
|
128 | formatted as `a + bi` where `a` is the real part and `b` the imaginary part.
|
129 | If precision is defined, the units value will be rounded to the provided
|
130 | number of digits.
|
131 |
|
132 | ## Static methods
|
133 | The following static methods can be accessed using `math.Complex`
|
134 |
|
135 |
|
136 | ### Complex.fromJSON(json)
|
137 |
|
138 | Revive a complex number from a JSON object. Accepts
|
139 | An object `{mathjs: 'Complex', re: number, im: number}`, where the property
|
140 | `mathjs` is optional.
|
141 | Used when deserializing a complex number, see [Serialization](../core/serialization.md).
|
142 |
|
143 | ### Complex.fromPolar(r: number, phi: number)
|
144 |
|
145 | Create a complex number from polar coordinates.
|
146 |
|
147 |
|
148 | ### Complex.compare(a: Complex, b: Complex)
|
149 |
|
150 | Returns the comparision result of two complex number:
|
151 |
|
152 | - Returns 1 when the real part of `a` is larger than the real part of `b`
|
153 | - Returns -1 when the real part of `a` is smaller than the real part of `b`
|
154 | - Returns 1 when the real parts are equal
|
155 | and the imaginary part of `a` is larger than the imaginary part of `b`
|
156 | - Returns -1 when the real parts are equal
|
157 | and the imaginary part of `a` is smaller than the imaginary part of `b`
|
158 | - Returns 0 when both real and imaginary parts are equal.
|
159 |
|
160 | Example:
|
161 | ```js
|
162 | const a = math.complex(2, 3) // Complex 2 + 3i
|
163 | const b = math.complex(2, 1) // Complex 2 + 1i
|
164 | math.Complex.compare(a,b) // returns 1
|
165 |
|
166 | //create from json
|
167 | const c = math.Complex.fromJSON({mathjs: 'Complex', re: 4, im: 3}) // Complex 4 + 3i
|
168 | ```
|