(***********************************************************************

                    Mathematica-Compatible Notebook

This notebook can be used on any computer system with Mathematica 4.0,
MathReader 4.0, or any compatible application. The data for the notebook 
starts with the line containing stars above.

To get the notebook into a Mathematica-compatible application, do one of 
the following:

* Save the data starting with the line of stars above into a file
  with a name ending in .nb, then open the file inside the application;

* Copy the data starting with the line of stars above to the
  clipboard, then use the Paste menu command inside the application.

Data for notebooks contains only printable 7-bit ASCII and can be
sent directly in email or through ftp in text mode.  Newlines can be
CR, LF or CRLF (Unix, Macintosh or MS-DOS style).

NOTE: If you modify the data for this notebook not in a Mathematica-
compatible application, you must delete the line below containing the 
word CacheID, otherwise Mathematica-compatible applications may try to 
use invalid cache data.

For more information on notebooks and Mathematica-compatible 
applications, contact Wolfram Research:
  web: http://www.wolfram.com
  email: info@wolfram.com
  phone: +1-217-398-0700 (U.S.)

Notebook reader applications are available free of charge from 
Wolfram Research.
***********************************************************************)

(*CacheID: 232*)


(*NotebookFileLineBreakTest
NotebookFileLineBreakTest*)
(*NotebookOptionsPosition[     12155,        337]*)
(*NotebookOutlinePosition[     15371,        439]*)
(*  CellTagsIndexPosition[     15091,        424]*)
(*WindowFrame->Normal*)



Notebook[{
Cell[TextData[{
  "NumberTheory",
  StyleBox["`", "MB"],
  "Rationalize",
  StyleBox["`", "MB"]
}], "Subsection",
  CellTags->{"S5.72.1", "1.1"}],

Cell[TextData[{
  "It is frequently useful to approximate arbitrary real numbers with a \
nearby rational. The built\[Hyphen]in function ",
  StyleBox["Rationalize", "MR"],
  " gives a rational approximation to a single real number. \nIn certain \
situations, such as the computation of gear ratios or the setting of musical \
scales, one wants to get a list of rational numbers that approximates a given \
list of real numbers. You could simply apply ",
  StyleBox["Rationalize", "MR"],
  " to each element in the list, but it is very likely that the elements in \
the result would not be related to each other in a simple way. To get a \
simultaneous rational approximation that preserves the relationships among \
the given numbers, you should use ",
  StyleBox["ProjectiveRationalize", "MR"],
  " or ",
  StyleBox["AffineRationalize", "MR"],
  ". "
}], "Text",
  CellTags->{"S5.72.1", "1.2"}],

Cell[BoxData[
    FormBox[
      StyleBox[
        FrameBox[
          StyleBox[GridBox[{
                {
                  
                  RowBox[{"\<\"\\!\\(\\*StyleBox[\\\"\\\\\\\"\
ProjectiveRationalize[\\\\\\\"\\\", \\\"MR\\\"]\\)\"\>", 
                    StyleBox["{",
                      "InlineFormula"], 
                    StyleBox[
                      
                      SubscriptBox["\<\"\\!\\(\\*StyleBox[\\\"\\\\\\\"x\\\\\\\
\"\\\", \\\"TI\\\"]\\)\"\>", "0"],
                      
                      "InlineFormula"], \
"\<\"\\!\\(\\*StyleBox[\\\"\\\\\\\",\\\\\\\"\\\", \\\"MR\\\"]\\) \"\>", 
                    StyleBox[
                      
                      SubscriptBox["\<\"\\!\\(\\*StyleBox[\\\"\\\\\\\"x\\\\\\\
\"\\\", \\\"TI\\\"]\\)\"\>", "1"],
                      
                      "InlineFormula"], \
"\<\"\\!\\(\\*StyleBox[\\\"\\\\\\\",\\\\\\\"\\\", \\\"MR\\\"]\\) \[Ellipsis] \
\\!\\(\\*StyleBox[\\\"\\\\\\\",\\\\\\\"\\\", \\\"MR\\\"]\\) \"\>", 
                    StyleBox[
                      
                      SubscriptBox["\<\"\\!\\(\\*StyleBox[\\\"\\\\\\\"x\\\\\\\
\"\\\", \\\"TI\\\"]\\)\"\>", "n"],
                      "InlineFormula"], 
                    StyleBox["}",
                      
                      "InlineFormula"], \
"\<\"\\!\\(\\*StyleBox[\\\"\\\\\\\"]\\\\\\\"\\\", \\\"MR\\\"]\\) \"\>"}], 
                  
                  RowBox[{"\<\"give a list of integers whose ratios well \
approximate the corresponding ratios of the real numbers \"\>", 
                    StyleBox[
                      
                      SubscriptBox["\<\"\\!\\(\\*StyleBox[\\\"\\\\\\\"x\\\\\\\
\"\\\", \\\"TI\\\"]\\)\"\>", "i"],
                      "InlineFormula"]}]},
                {
                  
                  RowBox[{"\<\"\\!\\(\\*StyleBox[\\\"\\\\\\\"\
ProjectiveRationalize[\\\\\\\"\\\", \\\"MR\\\"]\\)\"\>", 
                    StyleBox["{",
                      "InlineFormula"], 
                    StyleBox[
                      
                      SubscriptBox["\<\"\\!\\(\\*StyleBox[\\\"\\\\\\\"x\\\\\\\
\"\\\", \\\"TI\\\"]\\)\"\>", "0"],
                      
                      "InlineFormula"], \
"\<\"\\!\\(\\*StyleBox[\\\"\\\\\\\",\\\\\\\"\\\", \\\"MR\\\"]\\) \"\>", 
                    StyleBox[
                      
                      SubscriptBox["\<\"\\!\\(\\*StyleBox[\\\"\\\\\\\"x\\\\\\\
\"\\\", \\\"TI\\\"]\\)\"\>", "1"],
                      
                      "InlineFormula"], \
"\<\"\\!\\(\\*StyleBox[\\\"\\\\\\\",\\\\\\\"\\\", \\\"MR\\\"]\\) \[Ellipsis] \
\\!\\(\\*StyleBox[\\\"\\\\\\\",\\\\\\\"\\\", \\\"MR\\\"]\\) \"\>", 
                    StyleBox[
                      
                      SubscriptBox["\<\"\\!\\(\\*StyleBox[\\\"\\\\\\\"x\\\\\\\
\"\\\", \\\"TI\\\"]\\)\"\>", "n"],
                      "InlineFormula"], 
                    StyleBox["}",
                      
                      "InlineFormula"], \
"\<\"\\!\\(\\*StyleBox[\\\"\\\\\\\",\\\\\\\"\\\", \\\"MR\\\"]\\) \
\\!\\(\\*StyleBox[\\\"\\\\\\\"prec\\\\\\\"\\\", \
\\\"TI\\\"]\\)\\!\\(\\*StyleBox[\\\"\\\\\\\"]\\\\\\\"\\\", \\\"MR\\\"]\\) \
\"\>"}], 
                  RowBox[{"\<\"use a tolerance of \"\>", 
                    StyleBox[\(10\^\(-p  r  e  c\)\),
                      "InlineFormula"], "\<\" in the approximation\"\>"}]},
                {
                  
                  RowBox[{"\<\"\\!\\(\\*StyleBox[\\\"\\\\\\\"\
AffineRationalize[\\\\\\\"\\\", \\\"MR\\\"]\\)\"\>", 
                    StyleBox["{",
                      "InlineFormula"], 
                    StyleBox[
                      
                      SubscriptBox["\<\"\\!\\(\\*StyleBox[\\\"\\\\\\\"x\\\\\\\
\"\\\", \\\"TI\\\"]\\)\"\>", "0"],
                      
                      "InlineFormula"], \
"\<\"\\!\\(\\*StyleBox[\\\"\\\\\\\",\\\\\\\"\\\", \\\"MR\\\"]\\) \"\>", 
                    StyleBox[
                      
                      SubscriptBox["\<\"\\!\\(\\*StyleBox[\\\"\\\\\\\"x\\\\\\\
\"\\\", \\\"TI\\\"]\\)\"\>", "1"],
                      
                      "InlineFormula"], \
"\<\"\\!\\(\\*StyleBox[\\\"\\\\\\\",\\\\\\\"\\\", \\\"MR\\\"]\\) \[Ellipsis] \
\\!\\(\\*StyleBox[\\\"\\\\\\\",\\\\\\\"\\\", \\\"MR\\\"]\\) \"\>", 
                    StyleBox[
                      
                      SubscriptBox["\<\"\\!\\(\\*StyleBox[\\\"\\\\\\\"x\\\\\\\
\"\\\", \\\"TI\\\"]\\)\"\>", "n"],
                      "InlineFormula"], 
                    StyleBox["}",
                      
                      "InlineFormula"], \
"\<\"\\!\\(\\*StyleBox[\\\"\\\\\\\"]\\\\\\\"\\\", \\\"MR\\\"]\\) \"\>"}], 
                  
                  RowBox[{"\<\"give a list of rational numbers with a small \
least common denominator that well approximate the corresponding real numbers \
\"\>", 
                    StyleBox[
                      
                      SubscriptBox["\<\"\\!\\(\\*StyleBox[\\\"\\\\\\\"x\\\\\\\
\"\\\", \\\"TI\\\"]\\)\"\>", "i"],
                      "InlineFormula"]}]},
                {
                  
                  RowBox[{"\<\"\\!\\(\\*StyleBox[\\\"\\\\\\\"\
AffineRationalize[\\\\\\\"\\\", \\\"MR\\\"]\\)\"\>", 
                    StyleBox["{",
                      "InlineFormula"], 
                    StyleBox[
                      
                      SubscriptBox["\<\"\\!\\(\\*StyleBox[\\\"\\\\\\\"x\\\\\\\
\"\\\", \\\"TI\\\"]\\)\"\>", "0"],
                      
                      "InlineFormula"], \
"\<\"\\!\\(\\*StyleBox[\\\"\\\\\\\",\\\\\\\"\\\", \\\"MR\\\"]\\) \"\>", 
                    StyleBox[
                      
                      SubscriptBox["\<\"\\!\\(\\*StyleBox[\\\"\\\\\\\"x\\\\\\\
\"\\\", \\\"TI\\\"]\\)\"\>", "1"],
                      
                      "InlineFormula"], \
"\<\"\\!\\(\\*StyleBox[\\\"\\\\\\\",\\\\\\\"\\\", \\\"MR\\\"]\\) \[Ellipsis] \
\\!\\(\\*StyleBox[\\\"\\\\\\\",\\\\\\\"\\\", \\\"MR\\\"]\\) \"\>", 
                    StyleBox[
                      
                      SubscriptBox["\<\"\\!\\(\\*StyleBox[\\\"\\\\\\\"x\\\\\\\
\"\\\", \\\"TI\\\"]\\)\"\>", "n"],
                      "InlineFormula"], 
                    StyleBox["}",
                      
                      "InlineFormula"], \
"\<\"\\!\\(\\*StyleBox[\\\"\\\\\\\",\\\\\\\"\\\", \\\"MR\\\"]\\) \
\\!\\(\\*StyleBox[\\\"\\\\\\\"prec\\\\\\\"\\\", \
\\\"TI\\\"]\\)\\!\\(\\*StyleBox[\\\"\\\\\\\"]\\\\\\\"\\\", \\\"MR\\\"]\\) \
\"\>"}], 
                  RowBox[{"\<\"use a tolerance of \"\>", 
                    StyleBox[\(10\^\(-p  r  e  c\)\),
                      "InlineFormula"], "\<\" in the approximation \"\>"}]}
                },
              ColumnAlignments->{Right, Left}],
            GridBoxOptions->{RowLines->False}]],
        "2ColumnBox"], TraditionalForm]], "Text",
  CellTags->{"S5.72.1", "1.3"}],

Cell["Getting a simultaneous rational approximation. ", "Caption",
  CellTags->"S5.72.1"],

Cell[TextData[{
  "The second argument in ",
  StyleBox["ProjectiveRationalize", "MR"],
  " and ",
  StyleBox["AffineRationalize", "MR"],
  " controls the number of digits that are to be considered significant in \
the numbers given in the first argument. If no second argument is given all \
digits are considered significant. Note that the convention used for \
specifying the tolerances in this package differs from the one used in the \
built\[Hyphen]in ",
  StyleBox["Rationalize", "MR"],
  ". "
}], "Text",
  CellTags->{"S5.72.1", "1.4"}],

Cell["This loads the package. ", "MathCaption",
  CellTags->{"S5.72.1", "1.5"}],

Cell["<<NumberTheory`Rationalize`", "Input",
  CellLabel->"In[1]:= ",
  CellTags->"S5.72.1"],

Cell[TextData[{
  "Here is the ratio of rational approximations. The result is close to ",
  Cell[BoxData[
      \(TraditionalForm\`3\/11\)], "InlineFormula"],
  ", but it is a very awkward way to approximate it."
}], "MathCaption",
  CellTags->{"S5.72.1", "1.6"}],

Cell[CellGroupData[{

Cell["\<\
Rationalize[N[3 Pi], 6]/
 Rationalize[N[11 Pi], 6]\
\>", "Input",
  CellLabel->"In[2]:= ",
  CellTags->"S5.72.1"],

Cell[BoxData[
    \(9\/34\)], "Output",
  CellLabel->"Out[2]= ",
  CellTags->"S5.72.1"]
}, Open  ]],

Cell["\<\
This computes a simultaneous rational approximation that preserves \
the simple relationship between the given numbers. \
\>", "MathCaption",
  CellTags->{"S5.72.1", "1.7"}],

Cell[CellGroupData[{

Cell["AffineRationalize[{N[3 Pi], N[11 Pi]}, 6]", "Input",
  CellLabel->"In[3]:= ",
  CellTags->"S5.72.1"],

Cell[BoxData[
    \({1065\/113, 3905\/113}\)], "Output",
  CellLabel->"Out[3]= ",
  CellTags->"S5.72.1"]
}, Open  ]],

Cell["This ratio is much simpler. ", "MathCaption",
  CellTags->{"S5.72.1", "1.8"}],

Cell[CellGroupData[{

Cell["%[[1]]/%[[2]]", "Input",
  CellLabel->"In[4]:= ",
  CellTags->"S5.72.1"],

Cell[BoxData[
    \(3\/11\)], "Output",
  CellLabel->"Out[4]= ",
  CellTags->"S5.72.1"]
}, Open  ]],

Cell[TextData[{
  "The ratios of the integers returned by ",
  StyleBox["ProjectiveRationalize", "MR"],
  " approximate the ratios of the corresponding irrationals. "
}], "MathCaption",
  CellTags->{"S5.72.1", "1.9"}],

Cell[CellGroupData[{

Cell["ProjectiveRationalize[{N[3 Pi], N[11 Pi]}]", "Input",
  CellLabel->"In[5]:= ",
  CellTags->"S5.72.1"],

Cell[BoxData[
    \({3, 11}\)], "Output",
  CellLabel->"Out[5]= ",
  CellTags->"S5.72.1"]
}, Open  ]]
},
FrontEndVersion->"4.0 for X",
ScreenRectangle->{{0, 800}, {0, 600}},
WindowSize->{520, 455},
WindowMargins->{{140, Automatic}, {Automatic, 72}},
StyleDefinitions -> "HelpBrowser.nb"
]


(***********************************************************************
Cached data follows.  If you edit this Notebook file directly, not using
Mathematica, you must remove the line containing CacheID at the top of 
the file.  The cache data will then be recreated when you save this file 
from within Mathematica.
***********************************************************************)

(*CellTagsOutline
CellTagsIndex->{
  "S5.72.1"->{
    Cell[1717, 49, 145, 6, 70, "Subsection",
      CellTags->{"S5.72.1", "1.1"}],
    Cell[1865, 57, 896, 18, 70, "Text",
      CellTags->{"S5.72.1", "1.2"}],
    Cell[2764, 77, 6871, 158, 70, "Text",
      CellTags->{"S5.72.1", "1.3"}],
    Cell[9638, 237, 89, 1, 70, "Caption",
      CellTags->"S5.72.1"],
    Cell[9730, 240, 544, 13, 70, "Text",
      CellTags->{"S5.72.1", "1.4"}],
    Cell[10277, 255, 79, 1, 70, "MathCaption",
      CellTags->{"S5.72.1", "1.5"}],
    Cell[10359, 258, 92, 2, 70, "Input",
      CellTags->"S5.72.1"],
    Cell[10454, 262, 264, 6, 70, "MathCaption",
      CellTags->{"S5.72.1", "1.6"}],
    Cell[10743, 272, 123, 5, 70, "Input",
      CellTags->"S5.72.1"],
    Cell[10869, 279, 87, 3, 70, "Output",
      CellTags->"S5.72.1"],
    Cell[10971, 285, 183, 4, 70, "MathCaption",
      CellTags->{"S5.72.1", "1.7"}],
    Cell[11179, 293, 106, 2, 70, "Input",
      CellTags->"S5.72.1"],
    Cell[11288, 297, 104, 3, 70, "Output",
      CellTags->"S5.72.1"],
    Cell[11407, 303, 83, 1, 70, "MathCaption",
      CellTags->{"S5.72.1", "1.8"}],
    Cell[11515, 308, 78, 2, 70, "Input",
      CellTags->"S5.72.1"],
    Cell[11596, 312, 87, 3, 70, "Output",
      CellTags->"S5.72.1"],
    Cell[11698, 318, 217, 5, 70, "MathCaption",
      CellTags->{"S5.72.1", "1.9"}],
    Cell[11940, 327, 107, 2, 70, "Input",
      CellTags->"S5.72.1"],
    Cell[12050, 331, 89, 3, 70, "Output",
      CellTags->"S5.72.1"]},
  "1.1"->{
    Cell[1717, 49, 145, 6, 70, "Subsection",
      CellTags->{"S5.72.1", "1.1"}]},
  "1.2"->{
    Cell[1865, 57, 896, 18, 70, "Text",
      CellTags->{"S5.72.1", "1.2"}]},
  "1.3"->{
    Cell[2764, 77, 6871, 158, 70, "Text",
      CellTags->{"S5.72.1", "1.3"}]},
  "1.4"->{
    Cell[9730, 240, 544, 13, 70, "Text",
      CellTags->{"S5.72.1", "1.4"}]},
  "1.5"->{
    Cell[10277, 255, 79, 1, 70, "MathCaption",
      CellTags->{"S5.72.1", "1.5"}]},
  "1.6"->{
    Cell[10454, 262, 264, 6, 70, "MathCaption",
      CellTags->{"S5.72.1", "1.6"}]},
  "1.7"->{
    Cell[10971, 285, 183, 4, 70, "MathCaption",
      CellTags->{"S5.72.1", "1.7"}]},
  "1.8"->{
    Cell[11407, 303, 83, 1, 70, "MathCaption",
      CellTags->{"S5.72.1", "1.8"}]},
  "1.9"->{
    Cell[11698, 318, 217, 5, 70, "MathCaption",
      CellTags->{"S5.72.1", "1.9"}]}
  }
*)

(*CellTagsIndex
CellTagsIndex->{
  {"S5.72.1", 12780, 354},
  {"1.1", 14230, 393},
  {"1.2", 14324, 396},
  {"1.3", 14413, 399},
  {"1.4", 14504, 402},
  {"1.5", 14594, 405},
  {"1.6", 14690, 408},
  {"1.7", 14787, 411},
  {"1.8", 14884, 414},
  {"1.9", 14980, 417}
  }
*)

(*NotebookFileOutline
Notebook[{
Cell[1717, 49, 145, 6, 70, "Subsection",
  CellTags->{"S5.72.1", "1.1"}],
Cell[1865, 57, 896, 18, 70, "Text",
  CellTags->{"S5.72.1", "1.2"}],
Cell[2764, 77, 6871, 158, 70, "Text",
  CellTags->{"S5.72.1", "1.3"}],
Cell[9638, 237, 89, 1, 70, "Caption",
  CellTags->"S5.72.1"],
Cell[9730, 240, 544, 13, 70, "Text",
  CellTags->{"S5.72.1", "1.4"}],
Cell[10277, 255, 79, 1, 70, "MathCaption",
  CellTags->{"S5.72.1", "1.5"}],
Cell[10359, 258, 92, 2, 70, "Input",
  CellTags->"S5.72.1"],
Cell[10454, 262, 264, 6, 70, "MathCaption",
  CellTags->{"S5.72.1", "1.6"}],

Cell[CellGroupData[{
Cell[10743, 272, 123, 5, 70, "Input",
  CellTags->"S5.72.1"],
Cell[10869, 279, 87, 3, 70, "Output",
  CellTags->"S5.72.1"]
}, Open  ]],
Cell[10971, 285, 183, 4, 70, "MathCaption",
  CellTags->{"S5.72.1", "1.7"}],

Cell[CellGroupData[{
Cell[11179, 293, 106, 2, 70, "Input",
  CellTags->"S5.72.1"],
Cell[11288, 297, 104, 3, 70, "Output",
  CellTags->"S5.72.1"]
}, Open  ]],
Cell[11407, 303, 83, 1, 70, "MathCaption",
  CellTags->{"S5.72.1", "1.8"}],

Cell[CellGroupData[{
Cell[11515, 308, 78, 2, 70, "Input",
  CellTags->"S5.72.1"],
Cell[11596, 312, 87, 3, 70, "Output",
  CellTags->"S5.72.1"]
}, Open  ]],
Cell[11698, 318, 217, 5, 70, "MathCaption",
  CellTags->{"S5.72.1", "1.9"}],

Cell[CellGroupData[{
Cell[11940, 327, 107, 2, 70, "Input",
  CellTags->"S5.72.1"],
Cell[12050, 331, 89, 3, 70, "Output",
  CellTags->"S5.72.1"]
}, Open  ]]
}
]
*)




(***********************************************************************
End of Mathematica Notebook file.
***********************************************************************)

