diff --git a/maths/arithmetic_geometric_mean.ts b/maths/arithmetic_geometric_mean.ts new file mode 100644 index 00000000..cc11b625 --- /dev/null +++ b/maths/arithmetic_geometric_mean.ts @@ -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 +} diff --git a/maths/test/arithmetic_geometric_mean.test.ts b/maths/test/arithmetic_geometric_mean.test.ts new file mode 100644 index 00000000..4ad7ede6 --- /dev/null +++ b/maths/test/arithmetic_geometric_mean.test.ts @@ -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) + }) +})