Debian Codegraph @

test/uflacs/unit/xtest_latex_formatting.py

raw
1 # -*- coding: utf-8 -*-
2 """
3 Tests of LaTeX formatting rules.
4 """
5
6 from ffc.log import ffc_assert, info, warning, error
7 from ffc.uflacs.codeutils.expr_formatter import ExprFormatter
8 from ffc.uflacs.codeutils.latex_expr_formatting_rules import LatexFormatter
9
10 import ufl_legacy as ufl
11 from ufl_legacy.algorithms import preprocess_expression, expand_indices
12
13
14 def expr2latex(expr, variables=None):
15 "This is a test specific function for formatting ufl to LaTeX."
16
17 # Preprocessing expression before applying formatting. In a
18 # compiler, one should probably assume that these have been
19 # applied and use ExprFormatter directly.
20 expr_data = preprocess_expression(expr)
21 expr = expand_indices(expr_data.preprocessed_expr)
22
23 # This formatter is a multifunction with single operator
24 # formatting rules for generic LaTeX formatting
25 latex_formatter = LatexFormatter()
26
27 # This final formatter implements a generic framework handling
28 # indices etc etc.
29 variables = variables or {}
30 expr_formatter = ExprFormatter(latex_formatter, variables)
31 code = expr_formatter.visit(expr)
32 return code
33
34
35 def assertLatexEqual(self, expr, code, variables=None):
36 r = expr2latex(expr, variables)
37 self.assertEqual(code, r)
38
39
40 def test_latex_formatting_of_literals():
41 # Test literals
42 assert expr2latex(ufl.as_ufl(2)) == "2"
43 assert expr2latex(ufl.as_ufl(3.14)) == '3.14'
44 assert expr2latex(ufl.as_ufl(0)) == "0"
45 # These are actually converted to int before formatting:
46 assert expr2latex(ufl.Identity(2)[0, 0]) == "1"
47 assert expr2latex(ufl.Identity(2)[0, 1]) == "0"
48 assert expr2latex(ufl.Identity(2)[1, 0]) == "0"
49 assert expr2latex(ufl.Identity(2)[1, 1]) == "1"
50 assert expr2latex(ufl.PermutationSymbol(3)[1, 2, 3]) == "1"
51 assert expr2latex(ufl.PermutationSymbol(3)[2, 1, 3]) == "-1"
52 assert expr2latex(ufl.PermutationSymbol(3)[1, 1, 3]) == "0"
53
54
55 def test_latex_formatting_of_geometry():
56 # Test geometry quantities
57 x = ufl.SpatialCoordinate(ufl.interval)[0]
58 assert expr2latex(x) == "x_0"
59 x, y = ufl.SpatialCoordinate(ufl.triangle)
60 assert expr2latex(x) == "x_0"
61 assert expr2latex(y) == "x_1"
62 nx, ny = ufl.FacetNormal(ufl.triangle)
63 assert expr2latex(nx) == "n_0"
64 assert expr2latex(ny) == "n_1"
65 Kv = ufl.CellVolume(ufl.triangle)
66 assert expr2latex(Kv) == r"K_{\text{vol}}"
67 Kr = ufl.Circumradius(ufl.triangle)
68 assert expr2latex(Kr) == r"K_{\text{rad}}"
69
70
71 def test_latex_formatting_of_form_arguments():
72 # Test form arguments (faked for testing!)
73 U = ufl.FiniteElement("CG", ufl.triangle, 1)
74 V = ufl.VectorElement("CG", ufl.triangle, 1)
75 W = ufl.TensorElement("CG", ufl.triangle, 1)
76
77 v = ufl.TestFunction(U)
78 assert expr2latex(v) == r"\overset{0}{v}"
79 f = ufl.Coefficient(U, count=0)
80 assert expr2latex(f) == r"\overset{0}{w}"
81
82 f = ufl.Coefficient(V, count=1)
83 assert expr2latex(f[0]) == r"\overset{1}{w}_{0}" # NOT renumbered to 0...
84 v = ufl.Argument(V, number=3)
85 assert expr2latex(v[1]) == r"\overset{3}{v}_{1}" # NOT renumbered to 0...
86
87 f = ufl.Coefficient(W, count=2)
88 assert expr2latex(f[1, 0]) == r"\overset{2}{w}_{1 0}" # NOT renumbered to 0...
89 v = ufl.Argument(W, number=3)
90 assert expr2latex(v[0, 1]) == r"\overset{3}{v}_{0 1}" # NOT renumbered to 0...
91
92 # TODO: Test mixed functions
93 # TODO: Test tensor functions with symmetries
94
95
96 def test_latex_formatting_of_arithmetic():
97 x = ufl.SpatialCoordinate(ufl.triangle)[0]
98 assert expr2latex(x + 3) == "3 + x_0"
99 assert expr2latex(x * 2) == "2 x_0"
100 assert expr2latex(x / 2) == r"\frac{x_0}{2}"
101 assert expr2latex(x * x) == r"{x_0}^{2}" # TODO: Will gcc optimize this to x*x for us?
102 assert expr2latex(x**3) == r"{x_0}^{3}"
103 # TODO: Test all basic operators
104
105
106 def test_latex_formatting_of_cmath():
107 x = ufl.SpatialCoordinate(ufl.triangle)[0]
108 assert expr2latex(ufl.exp(x)) == r"e^{x_0}"
109 assert expr2latex(ufl.ln(x)) == r"\ln(x_0)"
110 assert expr2latex(ufl.sqrt(x)) == r"\sqrt{x_0}"
111 assert expr2latex(abs(x)) == r"\|x_0\|"
112 assert expr2latex(ufl.sin(x)) == r"\sin(x_0)"
113 assert expr2latex(ufl.cos(x)) == r"\cos(x_0)"
114 assert expr2latex(ufl.tan(x)) == r"\tan(x_0)"
115 assert expr2latex(ufl.asin(x)) == r"\arcsin(x_0)"
116 assert expr2latex(ufl.acos(x)) == r"\arccos(x_0)"
117 assert expr2latex(ufl.atan(x)) == r"\arctan(x_0)"
118
119
120 def test_latex_formatting_of_derivatives():
121 xx = ufl.SpatialCoordinate(ufl.triangle)
122 x = xx[0]
123 # Test derivatives of basic operators
124 assert expr2latex(x.dx(0)) == "1"
125 assert expr2latex(x.dx(1)) == "0"
126 assert expr2latex(ufl.grad(xx)[0, 0]) == "1"
127 assert expr2latex(ufl.grad(xx)[0, 1]) == "0"
128 assert expr2latex(ufl.sin(x).dx(0)) == r"\cos(x_0)"
129
130 # Test derivatives of form arguments
131 V = ufl.FiniteElement("CG", ufl.triangle, 1)
132 f = ufl.Coefficient(V, count=0)
133 assert expr2latex(f.dx(0)) == r"\overset{0}{w}_{, 0}"
134 v = ufl.Argument(V, number=3)
135 assert expr2latex(v.dx(1)) == r"\overset{3}{v}_{, 1}"
136 # TODO: Test more derivatives
137 # TODO: Test variable derivatives using diff
138
139
140 def xtest_latex_formatting_of_conditionals():
141 # Test conditional expressions
142 assert expr2latex(ufl.conditional(ufl.lt(x, 2), y, 3)) == "x_0 < 2 ? x_1: 3"
143 assert expr2latex(ufl.conditional(ufl.gt(x, 2), 4 + y, 3)) == "x_0 > 2 ? 4 + x_1: 3"
144 assert expr2latex(ufl.conditional(ufl.And(ufl.le(x, 2), ufl.ge(y, 4)), 7, 8)) == "x_0 <= 2 && x_1 >= 4 ? 7: 8"
145 assert expr2latex(ufl.conditional(ufl.Or(ufl.eq(x, 2), ufl.ne(y, 4)), 7, 8)) == "x_0 == 2 || x_1 != 4 ? 7: 8"
146 # TODO: Some tests of nested conditionals with correct precedences?
147
148
149 def test_latex_formatting_precedence_handling():
150 x, y = ufl.SpatialCoordinate(ufl.triangle)
151 # Test precedence handling with sums
152 # Note that the automatic sorting is reflected in formatting!
153 assert expr2latex(y + (2 + x)) == "x_1 + (2 + x_0)"
154 assert expr2latex((x + 2) + y) == "x_1 + (2 + x_0)"
155
156 assert expr2latex((2 + x) + (3 + y)) == "(2 + x_0) + (3 + x_1)"
157
158 assert expr2latex((x + 3) + 2 + y) == "x_1 + (2 + (3 + x_0))"
159 assert expr2latex(2 + (x + 3) + y) == "x_1 + (2 + (3 + x_0))"
160 assert expr2latex(2 + (3 + x) + y) == "x_1 + (2 + (3 + x_0))"
161 assert expr2latex(y + (2 + (3 + x))) == "x_1 + (2 + (3 + x_0))"
162
163 assert expr2latex(2 + x + 3 + y) == "x_1 + (3 + (2 + x_0))"
164 assert expr2latex(2 + x + 3 + y) == "x_1 + (3 + (2 + x_0))"
165
166 # Test precedence handling with divisions
167 # This is more stable than sums since there is no sorting.
168 assert expr2latex((x / 2) / 3) == r"\frac{(\frac{x_0}{2})}{3}"
169 assert expr2latex(x / (y / 3)) == r"\frac{x_0}{(\frac{x_1}{3})}"
170 assert expr2latex((x / 2) / (y / 3)) == r"\frac{(\frac{x_0}{2})}{(\frac{x_1}{3})}"
171 assert expr2latex(x / (2 / y) / 3) == r"\frac{(\frac{x_0}{(\frac{2}{x_1})})}{3}"
172
173 # Test precedence handling with highest level types
174 assert expr2latex(ufl.sin(x)) == r"\sin(x_0)"
175 assert expr2latex(ufl.cos(x + 2)) == r"\cos(2 + x_0)"
176 assert expr2latex(ufl.tan(x / 2)) == r"\tan(\frac{x_0}{2})"
177 assert expr2latex(ufl.acos(x + 3 * y)) == r"\arccos(x_0 + 3 x_1)"
178 assert expr2latex(ufl.asin(ufl.atan(x**4))) == r"\arcsin(\arctan({x_0}^{4}))"
179 assert expr2latex(ufl.sin(y) + ufl.tan(x)) == r"\sin(x_1) + \tan(x_0)"
180
181 # Test precedence handling with mixed types
182 assert expr2latex(3 * (2 + x)) == "3 (2 + x_0)"
183 assert expr2latex((2 * x) + (3 * y)) == "2 x_0 + 3 x_1"
184 assert expr2latex(2 * (x + 3) * y) == "x_1 (2 (3 + x_0))"
185
186
187 def _fixme():
188 assert expr2latex(2 * (x + 3)**4 * y) == "x_1 (2 {(3 + x_0)}^{4)" # FIXME: Precedence handling fails here
189 # TODO: More tests covering all types and more combinations!
190
191
192 def test_latex_formatting_of_variables():
193 x, y = ufl.SpatialCoordinate(ufl.triangle)
194 # Test user-provided C variables for subexpressions
195 # we can use variables for x[0], and sum, and power
196 assert expr2latex(x**2 + y**2, variables={x**2: 'x2', y**2: 'y2'}) == "x2 + y2"
197 assert expr2latex(x**2 + y**2, variables={x: 'z', y**2: 'y2'}) == r"{z}^{2} + y2"
198 assert expr2latex(x**2 + y**2, variables={x**2 + y**2: 'q'}) == "q"
199 # we can use variables in conditionals
200 if 0:
201 assert expr2latex(ufl.conditional(ufl.Or(ufl.eq(x, 2), ufl.ne(y, 4)), 7, 8), variables={ufl.eq(x, 2): 'c1', ufl.ne(y, 4): 'c2'}) == "c1 || c2 ? 7: 8"
202 # we can replace coefficients (formatted by user provided code)
203 V = ufl.FiniteElement("CG", ufl.triangle, 1)
204 f = ufl.Coefficient(V, count=0)
205 assert expr2latex(f, variables={f: 'f'}) == "f"
206 assert expr2latex(f**3, variables={f: 'f'}) == r"{f}^{3}"
207 # variables do not replace derivatives of variable expressions
208 assert expr2latex(f.dx(0), variables={f: 'f'}) == r"\overset{0}{w}_{, 0}"
209 # variables do replace variable expressions that are themselves derivatives
210 assert expr2latex(f.dx(0), variables={f.dx(0): 'df', ufl.grad(f)[0]: 'df'}) == "df"
211 assert expr2latex(ufl.grad(f)[0], variables={f.dx(0): 'df', ufl.grad(f)[0]: 'df'}) == "df"
212
213 # TODO: Test variables in more situations with indices and derivatives
214
215 # TODO: Test various compound operators