First time here? Check out the FAQ!

Ask Your Question
0

Simplifying numerator

asked 4 years ago

StevenClontz gravatar image

updated 4 years ago

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.

Preview: (hide)

2 Answers

Sort by » oldest newest most voted
0

answered 4 years ago

Emmanuel Charpentier gravatar image

updated 4 years ago

Not 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...

Preview: (hide)
link
0

answered 4 years ago

dsejas gravatar image

Hello, @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!

Preview: (hide)
link

Comments

Thanks 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.

StevenClontz gravatar imageStevenClontz ( 4 years ago )

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 4 years ago

Seen: 808 times

Last updated: Jun 03 '20