Ask Your Question
0

Listing fractions

asked 2018-02-01 22:03:13 -0600

anonymous user

Anonymous

Hi, I would like to request help in listing rational numbers. For motivation, the command

[x for x in IntegerRange(1,10)]

outputs the integers from 1 through 9. Is there a similar command for listing out rational numbers a/b with a and b in the range (x1,y1) and (x2,y2), respectively.

For instance, if a is in range (1,3) and b is in (4,6), I want to return

[1/4, 1/5, 1/2, 2/5]

On a related note, how can one impose a gcd(x,y)==1 in the following list?

[(x,y) for x in IntegerRange(1,10) for y in IntegerRange(1,10)]

edit retag flag offensive close merge delete

3 answers

Sort by ยป oldest newest most voted
1

answered 2018-02-01 23:26:34 -0600

Emmanuel Charpentier gravatar image

Either I do not understand your specification or your example is incomplete. The one-liner :

sage: [v for v in sorted(set([t/u for t in (1..3) for u in (4..6)]))]
[1/6, 1/5, 1/4, 1/3, 2/5, 1/2, 3/5, 3/4]

returns elements not in your answer, but I do not understand why you exclude them.

On a related note, how can one impose a gcd(x,y)==1 in the following list?

Sage does that automatically :

sage: 2/4
1/2

To ensure unicity, use a set ; the result may be easier to read by using its sorted version (see the above one-liner).

HTH,

edit flag offensive delete link more
0

answered 2018-02-03 13:38:43 -0600

tmonteil gravatar image

Tom complement previous answers, you might by interested by the method:

sage: QQ.range_by_height?
edit flag offensive delete link more
0

answered 2018-02-03 08:28:45 -0600

dan_fulea gravatar image

A first trap is the following one, i somehow guess, that this was the reason for the question:

sage: for a in range(1,3):
....:     for b in range(4,6):
....:         print a, b, a/b
....:         
1 4 0
1 5 0
2 4 0
2 5 0

The reason for this is the fact that range builds a list of python integers, so the division is the plain division of integers, it delivers also an integer as result, this is the floor of the mathematically considered fraction $a/b$. To see the types:

sage: [ (a, type(a)) for a in range(1,3) ]
[(1, <type 'int'>), (2, <type 'int'>)]

So in order to get rational numbers, we have to tell sage to use sage specific objects. To get a "better list" suited for an immediate division, we may use instead one of the following solutions:

  • convert each a in range(1,3) to a sage integer, or to a sage rational number, i.e. use [ ZZ(a) for a in range(1,3) ] and/or [ QQ(a) for a in range(1,3) ] ,
  • use the sage interval [1..2] ,
  • use the sage range IntegerRange(1,3) .

And here are the lines of code doing the job the one or the other way:

sage: [ ZZ(a)/ZZ(b) for a in range(1,3) for b in range(4,6) ]
[1/4, 1/5, 1/2, 2/5]
sage: [ QQ(a)/QQ(b) for a in range(1,3) for b in range(4,6) ]
[1/4, 1/5, 1/2, 2/5]
sage: [ a/b for a in [1..2] for b in [4,5] ]
[1/4, 1/5, 1/2, 2/5]
sage: [ ZZ(a)/ZZ(b) for a in IntegerRange(1,3) for b in IntegerRange(4,6) ]
[1/4, 1/5, 1/2, 2/5]
sage: [ QQ(a)/QQ(b) for a in IntegerRange(1,3) for b in IntegerRange(4,6) ]
[1/4, 1/5, 1/2, 2/5]

The related answer. List comprehension also accepts a boolean condition (with and and or if more complicated). In our case:

[(x,y) for x in IntegerRange(1,10) for y in IntegerRange(1,10) if gcd(x,y)==1]

One may construct the last list also by generating all fractions, then passing to the list of the set of the list:

sage: L1 = [ x/y for x in IntegerRange(1,10) for y in IntegerRange(1,10) if gcd(x,y)==1]
sage: L2 = [ x/y for x in IntegerRange(1,10) for y in IntegerRange(1,10) ]
sage: L1.sort()
sage: L2 = list(set(L2))
sage: L2.sort()
sage: L1 == L2
True
sage: len(L1), len(L2)
(55, 55)

The one (or the other way) will list - after a small adaptation to two different ranges - all irreducible (resp. all possibly reducible) fractions with numerator and denominator in given ranges. (If the ranges start from the one, the two lists coincide.)

edit flag offensive delete link more

Your Answer

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

Add Answer

Question Tools

1 follower

Stats

Asked: 2018-02-01 22:03:13 -0600

Seen: 64 times

Last updated: Feb 03