Exemplo n.º 1
0
    def stack(*args):
        """Put pictures on top of each other,
        from top to bottom.
        Returns string, baseline arguments for stringPict.
        The baseline is the baseline of the second picture.
        Everything is centered.
        Baseline is the baseline of the second picture.
        Strings are allowed.
        The special value stringPict.LINE is a row of '-' extended to the width.
        """
        # convert everything to stringPicts; keep LINE
        objects = []
        for arg in args:
            if arg is not stringPict.LINE and isinstance(arg, basestring):
                arg = stringPict(arg)
            objects.append(arg)

        # compute new width
        newWidth = max(obj.width() for obj in objects if obj is not stringPict.LINE)

        lineObj = stringPict(hobj("-", newWidth))

        # replace LINE with proper lines
        for i, obj in enumerate(objects):
            if obj is stringPict.LINE:
                objects[i] = lineObj

        # stack the pictures, and center the result
        newPicture = []
        for obj in objects:
            newPicture.extend(obj.picture)
        newPicture = [line.center(newWidth) for line in newPicture]
        newBaseline = objects[0].height() + objects[1].baseline
        return "\n".join(newPicture), newBaseline
Exemplo n.º 2
0
    def stack(*args):
        """Put pictures on top of each other,
        from top to bottom.
        Returns string, baseline arguments for stringPict.
        The baseline is the baseline of the second picture.
        Everything is centered.
        Baseline is the baseline of the second picture.
        Strings are allowed.
        The special value stringPict.LINE is a row of '-' extended to the width.
        """
        #convert everything to stringPicts; keep LINE
        objects = []
        for arg in args:
            if arg is not stringPict.LINE and isinstance(arg, basestring):
                arg = stringPict(arg)
            objects.append(arg)

        #compute new width
        newWidth = max(obj.width() for obj in objects
                       if obj is not stringPict.LINE)

        lineObj = stringPict(hobj('-', newWidth))

        #replace LINE with proper lines
        for i, obj in enumerate(objects):
            if obj is stringPict.LINE:
                objects[i] = lineObj

        #stack the pictures, and center the result
        newPicture = []
        for obj in objects:
            newPicture.extend(obj.picture)
        newPicture = [line.center(newWidth) for line in newPicture]
        newBaseline = objects[0].height() + objects[1].baseline
        return '\n'.join(newPicture), newBaseline
Exemplo n.º 3
0
    def _print_Pow(self, power):
        if isinstance(power.exp, Basic.Half):
            # If it's a square root
            bpretty = self._print(power.base)
            H = bpretty.height()

            _zZ= xobj('/',1)
            s2 = stringPict(xobj('\\',1)+_zZ+' '*(H-1))
            for x in xrange(1, H):
                s3 = stringPict(' '*(x+1) + _zZ + ' '*(H-(x+1)))
                s2 = stringPict(*s2.above(s3))

            s2.baseline = bpretty.baseline  # vertical: each-to-each

            s = prettyForm(hobj('_', 2+ bpretty.width()))
            s = prettyForm(*bpretty.above(s))
            s = prettyForm(*s.left(s2))
            return s
        elif power.exp == -1:
            # Things like 1/x
            return prettyForm("1") / self._print(power.base)

        # None of the above special forms, do a standard power
        b,e = power.as_base_exp()
        return self._print(b)**self._print(e)
Exemplo n.º 4
0
    def _print_nth_root(self, base, expt):
        bpretty = self._print(base)

        # Construct root sign, start with the \/ shape
        _zZ = xobj('/', 1)
        rootsign = xobj('\\', 1) + _zZ
        # Make exponent number to put above it
        if isinstance(expt, C.Rational):
            exp = str(expt.q)
            if exp == '2':
                exp = ''
        else:
            exp = str(expt.args[0])
        exp = exp.ljust(2)
        if len(exp) > 2:
            rootsign = ' ' * (len(exp) - 2) + rootsign
        # Stack the exponent
        rootsign = stringPict(exp + '\n' + rootsign)
        rootsign.baseline = 0
        # Diagonal: length is one less than height of base
        linelength = bpretty.height() - 1
        diagonal = stringPict('\n'.join(' ' * (linelength - i - 1) + _zZ +
                                        ' ' * i for i in range(linelength)))
        # Put baseline just below lowest line: next to exp
        diagonal.baseline = linelength - 1
        # Make the root symbol
        rootsign = prettyForm(*rootsign.right(diagonal))
        # Det the baseline to match contents to fix the height
        # but if the height of bpretty is one, the rootsign must be one higher
        rootsign.baseline = max(1, bpretty.baseline)
        #build result
        s = prettyForm(hobj('_', 2 + bpretty.width()))
        s = prettyForm(*bpretty.above(s))
        s = prettyForm(*s.left(rootsign))
        return s
Exemplo n.º 5
0
    def _print_Pow(self, power):
        # square roots, other roots or n-th roots
        # test for fraction 1/n or power x**-1
        if power.is_commutative:
            if (isinstance(power.exp, C.Rational) and power.exp.p == 1 and power.exp.q != 1) or (
                isinstance(power.exp, C.Pow)
                and isinstance(power.exp.args[0], C.Symbol)
                and power.exp.args[1] == S.NegativeOne
            ):
                bpretty = self._print(power.base)

                # construct root sign, start with the \/ shape
                _zZ = xobj("/", 1)
                rootsign = xobj("\\", 1) + _zZ
                # make exponent number to put above it
                if isinstance(power.exp, C.Rational):
                    exp = str(power.exp.q)
                    if exp == "2":
                        exp = ""
                else:
                    exp = str(power.exp.args[0])
                exp = exp.ljust(2)
                if len(exp) > 2:
                    rootsign = " " * (len(exp) - 2) + rootsign
                # stack the exponent
                rootsign = stringPict(exp + "\n" + rootsign)
                rootsign.baseline = 0
                # diagonal: length is one less than height of base
                linelength = bpretty.height() - 1
                diagonal = stringPict("\n".join(" " * (linelength - i - 1) + _zZ + " " * i for i in range(linelength)))
                # put baseline just below lowest line: next to exp
                diagonal.baseline = linelength - 1
                # make the root symbol
                rootsign = prettyForm(*rootsign.right(diagonal))
                # set the baseline to match contents to fix the height
                # but if the height of bpretty is one, the rootsign must be one higher
                rootsign.baseline = max(1, bpretty.baseline)
                # build result
                s = prettyForm(hobj("_", 2 + bpretty.width()))
                s = prettyForm(*bpretty.above(s))
                s = prettyForm(*s.left(rootsign))
                return s
            elif power.exp.is_Rational and power.exp.is_negative:
                # Things like 1/x
                return prettyForm("1") / self._print(C.Pow(power.base, -power.exp))

        # None of the above special forms, do a standard power
        b, e = power.as_base_exp()
        return self._print(b) ** self._print(e)
Exemplo n.º 6
0
    def _print_Pow(self, power):
        # square roots, other roots or n-th roots
        #test for fraction 1/n or power x**-1
        if power.is_commutative:
            if (isinstance(power.exp, C.Rational) and power.exp.p==1 and power.exp.q !=1) or \
               (   isinstance(power.exp, C.Pow) and
                   isinstance(power.exp.args[0], C.Symbol) and
                   power.exp.args[1]==S.NegativeOne):
                bpretty = self._print(power.base)

                #construct root sign, start with the \/ shape
                _zZ = xobj('/', 1)
                rootsign = xobj('\\', 1) + _zZ
                #make exponent number to put above it
                if isinstance(power.exp, C.Rational):
                    exp = str(power.exp.q)
                    if exp == '2': exp = ''
                else: exp = str(power.exp.args[0])
                exp = exp.ljust(2)
                if len(exp) > 2: rootsign = ' ' * (len(exp) - 2) + rootsign
                #stack the exponent
                rootsign = stringPict(exp + '\n' + rootsign)
                rootsign.baseline = 0
                #diagonal: length is one less than height of base
                linelength = bpretty.height() - 1
                diagonal = stringPict('\n'.join(' ' * (linelength - i - 1) +
                                                _zZ + ' ' * i
                                                for i in range(linelength)))
                #put baseline just below lowest line: next to exp
                diagonal.baseline = linelength - 1
                #make the root symbol
                rootsign = prettyForm(*rootsign.right(diagonal))
                #set the baseline to match contents to fix the height
                #but if the height of bpretty is one, the rootsign must be one higher
                rootsign.baseline = max(1, bpretty.baseline)
                #build result
                s = prettyForm(hobj('_', 2 + bpretty.width()))
                s = prettyForm(*bpretty.above(s))
                s = prettyForm(*s.left(rootsign))
                return s
            elif power.exp.is_Rational and power.exp.is_negative:
                # Things like 1/x
                return prettyForm("1") / self._print(
                    C.Pow(power.base, -power.exp))

        # None of the above special forms, do a standard power
        b, e = power.as_base_exp()
        return self._print(b)**self._print(e)
Exemplo n.º 7
0
    def _print_nth_root (self, base, expt):
        bpretty = self._print(base)

        # Construct root sign, start with the \/ shape
        _zZ = xobj('/',1)
        rootsign = xobj('\\',1) + _zZ
        # Make exponent number to put above it
        if isinstance(expt, C.Rational):
            exp = str(expt.q)
            if exp == '2':
                exp = ''
        else:
            exp = str(expt.args[0])
        exp = exp.ljust(2)
        if len(exp) > 2:
            rootsign = ' '*(len(exp) - 2) + rootsign
        # Stack the exponent
        rootsign = stringPict(exp + '\n' + rootsign)
        rootsign.baseline = 0
        # Diagonal: length is one less than height of base
        linelength = bpretty.height() - 1
        diagonal = stringPict('\n'.join(
            ' '*(linelength - i - 1) + _zZ + ' '*i
            for i in range(linelength)
            ))
        # Put baseline just below lowest line: next to exp
        diagonal.baseline = linelength - 1
        # Make the root symbol
        rootsign = prettyForm(*rootsign.right(diagonal))
        # Det the baseline to match contents to fix the height
        # but if the height of bpretty is one, the rootsign must be one higher
        rootsign.baseline = max(1, bpretty.baseline)
        #build result
        s = prettyForm(hobj('_', 2 + bpretty.width()))
        s = prettyForm(*bpretty.above(s))
        s = prettyForm(*s.left(rootsign))
        return s
Exemplo n.º 8
0
 def _print_conjugate(self, e):
     pform = self._print(e.args[0])
     return prettyForm( *pform.above( hobj('_',pform.width())) )
Exemplo n.º 9
0
 def _print_conjugate(self, e):
     pform = self._print(e.args[0])
     return prettyForm(*pform.above(hobj('_', pform.width())))