[docs]@singledispatchdefinitialize(x:Any)->InitializedMatrix:"""Initialize an :py:class:`~mattress.InitializedMatrix.InitializedMatrix` from a Python matrix representation. This prepares the matrix for use in C++ code that can accept a ``tatami::Matrix`` instance. Args: x: Any matrix-like object. Raises: NotImplementedError: if x is not supported. Returns: A pointer to tatami object. """raiseNotImplementedError(f"initialize is not supported for objects of class: {type(x)}")
@initialize.registerdef_initialize_pointer(x:InitializedMatrix)->InitializedMatrix:returnx# no-op@initialize.registerdef_initialize_numpy(x:numpy.ndarray)->InitializedMatrix:iflen(x.shape)!=2:raiseValueError("'x' should be a 2-dimensional array")x=_contiguify(x)returnInitializedMatrix(lib.initialize_dense_matrix(x.shape[0],x.shape[1],x))ifis_package_installed("scipy"):importscipy.sparse@initialize.registerdef_initialize_sparse_csr_array(x:scipy.sparse.csr_array)->InitializedMatrix:dtmp=_contiguify(x.data)itmp=_contiguify(x.indices)indtmp=x.indptr.astype(numpy.uint64,copy=False,order="A")returnInitializedMatrix(lib.initialize_compressed_sparse_matrix(x.shape[0],x.shape[1],dtmp,itmp,indtmp,True))@initialize.registerdef_initialize_sparse_csr_matrix(x:scipy.sparse.csr_matrix)->InitializedMatrix:return_initialize_sparse_csr_array(x)@initialize.registerdef_initialize_sparse_csc_array(x:scipy.sparse.csc_array)->InitializedMatrix:dtmp=_contiguify(x.data)itmp=_contiguify(x.indices)indtmp=x.indptr.astype(numpy.uint64,copy=False,order="A")returnInitializedMatrix(lib.initialize_compressed_sparse_matrix(x.shape[0],x.shape[1],dtmp,itmp,indtmp,False))@initialize.registerdef_initialize_sparse_csc_matrix(x:scipy.sparse.csc_matrix)->InitializedMatrix:return_initialize_sparse_csc_array(x)@initialize.registerdef_initialize_delayed_array(x:delayedarray.DelayedArray)->InitializedMatrix:returninitialize(x.seed)@initialize.registerdef_initialize_SparseNdarray(x:delayedarray.SparseNdarray)->InitializedMatrix:ifx.contentsisnotNone:dvecs=[]ivecs=[]foryinx.contents:ifyisNone:ivecs.append(None)dvecs.append(None)else:ivecs.append(_contiguify(y[0]))dvecs.append(_contiguify(y[1]))else:nc=x.shape[1]dvecs=[None]*ncivecs=[None]*ncreturnInitializedMatrix(lib.initialize_fragmented_sparse_matrix(x.shape[0],x.shape[1],dvecs,ivecs,False,x.dtype,x.index_dtype))@initialize.registerdef_initialize_delayed_unary_isometric_operation_simple(x:delayedarray.UnaryIsometricOpSimple)->InitializedMatrix:components=initialize(x.seed)ptr=lib.initialize_delayed_unary_isometric_operation_simple(components.ptr,x.operation)returnInitializedMatrix(ptr)@initialize.registerdef_initialize_delayed_unary_isometric_operation_with_args(x:delayedarray.UnaryIsometricOpWithArgs)->InitializedMatrix:components=initialize(x.seed)ifisinstance(x.value,numpy.ndarray):contents=x.value.astype(numpy.float64,copy=False,order="A")ptr=lib.initialize_delayed_unary_isometric_operation_with_vector(components.ptr,x.operation,x.right,(x.along==0),contents)else:ptr=lib.initialize_delayed_unary_isometric_operation_with_scalar(components.ptr,x.operation,x.right,x.value)returnInitializedMatrix(ptr)@initialize.registerdef_initialize_delayed_subset(x:delayedarray.Subset)->InitializedMatrix:components=initialize(x.seed)fordiminrange(2):current=x.subset[dim]noop,current=_sanitize_subset(current,x.shape[dim])ifnotnoop:ptr=lib.initialize_delayed_subset(components.ptr,current,dim==0)components=InitializedMatrix(ptr)returncomponents@initialize.registerdef_initialize_delayed_bind(x:delayedarray.Combine)->InitializedMatrix:collected=[initialize(s)forsinx.seeds]returnInitializedMatrix(lib.initialize_delayed_bind([s.ptrforsincollected],x.along))@initialize.registerdef_initialize_delayed_transpose(x:delayedarray.Transpose)->InitializedMatrix:components=initialize(x.seed)ifx.perm==(1,0):ptr=lib.initialize_delayed_transpose(components.ptr)components=InitializedMatrix(ptr)returncomponents@initialize.registerdef_initialize_delayed_binary_isometric_operation(x:delayedarray.BinaryIsometricOp)->InitializedMatrix:lcomponents=initialize(x.left)rcomponents=initialize(x.right)ptr=lib.initialize_delayed_binary_isometric_operation(lcomponents.ptr,rcomponents.ptr,x.operation)returnInitializedMatrix(ptr)@initialize.registerdef_initialize_delayed_round(x:delayedarray.Round)->InitializedMatrix:components=initialize(x.seed)ifx.decimals!=0:raiseNotImplementedError("non-zero decimals in 'delayedarray.Round' are not yet supported")ptr=lib.initialize_delayed_unary_isometric_operation_simple(components.ptr,"round")returnInitializedMatrix(ptr)