FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
mathjs/src/function/statistics/mean.js at develop · pointGH/mathjs · GitHub
pointGH
/
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
/
statistics
/
mean.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
96 lines (87 loc) · 2.65 KB
Breadcrumbs
mathjs
/
src
/
function
/
statistics
/
mean.js
Copy path
File metadata and controls
96 lines (87 loc) · 2.65 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
import
{
containsCollections
,
deepForEach
,
reduce
}
from
'../../utils/collection'
import
{
arraySize
}
from
'../../utils/array'
import
{
factory
}
from
'../../utils/factory'
import
{
improveErrorMessage
}
from
'./utils/improveErrorMessage'
const
name
=
'mean'
const
dependencies
=
[
'typed'
,
'add'
,
'divide'
]
export
const
createMean
=
/* #__PURE__ */
factory
(
name
,
dependencies
,
(
{
typed
,
add
,
divide
}
)
=>
{
/**
* Compute the mean value of matrix or a list with values.
* In case of a multi dimensional array, the mean of the flattened array
* will be calculated. When `dim` is provided, the maximum over the selected
* dimension will be calculated. Parameter `dim` is zero-based.
*
* Syntax:
*
* math.mean(a, b, c, ...)
* math.mean(A)
* math.mean(A, dim)
*
* Examples:
*
* math.mean(2, 1, 4, 3) // returns 2.5
* math.mean([1, 2.7, 3.2, 4]) // returns 2.725
*
* math.mean([[2, 5], [6, 3], [1, 7]], 0) // returns [3, 5]
* math.mean([[2, 5], [6, 3], [1, 7]], 1) // returns [3.5, 4.5, 4]
*
* See also:
*
* median, min, max, sum, prod, std, variance
*
*
@param
{
... *
} args A single matrix or or multiple scalar values
*
@return
{
*
} The mean of all values
*/
return
typed
(
name
,
{
// mean([a, b, c, d, ...])
'Array | Matrix'
:
_mean
,
// mean([a, b, c, d, ...], dim)
'Array | Matrix, number | BigNumber'
:
_nmeanDim
,
// mean(a, b, c, d, ...)
'...'
:
function
(
args
)
{
if
(
containsCollections
(
args
)
)
{
throw
new
TypeError
(
'Scalar values expected in function mean'
)
}
return
_mean
(
args
)
}
}
)
/**
* Calculate the mean value in an n-dimensional array, returning a
* n-1 dimensional array
*
@param
{
Array
} array
*
@param
{
number
} dim
*
@return
{
number
} mean
*
@private
*/
function
_nmeanDim
(
array
,
dim
)
{
try
{
const
sum
=
reduce
(
array
,
dim
,
add
)
const
s
=
Array
.
isArray
(
array
)
?
arraySize
(
array
)
:
array
.
size
(
)
return
divide
(
sum
,
s
[
dim
]
)
}
catch
(
err
)
{
throw
improveErrorMessage
(
err
,
'mean'
)
}
}
/**
* Recursively calculate the mean value in an n-dimensional array
*
@param
{
Array
} array
*
@return
{
number
} mean
*
@private
*/
function
_mean
(
array
)
{
let
sum
let
num
=
0
deepForEach
(
array
,
function
(
value
)
{
try
{
sum
=
sum
===
undefined
?
value
:
add
(
sum
,
value
)
num
++
}
catch
(
err
)
{
throw
improveErrorMessage
(
err
,
'mean'
,
value
)
}
}
)
if
(
num
===
0
)
{
throw
new
Error
(
'Cannot calculate the mean of an empty array'
)
}
return
divide
(
sum
,
num
)
}
}
)
Back
|
FazBrowse Home
|
New Git URL