@@ -3419,109 +3419,6 @@ def __validate_extrapolation(self, extrapolation):
34193419 return extrapolation
34203420
34213421
3422- class PiecewiseFunction (Function ):
3423- """Class for creating piecewise functions. These kind of functions are
3424- defined by a dictionary of functions, where the keys are tuples that
3425- represent the domain of the function. The domains must be disjoint.
3426- """
3427-
3428- def __new__ (
3429- cls ,
3430- source ,
3431- inputs = None ,
3432- outputs = None ,
3433- interpolation = "spline" ,
3434- extrapolation = None ,
3435- datapoints = 100 ,
3436- ):
3437- """
3438- Creates a piecewise function from a dictionary of functions. The keys of
3439- the dictionary must be tuples that represent the domain of the function.
3440- The domains must be disjoint. The piecewise function will be evaluated
3441- at datapoints points to create Function object.
3442-
3443- Parameters
3444- ----------
3445- source: dictionary
3446- A dictionary of Function objects, where the keys are the domains.
3447- inputs : list of strings
3448- A list of strings that represent the inputs of the function.
3449- outputs: list of strings
3450- A list of strings that represent the outputs of the function.
3451- interpolation: str
3452- The type of interpolation to use. The default value is 'spline'.
3453- extrapolation: str
3454- The type of extrapolation to use. The default value is None.
3455- datapoints: int
3456- The number of points in which the piecewise function will be
3457- evaluated to create a base function. The default value is 100.
3458- """
3459- if inputs is None :
3460- inputs = ["Scalar" ]
3461- if outputs is None :
3462- outputs = ["Scalar" ]
3463- # Check if source is a dictionary
3464- if not isinstance (source , dict ):
3465- raise TypeError ("source must be a dictionary" )
3466- # Check if all keys are tuples
3467- for key in source .keys ():
3468- if not isinstance (key , tuple ):
3469- raise TypeError ("keys of source must be tuples" )
3470- # Check if all domains are disjoint
3471- for key1 in source .keys ():
3472- for key2 in source .keys ():
3473- if key1 != key2 :
3474- if key1 [0 ] < key2 [1 ] and key1 [1 ] > key2 [0 ]:
3475- raise ValueError ("domains must be disjoint" )
3476-
3477- # Crate Function
3478- def calc_output (func , inputs ):
3479- """Receives a list of inputs value and a function, populates another
3480- list with the results corresponding to the same results.
3481-
3482- Parameters
3483- ----------
3484- func : Function
3485- The Function object to be
3486- inputs : list, tuple, np.array
3487- The array of points to applied the func to.
3488-
3489- Examples
3490- --------
3491- >>> inputs = [0, 1, 2, 3, 4, 5]
3492- >>> def func(x):
3493- ... return x*10
3494- >>> calc_output(func, inputs)
3495- [0, 10, 20, 30, 40, 50]
3496-
3497- Notes
3498- -----
3499- In the future, consider using the built-in map function from python.
3500- """
3501- output = np .zeros (len (inputs ))
3502- for j , value in enumerate (inputs ):
3503- output [j ] = func .get_value_opt (value )
3504- return output
3505-
3506- input_data = []
3507- output_data = []
3508- for key in sorted (source .keys ()):
3509- i = np .linspace (key [0 ], key [1 ], datapoints )
3510- i = i [~ np .isin (i , input_data )]
3511- input_data = np .concatenate ((input_data , i ))
3512-
3513- f = Function (source [key ])
3514- output_data = np .concatenate ((output_data , calc_output (f , i )))
3515-
3516- return Function (
3517- np .concatenate (([input_data ], [output_data ])).T ,
3518- inputs = inputs ,
3519- outputs = outputs ,
3520- interpolation = interpolation ,
3521- extrapolation = extrapolation ,
3522- )
3523-
3524-
35253422def funcify_method (* args , ** kwargs ): # pylint: disable=too-many-statements
35263423 """Decorator factory to wrap methods as Function objects and save them as
35273424 cached properties.
0 commit comments