/*****************************************************************************
 *                                                                           *
 * FOX Controller Library, Version 1.3.3.                                    *
 *                                                                           *
 * Copyright (C) 1998,1999,2000 Russell Smith (rl.smith@auckland.ac.nz)      *
 *                                                                           *
 * The FOX Controller Library is free software; you can redistribute it      *
 * and/or modify it under the terms of the GNU Library General Public        *
 * License as published by the Free Software Foundation; either version      *
 * 2 of the License, or (at your option) any later version.                  *
 *                                                                           *
 * The FOX Controller Library is distributed in the hope that it will be     *
 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of    *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         *
 * Library General Public License for more details.                          *
 *                                                                           *
 * You should have received a copy of the GNU Library General Public         *
 * License along with the Fox Controller Library; see the file COPYING.LIB.  *
 * If not, write to the Free Software Foundation, Inc., 59 Temple Place -    *
 * Suite 330, Boston, MA 02111-1307, USA.                                    *
 *                                                                           *
 *****************************************************************************/

#ifndef __FOXSTUFF_H
#define __FOXSTUFF_H

#include "cftype-n.h"
#include "error.h"

//***************************************************************************
// Assertions and errors

#ifndef NDEBUG
#define myassert(condition,text,args...) \
  if (!(condition)) ZFatalDebug ("FoxN: " text, ## args);
#else
#define myassert(condition,text,args...) /* */
#endif

#define checkthat(condition,text,args...) \
  if (!(condition)) ZFatalDebug ("FoxN: " text, ## args);
#define checkthat2(condition,text,args...) \
  if (!(condition)) ZFatalDebug ("FoxN: " text, ## args);
#define checkmemptr(p) \
  if ((p)==0) ZFatalError ("FoxN: Out of memory!\n");

//***************************************************************************
// Matrix calculations

// Calculate  a = b*C  where `a' and `b' are 1*n and C is n*n
inline void MatrixMultiply1 (int n, cftype *a, cftype *b, cftype *C)
{
  cftype sum,*Cptr;
  for (int i=0; i<n; i++) {
    sum = 0.0;
    Cptr = C + i;
    for (int j=0; j<n; j++) {
      sum += b[j] * (*Cptr);
      Cptr += n;
    }
    a[i] = sum;
  }
}


// Calculate  a = B*c  where `a' and `c' are n*1 and B is n*n
inline void MatrixMultiply2 (int n, cftype *a, cftype *B, cftype *c)
{
  cftype sum;
  for (int i=n; i; i--) {
    sum = 0.0;
    for (int j=0; j<n; j++) sum += (*B++) * c[j];
    (*a++) = sum;
  }
}

//***************************************************************************

#endif
