mixin.factory
Implementation factory for AxisFuzzy mixin system structural operations.
This module provides the core implementation functions for mtype-agnostic
structural and container operations on Fuzznum and Fuzzarray
objects. These functions work directly with the SoA (Struct-of-Arrays) backend
architecture to provide efficient NumPy-like functionality across all fuzzy
number types.
Architecture
The factory follows a direct implementation approach where each function:
Input Validation: Checks input types and converts
FuzznumtoFuzzarraywhen necessary for consistent processing.Backend Interaction: Extracts component arrays from the SoA backend, performs NumPy operations on each component, then reconstructs the backend.
Result Construction: Creates new
Fuzzarrayinstances with the modified backend, preserving mtype and fuzzy-specific metadata.Type Consistency: Maintains proper return types (
FuzznumvsFuzzarray) based on the mathematical properties of each operation.
Core Implementation Functions
This module implements the following categories of structural operations:
- Shape Manipulation Functions:
_reshape_factory: Changes array shape without modifying data, supports automatic dimension inference (-1)_flatten_factory: Collapses multi-dimensional arrays into 1D while preserving element order_squeeze_factory: Removes dimensions of size 1, with optional axis specification_ravel_factory: Returns contiguous flattened view (currently implemented as copy, future optimization possible)
- Transformation Functions:
_transpose_factory: Permutes array dimensions according to specified axes order, supports back-reference optimization_broadcast_to_factory: Broadcasts arrays to specified shapes following NumPy broadcasting rules
- Data Access Functions:
_copy_factory: Creates deep independent copies of fuzzy objects with full data duplication_item_factory: Extracts scalarFuzznumelements from arrays, supports multi-dimensional indexing
- Container Operations:
_concat_factory: Joins multiple arrays along existing axes with shape and type compatibility checking_stack_factory: Combines arrays along new axes with strict shape matching requirements_append_factory: Flexible append operation supporting various input types and in-place modification_pop_factory: Removes and returns elements from 1D arrays with optional in-place operation
- Boolean Testing Functions:
_any_factory: Tests if arrays contain any elements (always True for non-empty valid fuzzy arrays)_all_factory: Tests if all elements are truthy (always True for valid fuzzy arrays)
- Future Implementation Placeholders:
_sort_factory: Sorting operations (not yet implemented)_argsort_factory: Argument sorting (not yet implemented)_argmax_factory: Argument maximum (not yet implemented)_argmix_factory: Argument minimum (not yet implemented)
Backend Integration
All functions work with the SoA backend architecture:
Component Extraction: Uses
backend.get_component_arrays()to access individual fuzzy number components (e.g., membership, non-membership values).Parallel Processing: Applies the same NumPy operation to all components simultaneously, maintaining data alignment and type consistency.
Backend Reconstruction: Uses
backend_cls.from_arrays()to create new backends from modified components, preserving mtype-specific parameters.Metadata Preservation: Maintains q-values, mtype information, and other fuzzy-specific metadata through the transformation process.
Error Handling
Functions provide comprehensive error handling for:
Type Validation: Ensures inputs are valid
FuzznumorFuzzarrayobjectsShape Compatibility: Validates shapes for operations like concatenation, broadcasting, and stacking
Index Bounds: Checks array bounds for element access and removal operations
Mathematical Constraints: Enforces fuzzy number constraints and backend limitations
Empty Array Handling: Special cases for zero-size arrays and edge conditions
Performance Considerations
The factory functions are optimized for:
Vectorized Operations: Leverages NumPy’s optimized C implementations through component arrays
Memory Efficiency: Minimizes object creation and copying where possible
Backend Reuse: Reuses backend classes and metadata to avoid unnecessary allocations
Lazy Evaluation: Some operations (like transpose) include optimizations for repeated use
Type Safety
Functions maintain strict type consistency:
Input Promotion:
Fuzznuminputs are promoted to single-elementFuzzarraywhen neededReturn Type Logic: Operations that may result in scalars return
Fuzznum, others returnFuzzarrayMetadata Consistency: All type-specific metadata (mtype, q-values) is preserved through operations
Backend Compatibility: Ensures all operations preserve backend-specific constraints and properties
Notes
Functions are designed to be called from registration wrappers in
axisfuzzy.mixin.registerAll operations preserve the original object when creating copies or views
Backend-specific optimizations may be added in future versions without changing the API
Thread safety depends on the underlying NumPy operations and backend implementations
See also
axisfuzzy.mixin.registerRegistration layer that exposes these functions as methods
axisfuzzy.mixin.registryInfrastructure for dynamic injection and registration
axisfuzzy.core.backendSoA backend architecture used by all operations
axisfuzzy.core.fuzzarrayPrimary data structure for array operations
axisfuzzy.core.fuzznumsScalar fuzzy number data structure
Examples
Factory functions are typically not called directly, but through registered methods:
from axisfuzzy.mixin.factory import _reshape_factory
from axisfuzzy.core import fuzzarray, fuzznum
# Direct factory call (not typical usage)
a = fuzznum((0.6, 0.3), mtype='qrofn')
arr = fuzzarray([a, a, a, a])
reshaped = _reshape_factory(arr, 2, 2)
# Normal usage through registered methods
reshaped = arr.reshape(2, 2) # Calls _reshape_factory internally
Backend Compatibility
All factory functions work with any backend that implements:
get_component_arrays(): Returns list of NumPy arrays for each fuzzy componentfrom_arrays(cls, *arrays, q, **kwargs): Class method to reconstruct from component arraysProper mtype and parameter preservation through the reconstruction process
References
NumPy documentation for array manipulation functions
AxisFuzzy backend architecture documentation
SoA (Struct-of-Arrays) design patterns for high-performance computing