• Stars
    star
    101
  • Rank 327,231 (Top 7 %)
  • Language
    Elixir
  • License
    Other
  • Created about 8 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

The missing Math module for Elixir.

Math

hex.pm version Build Status

The Math module adds many useful functions that extend Elixir's standard library.

  • General Functions

    • a <~> b Comparison of floats, to check if they are effectively equal (if their absolute difference is less than @epsilon).
    • Math.pow(x, n) Arithmetic exponentiation. Works both with integer powers and floats.
    • Math.sqrt(x) The square root of x.
    • Math.nth_root(x, n) The n-th root of x.
    • Math.isqrt(x) The integer square root of x.
    • Math.gcd(a, b) The greatest common divisor of a and b.
    • Math.egcd(a, b) Calculates integers gcd, s, and t in as + bt = gcd(a,b). See also Math.gcd(a, b)
    • Math.lcm(a, b) The least common multiple of a and b.
    • Math.factorial(n) The n-th factorial number.
    • Math.k_permutations(n, k) The number of distinct ways to create groups of size k from n distinct elements.
    • Math.k_combinations(n, k) The number of distinct ways to create groups of size k from n distinct elements where order does not matter.
    • Math.mod_inv(a, m) An integer b that fufills ab = 1 (mod m). Returns an ok/error tuple.
    • Math.mod_inv!(a, m) See Math.mod_inv(a, m), but returns the value or raises an error.
  • Logarithms

    • Math.exp(x) Calculates β„― to the xth power.
    • Math.log(x) Calculates the natural logarithm (base β„―) of x.
    • Math.log(x, b) Calculates the base-b logarithm of x
    • Math.log2(x) Calculates the binary logarithm (base 2) of x.
    • Math.log10(x) Calculates the common logarithm (base 10) of x.
    • Math.e Returns a floating-point approximation of the number β„―.
  • Trigonometry

    • Math.pi Returns a floating-point approximation of the number Ο€.
    • Math.deg2rad(x) converts from degrees to radians.
    • Math.rad2deg(x) converts from radians to degrees.
    • Math.sin(x) The sine of x.
    • Math.cos(x) The cosine of x.
    • Math.tan(x) The tangent of x.
    • Math.asin(x) The inverse sine of x.
    • Math.acos(x) The inverse cosine of x.
    • Math.atan(x) The inverse tangent of x.
    • Math.atan2(x, y) The inverse tangent of x and y. This variant returns the inverse tangent in the correct quadrant, as the signs of both x and y are known.
    • Math.sinh(x) The hyperbolic sine of x.
    • Math.cosh(x) The hyperbolic cosine of x.
    • Math.tanh(x) The hyperbolic tangent of x.
    • Math.asinh(x) The inverse hyperbolic sine of x.
    • Math.acosh(x) The inverse hyperbolic cosine of x.
    • Math.atanh(x) The inverse hyperbolic tangent of x.
  • Interpolation

    • Math.linear_interpolation(t,p0,p1) Returns {x,y} of t in a linear interpolation
    • Math.interpolation(fn t,x,y end, t,[p0,p1 ...]) Returns {x,y} of t in a given function of interpolation
    • Math.bezier_curve(t,[p0,p1 ...]) Returns {x,y} of t in a bezier_curve
  • Working with Collections

    • Math.Enum.product(collection) The result of multiplying all elements in the passed collection.
    • Math.Enum.mean(collection) the mean of the numbers in the collection.
    • Math.Enum.median(collection) the median of the numbers in the collection.
    • Math.Enum.mode(collection) the mode of the numbers in the collection.
    • Math.Enum.variance(collection) variance of the numbers in the collection.
    • Math.Enum.stdev(collection) the standard deviation of the numbers in the collection.

Installation

Math is available in Hex. The package can be installed by:

  1. Add math to your list of dependencies in mix.exs:
def deps do
  [
    {:math, "~> 0.6.0"}
  ]
end
  1. Require or import the Math library anywhere in your code you'd like:
require Math

# or

import Math

(Importing allows usage of the <~> operator)

Changelog

  • 0.7.0 adds Math.linear_interpolation/3 and Math.bezier_curve/2. Thank you, @pcarvsilva !
  • 0.6.0 Adds Math.Enum.variance/1 and Math.Enum.stdev/1. Thank you, @TenTakano !
  • 0.5.0 Adds Math.mod_inv, Math.mod_inv! and Math.egcd
  • 0.4.0 Adds Math.Enum.mode/1.
  • 0.3.1 Updates formatting to hide warnings in newer versions of Elixir.
  • 0.3.0 Fixed incorrect median for lists with even number of items. Updated tests.
  • 0.2.0 Added factorial/1, nth_sqrt/2, k_permutations/2, k_combinations/2, gcd/2, lcm/2 and Math.Enum functions. Improved documentation.
  • 0.1.0 Added integer variant of pow/1, isqrt/2, deg2rad/1, rad2deg/1. Improved documentation.
  • 0.0.1 First implementation, mostly a wrapper around Erlang's :math library.

More Repositories

1

bento

🍱 A fast, correct, pure-Elixir library for reading and writing Bencoded metainfo (.torrent) files.
Elixir
92
star
2

cleverbot.py

ARCHIVED - Use https://github.com/edwardslabs/cleverwrap.py instead.
Python
77
star
3

hydra-ts

A fork of ojack/hydra-synth in typescript, focusing on interoperability.
TypeScript
44
star
4

reaction-packs

Browser extensions to swap out Facebook's reactions for something a little more fun.
JavaScript
33
star
5

unsticker.me

unsticker.me
JavaScript
23
star
6

hiasynth

livecode visual synthesis environment
TypeScript
18
star
7

left_pad.ex

Pad a string to the left with any number of characters.
Elixir
12
star
8

hackers-new

Browser extension to show items from HN's /newest page on the front page.
JavaScript
11
star
9

Djitter

A Django Twitter clone built for Hackers @ Berkeley's Django talk
JavaScript
6
star
10

Genesis

Game + Game engine in Python using PyGame
Python
4
star
11

Museau

[Dead because of API changes] Listen to unlimited Pandora radio - no ads, listening limits, or (depending on your browser) plugins.
JavaScript
4
star
12

tractor

🚜 A fast and scalable pure-Elixir BitTorrent tracker. (On Hold)
JavaScript
4
star
13

broccoli-site

Old web site for broccolijs.com
HTML
4
star
14

Calculus-Blasters

Explode asteroids and solve calculus word problems the fun way!
Python
3
star
15

drop

Drop!
Python
2
star
16

folz

2
star
17

skill.ex

A skill-based ranking algorithms library for Elixir. Includes Elo and TrueSkill. (On Hold)
Elixir
2
star
18

Newsbrief

Teaching computers how to summarize the news.
Python
1
star
19

HackHackHack-backend

1
star
20

concerted

HackDFW hackathon project. Trigger events when enough people dance together.
Elixir
1
star
21

Eventually

JavaScript
1
star
22

map

JavaScript
1
star
23

Fundamentals

A collection of fundamental algorithms and data structures in various languages.
JavaScript
1
star
24

wdd

wdd
Ruby
1
star
25

buyc

JavaScript
1
star
26

GoogleForBulat

Could someone Google that for Bulat?
Python
1
star
27

lecturegauge

HackDuke '14 project. Realtime feedback tool for lecture.
JavaScript
1
star