DoubleLookupTable.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.DoubleMatrix;
import org.mklab.nfc.matrix.misc.LinearlySpacedVector;
import org.mklab.tool.control.system.continuous.DoubleBaseContinuousStaticSystem;
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
*/
public class DoubleLookupTable extends DoubleBaseContinuousStaticSystem implements StringExternalizable {
/** 入力データ */
@Parameter(name = "inputData", description = "LookupTable.1", internationalization = true)
private DoubleMatrix inputData = LinearlySpacedVector.create(0, 10, 11);
/** 出力データ */
@Parameter(name = "outputData", description = "LookupTable.3", internationalization = true)
private DoubleMatrix outputData = LinearlySpacedVector.create(0, 10, 11);
/**
* 新しく生成された<code>LookupTable</code>オブジェクトを初期化します。
*/
public DoubleLookupTable() {
super(1, 1);
setAutoSize(false);
setHasDirectFeedthrough(true);
}
/**
* {@inheritDoc}
*/
@Override
public DoubleMatrix outputEquation( final double t, final DoubleMatrix u) {
final double input = u.getDoubleElement(1);
final double inputFirst1 = this.inputData.getDoubleElement(1);
if (input < inputFirst1) {
final double inputFirst2 = this.inputData.getDoubleElement(2);
final double outputFirst1 = this.outputData.getDoubleElement(1);
final double outputFirst2 = this.outputData.getDoubleElement(2);
final double output = outputFirst1 + (outputFirst2 - outputFirst1) / (inputFirst2 - inputFirst1) * (input - inputFirst1);
return new DoubleMatrix(new double[] {output});
}
final int size = this.inputData.length();
final double inputLast1 = this.inputData.getDoubleElement(size);
if (inputLast1 < input) {
final double inputLast2 = this.inputData.getDoubleElement(size - 1);
final double outputLast1 = this.outputData.getDoubleElement(size);
final double outputLast2 = this.outputData.getDoubleElement(size - 1);
final double output = outputLast1 + (outputLast1 - outputLast2) / (inputLast1 - inputLast2) * (input - inputLast1);
return new DoubleMatrix(new double[] {output});
}
final List<Integer> lowerUpperNumber = getLowerUpperNumber(input);
return getInterpolatedOutput(input, lowerUpperNumber);
}
/**
* 下限と上限を用いて内挿した出力を返します。
*
* @param input 入力データ
* @param lowerUpperNumber 下限と上限の入力の番号
* @return 下限と上限を用いて内挿した出力
*/
private DoubleMatrix getInterpolatedOutput(final double input, final List<Integer> lowerUpperNumber) {
int lowerNumber = lowerUpperNumber.get(0).intValue();
int upperNumber = lowerUpperNumber.get(1).intValue();
if (lowerNumber == upperNumber) {
return new DoubleMatrix(new double[] {this.outputData.getDoubleElement(lowerNumber)});
}
final double lowerInput = this.inputData.getDoubleElement(lowerNumber);
final double upperInput = this.inputData.getDoubleElement(upperNumber);
final double lowerOutput = this.outputData.getDoubleElement(lowerNumber);
final double upperOutput = this.outputData.getDoubleElement(upperNumber);
final double output = (upperOutput - lowerOutput) / (upperInput - lowerInput) * (input - lowerInput) + lowerOutput;
return new DoubleMatrix(new double[] {output});
}
/**
* 下限と上限の入力データの番号を返します。
*
* @param input 入力データ
* @return 下限と上限の入力データの番号
*/
@SuppressWarnings("boxing")
private List<Integer> getLowerUpperNumber(final double input) {
final int size = this.inputData.length();
final double inputFirst1 = this.inputData.getDoubleElement(1);
final double inputLast1 = this.inputData.getDoubleElement(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)Math.floor((input - inputFirst1) / (inputLast1 - inputFirst1) * size) + 1, size);
double estimatedInput = this.inputData.getDoubleElement(estimatedNumber);
int lowerNumber = estimatedNumber;
int upperNumber = estimatedNumber;
if (input < estimatedInput) {
for (int i = estimatedNumber - 1; 1 <= i; i--) {
estimatedInput = this.inputData.getDoubleElement(i);
if (estimatedInput == input) {
lowerNumber = i;
upperNumber = i;
} else if (estimatedInput < input) {
lowerNumber = i;
upperNumber = i + 1;
break;
}
}
} else {
for (int i = estimatedNumber + 1; i <= size; i++) {
estimatedInput = this.inputData.getDoubleElement(i);
if (input == estimatedInput) {
lowerNumber = i;
upperNumber = i;
break;
}
if (input < estimatedInput) {
lowerNumber = i - 1;
upperNumber = i;
break;
}
}
}
return new ArrayList<>(Arrays.asList(new Integer[] {lowerNumber, upperNumber}));
}
/**
* 入力データを設定します。
*
* @param inputData 入力データ
*/
public void setInputData(final DoubleMatrix inputData) {
this.inputData = inputData;
}
/**
* 入力データを返します。
*
* @return 入力データ
*/
public DoubleMatrix getInputData() {
return this.inputData;
}
/**
* 出力データを設定します。
*
* @param outputData 出力データ
*/
public void setOutputData(final DoubleMatrix outputData) {
this.outputData = outputData;
}
/**
* 出力データを返します。
*
* @return 出力データ
*/
public DoubleMatrix 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;
}
DoubleLookupTable castedObj = (DoubleLookupTable)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 DoubleLookupTable clone() {
DoubleLookupTable inst = (DoubleLookupTable)super.clone();
inst.inputData = this.inputData == null ? null : inst.inputData.createClone();
inst.outputData = this.outputData == null ? null : inst.outputData.createClone();
return inst;
}
}