-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutil.py
40 lines (31 loc) · 1.02 KB
/
util.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
import numpy as np
import sympy as sy
from sympy.printing import pretty as sympy_pretty
from fractions import Fraction
def p(A, name: str = None, transpose=False):
"""pretty print numpy matrix"""
A = sy.Matrix(np.array(A, ndmin=2)) # force 2d array
if transpose:
A = sy.Transpose(A)
if name:
return sympy_pretty(sy.Eq(sy.Symbol(name), A, evaluate=False))
else:
return sympy_pretty(A)
def pT(A, name: str = None):
"""transpose and then pretty print numpy matrix"""
A = sy.Matrix(np.array(A, ndmin=2)) # force 2d array
A = sy.Transpose(A)
if name:
return sympy_pretty(sy.Eq(sy.Symbol(name), A, evaluate=False))
else:
return sympy_pretty(A)
def pp(A, name: str = None):
print(p(A, name))
def ppT(A, name: str = None):
print(p(A, name, transpose=True))
def fraction_to_sum(f: Fraction) -> str:
res = str(f.numerator//f.denominator)
mod = f.numerator % f.denominator
if mod != 0:
res += f" + {mod}/{f.denominator}"
return res