Function
Function
¶
Bases: Term
Polynomial term that represents a generic function.
Equation
\(f : x \mapsto f(x)\)
The function term is also used to convert the text of the antecedent of a rule from infix to postfix notation.
related
Attributes¶
Classes¶
Element
¶
Representation of a single element in a formula: either a function or an operator.
If the Element represents a function, its parameter is the arity of the function (only unary or binary supported)
If the Element represents an operator, its parameters are: arity
, precedence
, and associativity
.
Attributes¶
Classes¶
Functions¶
__init__
¶
__init__(
name: str,
description: str,
type: Type | str,
method: Callable[..., Scalar],
arity: int = 0,
precedence: int = 0,
associativity: int = -1,
) -> None
Constructor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
name of the element |
required |
description |
str
|
description of the element |
required |
type |
Type | str
|
type of the element |
required |
method |
Callable[..., Scalar]
|
reference to the function (only supports unary or binary) |
required |
arity |
int
|
number of operands required |
0
|
precedence |
int
|
precedence of operators, where higher precedence comes first (see Order of operations) |
0
|
associativity |
int
|
precedence of grouping operators in the absence of parentheses (see Operator associativity) |
-1
|
__repr__
¶
__repr__() -> str
Return the code to construct the element in Python.
Returns:
Type | Description |
---|---|
str
|
code to construct the element in Python. |
is_function
¶
is_function() -> bool
Return whether the element is a fuzzylite.term.Function.Element.Type.Function.
Returns:
Type | Description |
---|---|
bool
|
is_operator
¶
is_operator() -> bool
Return whether the element is a fuzzylite.term.Function.Element.Type.Operator.
Returns:
Type | Description |
---|---|
bool
|
Node
¶
Basic binary tree structure.
A node can point to left and right nodes to build a binary tree.
A node can represent:
- an element (Function or Operator),
- an input or output variable by name,
- a constant floating-point value
Attributes¶
Functions¶
__init__
¶
__init__(
element: Element | None = None,
variable: str = "",
constant: float = nan,
left: Node | None = None,
right: Node | None = None,
) -> None
Constructor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
element |
Element | None
|
node refers to a function or an operator |
None
|
variable |
str
|
node refers to a variable by name |
''
|
constant |
float
|
node refers to an arbitrary floating-point value |
nan
|
right |
Node | None
|
node has an expression tree on the right |
None
|
left |
Node | None
|
node has an expression tree on the left. |
None
|
__repr__
¶
__repr__() -> str
Return the code to construct the node in Python.
Returns:
Type | Description |
---|---|
str
|
code to construct the node in Python. |
evaluate
¶
Recursively evaluate the node and substitute the variables with the given values (if any).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
local_variables |
dict[str, Scalar] | None
|
map of substitutions of variable names for scalars |
None
|
Returns:
Type | Description |
---|---|
Scalar
|
scalar as the result of the evaluation. |
Raises:
Type | Description |
---|---|
ValueError
|
when function arity is not met with left and right nodes |
ValueError
|
when the node represents a variable but the map does not contain its substitution value |
infix
¶
postfix
¶
prefix
¶
Functions¶
__init__
¶
__init__(
name: str = "",
formula: str = "",
engine: Engine | None = None,
variables: dict[str, Scalar] | None = None,
load: bool = False,
) -> None
Constructor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
name of the term |
''
|
formula |
str
|
formula defining the membership function |
''
|
engine |
Engine | None
|
engine to which the Function can have access |
None
|
variables |
dict[str, Scalar] | None
|
map of substitution variables |
None
|
load |
bool
|
load the function on creation. |
False
|
__repr__
¶
__repr__() -> str
Return the code to construct the term in Python.
Returns:
Type | Description |
---|---|
str
|
code to construct the term in Python. |
configure
¶
configure(parameters: str) -> None
Configure the term with the parameters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
parameters |
str
|
|
required |
create
staticmethod
¶
Create and configure a function term.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
name of the term |
required |
formula |
str
|
formula defining the membership function |
required |
engine |
Engine | None
|
engine to which the Function can have access |
None
|
Returns:
Type | Description |
---|---|
Function
|
configured function term |
Raises:
Type | Description |
---|---|
SyntaxError
|
when the formula has a syntax error |
evaluate
¶
Evaluate the function value using the map of variable substitutions (if any).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
variables |
dict[str, Scalar] | None
|
map containing substitutions of variable names for values |
None
|
Returns:
Type | Description |
---|---|
Scalar
|
function value using the map of variable substitutions (if any). |
Raises:
Type | Description |
---|---|
RuntimeError
|
when the function is not loaded |
format_infix
classmethod
¶
infix_to_postfix
classmethod
¶
Convert the formula to postfix notation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
formula |
str
|
right-hand side of an equation expressed in infix notation |
required |
Returns:
Type | Description |
---|---|
str
|
formula in postfix notation |
Raises:
Type | Description |
---|---|
SyntaxError
|
when the formula has syntax errors. |
membership
¶
Computes the membership function evaluated at \(x\).
The value of variable x
will be added to the variables
map, and so will the current values of the input
variables and output variables if the engine has been set.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
Scalar
|
scalar |
required |
Returns:
Type | Description |
---|---|
Scalar
|
\(f(x)\) |
Raises:
Type | Description |
---|---|
ValueError
|
when an input or output variable is named |
ValueError
|
when the map of variables contain names of input or output variables |
parse
classmethod
¶
Create a node as a binary expression tree of the formula.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
formula |
str
|
right-hand side of an equation expressed in infix notation |
required |
Returns:
Type | Description |
---|---|
Node
|
node as a binary expression tree of the formula |
Raises:
Type | Description |
---|---|
SyntaxError
|
when the formula has syntax errors. |