DoubleDiscreteLinearDynamicSystem.java

/*
 * $Id$
 *
 * Copyright (C) 2004 Koga Laboratory. All rights reserved.
 *
 */

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

import org.mklab.nfc.matrix.DoubleMatrix;
import org.mklab.tool.control.DoubleLinearSystem;
import org.mklab.tool.control.DoubleLinearSystemFactory;
import org.mklab.tool.control.TimeDomainType;
import org.mklab.tool.control.system.DoubleLinearSystemOperator;


/**
 * 差分方程式で表現される離散時間線形動的システムを表すクラスです。
 * 
 * @author Koga Laboratory
 * @version $Revision$, 2004/11/24
 */
public class DoubleDiscreteLinearDynamicSystem extends DoubleBaseDiscreteDynamicSystem implements DoubleLinearSystemOperator {

  /** システム */
  private DoubleLinearSystem system;

  /** 識別するためのタグ */
  private String tag;

  /** Aを変数として扱うならばtrue */
  private boolean hasVariableA = true;
  /** Bを変数として扱うならばtrue */
  private boolean hasVariableB = true;
  /** Cを変数として扱うならばtrue */
  private boolean hasVariableC = true;
  /** Dを変数として扱うならばtrue */
  private boolean hasVariableD = true;
  /** Eを変数として扱うならばtrue */
  private boolean hasVariableE = true;

  /**
   * 新しく生成された<code>DiscreteLinearDynamicSystem</code>オブジェクトを初期化します。
   */
  public DoubleDiscreteLinearDynamicSystem() {
    super(0, 0, 0);
    setLinear(true);
    this.tag = "" + hashCode(); //$NON-NLS-1$
  }

  /**
   * 新しく生成された<code>DiscreteLinearDynamicSystem</code>オブジェクトを初期化します。
   * 
   * @param system 線形システム
   */
  public DoubleDiscreteLinearDynamicSystem(final DoubleLinearSystem system) {
    super(system.getInputSize(), system.getOutputSize(), system.getStateSize());
    this.system = system;
    this.system.setTimeDomainType(TimeDomainType.DISCRETE);
    final DoubleMatrix D = system.getD();

    if (D == null || D.isEmpty() || D.isZero()) {
      setHasDirectFeedthrough(false);
    }

    setLinear(true);
    this.tag = "" + hashCode(); //$NON-NLS-1$
  }

  /**
   * 新しく生成された<code>DiscreteLinearDynamicSystem</code>オブジェクトを初期化します。
   * 
   * @param A システム行列
   * @param B 入力行列
   * @param C 出力行列
   * @param D ゲイン行列
   */
  public DoubleDiscreteLinearDynamicSystem(final DoubleMatrix A, final DoubleMatrix B, final DoubleMatrix C, final DoubleMatrix D) {
    this(DoubleLinearSystemFactory.createLinearSystem(A, B, C, D));
  }

  /**
   * 新しく生成された<code>DiscreteLinearDynamicSystem</code>オブジェクトを初期化します。
   * 
   * <p>Dは、零行列です。
   * 
   * @param A システム行列
   * @param B 入力行列
   * @param C 出力行列
   */
  public DoubleDiscreteLinearDynamicSystem(final DoubleMatrix A, final DoubleMatrix B, final DoubleMatrix C) {
    this(A, B, C, new DoubleMatrix(C.getRowSize(), B.getColumnSize()));
    setHasDirectFeedthrough(false);
    setForcedSystem(true);
  }

  /**
   * {@inheritDoc}
   */
  public DoubleMatrix stateEquation( final int k, final DoubleMatrix x, final DoubleMatrix u) {
    final DoubleMatrix A = this.system.getA();
    final DoubleMatrix B = this.system.getB();
    return A.multiply(x).add(B.multiply(u));
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public DoubleMatrix outputEquation( final int k, final DoubleMatrix x, final DoubleMatrix u) {
    if (!hasDirectFeedthrough()) {
      throw new RuntimeException(Messages.getString("DiscreteLinearDynamicSystem.0")); //$NON-NLS-1$
    }
    final DoubleMatrix C = this.system.getC();
    final DoubleMatrix D = this.system.getD();
    return C.multiply(x).add(D.multiply(u));
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public DoubleMatrix outputEquation( final int k, final DoubleMatrix x) {
    if (hasDirectFeedthrough()) {
      throw new RuntimeException(Messages.getString("DiscreteLinearDynamicSystem.1")); //$NON-NLS-1$
    }
    final DoubleMatrix C = this.system.getC();
    return C.multiply(x);
  }

  /**
   * @see org.mklab.tool.control.system.SystemOperator#getLinearSystem()
   */
  @Override
  public DoubleLinearSystem getLinearSystem() {
    return this.system;
  }

  /**
   * @see org.mklab.tool.control.system.LinearSystemOperator#setLinearSystem(org.mklab.tool.control.LinearSystem)
   */
  public void setLinearSystem(final DoubleLinearSystem system) {
    this.system = system;
    this.system.setTimeDomainType(TimeDomainType.DISCRETE);
    if (system.isStrictlyProper()) {
      setHasDirectFeedthrough(false);
    } else {
      setHasDirectFeedthrough(true);
    }

    setStateSize(this.system.getStateSize());
  }

  /**
   * @see org.mklab.tool.control.system.LinearSystemOperator#hasVariableA()
   */
  public boolean hasVariableA() {
    return this.hasVariableA;
  }

  /**
   * @see org.mklab.tool.control.system.LinearSystemOperator#hasVariableB()
   */
  public boolean hasVariableB() {
    return this.hasVariableB;
  }

  /**
   * @see org.mklab.tool.control.system.LinearSystemOperator#hasVariableC()
   */
  public boolean hasVariableC() {
    return this.hasVariableC;
  }

  /**
   * @see org.mklab.tool.control.system.LinearSystemOperator#hasVariableD()
   */
  public boolean hasVariableD() {
    return this.hasVariableD;
  }

  /**
   * @see org.mklab.tool.control.system.LinearSystemOperator#hasVariableE()
   */
  public boolean hasVariableE() {
    return this.hasVariableE;
  }

  /**
   * @see org.mklab.tool.control.system.LinearSystemOperator#setHasVariableA(boolean)
   */
  public void setHasVariableA(final boolean hasVariableA) {
    this.hasVariableA = hasVariableA;
  }

  /**
   * @see org.mklab.tool.control.system.LinearSystemOperator#setHasVariableB(boolean)
   */
  public void setHasVariableB(final boolean hasVariableB) {
    this.hasVariableA = hasVariableB;
  }

  /**
   * @see org.mklab.tool.control.system.LinearSystemOperator#setHasVariableC(boolean)
   */
  public void setHasVariableC(final boolean hasVariableC) {
    this.hasVariableC = hasVariableC;
  }

  /**
   * @see org.mklab.tool.control.system.LinearSystemOperator#setHasVariableD(boolean)
   */
  public void setHasVariableD(final boolean hasVariableD) {
    this.hasVariableD = hasVariableD;
  }

  /**
   * @see org.mklab.tool.control.system.LinearSystemOperator#setHasVariableE(boolean)
   */
  public void setHasVariableE(final boolean hasVariableE) {
    this.hasVariableE = hasVariableE;
  }

  /**
   * @see org.mklab.tool.control.system.LinearSystemOperator#getA()
   */
  public DoubleMatrix getA() {
    return this.system.getA();
  }

  /**
   * @see org.mklab.tool.control.system.LinearSystemOperator#getB()
   */
  public DoubleMatrix getB() {
    return this.system.getB();
  }

  /**
   * @see org.mklab.tool.control.system.LinearSystemOperator#getC()
   */
  public DoubleMatrix getC() {
    return this.system.getC();
  }

  /**
   * @see org.mklab.tool.control.system.LinearSystemOperator#getD()
   */
  public DoubleMatrix getD() {
    return this.system.getD();
  }

  /**
   * @see org.mklab.tool.control.system.LinearSystemOperator#getE()
   */
  public DoubleMatrix getE() {
    return this.system.getE();
  }

  /**
   * @see org.mklab.tool.control.system.LinearSystemOperator#add(org.mklab.tool.control.system.LinearSystemOperator)
   */
  public DoubleLinearSystemOperator add(final DoubleLinearSystemOperator opponent) {
    return add(opponent, true);
  }

  /**
   * @see org.mklab.tool.control.system.LinearSystemOperator#add(org.mklab.tool.control.system.LinearSystemOperator, boolean)
   */
  public DoubleLinearSystemOperator add(final DoubleLinearSystemOperator opponent, final boolean simplify) {
    if (!(opponent instanceof DoubleDiscreteLinearDynamicSystem)) {
      throw new RuntimeException(Messages.getString("DiscreteLinearDynamicSystem.2")); //$NON-NLS-1$
    }
    return new DoubleDiscreteLinearDynamicSystem(this.system.add(opponent.getLinearSystem(), simplify));
  }

  /**
   * @see org.mklab.tool.control.system.LinearSystemOperator#subtract(org.mklab.tool.control.system.LinearSystemOperator)
   */
  public DoubleLinearSystemOperator subtract(final DoubleLinearSystemOperator opponent) {
    return subtract(opponent, true);
  }

  /**
   * @see org.mklab.tool.control.system.LinearSystemOperator#subtract(org.mklab.tool.control.system.LinearSystemOperator, boolean)
   */
  public DoubleLinearSystemOperator subtract(final DoubleLinearSystemOperator opponent, final boolean simplify) {
    if (!(opponent instanceof DoubleDiscreteLinearDynamicSystem)) {
      throw new RuntimeException(Messages.getString("DiscreteLinearDynamicSystem.3")); //$NON-NLS-1$
    }
    return new DoubleDiscreteLinearDynamicSystem(this.system.subtract(opponent.getLinearSystem(), simplify));
  }

  /**
   * @see org.mklab.tool.control.system.LinearSystemOperator#multiply(org.mklab.tool.control.system.LinearSystemOperator)
   */
  public DoubleLinearSystemOperator multiply(final DoubleLinearSystemOperator opponent) {
    return multiply(opponent, true);
  }

  /**
   * @see org.mklab.tool.control.system.LinearSystemOperator#multiply(org.mklab.tool.control.system.LinearSystemOperator, boolean)
   */
  public DoubleLinearSystemOperator multiply(final DoubleLinearSystemOperator opponent, final boolean simplify) {
    return new DoubleDiscreteLinearDynamicSystem(this.system.multiply(opponent.getLinearSystem(), simplify));
  }

  /**
   * @see org.mklab.tool.control.system.LinearSystemOperator#feedback(org.mklab.tool.control.system.LinearSystemOperator)
   */
  public DoubleLinearSystemOperator feedback(final DoubleLinearSystemOperator feedbackElement) {
    return feedback(feedbackElement, true, true);
  }

  /**
   * @see org.mklab.tool.control.system.LinearSystemOperator#feedback(org.mklab.tool.control.system.LinearSystemOperator, boolean)
   */
  public DoubleLinearSystemOperator feedback(final DoubleLinearSystemOperator feedbackElement, final boolean negative) {
    return feedback(feedbackElement, negative, true);
  }

  /**
   * @see org.mklab.tool.control.system.LinearSystemOperator#feedback(org.mklab.tool.control.system.LinearSystemOperator, boolean, boolean)
   */
  public DoubleLinearSystemOperator feedback(final DoubleLinearSystemOperator feedbackElement, final boolean negative, final boolean simplify) {
    if (!(feedbackElement instanceof DoubleDiscreteLinearDynamicSystem)) {
      throw new RuntimeException(Messages.getString("DiscreteLinearDynamicSystem.4")); //$NON-NLS-1$
    }
    return new DoubleDiscreteLinearDynamicSystem(this.system.feedback(feedbackElement.getLinearSystem(), negative, simplify));
  }

  /**
   * @see org.mklab.tool.control.system.LinearSystemOperator#unaryMinus()
   */
  public DoubleLinearSystemOperator unaryMinus() {
    return new DoubleDiscreteLinearDynamicSystem(this.system.unaryMinus());
  }

  /**
   * @see org.mklab.tool.control.system.LinearSystemOperator#unityFeedback()
   */
  public DoubleLinearSystemOperator unityFeedback() {
    return unityFeedback(true, true);
  }

  /**
   * @see org.mklab.tool.control.system.LinearSystemOperator#unityFeedback(boolean)
   */
  public DoubleLinearSystemOperator unityFeedback(final boolean negative) {
    return unityFeedback(negative, true);
  }

  /**
   * @see org.mklab.tool.control.system.LinearSystemOperator#unityFeedback(boolean, boolean)
   */
  public DoubleLinearSystemOperator unityFeedback(final boolean negative, final boolean simplify) {
    return new DoubleDiscreteLinearDynamicSystem(this.system.unityFeedback(negative, simplify));
  }

  /**
   * @see org.mklab.tool.control.system.LinearSystemOperator#getTag()
   */
  public String getTag() {
    return this.tag;
  }

  /**
   * @see org.mklab.tool.control.system.LinearSystemOperator#setTag(java.lang.String)
   */
  public void setTag(final String tag) {
    this.tag = tag;
  }

  /**
   * @see org.mklab.tool.control.system.discrete.BaseDiscreteDynamicSystem#hashCode()
   */
  @Override
  public int hashCode() {
    int hashCode = super.hashCode();
    hashCode = 31 * hashCode + (this.system == null ? 0 : this.system.hashCode());
    return hashCode;
  }

  /**
   * @see org.mklab.tool.control.system.discrete.BaseDiscreteDynamicSystem#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;
    }
    DoubleDiscreteLinearDynamicSystem castedObj = (DoubleDiscreteLinearDynamicSystem)o;
    return ((this.system == null ? castedObj.system == null : this.system.equals(castedObj.system)));
  }
}