FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
mathjs/examples/matrices.js at v4 · JavaScriptIOT/mathjs · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
JavaScriptIOT
/
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
/
matrices.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
104 lines (88 loc) · 3.57 KB
Breadcrumbs
mathjs
/
examples
/
matrices.js
Copy path
File metadata and controls
104 lines (88 loc) · 3.57 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// matrices
// load math.js (using node.js)
var
math
=
require
(
'../index'
)
;
// create matrices and arrays. a matrix is just a wrapper around an Array,
// providing some handy utilities.
console
.
log
(
'create a matrix'
)
;
var
a
=
math
.
matrix
(
[
1
,
4
,
9
,
16
,
25
]
)
;
print
(
a
)
;
// [1, 4, 9, 16, 25]
var
b
=
math
.
matrix
(
math
.
ones
(
[
2
,
3
]
)
)
;
print
(
b
)
;
// [[1, 1, 1], [1, 1, 1]]
print
(
b
.
size
(
)
)
;
// [2, 3]
// the Array data of a Matrix can be retrieved using valueOf()
var
array
=
a
.
valueOf
(
)
;
print
(
array
)
;
// [1, 4, 9, 16, 25]
// Matrices can be cloned
var
clone
=
a
.
clone
(
)
;
print
(
clone
)
;
// [1, 4, 9, 16, 25]
console
.
log
(
)
;
// perform operations with matrices
console
.
log
(
'perform operations'
)
;
print
(
math
.
sqrt
(
a
)
)
;
// [1, 2, 3, 4, 5]
var
c
=
[
1
,
2
,
3
,
4
,
5
]
;
print
(
math
.
factorial
(
c
)
)
;
// [1, 2, 6, 24, 120]
console
.
log
(
)
;
// create and manipulate matrices. Arrays and Matrices can be used mixed.
console
.
log
(
'manipulate matrices'
)
;
var
d
=
[
[
1
,
2
]
,
[
3
,
4
]
]
;
print
(
d
)
;
// [[1, 2], [3, 4]]
var
e
=
math
.
matrix
(
[
[
5
,
6
]
,
[
1
,
1
]
]
)
;
print
(
e
)
;
// [[5, 6], [1, 1]]
// set a submatrix.
// Matrix indexes are zero-based.
e
.
subset
(
math
.
index
(
1
,
[
0
,
1
]
)
,
[
[
7
,
8
]
]
)
;
print
(
e
)
;
// [[5, 6], [7, 8]]
var
f
=
math
.
multiply
(
d
,
e
)
;
print
(
f
)
;
// [[19, 22], [43, 50]]
var
g
=
f
.
subset
(
math
.
index
(
1
,
0
)
)
;
print
(
g
)
;
// 43
console
.
log
(
)
;
// get a sub matrix
// Matrix indexes are zero-based.
console
.
log
(
'get a sub matrix'
)
;
var
h
=
math
.
diag
(
math
.
range
(
1
,
4
)
)
;
print
(
h
)
;
// [[1, 0, 0], [0, 2, 0], [0, 0, 3]]
print
(
h
.
subset
(
math
.
index
(
[
1
,
2
]
,
[
1
,
2
]
)
)
)
;
// [[2, 0], [0, 3]]
var
i
=
math
.
range
(
1
,
6
)
;
print
(
i
)
;
// [1, 2, 3, 4, 5]
print
(
i
.
subset
(
math
.
index
(
math
.
range
(
1
,
4
)
)
)
)
;
// [2, 3, 4]
console
.
log
(
)
;
// resize a multi dimensional matrix
console
.
log
(
'resizing a matrix'
)
;
var
j
=
math
.
matrix
(
)
;
var
defaultValue
=
0
;
j
.
resize
(
[
2
,
2
,
2
]
,
defaultValue
)
;
print
(
j
)
;
// [[[0, 0], [0, 0]], [[0, 0], [0, 0]]]
print
(
j
.
size
(
)
)
;
// [2, 2, 2]
j
.
resize
(
[
2
,
2
]
)
;
print
(
j
)
;
// [[0, 0], [0, 0]]
print
(
j
.
size
(
)
)
;
// [2, 2]
console
.
log
(
)
;
// setting a value outside the matrices range will resize the matrix.
// new elements will be initialized with zero.
console
.
log
(
'set a value outside a matrices range'
)
;
var
k
=
math
.
matrix
(
)
;
k
.
subset
(
math
.
index
(
2
)
,
6
)
;
print
(
k
)
;
// [0, 0, 6]
console
.
log
(
)
;
console
.
log
(
'set a value outside a matrices range, leaving new entries uninitialized'
)
;
var
m
=
math
.
matrix
(
)
;
defaultValue
=
math
.
uninitialized
;
m
.
subset
(
math
.
index
(
2
)
,
6
,
defaultValue
)
;
print
(
m
)
;
// [undefined, undefined, 6]
console
.
log
(
)
;
// create ranges
console
.
log
(
'create ranges'
)
;
print
(
math
.
range
(
1
,
6
)
)
;
// [1, 2, 3, 4, 5]
print
(
math
.
range
(
0
,
18
,
3
)
)
;
// [0, 3, 6, 9, 12, 15]
print
(
math
.
range
(
'2:-1:-3'
)
)
;
// [2, 1, 0, -1, -2]
print
(
math
.
factorial
(
math
.
range
(
'1:6'
)
)
)
;
// [1, 2, 6, 24, 120]
console
.
log
(
)
;
/**
* Helper function to output a value in the console. Value will be formatted.
*
@param
{
*
} value
*/
function
print
(
value
)
{
var
precision
=
14
;
console
.
log
(
math
.
format
(
value
,
precision
)
)
;
}
Back
|
FazBrowse Home
|
New Git URL