core.Fuzzarray

High-level Fuzznum container built on a Struct-of-Arrays (SoA) backend.

This module exposes the Fuzzarray class, which provides ndarray-like API semantics for collections of fuzzy numbers while delegating storage and bulk computation to a specialized FuzzarrayBackend implementation.

Overview

  • Fuzzarray is the main container for high-dimensional fuzzy numbers.

  • It supports efficient batch operations and slicing, similar to numpy arrays.

  • All storage and vectorized computation is delegated to a backend, which is selected based on the fuzzy number type (mtype).

Notes

  • The backend system enables high performance by avoiding per-element Python object overhead.

  • Operator overloading is supported for elementwise arithmetic and comparison.

  • Specialized vectorized operations can be registered for each fuzzy type.

Examples

from axisfuzzy.core.fuzznums import fuzznum
from axisfuzzy.core.fuzzarray import fuzzarray

# Create a 1D Fuzzarray by broadcasting a Fuzznum
a = fuzznum(mtype='qrofn', q=2, md=0.6, nmd=0.3)
arr = fuzzarray(a, shape=(3,))

# Create from a list of Fuzznum
arr2 = fuzzarray([fuzznum(md=0.1, nmd=0.2), fuzznum(md=0.2, nmd=0.3), fuzznum(md=0.3, nmd=0.4)])

# Elementwise arithmetic
result = arr + arr2

# Comparison yields boolean ndarray
mask = arr > arr2
print(mask)
class axisfuzzy.core.fuzzarray.Fuzzarray(data=None, backend=None, mtype=None, q=None, shape=None, **kwargs)[source]

Bases: object

High-performance fuzzy array using Struct of Arrays (SoA) architecture.

The Fuzzarray provides an ndarray-like interface for collections of fuzzy numbers while delegating memory layout and vectorized computations to a backend class (see FuzzarrayBackend).

Parameters:
  • data (array-like, Fuzznum or None, optional) – Input content used to initialize the array. Accepted types: - None : create an empty backend (requires shape). - Fuzznum : broadcast a single fuzznum to fill the target shape. - list/tuple/numpy.ndarray of Fuzznum : element-wise initialization.

  • backend (FuzzarrayBackend, optional) – Pre-constructed backend instance. When provided, data is ignored.

  • mtype (str, optional) – Membership type name (e.g. 'qrofn'). If omitted, the default from configuration is used.

  • q (int, optional) – q-rung parameter for q-rung based mtypes. If omitted, default is used.

  • shape (tuple of int, optional) – Target logical shape for the array (required when data is None).

  • **kwargs – Additional backend/mtype-specific keyword arguments.

Examples

from axisfuzzy.core.fuzznums import fuzznum
from axisfuzzy.core.fuzzarray import fuzzarray

# Create a 2D Fuzzarray from a list of lists
arr = fuzzarray([[fuzznum(md=0.1, nmd=0.2), fuzznum(md=0.2, nmd=0.3)],
                 [fuzznum(md=0.3, nmd=0.4), fuzznum(md=0.4, nmd=0.5)]])

# Indexing returns Fuzznum or Fuzzarray
print(arr[0, 1])  # Fuzznum
print(arr[0:1])   # Fuzzarray

# Elementwise operations
arr2 = arr + 0.1  # Broadcasting supported if operation registered

# Copy
arr3 = arr.copy()
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

all()

Test whether all array elements evaluate to True.

For fuzzy arrays, this is always True for non-empty arrays, since all valid Fuzznum instances are considered truthy.

Returns:

True if all elements are truthy (always True for valid Fuzzarrays with at least one element); False for empty arrays.

Return type:

bool

Examples

# Non-empty array with valid fuzzy numbers
arr = fuzzarray([a, b, c])
result = arr.all()  # True

# Empty array
empty = fuzzarray([])
result = empty.all()  # True (vacuously true)
any()

Test whether any array element evaluates to True.

For fuzzy arrays, this returns True if the array is not empty, since all valid Fuzznum instances are considered truthy.

Returns:

True if the array contains any elements; False for empty arrays.

Return type:

bool

Examples

# Non-empty array
arr = fuzzarray([a, b, c])
result = arr.any()  # True

# Empty array
empty = fuzzarray([])
result = empty.any()  # False
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)
property backend: FuzzarrayBackend

Access to the underlying backend.

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)
concat(*others, axis=0)

Join a sequence of Fuzzarrays along an existing axis.

This method provides NumPy-like concatenate functionality for fuzzy arrays, combining multiple arrays along a specified dimension.

Parameters:
  • *others (Fuzzarray) – Additional Fuzzarray objects to concatenate with this array.

  • axis (int, optional) – Axis along which the arrays are joined. Default is 0.

Returns:

Concatenated array with combined data from all input arrays.

Return type:

Fuzzarray

Raises:
  • TypeError – If any input is not a Fuzzarray.

  • ValueError – If arrays have incompatible mtypes, q values, or shapes for concatenation.

Examples

# Concatenate along first axis
arr1 = fuzzarray([a, b])        # shape (2,)
arr2 = fuzzarray([c, d])        # shape (2,)
concatenated = arr1.concat(arr2)  # shape (4,)

# Concatenate multiple arrays
arr3 = fuzzarray([e, f])
concatenated = arr1.concat(arr2, arr3)  # shape (6,)

# Concatenate along specific axis
arr2d1 = fuzzarray([[a, b]])    # shape (1, 2)
arr2d2 = fuzzarray([[c, d]])    # shape (1, 2)
concatenated = arr2d1.concat(arr2d2, axis=0)  # shape (2, 2)
copy()[source]

Create a deep copy

Return type:

Fuzzarray

distance(*args, **kwargs)

Dispatched method for distance

property dtype

Data type (always object for compatibility).

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.

execute_vectorized_op(op_name, other=None)[source]

Execute a vectorized operation using the registered operation handlers.

The method queries the global operation registry for the named operation implementation for this Fuzzarray’s mtype. If a backend/vectorized specialization exists it is used; otherwise a fallback element-wise path is taken.

Parameters:
  • op_name (str) – Operation name (e.g. 'add', 'mul', 'gt').

  • other (Fuzzarray, Fuzznum, scalar, ndarray or None, optional) – Second operand.

Returns:

Result of the vectorized operation. Comparison operations yield boolean numpy arrays; arithmetic operations yield Fuzzarray.

Return type:

Fuzzarray or numpy.ndarray

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,)
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
property kwargs: Dict[str, Any]

Additional parameters for the fuzzy type.

max(*args, **kwargs)

Dispatched method for max

property md: ndarray
mean(*args, **kwargs)

Dispatched method for mean

min(*args, **kwargs)

Dispatched method for min

property mtype: str

fuzzy type of fuzzy numbers.

property ndim: int

Number of dimensions.

property nmd: ndarray
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

property q: int | None

Q-rung parameter (if applicable).

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, ...]

Shape of the fuzzy array.

property size: int

Total number of elements.

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
stack(*others, axis=0)

Stack Fuzzarrays along a new axis.

This method provides NumPy-like stack functionality, joining arrays along a newly created dimension rather than an existing one.

Parameters:
  • *others (Fuzzarray) – Additional Fuzzarray objects to stack with this array. All arrays must have the same shape.

  • axis (int, optional) – Axis position in the result array along which the input arrays are stacked. Default is 0.

Returns:

Stacked array with one additional dimension compared to the input arrays.

Return type:

Fuzzarray

Raises:
  • TypeError – If any input is not a Fuzzarray.

  • ValueError – If arrays have incompatible mtypes, q values, or different shapes.

Examples

# Stack 1D arrays to create 2D array
arr1 = fuzzarray([a, b])        # shape (2,)
arr2 = fuzzarray([c, d])        # shape (2,)
stacked = arr1.stack(arr2)      # shape (2, 2)

# Stack along different axis
stacked_axis1 = arr1.stack(arr2, axis=1)  # shape (2, 2)

# Stack multiple arrays
arr3 = fuzzarray([e, f])
stacked = arr1.stack(arr2, arr3)  # shape (3, 2)
std(*args, **kwargs)

Dispatched method for std

sum(*args, **kwargs)

Dispatched method for sum

to_csv(*args, **kwargs)

Dispatched method for to_csv

to_json(*args, **kwargs)

Dispatched method for to_json

to_npy(*args, **kwargs)

Dispatched method for to_npy

var(*args, **kwargs)

Dispatched method for var