ProductLikesSimulink.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.ComplexNumericalMatrix;
import org.mklab.nfc.matrix.RealNumericalMatrix;
import org.mklab.nfc.scalar.ComplexNumericalScalar;
import org.mklab.nfc.scalar.RealNumericalScalar;
import org.mklab.tool.control.system.continuous.BaseContinuousStaticSystem;


/*
 * 未テスト
 */

/**
 * Simulinkのような乗除算ブロックです。
 * 
 * @author Yuhi Ishikura
 * @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 ProductLikesSimulink<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> {

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

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

  /**
   * 新しく生成された<code>Product</code>オブジェクトを初期化します。
   * 
   * @param multipliers 乗算または除算の指定(乗算ならばtrue、除算ならばfalse)
   * @param sunit unit of scalar
   */
  public ProductLikesSimulink(final List<Boolean> multipliers, RS sunit) {
    super(-1, -1, sunit);
    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 RM outputEquation( final RS t, final RM u) {
    final int inputCount = u.getRowSize();
    final int signalCountPerInput = u.getColumnSize();
    RM y = this.sunit.createOnesGrid(signalCountPerInput);
    for (int i = 1; i <= inputCount; i++) {
      final boolean isMultiply = this.multipliers.get(i - 1);

      RM 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;
    }
    ProductLikesSimulink<RS,RM,CS,CM> castedObj = (ProductLikesSimulink<RS,RM,CS,CM>)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;
  }
}