MathGroup Archive 1995

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

Search the Archive

Re: Re: Oh why is life never easy? Need help with |vector|

  • To: mathgroup at smc.vnet.net
  • Subject: [mg2811] Re: [mg2798] Re: Oh why is life never easy? Need help with |vector|
  • From: brucec (Bruce Carpenter)
  • Date: Tue, 19 Dec 1995 02:41:33 -0500

>In article <4ar5ut$2hq at dragonfly.wri.com>,
>ULI WORTMANN <ULI12 at HOMER.geol.chemie.tu-muenchen.de> wrote:
>>Hi All,
>>
>>I need to calculate v0 = v / |v| where v is a vector of size 3.
>>However, |v|, which is a scalar, is for some reason (I don't
>>understand) treated like a vector (at least VectorQ[|v|] gives
>>TRUE). Obviously, it is impossible to divide two vectors of different
>>length.
>>
>> |v| is defined as:
>>       absvec[u_] :=
>>               Module[{a},
>>               {a = Sqrt[(u[[1]]^2+u[[2]]^2+u[[3]]^2)]}]
>
>The problem is that the body of your calculation is wrapped in list
>braces, so the result comes back wrapped in list braces, which is
>a vector of length 1.
>
>Anyway, a much better way to calculate the magnitude of a vector is
>like this:
>
>    absvec[u_] := Sqrt[Plus @@ (u^2)]
>
>This function works on vectors of any length.
>
>                Dave Wagner
>                Principia Consulting
>                (303) 786-8371
>                dbwagner at princon.com
>                http://www.princon.com/princon

Another way to do it would be

     absvec[u_?VectorQ] := Sqrt[u.u]

which uses Mathematica's built-in Dot product--faster for large vectors and
a little clearer mathematically.
(This isn't meant to be a criticism of Dave's solution :-)

Cheers,
Bruce Carpenter




  • Prev by Date: Distribute
  • Next by Date: Re: Help with lists
  • Previous by thread: Re: Oh why is life never easy? Need help with |vector|
  • Next by thread: Data manipulation in Mathematica