1

I'm trying to calculate the vector basis for any transformation in R^n -> R^m.
In order to achieve this I wrote lamba functions to represent the actual function. Some samples are:

R2 -> R2: g1 = lambda x,y: np.array([2*x+y, x+2*y])
R3 -> R1: g3 = lambda x,y,z: np.array([x, -2*y, 3*z])

To have a function doing my work I came up with this:

def calculate_Vector_Basis(f, numberOfArgs):
    """
    Calculates the result for every base vector ex, ey, ...
    Params:
        f : function with generic number of arguments
        numberOfArgs: The number of arguments for the generic function
    Returns:
        [] : Array of base vectors
    """

    # Collection of base vectors
    vector_basis = []
    for i in range(numberOfArgs):
        # Create unit vector e with zeros only
        base = np.zeros(numberOfArgs)
        # Set 1 where at the axis required (for R3: x = [1 0 0], y = [0 1 0], z = [0 0 1])
        base[i] = 1
        # Call function f for every unit vector e
        vector_basis.append(f(base[0], base[1]))
    return vector_basis

The function should iterate and create unit vectors according to the dimension of the given n-dimensonal room of the rational numbers.
Samples:

R2 = [1 0], [0 1]
R3 = [1 0 0], [0 1 0], [0 0 1]

I'm stuck where I want to call the passed in lambda function f.
Based on f's definition I need 1 up to 5 params.
For R2 I'd have to call f(base[0], base[1]) in R3 I'd have to call f(base[0], base[1], base[2]). base is an ndarray that looks like this [xval, yval, ...]. Is there a possibility to call f with every value in the ndarray?

1 Answer 1

2

Yes, can do that with argument unpacking:

def calculate_Vector_Basis(f, numberOfArgs):
    """
    Calculates the result for every base vector ex, ey, ...
    Params:
        f : function with generic number of arguments
        numberOfArgs: The number of arguments for the generic function
    Returns:
        [] : Array of base vectors
    """

    # Collection of base vectors
    vector_basis = []
    for i in range(numberOfArgs):
        # Create unit vector e with zeros only
        base = np.zeros(numberOfArgs)
        # Set 1 where at the axis required (for R3: x = [1 0 0], y = [0 1 0], z = [0 0 1])
        base[i] = 1
        # Call function f for every unit vector e
        vector_basis.append(f(*base))
    return vector_basis
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.