codac 1.5.6
Loading...
Searching...
No Matches
codac2::FunctionBase< E > Class Template Reference

A base class for functions (either analytic functions, or set functions). More...

#include <codac2_FunctionBase.h>

Collaboration diagram for codac2::FunctionBase< E >:

Public Member Functions

 FunctionBase (const std::vector< std::reference_wrapper< VarBase > > &args, const std::shared_ptr< E > &y)
 Constructs a FunctionBase with a list of arguments and a shared pointer to an output expression.
 
 FunctionBase (const FunctionArgsList &args, const std::shared_ptr< E > &y)
 Constructs a FunctionBase with a list of arguments and a shared pointer to an output expression.
 
 FunctionBase (const FunctionBase< E > &f)
 Copy constructor.
 
virtual ~FunctionBase ()
 Virtual destructor.
 
const FunctionArgsListargs () const
 Returns the arguments of the function.
 
const std::shared_ptr< E > & expr () const
 Returns the expression associated with the function.
 
template<typename... X>
std::shared_ptr< E > operator() (const X &... x) const
 Applies this function to the arguments and returns the resulting expression.
 
std::shared_ptr< E > operator() (const std::vector< std::shared_ptr< ExprBase > > &x) const
 Applies this function to the arguments and returns the resulting expression.
 
Index input_size () const
 Calculates the total size of the function arguments, as the sum of the sizes of each argument.
 

Protected Member Functions

template<typename X>
std::shared_ptr< X > __get_copy (std::shared_ptr< X > x) const
 Helper function to get a copy of a an expression.
 
template<typename X>
std::shared_ptr< ExprBase__get_copy (const AnalyticExprWrapper< X > &x) const
 Helper function to get a copy of an AnalyticExprWrapper.
 
template<typename A>
auto __get_copy (const A &x) const
 Helper function to get a copy of various expression types.
 

Protected Attributes

const std::shared_ptr< E > _y
 expression that defines the function
 
const FunctionArgsList _args
 arguments of the function
 

Detailed Description

template<typename E>
class codac2::FunctionBase< E >

A base class for functions (either analytic functions, or set functions).

The FunctionBase class represents a generic function that operates on expressions. It allows defining functions that take a list of arguments and apply a specific expression to those arguments.

This class serves as a base for creating specific types of functions by providing the mechanism to handle arguments and perform the function application.

Note that a function may be invoked in another expression, by using the operator() which allows compositions of functions. For such call, the arguments of this operator are either other function variables or any expressions.

Template Parameters
EThe type of the output expression of this function.

Constructor & Destructor Documentation

◆ FunctionBase() [1/3]

template<typename E>
codac2::FunctionBase< E >::FunctionBase ( const std::vector< std::reference_wrapper< VarBase > > & args,
const std::shared_ptr< E > & y )
inline

Constructs a FunctionBase with a list of arguments and a shared pointer to an output expression.

The arguments are stored as a FunctionArgsList, and the expression is deep-copied.

Parameters
argsA reference to a vector of VarBase references representing the function's arguments.
yA shared pointer to the expression that defines the function.
54 { }
A base class for functions (either analytic functions, or set functions).
Definition codac2_FunctionBase.h:41
const FunctionArgsList & args() const
Returns the arguments of the function.
Definition codac2_FunctionBase.h:93
FunctionBase(const std::vector< std::reference_wrapper< VarBase > > &args, const std::shared_ptr< E > &y)
Constructs a FunctionBase with a list of arguments and a shared pointer to an output expression.
Definition codac2_FunctionBase.h:52

◆ FunctionBase() [2/3]

template<typename E>
codac2::FunctionBase< E >::FunctionBase ( const FunctionArgsList & args,
const std::shared_ptr< E > & y )
inline

Constructs a FunctionBase with a list of arguments and a shared pointer to an output expression.

The expression is deep-copied.

Parameters
argsA FunctionArgsList containing the function's arguments.
yA shared pointer to the function's expression.
66 { }
const FunctionArgsList _args
arguments of the function
Definition codac2_FunctionBase.h:208
const std::shared_ptr< E > _y
expression that defines the function
Definition codac2_FunctionBase.h:207

◆ FunctionBase() [3/3]

template<typename E>
codac2::FunctionBase< E >::FunctionBase ( const FunctionBase< E > & f)
inline

Copy constructor.

The expression is deep-copied.

Parameters
fThe FunctionBase instance to copy.
76 : _y(std::dynamic_pointer_cast<E>(f.expr()->copy())), _args(f.args()) // todo: keep this dynamic_pointer_cast?
77 { }
const std::shared_ptr< E > & expr() const
Returns the expression associated with the function.
Definition codac2_FunctionBase.h:103

◆ ~FunctionBase()

template<typename E>
virtual codac2::FunctionBase< E >::~FunctionBase ( )
inlinevirtual

Virtual destructor.

The destructor is virtual to ensure proper cleanup of derived classes when an object of a derived class is deleted through a pointer to FunctionBase.

86 { }

Member Function Documentation

◆ args()

template<typename E>
const FunctionArgsList & codac2::FunctionBase< E >::args ( ) const
inline

Returns the arguments of the function.

Returns
A constant reference to the FunctionArgsList containing the function's arguments.
94 {
95 return _args;
96 }

◆ expr()

template<typename E>
const std::shared_ptr< E > & codac2::FunctionBase< E >::expr ( ) const
inline

Returns the expression associated with the function.

Returns
A constant reference to the shared pointer to the function's expression.
104 {
105 return _y;
106 }

◆ operator()() [1/2]

template<typename E>
template<typename... X>
std::shared_ptr< E > codac2::FunctionBase< E >::operator() ( const X &... x) const
inline

Applies this function to the arguments and returns the resulting expression.

The method deep-copies the expressions provided as arguments and replaces the arguments in the expression with the provided ones.

Parameters
xThe arguments to apply to the function, for instance expressions or variables.
Returns
A shared pointer to the resulting expression after the arguments have been replaced.
119 {
120 auto expr_copy = expr()->copy();
121 size_t i = 0;
122 (expr_copy->replace_arg(_args[i++]->unique_id(), this->__get_copy(x)), ...);
123 assert_release(i == this->args().size() &&
124 "Invalid arguments: wrong number of input arguments");
126 }
std::shared_ptr< X > __get_copy(std::shared_ptr< X > x) const
Helper function to get a copy of a an expression.
Definition codac2_FunctionBase.h:170

◆ operator()() [2/2]

template<typename E>
std::shared_ptr< E > codac2::FunctionBase< E >::operator() ( const std::vector< std::shared_ptr< ExprBase > > & x) const
inline

Applies this function to the arguments and returns the resulting expression.

The method deep-copies the expressions provided as arguments and replaces the arguments in the expression with the provided ones.

This method is mainly used for Python binding.

Parameters
xA vector of shared pointers to arguments to apply to the function, for instance expressions or variables.
Returns
A shared pointer to the resulting expression after the arguments have been replaced.
140 {
141 assert_release(x.size() == this->args().size() &&
142 "Invalid arguments: wrong number of input arguments");
143 auto expr_copy = expr()->copy();
144 for(size_t i = 0 ; i < x.size() ; i++)
145 expr_copy->replace_arg(_args[i]->unique_id(), x[i]->copy());
147 }

◆ input_size()

template<typename E>
Index codac2::FunctionBase< E >::input_size ( ) const
inline

Calculates the total size of the function arguments, as the sum of the sizes of each argument.

Returns
The sum of the sizes of all arguments.
156 {
157 return this->_args.total_size();
158 }

◆ __get_copy() [1/3]

template<typename E>
template<typename X>
std::shared_ptr< X > codac2::FunctionBase< E >::__get_copy ( std::shared_ptr< X > x) const
inlineprotected

Helper function to get a copy of a an expression.

Template Parameters
XThe type of the expression to copy.
Parameters
xA shared pointer to the expression to copy.
Returns
A shared pointer to the copied expression.
171 {
172 return std::dynamic_pointer_cast<X>(x->copy());
173 }

◆ __get_copy() [2/3]

template<typename E>
template<typename X>
std::shared_ptr< ExprBase > codac2::FunctionBase< E >::__get_copy ( const AnalyticExprWrapper< X > & x) const
inlineprotected

Helper function to get a copy of an AnalyticExprWrapper.

Template Parameters
XThe type of the expression to copy.
Parameters
xThe AnalyticExprWrapper to copy.
Returns
A shared pointer to the copied expression.
184 {
185 return x->copy();
186 }

◆ __get_copy() [3/3]

template<typename E>
template<typename A>
auto codac2::FunctionBase< E >::__get_copy ( const A & x) const
inlineprotected

Helper function to get a copy of various expression types.

Template Parameters
AThe type of the expression.
Parameters
xThe expression to copy.
Returns
A shared pointer to the copied expression.
197 {
198 if constexpr(std::is_base_of_v<VarBase,A>)
199 return std::dynamic_pointer_cast<A>(x.copy());
200 else
201 {
202 // todo: make this generic (analytic / set):
203 return const_value(x);
204 }
205 }

The documentation for this class was generated from the following file: