(cls, args)
Evaluates the direct product.
Evaluates the direct product.
Evaluates the direct product .
def eval(cls, args):
        """Evaluates the direct product."""
        new_args = []
        recall = False
        #flatten arguments
        for arg in args:
            if isinstance(arg, TensorProductHilbertSpace):
                new_args.extend(arg.args)
                recall = True
            elif isinstance(arg, (HilbertSpace, TensorPowerHilbertSpace)):
                new_args.append(arg)
            else:
                raise TypeError('Hilbert spaces can only be multiplied by \
                other Hilbert spaces: %r' % arg)
        #combine like arguments into direct powers
        comb_args = []
        prev_arg = None
        for new_arg in new_args:
            if prev_arg is not None:
                if isinstance(new_arg, TensorPowerHilbertSpace) and \
                    isinstance(prev_arg, TensorPowerHilbertSpace) and \
                        new_arg.base == prev_arg.base:
                    prev_arg = new_arg.base**(new_arg.exp + prev_arg.exp)
                elif isinstance(new_arg, TensorPowerHilbertSpace) and \
                        new_arg.base == prev_arg:
                    prev_arg = prev_arg**(new_arg.exp + 1)
                elif isinstance(prev_arg, TensorPowerHilbertSpace) and \
                        new_arg == prev_arg.base:
                    prev_arg = new_arg**(prev_arg.exp + 1)
                elif new_arg == prev_arg:
                    prev_arg = new_arg**2
                else:
                    comb_args.append(prev_arg)
                    prev_arg = new_arg
            elif prev_arg is None:
                prev_arg = new_arg
        comb_args.append(prev_arg)
        if recall:
            return TensorProductHilbertSpace(*comb_args)
        elif len(comb_args) == 1:
            return TensorPowerHilbertSpace(comb_args[0].base, comb_args[0].exp)
        else:
            return None
def eval ( cls , args ) : new_args = [ ] recall = False #flatten arguments for arg in args : if isinstance ( arg , TensorProductHilbertSpace ) : new_args . extend ( arg . args ) recall = True elif isinstance ( arg , ( HilbertSpace , TensorPowerHilbertSpace ) ) : new_args . append ( arg ) else : raise TypeError ( 'Hilbert spaces can only be multiplied by \
                other Hilbert spaces: %r' % arg ) #combine like arguments into direct powers comb_args = [ ] prev_arg = None for new_arg in new_args : if prev_arg is not None : if isinstance ( new_arg , TensorPowerHilbertSpace ) and isinstance ( prev_arg , TensorPowerHilbertSpace ) and new_arg . base == prev_arg . base : prev_arg = new_arg . base ** ( new_arg . exp + prev_arg . exp ) elif isinstance ( new_arg , TensorPowerHilbertSpace ) and new_arg . base == prev_arg : prev_arg = prev_arg ** ( new_arg . exp + 1 ) elif isinstance ( prev_arg , TensorPowerHilbertSpace ) and new_arg == prev_arg . base : prev_arg = new_arg ** ( prev_arg . exp + 1 ) elif new_arg == prev_arg : prev_arg = new_arg ** 2 else : comb_args . append ( prev_arg ) prev_arg = new_arg elif prev_arg is None : prev_arg = new_arg comb_args . append ( prev_arg ) if recall : return TensorProductHilbertSpace ( * comb_args ) elif len ( comb_args ) == 1 : return TensorPowerHilbertSpace ( comb_args [ 0 ] . base , comb_args [ 0 ] . exp ) else : return None