UNPKG

5.58 kBMarkdownView Raw
1# Complex Numbers
2
3Math.js supports the creation, manipulation, and calculations with complex numbers.
4Support of complex numbers is powered by the library [complex.js](https://github.com/infusion/Complex.js).
5
6In mathematics, a complex number is an expression of the form `a + bi`,
7where `a` and `b` are real numbers and `i` represents the imaginary number
8defined as `i^2 = -1`. (In other words, `i` is the square root of `-1`.)
9The real number `a` is called the real part of the complex number,
10and the real number `b` is the imaginary part. For example, `3 + 2i` is a
11complex number, having real part `3` and imaginary part `2`.
12Complex numbers are often used in applied mathematics, control theory,
13signal analysis, fluid dynamics and other fields.
14
15## Usage
16
17A complex number is created using the function `math.complex`. This function
18accepts:
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.
26The function returns a `Complex` object.
27
28Syntax:
29
30```js
31math.complex(re: number) : Complex
32math.complex(re: number, im: number) : Complex
33math.complex(complex: Complex) : Complex
34math.complex({re: Number, im: Number}) : Complex
35math.complex({r: number, phi: number}) : Complex
36math.complex({abs: number, arg: number}) : Complex
37math.complex(str: string) : Complex
38```
39
40Examples:
41
42```js
43const a = math.complex(2, 3) // Complex 2 + 3i
44a.re // Number 2
45a.im // Number 3
46
47const b = math.complex('4 - 2i') // Complex 4 - 2i
48b.re = 5 // Number 5
49b // Complex 5 - 2i
50```
51
52## Calculations
53
54Most functions of math.js support complex numbers. Complex and real numbers
55can be used together.
56
57```js
58const a = math.complex(2, 3) // Complex 2 + 3i
59const b = math.complex('4 - 2i') // Complex 4 - 2i
60
61math.re(a) // Number 2
62math.im(a) // Number 3
63math.conj(a) // Complex 2 - 3i
64
65math.add(a, b) // Complex 6 + i
66math.multiply(a, 2) // Complex 4 + 6i
67math.sqrt(-4) // Complex 2i
68```
69
70## API
71A `Complex` object created by `math.complex` contains the following properties and functions:
72
73### complex.re
74
75A number containing the real part of the complex number. Can be read and replaced.
76
77### complex.im
78
79A number containing the imaginary part of the complex number. Can be read and replaced.
80
81### complex.clone()
82
83Create a clone of the complex number.
84
85### complex.equals(other)
86
87Test 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
94Returns 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
98Returns 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
102Returns a complex number that is inverse of the current complex number.
103
104### complex.toVector()
105
106Get the vector representation of the current complex number. Returns an array of size 2.
107
108### complex.toJSON()
109
110Returns 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
116Get the polar coordinates of the complex number, returns
117 an object with properties `r` and `phi`.
118
119### complex.toString()
120
121Returns 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
127Get 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
133The following static methods can be accessed using `math.Complex`
134
135
136### Complex.fromJSON(json)
137
138Revive 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
145Create a complex number from polar coordinates.
146
147
148### Complex.compare(a: Complex, b: Complex)
149
150Returns 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
160Example:
161```js
162const a = math.complex(2, 3) // Complex 2 + 3i
163const b = math.complex(2, 1) // Complex 2 + 1i
164math.Complex.compare(a,b) // returns 1
165
166//create from json
167const c = math.Complex.fromJSON({mathjs: 'Complex', re: 4, im: 3}) // Complex 4 + 3i
168```