FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
mathjs/examples/complex_numbers.js at develop · niebert/mathjs · GitHub
niebert
/
mathjs
Public
forked from
josdejong/mathjs
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
mathjs
/
examples
/
complex_numbers.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
67 lines (55 loc) · 1.95 KB
Breadcrumbs
mathjs
/
examples
/
complex_numbers.js
Copy path
File metadata and controls
67 lines (55 loc) · 1.95 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// complex numbers
// load js (using node.js)
const
{
complex
,
add
,
multiply
,
sin
,
sqrt
,
pi
,
equal
,
sort
,
format
}
=
require
(
'..'
)
// create a complex number with a numeric real and complex part
console
.
log
(
'create and manipulate complex numbers'
)
const
a
=
complex
(
2
,
3
)
print
(
a
)
// 2 + 3i
// read the real and complex parts of the complex number
print
(
a
.
re
)
// 2
print
(
a
.
im
)
// 3
// clone a complex value
const
clone
=
a
.
clone
(
)
print
(
clone
)
// 2 + 3i
// adjust the complex value
a
.
re
=
5
print
(
a
)
// 5 + 3i
// create a complex number by providing a string with real and complex parts
const
b
=
complex
(
'3-7i'
)
print
(
b
)
// 3 - 7i
console
.
log
(
)
// perform operations with complex numbers
console
.
log
(
'perform operations'
)
print
(
add
(
a
,
b
)
)
// 8 - 4i
print
(
multiply
(
a
,
b
)
)
// 36 - 26i
print
(
sin
(
a
)
)
// -9.6541254768548 + 2.8416922956064i
// some operations will return a complex number depending on the arguments
print
(
sqrt
(
4
)
)
// 2
print
(
sqrt
(
-
4
)
)
// 2i
console
.
log
(
)
// create a complex number from polar coordinates
console
.
log
(
'create complex numbers with polar coordinates'
)
const
c
=
complex
(
{
r
:
sqrt
(
2
)
,
phi
:
pi
/
4
}
)
print
(
c
)
// 1 + i
// get polar coordinates of a complex number
const
d
=
complex
(
3
,
4
)
console
.
log
(
d
.
abs
(
)
,
d
.
arg
(
)
)
// radius = 5, phi = 0.9272952180016122
console
.
log
(
)
// comparision operations
// note that there is no mathematical ordering defined for complex numbers
// we can only check equality. To sort a list with complex numbers,
// the natural sorting can be used
console
.
log
(
'\ncomparision and sorting operations'
)
console
.
log
(
'equal'
,
equal
(
a
,
b
)
)
// returns false
const
values
=
[
a
,
b
,
c
]
console
.
log
(
'values:'
,
format
(
values
,
14
)
)
// [5 + 3i, 3 - 7i, 1 + i]
sort
(
values
,
'natural'
)
console
.
log
(
'sorted:'
,
format
(
values
,
14
)
)
// [1 + i, 3 - 7i, 5 + 3i]
/**
* Helper function to output a value in the console. Value will be formatted.
*
@param
{
*
} value
*/
function
print
(
value
)
{
const
precision
=
14
console
.
log
(
format
(
value
,
precision
)
)
}
Back
|
FazBrowse Home
|
New Git URL