Essays/Absolute and Relative Tolerance

From J Wiki
Jump to navigation Jump to search

Absolute and Relative Tolerance

Tolerant comparison in J primitives is limited to small numbers, see = Equal in dictionary. It is possible to extend it for arbitrary tolerances as described in Essays/Tolerant Comparison. However, tolerance is this sense may not be appropriate for all applications.

There are cases when magnitudes depend on scale, such as quantities of length, distance, etc., where error is proportional to magnitude which can be thought of as relative. That's the sense in which tolerance works in J primitives.

In other cases magnitudes are purely coincidental, such as positions in space or time, where error is estimated regardless of point's proximity to origin, so we can call it absolute.

In the case of time samples, JForum:programming/2007-March/005781 , to relatively express, say 4 seconds you would have to use an unwieldy relative tolerance of

  4000 % 1138000
0.00351494

So to treat each case separately, we will use separate absolute and relative tolerance comparison. While relative tolerance can be expressed in terms of absolute.

   acmp=: 1 : 'u (<&| * *@]) -'
   rcmp=: 1 : '(m * >.) acmp'

Here's how relative tolerance impacts comparison

   (1134000 1142000+/i:16) (4000 acmp) 1138000
_1 _1 _1 _1 _1 _1 _1 _1 _1 _1 _1 _1 _1 _1 _1 _1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

   (1134000 1142000+/i:16) (0.00351494 rcmp) 1138000
_1 _1 _1 _1 _1 _1 _1 _1 _1 _1 _1 _1 _1 _1 _1 _1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1

And with a second point,

   '<=>'{~(i.~~.@,)(1134000 1142000+/i:40) (4000 acmp)"_ 0/ 1138000 1146000
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<=========================================
=========================================>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<=========================================


   '<=>'{~(i.~~.@,)(1134000 1142000+/i:40) (0.00351494 rcmp)"_ 0/ 1138000 1146000
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<=========================================
=======================================================>>>>>>>>>>>>>>>>>>>>>>>>>>

<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
<<<<<<<<<<<<=====================================================================

Complex Tolerance

For relative complex tolerance while absolute tolerance remains the unchanged, in relative tolerance definition we want to make sure that selecting maximum we take the magnitude, as complex numbers cannot be compared directly. Thus universal definition for relative tolerant comparison is

   rcmp=: 1 : '(m * >.&|) acmp'

Result of tolerant complex comparison is zero for "same" points and direction of x related to y.

   ]&.((0j1&":)@+.)"0(j./~i:5) 3 acmp 0
_0.7j_0.7 _0.8j_0.6 _0.9j_0.5 _0.9j_0.4   _1j_0.2 _1   _1j0.2 _0.9j0.4 _0.9j0.5 _0.8j0.6 _0.7j0.7
_0.6j_0.8 _0.7j_0.7 _0.8j_0.6 _0.9j_0.4   _1j_0.2 _1   _1j0.2 _0.9j0.4 _0.8j0.6 _0.7j0.7 _0.6j0.8
_0.5j_0.9 _0.6j_0.8 _0.7j_0.7 _0.8j_0.6 _0.9j_0.3  0 _0.9j0.3 _0.8j0.6 _0.7j0.7 _0.6j0.8 _0.5j0.9
_0.4j_0.9 _0.4j_0.9 _0.6j_0.8         0         0  0        0        0 _0.6j0.8 _0.4j0.9 _0.4j0.9
  _0.2j_1   _0.2j_1 _0.3j_0.9         0         0  0        0        0 _0.3j0.9   _0.2j1   _0.2j1
     0j_1      0j_1         0         0         0  0        0        0        0      0j1      0j1
   0.2j_1    0.2j_1  0.3j_0.9         0         0  0        0        0  0.3j0.9    0.2j1    0.2j1
 0.4j_0.9  0.4j_0.9  0.6j_0.8         0         0  0        0        0  0.6j0.8  0.4j0.9  0.4j0.9
 0.5j_0.9  0.6j_0.8  0.7j_0.7  0.8j_0.6  0.9j_0.3  0  0.9j0.3  0.8j0.6  0.7j0.7  0.6j0.8  0.5j0.9
 0.6j_0.8  0.7j_0.7  0.8j_0.6  0.9j_0.4    1j_0.2  1    1j0.2  0.9j0.4  0.8j0.6  0.7j0.7  0.6j0.8
 0.7j_0.7  0.8j_0.6  0.9j_0.5  0.9j_0.4    1j_0.2  1    1j0.2  0.9j0.4  0.9j0.5  0.8j0.6  0.7j0.7

Name "direction" is appropriate because the magnitude of the result is boolean:

   |(j./~i:5) 3 acmp 0
1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 0 1 1 1 1 1
1 1 1 0 0 0 0 0 1 1 1
1 1 1 0 0 0 0 0 1 1 1
1 1 0 0 0 0 0 0 0 1 1
1 1 1 0 0 0 0 0 1 1 1
1 1 1 0 0 0 0 0 1 1 1
1 1 1 1 1 0 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1

Visualizing effects of tolerant comparison on complex plane better illustrates the impact of absolute and relative tolerance.

As an example we will compare complex unit grid in positive quadrant with three points of increasing magnitude.

   |4j8 8j14 16j8
8.94427 16.1245 17.8885

Absolute tolerance produces even circles with the radius of tolerance.

   2#"1'*=/2!10.'{~(++:)/(j./~i.30) |@(8 acmp"_ 0) 4j8 8j14 16j8
....000000000000000000000000!!..............................
..00000000000000000000!!!!!!!!!!1111........................
..0000000000000000!!!!!!!!!!!!!!11111111....................
..00000000000000!!!!!!!!!!!!!!!!1111111111..................
0000000000000000!!!!!!!!!!!!!!!!!!11111111..................
..000000000000!!!!!!!!!!!!!!!!!!111111111111................
..000000000000!!!!!!!!!!!!!!!!!!111111111111................
..000000000000!!!!!!!!!!!!!!!!!!111111111111................
....00000000!!!!**!!!!!!!!!!!!1111111111111111..............
....000000////**********!!!!!!11111111111111................
......////////**************1111111111111111................
....222222////**********======11111111111111................
....222222222222**============111111111111..................
..22222222222222================1111111111..................
..2222222222222222==============11111111....................
..22222222222222222222==========1111........................
2222222222222222222222222222==2222..........................
..222222222222222222222222222222............................
..222222222222222222222222222222............................
..222222222222222222222222222222............................
....22222222222222222222222222..............................
....22222222222222222222222222..............................
......2222222222222222222222................................
..........22222222222222....................................
................22..........................................
............................................................
............................................................
............................................................
............................................................
............................................................

For absolute tolerance 8 relative to middle point 8j14 we obtain relative tolerance

   8%|8j12
0.5547

Relative tolerance causes the tolerance circles to deform into a combination shape of a circle in direction of origin and an egg shape on periphery based on the relative magnitude of the point chosen to be the center of local tolerance radius.

   2#"1'*=/2!10.'{~(++:)/(j./~i.30) |@(0.55 rcmp"_ 0) 4j8 8j14 16j8
............0000000000!!!!!!!!!!11111111111111111111........
..........00000000!!!!!!!!!!!!!!!!1111111111111111111111....
........00000000!!!!!!!!!!!!!!!!!!!!1111111111111111111111..
........000000!!!!!!!!!!!!!!!!!!!!!!111111111111111111111111
........000000!!!!!!!!!!!!!!!!!!!!!!!!1111111111111111111111
........0000!!!!!!!!!!!!!!!!!!!!!!!!!!1111111111111111111111
........0000!!!!!!!!!!!!!!!!!!!!!!!!!!1111111111111111111111
..........//************!!!!!!!!!!!!!!1111111111111111111111
......2222//****************!!!!!!!!!!1111111111111111111111
....22222222******************!!!!!!111111111111111111111111
..2222222222************************111111111111111111111111
222222222222==********************====1111111111111111111111
22222222222222====************============111111111111111111
22222222222222==============================1111111111111111
2222222222222222==============================11111111111111
2222222222222222================================111111111111
222222222222222222==============================111111111111
222222222222222222================================1111111111
22222222222222222222==============================1111111111
22222222222222222222================================11111111
2222222222222222222222==============================11111111
222222222222222222222222============================111111..
22222222222222222222222222==========================1111....
222222222222222222222222222222======================........
222222222222222222222222222222222222==========222222........
2222222222222222222222222222222222222222222222222222........
2222222222222222222222222222222222222222222222222222........
22222222222222222222222222222222222222222222222222..........
22222222222222222222222222222222222222222222222222..........
22222222222222222222222222222222222222222222222222..........


Contributed by Oleg Kobchenko