BaseContinuousDynamicSystem.java

/*
 * $Id: BaseContinuousDynamicSystem.java,v 1.21 2008/06/26 10:10:34 koga Exp $
 *
 * Copyright (C) 2004 Koga Laboratory. All rights reserved.
 *
 */

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

import org.mklab.nfc.matrix.ComplexNumericalMatrix;
import org.mklab.nfc.matrix.IntMatrix;
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;


/**
 * 常微分方程式で表現される連続時間動的システムを表わすクラスです。
 * 
 * @author Koga Laboratory
 * @version $Revision: 1.21 $, 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 BaseContinuousDynamicSystem<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 ContinuousDynamicSystem<RS,RM,CS,CM> {

  /** 状態の初期値 */
  private RM initialState;
  /** 状態の微分の初期値 */
  private RM initialStateDerivative;
  /** 状態 */
  private RM state;
  /** 状態の微分。 */
  private RM stateDerivative;

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

  /**
   * 新しく生成された<code>BaseContinuousDynamicSystem</code>オブジェクトを初期化します。
   * 
   * @param inputSize 入力の数
   * @param outputSize 出力の数
   * @param stateSize 状態の数
   * @param sunit unit of scalar
   */
  public BaseContinuousDynamicSystem(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 = sunit.createZeroGrid(stateSize, 1);
    this.initialStateDerivative = sunit.createZeroGrid(stateSize, 1);
    this.state = this.initialState.createClone();
    this.index = new IntMatrix(stateSize, 1);
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public void initialize() {
    setState(getInitialState());
  }

  /**
   * {@inheritDoc}
   */
  @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;
    }
    BaseContinuousDynamicSystem<RS,RM,CS,CM> castedObj = (BaseContinuousDynamicSystem<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.index == null ? castedObj.index == null : this.index.equals(castedObj.index)));
  }
  /**
   * {@inheritDoc}
   */
  @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.index == null ? 0 : this.index.hashCode());
    return hashCode;
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public BaseContinuousDynamicSystem<RS,RM,CS,CM> clone() {
    final BaseContinuousDynamicSystem<RS,RM,CS,CM> inst = (BaseContinuousDynamicSystem<RS,RM,CS,CM>)super.clone();
    inst.setInitialState(this.getInitialState() == null ? null : this.getInitialState().createClone());
    inst.setState(this.getState() == null ? null : this.getState().createClone());
    inst.setIndex(this.getIndex() == null ? null : this.getIndex().createClone());
    return inst;
  }

  /**
   * {@inheritDoc}
   */
  public void setInitialState(final RM initialState) {
    this.initialState = initialState.createClone();
  }

  /**
   * {@inheritDoc}
   */
  public RM getInitialState() {
    if (this.initialState == null) {
      return null;
    }
    return this.initialState.createClone();
  }

  /**
   * {@inheritDoc}
   */
  public void setInitialStateDerivative(final RM initialStateDerivative) {
    this.initialStateDerivative = initialStateDerivative.createClone();
  }

  /**
   * {@inheritDoc}
   */
  public RM getInitialStateDerivative() {
    if (this.initialStateDerivative == null) {
      return null;
    }
    return this.initialStateDerivative.createClone();
  }

  /**
   * {@inheritDoc}
   */
  public RM getState() {
    if (this.state == null) {
      return null;
    }

    return this.state.createClone();
  }

  /**
   * {@inheritDoc}
   */
  public void setState(final RM state) {
    this.state = state.createClone();
  }

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

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

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

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

  /**
   * {@inheritDoc}
   */
  public int getStateNumber() {
    return this.stateNumber;
  }

  /**
   * {@inheritDoc}
   */
  public void setStateNumber(final int stateNumber) {
    this.stateNumber = stateNumber;
  }

  /**
   * {@inheritDoc}
   */
  public IntMatrix getIndex() {
    return this.index;
  }

  /**
   * {@inheritDoc}
   */
  public void setIndex(IntMatrix index) {
    this.index = index;
  }
  
  
  /**
   * {@inheritDoc}
   */
  public RM getStateDerivative() {
    return this.stateDerivative;
  }
  
  /**
   * {@inheritDoc}
   */
  public void setStateDerivative(RM stateDerivative) {
    this.stateDerivative = stateDerivative;
  }
}