/*****************************************************************************
 *                                                                           *
 * 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.                                    *
 *                                                                           *
 *****************************************************************************/

/*

Given a sequence of scalar values e0,e1,e2,...eT (time step `h' apart) this
class lets you compute the integral (sum)

	delta(t) = sum (i=t...T) e(i) * C * A^(i-t)

where C is a 1*ny matrix and A is an ny*ny matrix.
This is a kind of "backwards" filter.
A buffer of `n' values is maintained, which limits the value of T-t to
at most n-2.

`ny' is the size of the Fox system y vector.
The maximum value of `ny' is currently 4

*/

#ifndef __TRACE_N_H
#define __TRACE_N_H

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


class TraceN {
  int valid;			// set to 1 if constructor was successfull
  int ny;
  cftype *C;			// array of 1*ny matrix values (constant)
  cftype *A;			// array of ny*ny matrix values (constant)
  int n;			// size of buffer
  cftype *lambda;		// buffer of n ny-sized vectors (size=n*ny)
  int T;			// current time step (starts at 0)
  int p;			// position of current timestep in buffer
  int T0;			// time step at lambda[0]
  cftype Lambda[MAX_NY];	// current integrated value of (e * C * A^i)
  cftype CAtop[MAX_NY];		// current value of C*A^p (1*ny)
  GetAtoi *atoi;

public:
  TraceN (int _ny, cftype *_A, cftype *_C, int _n, GetAtoi *_atoi);
    // Initialize for a given ny, matrices A and C, and buffer size n.
    // The A and C matrices are referenced but not deallocated by this class.
    // If initialization is unsuccessful in user mode a fatal error occurrs.
    // If initialization is unsuccessful in kernel mode then the constructor
    // returns and Valid() returns 0. The object can be safely destructed.
  ~TraceN();

  int Valid() { return valid; }

  void Reset();
    // Reset to time 0.

  void Next (cftype e);
    // Set the next error e(i) value. The first time this is called is for
    // time 0.

  cftype * GetDelta (int t);
    // Return an ny sized vector (\delta_t). The returned pointer points into
    // a static storage area. This must not be called for more than n
    // timesteps ago.
};


#endif
