## Numerator & Denominator

This is the place for queries that don't fit in any of the other categories.

### Numerator & Denominator

How can I go about grabbing the numerator and the denominator separately from a fraction.

I want to have an input which will be a fraction with polynomials in the numerator and in the denominator. I would like to factor both of the num and denom and then place them back into the fraction and have it print to the screen.

Getting into a fraction is easy, I can't seem to figure out how to take them apart.

Thanks for any help I am fairly new to Python.
Peglam

Posts: 3
Joined: Wed Apr 24, 2013 8:45 pm

### Re: Numerator & Denominator

We're happy to help, but we'd like to see your attempt, as well as the exact input and output you want.

You should also look into the fractions module.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.

micseydel

Posts: 1931
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

### Re: Numerator & Denominator

Thanks,

Well the input will be a transfer function G, where G = control.transferfunction([Num],[Den]). The Num or Den can change from 1st order to nth order.

I looked at fraction module but I can't find a way to use it to return the denominator or numerator of G.

One example of what I would like to do it this.

input
g = control.transferfuntion([1,4,4],[1,11,39,45]

print g returns (s^2 +4s+4) / ( s^3 + 11*s^2 + 39s +45)

For output
I want it to look like this g = (s+2)(s+2) / (s+3)(s+3)(s+5)
Peglam

Posts: 3
Joined: Wed Apr 24, 2013 8:45 pm

### Re: Numerator & Denominator

You should read this. It's much easier to read your post if you use code tags to denote any Python from math or anything else. And is g supposed to return a string? A special object?

What you're doing isn't just about fractions, it's about working with polynomials by using vectors to describe the coefficients. Numpy or scipy might have functionality for that, although the Fraction class in fractions is for straight numbers (although you can indeed get a numerator and denominator out of a Fraction object). Also, you're not just working with polynomials, it looks like you even want to factor them. I couldn't find factoring functions with a very quick Google search. There might be other modules for it, or you might me able to add the functionality on top of numpy.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.

micseydel

Posts: 1931
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

### Re: Numerator & Denominator

I would suggest you try to start with some more simpler problems, then grow from there. That is, if you haven't done so, yet, just forgot telling us about it.

For example how do you go from (s^2+4s+4) to (s+2)(s+2)? There is a formula for this, or you can test a few numbers. Worry about the fractions later.

Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap

Posts: 527
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe