## 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.
Due to the reasons discussed here we will be moving to python-forum.io on October 1, 2016.

This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.

micseydel

Posts: 3000
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.
Due to the reasons discussed here we will be moving to python-forum.io on October 1, 2016.

This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.

micseydel

Posts: 3000
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.
Due to the reasons discussed here we are moving to python-forum.net on October 1, 2016.

This forum will be closed. Please create an account at the new site to continue discussion.

IRC://irc.freenode.net/python-forum
Kebap

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

### Who is online

Users browsing this forum: No registered users and 4 guests