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

  1.  
  2.  
  3. (*:Version: Mathematica 2.0 *)
  4.  
  5. (*:Name: NumberTheory`Recognize` *)
  6.  
  7. (*:Title: Recognition of Polynomials *)
  8.  
  9. (*:Author: Daniel R. Grayson *)
  10.  
  11. (*:Keywords:
  12.     Polynomial, Degree
  13. *)
  14.  
  15. (*:Requirements: none. *)
  16.  
  17. (*:Warnings: 
  18.     Trouble with bignum:
  19.  
  20.     In[5]:= Recognize[ 902589045298.90329599084,10,t]
  21.  
  22.     Out[5]= 1
  23.  
  24.     In[6]:= Recognize[ 902589045298.90329599084,15,t]
  25.  
  26.     Out[6]= 1
  27.  
  28. *)
  29.  
  30. (*:Sources:
  31.  
  32. *)
  33.  
  34. (*:Summary: This package allows you to find the minimal polynomial of
  35. which a given approximate number is a root.
  36. *)
  37.  
  38. BeginPackage["NumberTheory`Recognize`"]
  39.  
  40. Recognize::usage = "Recognize[x,n,t] finds a polynomial of degree at 
  41.     most n in the variable t which is satisfied by the number x.
  42.     Recognize[x,n,t,k] also finds a polynomial of degree at most n
  43.     in the variable t, but with a penalty of weight k against
  44.     higher degree polynomials. k must be a nonnegative integer."
  45. Begin["`Private`"]
  46.  
  47. Recognize[x_, n_Integer?Positive, t_Symbol, k_Integer:0] := 
  48.     Block[{data, i, scale},
  49.         If[IntegerQ[x] || ! NumberQ[x], Message[Recognize::nodec],
  50.             data = Table[x^i, {i, 0, n}];
  51.             data = Round[data 10^Max[Accuracy /@ Rest[data]]];
  52.             scale = Table[(k+1)^i, {i, 0, n}];
  53.             data = Transpose[ Append[ DiagonalMatrix[scale], data ]];
  54.             data = Drop[LatticeReduce[data][[1]], -1];
  55.             Table[t^i, {i, 0, n}] . (data/scale)
  56.             ]
  57.         ]
  58.  
  59.     
  60. Recognize::nodec = "Not a decimal number."
  61.  
  62. End[ ]   (* NumberTheory`Recognize`Private` *)
  63.  
  64. Protect[ Recognize ]
  65.  
  66. EndPackage[ ]   (* NumberTheory`Recognize` *)
  67.  
  68. (*:Limitations: none known. *)
  69.  
  70.  
  71. (*:Examples:
  72.  
  73. Recognize[1.17,3,t]
  74.  
  75. Recognize[984.945,10,t,3]
  76.  
  77.  
  78. *)
  79.  
  80.  
  81.  
  82.