Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add arithmetic and geometric mean calculation functions #267

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions maths/arithmetic_geometric_mean.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* @function calculateArithmeticMean
* @description Calculate the arithmetic mean of a list of numbers. Arithmetic mean is the sum of all the values divided by the number of values in the list. Valid for both positive and negative numbers.
* @param {number[]} values - Array of numeric values
* @returns {number} - arithmetic mean of input values
* @throws {TypeError} - If the input is not an array
* @throws {RangeError} - If the input array is empty
* @see https://en.wikipedia.org/wiki/Arithmetic_mean
* @example calculateArithmeticMean([1, 2, 4, 5]) = 3
*/

export const calculateArithmeticMean = (values: number[]) => {
// Check if the input is an array
if (Array.isArray(values) === false) {
throw new TypeError('Invalid Input')
}

// Check if the array is empty
if (values.length === 0) {
throw new RangeError('Input array cannot be empty')
}

// Calculate the arithmetic mean
const arithmeticMean =
values.reduce((sum, current) => sum + current, 0) / values.length

return arithmeticMean
}

/**
* @function calculateGeometricMean
* @description Calculate the geometric mean of list of numbers. It is the nth root of the product of n numbers. It is only valid for positive numbers.
* @param {number[]} values - Array of numeric values
* @returns {number} - geometric mean of input values
* @throws {TypeError} - If the input is not an array
* @throws {RangeError} - If the input array is empty
* @see https://en.wikipedia.org/wiki/Geometric_mean
* @example calculateGeometricMean([1, 2, 4, 5]) = 2.514866859365871
*/

export const calculateGeometricMean = (values: number[]) => {
//check if input is an array
if (Array.isArray(values) === false) {
throw new TypeError('Invalid Input')
}

//check if the array is empty
if (values.length === 0) {
throw new RangeError('Input array cannot be empty')
}

//calculate the geometric mean
const geometricMean = Math.pow(
values.reduce((product, current) => product * current, 1),
1 / values.length
)

return geometricMean
}
36 changes: 36 additions & 0 deletions maths/test/arithmetic_geometric_mean.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import {
calculateArithmeticMean,
calculateGeometricMean
} from '../arithmetic_geometric_mean'

describe('Arithmetic Mean', () => {
test('Should return the arithmetic mean of list of numbers', () => {
expect(calculateArithmeticMean([1, 2, 3, 4])).toBe(2.5)
})

test('Should throw a TypeError if the input is not an array', () => {
expect(() => calculateArithmeticMean(1 as unknown as number[])).toThrow(
TypeError
)
})

test('Should throw a RangeError if the input array is empty', () => {
expect(() => calculateArithmeticMean([])).toThrow(RangeError)
})
})

describe('Geometric Mean', () => {
test('Should return geometric mean of list of numbers', () => {
expect(calculateGeometricMean([1, 2, 3, 4])).toBe(2.2133638394006434)
})

test('Should throw a TypeError if the input is not an array', () => {
expect(() => calculateGeometricMean(1 as unknown as number[])).toThrow(
TypeError
)
})

test('Shoud throw a RangeError if the input array is empty', () => {
expect(() => calculateGeometricMean([])).toThrow(RangeError)
})
})