Numy v0.1.1 Numy.Enumy View Source

Extend Enum for homogeneous enumerables.

Link to this section Summary

Functions

Check if all elements of a list are floats.

Check if all elements of a list are integers.

Convert all numerical elements of a list to float type.

The dot product is the sum of the products of the corresponding entries of the two sequences of numbers.

Get mean (average) of a sequence of numbers.

Link to this section Functions

Link to this function

all_floats?(enumerable)

View Source
all_floats?(Enumerable.t()) :: boolean()

Check if all elements of a list are floats.

Examples

iex(10)> import Numy.Enumy
Numy.Enumy
iex(11)> all_floats?([1.1, 2.2, 3.3])
true
iex(12)> all_floats?([1.1, 2.2, 3])
false
Link to this function

all_integers?(enumerable)

View Source
all_integers?(Enumerable.t()) :: boolean()

Check if all elements of a list are integers.

Examples

iex(1)> import Numy.Enumy
Numy.Enumy
iex(2)> all_integers?([1, 2, 3])
true
iex(3)> all_integers?([1.1, 2, 3])
false
Link to this function

all_numbers?(enumerable)

View Source
all_numbers?(Enumerable.t()) :: boolean()
Link to this function

all_to_float(enumerable)

View Source
all_to_float(Enumerable.t()) :: [float()]

Convert all numerical elements of a list to float type.

Examples

iex(13)> all_to_float([1.1, 2.2, 3])
[1.1, 2.2, 3.0]
Link to this function

dot_product(vec1, vec2)

View Source
dot_product([number()], [number()]) :: number()

The dot product is the sum of the products of the corresponding entries of the two sequences of numbers.

Examples

iex> dot_product([1,2,3],[2,3,0])
8

Benchmarks

iex(1)> fn1 = fn(v1,v2) -> Numy.Enumy.dot_product(v1,v2) end
iex(2)> fn2 = fn(v1,v2) -> Enum.zip(v1,v2) |> Enum.map(fn {a,b} -> a * b end) |>
...(2)> Enum.reduce(&Kernel.+/2) end
iex(3)> fn3 = fn(v1,v2) -> Enum.zip(v1,v2) |> Flow.from_enumerable |>
...(3)> Flow.map(fn {a,b} -> a * b end) |> Enum.sum end
iex(4)> vec = Enum.to_list(1..999_000)
iex(20)> Benchee.run(%{"1" => fn -> fn1.(vec,vec) end,
...(20)> "2" => fn -> fn2.(vec,vec) end, "3" => fn -> fn3.(vec,vec) end})
Comparison:
1            22.58
3             0.58 - 38.84x slower +1.68 s
2             0.30 - 76.26x slower +3.33 s

Get mean (average) of a sequence of numbers.

Examples

iex(14)> mean([1,2,3,4,5,6,7,8,9])
5.0