core.backend

Abstract backend interface for Fuzzarray (Struct-of-Arrays).

This module defines the FuzzarrayBackend abstract base class which is the primary contract between high-level Fuzzarray containers and concrete, mtype-specific, NumPy-backed implementations.

class axisfuzzy.core.backend.FuzzarrayBackend(shape, q=None, **kwargs)[source]

Bases: ABC

Abstract base class for SoA (Struct-of-Arrays) fuzzy-number backends.

The backend owns the NumPy arrays that store components for each element (e.g. membership / non-membership arrays depending on mtype) and exposes array-level operations used by higher-level containers.

Parameters:
  • shape (tuple of int) – Logical shape of the array.

  • q (int, optional) – Effective q-rung for q-rung fuzzy types. If None, uses axisfuzzy.config.get_config().DEFAULT_Q().

  • **kwargs – Backend-specific options.

shape

Logical shape of the array.

Type:

tuple of int

size

Total number of elements (product of shape).

Type:

int

q

Effective q-rung used by this backend instance.

Type:

int

kwargs

Backend-specific extra parameters.

Type:

dict

mtype

Backend-reported mtype; default read from configuration.

Type:

str

Notes

  • Concrete subclasses must implement array initialization, element views, slicing/copy semantics and formatting helpers.

  • Implementations should prefer views (shared memory) for slicing when possible to avoid unnecessary copies.

Examples

Create a concrete backend subclass and instantiate it:

class MyBackend(FuzzarrayBackend):
    def _initialize_arrays(self):
        self._a = np.zeros(self.shape, dtype=float)
        self._b = np.ones(self.shape, dtype=float)
    # implement other abstract methods...

be = MyBackend((2, 3), q=2)
print(be.shape, be.size)
abstract property cmpnames: Tuple[str, ...]

Return the names of the component arrays.

Returns:

A tuple of component names, e.g., (‘md’, ‘nmd’).

Return type:

Tuple[str, …]

abstract property cmpnum: int

Return the number of component arrays expected by this backend.

Returns:

The number of component arrays (e.g., 2 for q-ROFNs).

Return type:

int

abstractmethod copy()[source]

Produce a deep copy of this backend and its arrays.

Returns:

New backend instance with duplicated arrays (no shared memory).

Return type:

FuzzarrayBackend

Notes

  • The returned instance must not share memory with the original.

Examples

new_backend = backend.copy()
assert new_backend is not backend
abstract property dtype: dtype

Return the expected numpy dtype for component arrays.

Returns:

The expected data type, e.g., np.float64.

Return type:

np.dtype

fill_from_values(*values)[source]

Broadcast provided component values to every element.

Parameters:

*values (float) – Values to broadcast to each component across the whole backend.

Notes

  • Subclasses should validate the number of values against the expected number of components for the backend’s mtype.

format_elements(format_spec='')[source]

Smart, truncated formatting for element display.

For small arrays the entire content is formatted. For larger arrays a head/tail strategy is used and middle elements are replaced by ellipses.

Parameters:

format_spec (str, optional) – Format specification passed from higher-level formatting calls.

Returns:

Array of formatted strings with the same logical shape.

Return type:

numpy.ndarray

Examples

formatted = backend.format_elements('.3f')
static from_arrays(*components, **kwargs)[source]

Factory to construct a backend from raw component arrays.

Parameters:
  • *components (array_like) – Component arrays representing the SoA layout for a specific mtype.

  • **kwargs – Backend-specific keyword arguments (e.g., shape, q, dtype).

Returns:

New backend instance initialised from the component arrays.

Return type:

FuzzarrayBackend

Notes

  • Concrete subclasses should validate shapes and dtypes and may accept already-viewed arrays to avoid copies.

  • Subclasses MUST implement fuzzy constraint validation for the component arrays to ensure all elements satisfy the fuzzy number constraints specific to their mtype.

Examples

be = ConcreteBackend.from_arrays(m_arr, n_arr)
get_component_arrays()[source]

Return the underlying component arrays.

In fact, it is the attribute component of a specific mtype.

Returns:

Tuple of NumPy arrays that form the SoA backend.

Return type:

tuple

Notes

  • The order and meaning of the returned arrays is backend-specific.

abstractmethod get_fuzznum_view(index)[source]

Return a lightweight Fuzznum view for the element at index.

The view should be a thin proxy that reads/writes directly into the backend arrays without performing a deep copy.

Parameters:

index (int or tuple or slice) – Indexing key following NumPy semantics for single-element access.

Returns:

A Fuzznum-like proxy object.

Return type:

Fuzznum

Notes

  • Implementations may return a proxy class that reflects changes back into the underlying arrays.

Examples

view = backend.get_fuzznum_view((0, 1))
view.membership = 0.7  # updates backend arrays in-place
property ndim: int

Number of dimensions of the logical Fuzzarray.

Returns:

The number of dimensions (len(shape)).

Return type:

int

abstractmethod set_fuzznum_data(index, fuzznum)[source]

Assign values at index using a Fuzznum-like object.

Parameters:
  • index (int or tuple or slice) – Target location to set.

  • fuzznum (Fuzznum) – Source providing component values.

Raises:
  • IndexError – If index is out of bounds.

  • TypeError – If fuzznum is incompatible with this backend’s mtype.

Examples

backend.set_fuzznum_data(0, some_fuzznum)
abstractmethod slice_view(key)[source]

Return a backend representing a view/slice of this backend.

Parameters:

key (slice, tuple of slices or other valid indexing key) – Indexing key describing the requested view.

Returns:

Backend representing the requested slice. Should share memory with the original wherever feasible.

Return type:

FuzzarrayBackend

Notes

  • Prefer returning a view that supports read/write semantics without unnecessary copies.

Examples

view = backend.slice_view(np.s_[0:2, :])