forked from OrangeX4/typst-sympy-calculator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDefaultTypstCalculator.py
336 lines (251 loc) · 7.59 KB
/
DefaultTypstCalculator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
import sympy
from TypstCalculator import TypstCalculator
def get_default_calculator(calculator: TypstCalculator = None, complex_number: bool = True):
if calculator is None:
calculator = TypstCalculator(return_text=False, enable_subs=True)
operator, relation_op, additive_op, mp_op, postfix_op, reduce_op, func, func_mat, constant = calculator.get_decorators()
# Accents
accents = ['cancel', 'grave', 'acute', 'hat', 'tilde', 'macron', 'breve', 'hdot', 'hdot.double',
'hdot.triple', 'hdot.quad', 'diaer', 'circle', 'acute.double', 'caron', 'harrow', 'harrow.l']
for accent in accents:
calculator.define_accent(accent)
# Styles
styles = ['upright', 'italic', 'bold']
for style in styles:
calculator.define_accent(style)
# Variants
variants = ['serif', 'sans', 'frak', 'mono', 'bb', 'cal']
for variant in variants:
calculator.define_accent(variant)
# Under/Over
underover = ['underline', 'overline']
for uo in underover:
calculator.define_accent(uo)
# Symbols
abc = 'abcdefghijklmnopqrstuvwxyz'
for c in abc:
calculator.define_symbol_base(c)
calculator.define_symbol_base(c.upper())
greeks = ['alpha', 'beta', 'gamma', 'delta', 'epsilon', 'zeta', 'eta', 'theta', 'iota', 'kappa',
'lambda', 'mu', 'nu', 'xi', 'omicron', 'pi', 'rho', 'sigma', 'tau', 'upsilon', 'phi',
'chi', 'psi', 'omega']
for greek in greeks:
calculator.define_symbol_base(greek)
calculator.define_symbol_base(greek[0].upper() + greek[1:])
greek_alts = ['beta.alt', 'epsilon.alt', 'kappa.alt',
'phi.alt', 'pi.alt', 'rho.alt', 'theta.alt']
for greek_alt in greek_alts:
calculator.define_symbol_base(greek_alt)
# Constants
@constant()
def convert_oo():
return sympy.oo
calculator.set_variance('pi', sympy.pi)
calculator.set_variance('e', sympy.E)
calculator.set_variance('E', sympy.E)
# complex_number
if complex_number:
calculator.set_variance('i', sympy.I)
calculator.set_variance('I', sympy.I)
calculator.set_variance('j', sympy.I)
# Cases
@func()
def convert_cases(*args):
return args
# Relation Operators
@relation_op()
def convert_eq(a, b):
return sympy.Eq(a, b)
@relation_op()
def convert_eq_dot_not(a, b):
return sympy.Ne(a, b)
@relation_op()
def convert_gt(a, b):
return sympy.Gt(a, b)
@relation_op()
def convert_lt(a, b):
return sympy.Lt(a, b)
@relation_op()
def convert_gt_dot_eq(a, b):
return sympy.Ge(a, b)
@relation_op()
def convert_lt_dot_eq(a, b):
return sympy.Le(a, b)
# Additive Operators
@additive_op()
def convert_plus(a, b):
return a + b
# Mp Operators
@mp_op()
def convert_times(a, b):
return a * b
@mp_op()
def convert_times_dot(a, b):
return a * b
@mp_op()
def convert_div(a, b):
return a / b
# Postfix Operators
@postfix_op()
def convert_degree(expr):
return expr / 180 * sympy.pi
# Matrix
@func_mat()
def convert_mat(mat):
return sympy.Matrix(mat)
@func()
def convert_vec(*vec):
return sympy.Matrix(vec)
# Reduces
@reduce_op()
def convert_sum(expr, args):
return sympy.Sum(expr, args)
@reduce_op()
def convert_product(expr, args):
return sympy.Product(expr, args)
# Functions
@func()
def convert_derivative(expr, var):
return sympy.Derivative(expr, var)
@func()
def convert_binom(n, k):
return sympy.binomial(n, k)
@func()
def convert_frac(n, d):
return sympy.Rational(n, d)
@func()
def convert_lr(expr):
return expr
@func()
def convert_sqrt(expr):
return sympy.sqrt(expr)
@func()
def convert_root(n, expr):
return sympy.root(expr, n)
@func()
def convert_round(expr):
return sympy.Number(round(expr.evalf(), 0))
@func()
def convert_abs(expr):
return sympy.Abs(expr)
@func()
def convert_arccos(expr):
return sympy.acos(expr)
@func()
def convert_arcsin(expr):
return sympy.asin(expr)
@func()
def convert_arctan(expr):
return sympy.atan(expr)
@func()
def convert_arctan2(expr):
return sympy.atan2(expr)
@func()
def convert_ceil(expr):
return sympy.ceiling(expr)
@func()
def convert_cos(expr):
return sympy.cos(expr)
@func()
def convert_cosh(expr):
return sympy.cosh(expr)
@func()
def convert_fact(expr):
return sympy.factorial(expr)
@func()
def convert_floor(expr):
return sympy.floor(expr)
@func()
def convert_gcd(f, g):
return sympy.gcd(f, g)
@func()
def convert_lcm(f, g):
return sympy.lcm(f, g)
@func()
def convert_log(expr):
return sympy.log(expr)
@func()
def convert_max(*args):
return sympy.Max(*args)
@func()
def convert_min(*args):
return sympy.Min(*args)
@func()
def convert_pow(b, e):
return sympy.Pow(b, e)
@func()
def convert_quo(f, g):
return sympy.quo(f, g)
@func()
def convert_rem(f, g):
return sympy.rem(f, g)
@func()
def convert_sin(expr):
return sympy.sin(expr)
@func()
def convert_sinh(expr):
return sympy.sinh(expr)
@func()
def convert_tan(expr):
return sympy.tan(expr)
@func()
def convert_tanh(expr):
return sympy.tanh(expr)
# Matrix Functions
@func()
def convert_rank(expr):
return sympy.Matrix.rank(expr)
@func()
def convert_rref(expr):
return sympy.Matrix.rref(expr)
@func()
def convert_det(expr):
return sympy.det(expr)
@func()
def convert_transpose(expr):
return sympy.transpose(expr)
@func()
def convert_inverse(expr):
return expr ** -1
@func()
def convert_trace(expr):
return sympy.trace(expr)
return calculator
if __name__ == '__main__':
calculator = get_default_calculator(complex_number=True)
calculator.return_text = True
operator, relation_op, additive_op, mp_op, postfix_op, \
reduce_op, func, func_mat, constant = calculator.get_decorators()
expr = calculator.simplify('1 + 1')
assert expr == '2'
expr = calculator.evalf('1/2', n=3)
assert expr == '0.500'
calculator.set_variance('a', '1/2')
expr = calculator.simplify('a + 1')
assert expr == '3/2'
calculator.unset_variance('a')
expr = calculator.simplify('a + 1')
assert expr == 'a + 1' or expr == '1 + a'
expr = calculator.evalf('pi', n=3)
assert expr == '3.14'
expr = calculator.simplify('max(1, 2)')
assert expr == '2'
calculator.define_function('f')
expr = calculator.simplify('f(1) + f(1) - f(1)')
assert expr == 'f(1)'
expr = calculator.simplify('lim_(x -> oo) 1/x')
assert expr == '0'
expr = calculator.simplify('sum_(k = 1)^n k')
assert expr == '1/2 n (n + 1)'
expr = calculator.simplify('product_(k = 1)^3 k')
assert expr == '6'
expr = calculator.simplify('integral_0^1 x^2 dif x')
assert expr == '1/3'
expr = calculator.simplify('derivative(x^2, x)')
assert expr == '2 x'
expr = calculator.simplify('x^2 bar_1^(2+1)')
assert expr == '8'
expr = calculator.simplify('(x - 1) / (x + 1)^2')
assert expr == '(x - 1) / (x + 1)^2'
expr = calculator.simplify('-1 / (x + 1)')
assert expr == '-1 / (x + 1)'