If you want to pull out common terms you should use collect(). factor() only performs complete factorizations (meaning the result should be a product of polynomials).
Aaron Meurer On Fri, Jan 22, 2016 at 11:21 AM, Alexander Lindsay <[email protected]> wrote: > I have this short code that solves for an expression I need to input into > a finite element code. The simpler the expression the better. The code is > here: > > from sympy import * > > r_e, a, mu_el, E, n, epsilon, n_gamma, epsilon_gamma, v_th, Gamma_p, mu_e, > gamma_p, n_alpha, epsilon_alpha, Gamma_e, mu_i, b, n_i, v_ith, bob, e, A, > R, V_bat, u = symbols('r_e a mu_el E n epsilon n_gamma epsilon_gamma v_th > Gamma_p mu_e gamma_p n_alpha epsilon_alpha Gamma_e mu_i b n_i v_ith bob e A > R V_bat u') > > Gamma_e = (1 - r_e) / (1 + r_e) * (-(2 * a -1) * mu_e * E * n_alpha + > v_th * 1 / 2 * n_alpha) - (1 - a) * gamma_p * Gamma_p > Gamma_e = Gamma_e.subs(n_alpha, n - n_gamma) > Gamma_e = Gamma_e.subs(Gamma_p, (1 - r_e) / (1 + r_e) * ((2 * b - 1) * > mu_i * E * n_i + v_ith * 1 / 2 * n_i)) > Gamma_p = (1 - r_e) / (1 + r_e) * ((2 * b - 1) * mu_i * E * n_i + v_ith * > 1 / 2 * n_i) > J_tot = simplify(e * Gamma_p - e * Gamma_e) > soln = solve(V_bat + u - J_tot * A * R, E, dict=True) > Gamma_eps = simplify(soln[0][E]) > factored = Gamma_eps.factor() > > print Gamma_eps > # Output: (-A*R*a*e*gamma_p*n_i*r_e*v_ith + A*R*a*e*gamma_p*n_i*v_ith + > A*R*e*gamma_p*n_i*r_e*v_ith - A*R*e*gamma_p*n_i*v_ith - A*R*e*n*r_e*v_th + > A*R*e*n*v_th + A*R*e*n_gamma*r_e*v_th - A*R*e*n_gamma*v_th + > A*R*e*n_i*r_e*v_ith - A*R*e*n_i*v_ith + 2*V_bat*r_e + 2*V_bat + 2*r_e*u + > 2*u)/(2*A*R*e*(2*a*b*gamma_p*mu_i*n_i*r_e - 2*a*b*gamma_p*mu_i*n_i - > a*gamma_p*mu_i*n_i*r_e + a*gamma_p*mu_i*n_i - 2*a*mu_e*n*r_e + 2*a*mu_e*n + > 2*a*mu_e*n_gamma*r_e - 2*a*mu_e*n_gamma - 2*b*gamma_p*mu_i*n_i*r_e + > 2*b*gamma_p*mu_i*n_i - 2*b*mu_i*n_i*r_e + 2*b*mu_i*n_i + > gamma_p*mu_i*n_i*r_e - gamma_p*mu_i*n_i + mu_e*n*r_e - mu_e*n - > mu_e*n_gamma*r_e + mu_e*n_gamma + mu_i*n_i*r_e - mu_i*n_i)) > print factored > # Output: (-A*R*a*e*gamma_p*n_i*r_e*v_ith + A*R*a*e*gamma_p*n_i*v_ith + > A*R*e*gamma_p*n_i*r_e*v_ith - A*R*e*gamma_p*n_i*v_ith - A*R*e*n*r_e*v_th + > A*R*e*n*v_th + A*R*e*n_gamma*r_e*v_th - A*R*e*n_gamma*v_th + > A*R*e*n_i*r_e*v_ith - A*R*e*n_i*v_ith + 2*V_bat*r_e + 2*V_bat + 2*r_e*u + > 2*u)/(2*A*R*e*(r_e - 1)*(2*a*b*gamma_p*mu_i*n_i - a*gamma_p*mu_i*n_i - > 2*a*mu_e*n + 2*a*mu_e*n_gamma - 2*b*gamma_p*mu_i*n_i - 2*b*mu_i*n_i + > gamma_p*mu_i*n_i + mu_e*n - mu_e*n_gamma + mu_i*n_i)) > > > As it stands now Gamma_eps is a very long expression with a lot of terms > that should be readily factorable. However, a naive application of the > factor() method at the end does one factorization in the denominator and > that's it. Clearly I'm a noobie here and I'm going to do some research on > my own to see whether I can make this work, but do others have some quick > suggestions for how I might reduce this expression, say with minimizing the > number of operations as the goal criterion? > > Alex > > -- > You received this message because you are subscribed to the Google Groups > "sympy" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To post to this group, send email to [email protected]. > Visit this group at https://groups.google.com/group/sympy. > To view this discussion on the web visit > https://groups.google.com/d/msgid/sympy/c9eb95f7-7984-4246-99ce-9c8b5129e47d%40googlegroups.com > <https://groups.google.com/d/msgid/sympy/c9eb95f7-7984-4246-99ce-9c8b5129e47d%40googlegroups.com?utm_medium=email&utm_source=footer> > . > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "sympy" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at https://groups.google.com/group/sympy. To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CAKgW%3D6%2BZm2RVM1Vj2DH0iaWC-RrpxTPzKOz1wF3%3DGn82ZePtCg%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
