BaseDiscreteDynamicSystem.java

/*
 * $Id: BaseDiscreteDynamicSystem.java,v 1.16 2008/06/30 08:29:30 koga Exp $
 *
 * Copyright (C) 2004 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 Laboratory
 * @version $Revision: 1.16 $, 2004/11/09
 * @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 BaseDiscreteDynamicSystem<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 DiscreteDynamicSystem<RS,RM,CS,CM>, Sampler<RS,RM,CS,CM> {

  /** 初期状態 */
  private RM initialState;
  /** 状態 */
  private RM state;

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

  /** 結合システムにおける状態の順番 */
  private int stateNumber;

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

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

    this.initialState = this.sunit.createZeroGrid(stateSize, 1);
    this.state = this.initialState.createClone();
  }

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

  /**
   * 初期状態を設定します。
   * 
   * @param initialState 初期状態
   */
  public void setInitialState(final RM initialState) {
    this.initialState = initialState;
  }

  /**
   * 初期状態を返します。
   * 
   * @return 初期状態
   */
  public RM getInitialState() {
    return this.initialState.createClone();
  }

  /**
   * 現在の状態を返します。
   * 
   * @return 現在の状態
   */
  public RM getState() {
    return this.state.createClone();
  }

  /**
   * 現在の状態の設定します。
   * 
   * @param state 現在の状態
   */
  public void setState(final RM state) {
    this.state = state;
  }

  /**
   * {@inheritDoc}
   */
  public RM differenceEquation(final int k, final RM x, final RM inputOutput) throws SolverStopException {
    final RM u = inputOutput.getRowVectors(1, getInputSize());
    return stateEquation(k, x, u);
  }

  /**
   * {@inheritDoc}
   */
  public RM differenceEquation(final RS t, final RM x, final RM inputOutput) throws SolverStopException {
    final int k = (int)t.divide(getSamplingInterval()).floor().toDouble();
    return differenceEquation(k, x, inputOutput);
  }

  /**
   * {@inheritDoc}
   */
  public RM inputOutputEquation(final int k, final RM x) throws SolverStopException {
    if (hasDirectFeedthrough()) {
      throw new RuntimeException(Messages.getString("BaseDiscreteDynamicSystem.0")); //$NON-NLS-1$
    }

    final RM u = x.createZero(getInputSize(), 1);
    final RM y = outputEquation(k, x);
    return u.appendDown(y);
  }

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

    return outputEquation(t, x);
  }

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

  /**
   * {@inheritDoc}
   */
  public RM outputEquation(final RS t, final RM x, 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, x, u);

    return this.previousOutput.createClone();
  }

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

  /**
   * {@inheritDoc}
   */
  public RM outputEquation(final RS t, final RM x) 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, x);

    return this.previousOutput.createClone();
  }

  /**
   * {@inheritDoc}
   */
  public RS getSamplingInterval() {
    return this.samplingInterval;
  }

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

  /**
   * {@inheritDoc}
   */
  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;
  }

  /**
   * {@inheritDoc}
   */
  public RM stateEquation(final RS t, final RM xd, final RM inputOutput) throws SolverStopException {
    final int k = (int)t.divide(getSamplingInterval()).floor().toDouble();
    
    return stateEquation(k, xd, inputOutput);
  }

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

  /**
   * @see org.mklab.tool.control.system.DynamicSystem#getStateNumber()
   */
  public int getStateNumber() {
    return this.stateNumber;
  }

  /**
   * @see org.mklab.tool.control.system.DynamicSystem#setStateNumber(int)
   */
  public void setStateNumber(final int stateNumber) {
    this.stateNumber = stateNumber;
  }
}