v9.1.1
Menu

Mathematical Functions

easyFREEArray MathematicsNumPy MathematicsNumpy

Problem Statement

Apply mathematical functions to NumPy arrays.

Background

NumPy provides universal functions (ufuncs):

  • np.sqrt(): Square root
  • np.exp(): Exponential (e^x)
  • np.log(): Natural logarithm
  • np.abs(): Absolute value

Your Task

Write a function math_functions(arr) that returns a dictionary with:

  • "sqrt": Square root of each element (rounded to 2 decimals)
  • "square": Square of each element
  • "abs": Absolute value of each element

Output Format

Return a dictionary with exactly these three keys. All values as lists.

Example

Input
[1, 4, 9, 16]
Output
{'sqrt': [1.0, 2.0, 3.0, 4.0], 'square': [1, 16, 81, 256], 'abs': [1, 4, 9, 16]}
Reasoning
  • Input: arr is a 1-D array of four non-negative integers, [1,4,9,16][1, 4, 9, 16]. Each of the three results is computed element by element, so every output list also has 4 entries in the same order.
  • sqrt: each value is a perfect square, so 1=1\sqrt{1}=1, 4=2\sqrt{4}=2, 9=3\sqrt{9}=3, 16=4\sqrt{16}=4. A square root comes back as a real number, which is why the list reads [1.0, 2.0, 3.0, 4.0] with decimals. Rounding to 2 places only matters for non-perfect squares, e.g. 2≈1.4142→1.41\sqrt{2} \approx 1.4142 \to 1.41.
  • square: 12=11^2=1, 42=164^2=16, 92=819^2=81, 162=25616^2=256, giving [1, 16, 81, 256]. Squaring integers keeps them integers, so no decimal point appears.
  • abs: ∣x∣=x|x| = x for x≥0x \ge 0, so the list is just the input again: [1, 4, 9, 16].
  • Notice the result is a dictionary of plain lists keyed 'sqrt', 'square', 'abs', and that only the square-root entry changes number type.

Constraints

  • Input will contain non-negative numbers
  • Round sqrt results to 2 decimal places

Learn this in

More from Numpy

solution.py

Test results

Sign in to run your code against the test cases.