/* Copyright (c) 2001-2004 by SoftIntegration, Inc. All Rights Reserved */
/* summary.c 
   this case is for the C function which takes
   arguments of double * (one dimensions) to represent arrays
   of different dimension, as well as information about the 
   array including dimensions and extents.
*/
#include <ch.h>
#include <stdio.h>

double summary(double *aa, int dim, int *ext);

EXPORTCH double summary_chdl(void *varg) {
  ChInterp_t interp;
  ChVaList_t ap;
  double *a;
  int dim, *ext;
  double retval = 0;

  Ch_VaStart(interp, ap, varg);
 
  a = Ch_VaArg(interp, ap, double *);
  dim = Ch_VaArg(interp, ap, int);
  ext = Ch_VaArg(interp, ap, int*);

  retval = summary(a, dim, ext);

  Ch_VaEnd(interp, ap);
  return retval;
}

double summary(double *aa, int dim, int *ext) {
    int i, totnum = 1;
    double sums = 0;

    for(i = 0; i < dim; i++) {
      totnum = totnum * ext[i];
    }
    for(i = 0; i < totnum; i++) {
      sums = sums + aa[i];
    }
    return sums;
}
