avector.tex 13 KB

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