BaseDiscreteStaticSystem.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.ComplexNumericalMatrix;
import org.mklab.nfc.matrix.RealNumericalMatrix;
import org.mklab.nfc.ode.SolverStopException;
import org.mklab.nfc.scalar.ComplexNumericalScalar;
import org.mklab.nfc.scalar.RealNumericalScalar;
import org.mklab.tool.control.system.SystemOperator;
import org.mklab.tool.control.system.sampled.Sampler;


/**
 * 差分方程式で表現される離散時間静的システムを表わすクラスです。
 * 
 * @author koga
 * @version $Revision: 1.12 $
 * @param <RS> type of real scalar
 * @param <RM> type of real matrix
 * @param <CS> type of complex scalar
 * @param <CM> type of complex matrix
 */
public abstract class BaseDiscreteStaticSystem<RS extends RealNumericalScalar<RS,RM,CS,CM>, RM extends RealNumericalMatrix<RS,RM,CS,CM>, CS extends ComplexNumericalScalar<RS,RM,CS,CM>, CM extends ComplexNumericalMatrix<RS,RM,CS,CM>> extends SystemOperator<RS,RM,CS,CM> implements DiscreteStaticSystem<RS,RM,CS,CM>, Sampler<RS,RM,CS,CM> {

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

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

  /**
   * 新しく生成された<code>BaseDiscreteStaticSystem</code>オブジェクトを初期化します。
   * 
   * @param inputSize 入力の数
   * @param outputSize 出力の数
   * @param sunit unit of scalar
   */
  public BaseDiscreteStaticSystem(final int inputSize, final int outputSize, RS sunit) {
    super(sunit);
    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 RS getSamplingInterval() {
    return this.samplingInterval;
  }

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

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

  /**
   * {@inheritDoc}
   */
  public RM outputEquation(final RS t, final RM u) throws SolverStopException {
    int k = (int)t.divide(this.samplingInterval).ceil().toDouble();

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

    if (!isAtSamplingPoint() && (t.subtract(this.samplingInterval.multiply(k)).isLessThan(0))) {
      k = k - 1;
    }

    this.previousOutput = outputEquation(k, u);

    return this.previousOutput.createClone();
  }

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

  /**
   * {@inheritDoc}
   */
  public RM outputEquation(final RS t) throws SolverStopException {
    int k = (int)t.divide(this.samplingInterval).ceil().toDouble();

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

    if (!isAtSamplingPoint() && (t.subtract(this.samplingInterval.multiply(k)).isLessThan(0))) {
      k = k - 1;
    }

    this.previousOutput = outputEquation(k);

    return this.previousOutput.createClone();
  }

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

    final RM u = this.sunit.createZeroGrid(getInputSize(), 1);
    final RM y = outputEquation(k);
    return u.appendDown(y);
  }

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

    final RM u = this.sunit.createZeroGrid(getInputSize(), 1);
    final RM 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 RS getNextSamplingTime(final RS t, final RS tolerance) {
    RS nextSamplingTime = t.divide(this.samplingInterval).ceil().multiply(this.samplingInterval);
    if (t.subtract(nextSamplingTime).abs().isLessThan(tolerance)) {
      nextSamplingTime = t.add(this.samplingInterval);
    }
    
    return nextSamplingTime;
  }

  /**
   * @see org.mklab.tool.control.system.SystemOperator#hashCode()
   */
  @Override
  public int hashCode() {
    int hashCode = super.hashCode();
    hashCode = 31 * hashCode + (this.samplingInterval.hashCode()  ^ (this.samplingInterval.hashCode() >>> 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;
    }
    BaseDiscreteStaticSystem<RS,RM,CS,CM> castedObj = (BaseDiscreteStaticSystem<RS,RM,CS,CM>)o;
    return ((this.samplingInterval == castedObj.samplingInterval) && (this.atSamplingPoint == castedObj.atSamplingPoint));
  }

}