DiscreteLinearDynamicSystem.java

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

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

import org.mklab.nfc.matrix.ComplexNumericalMatrix;
import org.mklab.nfc.matrix.RealNumericalMatrix;
import org.mklab.nfc.scalar.ComplexNumericalScalar;
import org.mklab.nfc.scalar.RealNumericalScalar;
import org.mklab.tool.control.LinearSystem;
import org.mklab.tool.control.LinearSystemFactory;
import org.mklab.tool.control.TimeDomainType;
import org.mklab.tool.control.system.LinearSystemOperator;


/**
 * 差分方程式で表現される離散時間線形動的システムを表すクラスです。
 * 
 * @author Koga Laboratory
 * @version $Revision$, 2004/11/24
 * @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 DiscreteLinearDynamicSystem<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 BaseDiscreteDynamicSystem<RS,RM,CS,CM> implements LinearSystemOperator<RS,RM,CS,CM> {

  /** システム */
  private LinearSystem<RS,RM,CS,CM> 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>オブジェクトを初期化します。
   * @param sunit unit of scalar
   */
  public DiscreteLinearDynamicSystem(RS sunit) {
    super(0, 0, 0, sunit);
    setLinear(true);
    this.tag = "" + hashCode(); //$NON-NLS-1$
  }

  /**
   * 新しく生成された<code>DiscreteLinearDynamicSystem</code>オブジェクトを初期化します。
   * 
   * @param system 線形システム
   * @param sunit unit of scalar
   */
  public DiscreteLinearDynamicSystem(final LinearSystem<RS,RM,CS,CM> system, RS sunit) {
    super(system.getInputSize(), system.getOutputSize(), system.getStateSize(), sunit);
    this.system = system;
    this.system.setTimeDomainType(TimeDomainType.DISCRETE);
    final RM 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 ゲイン行列
   * @param sunit unit of scalar
   */
  public DiscreteLinearDynamicSystem(final RM A, final RM B, final RM C, final RM D, RS sunit) {
    this(LinearSystemFactory.createLinearSystem(A, B, C, D), sunit);
  }

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

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

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

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

  /**
   * @see org.mklab.tool.control.system.SystemOperator#getLinearSystem()
   */
  @Override
  public LinearSystem<RS,RM,CS,CM> getLinearSystem() {
    return this.system;
  }

  /**
   * @see org.mklab.tool.control.system.LinearSystemOperator#setLinearSystem(org.mklab.tool.control.LinearSystem)
   */
  public void setLinearSystem(final LinearSystem<RS,RM,CS,CM> 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 RM getA() {
    return this.system.getA();
  }

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

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

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

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

  /**
   * @see org.mklab.tool.control.system.LinearSystemOperator#add(org.mklab.tool.control.system.LinearSystemOperator)
   */
  public LinearSystemOperator<RS,RM,CS,CM> add(final LinearSystemOperator<RS,RM,CS,CM> opponent) {
    return add(opponent, true);
  }

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

  /**
   * @see org.mklab.tool.control.system.LinearSystemOperator#subtract(org.mklab.tool.control.system.LinearSystemOperator)
   */
  public LinearSystemOperator<RS,RM,CS,CM> subtract(final LinearSystemOperator<RS,RM,CS,CM> opponent) {
    return subtract(opponent, true);
  }

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

  /**
   * @see org.mklab.tool.control.system.LinearSystemOperator#multiply(org.mklab.tool.control.system.LinearSystemOperator)
   */
  public LinearSystemOperator<RS,RM,CS,CM> multiply(final LinearSystemOperator<RS,RM,CS,CM> opponent) {
    return multiply(opponent, true);
  }

  /**
   * @see org.mklab.tool.control.system.LinearSystemOperator#multiply(org.mklab.tool.control.system.LinearSystemOperator, boolean)
   */
  public LinearSystemOperator<RS,RM,CS,CM> multiply(final LinearSystemOperator<RS,RM,CS,CM> opponent, final boolean simplify) {
    return new DiscreteLinearDynamicSystem<>(this.system.multiply(opponent.getLinearSystem(), simplify), this.sunit);
  }

  /**
   * @see org.mklab.tool.control.system.LinearSystemOperator#feedback(org.mklab.tool.control.system.LinearSystemOperator)
   */
  public LinearSystemOperator<RS,RM,CS,CM> feedback(final LinearSystemOperator<RS,RM,CS,CM> feedbackElement) {
    return feedback(feedbackElement, true, true);
  }

  /**
   * @see org.mklab.tool.control.system.LinearSystemOperator#feedback(org.mklab.tool.control.system.LinearSystemOperator, boolean)
   */
  public LinearSystemOperator<RS,RM,CS,CM> feedback(final LinearSystemOperator<RS,RM,CS,CM> 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 LinearSystemOperator<RS,RM,CS,CM> feedback(final LinearSystemOperator<RS,RM,CS,CM> feedbackElement, final boolean negative, final boolean simplify) {
    if (!(feedbackElement instanceof DiscreteLinearDynamicSystem)) {
      throw new RuntimeException(Messages.getString("DiscreteLinearDynamicSystem.4")); //$NON-NLS-1$
    }
    return new DiscreteLinearDynamicSystem<>(this.system.feedback(feedbackElement.getLinearSystem(), negative, simplify), this.sunit);
  }

  /**
   * @see org.mklab.tool.control.system.LinearSystemOperator#unaryMinus()
   */
  public LinearSystemOperator<RS,RM,CS,CM> unaryMinus() {
    return new DiscreteLinearDynamicSystem<>(this.system.unaryMinus(), this.sunit);
  }

  /**
   * @see org.mklab.tool.control.system.LinearSystemOperator#unityFeedback()
   */
  public LinearSystemOperator<RS,RM,CS,CM> unityFeedback() {
    return unityFeedback(true, true);
  }

  /**
   * @see org.mklab.tool.control.system.LinearSystemOperator#unityFeedback(boolean)
   */
  public LinearSystemOperator<RS,RM,CS,CM> unityFeedback(final boolean negative) {
    return unityFeedback(negative, true);
  }

  /**
   * @see org.mklab.tool.control.system.LinearSystemOperator#unityFeedback(boolean, boolean)
   */
  public LinearSystemOperator<RS,RM,CS,CM> unityFeedback(final boolean negative, final boolean simplify) {
    return new DiscreteLinearDynamicSystem<>(this.system.unityFeedback(negative, simplify), this.sunit);
  }

  /**
   * @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;
    }
    DiscreteLinearDynamicSystem<RS,RM,CS,CM> castedObj = (DiscreteLinearDynamicSystem<RS,RM,CS,CM>)o;
    return ((this.system == null ? castedObj.system == null : this.system.equals(castedObj.system)));
  }
}