-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
802 lines (647 loc) · 28.1 KB
/
app.py
File metadata and controls
802 lines (647 loc) · 28.1 KB
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
from flask import Flask, render_template, request, jsonify
app = Flask(__name__, static_url_path = "/static", static_folder = "static")
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import proj3d, Axes3D
import matplotlib
from matplotlib import cm
from io import BytesIO
import base64
import numpy as np
from sympy import symbols, sympify, latex, integrate, solve, solveset, Matrix, expand, factor, primitive, simplify, factor_list
from sympy.parsing.sympy_parser import parse_expr
from scipy.stats import scoreatpercentile
import graph as graphplot
from sympy.matrices import randMatrix
font = {'size': 12}
matplotlib.rc('font', **font)
colormap = cm.magma
from matplotlib.patches import FancyArrowPatch
class Arrow3D(FancyArrowPatch):
def __init__(self, xs, ys, zs, *args, **kwargs):
FancyArrowPatch.__init__(self, (0,0), (0,0), *args, **kwargs)
self._verts3d = xs, ys, zs
def draw(self, renderer):
xs3d, ys3d, zs3d = self._verts3d
xs, ys, zs = proj3d.proj_transform(xs3d, ys3d, zs3d, renderer.M)
self.set_positions((xs[0],ys[0]),(xs[1],ys[1]))
FancyArrowPatch.draw(self, renderer)
@app.route('/')
def index():
return render_template("index.html"), 404
def is_sqr_matrix(M):
if not 'Matrix' in str(type(M)): return False
if M.shape[0] != M.shape[1]: return False
return True
@app.route('/expression', methods=['POST'])
def expression():
try:
print('expression: {}'.format(request.json))
ps = parse_expr(request.json['expression'], locals())
symbols = [str(s) for s in ps.free_symbols]
symbols_latex = [latex(s) for s in ps.free_symbols]
symbols.sort()
symbols_latex.sort()
return jsonify({
'expression': str(ps),
'expression_latex': latex(ps),
'variables': symbols,
'is_constant': 'Integer' in str(type(ps))
or 'Float' in str(type(ps))
or 'Rational' in str(type(ps)),
'is_equality': 'Equality' in str(type(ps)),
'is_inequality': 'Inequality' in str(type(ps))
or 'Greater' in str(type(ps))
or 'Less' in str(type(ps)),
'is_matrix': 'Matrix' in str(type(ps)),
'dimension': list(ps.shape) if 'Matrix' in str(type(ps)) else [0, 0],
'is_ugly': True if 'Matrix' in str(type(ps)) and (ps.shape[0] > 10 or ps.shape[1] > 10) else False,
'is_weighted': graphplot.is_weighted(ps) if is_sqr_matrix(ps) else False,
'is_directed': graphplot.is_directed(ps) if is_sqr_matrix(ps) else False
})
except Exception as e:
print(e)
return str(e), 400
@app.route('/simplify', methods=['POST'])
def simplifyexpr():
try:
print('simplify: {}'.format(request.json))
ps = parse_expr(request.json['expression'], locals())
result = expand(ps)
return jsonify({ 'in': latex(ps), 'out': latex(result), 'out_expression': str(result) })
except Exception as e:
print(e)
return str(e), 400
@app.route('/expand', methods=['POST'])
def expandexpr():
try:
print('expand: {}'.format(request.json))
ps = parse_expr(request.json['expression'], locals())
result = expand(ps, trig=request.json['trig'])
return jsonify({ 'in': latex(ps), 'out': latex(result), 'out_expression': str(result) })
except Exception as e:
print(e)
return str(e), 400
@app.route('/factor', methods=['POST'])
def factorexpr():
try:
print('factor: {}'.format(request.json))
ps = parse_expr(request.json['expression'], locals())
result = factor(ps)
return jsonify({ 'in': latex(ps), 'out': latex(result), 'out_expression': str(result) })
except Exception as e:
print(e)
return str(e), 400
@app.route('/factors', methods=['POST'])
def factorlist():
try:
print('factor list: {}'.format(request.json))
ps = parse_expr(request.json['expression'], locals())
return jsonify({ 'in': latex(ps), 'out': latex(factor_list(ps)) })
except Exception as e:
print(e)
return str(e), 400
@app.route('/primitive', methods=['POST'])
def primitives():
try:
print('primitive: {}'.format(request.json))
ps = parse_expr(request.json['expression'], locals())
return jsonify({ 'in': latex(ps), 'out': latex(primitive(ps)) })
except Exception as e:
print(e)
return str(e), 400
@app.route('/diff', methods=['POST'])
def diff():
try:
print('diff: {}'.format(request.json))
ps = parse_expr(request.json['expression'], locals())
var = request.json['var']
result = ps.diff(var)
return jsonify({ 'in': latex(ps), 'out': latex(result), 'var': var, 'out_expression': str(result) })
except Exception as e:
print(e)
return str(e), 400
@app.route('/diff2', methods=['POST'])
def diff2():
try:
print('diff2: {}'.format(request.json))
ps = parse_expr(request.json['expression'], locals())
var = request.json['var']
result = ps.diff(var).diff(var)
return jsonify({ 'in': latex(ps), 'out': latex(result), 'var': var, 'out_expression': str(result) })
except Exception as e:
print(e)
return str(e), 400
@app.route('/grad', methods=['POST'])
def grad():
try:
print('grad: {}'.format(request.json))
ps = parse_expr(request.json['expression'], locals())
symbols = [str(s) for s in ps.free_symbols]
symbols.sort()
grad = Matrix([ps.diff(v) for v in symbols])
return jsonify({ 'in': latex(ps), 'out': latex(grad), 'out_expression': str(grad) })
except Exception as e:
print(e)
return str(e), 400
@app.route('/hessian', methods=['POST'])
def hessian():
try:
print('hessian: {}'.format(request.json))
ps = parse_expr(request.json['expression'], locals())
symbols = [str(s) for s in ps.free_symbols]
symbols.sort()
x = symbols[0]
y = symbols[1]
hessian = Matrix([[ps.diff(x).diff(x), ps.diff(x).diff(y)],[ps.diff(y).diff(x), ps.diff(y).diff(y)]])
return jsonify({ 'in': latex(ps), 'out': latex(hessian), 'hessian': latex(hessian.det()), 'out_expression': str(hessian) })
except Exception as e:
print(e)
return str(e), 400
@app.route('/integrate', methods=['POST'])
def integration():
try:
print('integrate: {}'.format(request.json))
ps = parse_expr(request.json['expression'], locals())
var = [s for s in ps.free_symbols if str(s) == request.json['var']][0]
limits = (var, request.json['from'], request.json['to']) if len(request.json['to']) > 0 else var
result = integrate(ps, limits)
return jsonify({ 'in': latex(ps), 'out': latex(result), 'var': str(var), 'out_expression': str(result) })
except Exception as e:
print(e)
return str(e), 400
@app.route('/solvefor', methods=['POST'])
def solve_for():
try:
print('solve for: {}'.format(request.json))
ps = parse_expr(request.json['expression'], locals())
var = request.json['var']
result = solve(ps, var)
return jsonify({ 'in': latex(ps), 'out': latex(result), 'var': var, 'out_expression': str(result) })
except Exception as e:
print(e)
return str(e), 400
@app.route('/plot', methods=['POST'])
def plot():
try:
print('plot: {}'.format(request.json))
ps = parse_expr(request.json['expression'], locals())
fig = plt.figure(figsize=(6.15,5))
fig.clf()
if len(ps.free_symbols) == 1:
X = np.linspace(request.json['xlim'][0], request.json['xlim'][1], 512)
Y = np.array([ps.subs(list(ps.free_symbols)[0], x) for x in X]).astype('float')
ax = fig.add_subplot(111)
ax.plot(X,list(Y), c='purple', lw=3)
ax.set_xlabel(str(list(ps.free_symbols)[0]))
ax.set_ylabel('f({})'.format(str(list(ps.free_symbols)[0])))
if '\\' not in latex(ps):
plt.title('Line plot of ${}$'.format(latex(ps)))
plt.grid(ls='dashed', alpha=.5)
elif len(ps.free_symbols) == 2:
var = [str(s) for s in ps.free_symbols]
var.sort()
xs = np.linspace(request.json['xlim'][0], request.json['xlim'][1], 32)
ys = np.linspace(request.json['ylim'][0], request.json['ylim'][1], 32)
X, Y = np.meshgrid(xs, ys)
zs = np.array([expand(ps).subs(var[0], x).subs(var[1], y).evalf() for x, y in zip(np.ravel(X), np.ravel(Y))]).astype('float')
Z = zs.reshape(X.shape)
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X, Y, Z, cmap=colormap)
plt.title('Surface plot of ${}$'.format(latex(ps)))
ax.set_xlabel(str(var[0]))
ax.set_ylabel(str(var[1]))
ax.set_zlabel('f({},{})'.format(str(var[0]), str(var[1])))
else:
return 'Too many or no variables to plot!', 400
data = BytesIO()
fig.savefig(data)
data.seek(0)
encoded_img = base64.b64encode(data.read())
return jsonify({ 'expression': str(ps), 'latex': latex(ps), 'img': 'data:image/png;base64,' + str(encoded_img)[2:-1] })
except Exception as e:
print(e)
return str(e), 400
@app.route('/cplot', methods=['POST'])
def cplot():
try:
print('cplot: {}'.format(request.json))
ps = parse_expr(request.json['expression'], locals())
var = [str(s) for s in ps.free_symbols]
var.sort()
if len(ps.free_symbols) != 2:
raise ValueError('Contour plots requires a function of two variables.')
detail = 24
fig = plt.figure(figsize=(6.15,5))
fig.clf()
xs = np.linspace(request.json['xlim'][0], request.json['xlim'][1], 32)
ys = np.linspace(request.json['ylim'][0], request.json['ylim'][1], 32)
X, Y = np.meshgrid(xs, ys)
zs = np.array([expand(ps).subs(var[0], x).subs(var[1], y).evalf() for x, y in zip(np.ravel(X), np.ravel(Y))]).astype('float')
Z = zs.reshape(X.shape)
ax = fig.add_subplot(111)
CS = ax.contourf(X, Y, Z, detail*2, cmap=colormap)
fig.colorbar(CS, shrink=0.5, aspect=5)
plt.title('Contour plot of ${}$'.format(latex(ps)))
ax.set_xlabel(str(var[0]))
ax.set_ylabel(str(var[1]))
data = BytesIO()
fig.savefig(data)
data.seek(0)
encoded_img = base64.b64encode(data.read())
return jsonify({ 'expression': str(ps), 'latex': latex(ps), 'img': 'data:image/png;base64,' + str(encoded_img)[2:-1] })
except Exception as e:
print(e)
return str(e), 400
@app.route('/gplot', methods=["POST"])
def gplot():
try:
print('gplot: {}'.format(request.json))
ps = parse_expr(request.json['expression'], locals())
var = [str(s) for s in ps.free_symbols]
var.sort()
if len(ps.free_symbols) != 2:
raise ValueError('Contour plots requires a function of two variables.')
fig = plt.figure(figsize=(6.15,5))
fig.clf()
ax = fig.add_subplot(111)
detail = 24
arrows = 1.5
# contour plot
grad = Matrix([ps.diff(var[0]), ps.diff(var[1])])
xs = np.linspace(request.json['xlim'][0], request.json['xlim'][1], detail)
ys = np.linspace(request.json['ylim'][0], request.json['ylim'][1], detail)
X, Y = np.meshgrid(xs, ys)
zs = np.array([expand(ps).subs(var[0], x).subs(var[1], y).evalf() for x, y in zip(np.ravel(X), np.ravel(Y))]).astype('float')
Z = zs.reshape(X.shape)
plt.contourf(X, Y, Z, detail*2, cmap=colormap)
# vector field
xs = np.linspace(request.json['xlim'][0], request.json['xlim'][1], np.floor(detail/arrows))
ys = np.linspace(request.json['ylim'][0], request.json['ylim'][1], np.floor(detail/arrows))
X, Y = np.meshgrid(xs, ys)
dzs = np.array([grad.subs(var[0], x).subs(var[1], y).evalf() for x, y in zip(np.ravel(X), np.ravel(Y))]).astype('float')
dZx = np.array([z[0] for z in dzs])
dZy = np.array([z[1] for z in dzs])
dZz = np.array([np.sqrt(np.dot(z,z)) for z in dzs])
#unitX = dZx / dZz
#unitY = dZy / dZz
#dZx = np.min(dZx, unitX)
#dZy = np.min(dZy, unitY)
quiv = plt.quiver(X,Y,dZx,dZy,dZz,cmap=colormap, pivot="middle")
plt.colorbar(quiv, shrink=0.8)
plt.title('Gradient plot of ${}$'.format(latex(ps)))
ax.set_xlabel(str(var[0]))
ax.set_ylabel(str(var[1]))
data = BytesIO()
fig.savefig(data)
data.seek(0)
encoded_img = base64.b64encode(data.read())
return jsonify({ 'expression': str(ps), 'latex': latex(ps), 'img': 'data:image/png;base64,' + str(encoded_img)[2:-1] })
except Exception as e:
print(e)
return str(e), 400
@app.route('/pplot', methods=["POST"])
def pplot():
try:
print('pplot: {}'.format(request.json))
ps = parse_expr(request.json['expression'], locals())
var = [str(s) for s in ps.free_symbols]
var.sort()
if len(ps.free_symbols) != 1:
raise ValueError('Parametric plot requires a function of one variable.')
if not 'Matrix' in str(type(ps)):
raise ValueError('Parametric plot requires a matrix.')
a = 24
fig = plt.figure(figsize=(5,5))
fig.clf()
if ps.shape == (2, 1):
ax = fig.add_subplot(111)
ts = np.linspace(request.json['xlim'][0], request.json['xlim'][1], a**2)
Y = np.array([ps.subs(var[0], t).evalf() for t in ts]).astype('float')
xs = [x[0] for x in Y]
ys = [x[1] for x in Y]
plt.plot(xs, ys, c="purple", lw=3)
plt.grid(ls='dashed',alpha=0.5)
elif ps.shape == (3, 1):
ts = np.linspace(request.json['xlim'][0], request.json['xlim'][1], a**2)
Y = np.array([expand(ps).subs(var[0], t).evalf() for t in ts]).astype('float')
xs = [x[0] for x in Y]
ys = [x[1] for x in Y]
zs = [x[2] for x in Y]
ax = fig.add_subplot(111, projection='3d')
plt.plot(xs, ys, zs, c="purple",lw=3)
ax.set_xlabel('${}$'.format(latex(ps[0])))
ax.set_ylabel('${}$'.format(latex(ps[1])))
ax.set_zlabel('${}$'.format(latex(ps[2])))
else:
raise ValueError('Parametric plot requires a 3x1 or 2x1 matrix.')
plt.title('Parametric plot of $\\left< {} \\right>$'.format( ', '.join([latex(p) for p in ps] )))
data = BytesIO()
fig.savefig(data)
data.seek(0)
encoded_img = base64.b64encode(data.read())
return jsonify({ 'expression': str(ps), 'latex': latex(ps), 'img': 'data:image/png;base64,' + str(encoded_img)[2:-1] })
except Exception as e:
print(e)
return str(e), 400
@app.route('/tplot', methods=["POST"])
def tplot():
try:
print('tplot: {}'.format(request.json))
raise ValueError('not implemented.')
except Exception as e:
print(e)
return str(e), 400
@app.route('/vplot', methods=["POST"])
def vplot():
try:
print('vplot: {}'.format(request.json))
ps = parse_expr(request.json['expression'], locals())
var = [str(s) for s in ps.free_symbols]
var.sort()
if len(ps.free_symbols) > 0:
raise ValueError('Vector plot requires scalars.')
if not 'Matrix' in str(type(ps)):
raise ValueError('Vector plot requires a matrix.')
if not (ps.shape[1] == 2 or ps.shape[1] == 3 or ps.shape[0] == 2 or ps.shape[0] == 3):
raise ValueError('Vector plot requires a MxN matrix where M > 0 and N is [2, 3].')
fig = plt.figure(figsize=(5,5))
fig.clf()
U = np.array(ps).astype('float')
if U.shape == (1, 2): U = U.T
elif U.shape == (1, 3): U = U.T
if U.shape == (2, 1): U = np.array([U])
elif U.shape == (3, 1): U = np.array([U])
if U.shape[1] == 3:
ax = fig.add_subplot(111, projection='3d')
plt.xlim([-1,1])
plt.ylim([-1,1])
plt.axis('equal')
X = np.array([np.cos(x) for x in np.linspace(0, 2*np.pi, 64)])
Y = np.array([np.sin(y) for y in np.linspace(0, 2*np.pi, 64)])
zeros = np.zeros(X.shape)
plt.plot(X,zeros,zeros,c='darkgray', lw=2, alpha=0.33, ls='dashed')
plt.plot(zeros,X,zeros,c='darkgray', lw=2, alpha=0.33, ls='dashed')
plt.plot(zeros,zeros,X,c='darkgray', lw=2, alpha=0.33, ls='dashed')
for i in range(U.shape[0]):
x = U[i][0]; y = U[i][1]; z = U[i][2]
magn = np.sqrt(x**2+y**2+z**2)
ax.add_artist(Arrow3D([0, x/magn], [0, y/magn], [0,z/magn], mutation_scale=15, lw=3, arrowstyle="-|>", color="purple"))
ax.add_artist(Arrow3D([0, x/magn], [0, y/magn], [0,0], mutation_scale=8, lw=2, arrowstyle="-|>", color="darkgray", alpha=0.5))
plt.plot(X,Y,zeros,c='darkgray', lw=3, alpha=0.33)
plt.plot(zeros,X,Y,c='darkgray', lw=3, alpha=0.33)
plt.axis('off')
fig.tight_layout()
elif U.shape[1] == 2:
plt.xlim([-1,1])
plt.ylim([-1,1])
plt.axis('equal')
X = [np.cos(x) for x in np.linspace(0, 2*np.pi, 64)]
Y = [np.sin(y) for y in np.linspace(0, 2*np.pi, 64)]
plt.plot(X,Y, c='darkgray', lw=3)
for i in range(U.shape[0]):
x = U[i][0]; y = U[i][1]
magn = np.sqrt(x**2 + y**2)
plt.annotate("", xy=(x/magn, y/magn), xytext=(0, 0),arrowprops=dict(arrowstyle="->", color="purple", lw=4))
plt.axhline(0, ls='dashed', alpha=0.33, c='gray', lw=3)
plt.axvline(0, ls='dashed', alpha=0.33, c='gray', lw=3)
plt.axis('off')
else:
raise ValueError('vplot requires the columns to be 3 or 2 dimensional.')
data = BytesIO()
fig.savefig(data, bbox_inches='tight')
data.seek(0)
encoded_img = base64.b64encode(data.read())
return jsonify({ 'expression': str(ps), 'latex': latex(ps), 'img': 'data:image/png;base64,' + str(encoded_img)[2:-1] })
except Exception as e:
print(e)
return str(e), 400
@app.route('/mplot', methods=["POST"])
def mplot():
try:
print('mplot: {}'.format(request.json))
ps = parse_expr(request.json['expression'], locals())
var = [str(s) for s in ps.free_symbols]
var.sort()
if len(ps.free_symbols) > 0:
raise ValueError('Matrix plot requires scalars.')
if not 'Matrix' in str(type(ps)):
raise ValueError('Matrix plot requires a matrix.')
fig = plt.figure(figsize=(5,5))
fig.clf()
M = np.array(ps).astype('float')
im = plt.imshow(M, cmap=colormap)
plt.colorbar(im, shrink=0.6, aspect=8)
data = BytesIO()
fig.savefig(data, bbox_inches='tight')
data.seek(0)
encoded_img = base64.b64encode(data.read())
return jsonify({ 'expression': str(ps), 'latex': latex(ps), 'img': 'data:image/png;base64,' + str(encoded_img)[2:-1] })
except Exception as e:
print(e)
return str(e), 400
@app.route('/polar_plot', methods=["POST"])
def polarplot():
try:
print('polar plot: {}'.format(request.json))
ps = parse_expr(request.json['expression'], locals())
if len(ps.free_symbols) != 1:
raise ValueError('Polar plot requires one variable.')
fig = plt.figure(figsize=(5,5))
fig.clf()
f = ps
var = list(f.free_symbols)[0]
r = np.linspace(request.json['xlim'][0]*np.pi, request.json['xlim'][1]*np.pi, 1024)
theta = [f.subs(var, r).evalf() for r in r]
ax = plt.subplot(111, projection='polar')
ax.plot(r, theta, c='purple', lw=4)
ax.grid(True)
title = latex(f)
if 'Matrix' in str(type(f)):
title = '\\left<{}\\right>'.format(latex(', '.join([latex(x) for x in f])))
if len(title) > 60:
title = '\\left<\\theta\\right>'
ax.set_title("Polar plot of ${}$".format(title), va='bottom')
data = BytesIO()
fig.savefig(data, bbox_inches='tight')
data.seek(0)
encoded_img = base64.b64encode(data.read())
return jsonify({ 'expression': str(ps), 'latex': latex(ps), 'img': 'data:image/png;base64,' + str(encoded_img)[2:-1] })
except Exception as e:
print(e)
return str(e), 400
@app.route('/graph', methods=["POST"])
def graph():
try:
print('graph: {}'.format(request.json))
ps = parse_expr(request.json['expression'], locals())
var = [str(s) for s in ps.free_symbols]
var.sort()
if len(ps.free_symbols) > 0:
raise ValueError('Graph plot requires scalars.')
if not 'Matrix' in str(type(ps)):
raise ValueError('Graph plot requires a matrix.')
if ps.shape[0] != ps.shape[1]:
raise ValueError('Graph plot requires square matrix.')
M = np.array(ps).astype('float')
G = graphplot.plot(M)
encoded_img = graphplot.to_base64_png(G.pipe())
return jsonify({ 'expression': str(ps), 'latex': latex(ps), 'img': encoded_img })
except Exception as e:
print(e)
return str(e), 400
@app.route('/transpose', methods=['POST'])
def la_transpose():
try:
print('transpose: {}'.format(request.json))
ps = parse_expr(request.json['expression'], locals())
if not 'Matrix' in str(type(ps)):
raise ValueError('Requires a matrix.')
ps = parse_expr(request.json['expression'], locals())
result = ps.T
return jsonify({ 'in': latex(ps), 'out': latex(result), 'out_expression': str(result) })
except Exception as e:
print(e)
return str(e), 400
@app.route('/inverse', methods=['POST'])
def la_inverse():
try:
print('inverse: {}'.format(request.json))
ps = parse_expr(request.json['expression'], locals())
if not 'Matrix' in str(type(ps)):
raise ValueError('Requires a matrix.')
if ps.shape[0] != ps.shape[1]:
raise ValueError('Requires a square matrix.')
ps = parse_expr(request.json['expression'], locals())
result = ps.inv()
return jsonify({ 'in': latex(ps), 'out': latex(result), 'out_expression': str(result) })
except Exception as e:
print(e)
return str(e), 400
@app.route('/det', methods=['POST'])
def la_det():
try:
print('det: {}'.format(request.json))
ps = parse_expr(request.json['expression'], locals())
if not 'Matrix' in str(type(ps)):
raise ValueError('Requires a matrix.')
if ps.shape[0] != ps.shape[1]:
raise ValueError('Requires a square matrix.')
ps = parse_expr(request.json['expression'], locals())
det = ps.det()
result = latex(det)
if len(det.free_symbols) == 1:
var = list(det.free_symbols)[0]
sln = solve(det, var)
result += ' \\implies {} = {}'.format(latex(var), latex(sln[0]))
return jsonify({ 'in': latex(ps), 'out': result, 'out_expression': str(det) })
except Exception as e:
print(e)
return str(e), 400
@app.route('/eigen', methods=['POST'])
def la_eigen():
try:
print('det: {}'.format(request.json))
ps = parse_expr(request.json['expression'], locals())
if not 'Matrix' in str(type(ps)):
raise ValueError('Requires a matrix.')
if ps.shape[0] != ps.shape[1]:
raise ValueError('Requires a square matrix.')
ps = parse_expr(request.json['expression'], locals())
return jsonify({ 'in': latex(ps), 'vectors': latex([v[2][0][0] for v in ps.eigenvects()]), 'values': latex(ps.eigenvals()) })
except Exception as e:
print(e)
return str(e), 400
@app.route('/vlength', methods=['POST'])
def la_vlength():
try:
print('vlength: {}'.format(request.json))
ps = parse_expr(request.json['expression'], locals())
if not 'Matrix' in str(type(ps)):
raise ValueError('Requires a matrix.')
if not (ps.shape[0] > 1 and ps.shape[1] == 1):
raise ValueError('Requires a MxN matrix where M > 1 and N = 1.')
product = sum([ps[i]**2 for i in range(ps.shape[0])])
result = simplify( product**0.5 )
return jsonify({ 'in': latex(ps), 'out': latex(result), 'out_expression': str(result) })
except Exception as e:
print(e)
return str(e), 400
@app.route('/graph_complement', methods=['POST'])
def graph_complement():
try:
print('graph complement: {}'.format(request.json))
ps = parse_expr(request.json['expression'], locals())
if len(ps.free_symbols) > 0:
raise ValueError('Graph plot requires scalars.')
if not 'Matrix' in str(type(ps)):
raise ValueError('Graph plot requires a matrix.')
if ps.shape[0] != ps.shape[1]:
raise ValueError('Graph plot requires square matrix.')
G = np.array(ps).astype('float')
GC = Matrix(graphplot.complement(G))
return jsonify({ 'in': latex(ps), 'out': latex( GC ), 'out_expression': str(GC) })
except Exception as e:
print(e)
return str(e), 400
@app.route('/graph_degree', methods=['POST'])
def graph_degree():
try:
print('graph degree: {}'.format(request.json))
ps = parse_expr(request.json['expression'], locals())
if len(ps.free_symbols) > 0:
raise ValueError('Graph plot requires scalars.')
if not 'Matrix' in str(type(ps)):
raise ValueError('Graph plot requires a matrix.')
if ps.shape[0] != ps.shape[1]:
raise ValueError('Graph plot requires square matrix.')
G = np.array(ps).astype('float')
GC = Matrix(np.array(graphplot.degree_matrix(G)).astype('int'))
return jsonify({ 'in': latex(ps), 'out': latex( GC ), 'out_expression': str(GC) })
except Exception as e:
print(e)
return str(e), 400
@app.route('/graph_mst', methods=['POST'])
def graph_mst():
try:
print('graph mst: {}'.format(request.json))
ps = parse_expr(request.json['expression'], locals())
if len(ps.free_symbols) > 0:
raise ValueError('Graph plot requires scalars.')
if not 'Matrix' in str(type(ps)):
raise ValueError('Graph plot requires a matrix.')
if ps.shape[0] != ps.shape[1]:
raise ValueError('Graph plot requires square matrix.')
G = ps #np.array(ps).astype('float')
mst, weight = graphplot.mst(G)
mst = Matrix(mst)
print(mst)
img = graphplot.plot_mst(np.array(G).astype('float'), mst)
encoded_img = graphplot.to_base64_png(img.pipe())
return jsonify({ 'in': latex(ps), 'out': latex( mst ), 'out_expression': str(mst), 'weight': str(float(weight)), 'image': encoded_img })
except Exception as e:
print(e)
return str(e), 400
@app.route('/graph_info', methods=['POST'])
def graph_info():
try:
print('graph info: {}'.format(request.json))
ps = parse_expr(request.json['expression'], locals())
if len(ps.free_symbols) > 0:
raise ValueError('Graph plot requires scalars.')
if not 'Matrix' in str(type(ps)):
raise ValueError('Graph plot requires a matrix.')
if ps.shape[0] != ps.shape[1]:
raise ValueError('Graph plot requires square matrix.')
G = ps #np.array(ps).astype('float')
degree_row = list(np.array(np.diagonal(graphplot.degree_matrix(G))).astype('int'))
degree_row.sort()
info = { 'degrees': latex(degree_row), 'degrees_sum': str(np.sum(degree_row)) }
return jsonify({ 'in': latex(ps), 'out': info })
except Exception as e:
print(e)
return str(e), 400
@app.route('/test')
def test():
return "This is a test endpoint!"