sandialabs / pyttb

Python Tensor Toolbox
https://pyttb.readthedocs.io
BSD 2-Clause "Simplified" License
26 stars 13 forks source link

Ktensor split out normalizing mode #308

Open ntjohnson1 opened 4 months ago

ntjohnson1 commented 4 months ago

Right now there is an almost circular dependence between normalize and arrange in ktensor. Arrange calls normalize here and normalize calls arrange here. The arguments line up so its not actually circular but the logic is definitely more complex.

One way to break this is to update normalize into two methods:

def normalize(
        self,
        weight_factor: Optional[Union[int, Literal["all"]]] = None,
        sort: Optional[bool] = False,
        normtype: float = 2,
    ) -> Self:

def normalize_mode(
        self,
        mode: int,
        normtype: float = 2,
    ) -> Self:

Right now the whole thing early exits and if mode is provided so we can just lift this out. Additionally this will reduce our duplication a bit because this basically becomes

for mode_idx in range(self.ndims):
   self.normalize_mode(mode_idx, normtype)

Then in arrange we probably add a flag to opt out of normalization (for when normalization calls arrange), and we change this to

for mode_idx in range(self.ndims):
   self.normalize_mode(mode_idx, normtype)