Forum archive 2000-2006

Michal Charemza - Extract Components of Vector

Michal Charemza - Extract Components of Vector

by Arnold Pizer -
Number of replies: 0
inactiveTopicExtract Components of Vector topic started 9/2/2006; 12:17:11 PM
last post 9/3/2006; 11:17:04 AM
userMichal Charemza - Extract Components of Vector  blueArrow
9/2/2006; 12:17:11 PM (reads: 279, responses: 2)
Hi,

Is it possible to extract the compenents of a vector. For example, if I create a vector by

$myVector = Vector("<1,2>");

How can I later access the first and second components of $myVector?

Michal.

<| Post or View Comments |>


userDavide P. Cervone - Re: Extract Components of Vector  blueArrow
9/2/2006; 12:29:49 PM (reads: 349, responses: 0)
You do this via the "extract" method:
   $V = Vector("<1,2>");
$x = $V->extract(1);
$y = $V->extract(2);
This only works for constant vectors, however, not vector formulas (since the formula could be produced from a cross product or something so that it would not be straight-forward to obtain the coordinates separately). If you are interested in COMPUTING the value rather than displaying the formula, however, you could use dot products to obtain the value. For example,
  $V = Vector("<1,x>");
$x = $V . Vector(1,0);
$y = $V . Vector(0,1);
will make $x and $y be formulas that compute the first and second coordinates. (Note that you can give vectors by lists of coordinates rather than strings, and you can even do $V = Vector(1,"x").) Of course, this would also work for constant vectors as well as formula vectors.

Alternatively, you could get both coordinates of a constant vector at once,

  $V = Vector("<1,2>");
($x,$y) = $V->value;
since the "value" method returns an array of coordinates.

Davide

<| Post or View Comments |>


userMichal Charemza - Re: Extract Components of Vector  blueArrow
9/3/2006; 11:17:04 AM (reads: 334, responses: 0)
> You do this via the "extract" method:

Thanks - this works.

<| Post or View Comments |>