next up previous contents
Next: General size vector addition Up: General size vectors Previous: Accessing the elements of   Contents

Copying a general size vector

To copy a vector ${\bf x}$ to another vector ${\bf y}$, both vectors must have been created, and ${\bf x}$ should be filled with values. ${\bf y}$ can be created with arbitrary initial size (for instance zero), since Gandalf will if necessary reallocate ${\bf y}$ to the same size as ${\bf x}$ if necessary. So for instance the following code is perfectly valid:
      Gan_Vector vx, vy; /* declare vectors x & y */

      gan_vec_form ( &vx, 0 ); /* create vector x */
      gan_vec_form ( &vy, 0 ); /* create vector y */
      gan_vec_fill_va ( &vx, 5, 11.0, 9.0, 7.0, 5.0, 3.0 ); /* reallocate & initialise x */
      gan_vec_copy_q ( &vx, &vy ); /* set y = x, reallocating y */
The last two lines reallocate first ${\bf x}$ and then ${\bf y}$, because both were created with zero size. Note that ${\bf y}$ may have previously been filled with other values, which are now lost.

There is also a version that creates a copy of a vector from scratch:

      Gan_Vector *pvy; /* declare vector y */

      pvy = gan_vec_copy_s ( &vx ); /* create y and set y = x */

Error detection: The vector copy routines return NULL and invoke the Gandalf error handler upon failure.



2006-03-17