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

  1.  
  2. (* :Title: Orthogonalization *)
  3.  
  4. (* :Author: John M. Novak *)
  5.  
  6. (* :Summary: A basic package for performing orthogonalization;  needs
  7.     considerable expansion;  currently contains only the Gram-Schmidt
  8.     routine for vector or function spaces. *)
  9.  
  10. (* :Context: LinearAlgebra`Orthogonalization` *)
  11.  
  12. (* :Package Version: 1.1 *)
  13.  
  14. (* :Copyright: Copyright 1990, Wolfram Research, Inc. *)
  15.  
  16. (* :History: 
  17.     Version 1.0 by John M. Novak (Wolfram Research), Jan., 1991.
  18.     Version 1.1 by John M. Novak, Feb., 1991. Adds ability to handle
  19.         function spaces and ability to generate normalized basis.
  20. *)
  21.  
  22. (* :Keywords: orthogonalization, linear algebra, matrices, vector spaces *)
  23.  
  24. (* :Sources: Standard linear algebra and calculus texts *)
  25.  
  26. (* :Mathematica Version: 2.0 *)
  27.  
  28. (* :Example:
  29.     Here is an example of using GramSchmidt with an arbitrary inner product
  30.     in a function space:
  31.     GramSchmidt[{1,x,x^2,x^3,x^4},
  32.         InnerProduct->(Integrate[#1 #2,{x,-1,1}]&)]
  33.     This generates the first five Legendre Polynomials.
  34. *)
  35.  
  36. BeginPackage["LinearAlgebra`Orthogonalization`"]
  37.  
  38. (* Usage messages *)
  39.  
  40. Normalize::usage =
  41.     "Normalize[vec] normalizes a vector passed in; note that an inner
  42.     product can be specified with the option InnerProduct to allow, for
  43.     instance, function spaces to be used."
  44.  
  45. Projection::usage =
  46.     "Projection[v1,v2] projects a vector on another vector. Note that an
  47.     inner product can be specified with the option InnerProduct to allow,
  48.     for instance, function spaces."
  49.  
  50. GramSchmidt::usage =
  51.     "GramSchmidt[vectors] performs the GramSchmidt orthogonaliztion process
  52.     on a list of vectors. Note that an inner product can be specified,
  53.     allowing, for instance, a function space to used.  Also, the option
  54.     Normalized can be set to determine whether or not the basis is
  55.     orthonormal."
  56.  
  57. InnerProduct::usage =
  58.     "InnerProduct is an option to functions in the Orthogonalization
  59.     package specifying an inner product as a pure function.  The
  60.     default is Dot.  For example: InnerProduct->(Integrate[#1 #2,{x,-1,1}]&)."
  61.  
  62. Normalized::usage =
  63.     "Normalized is an option for GramSchmidt that determines
  64.     whether the basis generated is orthonormal or not.  If
  65.     True, then it is orthonormal, False is returned otherwise."
  66.  
  67. Begin["`Private`"]
  68.  
  69. Options[Normalize] = {InnerProduct->Dot};
  70.  
  71. Options[Projection] = {InnerProduct->Dot};
  72.  
  73. Options[GramSchmidt] = {InnerProduct->Dot,Normalized->True};
  74.  
  75. Normalize[vec_,opts___] :=
  76.     Module[{ip},
  77.         {ip} = {InnerProduct}/.{opts}/.Options[Normalize];
  78.         vec/Sqrt[ip[vec,vec]]]
  79.  
  80. Projection[v1_,v2_,opts___] := 
  81.     Module[{ip},
  82.         {ip} = {InnerProduct}/.{opts}/.Options[Projection];
  83.         ip[v1,v2] v2/ip[v2,v2]
  84.     ]
  85.  
  86. (* auxiliary internal function; projection of the vector on a list of vectors.
  87. *)
  88.  
  89. multipleprojection[v1_,vecs_,opts___] := 
  90.     Plus @@ Map[Projection[v1,#,opts]&,vecs]
  91.  
  92. (*
  93. The Gram-Schmidt method works by taking a list of vectors; starting with the
  94. first vector, it finds a new basis vector in the orthogonal set by subtracting
  95. from each vector the projection of that vector on the basis vectors determined
  96. so far. Note that a different inner product can be specified, allowing the
  97. use of this function in vector spaces.
  98. *)
  99.  
  100. GramSchmidt[vecs_List,opts___] :=
  101.     Module[{norm,ip,ans},
  102.         {norm,ip} = {Normalized,InnerProduct}/.{opts}/.
  103.             Options[GramSchmidt];
  104.         ans = Fold[Join[#1,
  105.             {#2 - multipleprojection[#2,#1, InnerProduct->ip]}]&,
  106.             {},vecs];
  107.         If[TrueQ[norm],
  108.             Map[Normalize[#,InnerProduct->ip]&,ans],
  109.             ans]
  110.     ]
  111.  
  112. End[]
  113.  
  114. EndPackage[]
  115.