Fifty Shades of J/Chapter 15

From J Wiki
Jump to: navigation, search

Table of Contents ... Glossary ... Previous Chapter ... Next Chapter

Cancel, cancel little fraction

Principal Topics

x: (extended precision) , numerical data types, conversion rules, rational approximations.

Rational Numbers in J

There are various forms of number representation in J, as well as rules and conditions for converting between them. To start with, in complex arithmetic +. transforms a complex number in to a list of its components

   +.2j3
2 3

An analogous problem is that of transforming a rational number into a list {numerator,denominator} following any possible cancellation. This is provided by the primitive verb x: with left argument 2

   2 x: 3r5 6r10 14r7
3 5
3 5
2 1

but notice

   %/2 x:8r6
4r3

does not convert 4%3 into decimal format as you might expect. Instead say either of the following

   _1 x:8r6
1.33333
   x:^:_1 (8r6)
1.33333

There are conversion rules which apply in calculations in which the six different data types (Boolean, Integer, Extended Integer, Rational, Floating Point and Complex) are mixed. It is not usually necessary to be explicitly aware of the thirty possible conversions - the main ones are

(1) the presence of just one decimal notated number in a calculation is enough to force decimal notation for the result

   1r1 2r1 3r1 + 3r5 3r5 14r7
8r5 13r5 5
   1r1 2r1 3r1 + 3r5 0.6 14r7
1.6 2.6 5

(2) mixing j and r results in conversion to floating point

   1r9j2r3        NB. (1/9) + (2/3)i
0.111111j0.666667

Also rational notation cannot be used to perform rational complex division

   1j9r2j3        NB. not (1+9i) / (2+3i)
|ill-formed number

(3) where present, floating point and complex are dominant. Rational is also “strong” except as under (1), Boolean and integer are “weak”.

   2+3j2+7r2
8.5j2

The full conversion table is:

Boolean Integer Extended Rational Float Complex
Boolean Boolean Integer Extended Rational Float Complex
Integer Integer Integer Extended Rational Float Complex
Extended Extended Extended Extended Rational Float Complex
Rational Rational Rational Rational Rational Float Complex
Float Float Float Float Float Float Complex
Complex Complex Complex Complex Complex Complex Complex

Pure rational calculations perform cancellation automatically

   ]t=.+/\1r3 1r4 1r5
1r3 7r12 47r60
   t*/t
   1r9    7r36    47r180
  7r36  49r144   329r720
47r180 329r720 2209r3600
   t +/ .*t
213r200
   1r9 + 49r144 + 2209r3600
213r200

Interestingly cancellation can also be done using +. in its dyadic role of greatest common denominator

   dac=.%@(1&+.)

gives the denominator after cancelling, and multiplying this by the number itself gives the numerator, so ((*dac), dac) does the job!

   ((*dac), dac)8r6
4 3

Again subsequent calculations do not convert to decimal format unless such a number appears

   %/((*dac), dac)8r6
4r3
   1.6* %/((*dac), dac)8r6
2.13333

To extend to objects with rank, say

   ((*dac),"0 dac)8r6 12r2 34r51
4 3
6 1
2 3

Some ingenious alternatives have been discussed of which (,&1)%(+.&1) proposed by Roger Hui avoids the double calculation of dac and has a certain appealing symmetry, extending to ,"0&1 % +.&1 to deal with general rank

2 x:2 3$8r6 12r2 34r51 
4 3 
6 1 
2 3

4 3 
6 1 
2 3

To obtain close rational approximations to irrational numbers such as π and e, say

   x: o.1
1285290289249r409120605684    NB. 1,285,290,289,249 / 409,120,605,684
   _1 x: x: o.1
3.14159
   _1 x: x: ^1
2.71828

Code Summary

dac=.%@(1&+.)

Script

File:Fsojc15.ijs