DoubleBaseDiscreteStaticSystem.java

/*
 * $Id: BaseDiscreteStaticSystem.java,v 1.12 2008/06/30 08:29:30 koga Exp $
 *
 * Copyright (C) 2004-2005 Koga Laboratory. All rights reserved.
 */

package org.mklab.tool.control.system.discrete;

import org.mklab.nfc.matrix.DoubleMatrix;
import org.mklab.nfc.ode.SolverStopException;
import org.mklab.tool.control.system.DoubleSystemOperator;
import org.mklab.tool.control.system.sampled.DoubleSampler;


/**
 * 差分方程式で表現される離散時間静的システムを表わすクラスです。
 * 
 * @author koga
 * @version $Revision: 1.12 $
 */
public abstract class DoubleBaseDiscreteStaticSystem extends DoubleSystemOperator implements DoubleDiscreteStaticSystem, DoubleSampler {

  /** サンプリング周期 */
  private double samplingInterval;
  /** サンプリング点ならばtrue */
  private boolean atSamplingPoint;

  /** 前サンプリング点での出力 */
  private DoubleMatrix previousOutput;

  /**
   * 新しく生成された<code>BaseDiscreteStaticSystem</code>オブジェクトを初期化します。
   * 
   * @param inputSize 入力の数
   * @param outputSize 出力の数
   */
  public DoubleBaseDiscreteStaticSystem(final int inputSize, final int outputSize) {
    super();
    setInputSize(inputSize);
    setOutputSize(outputSize);
    setDynamic(false);
    if (inputSize == 0) {
      setForcedSystem(false);
    }
  }

  /**
   * @see org.mklab.tool.control.system.SystemOperator#initialize()
   */
  @Override
  public void initialize() {
    this.atSamplingPoint = false;
  }

  /**
   * @see org.mklab.tool.control.system.sampled.Sampler#getSamplingInterval()
   */
  public double getSamplingInterval() {
    return this.samplingInterval;
  }

  /**
   * {@inheritDoc}
   */
  public void setSamplingInterval(final double interval) {
    this.samplingInterval = interval;
  }

  /**
   * {@inheritDoc}
   */
  public DoubleMatrix outputEquation( final int k,  final DoubleMatrix u) throws SolverStopException {
    throw new SolverStopException(Messages.getString("BaseDiscreteStaticSystem.0")); //$NON-NLS-1$
  }

  /**
   * {@inheritDoc}
   */
  public DoubleMatrix outputEquation(final double t, final DoubleMatrix u) throws SolverStopException {
    int k = (int)Math.ceil(t / this.samplingInterval);

    if (!isAtSamplingPoint() && this.previousOutput != null) {
      return this.previousOutput.createClone();
    }

    if (!isAtSamplingPoint() && (t - k * this.samplingInterval < 0)) {
      k = k - 1;
    }

    this.previousOutput = outputEquation(k, u);

    return this.previousOutput.createClone();
  }

  /**
   * @see org.mklab.tool.control.system.discrete.DiscreteStaticSystem#outputEquation(int)
   */
  public DoubleMatrix outputEquation( final int k) throws SolverStopException {
    throw new SolverStopException(Messages.getString("BaseDiscreteStaticSystem.1")); //$NON-NLS-1$
  }

  /**
   * {@inheritDoc}
   */
  public DoubleMatrix outputEquation(final double t) throws SolverStopException {
    int k = (int)Math.ceil(t / this.samplingInterval);

    if (!isAtSamplingPoint() && this.previousOutput != null) {
      return this.previousOutput.createClone();
    }

    if (!isAtSamplingPoint() && (t - k * this.samplingInterval < 0)) {
      k = k - 1;
    }

    this.previousOutput = outputEquation(k);

    return this.previousOutput.createClone();
  }

  /**
   * @see org.mklab.nfc.ode.DiscreteAlgebraicSystem#inputOutputEquation(int)
   */
  public DoubleMatrix inputOutputEquation(final int k) throws SolverStopException {
    if (hasDirectFeedthrough()) {
      throw new RuntimeException(Messages.getString("BaseDiscreteStaticSystem.2")); //$NON-NLS-1$
    }

    final DoubleMatrix u = new DoubleMatrix(getInputSize(), 1);
    final DoubleMatrix y = outputEquation(k);
    return u.appendDown(y);
  }

  /**
   * {@inheritDoc}
   */
  public DoubleMatrix inputOutputEquation(final double t) throws SolverStopException {
    if (hasDirectFeedthrough()) {
      throw new RuntimeException(Messages.getString("BaseDiscreteStaticSystem.3")); //$NON-NLS-1$
    }

    final DoubleMatrix u = new DoubleMatrix(getInputSize(), 1);
    final DoubleMatrix y = outputEquation(t);
    return u.appendDown(y);
  }

  /**
   * @see org.mklab.nfc.ode.Sampling#isAtSamplingPoint()
   */
  public boolean isAtSamplingPoint() {
    return this.atSamplingPoint;
  }

  /**
   * @see org.mklab.nfc.ode.Sampling#setAtSamplingPoint(boolean)
   */
  public void setAtSamplingPoint(final boolean samplingPoint) {
    this.atSamplingPoint = samplingPoint;
  }

  /**
   * {@inheritDoc}
   */
  public double getNextSamplingTime(final double t, final double tolerance) {
    double nextSamplingTime = Math.ceil(t / this.samplingInterval) * this.samplingInterval;
    if (Math.abs(t - nextSamplingTime) < tolerance) {
      nextSamplingTime = t + this.samplingInterval;
    }

    return nextSamplingTime;
  }

  /**
   * @see org.mklab.tool.control.system.SystemOperator#hashCode()
   */
  @Override
  public int hashCode() {
    int hashCode = super.hashCode();
    hashCode = 31 * hashCode + (int)(Double.doubleToLongBits(this.samplingInterval) ^ (Double.doubleToLongBits(this.samplingInterval) >>> 32));
    hashCode = 31 * hashCode + (this.atSamplingPoint ? 1231 : 1237);
    return hashCode;
  }

  /**
   * @see org.mklab.tool.control.system.SystemOperator#equals(java.lang.Object)
   */
  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (!super.equals(o)) {
      return false;
    }
    if (o == null) {
      return false;
    }
    if (o.getClass() != getClass()) {
      return false;
    }
    DoubleBaseDiscreteStaticSystem castedObj = (DoubleBaseDiscreteStaticSystem)o;
    return ((this.samplingInterval == castedObj.samplingInterval) && (this.atSamplingPoint == castedObj.atSamplingPoint));
  }

}