ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 03 Jun 2020 21:08:56 +0200Simplifying numeratorhttps://ask.sagemath.org/question/51726/simplifying-numerator/I'm writing a script to create quotient rule exercises, but I cannot coerce the fraction to simplify as desired. In particular, I'd like `latex(sol)` to look like the following:
```
\frac{2x+4}{(x^2+1)^2}
```
But evaluating
```
(2*x+4)/(x^2+1)^2
```
always seems to factor the numerator to get
```
2*(x + 2)/(x^2 + 1)^2
```
and `simplify_full()` seems to only expand the denominator, not the numerator:
```
2*(x + 2)/(x^4 + 2*x^2 + 1)
```
---
For clarity, I think the general problem is this (perhaps?) surprising phenomenon: `latex(f/g)` doesn't seem to respect whether the expression `f` is factored or not.Wed, 03 Jun 2020 18:59:28 +0200https://ask.sagemath.org/question/51726/simplifying-numerator/Answer by Emmanuel Charpentier for <p>I'm writing a script to create quotient rule exercises, but I cannot coerce the fraction to simplify as desired. In particular, I'd like <code>latex(sol)</code> to look like the following:</p>
<p><code>
\frac{2x+4}{(x^2+1)^2}
</code></p>
<p>But evaluating</p>
<p><code>
(2*x+4)/(x^2+1)^2
</code></p>
<p>always seems to factor the numerator to get</p>
<p><code>
2*(x + 2)/(x^2 + 1)^2
</code></p>
<p>and <code>simplify_full()</code> seems to only expand the denominator, not the numerator:</p>
<p><code>
2*(x + 2)/(x^4 + 2*x^2 + 1)
</code></p>
<hr>
<p>For clarity, I think the general problem is this (perhaps?) surprising phenomenon: <code>latex(f/g)</code> doesn't seem to respect whether the expression <code>f</code> is factored or not.</p>
https://ask.sagemath.org/question/51726/simplifying-numerator/?answer=51732#post-id-51732Not strictly true :
sage: ((2*x+4)/(x^2+1)^2).expand()
2*x/(x^4 + 2*x^2 + 1) + 4/(x^4 + 2*x^2 + 1)
But :
sage: ((2*x+4)/(x^2+1)^2).expand().factor()
2*(x + 2)/(x^2 + 1)^2
sage: ((2*x+4)/(x^2+1)^2).expand().combine()
2*(x + 2)/(x^4 + 2*x^2 + 1)
Note that :
sage: ((2*x+4)/(x^2+1)^2).expand().simplify()
2*x/(x^4 + 2*x^2 + 1) + 4/(x^4 + 2*x^2 + 1)
But that :
sage: ((2*x+4)/(x^2+1)^2).expand().simplify_full()
2*(x + 2)/(x^4 + 2*x^2 + 1)
Sorry.
This is general :
sage: var("a")
a
sage: a*(2*x+4)
2*a*(x + 2)
PS : I fail to see the point...
Wed, 03 Jun 2020 19:37:40 +0200https://ask.sagemath.org/question/51726/simplifying-numerator/?answer=51732#post-id-51732Answer by dsejas for <p>I'm writing a script to create quotient rule exercises, but I cannot coerce the fraction to simplify as desired. In particular, I'd like <code>latex(sol)</code> to look like the following:</p>
<p><code>
\frac{2x+4}{(x^2+1)^2}
</code></p>
<p>But evaluating</p>
<p><code>
(2*x+4)/(x^2+1)^2
</code></p>
<p>always seems to factor the numerator to get</p>
<p><code>
2*(x + 2)/(x^2 + 1)^2
</code></p>
<p>and <code>simplify_full()</code> seems to only expand the denominator, not the numerator:</p>
<p><code>
2*(x + 2)/(x^4 + 2*x^2 + 1)
</code></p>
<hr>
<p>For clarity, I think the general problem is this (perhaps?) surprising phenomenon: <code>latex(f/g)</code> doesn't seem to respect whether the expression <code>f</code> is factored or not.</p>
https://ask.sagemath.org/question/51726/simplifying-numerator/?answer=51737#post-id-51737Hello, @StevenClontz! I don't know if this solves the general case of your question, but it does solve the particular example your present. Write the following:
def latex_frac(frac):
if frac == 0: return '0'
num = frac.numerator()
den = frac.denominator()
if den == 1: return str(num)
return r'\frac{' + str(num) + '}{' + str(den) + '}'
You can then call this functions like this:
latex_frac((2*x+4)/(x^2+1)^2)
which will give the result you want.
I hope this helps!Wed, 03 Jun 2020 20:13:34 +0200https://ask.sagemath.org/question/51726/simplifying-numerator/?answer=51737#post-id-51737Comment by StevenClontz for <p>Hello, <a href="/users/25498/stevenclontz/">@StevenClontz</a>! I don't know if this solves the general case of your question, but it does solve the particular example your present. Write the following:</p>
<pre><code>def latex_frac(frac):
if frac == 0: return '0'
num = frac.numerator()
den = frac.denominator()
if den == 1: return str(num)
return r'\frac{' + str(num) + '}{' + str(den) + '}'
</code></pre>
<p>You can then call this functions like this:</p>
<pre><code>latex_frac((2*x+4)/(x^2+1)^2)
</code></pre>
<p>which will give the result you want.</p>
<p>I hope this helps!</p>
https://ask.sagemath.org/question/51726/simplifying-numerator/?comment=51740#post-id-51740Thanks for the reply! I was trying to use the sage standard library to solve this, but you're correct, it'd be pretty easy to write a custom function to do what I need in this narrow case. I'm editing the original question to make this clearer.Wed, 03 Jun 2020 21:08:56 +0200https://ask.sagemath.org/question/51726/simplifying-numerator/?comment=51740#post-id-51740