@@ -70,24 +70,23 @@ def sum_with_limit_two_args(a, n):
70
70
)
71
71
72
72
73
- func_and_latex_str_list = [
74
- ( solve , solve_latex , None ) ,
75
- ( sinc , sinc_latex , None ),
76
- ( xtimesbeta , xtimesbeta_latex , True ),
77
- ( xtimesbeta , xtimesbeta_latex_no_symbols , False ),
78
- ( sum_with_limit , sum_with_limit_latex , None ),
79
- ( sum_with_limit_two_args , sum_with_limit_two_args_latex , None ),
80
- ]
81
-
82
-
83
- @ pytest . mark . parametrize ( "func, expected_latex, math_symbol" , func_and_latex_str_list )
84
- def test_with_latex_to_str (func , expected_latex , math_symbol ):
73
+ @ pytest . mark . parametrize (
74
+ "func, expected_latex, use_math_symbols" ,
75
+ [
76
+ ( solve , solve_latex , None ),
77
+ ( sinc , sinc_latex , None ),
78
+ ( xtimesbeta , xtimesbeta_latex , True ),
79
+ ( xtimesbeta , xtimesbeta_latex_no_symbols , False ),
80
+ ( sum_with_limit , sum_with_limit_latex , None ),
81
+ ( sum_with_limit_two_args , sum_with_limit_two_args_latex , None ),
82
+ ],
83
+ )
84
+ def test_with_latex_to_str (func , expected_latex , use_math_symbols ):
85
85
"""Test with_latex to str."""
86
- # pylint: disable=protected-access
87
- if math_symbol is None :
86
+ if use_math_symbols is None :
88
87
latexified_function = with_latex (func )
89
88
else :
90
- latexified_function = with_latex (math_symbol = math_symbol )(func )
89
+ latexified_function = with_latex (use_math_symbols = use_math_symbols )(func )
91
90
assert str (latexified_function ) == expected_latex
92
91
expected_repr = r"$$ \displaystyle %s $$" % expected_latex
93
92
assert latexified_function ._repr_latex_ () == expected_repr
@@ -110,57 +109,46 @@ def inner(y):
110
109
assert get_latex (nested (3 )) == r"\mathrm{inner}(y) \triangleq xy"
111
110
112
111
113
- def test_assign_feature ():
114
- @with_latex
115
- def f (x ):
116
- return abs (x ) * math .exp (math .sqrt (x ))
117
-
118
- @with_latex
119
- def g (x ):
120
- a = abs (x )
121
- b = math .exp (math .sqrt (x ))
122
- return a * b
123
-
124
- @with_latex (reduce_assignments = False )
125
- def h (x ):
126
- a = abs (x )
127
- b = math .exp (math .sqrt (x ))
128
- return a * b
129
-
130
- assert str (f ) == (
131
- r"\mathrm{f}(x) \triangleq \left|{x}\right|\exp{\left({\sqrt{x}}\right)}"
132
- )
133
- assert str (g ) == (
134
- r"\mathrm{g}(x) \triangleq "
135
- r"\left( "
136
- r"\left|{x}\right| \right)\left( \exp{\left({\sqrt{x}}\right)} "
137
- r"\right)"
112
+ def test_use_raw_function_name ():
113
+ def foo_bar ():
114
+ return 42
115
+
116
+ assert str (with_latex (foo_bar )) == r"\mathrm{foo_bar}() \triangleq 42"
117
+ assert (
118
+ str (with_latex (foo_bar , use_raw_function_name = True ))
119
+ == r"\mathrm{foo\_bar}() \triangleq 42"
138
120
)
139
- assert str (h ) == (
140
- r"a \triangleq "
141
- r"\left|{x}\right| \\ "
142
- r"b \triangleq \exp{\left({\sqrt{x}}\right)} \\ "
143
- r"\mathrm{h}(x) \triangleq ab"
121
+ assert (
122
+ str (with_latex (use_raw_function_name = True )(foo_bar ))
123
+ == r"\mathrm{foo\_bar}() \triangleq 42"
144
124
)
145
125
146
- @with_latex (reduce_assignments = True )
126
+
127
+ def test_reduce_assignments ():
147
128
def f (x ):
148
- a = math .sqrt (math .exp (x ))
149
- return abs (x ) * math .log10 (a )
129
+ a = x + x
130
+ return 3 * a
131
+
132
+ assert str (with_latex (f )) == r"a \triangleq x + x \\ \mathrm{f}(x) \triangleq 3a"
133
+
134
+ latex_with_option = r"\mathrm{f}(x) \triangleq 3\left( x + x \right)"
135
+ assert str (with_latex (f , reduce_assignments = True )) == latex_with_option
136
+ assert str (with_latex (reduce_assignments = True )(f )) == latex_with_option
150
137
151
- assert str (f ) == (
152
- r"\mathrm{f}(x) \triangleq "
153
- r"\left|{x}\right|"
154
- r"\log_{10}{\left({\left( \sqrt{\exp{\left({x}\right)}} \right)}\right)}"
155
- )
156
138
157
- @ with_latex ( reduce_assignments = False )
139
+ def test_reduce_assignments_double ():
158
140
def f (x ):
159
- a = math .sqrt (math .exp (x ))
160
- return abs (x ) * math .log10 (a )
141
+ a = x ** 2
142
+ b = a + a
143
+ return 3 * b
161
144
162
- assert str (f ) == (
163
- r"a \triangleq "
164
- r"\sqrt{\exp{\left({x}\right)}} \\ "
165
- r"\mathrm{f}(x) \triangleq \left|{x}\right|\log_{10}{\left({a}\right)}"
145
+ assert str (with_latex (f )) == (
146
+ r"a \triangleq x^{2} \\ b \triangleq a + a \\ \mathrm{f}(x) \triangleq 3b"
147
+ )
148
+
149
+ latex_with_option = (
150
+ r"\mathrm{f}(x) \triangleq "
151
+ r"3\left( \left( x^{2} \right) + \left( x^{2} \right) \right)"
166
152
)
153
+ assert str (with_latex (f , reduce_assignments = True )) == latex_with_option
154
+ assert str (with_latex (reduce_assignments = True )(f )) == latex_with_option
0 commit comments