Skip to content

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

engine instance-attribute

engine = engine

formula instance-attribute

formula = formula

root instance-attribute

root: Node | None = None

variables instance-attribute

variables: dict[str, Scalar] = copy() if variables else {}

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
arity instance-attribute
arity = arity
associativity instance-attribute
associativity = associativity
description instance-attribute
description = description
method instance-attribute
method = method
name instance-attribute
name = name
precedence instance-attribute
precedence = precedence
type instance-attribute
type = type if isinstance(type, Type) else Type[type]
Classes
Type

Bases: Enum

Type of function element.

Attributes
Function class-attribute instance-attribute
Function = auto()
Operator class-attribute instance-attribute
Operator = auto()
Functions
__repr__
__repr__() -> str

Return the code to construct the name of the element in Python.

Returns:

Type Description
str

code to construct the name of the element in Python.

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
constant instance-attribute
constant = constant
element instance-attribute
element = element
left instance-attribute
left = left
right instance-attribute
right = right
variable instance-attribute
variable = variable
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
evaluate(local_variables: dict[str, Scalar] | None = None) -> Scalar

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
infix(node: Node | None = None) -> str

Return the infix notation of the node.

Parameters:

Name Type Description Default
node Node | None

node in the expression tree (defaults to self if None)

None

Returns:

Type Description
str

infix notation of the node.

postfix
postfix(node: Node | None = None) -> str

Return the postfix notation of the node.

Parameters:

Name Type Description Default
node Node | None

node in the expression tree (defaults to self if None)

None

Returns:

Type Description
str

postfix notation of the node.

prefix
prefix(node: Node | None = None) -> str

Return the prefix notation of the node.

Parameters:

Name Type Description Default
node Node | None

node in the expression tree (defaults to self if None)

None

Returns:

Type Description
str

prefix notation of the node.

value
value() -> str

Return the value of the node based on its contents.

The value of the node is the first of:

  1. operation or function name if there is an element
  2. variable name if it is not empty
  3. constant value.

Returns:

Type Description
str

value of the node based on its contents.

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

formula.

required

create staticmethod

create(name: str, formula: str, engine: Engine | None = None) -> Function

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(variables: dict[str, Scalar] | None = None) -> Scalar

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

format_infix(formula: str) -> str

Format the formula expressed in infix notation.

Parameters:

Name Type Description Default
formula str

formula expressed in infix notation.

required

Returns:

Type Description
str

formatted formula expressed in infix notation.

infix_to_postfix classmethod

infix_to_postfix(formula: str) -> str

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.

is_loaded

is_loaded() -> bool

Return whether the function is loaded.

Returns:

Type Description
bool

function is loaded.

load

load() -> None

Load the function using the formula expressed in infix notation.

membership

membership(x: Scalar) -> Scalar

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 x

ValueError

when the map of variables contain names of input or output variables

parameters

parameters() -> str

Return the parameters of the term.

Returns:

Type Description
str

formula.

parse classmethod

parse(formula: str) -> Node

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.

unload

unload() -> None

Unload the function and reset the map of substitution variables.

update_reference

update_reference(engine: Engine | None) -> None

Update the reference to the engine (if any) and load the function if it is not loaded.

Parameters:

Name Type Description Default
engine Engine | None

engine to which the term belongs to.

required