next up previous contents
Next: General size vector subtraction Up: General size vectors Previous: Copying a general size   Contents

General size vector addition

To add two vectors ${\bf x}$ and ${\bf y}$ together, obtaining the sum ${\bf z}= {\bf x}+ {\bf y}$, use the routine
      Gan_Vector vx, vy, vz; /* declare vectors x, y and z */

      /* ... create and fill vx & vy, create vz ... */
      gan_vec_add_q ( &vx, &vy, &vz ); /* compute z = x + y */
Again vector ${\bf z}$ is reallocated if necessary. Vectors ${\bf x}$ and ${\bf y}$ must of course be the same size, or the error handler is invoked and NULL is returned. The sum vector ${\bf z}$ may be create from scratch using
      Gan_Vector *pvz; /* declare vector z as pointer */

      /* ... create and fill vx & vy ... */
      pvz = gan_vec_add_s ( &vx, &vy ); /* compute z = x + y */
Another way of computing vector addition is to replace one of the input vector ${\bf x}$ or ${\bf y}$ with the result, using one of the in-place routines
      gan_vec_add_i1 ( &vx, &vy ); /* replace x = x + y */
      gan_vec_add_i2 ( &vx, &vy ); /* replace y = x + y */
An alternative to gan_vec_add_i1() is the more explicit routine
      gan_vec_increment ( &vx, &vy ); /* replace x = x + y */

Error detection: NULL is returned and the Gandalf error handler invoked if the vector addition fails. The most likely failure modes are failing to create/set the result vector, or size incompatibility between the input vectors.



2006-03-17