DoubleProductLikesSimulink.java

/*
 * Created on 2011/01/23
 * Copyright (C) 2011 Koga Laboratory. All rights reserved.
 *
 */
package org.mklab.tool.control.system.math;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.mklab.nfc.matrix.DoubleMatrix;
import org.mklab.tool.control.system.continuous.DoubleBaseContinuousStaticSystem;


/*
 * 未テスト
 */

/**
 * Simulinkのような乗除算ブロックです。
 * 
 * @author Yuhi Ishikura
 */
public class DoubleProductLikesSimulink extends DoubleBaseContinuousStaticSystem {

  /** 乗算または除算の指定(乗算ならばtrue、除算ならばfalse) */
  private List<Boolean> multipliers;

  /**
   * {@link DoubleProductLikesSimulink}オブジェクトを構築します。
   */
  @SuppressWarnings("boxing")
  public DoubleProductLikesSimulink() {
    this(new ArrayList<>(Arrays.asList(new Boolean[] {true})));
  }

  /**
   * 新しく生成された<code>Product</code>オブジェクトを初期化します。
   * 
   * @param multipliers 乗算または除算の指定(乗算ならばtrue、除算ならばfalse)
   */
  public DoubleProductLikesSimulink(final List<Boolean> multipliers) {
    super(-1, -1);
    if (multipliers == null) throw new NullPointerException();
    setAutoSize(true);

    this.multipliers = new ArrayList<>(multipliers.size());
    for (final Boolean multiplier : multipliers) {
      this.multipliers.add(multiplier);
    }
  }

  /**
   * {@inheritDoc}
   */
  @SuppressWarnings("boxing")
  @Override
  public DoubleMatrix outputEquation( final double t, final DoubleMatrix u) {
    final int inputCount = u.getRowSize();
    final int signalCountPerInput = u.getColumnSize();
    DoubleMatrix y = DoubleMatrix.ones(signalCountPerInput);
    for (int i = 1; i <= inputCount; i++) {
      final boolean isMultiply = this.multipliers.get(i - 1);

      DoubleMatrix input = u.getSubVector(i, i);
      if (isMultiply == false) {
        input = input.inverseElementWise();
      }
      y = y.multiply(input);
    }

    return y;
  }

  /**
   * 乗算または除算の指定(乗算ならばtrue、除算ならばfalse)を設定します。
   * 
   * @param multipliers 乗算または除算の指定(乗算ならばtrue、除算ならばfalse)
   */
  public void setMultipliers(final List<Boolean> multipliers) {
    this.multipliers = new ArrayList<>(multipliers.size());
    for (Boolean multiplier : multipliers) {
      this.multipliers.add(multiplier);
    }
  }

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

  /**
   * {@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;
    }
    DoubleProductLikesSimulink castedObj = (DoubleProductLikesSimulink)o;
    return ((this.multipliers == null ? castedObj.multipliers == null : this.multipliers.equals(castedObj.multipliers)));
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public int hashCode() {
    int hashCode = super.hashCode();
    hashCode = 31 * hashCode + (this.multipliers == null ? 0 : this.multipliers.hashCode());
    return hashCode;
  }
}