(*^
::[     Information =

        "This is a Mathematica Notebook file.  It contains ASCII text, and can be
        transferred by email, ftp, or other text-file transfer utility.  It should
        be read or edited using a copy of Mathematica or MathReader.  If you 
        received this as email, use your mail application or copy/paste to save 
        everything from the line containing (*^ down to the line containing ^*)
        into a plain text file.  On some systems you may have to give the file a 
        name ending with ".ma" to allow Mathematica to recognize it as a Notebook.
        The line below identifies what version of Mathematica created this file,
        but it can be opened using any other version as well.";

        FrontEndVersion = "Macintosh Mathematica Notebook Front End Version 2.2";

        MacintoshStandardFontEncoding; 
        
        fontset = title, inactive, noPageBreakBelow, nohscroll, preserveAspect, groupLikeTitle, center, M7, bold, e8,  24, "Times"; 
        fontset = subtitle, inactive, noPageBreakBelow, nohscroll, preserveAspect, groupLikeTitle, center, M7, bold, e6,  18, "Times"; 
        fontset = subsubtitle, inactive, noPageBreakBelow, nohscroll, preserveAspect, groupLikeTitle, center, M7, italic, e6,  14, "Times"; 
        fontset = section, inactive, noPageBreakBelow, nohscroll, preserveAspect, groupLikeSection, grayBox, M22, bold, a20,  18, "Times"; 
        fontset = subsection, inactive, noPageBreakBelow, nohscroll, preserveAspect, groupLikeSection, blackBox, M19, bold, a15,  14, "Times"; 
        fontset = subsubsection, inactive, noPageBreakBelow, nohscroll, preserveAspect, groupLikeSection, whiteBox, M18, bold, a12,  12, "Times"; 
        fontset = text, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7,  12, "Times"; 
        fontset = smalltext, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7,  10, "Times"; 
        fontset = input, noPageBreakInGroup, nowordwrap, preserveAspect, groupLikeInput, M42, N23, bold, L-5,  12, "Courier"; 
        fontset = output, output, inactive, noPageBreakInGroup, nowordwrap, preserveAspect, groupLikeOutput, M42, N23, L-5,  12, "Courier"; 
        fontset = message, inactive, noPageBreakInGroup, nowordwrap, preserveAspect, groupLikeOutput, M42, N23, R65535, L-5,  12, "Courier"; 
        fontset = print, inactive, noPageBreakInGroup, nowordwrap, preserveAspect, groupLikeOutput, M42, N23, L-5,  12, "Courier"; 
        fontset = info, inactive, noPageBreakInGroup, nowordwrap, preserveAspect, groupLikeOutput, M42, N23, B65535, L-5,  12, "Courier"; 
        fontset = postscript, PostScript, formatAsPostScript, output, inactive, noPageBreakInGroup, nowordwrap, preserveAspect, groupLikeGraphics, M7, l34, w282, h287,  12, "Courier"; 
        fontset = name, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, italic,  10, "Geneva"; 
        fontset = header, inactive, noKeepOnOnePage, preserveAspect, M7,  12, "Times"; 
        fontset = leftheader, inactive, L2,  12, "Times"; 
        fontset = footer, inactive, noKeepOnOnePage, preserveAspect, center, M7,  12, "Times"; 
        fontset = leftfooter, inactive, L2,  12, "Times"; 
        fontset = help, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7,  10, "Times"; 
        fontset = clipboard, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7,  12, "Times"; 
        fontset = completions, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7,  12, "Times"; 
        fontset = special1, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7,  12, "Times"; 
        fontset = special2, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7,  12, "Times"; 
        fontset = special3, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7,  12, "Times"; 
        fontset = special4, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7,  12, "Times"; 
        fontset = special5, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7,  12, "Times"; 
        paletteColors = 128; currentKernel; 
]
:[font = subsubsection; inactive; Cclosed; preserveAspect; startGroup]
Legal Notice, Disclaimer, and Book Ordering Information:
:[font = input; inactive; preserveAspect; endGroup]
(* Legal Notice, Disclaimer, and Book Ordering Information:

Last Modified: FIO January 1, 1995

Mathematica For Physics
Robert L. Zimmerman and Fredrick I. Olness
MathSource Number: 0206-862 

ISBN 0-201-53796-6
For ordering information, call 1-800-822-6339
Addison-Wesley Publishing Company

Communication with the authors:
Fredrick I. Olness:  olness@mail.physics.smu.edu
Robert L. Zimmerman: bob@zim.uoregon.edu

Copyright 1995, Addison-Wesley Publishing Company, Inc. The material in
this file may be distributed freely  so long as the content remains
unchanged, and the copyright and  reference notices are included. The
publisher grants permission for the noncommercial use of these programs
and program segments.  All other uses require the prior written consent
of the publisher.    

All rights are reserved with regard to the  material in the
"Mathematica For Physics" text, and may not be reproduced, stored in a
retrieval system, or transmitted,  in any form or by any means,
electronic, mechanical, photocopying,  recording, or otherwise, without
the prior written permission  of the publisher. 

The programs and applications presented in this book have been included
for  their instructional value. They have been tested with care but are
not  guaranteed for any particular purpose. The publisher does not
offer any  warranties or representations, nor does it accept any
liabilities with  respect to the programs or applications. 

 *)

:[font = title; inactive; initialization; preserveAspect]
4. Lagrangians and Hamiltonians
:[font = input; initialization; preserveAspect]
*)
(* Some "Mathematica for Physics" Initializations *)
$DefaultFont={"Courier",8};
Off[General::spell];
Off[General::spell1];
PageWidth->70;
(*
:[font = input; initialization; preserveAspect]
*)
(* Needs["Algebra`SymbolicSum`"]  obsolete *)
(* Needs["Algebra`Trigonometry`"] obsolete *)
Needs["Calculus`VectorAnalysis`"] 
Needs["Graphics`Graphics`"]  
Needs["Graphics`Graphics3D`"]  
(*
:[font = subsection; inactive; initialization; preserveAspect; startGroup]
User-Defined Rules
:[font = subsubsection; inactive; initialization; preserveAspect; startGroup]
1. HyperbolicToComplex and ComplexToHyperbolic
:[font = input; initialization; preserveAspect]
*)
HyperbolicToComplexRule={Cosh[x___]:>(Exp[x]+Exp[-x])/2,
                         Sinh[x___]:>(Exp[x]-Exp[-x])/2};
                     
ComplexToHyperbolicRule={Exp[x___]:>Cosh[x]+Sinh[x]};
(*
:[font = input; initialization; preserveAspect; endGroup; endGroup]
*)
HyperbolicToComplex[expression_]:=
           expression //.HyperbolicToComplexRule
ComplexToHyperbolic[expression_]:=
           expression //.ComplexToHyperbolicRule
           
(*
:[font = subsection; inactive; initialization; preserveAspect]
 User-Defined Procedures
:[font = subsubsection; inactive; initialization; preserveAspect; startGroup]
1. Procedure to find Lagrange's Equations:     
:[font = input; initialization; preserveAspect]
*)
Lag::usage=" Lag[qi,L,Qi:0,t:t] computes Lagrange's 
equations of motion given the canonical coordinate 
qi,the Lagrangian L, and the generalized force Qi.
The generalized coordinate qi can be entered as a
list or as a single coordinate.";
(*
:[font = input; initialization; preserveAspect]
*)
 Lag[qi_,L_,Qi_:0,t_:t] := 
  (D[ D[L,(D[qi[t],t])],t]-D[L,qi[t]]==Qi);
(*
:[font = input; initialization; preserveAspect; endGroup]
*)
Lag[qi_List,L_,Qi_:0,t_:t] := 
  (D[ D[L,(D[#[t],t])],t]-D[L,#[t]]==Qi)& /@ qi;
(*
:[font = subsubsection; inactive; initialization; preserveAspect; startGroup]
2. Procedure to find the Canonical Momentum, Hamiltonian, and Equations of motion:     
:[font = input; initialization; preserveAspect]
*)
Clear[Hamilton];
Hamilton::usage=" Hamilton[L,{xList},{pList},t:t]
 returns, \n
     i.   the expression for the canonical momentum; \n
     ii.  the hamiltonian; and   \n
     iii. Hamilton's equation of motion.";
(*
:[font = input; initialization; preserveAspect; endGroup]
*)
 Hamilton[L_,xList_List,pList_List,t_:t]:= 
  Module[{xx,vv,pp,sol,ham,eqp,eqx,eqs},
    xx = Map[ #[t]&,xList];
    vv = Map[#'[t]&,xList];
    pp = Map[ #[t]&,pList];
    sol= Solve[ (D[L,#]& /@ vv) == pp,vv] //Flatten;
    ham= pp.vv-L //.sol //Simplify //Expand;
    eqp= D[pp,t]==-Map[D[ham,#]&,xx] //Thread;
    eqx= D[xx,t]==+Map[D[ham,#]&,pp] //Thread;
    eqs= Join[eqp,eqx];
    Return[{sol,ham,eqs}]
   ]
(*
:[font = subsubsection; inactive; initialization; preserveAspect; startGroup]
3. Procedure to find the Canonical Momentum, Hamilton's Principal Function, and Hamilton-Jacobi Equation:
:[font = input; initialization; preserveAspect]
*)
Clear[HamiltonJacobi];
HamiltonJacobi::usage=
 " HamiltonJacobi[L,{xList},{pList},{wList[...]},a,t:t]
 returns,  \n\n
    i. the expression for the canonical momentum; \n
   ii. the Hamilton's Principal Function; and  \n
  iii. Hamilton-Jacobi Equation.  \n\n
The procedure assumes that the Hamilton Principal
Function can be expressed in the form W[q]-a t.";
(*
:[font = input; initialization; preserveAspect; endGroup]
*)
 HamiltonJacobi[L_,xList_List,pList__List,wList_List,a_,t_:t]:= 
  Module[{xx,vv,pp,sol,ham,ww,hamPrincFunc,pNewRule,HJeq},
    xx = Map[ #[t]&,xList];
    vv = Map[#'[t]&,xList];
    pp = Map[ #[t]&,pList];
    sol= Solve[ (D[L,#]& /@ vv) == pp,vv] //Flatten;
    ham= (pp.vv-L) //.sol //Simplify //Expand;

    ww = Map[ #[t]&,wList,{2}];
    hamPrincFunc = (Plus @@ ww) - a t;
    pNewRule = 
      {pp -> (D[hamPrincFunc,#]& /@ xx) //Thread }//Flatten;
    HJeq = (ham//.pNewRule ) - a ==0;       
    
    Return[{sol,hamPrincFunc,HJeq}]
   ]
(*
:[font = subsubsection; inactive; initialization; preserveAspect; startGroup]
4  Series Expansion Solution for  Second Order Equation:
:[font = input; initialization; preserveAspect]
*)
diffSeriesOne::usage=" 
diffSeriesOne[eq,z,t,order,{initialList}]
eq is an equation, z is the function, t is the independent 
variable, order is the order of the Series solution, and 
{initialList} is a list of initial conditions such as
{z[0]->z0}.
This procedure assumes the solution can be written as 
a power series.";
(*
:[font = input; initialization; preserveAspect; endGroup]
*)
Clear[diffSeriesOne];
diffSeriesOne[eqin_,z_,t_,order_,initList__List]:= 
 Module[{eq,zSer,eSer,eqs,vars,sol,zSol},
   eq=eqin[[1]]-eqin[[2]]==0;
   zSer= Series[z[t],{t,0,order}];
   eSer= eq/.(z[t]->zSer)//ExpandAll;
   eqs = Thread[CoefficientList[eSer[[1]],t]==0];
   vars=(Table[D[z[t],{t,j}],{j,2,order+2}]/.t->0);     
   sol = Solve[eqs,vars][[1]];
   zSol= zSer/.sol/.initList;
   Return[ zSol]  
 ]
(*
:[font = subsubsection; inactive; initialization; preserveAspect; startGroup]
5  Series Expansion Solution for two First Order Equation:     
:[font = input; initialization; preserveAspect]
*)
Clear[firstDiffSeries];
firstDiffSeries::usage=" 
firstDiffSeries[eqin,{z,p},initRule_List,order,t_:t]:= 
 returns and expansion for the solution of two first order
 equations in powers of t.";
(*
:[font = input; initialization; preserveAspect; endGroup]
*)
firstDiffSeries[eqin_,{z_,p_},initRule_List,order_,t_:t]:= 
 Module[{eq,zSer,pSer,eSer,vars,sol,zpsol},
   eq=Table[eqin[[i,1]]-eqin[[i,2]]==0,{i,2}];
   zSer= Series[z[t],{t,0,order}];
   pSer= Series[p[t],{t,0,order}];
   eSer= eq//.({z[t]->zSer,p[t]->pSer})//ExpandAll;
   vars=(Table[{D[z[t],{t,j}],
                D[p[t],{t,j}]},{j,1,order+1}]/.t->0)//Flatten;     
   sol = Solve[eSer,vars] //Flatten;
   zpSol= {zSer,pSer} //.sol //.initRule;
   Return[ zpSol]  
 ]
(*
:[font = subsubsection; inactive; initialization; preserveAspect; startGroup]
6. First Order Perturbation Solution:
:[font = input; initialization; preserveAspect]
*)
Clear[firstOrderPert];
firstOrderPert::usage=
    "firstOrderPert[eq,x,{x0,v0},eps:eps,t:t]
 returns the first order correction to the term with coefficient eps.";
(*
:[font = input; initialization; preserveAspect; endGroup]
*)
firstOrderPert[eq1_,x_,{x0_,v0_},eps_:eps,t_:t]:=
  Module[{xRule,eq2,eqEps,initCon0,initCon1,dsol1,dsol2},
     xRule={x->(x[0][#] + eps x[1][#]&)};
     eq2=eq1[[1]]-eq1[[2]] /.xRule;
     eqEps= CoefficientList[eq2,eps]==0 //Thread;
     initCon0={x[0][0]==x0, (x[0])'[0]==v0};
     initCon1={x[1][0]==0 , (x[1])'[0]==0 };
     dsol1=( DSolve[Join[{eqEps[[1]]},initCon0],{x[0][t]},t] 
               //Flatten );
     dsol2=( DSolve[Join[{eqEps[[2]]},initCon1] //.dsol1,{x[1][t]},t] 
               //Flatten );
     Return[ x[t] /.xRule //.Join[dsol1,dsol2] ]
   ]
(*
:[font = subsection; inactive; initialization; preserveAspect; startGroup]
 Protect User-Defined Procedures
:[font = input; initialization; preserveAspect; endGroup]
*)
Protect[
,ComplexToHyperbolic ,ComplexToHyperbolicRule
,HyperbolicToComplex ,HyperbolicToComplexRule
,Lag,Hamilton,HamiltonJacobi
,diffSeriesOne,firstDiffSeries,firstOrderPert
];
(*
^*)
