LookupTable.java

/*
 * $Id: LookupTable.java,v 1.4 2008/07/16 03:51:37 koga Exp $
 *
 * Copyright (C) 2004 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.matrix.misc.LinearlySpacedVector;
import org.mklab.nfc.scalar.ComplexNumericalScalar;
import org.mklab.nfc.scalar.RealNumericalScalar;
import org.mklab.tool.control.system.continuous.BaseContinuousStaticSystem;
import org.mklab.tool.control.system.parameter.Parameter;
import org.mklab.tool.control.system.parameter.StringExternalizable;


/**
 * データテーブルの値に基づく内挿・外挿による補間により出力を決定するシステムを表わすクラスです。
 * 
 * @author Koga Laboratory
 * @version $Revision: 1.4 $, 2004/11/12
 * @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 LookupTable<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 = "inputData", description = "LookupTable.1", internationalization = true)
  private RM inputData = LinearlySpacedVector.create(this.sunit.create(0), this.sunit.create(10), 11);

  /** 出力データ */
  @Parameter(name = "outputData", description = "LookupTable.3", internationalization = true)
  private RM outputData = LinearlySpacedVector.create(this.sunit.create(0), this.sunit.create(10), 11);

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

  /**
   * {@inheritDoc}
   */
  @Override
  public RM outputEquation( final RS t, final RM u) {
    final RS input = u.getElement(1);

    final RS inputFirst1 = this.inputData.getElement(1);

    if (input.isLessThan(inputFirst1)) {
      final RS inputFirst2 = this.inputData.getElement(2);
      final RS outputFirst1 = this.outputData.getElement(1);
      final RS outputFirst2 = this.outputData.getElement(2);
      final RS output = outputFirst1.add(outputFirst2.subtract(outputFirst1).divide(inputFirst2.subtract(inputFirst1)).multiply(input.subtract(inputFirst1)));
      return this.sunit.createUnitGrid(1).multiply(output);
    }

    final int size = this.inputData.length();

    final RS inputLast1 = this.inputData.getElement(size);

    if (inputLast1.isLessThan(input)) {
      final RS inputLast2 = this.inputData.getElement(size - 1);
      final RS outputLast1 = this.outputData.getElement(size);
      final RS outputLast2 = this.outputData.getElement(size - 1);
      final RS output = outputLast1.add(outputLast1.subtract(outputLast2).divide(inputLast1.subtract(inputLast2)).multiply(input.subtract(inputLast1)));
      return this.sunit.createUnitGrid(1).multiply(output);
    }

    final List<Integer> lowerUpperNumber = getLowerUpperNumber(input);
    return getInterpolatedOutput(input, lowerUpperNumber);
  }

  /**
   * 下限と上限を用いて内挿した出力を返します。
   * 
   * @param input 入力データ
   * @param lowerUpperNumber 下限と上限の入力の番号
   * @return 下限と上限を用いて内挿した出力
   */
  private RM getInterpolatedOutput(final RS input, final List<Integer> lowerUpperNumber) {
    int lowerNumber = lowerUpperNumber.get(0).intValue();
    int upperNumber = lowerUpperNumber.get(1).intValue();

    if (lowerNumber == upperNumber) {
      RS[] out = this.sunit.createArray(1);
      out[0]= this.outputData.getElement(lowerNumber);
      return this.sunit.createGrid(out);
    }

    final RS lowerInput = this.inputData.getElement(lowerNumber);
    final RS upperInput = this.inputData.getElement(upperNumber);
    final RS lowerOutput = this.outputData.getElement(lowerNumber);
    final RS upperOutput = this.outputData.getElement(upperNumber);

    final RS output = upperOutput.subtract(lowerOutput).divide(upperInput.subtract(lowerInput)).multiply(input.subtract(lowerInput)).add(lowerOutput);
    RS[] out = this.sunit.createArray(1);
    out[0] = output;
    return this.sunit.createGrid(out);
  }

  /**
   * 下限と上限の入力データの番号を返します。
   * 
   * @param input 入力データ
   * @return 下限と上限の入力データの番号
   */
  @SuppressWarnings("boxing")
  private List<Integer> getLowerUpperNumber(final RS input) {
    final int size = this.inputData.length();
    final RS inputFirst1 = this.inputData.getElement(1);
    final RS inputLast1 = this.inputData.getElement(size);

    if (input == inputFirst1) {
      return new ArrayList<>(Arrays.asList(new Integer[] {1, 1}));
    }
    if (input == inputLast1) {
      return new ArrayList<>(Arrays.asList(new Integer[] {size, size}));
    }

    int estimatedNumber = Math.min((int)input.subtract(inputFirst1).divide(inputLast1.subtract(inputFirst1)).multiply(size).floor().add(1).toDouble(), size);
    RS estimatedInput = this.inputData.getElement(estimatedNumber);

    int lowerNumber = estimatedNumber;
    int upperNumber = estimatedNumber;

    if (input.isLessThan(estimatedInput)) {
      for (int i = estimatedNumber - 1; 1 <= i; i--) {
        estimatedInput = this.inputData.getElement(i);
        if (estimatedInput == input) {
          lowerNumber = i;
          upperNumber = i;
        } else if (estimatedInput.isLessThan(input)) {
          lowerNumber = i;
          upperNumber = i + 1;
          break;
        }
      }
    } else {
      for (int i = estimatedNumber + 1; i <= size; i++) {
        estimatedInput = this.inputData.getElement(i);
        if (input == estimatedInput) {
          lowerNumber = i;
          upperNumber = i;
          break;
        }
        if (input.isLessThan(estimatedInput)) {
          lowerNumber = i - 1;
          upperNumber = i;
          break;
        }
      }
    }

    return new ArrayList<>(Arrays.asList(new Integer[] {lowerNumber, upperNumber}));
  }

  /**
   * 入力データを設定します。
   * 
   * @param inputData 入力データ
   */
  public void setInputData(final RM inputData) {
    this.inputData = inputData;
  }

  /**
   * 入力データを返します。
   * 
   * @return 入力データ
   */
  public RM getInputData() {
    return this.inputData;
  }

  /**
   * 出力データを設定します。
   * 
   * @param outputData 出力データ
   */
  public void setOutputData(final RM outputData) {
    this.outputData = outputData;
  }

  /**
   * 出力データを返します。
   * 
   * @return 出力データ
   */
  public RM getOutputData() {
    return this.outputData;
  }

  /**
   * @see org.mklab.tool.control.system.parameter.StringExternalizable#getString(java.lang.String)
   */
  public String getString(String key) {
    return Messages.getString(key);
  }

  /**
   * @see org.mklab.tool.control.system.SystemOperator#equals(java.lang.Object)
   */
  @Override
  public boolean equals(Object opponent) {
    if (this == opponent) {
      return true;
    }
    if (!super.equals(opponent)) {
      return false;
    }
    if (opponent == null) {
      return false;
    }
    if (opponent.getClass() != getClass()) {
      return false;
    }
    LookupTable<RS,RM,CS,CM> castedObj = (LookupTable<RS,RM,CS,CM>)opponent;
    return ((this.inputData == null ? castedObj.inputData == null : this.inputData.equals(castedObj.inputData)) && (this.outputData == null ? castedObj.outputData == null : this.outputData
        .equals(castedObj.outputData)));
  }

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

  /**
   * @see org.mklab.tool.control.system.SystemOperator#clone()
   */
  @Override
  public LookupTable<RS,RM,CS,CM> clone() {
    LookupTable<RS,RM,CS,CM> inst = (LookupTable<RS,RM,CS,CM>)super.clone();
    inst.inputData = this.inputData == null ? null : inst.inputData.createClone();
    inst.outputData = this.outputData == null ? null : inst.outputData.createClone();
    return inst;
  }

}