Comment #7 on issue 2015 by smichr: Hangs attempting to solve a system of linear equations
http://code.google.com/p/sympy/issues/detail?id=2015

On my fairly modest system this takes about 16 seconds to do with the code appended below:


for k,v in soln.items():
...     print k, v.count_ops()
...     
k 43777
c 5059
n 6746
b 5067
f 5047
h 1665

========= CODE ===========

var('q, p, o, m, l, j, e, r, d, g, i, a, b, c, f, h, k, n')
eqs = [b + r/d - c/d,
       c*(1/d + 1/e + 1/g) - f/g - r/d,
       f*(1/g + 1/i + 1/j) - c/g - h/i,
       h*(1/i + 1/l + 1/m) - f/i - k/m,
       k*(1/m + 1/o + 1/p) - h/m - n/p,
       n*(1/p + 1/q) - k/p]
syms = set([b, c, f, h, k, n])

f = eqs
symbols = syms
# There might be a linear path through the system; for now we are just
# looking for a system linear in symbols, not a linearizable system
soln = None
n = len(f)
seq = list(f)
got = []
did = {}
syms = symbols
rem = dict([(i, f[i].atoms(Symbol).intersection(set(syms))) for i in range(n)])
for syms in variations(list(syms), n):
    got = []
    for s in syms:
        for i in range(n):
            if s not in rem[i]:
                continue
            sol = solve_linear(f[i], x=[s])
            if sol[0] == s:
                did[(i,s)] = sol
                got.append(sol)
                rem[i].remove(s)
                break
            else:
                rem[i].remove(s)
        else:
            break
    # once we have attempted all
    if not any(rem[i] for i in range(len(rem))):
        break

# record which equation the different variables were found in
where = {}
for kk in did:
    where.setdefault(kk[1], []).append(kk[0])
sy = where.keys()
if len(sy) == n:
    ok = False
    for sol in cartes(*[tuple(where[ki]) for ki in sy]):
        if len(set(sol)) == n:
            ok = True
            break
    if ok:
        seq = [did[kk] for kk in zip(sol, sy)]
        # forward substitution
        for i in range(len(seq)):
            for j in range(i + 1, len(seq)):
                seq[j] = seq[j][0], seq[j][1].subs(*seq[i])
        # resolution of symbols, i.e. given x = f(x, y) solve for x
        for i in range(-1, -len(seq), -1):
            seq[i] = seq[i][0], solve(seq[i][0]-seq[i][1], seq[i][0])[0]
        # back substitution
        soln = {}
        seq.reverse()
        for i in range(len(seq)):
            print seq[i]
            soln[seq[i][0]] = seq[i][1].subs(soln)


--
You received this message because you are subscribed to the Google Groups 
"sympy-issues" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sympy-issues?hl=en.

Reply via email to