MathGroup Archive 1998

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Vectors and Mathematica


  • To: mathgroup@smc.vnet.net
  • Subject: [mg11784] Re: Vectors and Mathematica
  • From: tburton@brahea.com (Tom Burton)
  • Date: Sat, 28 Mar 1998 00:25:20 -0500
  • Organization: Brahea, Inc.
  • References: <6fd5vi$6bu@smc.vnet.net>

On 26 Mar 1998 04:09:06 -0500, in comp.soft-sys.math.mathematica you
wrote:

>I'm really lost here. It seems there is almost NO support of simple
>vectors in Mathematica v.3.0
>If I want to add 2 vectors of which I have a polar (cylindrical)
>representation I am forced to do something like
>
><< Calculus`VectorAnalysis`
>SetCoordinates[Cylindrical]
>a±00, -115*Pi/180, 0}
>b²00, -30*Pi/180, 0}
>AÈordinatesToCartesian[a, Cylindrical] BÈordinatesToCartesian[b,
>Cylindrical] d«B
>DÈordinatesFromCartesian[d, Cylindrical]
>
>and that is a HECK longer to do then by hand. There's got to be
>something simplier like:
>{100, -115*Pi/180, 0} + {200, -30*Pi/180, 0}
>
>or something...
>
>Also how do I find a magnitude of a vector. The Abs[] doesn't support
>this. Sqrt[d[[1]]^2+d[[2]]^2+d[[3]]^2]
>
>is simply hedious!!!
>
>Please help cause I'm really lost ;(
>
>Michael
>
>P.S. If possible, please also send a direct email to
>mmichael@idirect.com
>
Taking the second issue first, the length of a vector v is equal to
Sqrt[DotProduct[v,v]]. DotProduct is a function built into the package
Calculus`VectorAnalysis`, so you're home free there.

There seems to be no similar support for the sum of two vectors. Once
you understand a little about how packages work, it's easy to extend
that package to support this new function. Below is a small v. 3
notebook showing how to add a new function VectorSum, a trival
modification of DotProduct.

There are many holes is the functionality provided by the core and
standard packages. The transparency of Mathematica partially makes up
for a lot of these deficiencies.

Tom Burton           Brahea, Inc.       tburton@brahea.com

(***********************************************************************

                    Mathematica-Compatible Notebook

This notebook can be used on any computer system with Mathematica 3.0,
MathReader 3.0, or any compatible application. The data for the
notebook

starts with the line of stars above.

To get the notebook into a Mathematica-compatible application, do one of

the following:

* Save the data starting with the line of stars above into a file
  with a name ending in .nb, then open the file inside the application;

* Copy the data starting with the line of stars above to the
  clipboard, then use the Paste menu command inside the application.

Data for notebooks contains only printable 7-bit ASCII and can be sent
directly in email or through ftp in text mode.  Newlines can be CR, LF
or CRLF (Unix, Macintosh or MS-DOS style).

NOTE: If you modify the data for this notebook not in a Mathematica-
compatible application, you must delete the line below containing
the word CacheID, otherwise Mathematica-compatible applications may
try to use invalid cache data.

For more information on notebooks and Mathematica-compatible
applications, contact Wolfram Research:
  web: http://www.wolfram.com
  email: info@wolfram.com
  phone: +1-217-398-0700 (U.S.)

Notebook reader applications are available free of charge from
Wolfram Research.
***********************************************************************)


(*NotebookFileLineBreakTest
NotebookFileLineBreakTest*)
(*NotebookOptionsPosition[     10118,        352]*)
(*NotebookOutlinePosition[     10767,        375]*) (*
CellTagsIndexPosition[     10723,        371]*) (*WindowFrame->Normal*)


Notebook[{
Cell["Load the package we want to extend:", "Text"],

Cell[BoxData[
    \(<< Calculus`VectorAnalysis`\)], "Input"],

Cell["\<\
What we want is a function similar to DotProduct, but that forms a sum:\
\>", "Text"],

Cell[CellGroupData[{

Cell[BoxData[
    \(\(?DotProduct\)\)], "Input"],

Cell[BoxData[
    \("DotProduct[v1, v2] gives the dot product (sometimes called inner
\
product or scalar product) of the two vectors v1, v2 in three space in
the \
default coordinate system.  DotProduct[v1, v2, coordsys] gives the dot \
product of v1 and v2 in the coordinate system coordsys."\)], "Print"]
}, Open  ]],

Cell["Get the context of this function,", "Text"],

Cell[CellGroupData[{

Cell[BoxData[
    \(Context[DotProduct]\)], "Input"],

Cell[BoxData[
    \("Calculus`VectorAnalysis`"\)], "Output"] }, Open  ]],

Cell["and go there:", "Text"],

Cell[CellGroupData[{

Cell[BoxData[
    \(BeginPackage["\<Calculus`VectorAnalysis`\>"]\)], "Input"],

Cell[BoxData[
    \("Calculus`VectorAnalysis`"\)], "Output"] }, Open  ]],

Cell["\<\
Let's call our new function VectorSum. We'll pattern the usage after \
DotProduct:\
\>", "Text"],

Cell[BoxData[
    \(\(VectorSum::"\<usage\>"       "\<VectorSum[v1, v2] gives the sum of the two vectors v1, v2 in
three \
space in the default coordinate system.  VectorSum[v1, v2, coordsys]
gives \
the sum of v1 and v2 in the coordinate system coordsys.\>"; \)\)],
"Input"],

Cell["\<\
We'll pattern the actual function after DotProduct, too. To see it, we
need \
to clear the read-lock:\
\>", "Text"],

Cell[BoxData[
    \(ClearAttributes[DotProduct, ReadProtected]\)], "Input"],

Cell["\<\
Now let's enter the Private subcontext, where we will want to add the
new \
function:\
\>", "Text"],

Cell[CellGroupData[{

Cell[BoxData[
    \(Begin["\<`Private`\>"]\)], "Input"],

Cell[BoxData[
    \("Calculus`VectorAnalysis`Private`"\)], "Output"] }, Open  ]],

Cell["Here is our pattern:", "Text"],

Cell[CellGroupData[{

Cell[BoxData[
    \(?? DotProduct\)], "Input"],

Cell[BoxData[
    \("DotProduct[v1, v2] gives the dot product (sometimes called inner
\
product or scalar product) of the two vectors v1, v2 in three space in
the \
default coordinate system.  DotProduct[v1, v2, coordsys] gives the dot \
product of v1 and v2 in the coordinate system coordsys."\)], "Print"],

Cell[BoxData[
    InterpretationBox[
      StyleBox[\(Attributes[DotProduct]\   {Protected}\n\ \n
        DotProduct[\((v1_)\)?$VecQ, \ \((v2_)\)?$VecQ, \
            coordsys_: $CoordinateSystem]\  :
          Module[{cs\   $ExpandCoordSys[coordsys], \ cv1, \ cv2},
\
            cv1\  . \ cv2\  /; \
              cs\   \ $Failed\  && \
                \((cv1\   $CTToCart[v1, \ cs])\)\   \ $Failed\  &&
\
                \((cv2\   $CTToCart[v2, \ cs])\)\   \ $Failed]\),
        ShowStringCharacters->True,
        NumberMarks->True],
      InputForm[
        Definition[ DotProduct]],
      Editable->True,
      AutoDelete->True]], "Print"]
}, Open  ]],

Cell["\<\
There is a small trick, which took me a little while to figure out. We
need \
to clear $CoordinateSystem before defining our function:\ \>", "Text"],

Cell[BoxData[
    \(Clear[$CoordinateSystem]\)], "Input"],

Cell["\<\
Other than that, making VectorSum from DotProduct is easy: change cv1 .
cv2 \
to $CTFromCart[cv1+cv2,cs]. I guessed the existence of CTFromCart from
the \
presence of CTToCart. \
\>", "Text"],

Cell[BoxData[
    \(VectorSum[\((v1_)\)?$VecQ, \ \((v2_)\)?$VecQ, \
        coordsys_:  $CoordinateSystem]\  :
      Module[{cs\   $ExpandCoordSys[coordsys], \ cv1, \
          cv2}, $CTFromCart[\ cv1\  + \ cv2, cs]\  /; \
          cs\   \ $Failed\  && \
            \((cv1\  CTToCart[v1, \ cs])\)\   \ $Failed\  &&

            \((cv2\   $CTToCart[v2, \ cs])\)\   \ $Failed]\)],
"Input"],

Cell["Let's check to see that we wrote what we think we wrote:",
"Text"],

Cell[CellGroupData[{

Cell[BoxData[
    \(?? VectorSum\)], "Input"],

Cell[BoxData[
    \("VectorSum[v1, v2] gives the sum of the two vectors v1, v2 in
three \
space in the default coordinate system.  VectorSum[v1, v2, coordsys]
gives \
the sum of v1 and v2 in the coordinate system coordsys."\)], "Print"],

Cell[BoxData[
    InterpretationBox[
      StyleBox[
        \(VectorSum[\((v1_)\)?$VecQ, \ \((v2_)\)?$VecQ, \
            coordsys_: $CoordinateSystem]\  :
          Module[{cs\   $ExpandCoordSys[coordsys], \ cv1, \
cv2},
            \ $CTFromCart[cv1\  + \ cv2, \ cs]\  /; \
              cs\   \ $Failed\  && \
                \((cv1\   $CTToCart[v1, \ cs])\)\   \ $Failed\  &&
\
                \((cv2\   $CTToCart[v2, \ cs])\)\   \ $Failed]\),
        ShowStringCharacters->True,
        NumberMarks->True],
      InputForm[
        Definition[ VectorSum]],
      Editable->True,
      AutoDelete->True]], "Print"]
}, Open  ]],

Cell["\<\
(Looking at this is how I knew to clear $CoordinateSystem.) Now return
to the \
previous context:\
\>", "Text"],

Cell[CellGroupData[{

Cell[BoxData[
    \(End[]\)], "Input"],

Cell[BoxData[
    \("Calculus`VectorAnalysis`Private`"\)], "Output"] }, Open  ]],

Cell[BoxData[
    \(EndPackage[]\)], "Input"],

Cell["Now let's try it.", "Text"],

Cell[CellGroupData[{

Cell[BoxData[
    \(\(?VectorSum\)\)], "Input"],

Cell[BoxData[
    \("VectorSum[v1, v2] gives the sum of the two vectors v1, v2 in
three \
space in the default coordinate system.  VectorSum[v1, v2, coordsys]
gives \
the sum of v1 and v2 in the coordinate system coordsys."\)], "Print"] },
Open  ]],

Cell[CellGroupData[{

Cell[BoxData[
    \(SetCoordinates[Cartesian[]]\)], "Input"],

Cell[BoxData[
    \(Cartesian[Xx, Yy, Zz]\)], "Output"] }, Open  ]],

Cell[CellGroupData[{

Cell[BoxData[
    \(VectorSum[{x1, y1, z1}, {x2, y2, z2}]\)], "Input"],

Cell[BoxData[
    \({x1 + x2, y1 + y2, z1 + z2}\)], "Output"] }, Open  ]],

Cell[CellGroupData[{

Cell[BoxData[
    \(SetCoordinates[Cylindrical[]]\)], "Input"],

Cell[BoxData[
    \(Cylindrical[Rr, Ttheta, Zz]\)], "Output"] }, Open  ]],

Cell[CellGroupData[{

Cell[BoxData[
    \(VectorSum[{r1, t1, z1}, {r2, t2, z2}]\)], "Input"],

Cell[BoxData[
    \({\@\(\((r1\ Cos[t1] + r2\ Cos[t2])\)\^2 +
          \((r1\ Sin[t1] + r2\ Sin[t2])\)\^2\),
      ArcTan[r1\ Cos[t1] + r2\ Cos[t2], r1\ Sin[t1] + r2\ Sin[t2]], z1 +
z2}
      \)], "Output"]
}, Open  ]],

Cell[CellGroupData[{

Cell[BoxData[
    \(SetCoordinates[Spherical[]]\)], "Input"],

Cell[BoxData[
    \(Spherical[Rr, Ttheta, Pphi]\)], "Output"] }, Open  ]],

Cell[CellGroupData[{

Cell[BoxData[
    \(VectorSum[{r1, t1, p1}, {r2, t2, p2}]\)], "Input"],

Cell[BoxData[
    \({\[Sqrt]\((
          \((r1\ Cos[t1] + r2\ Cos[t2])\)\^2 +
            \((r1\ Cos[p1]\ Sin[t1] + r2\ Cos[p2]\ Sin[t2])\)\^2 +
            \((r1\ Sin[p1]\ Sin[t1] + r2\ Sin[p2]\ Sin[t2])\)\^2)\),
      ArcCos[\((r1\ Cos[t1] + r2\ Cos[t2])\)/
          \((\[Sqrt]\((
                \((r1\ Cos[t1] + r2\ Cos[t2])\)\^2 +
                  \((r1\ Cos[p1]\ Sin[t1] + r2\ Cos[p2]\ Sin[t2])\)\^2 +

                  \((r1\ Sin[p1]\ Sin[t1] + r2\ Sin[p2]\
Sin[t2])\)\^2)\))
            \)], ArcTan[r1\ Cos[p1]\ Sin[t1] + r2\ Cos[p2]\ Sin[t2],
        r1\ Sin[p1]\ Sin[t1] + r2\ Sin[p2]\ Sin[t2]]}\)], "Output"] },
Open  ]],

Cell["\<\
Seems like it's working. Now return to the Cylindrical coordinates and
try a \
few simple numerical examples:\
\>", "Text"],

Cell[CellGroupData[{

Cell[BoxData[
    \(SetCoordinates[Cylindrical[]]\)], "Input"],

Cell[BoxData[
    \(Cylindrical[Rr, Ttheta, Zz]\)], "Output"] }, Open  ]],

Cell[CellGroupData[{

Cell[BoxData[
    \(VectorSum[{3. , 1. , 0. }, {1. , 1. , 0. }]\)], "Input"],

Cell[BoxData[
    \({4.`, 1.`, 0.`}\)], "Output"]
}, Open  ]],

Cell[CellGroupData[{

Cell[BoxData[
    \(VectorSum[{1. , \[Pi]/4, 0. }, {1. , \(-\[Pi]\)/4, 0. }]\)],
"Input"],

Cell[BoxData[
    \({1.41421356237309492`, 0``307.8032, 0.`}\)], "Output"] }, Open
]],

Cell["\<\
If you like the way this is working, it's easy to add the definitions
above \
to appropriate places in
Addons:StandardPackages:Calculus:VectorAnalysis.m.\ \>", "Text"],

Cell[TextData[{
  "Note that I was able to figure all of this out without even visiting
the \
package. The kernel told me everything I needed to know. This
transparency is \
one of my favorite things about ",
  StyleBox["Mathematica",
    FontSlant->"Italic"],
  "."
}], "Text"]
},
FrontEndVersion->"Microsoft Windows 3.0", ScreenRectangle->{{0, 1280},
{0, 992}}, WindowSize->{482, 725},
WindowMargins->{{0, Automatic}, {Automatic, 5}} ]


(***********************************************************************
Cached data follows.  If you edit this Notebook file directly, not
using Mathematica, you must remove the line containing CacheID at the
top of the file.  The cache data will then be recreated when you
save this file

from within Mathematica.
***********************************************************************)

(*CellTagsOutline
CellTagsIndex->{}
*)

(*CellTagsIndex
CellTagsIndex->{}
*)

(*NotebookFileOutline
Notebook[{
Cell[1709, 49, 51, 0, 33, "Text"],
Cell[1763, 51, 60, 1, 30, "Input"],
Cell[1826, 54, 95, 2, 33, "Text"],

Cell[CellGroupData[{
Cell[1946, 60, 48, 1, 30, "Input"],
Cell[1997, 63, 307, 4, 101, "Print"] }, Open  ]],
Cell[2319, 70, 49, 0, 33, "Text"],

Cell[CellGroupData[{
Cell[2393, 74, 52, 1, 30, "Input"],
Cell[2448, 77, 60, 1, 29, "Output"]
}, Open  ]],
Cell[2523, 81, 29, 0, 33, "Text"],

Cell[CellGroupData[{
Cell[2577, 85, 77, 1, 30, "Input"],
Cell[2657, 88, 60, 1, 29, "Output"]
}, Open  ]],
Cell[2732, 92, 106, 3, 33, "Text"],
Cell[2841, 97, 278, 4, 110, "Input"], Cell[3122, 103, 125, 3, 52,
"Text"], Cell[3250, 108, 75, 1, 30, "Input"], Cell[3328, 111, 109, 3,
33, "Text"],

Cell[CellGroupData[{
Cell[3462, 118, 55, 1, 30, "Input"], Cell[3520, 121, 68, 1, 29,
"Output"] }, Open  ]],
Cell[3603, 125, 36, 0, 33, "Text"],

Cell[CellGroupData[{
Cell[3664, 129, 46, 1, 30, "Input"], Cell[3713, 132, 307, 4, 101,
"Print"], Cell[4023, 138, 676, 15, 158, "Print"] }, Open  ]],
Cell[4714, 156, 158, 3, 52, "Text"], Cell[4875, 161, 57, 1, 30,
"Input"], Cell[4935, 164, 201, 4, 71, "Text"], Cell[5139, 170, 417, 7,
130, "Input"], Cell[5559, 179, 72, 0, 33, "Text"],

Cell[CellGroupData[{
Cell[5656, 183, 45, 1, 30, "Input"], Cell[5704, 186, 236, 3, 82,
"Print"], Cell[5943, 191, 647, 15, 120, "Print"] }, Open  ]],
Cell[6605, 209, 121, 3, 52, "Text"],

Cell[CellGroupData[{
Cell[6751, 216, 38, 1, 30, "Input"], Cell[6792, 219, 68, 1, 29,
"Output"] }, Open  ]],
Cell[6875, 223, 45, 1, 30, "Input"], Cell[6923, 226, 33, 0, 33, "Text"],

Cell[CellGroupData[{
Cell[6981, 230, 47, 1, 30, "Input"], Cell[7031, 233, 236, 3, 82,
"Print"] }, Open  ]],

Cell[CellGroupData[{
Cell[7304, 241, 60, 1, 30, "Input"], Cell[7367, 244, 55, 1, 29,
"Output"] }, Open  ]],

Cell[CellGroupData[{
Cell[7459, 250, 70, 1, 30, "Input"], Cell[7532, 253, 61, 1, 29,
"Output"] }, Open  ]],

Cell[CellGroupData[{
Cell[7630, 259, 62, 1, 30, "Input"], Cell[7695, 262, 61, 1, 29,
"Output"] }, Open  ]],

Cell[CellGroupData[{
Cell[7793, 268, 70, 1, 30, "Input"], Cell[7866, 271, 209, 4, 82,
"Output"] }, Open  ]],

Cell[CellGroupData[{
Cell[8112, 280, 60, 1, 30, "Input"], Cell[8175, 283, 61, 1, 29,
"Output"] }, Open  ]],

Cell[CellGroupData[{
Cell[8273, 289, 70, 1, 30, "Input"], Cell[8346, 292, 628, 11, 191,
"Output"] }, Open  ]],
Cell[8989, 306, 133, 3, 52, "Text"],

Cell[CellGroupData[{
Cell[9147, 313, 62, 1, 30, "Input"], Cell[9212, 316, 61, 1, 29,
"Output"] }, Open  ]],

Cell[CellGroupData[{
Cell[9310, 322, 76, 1, 30, "Input"], Cell[9389, 325, 49, 1, 29,
"Output"] }, Open  ]],

Cell[CellGroupData[{
Cell[9475, 331, 89, 1, 30, "Input"], Cell[9567, 334, 74, 1, 29,
"Output"] }, Open  ]],
Cell[9656, 338, 177, 3, 52, "Text"], Cell[9836, 343, 278, 7, 71, "Text"]
}
]
*)



(***********************************************************************
End of Mathematica Notebook file.
***********************************************************************)


Tom Burton        Brahea, Inc.       619/436-7436



  • Prev by Date: Re: Plots from Previous Session
  • Next by Date: new website on Mathematica graphics
  • Prev by thread: Re: Vectors and Mathematica
  • Next by thread: vectors in polar coordinates