Skip to content

Commit

Permalink
* Add align attribute in stack, top and bottom, left and `rig…
Browse files Browse the repository at this point in the history
…ht` methods.

* `paren`, `top`, `bottom`, `left` and `right` produce `stringPict` objects
* adding `superscript` and `subscript` methods
* fixing `root`
  • Loading branch information
mmatera committed Nov 13, 2024
1 parent 6ca6ec7 commit ccd8fd9
Show file tree
Hide file tree
Showing 14 changed files with 729 additions and 502 deletions.
1 change: 1 addition & 0 deletions .mailmap
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,7 @@ João Moura <[email protected]>
Juan Barbosa <[email protected]>
Juan Felipe Osorio <[email protected]>
Juan Luis Cano Rodríguez <[email protected]>
Juan Mauricio Matera <[email protected]>
Juha Remes <[email protected]>
Julien Palard <[email protected]>
Julien Rioux <[email protected]>
Expand Down
6 changes: 3 additions & 3 deletions sympy/physics/quantum/anticommutator.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,9 @@ def _sympystr(self, printer, *args):

def _pretty(self, printer, *args):
pform = printer._print(self.args[0], *args)
pform = prettyForm(*pform.right(prettyForm(',')))
pform = prettyForm(*pform.right(printer._print(self.args[1], *args)))
pform = prettyForm(*pform.parens(left='{', right='}'))
pform = pform.right(prettyForm(','))
pform = pform.right(printer._print(self.args[1], *args))
pform = pform.parens(left='{', right='}')
return pform

def _latex(self, printer, *args):
Expand Down
58 changes: 29 additions & 29 deletions sympy/physics/quantum/cg.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from sympy.core.sympify import sympify
from sympy.functions.elementary.miscellaneous import sqrt
from sympy.functions.elementary.piecewise import Piecewise
from sympy.printing.pretty.stringpict import prettyForm, stringPict
from sympy.printing.pretty.stringpict import prettyForm

from sympy.functions.special.tensor_functions import KroneckerDelta
from sympy.physics.wigner import clebsch_gordan, wigner_3j, wigner_6j, wigner_9j
Expand Down Expand Up @@ -129,21 +129,21 @@ def _pretty(self, printer, *args):
wleft = wdelta //2
wright = wdelta - wleft

s = prettyForm(*s.right(' '*wright))
s = prettyForm(*s.left(' '*wleft))
s = s.right(' '*wright)
s = s.left(' '*wleft)

if D_row is None:
D_row = s
continue
D_row = prettyForm(*D_row.right(' '*hsep))
D_row = prettyForm(*D_row.right(s))
D_row = D_row.right(' '*hsep)
D_row = D_row.right(s)
if D is None:
D = D_row
continue
for _ in range(vsep):
D = prettyForm(*D.below(' '))
D = prettyForm(*D.below(D_row))
D = prettyForm(*D.parens())
D = D.below(' ')
D = D.below(D_row)
D = D.parens()
return D

def _latex(self, printer, *args):
Expand Down Expand Up @@ -226,16 +226,16 @@ def _pretty(self, printer, *args):
top = printer._print_seq((self.j3, self.m3), delimiter=',')

pad = max(top.width(), bot.width())
bot = prettyForm(*bot.left(' '))
top = prettyForm(*top.left(' '))
bot = bot.left(' ')
top = top.left(' ')

if not pad == bot.width():
bot = prettyForm(*bot.right(' '*(pad - bot.width())))
bot = bot.right(' '*(pad - bot.width()))
if not pad == top.width():
top = prettyForm(*top.right(' '*(pad - top.width())))
s = stringPict('C' + ' '*pad)
s = prettyForm(*s.below(bot))
s = prettyForm(*s.above(top))
top = top.right(' '*(pad - top.width()))
s = prettyForm('C' + ' '*pad)
s = s.below(bot)
s = s.above(top)
return s

def _latex(self, printer, *args):
Expand Down Expand Up @@ -304,21 +304,21 @@ def _pretty(self, printer, *args):
wleft = wdelta //2
wright = wdelta - wleft

s = prettyForm(*s.right(' '*wright))
s = prettyForm(*s.left(' '*wleft))
s = s.right(' '*wright)
s = s.left(' '*wleft)

if D_row is None:
D_row = s
continue
D_row = prettyForm(*D_row.right(' '*hsep))
D_row = prettyForm(*D_row.right(s))
D_row = D_row.right(' '*hsep)
D_row = D_row.right(s)
if D is None:
D = D_row
continue
for _ in range(vsep):
D = prettyForm(*D.below(' '))
D = prettyForm(*D.below(D_row))
D = prettyForm(*D.parens(left='{', right='}'))
D = D.below(' ')
D = D.below(D_row)
D = D.parens(left='{', right='}')
return D

def _latex(self, printer, *args):
Expand Down Expand Up @@ -408,21 +408,21 @@ def _pretty(self, printer, *args):
wleft = wdelta //2
wright = wdelta - wleft

s = prettyForm(*s.right(' '*wright))
s = prettyForm(*s.left(' '*wleft))
s = s.right(' '*wright)
s = s.left(' '*wleft)

if D_row is None:
D_row = s
continue
D_row = prettyForm(*D_row.right(' '*hsep))
D_row = prettyForm(*D_row.right(s))
D_row = D_row.right(' '*hsep)
D_row = D_row.right(s)
if D is None:
D = D_row
continue
for _ in range(vsep):
D = prettyForm(*D.below(' '))
D = prettyForm(*D.below(D_row))
D = prettyForm(*D.parens(left='{', right='}'))
D = D.below(' ')
D = D.below(D_row)
D = D.parens(left='{', right='}')
return D

def _latex(self, printer, *args):
Expand Down
6 changes: 3 additions & 3 deletions sympy/physics/quantum/commutator.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,9 @@ def _sympystr(self, printer, *args):

def _pretty(self, printer, *args):
pform = printer._print(self.args[0], *args)
pform = prettyForm(*pform.right(prettyForm(',')))
pform = prettyForm(*pform.right(printer._print(self.args[1], *args)))
pform = prettyForm(*pform.parens(left='[', right=']'))
pform = pform.right(prettyForm(','))
pform = pform.right(printer._print(self.args[1], *args))
pform = pform.parens(left='[', right=']')
return pform

def _latex(self, printer, *args):
Expand Down
4 changes: 2 additions & 2 deletions sympy/physics/quantum/gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from sympy.core.sorting import default_sort_key
from sympy.core.sympify import _sympify
from sympy.functions.elementary.miscellaneous import sqrt
from sympy.printing.pretty.stringpict import prettyForm, stringPict
from sympy.printing.pretty.stringpict import stringPict

from sympy.physics.quantum.anticommutator import AntiCommutator
from sympy.physics.quantum.commutator import Commutator
Expand Down Expand Up @@ -431,7 +431,7 @@ def _pretty(self, printer, *args):
gate_name = stringPict(self.gate_name)
first = self._print_subscript_pretty(gate_name, controls)
gate = self._print_parens_pretty(gate)
final = prettyForm(*first.right(gate))
final = first.right(gate)
return final

def _latex(self, printer, *args):
Expand Down
24 changes: 10 additions & 14 deletions sympy/physics/quantum/hilbert.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,15 +414,13 @@ def _pretty(self, printer, *args):
next_pform = printer._print(self.args[i], *args)
if isinstance(self.args[i], (DirectSumHilbertSpace,
TensorProductHilbertSpace)):
next_pform = prettyForm(
*next_pform.parens(left='(', right=')')
)
pform = prettyForm(*pform.right(next_pform))
next_pform = next_pform.parens(left='(', right=')')
pform = pform.right(next_pform)
if i != length - 1:
if printer._use_unicode:
pform = prettyForm(*pform.right(' ' + '\N{N-ARY CIRCLED TIMES OPERATOR}' + ' '))
pform = pform.right(' ' + '\N{N-ARY CIRCLED TIMES OPERATOR}' + ' ')
else:
pform = prettyForm(*pform.right(' x '))
pform = pform.right(' x ')
return pform

def _latex(self, printer, *args):
Expand Down Expand Up @@ -524,15 +522,13 @@ def _pretty(self, printer, *args):
next_pform = printer._print(self.args[i], *args)
if isinstance(self.args[i], (DirectSumHilbertSpace,
TensorProductHilbertSpace)):
next_pform = prettyForm(
*next_pform.parens(left='(', right=')')
)
pform = prettyForm(*pform.right(next_pform))
next_pform = next_pform.parens(left='(', right=')')
pform = pform.right(next_pform)
if i != length - 1:
if printer._use_unicode:
pform = prettyForm(*pform.right(' \N{CIRCLED PLUS} '))
pform = pform.right(' \N{CIRCLED PLUS} ')
else:
pform = prettyForm(*pform.right(' + '))
pform = pform.right(' + ')
return pform

def _latex(self, printer, *args):
Expand Down Expand Up @@ -641,9 +637,9 @@ def _sympystr(self, printer, *args):
def _pretty(self, printer, *args):
pform_exp = printer._print(self.exp, *args)
if printer._use_unicode:
pform_exp = prettyForm(*pform_exp.left(prettyForm('\N{N-ARY CIRCLED TIMES OPERATOR}')))
pform_exp = pform_exp.left(prettyForm('\N{N-ARY CIRCLED TIMES OPERATOR}'))
else:
pform_exp = prettyForm(*pform_exp.left(prettyForm('x')))
pform_exp = pform_exp.left(prettyForm('x'))
pform_base = printer._print(self.base, *args)
return pform_base**pform_exp

Expand Down
9 changes: 4 additions & 5 deletions sympy/physics/quantum/innerproduct.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from sympy.core.expr import Expr
from sympy.functions.elementary.complexes import conjugate
from sympy.printing.pretty.stringpict import prettyForm
from sympy.physics.quantum.dagger import Dagger
from sympy.physics.quantum.state import KetBase, BraBase

Expand Down Expand Up @@ -111,10 +110,10 @@ def _pretty(self, printer, *args):
lbracket, _ = self.bra._pretty_brackets(height, use_unicode)
cbracket, rbracket = self.ket._pretty_brackets(height, use_unicode)
# Build innerproduct
pform = prettyForm(*bra.left(lbracket))
pform = prettyForm(*pform.right(cbracket))
pform = prettyForm(*pform.right(ket))
pform = prettyForm(*pform.right(rbracket))
pform = bra.left(lbracket)
pform = pform.right(cbracket)
pform = pform.right(ket)
pform = pform.right(rbracket)
return pform

def _latex(self, printer, *args):
Expand Down
10 changes: 4 additions & 6 deletions sympy/physics/quantum/operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,8 @@ def _print_contents_pretty(self, printer, *args):
else:
pform = self._print_operator_name_pretty(printer, *args)
label_pform = self._print_label_pretty(printer, *args)
label_pform = prettyForm(
*label_pform.parens(left='(', right=')')
)
pform = prettyForm(*pform.right(label_pform))
label_pform = label_pform.parens(left='(', right=')')
pform = pform.right(label_pform)
return pform

def _print_contents_latex(self, printer, *args):
Expand Down Expand Up @@ -479,7 +477,7 @@ def _sympyrepr(self, printer, *args):

def _pretty(self, printer, *args):
pform = self.ket._pretty(printer, *args)
return prettyForm(*pform.right(self.bra._pretty(printer, *args)))
return pform.right(self.bra._pretty(printer, *args))

def _latex(self, printer, *args):
k = printer._print(self.ket, *args)
Expand Down Expand Up @@ -653,5 +651,5 @@ def _print_pretty(self, printer, *args):
label_pform = prettyForm(
*label_pform.parens(left='(', right=')')
)
pform = prettyForm(*pform.right(label_pform))
pform = pform.right(label_pform)
return pform
13 changes: 7 additions & 6 deletions sympy/physics/quantum/qexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,22 +227,23 @@ def _print_sequence(self, seq, sep, printer, *args):
def _print_sequence_pretty(self, seq, sep, printer, *args):
pform = printer._print(seq[0], *args)
for item in seq[1:]:
pform = prettyForm(*pform.right(sep))
pform = prettyForm(*pform.right(printer._print(item, *args)))
pform = pform.right(sep)
pform = pform.right(printer._print(item, *args))
return pform

# Utilities for printing: these operate prettyForm objects

def _print_subscript_pretty(self, a, b):
top = prettyForm(*b.left(' '*a.width()))
bot = prettyForm(*a.right(' '*b.width()))
return prettyForm(binding=prettyForm.POW, *bot.below(top))
result = b.left(' '*a.width())
result = a.right(' '*b.width()).below(result)
result.binding = prettyForm.POW
return result

def _print_superscript_pretty(self, a, b):
return a**b

def _print_parens_pretty(self, pform, left='(', right=')'):
return prettyForm(*pform.parens(left=left, right=right))
return pform.parens(left=left, right=right)

# Printing of labels (i.e. args)

Expand Down
18 changes: 9 additions & 9 deletions sympy/physics/quantum/spin.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from sympy.functions.elementary.trigonometric import (cos, sin)
from sympy.simplify.simplify import simplify
from sympy.matrices import zeros
from sympy.printing.pretty.stringpict import prettyForm, stringPict
from sympy.printing.pretty.stringpict import prettyForm
from sympy.printing.pretty.pretty_symbology import pretty_symbol

from sympy.physics.quantum.qexpr import QExpr
Expand Down Expand Up @@ -91,8 +91,8 @@ def _print_contents(self, printer, *args):
return '%s%s' % (self.name, self._coord)

def _print_contents_pretty(self, printer, *args):
a = stringPict(str(self.name))
b = stringPict(self._coord)
a = prettyForm(str(self.name))
b = prettyForm(self._coord)
return self._print_subscript_pretty(a, b)

def _print_contents_latex(self, printer, *args):
Expand Down Expand Up @@ -841,20 +841,20 @@ def _pretty(self, printer, *args):
bot = prettyForm(*bot.right(' '*(pad - bot.width())))
if self.alpha == 0 and self.gamma == 0:
args = printer._print(self.beta)
s = stringPict('d' + ' '*pad)
s = prettyForm('d' + ' '*pad)
else:
args = printer._print(self.alpha)
args = prettyForm(*args.right(','))
args = prettyForm(*args.right(printer._print(self.beta)))
args = prettyForm(*args.right(','))
args = prettyForm(*args.right(printer._print(self.gamma)))

s = stringPict('D' + ' '*pad)
s = prettyForm('D' + ' '*pad)

args = prettyForm(*args.parens())
s = prettyForm(*s.above(top))
s = prettyForm(*s.below(bot))
s = prettyForm(*s.right(args))
args = args.parens()
s = s.above(top)
s = s.below(bot)
s = s.right(args)
return s

def doit(self, **hints):
Expand Down
5 changes: 2 additions & 3 deletions sympy/physics/quantum/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,13 @@ def _sympystr(self, printer, *args):
return '%s%s%s' % (getattr(self, 'lbracket', ""), contents, getattr(self, 'rbracket', ""))

def _pretty(self, printer, *args):
from sympy.printing.pretty.stringpict import prettyForm
# Get brackets
pform = self._print_contents_pretty(printer, *args)
lbracket, rbracket = self._pretty_brackets(
pform.height(), printer._use_unicode)
# Put together state
pform = prettyForm(*pform.left(lbracket))
pform = prettyForm(*pform.right(rbracket))
pform = pform.left(lbracket)
pform = pform.right(rbracket)
return pform

def _latex(self, printer, *args):
Expand Down
Loading

0 comments on commit ccd8fd9

Please sign in to comment.