-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patheg_linear_equations.py
70 lines (62 loc) · 2.25 KB
/
eg_linear_equations.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
"""
Systems of linear equations over complex numbers, reimplementing a
hand-coded parser by Dave Long.
"""
from parson import Grammar
grammar = r""" equations :end.
equations: equation ** ';'.
equation: sum '=' sum :hug.
sum: term ++ '+' :sum.
term: number ('*' variable | :'') :swap
| variable [:'1' :complex] :hug.
variable: /([a-zA-Z]+)/ .
number: ( (real ',' :'+')? real {'j'}
| real ) :join :complex.
real ~: { '-'? /\d+/ ('.' /\d*/)? } FNORD.
FNORD ~: /\s*/.
"""
parse = Grammar(grammar)(sum=lambda *terms: dict((('',0j),) + terms),
swap=lambda x,y: (y,x))
examples = """\
ht = 2j; wd = 1; sw = 0
ht = 2j; 2j*wd = ht; nw = 2j
wd = 1; sw = 0; nw = 2j
ne = 1,2j; sw = 0; 2j*wd = ht
ne = 1,2j; nw = 2j; se = 1
0.5*nw + 0.5*sw = 1j; nw = 2j; wd = 1
0.5*nw + 0.5*sw = 1j; nw = 2j; wd = -0.5j*ht
""".splitlines()
def test():
for s in examples:
print s
for lhs, rhs in parse(s):
print ' ', lhs, '\t', '=', rhs
## test()
#. ht = 2j; wd = 1; sw = 0
#. {'': 0j, 'ht': (1+0j)} = {'': 2j}
#. {'': 0j, 'wd': (1+0j)} = {'': (1+0j)}
#. {'': 0j, 'sw': (1+0j)} = {'': 0j}
#. ht = 2j; 2j*wd = ht; nw = 2j
#. {'': 0j, 'ht': (1+0j)} = {'': 2j}
#. {'': 0j, 'wd': 2j} = {'': 0j, 'ht': (1+0j)}
#. {'': 0j, 'nw': (1+0j)} = {'': 2j}
#. wd = 1; sw = 0; nw = 2j
#. {'': 0j, 'wd': (1+0j)} = {'': (1+0j)}
#. {'': 0j, 'sw': (1+0j)} = {'': 0j}
#. {'': 0j, 'nw': (1+0j)} = {'': 2j}
#. ne = 1,2j; sw = 0; 2j*wd = ht
#. {'': 0j, 'ne': (1+0j)} = {'': (1+2j)}
#. {'': 0j, 'sw': (1+0j)} = {'': 0j}
#. {'': 0j, 'wd': 2j} = {'': 0j, 'ht': (1+0j)}
#. ne = 1,2j; nw = 2j; se = 1
#. {'': 0j, 'ne': (1+0j)} = {'': (1+2j)}
#. {'': 0j, 'nw': (1+0j)} = {'': 2j}
#. {'': 0j, 'se': (1+0j)} = {'': (1+0j)}
#. 0.5*nw + 0.5*sw = 1j; nw = 2j; wd = 1
#. {'': 0j, 'sw': (0.5+0j), 'nw': (0.5+0j)} = {'': 1j}
#. {'': 0j, 'nw': (1+0j)} = {'': 2j}
#. {'': 0j, 'wd': (1+0j)} = {'': (1+0j)}
#. 0.5*nw + 0.5*sw = 1j; nw = 2j; wd = -0.5j*ht
#. {'': 0j, 'sw': (0.5+0j), 'nw': (0.5+0j)} = {'': 1j}
#. {'': 0j, 'nw': (1+0j)} = {'': 2j}
#. {'': 0j, 'wd': (1+0j)} = {'': 0j, 'ht': -0.5j}