FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
mathjs/src/function/matrix/matrixFromFunction.js at develop · husayt/mathjs · GitHub
husayt
/
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
/
src
/
function
/
matrix
/
matrixFromFunction.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
107 lines (102 loc) · 4.36 KB
Breadcrumbs
mathjs
/
src
/
function
/
matrix
/
matrixFromFunction.js
Copy path
File metadata and controls
107 lines (102 loc) · 4.36 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
105
106
107
import
{
factory
}
from
'../../utils/factory.js'
const
name
=
'matrixFromFunction'
const
dependencies
=
[
'typed'
,
'matrix'
,
'isZero'
]
export
const
createMatrixFromFunction
=
/* #__PURE__ */
factory
(
name
,
dependencies
,
(
{
typed
,
matrix
,
isZero
}
)
=>
{
/**
* Create a matrix by evaluating a generating function at each index.
* The simplest overload returns a multi-dimensional array as long as `size`
* is an array.
* Passing `size` as a Matrix or specifying a `format` will result in
* returning a Matrix.
*
* Syntax:
*
* math.matrixFromFunction(size, fn)
* math.matrixFromFunction(size, fn, format)
* math.matrixFromFunction(size, fn, format, datatype)
* math.matrixFromFunction(size, format, fn)
* math.matrixFromFunction(size, format, datatype, fn)
*
* Where:
*
* - `size: (number[] | Matrix)`
* A vector giving the extent of the array to be created in each
* dimension. If size has one entry, a vector is created; if it
* has two, a rectangular array/Matrix is created; if three, a
* three-dimensional array/Matrix is created; and so on.
* - `fn: (index: number[]) => MathType`
* The callback function that will generate the entries of the
* matrix. It is called in turn with the index of each entry of
* the matrix. The index is always an ordinary array of numbers
* with the same length as _size_. So for vectors, you will get
* indices like `[0]` or `[1]`, whereas for matrices, you will
* get indices like `[2, 0]` or `[1,3]`. The return value may
* be any type that can go in an array or Matrix entry, although
* if you supply the _datatype_ argument, you must yourself ensure
* the type of the return value matches. Note that currently,
* your callback _fn_ will receive 0-based indices for the matrix
* entries, regardless of whether matrixFromFunction is invoked
* directly from JavaScript or via the mathjs expression language.
* - `format: 'dense'|'sparse'`
* Specifies the storage format for the resulting Matrix. Note that
* if this argument is given, the return value will always be a
* Matrix (rather than possibly an Array).
* - `datatype: string`
* Specifies the data type of entries of the new matrix. If given,
* it should be the name of a data type that mathjs supports, as
* returned by the math.typeOf function. It is up to the caller
* to make certain that all values returned by _fn_ are consistent
* with this datatype if specified.
*
* Examples:
*
* math.matrixFromFunction([3,3], i => i[0] - i[1]) // an antisymmetric matrix
* math.matrixFromFunction([100, 100], 'sparse', i => i[0] - i[1] === 1 ? 4 : 0) // a sparse subdiagonal matrix
* math.matrixFromFunction([5], i => math.random()) // a random vector
*
* See also:
*
* matrix, typeOf, zeros
*
*
@param
{
Array | Matrix
} size The size of the matrix to be created
*
@param
{
function
} fn Callback function invoked for every entry in the matrix
*
@param
{
string
} [format] The Matrix storage format, either `'dense'` or `'sparse'`
*
@param
{
string
} [datatype] Type of the values
*
@return
{
Array | Matrix
} Returns the created matrix
*/
return
typed
(
name
,
{
'Array | Matrix, function, string, string'
:
function
(
size
,
fn
,
format
,
datatype
)
{
return
_create
(
size
,
fn
,
format
,
datatype
)
}
,
'Array | Matrix, function, string'
:
function
(
size
,
fn
,
format
)
{
return
_create
(
size
,
fn
,
format
)
}
,
'Matrix, function'
:
function
(
size
,
fn
)
{
return
_create
(
size
,
fn
,
'dense'
)
}
,
'Array, function'
:
function
(
size
,
fn
)
{
return
_create
(
size
,
fn
,
'dense'
)
.
toArray
(
)
}
,
'Array | Matrix, string, function'
:
function
(
size
,
format
,
fn
)
{
return
_create
(
size
,
fn
,
format
)
}
,
'Array | Matrix, string, string, function'
:
function
(
size
,
format
,
datatype
,
fn
)
{
return
_create
(
size
,
fn
,
format
,
datatype
)
}
}
)
function
_create
(
size
,
fn
,
format
,
datatype
)
{
let
m
if
(
datatype
!==
undefined
)
{
m
=
matrix
(
format
,
datatype
)
}
else
{
m
=
matrix
(
format
)
}
m
.
resize
(
size
)
m
.
forEach
(
function
(
_
,
index
)
{
const
val
=
fn
(
index
)
if
(
isZero
(
val
)
)
return
m
.
set
(
index
,
val
)
}
)
return
m
}
}
)
Back
|
FazBrowse Home
|
New Git URL