def sols_to_co(sols): sol, order = sols src = "" q = sol.keys() for var in order: src = "%s = %s\n" % (str(var), numpy_print(sol[var])) \ + src q.remove(var) reject = [] len_reject = 0 while len(q) != 0 or len(reject) != 0: if len(q) == 0: if len_reject == len(reject): break len_reject = len(reject) q = reject reject = [] var = q.pop(0) rejected = False for k in q: if var in sol[k]: reject.append(var) rejected = True break if rejected: continue src = "%s = %s\n" % (str(var), numpy_print(sol[var])) + src if len(reject) != 0: raise ValueError("Sols has circular dependency,\n%s" % \ str((reject, sol))) return compile(src, "<sols_to_co>", "exec")
def test_numpy_print(): assert(numpy_print(-mu_12 * p_1 + r_2) == "-dot(mu_12, p_1) + r_2") assert(numpy_print(delta_1 * A * p_1 + r_1) == "dot(delta_1, dot(A, p_1)) + r_1") assert(numpy_print(delta_1 * p_1 + x_1) == "dot(delta_1, p_1) + x_1") assert(numpy_print((T(r_1) * r_1) / (T(r_1) * A * p_1)) == \ "dot(dot((r_1).transpose(), r_1), 1.0 / (dot(dot((p_1).transpose(), A), r_1)))") assert(numpy_print((T(p_1) * A * r_2) / (T(p_1) * A * p_1)) == \ "dot(dot(dot((p_1).transpose(), A), r_2), 1.0 / (dot(dot((p_1).transpose(), A), p_1)))")
def test_numpy_print(): assert (numpy_print(-mu_12 * p_1 + r_2) == "-dot(mu_12, p_1) + r_2") assert (numpy_print(delta_1 * A * p_1 + r_1) == "dot(delta_1, dot(A, p_1)) + r_1") assert (numpy_print(delta_1 * p_1 + x_1) == "dot(delta_1, p_1) + x_1") assert(numpy_print((T(r_1) * r_1) / (T(r_1) * A * p_1)) == \ "dot(dot((r_1).transpose(), r_1), 1.0 / (dot(dot((p_1).transpose(), A), r_1)))") assert(numpy_print((T(p_1) * A * r_2) / (T(p_1) * A * p_1)) == \ "dot(dot(dot((p_1).transpose(), A), r_2), 1.0 / (dot(dot((p_1).transpose(), A), p_1)))")