[docs]defassume_n_end_and_include_negative_m_from_harmonics[TCartesian,TSpherical](c:SphericalCoordinates[TSpherical,TCartesian],expansion:Mapping[TSpherical,Array]|Array|tuple[int,...],/,*,flatten:bool=True,)->tuple[int,bool]:""" Assume `n_end` and `include_negative_m` from the expansion coefficients. Parameters ---------- c : SphericalCoordinates[TSpherical, TCartesian] The spherical coordinates. expansion : Mapping[TSpherical, Array] | Array | tuple[int, ...] The expansion coefficients. If mapping, assume that the expansion is not expanded. flatten : bool, optional Whether the expansion is flattened. Returns ------- tuple[int, bool] n_end, include_negative_m Example ------- >>> from array_api_compat import numpy as np >>> from ultrasphere import create_spherical >>> from ultrasphere_harmonics import harmonics >>> c = create_spherical() >>> harm = harmonics( ... c, ... {"theta": np.asarray(0.5), "phi": np.asarray(1.0)}, ... n_end=3, ... phase=0 ... ) >>> assume_n_end_and_include_negative_m_from_harmonics(c, harm) (3, True) """ifflatten:ifisinstance(expansion,Mapping):raiseNotImplementedError()ifisinstance(expansion,tuple):raiseNotImplementedError()size=expansion.shape[-1]n_end=0whileTrue:size_current=harm_n_ndim_le(n_end,c_ndim=c.c_ndim)ifsize_current==size:returnn_end,Trueelifsize_current>size:raiseValueError(f"The size of the last axis {size=} ""does not correspond to any n_end.")n_end+=1else:ifc.s_ndim==0:return0,Falseifisinstance(expansion,Mapping):sizes=tuple([expansion[k].shape[-1]forkinc.s_nodes])elifisinstance(expansion,tuple):sizes=expansion[-c.s_ndim:]else:sizes=expansion.shape[-c.s_ndim:]n_end=(max(sizes)+1)//2include_negative_m=notall(size==n_endforsizeinsizes)returnn_end,include_negative_m