PY-83

Match Loop and Array Results

  • Medium
  • Array Equivalence
  • Python

Task

Write match_loop_and_array(values, scale, offset). values is a finite, two-dimensional NumPy array of numbers. scale and offset are finite scalar numbers. At every position apply the complete rule

result = value * scale + offset

Return a dictionary with exactly these keys:

  • "loop": an independent float64 array produced by visiting every position with a clear nested loop;
  • "array": an independent float64 array produced by one whole-array expression for the same rule;
  • "shape_equal": whether the two result shapes are equal;
  • "dtype_equal": whether the two result dtypes are equal;
  • "values_equal": whether corresponding values agree under np.allclose(..., rtol=1e-9, atol=1e-12).

Convert the source values to float64 for both calculations. The two result arrays must have the same shape as values, and values must remain unchanged. An empty two-dimensional array is valid and still needs the declared result shape and dtype.

Example

For

values = np.array([[2, 10], [4, 14], [6, 18]])

with scale=0.5 and offset=1.0, both arrays are [[2., 6.], [3., 8.], [4., 10.]], and all three evidence flags are True.

Your implementation

You may import NumPy as np. Do not modify an input, print, or ask for input.