changed
README.md
|
@@ -52,6 +52,8 @@ The Math module adds many useful functions that extend Elixir's standard library
|
52
52
|
- `Math.Enum.mean(collection)` the mean of the numbers in the collection.
|
53
53
|
- `Math.Enum.median(collection)` the median of the numbers in the collection.
|
54
54
|
- `Math.Enum.mode(collection)` the mode of the numbers in the collection.
|
55
|
+ - `Math.Enum.variance(collection)` variance of the numbers in the collection.
|
56
|
+ - `Math.Enum.stdev(collection)` the standard deviation of the numbers in the collection.
|
55
57
|
|
56
58
|
## Installation
|
57
59
|
|
|
@@ -62,7 +64,7 @@ Math is [available in Hex](https://hex.pm/packages/math). The package can be ins
|
62
64
|
```ex
|
63
65
|
def deps do
|
64
66
|
[
|
65
|
- {:math, "~> 0.4.0"}
|
67
|
+ {:math, "~> 0.6.0"}
|
66
68
|
]
|
67
69
|
end
|
68
70
|
```
|
|
@@ -80,6 +82,7 @@ import Math
|
80
82
|
(Importing allows usage of the `<~>` operator)
|
81
83
|
|
82
84
|
## Changelog
|
85
|
+ - 0.6.0 Adds `Math.Enum.variance/1` and `Math.Enum.stdev/1`. Thank you, @TenTakano !
|
83
86
|
- 0.5.0 Adds `Math.mod_inv`, `Math.mod_inv!` and `Math.egcd`
|
84
87
|
- 0.4.0 Adds `Math.Enum.mode/1`.
|
85
88
|
- 0.3.1 Updates formatting to hide warnings in newer versions of Elixir.
|
changed
hex_metadata.config
|
@@ -2,7 +2,7 @@
|
2
2
|
{<<"build_tools">>,[<<"mix">>]}.
|
3
3
|
{<<"description">>,
|
4
4
|
<<"The Math library extends Elixir with many common math-related functions, constants and (optionally) operators.">>}.
|
5
|
- {<<"elixir">>,<<"~> 1.2">>}.
|
5
|
+ {<<"elixir">>,<<"~> 1.4">>}.
|
6
6
|
{<<"files">>,
|
7
7
|
[<<"lib">>,<<"lib/math.ex">>,<<"lib/math">>,<<"lib/math/enum.ex">>,
|
8
8
|
<<"mix.exs">>,<<"README.md">>,<<"LICENSE">>]}.
|
|
@@ -10,4 +10,4 @@
|
10
10
|
{<<"links">>,[{<<"GitHub">>,<<"https://github.com/folz/math">>}]}.
|
11
11
|
{<<"name">>,<<"math">>}.
|
12
12
|
{<<"requirements">>,[]}.
|
13
|
- {<<"version">>,<<"0.5.0">>}.
|
13
|
+ {<<"version">>,<<"0.6.0">>}.
|
changed
lib/math/enum.ex
|
@@ -127,4 +127,61 @@ defmodule Math.Enum do
|
127
127
|
|> Enum.max_by(&elem(&1, 0), fn -> {nil, []} end)
|
128
128
|
|> elem(1)
|
129
129
|
end
|
130
|
+
|
131
|
+ @doc """
|
132
|
+ Calculates the variance of a given collection of numbers.
|
133
|
+
|
134
|
+ If the collection is empty, returns `nil`
|
135
|
+
|
136
|
+ ## Examples
|
137
|
+ iex> Math.Enum.variance [1, 2, 3, 4, 5]
|
138
|
+ 2.0
|
139
|
+ iex> Math.Enum.variance 1..10
|
140
|
+ 8.25
|
141
|
+ iex> Math.Enum.variance [1, 2, 3, 4, 5, -100]
|
142
|
+ 1475.138888888889
|
143
|
+ iex> Math.Enum.variance []
|
144
|
+ nil
|
145
|
+ """
|
146
|
+
|
147
|
+ @spec variance(Enum.t()) :: number | nil
|
148
|
+ def variance(collection)
|
149
|
+
|
150
|
+ def variance(collection) do
|
151
|
+ count = Enum.count(collection)
|
152
|
+
|
153
|
+ case count do
|
154
|
+ 0 -> nil
|
155
|
+ _ ->
|
156
|
+ mean_square = (Enum.map(collection, &Math.pow(&1, 2)) |> Enum.sum()) / count
|
157
|
+ squared_mean = mean(collection) |> Math.pow(2)
|
158
|
+ mean_square - squared_mean
|
159
|
+ end
|
160
|
+ end
|
161
|
+
|
162
|
+ @doc """
|
163
|
+ Calculates the standard deviation of a given collection of numbers.
|
164
|
+
|
165
|
+ If the collection is empty, returns `nil`
|
166
|
+
|
167
|
+ ## Examples
|
168
|
+ iex> Math.Enum.stdev([1,2,3,4,5])
|
169
|
+ 1.4142135623730951
|
170
|
+ iex> Math.Enum.stdev 1..10
|
171
|
+ 2.8722813232690143
|
172
|
+ iex> Math.Enum.stdev [1,2,3,4,5,-100]
|
173
|
+ 38.407536876098796
|
174
|
+ iex> Math.Enum.stdev []
|
175
|
+ nil
|
176
|
+ """
|
177
|
+
|
178
|
+ @spec stdev(Enum.t()) :: number | nil
|
179
|
+ def stdev(collection)
|
180
|
+
|
181
|
+ def stdev(collection) do
|
182
|
+ case variance(collection) do
|
183
|
+ nil -> nil
|
184
|
+ res -> Math.sqrt(res)
|
185
|
+ end
|
186
|
+ end
|
130
187
|
end
|
changed
mix.exs
|
@@ -6,8 +6,8 @@ defmodule Math.Mixfile do
|
6
6
|
def project do
|
7
7
|
[
|
8
8
|
app: :math,
|
9
|
- version: "0.5.0",
|
10
|
- elixir: "~> 1.2",
|
9
|
+ version: "0.6.0",
|
10
|
+ elixir: "~> 1.4",
|
11
11
|
description: description(),
|
12
12
|
package: package(),
|
13
13
|
deps: deps(),
|