Since October 7, 2023, Israel has murdered 57,200 Palestinians, out of whom, 18,188 were children. Please consider donating to Palestine Children's Relief Fund.
NumPy IconNumPy
0
0
Beginner

Linear Transformation

The concept of linear transformations is one of the most fundamental concepts of linear algebra. It involves mapping our initial matrix using some rule (i.e. the transformation matrix) to obtain a resulting matrix.

This problem involves the following simple scalar transformation function :

Fun fact: The function defined above is the one used to formulate the Collatz conjecture.

Exercise

Write a program using NumPy that takes in a multi-dimensional array A, and returns another array B (do not modify A) of the same dimensions, where every element in B is obtained by applying function to the corresponding element in A.

For example, if the initial array A is:

then the transformed array B will be:

Sample Test Cases

Test Case 1

Input:

[
  [[ 8,  3],
   [17, 12]]
]

Output:

[[ 4, 10],
 [52, 6]]

Test Case 2

Input:

[
  [[12, 45, 0],
   [11,  6, 5],
   [23, 88, 8]]
]

Output:

[[  6, 136,   0],
 [ 34,   3,  16],
 [ 70,  44,   4]]
Login to Start Coding
import numpy as np
from numpy.typing import NDArray


class LinearTransformation:
def transform_array(
self,
A: NDArray[np.int64],
) -> NDArray[np.int64]:
# rewrite this function
# dummy code to get you started
A_transformed = np.array([])

return A_transformed