ApproximateDerivativeSystem.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.ComplexNumericalMatrix;
import org.mklab.nfc.matrix.RealNumericalMatrix;
import org.mklab.nfc.ode.EquationSolver;
import org.mklab.nfc.scalar.ComplexNumericalScalar;
import org.mklab.nfc.scalar.RealNumericalScalar;
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
 * @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 class ApproximateDerivativeSystem<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 BaseContinuousStaticSystem<RS, RM, CS, CM> implements StringExternalizable {

  /** 初期出力 */
  @Parameter(name = "initialOutput", description = "Derivative.1", internationalization = true)
  private RM initialOutput = this.sunit.createZeroGrid(1, 1);

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

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

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

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

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

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

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

    this.previousInput = this.sunit.createZeroGrid(getInputSize(), 1);
  }

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

    final RM output = u.subtract(this.previousInput).divide(t.subtract(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 = this.sunit.createZeroGrid(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 = this.sunit.createZeroGrid(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 ApproximateDerivativeSystem<RS, RM, CS, CM> castedObj = (ApproximateDerivativeSystem<RS,RM,CS,CM>)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 + (this.previousTime.hashCode() ^ (this.previousTime.hashCode() >>> 32));
    hashCode = 31 * hashCode + (this.previousInput == null ? 0 : this.previousInput.hashCode());
    hashCode = 31 * hashCode + (this.previousOutput == null ? 0 : this.previousOutput.hashCode());
    return hashCode;
  }
}