# Factor fraction, where variable occurring in both, numerator and denominator, only appears once

I have an expression like $$\frac{1+a^2+2 a \cos\left(p\right)}{\left(1+b^2\right)z-1-a^2-2 \left(a+b z\right)\cos\left(p\right)}\text{.}\tag{1}$$

Is there a combination of Mathematica functions to factor it in such a way that fraction (1) only has one $\cos\left(p\right)$? For instance, this would make symbolic integration much easier.

Manipulating (1) manually, one gets $$\frac{-a}{a+b z}\left(1+z\left(-1-\frac{b}{a}-a b-b^2\right)\frac{1}{\left(1+b^2\right) z-1-a^2-2 \left(a+b z\right)\cos\left(p\right)}\right)\tag{2}$$

Mathematica agrees on that both terms (1) and (2) are equivalent:

Simplify[
(1+a^2+2 a Cos[p])/((1+b^2)z-1-a^2-2(a+b z)Cos[p])
-
(-(a/(a+b z))(1+z(-1-b/a-a b-b^2)*1/(-1-a^2+(1+b^2)z-2(a+b z)Cos[p])))
]
(*0*)


As I understand it, Simplify manipulates an expression and tries to reduce its complexity, which is given as the "length" of its TreeForm. Maybe one can force Mathematica to factor and control the TreeForm if it contains a suitable expression?

btw., Mathematica is not strong at factoring expressions of medium length. Any suggestions or links on how to factor those?

• Collect[expr, Cos[p], Simplify]? Commented Aug 18, 2015 at 12:29
• Collect[expr, Cos[p], Simplify] does not change anything, I still get expression (1). Commented Aug 18, 2015 at 13:29
• Might or might not help, but you can try to tinker with the ComplexityFunction option, in such a way as to "penalize" presence of Sin and Cos. Commented Aug 18, 2015 at 16:09
• This sounds good, I also looked up ExcludedForms and TransformationFunctions. But the hard part is on how to specify those for a specific problem, where one does not know the solution beforehand. But I'll give it a try, thank you. Commented Aug 18, 2015 at 16:14
• I was playing around with ComplexityFunction -> 2^10*Count[ FullForm[#] /. {Power[_, -1] -> Sequence[]}, _Cos, {0, Infinity}] + LeafCount[#] &, but the hard thing is that Mathematica's FullForm is different from what I need. Can one combine ComplexityFunction and Hold in such a way that eq. (2) is exactly in that form, which can be evaluated by ComplexityFunction? I think this route gets rather messy, because Factor, Expand etc. will not adhere to a form necessary for this ComlexityFunction. Commented Aug 18, 2015 at 17:11

The OP's output form:

(-(a/(a+b z))(1+z(-1-b/a-a b-b^2)*1/(-1-a^2+(1+b^2)z-2(a+b z)Cos[p])))


It is converted to LaTex in Mathematica as: $$-\frac{a \left(\frac{z \left(-a b-\frac{b}{a}-b^2-1\right)}{-a^2-2 \cos (p) (a+b z)+\left(b^2+1\right) z-1}+1\right)}{a+b z}$$

The general form of the expression (with a slight abuse of notation) $$\frac{a+b x}{c+d x},$$ where $x$ stands for $Cos(p)$.

The conversion such as is needed: $$\frac{b}{d}+\frac{a-\frac{b c}{d}}{c+d x}$$ Such conversion can be done as follows:

expr /. (p_/q_) :>
PolynomialQuotient[p, q, x] + PolynomialRemainder[p, q, x]/q


Another version closer to the case in OP: $$\frac{b \left(1+\frac{a d-b c}{b}\frac{1}{c+d x}\right)}{d}$$

To comply with OP's form we need an ad hoc Rule.

exp = (1+a^2+2 a Cos[p])/((1+b^2)z-1-a^2-2(a+b z)Cos[p]);

exp /. (x_ + y_ t_)/(v_ + w_ t_) :> (y/w) (1 +
Simplify[(x w - y v)/y,
ComplexityFunction -> (StringLength[ToString[#1]] & )] (1/(v + w t)))


Result: $$-\frac{a \left(1+\frac{z \left(-a b-\frac{b}{a}-b^2-1\right)}{-a^2-2 \cos (p) (a+b z)+\left(b^2\right) z-1}\right)}{a+b z}$$ As input form:

-((a*((z*(-(a*b)-b/a-b^2-1))/(-a^2-2*Cos[p]*(a + b*z)+(b^2 + 1)*z-1)+1))/(a+b*z))


Addenum If we want to make polynomial division applied directly to initial expression:

f = PolynomialQuotient[Numerator[#], Denominator[#], Cos[p]] +
Simplify@PolynomialRemainder[Numerator[#], Denominator[#], Cos[p]]/
Denominator[#] &;
f[exp]


that gives:

-(a/(a + b*z)) + ((a + b + a^2*b + a*b^2)*z)/((a + b*z)*(-1 - a^2 + (1 + b^2)*z - 2*(a + b*z)*Cos[p]))

In LaTeX format: $$\frac{z \left(a^2 b+a b^2+a+b\right)}{(a+b z) \left(-a^2-2 \cos (p) (a+b z)+\left(b^2+1\right) z-1\right)}-\frac{a}{a+b z}$$

• It was more a generic question on how to come from $\frac{a+b x}{c+d x}$ to $\frac{b}{d}+\frac{a d-b c}{d}\frac{1}{c+d x}$, i.e. on how to modify the ComplexityFunction that my expression is "pushed" in the intended form. So manipulating terms manually seems the way to go. Thank you. Commented Dec 21, 2015 at 10:47