gleam-community-maths/README.md

64 lines
1.8 KiB
Markdown
Raw Normal View History

2023-01-04 19:39:01 +00:00
# gleam-community/maths
2022-12-23 14:26:58 +00:00
2023-01-04 19:39:01 +00:00
[![Package Version](https://img.shields.io/hexpm/v/gleam_community_maths)](https://hex.pm/packages/gleam_community_maths)
[![Hex Docs](https://img.shields.io/badge/hex-docs-ffaff3)](https://hexdocs.pm/gleam_community_maths/)
2022-12-23 14:26:58 +00:00
2023-01-04 19:39:01 +00:00
A basic mathematics library that contains some of the most fundamental mathematics functions and utilities.
2022-12-23 14:26:58 +00:00
2023-01-04 19:39:01 +00:00
The library supports both targets: Erlang and JavaScript.
## Quickstart
```gleam
2024-08-16 23:13:57 +00:00
import gleam/float
2024-12-07 22:46:49 +00:00
import gleam/yielder
import gleam_community/maths
2024-08-16 23:13:57 +00:00
import gleeunit/should
2023-01-04 19:39:01 +00:00
2024-08-16 23:13:57 +00:00
pub fn example() {
2023-01-04 19:39:01 +00:00
// Evaluate the sine function
2024-12-07 22:46:49 +00:00
let result = maths.sin(maths.pi())
2024-08-16 23:13:57 +00:00
// Set the relative and absolute tolerance
2024-12-07 22:46:49 +00:00
let assert Ok(absolute_tol) = float.power(10.0, -6.0)
2024-08-16 23:13:57 +00:00
let relative_tol = 0.0
// Check that the value is very close to 0.0
// That is, if 'result' is within +/- 10^(-6)
2024-12-07 22:46:49 +00:00
maths.is_close(result, 0.0, relative_tol, absolute_tol)
2024-08-16 23:13:57 +00:00
|> should.be_true()
2023-01-04 19:39:01 +00:00
// Find the greatest common divisor
2024-12-07 22:46:49 +00:00
maths.gcd(54, 24)
2024-08-16 23:13:57 +00:00
|> should.equal(6)
2023-01-04 19:39:01 +00:00
// Find the minimum and maximum of a list
2024-12-07 22:46:49 +00:00
maths.extrema([10.0, 3.0, 50.0, 20.0, 3.0], float.compare)
2024-08-16 23:13:57 +00:00
|> should.equal(Ok(#(3.0, 50.0)))
2023-09-17 12:15:10 +00:00
2023-09-17 12:15:35 +00:00
// Determine if a number is fractional
2024-12-07 22:46:49 +00:00
maths.is_fractional(0.3333)
2024-08-16 23:13:57 +00:00
|> should.equal(True)
2023-09-17 12:15:10 +00:00
2024-08-16 23:13:57 +00:00
// Generate all k = 2 combinations of [1, 2, 3]
2024-12-07 22:46:49 +00:00
let assert Ok(combinations) = maths.list_combination([1, 2, 3], 2)
2024-08-16 23:13:57 +00:00
combinations
2024-12-07 22:46:49 +00:00
|> yielder.to_list()
2024-08-16 23:13:57 +00:00
|> should.equal([[1, 2], [1, 3], [2, 3]])
2024-12-07 22:46:49 +00:00
// Compute the Cosine Similarity between two (orthogonal) vectors
maths.cosine_similarity([#(-1.0, 1.0), #(1.0, 1.0), #(0.0, -1.0)])
|> should.equal(Ok(0.0))
2023-01-04 19:39:01 +00:00
}
2024-08-16 23:13:57 +00:00
2022-12-23 14:26:58 +00:00
```
## Installation
2023-01-04 19:39:01 +00:00
`gleam_community` packages are published to [hex.pm](https://hex.pm/packages/gleam_community_maths)
with the prefix `gleam_community_`. You can add them to your Gleam projects directly:
2022-12-23 14:26:58 +00:00
```sh
2023-01-04 19:39:01 +00:00
gleam add gleam_community_maths
2023-01-22 21:39:10 +00:00
```