home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / e / e032 / 3.ddi / FILES / NUMBERTH.PAK / RATIONAL.M < prev    next >
Encoding:
Text File  |  1992-07-29  |  2.2 KB  |  87 lines

  1.  
  2.  
  3. (* Copyright 1990 Wolfram Research, Inc. *)
  4.  
  5. (*:Version: Mathematica 2.0 *)
  6.  
  7. (*:Name: NumberTheory`Rationalize` *)
  8.  
  9. (*:Author:
  10.     Daniel R. Grayson
  11. *)
  12.  
  13. (*:Keywords:
  14.     rationalize, real number
  15. *)
  16.  
  17. (*:Requirements: none. *)
  18.  
  19. (*:Warnings:
  20.  
  21. *)
  22.  
  23. (*:Sources:
  24.  
  25. *)
  26.  
  27. (*:Summary:
  28.     This package complements the capability of the built-in function
  29.     Rationalize[].
  30. *)
  31.  
  32. BeginPackage["NumberTheory`Rationalize`"]
  33.  
  34. ProjectiveRationalize::usage = "ProjectiveRationalize[{x0,x1,...,xn}] returns
  35. a list of integers {s0,s1,...,sn} so that the ratios si/sj approximate
  36. the ratios xi/xj well for each pair 0 <= i,j <= n.
  37. ProjectiveRationalize[{x0,x1,...,xn}, prec] allows a tolerance of 10^-prec.
  38. The numbers xi must be real."
  39.  
  40. AffineRationalize::usage = "AffineRationalize[{x1,...,xn}] returns rational
  41. numbers {u1,...,un} so that ui approximates xi well for each
  42. 1 <= i <= n, and so the least common denominator of the ui's is small.
  43. AffineRationalize[{x0,x1,...,xn}, prec] allows a tolerance of 10^-prec.
  44. The numbers xi must be real."
  45.  
  46. Begin["NumberTheory`Rationalize`Private`"]
  47.  
  48. ProjectiveRationalize[x:{__Real}, prec_Integer:0] :=
  49.     Module[{scale, res, len = Length[x]},
  50.     scale = If[prec===0,
  51.             1,  (* tolerance depends on Accuracy[x] *)
  52.             (* else *)
  53.             scale = Max[Accuracy[x] - prec len/(len-1), 0];
  54.             scale = 10^Round[scale]
  55.         ];
  56.     res = Append[IdentityMatrix[len] scale, Round[x 10^Accuracy[x]]];
  57.     res = Drop[LatticeReduce[Transpose[res]], -1];
  58.     res = Minors[Drop[Transpose[res], -1], len-1];
  59.     res = Reverse[Flatten[res]] (-1)^Range[len] / scale^(len-1);
  60.     If[res[[1]] < 0, -res, res]
  61.     ]
  62.  
  63. AffineRationalize[{x__Real}, prec_Integer:0] := 
  64.     Module[{res},
  65.     res = ProjectiveRationalize[{N[1, Accuracy[{x}]], x}, prec];
  66.     res = Drop[res, 1]/res[[1]];
  67.     If[Sign[N[{x}[[1]]]] != Sign[res[[1]]], -res, res]
  68.     ]
  69.  
  70. End[]   (* NumberTheory`Rationalize`Private` *)
  71.  
  72. Protect[ ProjectiveRationalize, AffineRationalize ]
  73.  
  74. EndPackage[]   (* NumberTheory`Rationalize` *)
  75.  
  76. (*:Limitations: none known. *)
  77.  
  78.  
  79. (*:Examples:
  80.  
  81. ProjectiveRationalize[ N[{647367, 475748, 978694, 536475}/Pi] ]
  82.  
  83. AffineRationalize[ N[{647367, 475748, 978694} / 536475] ]
  84.  
  85. *)
  86.  
  87.