DoubleBaseSampledDataDynamicSystem.java

/*
 * $Id: BaseSampledDataDynamicSystem.java,v 1.11 2008/07/16 03:51:36 koga Exp $
 *
 * Copyright (C) 2004-2005 Koga Laboratory. All rights reserved.
 */

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

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


/**
 * サンプル値動的システムを表わすクラスです。
 * 
 * @author koga
 * @version $Revision: 1.11 $
 */
public abstract class DoubleBaseSampledDataDynamicSystem extends DoubleSystemOperator implements DoubleSampledDataDynamicSystem {

  /** 連続時間システムの初期状態 */
  private DoubleMatrix continuousInitialState;
  /** 離散時間システムの初期状態 */
  private DoubleMatrix discreteInitialState;

  /** 連続時間システムの状態 */
  private DoubleMatrix continuousState;
  /** 離散時間システムの状態 */
  private DoubleMatrix discreteState;

  /** サンプル点ならばtrue */
  private boolean atSamplingPoint;

  /**
   * 新しく生成された<code>AbstractSampledDataDynamicSystem</code>オブジェクトを初期化します。
   * 
   * @param inputSize 入力の数
   * @param outputSize 出力の数
   * @param continuousStateSize 状態の数
   * @param discreteStateSize 状態の数
   */
  public DoubleBaseSampledDataDynamicSystem(final int inputSize, final int outputSize, final int continuousStateSize, final int discreteStateSize) {
    super();
    setInputSize(inputSize);
    setOutputSize(outputSize);
    setStateSize(continuousStateSize + discreteStateSize);
    setDynamic(true);

    if (inputSize == 0) {
      setForcedSystem(false);
    }

    this.continuousInitialState = new DoubleMatrix(continuousStateSize, 1);
    this.continuousState = this.continuousInitialState.createClone();
    this.discreteInitialState = new DoubleMatrix(discreteStateSize, 1);
    this.discreteState = this.discreteInitialState.createClone();
  }

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

  /**
   * {@inheritDoc}
   */
  public DoubleMatrix differentialEquation(final double t, final DoubleMatrix xc, final DoubleMatrix xd, final DoubleMatrix inputOutput) throws SolverStopException {
    return continuousStateEquation(t, xc, xd, inputOutput);
  }

  /**
   * {@inheritDoc}
   */
  public DoubleMatrix differenceEquation(final double t, final DoubleMatrix xc, final DoubleMatrix xd, final DoubleMatrix inputOutput) throws SolverStopException {
    return discreteStateEquation(t, xc, xd, inputOutput);
  }

  /**
   * @see org.mklab.tool.control.system.sampled.SampledDataDynamicSystem#getContinuousInitialState()
   */
  public DoubleMatrix getContinuousInitialState() {
    return this.continuousInitialState.createClone();
  }

  /**
   * @see org.mklab.tool.control.system.sampled.SampledDataDynamicSystem#getContinuousState()
   */
  public DoubleMatrix getContinuousState() {
    return this.continuousState.createClone();
  }

  /**
   * {@inheritDoc}
   */
  public void setContinuousState(final DoubleMatrix state) {
    this.continuousState = state;
  }

  /**
   * {@inheritDoc}
   */
  public void setDiscreteState(final DoubleMatrix state) {
    this.discreteState = state;
  }

  /**
   * {@inheritDoc}
   */
  public void setContinuousInitialState(final DoubleMatrix initialState) {
    this.continuousInitialState = initialState;
  }

  /**
   * @see org.mklab.tool.control.system.sampled.SampledDataDynamicSystem#getDiscreteInitialState()
   */
  public DoubleMatrix getDiscreteInitialState() {
    return this.discreteInitialState.createClone();
  }

  /**
   * @see org.mklab.tool.control.system.sampled.SampledDataDynamicSystem#getDiscreteState()
   */
  public DoubleMatrix getDiscreteState() {
    return this.discreteState.createClone();
  }

  /**
   * {@inheritDoc}
   */
  public void setDiscreteInitialState(final DoubleMatrix initialState) {
    this.discreteInitialState = initialState;
  }

  /**
   * {@inheritDoc}
   */
  public DoubleMatrix outputEquation( final double t,  final DoubleMatrix xc,  final DoubleMatrix xd,
       final DoubleMatrix u) throws SolverStopException {
    throw new SolverStopException(Messages.getString("BaseSampledDataDynamicSystem.0")); //$NON-NLS-1$
  }

  /**
   * {@inheritDoc}
   */
  public DoubleMatrix outputEquation( final double t,  final DoubleMatrix xc,  final DoubleMatrix xd) throws SolverStopException {
    throw new SolverStopException(Messages.getString("BaseSampledDataDynamicSystem.1")); //$NON-NLS-1$
  }

  /**
   * @see org.mklab.tool.control.system.sampled.SampledDataDynamicSystem#getContinuousStateSize()
   */
  public int getContinuousStateSize() {
    return this.continuousState.length();
  }

  /**
   * @see org.mklab.tool.control.system.sampled.SampledDataDynamicSystem#getDiscreteStateSize()
   */
  public int getDiscreteStateSize() {
    return this.discreteState.length();
  }

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

  /**
   * @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;
    }
    DoubleBaseSampledDataDynamicSystem castedObj = (DoubleBaseSampledDataDynamicSystem)o;
    return ((this.continuousInitialState == null ? castedObj.continuousInitialState == null : this.continuousInitialState.equals(castedObj.continuousInitialState))
        && (this.discreteInitialState == null ? castedObj.discreteInitialState == null : this.discreteInitialState.equals(castedObj.discreteInitialState))
        && (this.continuousState == null ? castedObj.continuousState == null : this.continuousState.equals(castedObj.continuousState))
        && (this.discreteState == null ? castedObj.discreteState == null : this.discreteState.equals(castedObj.discreteState)) && (this.atSamplingPoint == castedObj.atSamplingPoint));
  }

  /**
   * @see org.mklab.tool.control.system.SystemOperator#hashCode()
   */
  @Override
  public int hashCode() {
    int hashCode = super.hashCode();
    hashCode = 31 * hashCode + (this.continuousInitialState == null ? 0 : this.continuousInitialState.hashCode());
    hashCode = 31 * hashCode + (this.discreteInitialState == null ? 0 : this.discreteInitialState.hashCode());
    hashCode = 31 * hashCode + (this.continuousState == null ? 0 : this.continuousState.hashCode());
    hashCode = 31 * hashCode + (this.discreteState == null ? 0 : this.discreteState.hashCode());
    hashCode = 31 * hashCode + (this.atSamplingPoint ? 1231 : 1237);
    return hashCode;
  }
}