core.Fuzznum

High-level Fuzznum wrapper and factory utilities.

This module provides the Fuzznum facade, which binds a concrete per-element strategy (FuzznumStrategy) implementation to a lightweight user-facing object. It also provides the convenient factory function fuzznum().

Overview

  • Fuzznum is the main user-facing class for single fuzzy numbers.

  • It delegates all logic and data to a registered FuzznumStrategy subclass, determined by mtype.

  • All mathematical operations, validation, and formatting are handled by the strategy.

  • The facade exposes strategy attributes and methods as if they were native to Fuzznum.

Notes

  • The actual fuzzy number logic is implemented in strategy classes (see axisfuzzy/core/base.py).

  • The registry system allows new fuzzy number types to be plugged in without modifying this module.

  • Operator overloading is supported via the dispatcher system.

Examples

from axisfuzzy.core.fuzznums import fuzznum

# Create a default fuzzy number (mtype and q from config)
a = fuzznum()
b = fuzznum((0.7,0.2))

# Access attributes
print(a.mtype, a.q)

# Set fuzzy number attributes
a.md = 0.5
a.nmd = 0.3

# Use operator overloading (requires operation registration)
c = a + b

# Serialize and deserialize
d = fuzznum.from_dict(a.to_dict())
class axisfuzzy.core.fuzznums.Fuzznum(mtype=None, q=None)[source]

Bases: object

Facade object representing a single fuzzy number.

Fuzznum binds a concrete strategy implementation (subclass of axisfuzzy.core.base.FuzznumStrategy) at construction time and exposes the strategy’s attributes and methods on the Fuzznum instance. This design keeps per-element logic in strategy classes while providing a small, ergonomic high-level API.

Parameters:
  • mtype (str, optional) – Name of the fuzzy-number strategy (membership type). When omitted, the default is read from configuration.

  • q (int, optional) – q-rung parameter used by some strategies. When omitted, the default is read from configuration.

mtype

Configured membership type name.

Type:

str

q

Effective q-rung for this instance.

Type:

int

Notes

  • The Fuzznum instance dynamically binds strategy methods and attributes into itself at initialization. Attempting to access strategy-backed attributes before initialization raises an AttributeError.

  • Internal attributes are prefixed with an underscore and are not forwarded to the strategy.

Examples

from axisfuzzy.core.fuzznums import fuzznum, Fuzznum

# Create a QROFN Fuzzy number through a class method
a = Fuzznum('qrofn').create(md=0.5, nmd=0.2)
print(a.md, a.nmd)

# Create a QROFN fuzzy number
a = fuzznum((0.6,0.3))
print(a.md, a.nmd)

# Copy and modify
b = a.copy()
b.md = 0.8

# Serialize/deserialize
d = fuzznum.from_dict(a.to_dict())
property T

View of the fuzzy array with axes transposed.

This property provides convenient access to the transpose operation, following NumPy’s .T property convention.

Returns:

Transposed view of the array. For arrays, returns a view with axes reversed. For Fuzznum, returns a copy (no dimensions to transpose).

Return type:

Fuzzarray or Fuzznum

Examples

# Transpose via property access
arr = fuzzarray([[a, b], [c, d]])  # shape (2, 2)
transposed = arr.T                 # shape (2, 2)

# For 1D arrays, T returns a copy
arr1d = fuzzarray([a, b, c])       # shape (3,)
transposed1d = arr1d.T             # shape (3,)
property acc

Dispatched property for acc

append(item, axis=None, inplace=False)

Append elements to the fuzzy object.

This method provides NumPy-like append functionality with additional support for in-place modification and fuzzy-specific data types.

Parameters:
  • item (Fuzznum, Fuzzarray, or list) – Elements to append. Can be a single Fuzznum, another Fuzzarray, or a list of Fuzznum/Fuzzarray objects.

  • axis (int or None, optional) – Axis along which to append. If None (default), both arrays are flattened before concatenation. Default is None.

  • inplace (bool, optional) – If True and the object is a Fuzzarray, modify it in place and return None. If False, return a new object. Default is False.

Returns:

If inplace=False, returns a new Fuzzarray with appended elements. If inplace=True, modifies the original object and returns None.

Return type:

Fuzzarray or None

Raises:
  • TypeError – If item contains unsupported types or if inplace=True for Fuzznum.

  • ValueError – If shapes are incompatible for concatenation.

Examples

# Append single element
arr = fuzzarray([a, b])
appended = arr.append(c)  # Fuzzarray([a, b, c])

# Append array
arr2 = fuzzarray([d, e])
appended = arr.append(arr2)  # Fuzzarray([a, b, d, e])

# In-place append
arr.append(f, inplace=True)  # modifies arr directly

# Append with axis specification
arr2d = fuzzarray([[a, b]])  # shape (1, 2)
item2d = fuzzarray([[c, d]]) # shape (1, 2)
appended = arr2d.append(item2d, axis=0)  # shape (2, 2)
broadcast_to(*shape)

Broadcast the fuzzy object to a new shape.

This method provides NumPy-like broadcast_to functionality, creating a view of the input array broadcasted to the specified shape according to NumPy’s broadcasting rules.

Parameters:

*shape (int) – Target shape for broadcasting. The shape must be compatible with the input array’s shape according to broadcasting rules.

Returns:

View of the input array broadcasted to the target shape.

Return type:

Fuzzarray

Raises:

ValueError – If the input shape cannot be broadcasted to the target shape.

Notes

Broadcasting rules follow NumPy conventions: shapes are compatible when, for each dimension, the sizes are equal, one of them is 1, or one of them does not exist.

Examples

# Broadcast a scalar to an array
scalar = fuzznum((0.6, 0.3))
broadcasted = scalar.broadcast_to(3, 4)  # shape (3, 4)

# Broadcast a 1D array to 2D
arr1d = fuzzarray([a, b, c])       # shape (3,)
broadcasted2d = arr1d.broadcast_to(2, 3)  # shape (2, 3)

# Broadcast with compatible dimensions
arr = fuzzarray([[a], [b]])        # shape (2, 1)
broadcasted = arr.broadcast_to(2, 4)     # shape (2, 4)
copy()[source]

Produce a shallow copy of this Fuzznum preserving current strategy attributes.

Returns:

New Fuzznum instance configured with the same mtype/q and strategy attribute values as this instance.

Return type:

Fuzznum

Raises:

RuntimeError – If called on an uninitialized Fuzznum.

create(**kwargs)[source]

Create a new Fuzznum instance of the same mtype/q and set initial attributes.

Parameters:

**kwargs – Attribute names and values to assign on the created Fuzznum. Only attributes declared by the underlying strategy will be set.

Returns:

A newly constructed Fuzznum instance with provided attributes applied.

Return type:

Fuzznum

Raises:

AttributeError – If an attribute provided in kwargs is not accepted by the strategy.

distance(*args, **kwargs)

Dispatched method for distance

equivalent(other)[source]

Calculate the equivalence level between two fuzzy numbers

Corresponding to the “if and only if” operation in classical logic, it represents the degree to which two fuzzy propositions are equivalent to each other.

flatten()

Return a copy of the array collapsed into one dimension.

This method provides NumPy-like flatten functionality, converting a multi-dimensional fuzzy array into a 1D array containing the same elements.

Returns:

A 1D copy of the input array. For Fuzznum input, returns a Fuzzarray with shape (1,) containing the input value.

Return type:

Fuzzarray

Examples

# Flatten a 2D array
arr = fuzzarray([[a, b], [c, d]])  # shape (2, 2)
flat = arr.flatten()               # shape (4,)

# Flatten a Fuzznum (creates single-element array)
scalar = fuzznum((0.6, 0.3))
flat_scalar = scalar.flatten()     # shape (1,)
classmethod from_dict(data)[source]

Construct a Fuzznum from a dictionary produced by to_dict().

Parameters:

data (dict) – Dictionary containing at minimum the ‘mtype’ key and, optionally, an ‘attributes’ mapping.

Returns:

Reconstructed Fuzznum instance.

Return type:

Fuzznum

Raises:

ValueError – If ‘mtype’ is missing from the input dictionary.

get_info()[source]

Get basic information about the Fuzznum instance.

Returns:

Dictionary containing basic information about the Fuzznum instance.

Return type:

dict

get_strategy_attributes_dict()[source]

Get a dictionary of strategy attributes and their current values.

Returns:

Dictionary mapping attribute names to their current values.

Return type:

dict

get_strategy_instance()[source]

Return the bound FuzznumStrategy instance.

Returns:

The strategy object that implements the per-element behavior.

Return type:

FuzznumStrategy

Raises:

RuntimeError – If the strategy instance is not available (e.g., partially initialized).

property ind

Dispatched property for ind

item(*args)

Return the scalar item of the fuzzy object.

This method provides NumPy-like item functionality, extracting a single Fuzznum from a Fuzzarray or returning a copy of a Fuzznum.

Parameters:

*args (int, optional) – Index arguments for multi-dimensional arrays. If not provided, the array must contain exactly one element.

Returns:

The scalar element at the specified location, or a copy of the input Fuzznum.

Return type:

Fuzznum

Raises:

ValueError – If the array has more than one element and no index is provided.

Examples

# Extract item from single-element array
arr = fuzzarray([fuzznum((0.6, 0.3))])  # shape (1,)
item = arr.item()                       # Fuzznum

# Extract item with index
arr2d = fuzzarray([[a, b], [c, d]])     # shape (2, 2)
item = arr2d.item(1, 0)                 # Fuzznum at position [1, 0]

# Item of Fuzznum returns copy
scalar = fuzznum((0.6, 0.3))
item = scalar.item()                    # copy of scalar
max(*args, **kwargs)

Dispatched method for max

mean(*args, **kwargs)

Dispatched method for mean

min(*args, **kwargs)

Dispatched method for min

property ndim: int
pop(index=-1, inplace=False)

Remove and return an element from a one-dimensional array.

This method provides list-like pop functionality for fuzzy arrays, removing and returning an element at a specified index.

Parameters:
  • index (int, optional) – Index of the element to remove and return. Default is -1 (last element).

  • inplace (bool, optional) – If True and the object is a Fuzzarray, modify it in place and return only the popped element. If False, return both the popped element and the modified array. Default is False.

Returns:

If inplace=True, returns the popped Fuzznum. If inplace=False, returns a tuple of (popped_element, remaining_array).

Return type:

Fuzznum or tuple of (Fuzznum, Fuzzarray)

Raises:
  • TypeError – If called on a Fuzznum (scalar objects don’t support pop).

  • ValueError – If called on a multi-dimensional array (only 1D arrays supported).

  • IndexError – If the array is empty or the index is out of bounds.

Examples

# Pop last element (out-of-place)
arr = fuzzarray([a, b, c])
popped, remaining = arr.pop()  # popped=c, remaining=[a, b]

# Pop specific index
popped, remaining = arr.pop(0)  # popped=a, remaining=[b, c]

# In-place pop
arr = fuzzarray([a, b, c])
popped = arr.pop(inplace=True)  # arr is now [a, b], returns c

# Pop from empty array raises error
empty = fuzzarray([])
# empty.pop()  # raises IndexError
prod(*args, **kwargs)

Dispatched method for prod

ravel()

Return a contiguous flattened array.

This method is similar to flatten but may return a view when possible, providing NumPy-like ravel semantics for fuzzy containers.

Returns:

A 1D array containing the same elements as the input. For Fuzznum input, returns a Fuzzarray with shape (1,).

Return type:

Fuzzarray

Notes

In the current implementation, this method always returns a copy (same as flatten). Future versions may optimize to return views when the memory layout allows.

Examples

# Ravel a multi-dimensional array
arr = fuzzarray([[a, b], [c, d]])  # shape (2, 2)
raveled = arr.ravel()              # shape (4,)

# Ravel a Fuzznum
scalar = fuzznum((0.6, 0.3))
raveled_scalar = scalar.ravel()    # shape (1,)
reshape(*shape)

Give a new shape to a fuzzy array without changing its data.

This method provides NumPy-like reshape functionality for fuzzy containers. It works by reshaping the underlying component arrays in the SoA backend while preserving the fuzzy number data.

Parameters:

*shape (int) – New shape for the array. One dimension can be -1, in which case its value is inferred from the array size and remaining dimensions.

Returns:

A new Fuzzarray with the specified shape. For Fuzznum input, returns a Fuzzarray with the requested shape filled with the input value.

Return type:

Fuzzarray

Raises:

ValueError – If the new shape is incompatible with the array size.

Examples

# Reshape a 1D array to 2D
arr = fuzzarray([a, a, a, a])  # shape (4,)
reshaped = arr.reshape(2, 2)   # shape (2, 2)

# Reshape with automatic dimension inference
reshaped2 = arr.reshape(2, -1)  # shape (2, 2)

# Broadcast a Fuzznum to an array shape
scalar = fuzznum((0.6, 0.3))
broadcasted = scalar.reshape(3, 3)  # shape (3, 3)
property score

Dispatched property for score

property shape: Tuple[int, ...]
property size: int
squeeze(axis=None)

Remove single-dimensional entries from the shape of an array.

This method provides NumPy-like squeeze functionality, eliminating dimensions of size 1 from the array shape.

Parameters:

axis (int, tuple of int, or None, optional) – Selects a subset of the single-dimensional entries in the shape. If None (default), all single-dimensional entries are removed.

Returns:

The input array with single-dimensional axes removed. If the result becomes 0-dimensional, returns a Fuzznum; otherwise returns a Fuzzarray.

Return type:

Fuzznum or Fuzzarray

Examples

# Remove all single dimensions
arr = fuzzarray([[[a]], [[b]]])  # shape (2, 1, 1)
squeezed = arr.squeeze()         # shape (2,)

# Remove specific single dimension
squeezed_axis = arr.squeeze(axis=1)  # shape (2, 1)

# Squeeze Fuzznum (no change)
scalar = fuzznum((0.6, 0.3))
squeezed_scalar = scalar.squeeze()   # still a Fuzznum
std(*args, **kwargs)

Dispatched method for std

sum(*args, **kwargs)

Dispatched method for sum

to_dict()[source]

Serialize the Fuzznum to a JSON-serializable dictionary.

Returns:

Dictionary containing ‘mtype’ and ‘attributes’ describing the object.

Return type:

dict

Raises:

RuntimeError – If the object is not fully initialized.

validate_state()[source]

Validate internal consistency of the Fuzznum and its bound strategy.

Returns:

Validation summary with keys: ‘is_valid’, ‘issues’, ‘warnings’.

Return type:

dict

Notes

  • Calls into the underlying strategy’s validation method if available.

var(*args, **kwargs)

Dispatched method for var