Takes the given functions and makes a function which returns the linear combination of the output of original functions. It works well with functions returning numpy arrays of the same shape. Args: *functions: Base functions for the linear combination.The functions shall have t
(*functions: Callable, weights: Optional[Sequence] = None)
| 6 | |
| 7 | |
| 8 | def make_linear_combination(*functions: Callable, weights: Optional[Sequence] = None) -> Callable: |
| 9 | """ |
| 10 | Takes the given functions and makes a function which returns the linear combination of the output of original |
| 11 | functions. It works well with functions returning numpy arrays of the same shape. |
| 12 | |
| 13 | Args: |
| 14 | *functions: Base functions for the linear combination.The functions shall have the same argument and if they |
| 15 | return numpy arrays, the returned arrays shall have the same shape. |
| 16 | weights: Coefficients of the functions in the linear combination. The i-th given function will be multiplied |
| 17 | with weights[i]. |
| 18 | |
| 19 | Todo: |
| 20 | Doesn't it better to accept functions as a Collection explicitly? |
| 21 | |
| 22 | Returns: |
| 23 | A function which returns the linear combination of the given functions output. |
| 24 | """ |
| 25 | if weights is None: |
| 26 | weights = np.ones(shape=(len(functions))) |
| 27 | else: |
| 28 | assert len(functions) == len(weights), 'the length of weights must be the ' \ |
| 29 | 'same as the number of given functions' |
| 30 | |
| 31 | def linear_combination(*args, **kwargs): |
| 32 | return sum((weights[i]*functions[i](*args, **kwargs) for i in range(len(weights)))) |
| 33 | |
| 34 | return linear_combination |
| 35 | |
| 36 | |
| 37 | def make_product(*functions: Callable, exponents: Optional[Sequence] = None) -> Callable: |
no outgoing calls
no test coverage detected
searching dependent graphs…