avector.tex 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. \documentstyle[11pt,reduce]{article}
  2. \date{}
  3. \title{A Vector Algebra and Calculus Package for REDUCE}
  4. \author{David Harper \\
  5. Astronomy Unit \\
  6. Queen Mary and Westfield College \\
  7. University of London \\
  8. Mile End Road \\
  9. London E1 4NS \\
  10. England \\[0.05in]
  11. Electronic mail: {\it adh@star.qmw.ac.uk}}
  12. \begin{document}
  13. \maketitle
  14. \index{AVECTOR package}
  15. \section{Introduction}
  16. This package \footnote{Reference: Computer Physics Communications,
  17. {\bf 54}, 295-305 (1989)}
  18. is written in RLISP (the LISP meta-language) and is
  19. intended for use with REDUCE 3.4. \index{vector algebra} It provides
  20. REDUCE with the ability to perform vector algebra using the same
  21. notation as scalar algebra. The basic algebraic operations are
  22. supported, as are differentiation and integration of vectors with
  23. respect to scalar variables, cross product and dot product, component
  24. manipulation and application of scalar functions ({\em e.g.} cosine)
  25. to a vector to yield a vector result.
  26. A set of vector calculus operators are provided for use with any
  27. orthogonal curvilinear coordinate system. These operators are
  28. gradient, divergence, curl and del-squared (Laplacian). The Laplacian
  29. operator can take scalar or vector arguments.
  30. Several important coordinate systems are pre-defined and can be
  31. invoked by name. It is also possible to create new coordinate systems
  32. by specifying the names of the coordinates and the values of the scale
  33. factors.
  34. \section{Vector declaration and initialisation}
  35. Any name may be declared to be a vector, provided that it has
  36. not previously been declared as a matrix or an array. To
  37. declare a list of names to be vectors use the VEC command:
  38. \index{VEC command}
  39. \begin{verbatim}
  40. VEC A,B,C;
  41. \end{verbatim}
  42. declares the variables {\tt A}, {\tt B} and {\tt C} to be vectors.
  43. If they have already been assigned (scalar) values, these will be lost.
  44. When a vector is declared using the {\tt VEC} command, it does not
  45. have an initial value.
  46. If a vector value is assigned to a scalar variable, then that
  47. variable will automatically be declared as a vector and the
  48. user will be notified that this has happened.
  49. \index{AVEC function}
  50. A vector may be initialised using the {\tt AVEC} function which
  51. takes three scalar arguments and returns a vector made up
  52. from those scalars. For example
  53. \begin{verbatim}
  54. A := AVEC(A1, A2, A3);
  55. \end{verbatim}
  56. sets the components of the vector {\tt A} to {\tt A1}, {\tt A2} and {\tt A3}.
  57. \section{Vector algebra}
  58. (In the examples which follow, {\tt V}, {\tt V1}, {\tt V2} {\em etc}
  59. are assumed to be vectors while {\tt S}, {\tt S1}, {\tt S2} etc are scalars.)
  60. \index{+ ! vector} \index{- ! vector} \index{* ! vector} \index{/ ! vector}
  61. The scalar algebra operators +,-,* and / may be used with
  62. vector operands according to the rules of vector algebra.
  63. Thus multiplication and division of a vector by a scalar
  64. are both allowed, but it is an error to multiply or
  65. divide one vector by another.
  66. \begin{tabular}{l l}
  67. {\tt V := V1 + V2 - V3;} & Addition and subtraction \\
  68. {\tt V := S1*3*V1;} & Scalar multiplication \\
  69. {\tt V := V1/S;} & Scalar division \\
  70. {\tt V := -V1;} & Negation \\
  71. \end{tabular}
  72. \index{DOT ! vector} \index{Dot product} \index{CROSS ! vector}
  73. \index{cross product}
  74. \noindent Vector multiplication is carried out using the infix
  75. operators {\tt DOT} and {\tt CROSS}. These are defined to have
  76. higher precedence than scalar multiplication and
  77. division.
  78. \begin{tabular}{l l}
  79. {\tt V := V1 CROSS V2;} & Cross product \\
  80. {\tt S := V1 DOT V2;} & Dot product \\
  81. {\tt V := V1 CROSS V2 + V3;} & \\
  82. {\tt V := (V1 CROSS V2) + V3;} & \\
  83. \end{tabular}
  84. The last two expressions are equivalent due to the precedence of
  85. the {\tt CROSS} operator.
  86. \index{VMOD operator}
  87. The modulus of a vector may be calculated using the {\tt VMOD} operator.
  88. \begin{verbatim}
  89. S := VMOD V;
  90. \end{verbatim}
  91. A unit vector may be generated from any vector using the {\tt VMOD}
  92. operator.
  93. \begin{verbatim}
  94. V1 := V/(VMOD V);
  95. \end{verbatim}
  96. Components may be extracted from any vector using index notation
  97. in the same way as an array.
  98. \begin{tabular}{l l}
  99. {\tt V := AVEC(AX, AY, AZ);} & \\
  100. {\tt V(0);} & yields AX \\
  101. {\tt V(1);} & yields AY \\
  102. {\tt V(2);} & yields AZ \\
  103. \end{tabular}
  104. It is also possible to set values of individual components. Following
  105. from above:
  106. \begin{verbatim}
  107. V(1) := B;
  108. \end{verbatim}
  109. The vector {\tt V} now has components {\tt AX}, {\tt B}, {\tt AZ}.
  110. \index{vector ! differentiation} \index{vector ! integration}
  111. \index{differentiation ! vector} \index{differentiation ! vector}
  112. Vectors may be used as arguments in the differentiation and
  113. integration routines in place of the dependent expression.
  114. \begin{tabular}{l l}
  115. {\tt V := AVEC(X**2, SIN(X), Y);} & \\
  116. {\tt DF(V,X);} & yields (2*X, COS(X), 0) \\
  117. {\tt INT(V,X);} & yields (X**3/3, -COS(X), Y*X) \\
  118. \end{tabular}
  119. Vectors may be given as arguments to monomial functions such as {\tt
  120. SIN}, {\tt LOG} and {\tt TAN}. The result is a vector obtained by
  121. applying the function component-wise to the argument vector.
  122. \begin{tabular}{l l}
  123. {\tt V := AVEC(A1, A2, A3);} & \\
  124. {\tt SIN(V);} & yields (SIN(A1), SIN(A2), SIN(A3)) \\
  125. \end{tabular}
  126. \section{ Vector calculus}
  127. \index{DIV ! operator} \index{divergence ! vector field}
  128. \index{GRAD ! operator} \index{gradient ! vector field}
  129. \index{CURL ! operator} \index{curl ! vector field}
  130. \index{DELSQ ! operator} \index{Laplacian ! vector field}
  131. The vector calculus operators div, grad and curl are recognised.
  132. The Laplacian operator is also available and may be applied to
  133. scalar and vector arguments.
  134. \begin{tabular}{l l}
  135. {\tt V := GRAD S;} & Gradient of a scalar field \\
  136. {\tt S := DIV V;} & Divergence of a vector field \\
  137. {\tt V := CURL V1;} & Curl of a vector field \\
  138. {\tt S := DELSQ S1;} & Laplacian of a scalar field \\
  139. {\tt V := DELSQ V1;} & Laplacian of a vector field \\
  140. \end{tabular}
  141. These operators may be used in any orthogonal curvilinear coordinate
  142. system. The user may alter the names of the coordinates and the values
  143. of the scale factors. Initially the coordinates are {\tt X}, {\tt Y}
  144. and {\tt Z} and the scale factors are all unity.
  145. \index{COORDS vector} \index{HFACTORS scale factors}
  146. There are two special vectors : {\tt COORDS} contains the names
  147. of the coordinates in the current system and {\tt HFACTORS}
  148. contains the values of the scale factors.
  149. \index{COORDINATES operator}
  150. The coordinate names may be changed using the {\tt COORDINATES}
  151. operator.
  152. \begin{verbatim}
  153. COORDINATES R,THETA,PHI;
  154. \end{verbatim}
  155. This command changes the coordinate names to {\tt R}, {\tt THETA} and
  156. {\tt PHI}.
  157. \index{SCALEFACTORS operator}
  158. The scale factors may be altered using the {\tt SCALEFACTORS} operator.
  159. \begin{verbatim}
  160. SCALEFACTORS(1,R,R*SIN(THETA));
  161. \end{verbatim}
  162. This command changes the scale factors to {\tt 1}, {\tt R} and {\tt R
  163. SIN(THETA)}.
  164. Note that the arguments of {\tt SCALEFACTORS} must be enclosed in
  165. parentheses. This is not necessary with {\tt COORDINATES}.
  166. When vector differential operators are applied to an expression,
  167. the current set of coordinates are used as the independent
  168. variables and the scale factors are employed in the calculation.
  169. (See, for example, Batchelor G.K. 'An Introduction to Fluid
  170. Mechanics', Appendix 2.)
  171. \index{"!*CSYSTEMS global (AVECTOR)}
  172. Several coordinate systems are pre-defined and may be invoked by
  173. name. To see a list of valid names enter
  174. \begin{verbatim}
  175. SYMBOLIC !*CSYSTEMS;
  176. \end{verbatim}
  177. and REDUCE will respond with something like
  178. \begin{verbatim}
  179. (CARTESIAN SPHERICAL CYLINDRICAL)
  180. \end{verbatim}
  181. \index{GETCSYSTEM command}
  182. To choose a coordinate system by name, use the command {\tt GETCSYSTEM}.
  183. To choose the Cartesian coordinate system :
  184. \begin{verbatim}
  185. GETCSYSTEM 'CARTESIAN;
  186. \end{verbatim}
  187. \index{PUTCSYSTEM command}
  188. Note the quote which prefixes the name of the coordinate system. This
  189. is required because {\tt GETCSYSTEM} (and its complement {\tt
  190. PUTCSYSTEM}) is a {\tt SYMBOLIC} procedure which requires a literal
  191. argument.
  192. REDUCE responds by typing a list of the coordinate names in that
  193. coordinate system. The example above would produce the response
  194. \begin{verbatim}
  195. (X Y Z)
  196. \end{verbatim}
  197. whilst
  198. \begin{verbatim}
  199. GETCSYSTEM 'SPHERICAL;
  200. \end{verbatim}
  201. would produce
  202. \begin{verbatim}
  203. (R THETA PHI)
  204. \end{verbatim}
  205. Note that any attempt to invoke a coordinate system is subject to the
  206. same restrictions as the implied calls to {\tt COORDINATES} and {\tt
  207. SCALEFACTORS}. In particular, {\tt GETCSYSTEM} fails if any of the
  208. coordinate names has been assigned a value and the previous coordinate
  209. system remains in effect.
  210. A user-defined coordinate system can be assigned a name using the
  211. command {\tt PUTCSYSTEM}. It may then be re-invoked at a later stage using
  212. {\tt GETCSYSTEM}.
  213. \example\index{AVECTOR package ! example}
  214. We define a general coordinate system with coordinate names {\tt
  215. X},{\tt Y},{\tt Z} and scale factors {\tt H1},{\tt H2},{\tt H3} :
  216. \begin{verbatim}
  217. COORDINATES X,Y,Z;
  218. SCALEFACTORS(H1,H2,H3);
  219. PUTCSYSTEM 'GENERAL;
  220. \end{verbatim}
  221. This system may later be invoked by entering
  222. \begin{verbatim}
  223. GETCSYSTEM 'GENERAL;
  224. \end{verbatim}
  225. \section{Volume and Line Integration}
  226. Several functions are provided to perform volume and line integrals.
  227. These operate in any orthogonal curvilinear coordinate system and
  228. make use of the scale factors described in the previous section.
  229. Definite integrals of scalar and vector expressions may be calculated
  230. using the {\tt DEFINT} function.
  231. \example\index{AVECTOR package ! example}
  232. \index{DEFINT function} \index{integration ! definite (simple)}
  233. \index{definite integration (simple)}
  234. \noindent To calculate the definite integral of $\sin(x)^2$ between 0 and
  235. 2$\pi$ we enter
  236. \begin{verbatim}
  237. DEFINT(SIN(X)**2,X,0,2*PI);
  238. \end{verbatim}
  239. This function is a simple extension of the {\tt INT} function taking
  240. two extra arguments, the lower and upper bounds of integration
  241. respectively.
  242. \index{VOLINTEGRAL function} \index{integration ! volume}
  243. Definite volume integrals may be calculated using the {\tt
  244. VOLINTEGRAL} function whose syntax is as follows :
  245. \noindent {\tt VOLINTEGRAL}({\tt integrand}, vector {\tt lower-bound},
  246. vector {\tt upper-bound});
  247. \example\index{AVECTOR package ! example}
  248. \noindent In spherical polar coordinates we may calculate the volume of a
  249. sphere by integrating unity over the range $r$=0 to {\tt RR}, $\theta$=0 to
  250. {\tt PI}, $\phi$=0 to 2*$\pi$ as follows :
  251. \begin{tabular}{l l}
  252. {\tt VLB := AVEC(0,0,0);} & Lower bound \\
  253. {\tt VUB := AVEC(RR,PI,2*PI);} & Upper bound in $r, \theta, \phi$
  254. respectively \\
  255. {\tt VOLINTORDER := (0,1,2);} & The order of integration \\
  256. {\tt VOLINTEGRAL(1,VLB,VUB);} & \\
  257. \end{tabular}
  258. \index{VOLINTORDER vector}
  259. Note the use of the special vector {\tt VOLINTORDER} which controls
  260. the order in which the integrations are carried out. This vector
  261. should be set to contain the number 0, 1 and 2 in the required order.
  262. The first component of {\tt VOLINTORDER} contains the index of the
  263. first integration variable, the second component is the index of the
  264. second integration variable and the third component is the index of
  265. the third integration variable.
  266. \example\index{AVECTOR package ! example}
  267. Suppose we wish to calculate the volume of a right circular cone. This
  268. is equivalent to integrating unity over a conical region with the
  269. bounds:
  270. \begin{tabular}{l l}
  271. z = 0 to H & (H = the height of the cone) \\
  272. r = 0 to pZ & (p = ratio of base diameter to height) \\
  273. phi = 0 to 2*PI & \\
  274. \end{tabular}
  275. We evaluate the volume by integrating a series of infinitesimally thin
  276. circular disks of constant z-value. The integration is thus performed
  277. in the order : d($\phi$) from 0 to $2\pi$, dr from 0 to p*Z, dz from 0 to H.
  278. The order of the indices is thus 2, 0, 1.
  279. \begin{verbatim}
  280. VOLINTORDER := AVEC(2,0,1);
  281. VLB := AVEC(0,0,0);
  282. VUB := AVEC(P*Z,H,2*PI);
  283. VOLINTEGRAL(1,VLB,VUB);
  284. \end{verbatim}
  285. (At this stage, we replace {\tt P*H} by {\tt RR}, the base radius of the cone,
  286. to obtain the result in its more familiar form.)
  287. \index{LINEINT function} \index{DEFLINEINT function}
  288. \index{integration ! line} \index{line integrals}
  289. Line integrals may be calculated using the {\tt LINEINT} and {\tt DEFLINEINT}
  290. functions. Their general syntax is
  291. \noindent {\tt LINEINT}({\tt vector-function}, {\tt vector-curve},
  292. {\tt variable});
  293. \noindent{\tt DEFLINENINT}({\tt vector-function}, {\tt vector-curve},
  294. {\tt variable}, {\tt lower-bound}, {\tt upper-bound});
  295. \noindent where
  296. \begin{description}
  297. \item[{\tt vector-function}] is any vector-valued expression;
  298. \item[{\tt vector-curve}] is a vector expression which describes the path of
  299. integration in terms of the independent variable;
  300. \item[{\tt variable}] is the independent variable;
  301. \item[{\tt lower-bound}]
  302. \item[{\tt upper-bound}] are the bounds of integration in terms of the
  303. independent variable.
  304. \end{description}
  305. \example\index{AVECTOR package ! example}
  306. In spherical polar coordinates, we may integrate round a line of
  307. constant theta (`latitude') to find the length of such a line. The
  308. vector function is thus the tangent to the `line of latitude', (0,0,1)
  309. and the path is {\tt (0,LAT,PHI)} where {\tt PHI} is the independent
  310. variable. We show how to obtain the definite integral {\em i.e.} from
  311. $\phi=0$ to $2 \pi$ :
  312. \begin{verbatim}
  313. DEFLINEINT(AVEC(0,0,1),AVEC(0,LAT,PHI),PHI,0,2*PI);
  314. \end{verbatim}
  315. \section{Defining new functions and procedures}
  316. Most of the procedures in this package are defined in symbolic mode
  317. and are invoked by the REDUCE expression-evaluator when a vector
  318. expression is encountered. It is not generally possible to define
  319. procedures which accept or return vector values in algebraic mode.
  320. This is a consequence of the way in which the REDUCE interpreter
  321. operates and it affects other non-scalar data types as well : arrays
  322. cannot be passed as algebraic procedure arguments, for example.
  323. \section{Acknowledgements}
  324. This package was written whilst the author was the U.K. Computer
  325. Algebra Support Officer at the University of Liverpool Computer Laboratory.
  326. \end{document}