DoubleApproximateDerivativeSystem.java

/*
 * $Id: DerivativeSystem.java,v 1.9 2008/07/15 15:27:16 koga Exp $
 *
 * Copyright (C) 2004 Masanobu Koga. All rights reserved.
 */
package org.mklab.tool.control.system.continuous;

import org.mklab.nfc.matrix.DoubleMatrix;
import org.mklab.nfc.ode.EquationSolver;
import org.mklab.tool.control.system.parameter.Parameter;
import org.mklab.tool.control.system.parameter.StringExternalizable;


/**
 * (差分による擬似)時間微分器を表すクラスです。
 * 
 * @author koga
 * @version $Revision: 1.9 $, 2007/06/03
 */
public class DoubleApproximateDerivativeSystem extends DoubleBaseContinuousStaticSystem implements StringExternalizable {

  /** 初期出力 */
  @Parameter(name = "initialOutput", description = "Derivative.1", internationalization = true)
  private DoubleMatrix initialOutput = new DoubleMatrix(1, 1);

  /** 1刻み前の時刻 */
  private double previousTime;

  /** 1呼び出し前の入力 */
  private DoubleMatrix previousInput;

  /** 1呼び出し前の出力 */
  private DoubleMatrix previousOutput;

  /**
   * 新しく生成された<code>DerivativeSystem</code>オブジェクトを初期化します。
   */
  public DoubleApproximateDerivativeSystem() {
    super(-1, -1);
    setAutoSize(true);
    setHasDirectFeedthrough(true);
  }

  /**
   * 初期出力を設定します。
   * 
   * @param initialOutput 初期出力
   */
  public void setInitialOutput(final DoubleMatrix initialOutput) {
    this.initialOutput = initialOutput.createClone();
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public void initialize() {
    this.previousOutput = this.initialOutput.createClone();
    this.previousTime = 0;

    if (getInputSize() < 0) {
      return;
    }

    this.previousInput = new DoubleMatrix(getInputSize(), 1);
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public DoubleMatrix outputEquation(final double t, final DoubleMatrix u) {
    if (t == this.previousTime) {
      return this.previousOutput.createClone();
    }

    final DoubleMatrix output = u.subtract(this.previousInput).divide(t - this.previousTime);

    if (EquationSolver.isTrial() == false) {
      this.previousInput = u.createClone();
      this.previousOutput = output.createClone();
      this.previousTime = t;
    }

    return output;
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public void setInputSize(final int size) {
    super.setInputSize(size);
    super.setOutputSize(size);

    if (size < 0) {
      return;
    }

    if (this.initialOutput == null || this.initialOutput.length() < size) {
      this.initialOutput = new DoubleMatrix(size, 1);
    }
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public void setOutputSize(final int size) {
    super.setInputSize(size);
    super.setOutputSize(size);

    if (size < 0) {
      return;
    }

    if (this.initialOutput == null || this.initialOutput.length() < size) {
      this.initialOutput = new DoubleMatrix(size, 1);
    }
  }

  /**
   * {@inheritDoc}
   */
  public String getString(String key) {
    return Messages.getString(key);
  }

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

    final DoubleApproximateDerivativeSystem castedObj = (DoubleApproximateDerivativeSystem)o;
    return ((this.initialOutput == null ? castedObj.initialOutput == null : this.initialOutput.equals(castedObj.initialOutput)) && (this.previousTime == castedObj.previousTime)
        && (this.previousInput == null ? castedObj.previousInput == null : this.previousInput.equals(castedObj.previousInput)) && (this.previousOutput == null ? castedObj.previousOutput == null
          : this.previousOutput.equals(castedObj.previousOutput)));
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public int hashCode() {
    int hashCode = super.hashCode();
    hashCode = 31 * hashCode + (this.initialOutput == null ? 0 : this.initialOutput.hashCode());
    hashCode = 31 * hashCode + (int)(Double.doubleToLongBits(this.previousTime) ^ (Double.doubleToLongBits(this.previousTime) >>> 32));
    hashCode = 31 * hashCode + (this.previousInput == null ? 0 : this.previousInput.hashCode());
    hashCode = 31 * hashCode + (this.previousOutput == null ? 0 : this.previousOutput.hashCode());
    return hashCode;
  }
}