First here is the rhs
of your equation:
In [58]: res.rhs
Out[58]:
⎛ _______ _______⎞ ⎛ 2 _______ _______ _______ _______⎞ ⎛ _______ _______⎞
⎛ x₀⋅ζ x₀ v₀ ⎞ -ωₙ⋅t⋅⎝ζ + ╲╱ ζ - 1 ⋅╲╱ ζ + 1 ⎠ ⎜ ωₙ⋅x₀⋅ζ ωₙ⋅x₀⋅ζ⋅╲╱ ζ - 1 ⋅╲╱ ζ + 1 ωₙ⋅x₀ v₀⋅╲╱ ζ - 1 ⋅╲╱ ζ + 1 ⎟ ωₙ⋅t⋅⎝-ζ + ╲╱ ζ - 1 ⋅╲╱ ζ + 1 ⎠
⎜- ───────────────────── + ── - ────────────────────────⎟⋅ℯ + ⎜────────────── + ─────────────────────────── - ────────────── + ──────────────────────⎟⋅ℯ
⎜ _______ _______ 2 _______ _______⎟ ⎜ 2 2 2 2 ⎟
⎝ 2⋅╲╱ ζ - 1 ⋅╲╱ ζ + 1 2⋅ωₙ⋅╲╱ ζ - 1 ⋅╲╱ ζ + 1 ⎠ ⎝2⋅ωₙ⋅ζ - 2⋅ωₙ 2⋅ωₙ⋅ζ - 2⋅ωₙ 2⋅ωₙ⋅ζ - 2⋅ωₙ 2⋅ωₙ⋅ζ - 2⋅ωₙ ⎠
For the first part of your question what we can do is collect on wn
for the rhs
of the equation. This has the effect of cancelling out the wn
in the terms where they can cancel:
In [45]: res.rhs.collect(wn)
Out[45]:
⎛ _______ _______⎞ ⎛ 2 _______ _______ _______ _______⎞ ⎛ _______ _______⎞
⎛ x₀⋅ζ x₀ v₀ ⎞ -ωₙ⋅t⋅⎝ζ + ╲╱ ζ - 1 ⋅╲╱ ζ + 1 ⎠ ⎜ x₀⋅ζ x₀⋅ζ⋅╲╱ ζ - 1 ⋅╲╱ ζ + 1 x₀ v₀⋅╲╱ ζ - 1 ⋅╲╱ ζ + 1 ⎟ ωₙ⋅t⋅⎝-ζ + ╲╱ ζ - 1 ⋅╲╱ ζ + 1 ⎠
⎜- ───────────────────── + ── - ────────────────────────⎟⋅ℯ + ⎜──────── + ──────────────────────── - ──────── + ──────────────────────⎟⋅ℯ
⎜ _______ _______ 2 _______ _______⎟ ⎜ 2 2 2 ⎛ 2 ⎞ ⎟
⎝ 2⋅╲╱ ζ - 1 ⋅╲╱ ζ + 1 2⋅ωₙ⋅╲╱ ζ - 1 ⋅╲╱ ζ + 1 ⎠ ⎝2⋅ζ - 2 2⋅ζ - 2 2⋅ζ - 2 ωₙ⋅⎝2⋅ζ - 2⎠ ⎠
Now we see there are various powers that could be simplified by combining them. In general sqrt(z-1)*sqrt(z+1)
is not necessarily equal to sqrt(z**2 - 1)
but the assumption that z
is positive means that they are equal. In that case powsimp
should combine those powers:
In [46]: res.rhs.collect(wn).powsimp()
Out[46]:
⎛ ________ ________⎞
⎛ _______ _______⎞ ⎜ 2 ╱ 2 ╱ 2 ⎟ ⎛ _______ _______⎞
⎛ x₀⋅ζ x₀ v₀ ⎞ -ωₙ⋅t⋅⎝ζ + ╲╱ ζ - 1 ⋅╲╱ ζ + 1 ⎠ ⎜ x₀⋅ζ x₀⋅ζ⋅╲╱ ζ - 1 x₀ v₀⋅╲╱ ζ - 1 ⎟ ωₙ⋅t⋅⎝-ζ + ╲╱ ζ - 1 ⋅╲╱ ζ + 1 ⎠
⎜- ───────────── + ── - ────────────────⎟⋅ℯ + ⎜──────── + ──────────────── - ──────── + ──────────────⎟⋅ℯ
⎜ ________ 2 ________⎟ ⎜ 2 2 2 ⎛ 2 ⎞ ⎟
⎜ ╱ 2 ╱ 2 ⎟ ⎝2⋅ζ - 2 2⋅ζ - 2 2⋅ζ - 2 ωₙ⋅⎝2⋅ζ - 2⎠ ⎠
⎝ 2⋅╲╱ ζ - 1 2⋅ωₙ⋅╲╱ ζ - 1 ⎠
Here powsimp
didn't apply to the exponents so we need to use deep=True
to tell it to look deeper for things to combine:
In [47]: res.rhs.collect(wn).powsimp(deep=True)
Out[47]:
⎛ ________⎞ ⎛ ________ ________⎞ ⎛ ________⎞
⎜ ╱ 2 ⎟ ⎜ 2 ╱ 2 ╱ 2 ⎟ ⎜ ╱ 2 ⎟
⎛ x₀⋅ζ x₀ v₀ ⎞ -ωₙ⋅t⋅⎝ζ + ╲╱ ζ - 1 ⎠ ⎜ x₀⋅ζ x₀⋅ζ⋅╲╱ ζ - 1 x₀ v₀⋅╲╱ ζ - 1 ⎟ ωₙ⋅t⋅⎝-ζ + ╲╱ ζ - 1 ⎠
⎜- ───────────── + ── - ────────────────⎟⋅ℯ + ⎜──────── + ──────────────── - ──────── + ──────────────⎟⋅ℯ
⎜ ________ 2 ________⎟ ⎜ 2 2 2 ⎛ 2 ⎞ ⎟
⎜ ╱ 2 ╱ 2 ⎟ ⎝2⋅ζ - 2 2⋅ζ - 2 2⋅ζ - 2 ωₙ⋅⎝2⋅ζ - 2⎠ ⎠
⎝ 2⋅╲╱ ζ - 1 2⋅ωₙ⋅╲╱ ζ - 1 ⎠
There are common factors like 2
that we can extract with factor_terms
:
In [48]: factor_terms(res.rhs.collect(wn).powsimp(deep=True))
Out[48]:
⎛ ________⎞ ⎛ ________⎞
⎜ ╱ 2 ⎟ ⎛ 2 ⎞ ⎜ ╱ 2 ⎟
⎛ x₀⋅ζ v₀ ⎞ -ωₙ⋅t⋅⎝ζ + ╲╱ ζ - 1 ⎠ ⎜x₀⋅ζ x₀⋅ζ x₀ v₀ ⎟ ωₙ⋅t⋅⎝-ζ + ╲╱ ζ - 1 ⎠
⎜- ─────────── + x₀ - ──────────────⎟⋅ℯ + ⎜────── + ─────────── - ────── + ──────────────⎟⋅ℯ
⎜ ________ ________⎟ ⎜ 2 ________ 2 ________⎟
⎜ ╱ 2 ╱ 2 ⎟ ⎜ζ - 1 ╱ 2 ζ - 1 ╱ 2 ⎟
⎝ ╲╱ ζ - 1 ωₙ⋅╲╱ ζ - 1 ⎠ ⎝ ╲╱ ζ - 1 ωₙ⋅╲╱ ζ - 1 ⎠
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
2
Now we can collect on powers of z**2 - 1
to reduce the repeating subexpressions:
In [55]: factor_terms(res.rhs.collect(wn).powsimp(deep=True)).collect(z**2 - 1)
Out[55]:
⎛ v₀⎞ ⎛ ________⎞ ⎛ v₀ ⎞ ⎛ ________⎞
⎜ -x₀⋅ζ - ──⎟ ⎜ ╱ 2 ⎟ ⎜ 2 x₀⋅ζ + ── ⎟ ⎜ ╱ 2 ⎟
⎜ ωₙ⎟ -ωₙ⋅t⋅⎝ζ + ╲╱ ζ - 1 ⎠ ⎜x₀⋅ζ - x₀ ωₙ ⎟ ωₙ⋅t⋅⎝-ζ + ╲╱ ζ - 1 ⎠
⎜x₀ + ───────────⎟⋅ℯ ⎜────────── + ───────────⎟⋅ℯ
⎜ ________⎟ ⎜ 2 ________⎟
⎜ ╱ 2 ⎟ ⎜ ζ - 1 ╱ 2 ⎟
⎝ ╲╱ ζ - 1 ⎠ ⎝ ╲╱ ζ - 1 ⎠
─────────────────────────────────────────── + ───────────────────────────────────────────────────
2 2
Finally applying factor_terms
to the coefficients of that last collect allows us to cancel the one remaining awkward term and normalise the minus signs:
In [56]: factor_terms(res.rhs.collect(wn).powsimp(deep=True)).collect(z**2 - 1, factor_terms)
Out[56]:
⎛ v₀ ⎞ ⎛ ________⎞ ⎛ v₀ ⎞ ⎛ ________⎞
⎜ x₀⋅ζ + ── ⎟ ⎜ ╱ 2 ⎟ ⎜ x₀⋅ζ + ── ⎟ ⎜ ╱ 2 ⎟
⎜ ωₙ ⎟ -ωₙ⋅t⋅⎝ζ + ╲╱ ζ - 1 ⎠ ⎜ ωₙ ⎟ ωₙ⋅t⋅⎝-ζ + ╲╱ ζ - 1 ⎠
⎜x₀ - ───────────⎟⋅ℯ ⎜x₀ + ───────────⎟⋅ℯ
⎜ ________⎟ ⎜ ________⎟
⎜ ╱ 2 ⎟ ⎜ ╱ 2 ⎟
⎝ ╲╱ ζ - 1 ⎠ ⎝ ╲╱ ζ - 1 ⎠
─────────────────────────────────────────── + ───────────────────────────────────────────
2 2