or run

tessl search
Log in

Version

Files

tile.json

task.mdevals/scenario-6/

Custom Statistical Functions

Build a custom math module that extends mathjs with additional statistical functions for analyzing datasets.

Requirements

Your task is to create a module that adds custom statistical functions to a mathjs instance. The module should provide functions for calculating the geometric mean and harmonic mean of datasets.

Geometric Mean

Implement a geometricMean function that calculates the geometric mean of an array of numbers. The geometric mean is calculated as: (x₁ × x₂ × ... × xₙ)^(1/n)

  • Should accept an array or multiple arguments
  • Should return a number representing the geometric mean
  • Formula: nth root of the product of n values

Harmonic Mean

Implement a harmonicMean function that calculates the harmonic mean of an array of numbers. The harmonic mean is calculated as: n / (1/x₁ + 1/x₂ + ... + 1/xₙ)

  • Should accept an array or multiple arguments
  • Should return a number representing the harmonic mean
  • Formula: number of values divided by the sum of reciprocals

Test Cases

  • Geometric mean of [2, 8] returns 4 @test
  • Geometric mean of [1, 2, 3, 4] returns approximately 2.213 @test
  • Harmonic mean of [2, 3, 4] returns approximately 2.769 @test
  • Custom functions work in expression evaluation like evaluate("geometricMean([4, 9])") @test

Implementation

@generates

API

/**
 * Creates a mathjs instance with custom statistical functions.
 *
 * @returns {object} A mathjs instance with geometricMean and harmonicMean functions
 */
function createCustomMath() {
  // IMPLEMENTATION HERE
}

module.exports = { createCustomMath };

Dependencies { .dependencies }

mathjs { .dependency }

Provides mathematical functionality and extensibility support.