Skip to content

Discrete

Discrete

Bases: Term

Basic term that represents a discrete membership function.

Equation

\(\mu(x) = h\dfrac{(y_\max - y_\min)}{(x_\max - x_\min)} (x - x_\min) + y_\min\)

where

  • \(h\): height of the Term
  • \(x_{\min}, x_{\max}\): lower and upper bounds of \(x\), respectively
  • \(y_{\min}, y_{\max}\): membership function values \(\mu(x_{\min})\) and \(\mu(x_{\max})\), respectively
related
Warning

The pairs of values in any Discrete term must be sorted in ascending order by the \(x\) coordinate because the membership function is computed using binary search to find the lower and upper bounds of \(x\).

Attributes

Floatable class-attribute instance-attribute

Floatable = Union[SupportsFloat, str]

values instance-attribute

values = values

Functions

__init__

__init__(name: str = '', values: ScalarArray | Sequence[Floatable] | None = None, height: float = 1.0) -> None

Constructor.

Parameters:

Name Type Description Default
name str

name of the term

''
values ScalarArray | Sequence[Floatable] | None

2D array of \((x,y)\) pairs

None
height float

height of the term.

1.0

configure

configure(parameters: str) -> None

Configure the term with the parameters.

Parameters:

Name Type Description Default
parameters str

x1 y1 ... xn yn [height].

required

create staticmethod

create(
    name: str,
    xy: str | Sequence[Floatable] | tuple[Sequence[Floatable], Sequence[Floatable]] | dict[Floatable, Floatable],
    height: float = 1.0,
) -> Discrete

Create a discrete term from the parameters.

Parameters:

Name Type Description Default
name str

name of the term

required
xy str | Sequence[Floatable] | tuple[Sequence[Floatable], Sequence[Floatable]] | dict[Floatable, Floatable]

coordinates

required
height float

height of the term

1.0

Returns:

Type Description
Discrete

Discrete term.

membership

membership(x: Scalar) -> Scalar

Compute the membership function value of \(x\).

The function uses binary search to find the lower and upper bounds of \(x\) and then linearly interpolates the membership function value between the bounds.

Warning

The pairs of values in any Discrete term must be sorted in ascending order by the \(x\) coordinate because the membership function is computed using binary search to find the lower and upper bounds of \(x\).

Parameters:

Name Type Description Default
x Scalar

scalar

required

Returns:

Type Description
Scalar

\(\mu(x) = h\dfrac{(y_\max - y_\min)}{(x_\max - x_\min)} (x - x_\min) + y_\min\)

parameters

parameters() -> str

Return the space-separated parameters of the term.

Returns:

Type Description
str

x1 y1 ... xn yn [height].

sort

sort() -> None

Sort in ascending order the pairs of values by the \(x\)-coordinate.

to_dict

to_dict() -> dict[float, float]

Return a dictionary of values in the form {x: y}.

Returns:

Type Description
dict[float, float]

dictionary of values in the form {x: y}.

to_list

to_list() -> list[float]

Return a list of values in the form [x1,y1, ..., xn, yn].

Returns:

Type Description
list[float]

list of values in the form [x1,y1, ..., xn, yn].

to_xy staticmethod

to_xy(x: Any, y: Any) -> ScalarArray

Create list of values from the parameters.

Parameters:

Name Type Description Default
x Any

\(x\)-coordinate(s) that can be converted into scalar(s)

required
y Any

\(y\)-coordinate(s) that can be converted into scalar(s)

required

Returns:

Type Description
ScalarArray

array of \(n\)-rows and \(2\)-columns \((n,2)\).

Raises:

Type Description
ValueError

when the shapes of \(x\) and \(y\) are different.

x

x() -> ScalarArray

Return \(x\) coordinates.

Returns:

Type Description
ScalarArray

\(x\) coordinates.

y

y() -> ScalarArray

Return \(y\) coordinates.

Returns:

Type Description
ScalarArray

\(y\) coordinates.