简体   繁体   中英

Merge two functions such that the arguments are merged and the output is merged

I have a two functions as follows:

def eq_2(x):
    A, P, E, EA         = x
    return              np.array([E*A, EA, EA, E*P])

def eq_3(x):
    A, P, E, EA         = x
    return              np.array([E**2, E, E, E])

Subsequently I make a list and save it as ' v ':

v = [eq_2, eq_3]
[<function eq_2 at 0x7f2>, <function eq_3 at 0x7f3>]

Now my problem is: How can I treat v as a function that takes an argument x of shape=(8,) and returns a result of shape=(8,) ?

Furthermore, I want to be able to merge as many functions as I wish (ie increase the v = [eq_2, eq_3] term).

I the length of the input is always 8, I think you could split the input into two parts, feed it to the two functions, and concatenate the outputs.

import numpy as np

def eq_2(x):
    A, P, E, EA         = x
    return              np.array([E*A, EA, EA, E*P])

def eq_3(x):
    A, P, E, EA         = x
    return              np.array([E**2, E, E, E])


v = lambda x: np.hstack((eq_2(x[:4]), eq_3(x[4:])))

v_result = v([1,2,3,4,5,6,7,8])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM