diff --git a/src/gleam_community/maths/float.gleam b/src/gleam_community/maths/float.gleam index c661683..81ab4cb 100644 --- a/src/gleam_community/maths/float.gleam +++ b/src/gleam_community/maths/float.gleam @@ -19,7 +19,7 @@ //// .katex { font-size: 1.1em; } //// //// -//// A module containing mathematical functions and constants that apply to real numbers (floats). +//// A module containing mathematical functions and constants that apply to floats. //// //// --- //// @@ -1902,7 +1902,7 @@ pub fn to_radian(x: Float) -> Float { /// /// /// -/// The minimum function that takes two arguments $$x$$ and $$y$$ and returns the smallest of the two. +/// The minimum function that takes two arguments $$x, y \in \mathbb{R}$$ and returns the smallest of the two. /// ///
/// Example @@ -1938,7 +1938,7 @@ pub fn minimum(x: Float, y: Float) -> Float { /// /// /// -/// The maximum function that takes two arguments $$x$$ and $$y$$ and returns the largest of the two. +/// The maximum function that takes two arguments $$x, y \in \mathbb{R}$$ and returns the largest of the two. /// ///
/// Example @@ -1974,7 +1974,7 @@ pub fn maximum(x: Float, y: Float) -> Float { /// /// /// -/// The minmax function that takes two arguments $$x$$ and $$y$$ and returns a tuple with the smallest value first and largest second. +/// The minmax function that takes two arguments $$x, y \in \mathbb{R}$$ and returns a tuple with the smallest value first and largest second. /// ///
/// Example diff --git a/src/gleam_community/maths/float_list.gleam b/src/gleam_community/maths/float_list.gleam index e8334e5..4a76861 100644 --- a/src/gleam_community/maths/float_list.gleam +++ b/src/gleam_community/maths/float_list.gleam @@ -19,17 +19,19 @@ //// .katex { font-size: 1.1em; } //// //// -//// A module containing several different kinds of mathematical functions -//// applying to lists of real numbers. +//// A module containing mathematical functions applying to one or more lists of floats. //// -//// Function naming has been adopted from C mathematical function. -//// //// --- //// -//// * **Distances, sums and products** +//// * **Distances, sums, and products** //// * [`sum`](#sum) //// * [`product`](#product) //// * [`norm`](#norm) +//// * [`minkowski`](#minkowski) +//// * [`euclidean`](#euclidean) +//// * [`manhatten`](#manhatten) +//// * [`root_mean_squared_error`](#root_mean_squared_error) +//// * [`mean_squared_error`](#mean_squared_error) //// * [`cumulative_sum`](#cumulative_sum) //// * [`cumulative_product`](#cumulative_product) //// * **Ranges and intervals** @@ -52,6 +54,168 @@ import gleam/pair import gleam/option import gleam_community/maths/float as floatx +///
+/// +/// Spot a typo? Open an issue! +/// +///
+/// +///
+/// Example: +/// +/// import gleeunit/should +/// import gleam_community/maths/float_list +/// +/// pub fn example () { +/// +/// } +///
+/// +///
+/// +/// Back to top ↑ +/// +///
+/// +pub fn norm(xarr: List(Float), p: Int) -> Float { + todo +} + +///
+/// +/// Spot a typo? Open an issue! +/// +///
+/// +///
+/// Example: +/// +/// import gleeunit/should +/// import gleam_community/maths/float_list +/// +/// pub fn example () { +/// +/// } +///
+/// +///
+/// +/// Back to top ↑ +/// +///
+/// +pub fn minkowski(xarr: List(Float), yarr: List(Float), p: Int) -> Float { + todo +} + +///
+/// +/// Spot a typo? Open an issue! +/// +///
+/// +///
+/// Example: +/// +/// import gleeunit/should +/// import gleam_community/maths/float_list +/// +/// pub fn example () { +/// +/// } +///
+/// +///
+/// +/// Back to top ↑ +/// +///
+/// +pub fn euclidean(xarr: List(Float), yarr: List(Float)) -> Float { + todo +} + +///
+/// +/// Spot a typo? Open an issue! +/// +///
+/// +///
+/// Example: +/// +/// import gleeunit/should +/// import gleam_community/maths/float_list +/// +/// pub fn example () { +/// +/// } +///
+/// +///
+/// +/// Back to top ↑ +/// +///
+/// +pub fn manhatten(xarr: List(Float), yarr: List(Float)) -> Float { + todo +} + +///
+/// +/// Spot a typo? Open an issue! +/// +///
+/// +///
+/// Example: +/// +/// import gleeunit/should +/// import gleam_community/maths/float_list +/// +/// pub fn example () { +/// +/// } +///
+/// +///
+/// +/// Back to top ↑ +/// +///
+/// +pub fn mean_squared_error(xarr: List(Float), yarr: List(Float)) -> Float { + todo +} + +///
+/// +/// Spot a typo? Open an issue! +/// +///
+/// +///
+/// Example: +/// +/// import gleeunit/should +/// import gleam_community/maths/float_list +/// +/// pub fn example () { +/// +/// } +///
+/// +///
+/// +/// Back to top ↑ +/// +///
+/// +pub fn root_mean_squared_error(xarr: List(Float), yarr: List(Float)) -> Float { + todo +} + ///
/// /// Spot a typo? Open an issue! @@ -340,34 +504,6 @@ pub fn cumumlative_product(arr: List(Float)) -> List(Float) { todo } -///
-/// -/// Spot a typo? Open an issue! -/// -///
-/// -/// -///
-/// Example: -/// -/// import gleeunit/should -/// import gleam_community/maths/float_list -/// -/// pub fn example () { -/// -/// } -///
-/// -///
-/// -/// Back to top ↑ -/// -///
-/// -pub fn norm(xarr: List(Float), yarr: List(Float), p: Int) -> List(Float) { - todo -} - ///
/// /// Spot a typo? Open an issue! diff --git a/src/gleam_community/maths/int.gleam b/src/gleam_community/maths/int.gleam index 05c396e..e77cc83 100644 --- a/src/gleam_community/maths/int.gleam +++ b/src/gleam_community/maths/int.gleam @@ -19,7 +19,7 @@ //// .katex { font-size: 1.1em; } //// //// -//// A module containing mathematical functions applying to integer (int) numbers. +//// A module containing mathematical functions applying to integers. //// //// --- //// @@ -28,17 +28,6 @@ //// * [`sign`](#sign) //// * [`copy_sign`](#copysign) //// * [`flip_sign`](#flipsign) -//// * **Powers, logs and roots** -//// * [`exponential`](#exp) -//// * [`natural_logarithm`](#ln) -//// * [`logarithm`](#logarithm) -//// * [`logarithm_10`](#logarithm_10) -//// * [`logarithm_2`](#logarithm_2) -//// * [`power`](#power) -//// * [`square_root`](#square_root) -//// * [`cube_root`](#cube_root) -//// * [`nth_root`](#nth_root) -//// * [`hypotenuse`](#hypotenuse) //// * **Misc. mathematical functions** //// * [`minimum`](#min) //// * [`maxximum`](#max) @@ -46,12 +35,14 @@ //// * **Division functions** //// * [`gcd`](#gcd) //// * [`lcm`](#lcm) +//// * [`divisors`](#divisors) //// * **Combinatorial functions** //// * [`combination`](#combination) //// * [`factorial`](#factorial) //// * [`permutation`](#permutation) //// * **Tests** -//// * [`is_power`](#is_power_of) +//// * [`is_power`](#is_power) +//// * [`is_perfect`](#is_perfect) //// * [`is_even`](#is_even) //// * [`is_odd`](#isodd) //// * **Misc. functions** @@ -70,7 +61,7 @@ import gleam_community/maths/float as floatx /// ///
/// -/// The min function. +/// The minimum function that takes two arguments $$x, y \in \mathbb{Z}$$ and returns the smallest of the two. /// ///
/// Example: @@ -79,11 +70,11 @@ import gleam_community/maths/float as floatx /// import gleam_community/maths/int as intx /// /// pub fn example() { -/// intx.minimum(2.0, 1.5) -/// |> should.equal(1.5) +/// intx.minimum(2, 1) +/// |> should.equal(1) /// -/// intx.minimum(1.5, 2.0) -/// |> should.equal(1.5) +/// intx.minimum(1, 2) +/// |> should.equal(1) /// } ///
/// @@ -106,7 +97,7 @@ pub fn minimum(x: Int, y: Int) -> Int { /// ///
/// -/// The maximum function. +/// The maximum function that takes two arguments $$x, y \in \mathbb{Z}$$ and returns the largest of the two. /// ///
/// Example: @@ -115,11 +106,11 @@ pub fn minimum(x: Int, y: Int) -> Int { /// import gleam_community/maths/int as intx /// /// pub fn example() { -/// intx.maximum(2.0, 1.5) -/// |> should.equal(1.5) +/// intx.maximum(2, 1) +/// |> should.equal(1) /// -/// intx.maximum(1.5, 2.0) -/// |> should.equal(1.5) +/// intx.maximum(1, 2) +/// |> should.equal(1) /// } ///
/// @@ -142,7 +133,7 @@ pub fn maximum(x: Int, y: Int) -> Int { /// /// /// -/// The minmax function. +/// The minmax function that takes two arguments $$x, y \in \mathbb{Z}$$ and returns a tuple with the smallest value first and largest second. /// ///
/// Example: @@ -151,11 +142,11 @@ pub fn maximum(x: Int, y: Int) -> Int { /// import gleam_community/maths/int as intx /// /// pub fn example() { -/// intx.minmax(2.0, 1.5) -/// |> should.equal(#(1.5, 2.0)) +/// intx.minmax(2, 1) +/// |> should.equal(#(1, 2)) /// -/// intx.minmax(1.5, 2.0) -/// |> should.equal(#(1.5, 2.0)) +/// intx.minmax(1, 2) +/// |> should.equal(#(1, 2)) /// } ///
/// @@ -176,7 +167,7 @@ pub fn minmax(x: Int, y: Int) -> #(Int, Int) { /// /// /// The sign function which returns the sign of the input, indicating -/// whether it is positive, negative, or zero. +/// whether it is positive (+1), negative (-1), or zero (0). /// ///
/// @@ -212,6 +203,7 @@ if javascript { /// ///
/// +/// The function flips the sign of a given input value. /// ///
/// @@ -229,7 +221,7 @@ pub fn flip_sign(x: Int) -> Int { /// ///
/// -/// The function takes two arguments $$x$$ and $$y$$ and returns $$x$$ such that it has the same sign as $$y$$. +/// The function takes two arguments $$x, y \in \mathbb{Z}$$ and returns $$x$$ such that it has the same sign as $$y$$. /// ///
/// @@ -467,8 +459,7 @@ pub fn permutation(n: Int, k: Int) -> Result(Int, String) { /// \forall x, y \in \mathbb{Z}, \\; |x - y| \in \mathbb{Z}_{+}. /// \\] /// -/// The function takes two inputs $$x$$ and $$y$$ and returns a positive integer -/// value which is the the absolute difference of the inputs. +/// The function takes two inputs $$x$$ and $$y$$ and returns a positive integer value which is the the absolute difference of the inputs. /// ///
/// Example: @@ -502,6 +493,8 @@ pub fn absolute_difference(a: Int, b: Int) -> Int { /// ///
/// +/// A function that tests whether a given integer value $$x \in \mathbb{Z}$$ is a power of another integer value $$y \in \mathbb{Z}$$. +/// ///
/// Example: /// @@ -509,7 +502,13 @@ pub fn absolute_difference(a: Int, b: Int) -> Int { /// import gleam_community/maths/int as intx /// /// pub fn example() { +/// // Check if 4 is a power of 2 (it is) +/// intx.is_power(4, 2) +/// |> should.equal(True) /// +/// // Check if 5 is a power of 2 (it is not) +/// intx.is_power(5, 2) +/// |> should.equal(False) /// } ///
/// @@ -526,3 +525,267 @@ pub fn is_power(x: Int, y: Int) -> Bool { let rem = value -. truncated rem == 0.0 } + +///
+/// +/// Spot a typo? Open an issue! +/// +///
+/// +/// A function that tests whether a given integer value $$x \in \mathbb{Z}$$ is a perfect number. A number is perfect if it is equal to the sum of its proper positive divisors. +/// +///
+/// Details +/// +/// For example: +/// - $$6$$ is a perfect number since the divisors of 6 are $$1 + 2 + 3 = 6$$. +/// - $$28$$ is a perfect number since the divisors of 28 are $$1 + 2 + 4 + 7 + 14 = 28$$ +/// +///
+/// +///
+/// Example: +/// +/// import gleeunit/should +/// import gleam_community/maths/int as intx +/// +/// pub fn example() { +/// intx.is_perfect(6) +/// |> should.equal(True) +/// +/// intx.is_perfect(28) +/// |> should.equal(True) +/// } +///
+/// +///
+/// +/// Back to top ↑ +/// +///
+/// +pub fn is_perfect(x: Int, y: Int) -> Bool { + todo +} + +///
+/// +/// Spot a typo? Open an issue! +/// +///
+/// +/// The function returns all the positive divisors of an integer. +/// +///
+/// Example: +/// +/// import gleeunit/should +/// import gleam_community/maths/int as intx +/// +/// pub fn example() { +/// intx.divisors(4) +/// |> should.equal([1, 2, 4]) +/// +/// intx.divisors(6) +/// |> should.equal([1, 2, 3, 6]) +/// } +///
+/// +///
+/// +/// Back to top ↑ +/// +///
+/// +pub fn divisors(x: Int) -> List(Int) { + todo +} + +///
+/// +/// Spot a typo? Open an issue! +/// +///
+/// +/// The function calculates the greatest common multiple of two integers $$x, y \in \mathbb{Z}$$. +/// The greatest common multiple is the largest positive integer that is divisible by both $$x$$ and $$y$$. +/// +///
+/// Example: +/// +/// import gleeunit/should +/// import gleam_community/maths/int as intx +/// +/// pub fn example() { +/// intx.lcm(1, 1) +/// |> should.equal(1) +/// +/// intx.lcm(100, 10) +/// |> should.equal(10) +/// +/// intx.gcd(-36, -17) +/// |> should.equal(1) +/// } +///
+/// +///
+/// +/// Back to top ↑ +/// +///
+/// +pub fn gcd(x: Int, y: Int) -> Int { + let absx: Int = int.absolute_value(x) + let absy: Int = int.absolute_value(y) + do_gcd(absx, absy) +} + +pub fn do_gcd(x: Int, y: Int) -> Int { + case x == 0 { + True -> y + False -> { + assert Ok(z) = int.modulo(y, x) + do_gcd(z, x) + } + } +} + +///
+/// +/// Spot a typo? Open an issue! +/// +///
+/// +/// The function calculates the least common multiple of two integers $$x, y \in \mathbb{Z}$$. +/// The least common multiple is the smallest positive integer that has both $$x$$ and $$y$$ as factors. +/// +///
+/// Example: +/// +/// import gleeunit/should +/// import gleam_community/maths/int as intx +/// +/// pub fn example() { +/// intx.lcm(1, 1) +/// |> should.equal(1) +/// +/// intx.lcm(100, 10) +/// |> should.equal(100) +/// +/// intx.lcm(-36, -17) +/// |> should.equal(612) +/// } +///
+/// +///
+/// +/// Back to top ↑ +/// +///
+/// +pub fn lcm(x: Int, y: Int) -> Int { + let absx: Int = int.absolute_value(x) + let absy: Int = int.absolute_value(y) + absx * absy / do_gcd(absx, absy) +} + +///
+/// +/// Spot a typo? Open an issue! +/// +///
+/// +/// A function that tests whether a given integer value $$x \in \mathbb{Z}$$ is odd. +/// +///
+/// Example: +/// +/// import gleeunit/should +/// import gleam_community/maths/int as intx +/// +/// pub fn example() { +/// intx.is_odd(-3) +/// |> should.equal(True) +/// +/// intx.is_odd(-4) +/// |> should.equal(False) +/// } +///
+/// +///
+/// +/// Back to top ↑ +/// +///
+/// +pub fn is_odd(x: Int) -> Bool { + x % 2 != 0 +} + +///
+/// +/// Spot a typo? Open an issue! +/// +///
+/// +/// A function that tests whether a given integer value $$x \in \mathbb{Z}$$ is even. +/// +///
+/// Example: +/// +/// import gleeunit/should +/// import gleam_community/maths/int as intx +/// +/// pub fn example() { +/// intx.is_even(-3) +/// |> should.equal(False) +/// +/// intx.is_even(-4) +/// |> should.equal(True) +/// } +///
+/// +///
+/// +/// Back to top ↑ +/// +///
+/// +pub fn is_even(x: Int) -> Bool { + x % 2 == 0 +} + +///
+/// +/// Spot a typo? Open an issue! +/// +///
+/// +/// A function that produces a number of type `Float` and from an `Int`. +/// +/// Note: The function is equivalent to the similar function in the Gleam stdlib. +/// +///
+/// Example: +/// +/// import gleeunit/should +/// import gleam_community/maths/int as intx +/// +/// pub fn example() { +/// intx.to_float(-1) +/// |> should.equal(-1.0) +/// +/// intx.is_even(1) +/// |> should.equal(1.0) +/// } +///
+/// +///
+/// +/// Back to top ↑ +/// +///
+/// +pub fn to_float(x: Int) -> Float { + int.to_float(x) +} diff --git a/src/gleam_community/maths/int_list.gleam b/src/gleam_community/maths/int_list.gleam index 1033f52..99e99a2 100644 --- a/src/gleam_community/maths/int_list.gleam +++ b/src/gleam_community/maths/int_list.gleam @@ -19,11 +19,8 @@ //// .katex { font-size: 1.1em; } //// //// -//// A module containing several different kinds of mathematical functions -//// applying to lists of real numbers. +//// A module containing mathematical functions applying to one or more lists of integers. //// -//// Function naming has been adopted from C mathematical function. -//// //// --- //// //// * **Distances, sums and products** diff --git a/test/gleam/gleam_community_maths_int_test.gleam b/test/gleam/gleam_community_maths_int_test.gleam index 771f4cf..26d790a 100644 --- a/test/gleam/gleam_community_maths_int_test.gleam +++ b/test/gleam/gleam_community_maths_int_test.gleam @@ -182,4 +182,98 @@ pub fn int_is_power_test() { intx.is_power(5, 2) |> should.equal(False) + + intx.is_power(27, 3) + |> should.equal(True) + + intx.is_power(28, 3) + |> should.equal(False) +} + +pub fn int_is_even_test() { + intx.is_even(0) + |> should.equal(True) + + intx.is_even(2) + |> should.equal(True) + + intx.is_even(12) + |> should.equal(True) + + intx.is_even(5) + |> should.equal(False) + + intx.is_even(-3) + |> should.equal(False) + + intx.is_even(-4) + |> should.equal(True) +} + +pub fn int_is_odd_test() { + intx.is_odd(0) + |> should.equal(False) + + intx.is_odd(3) + |> should.equal(True) + + intx.is_odd(13) + |> should.equal(True) + + intx.is_odd(4) + |> should.equal(False) + + intx.is_odd(-3) + |> should.equal(True) + + intx.is_odd(-4) + |> should.equal(False) +} + +pub fn int_gcd_test() { + intx.gcd(1, 1) + |> should.equal(1) + + intx.gcd(100, 10) + |> should.equal(10) + + intx.gcd(10, 100) + |> should.equal(10) + + intx.gcd(100, -10) + |> should.equal(10) + + intx.gcd(-36, -17) + |> should.equal(1) + + intx.gcd(-30, -42) + |> should.equal(6) +} + +pub fn int_lcm_test() { + intx.lcm(1, 1) + |> should.equal(1) + + intx.lcm(100, 10) + |> should.equal(100) + + intx.lcm(10, 100) + |> should.equal(100) + + intx.lcm(100, -10) + |> should.equal(100) + + intx.lcm(-36, -17) + |> should.equal(612) + + intx.lcm(-30, -42) + |> should.equal(210) +} + +pub fn int_to_float_test() { + intx.to_float(-1) + |> should.equal(-1.0) + + intx.to_float(1) + |> should.equal(1.0) }