DoublePeriodicSource.java
/*
* $Id: PeriodicSource.java,v 1.9 2008/07/15 15:27:15 koga Exp $
*
* Copyright (C) 2004 Koga Laboratory. All rights reserved.
*
*/
package org.mklab.tool.control.system.source;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.mklab.nfc.matrix.DoubleMatrix;
import org.mklab.nfc.ode.DoublePiecewiseContinuousAlgebraicSystem;
import org.mklab.tool.control.system.parameter.Parameter;
import org.mklab.tool.control.system.parameter.ParameterUpdator;
import org.mklab.tool.control.system.parameter.StringExternalizable;
/**
* 任意形状の周期信号を発生するシステムを表すクラスです。
*
* @author Koga Laboratory
* @version $Revision: 1.9 $, 2005/06/15
*/
public class DoublePeriodicSource extends DoubleContinuousSource implements DoublePiecewiseContinuousAlgebraicSystem, ParameterUpdator, StringExternalizable {
/** 時刻の系列 */
@Parameter(name = "timeValues", description = "PeriodicSource.1", internationalization = true)
private DoubleMatrix timeValues;
/** 出力の系列 */
@Parameter(name = "outputValues", description = "PeriodicSource.3", update = true, internationalization = true)
private DoubleMatrix outputValues;
/**
* 新しく生成された<code>PeriodicSource</code>オブジェクトを初期化します。
*
* @param timeValues 時刻の系列
* @param outputValues 出力の系列
*/
public DoublePeriodicSource(final DoubleMatrix timeValues, final DoubleMatrix outputValues) {
super(outputValues.getRowSize());
this.timeValues = timeValues;
this.outputValues = outputValues;
}
/**
* 新しく生成された<code>PeriodicSource</code>オブジェクトを初期化します。
*/
public DoublePeriodicSource() {
this(new DoubleMatrix(new double[] {0, 2}), new DoubleMatrix(new double[] {0, 2}));
}
/**
* {@inheritDoc}
*/
@Override
public DoubleMatrix outputEquation(final double t) {
final double period = this.timeValues.max().doubleValue();
final double time = t - Math.floor(t / period) * period;
if (time < this.timeValues.min().doubleValue()) {
return new DoubleMatrix(getOutputSize(), 1);
}
final int size = this.timeValues.length();
for (int piece = 1; piece < size; piece++) {
final double timeValue1 = this.timeValues.getDoubleElement(piece);
final double timeValue2 = this.timeValues.getDoubleElement(piece + 1);
if (timeValue1 <= time && time < timeValue2) {
final DoubleMatrix outputValue1 = this.outputValues.getColumnVector(piece);
final DoubleMatrix outputValue2 = this.outputValues.getColumnVector(piece + 1);
final DoubleMatrix slope = outputValue2.subtract(outputValue1).divide(timeValue2 - timeValue1);
return outputValue1.add(slope.multiply(time - timeValue1));
}
}
assert false : "never reached"; //$NON-NLS-1$
return new DoubleMatrix(getOutputSize(), 1);
}
/**
* {@inheritDoc}
*/
@SuppressWarnings("boxing")
public List<Integer> getPiece(final double t, final DoubleMatrix u) {
final double period = this.timeValues.max().doubleValue();
final double time = t - Math.floor(t / period) * period;
if (time < this.timeValues.min().doubleValue()) {
return new ArrayList<>(Arrays.asList(new Integer[] {0}));
}
final int size = this.timeValues.length();
for (int piece = 1; piece < size; piece++) {
final double timeValue1 = this.timeValues.getDoubleElement(piece);
final double timeValue2 = this.timeValues.getDoubleElement(piece + 1);
if (timeValue1 <= time && time < timeValue2) {
return new ArrayList<>(Arrays.asList(new Integer[] {piece}));
}
}
return new ArrayList<>(Arrays.asList(new Integer[] {size}));
}
/**
* {@inheritDoc}
*/
public double getDiscontinuousPoint(final double t1, final DoubleMatrix u1, final double t2, final DoubleMatrix u2) {
final List<Integer> piece1 = getPiece(t1, u1);
final List<Integer> piece2 = getPiece(t2, u2);
if (piece1.equals(piece2) && 2 < this.timeValues.length()) {
return Double.NaN;
}
final double period = this.timeValues.max().doubleValue();
final double cycle1 = Math.floor(t1 / period);
final double cycle2 = Math.floor(t2 / period);
if (cycle1 != cycle2) {
return cycle2 * period;
}
final double time1 = t1 - cycle1 * period;
final double time2 = t2 - cycle2 * period;
final int size = this.timeValues.length();
for (int piece = 1; piece <= size; piece++) {
final double timeValue = this.timeValues.getDoubleElement(piece);
if (time1 <= timeValue && timeValue < time2) {
return cycle1 * period + timeValue;
}
}
assert false : "never reached"; //$NON-NLS-1$
return Double.NaN;
}
/**
* @see org.mklab.tool.control.system.parameter.ParameterUpdator#updateWith(java.lang.String)
*/
public boolean updateWith(String parameter) {
if (parameter.equals("outputValues")) { //$NON-NLS-1$
final int size = this.outputValues.getRowSize();
setInputSize(size);
setOutputSize(size);
return true;
}
return false;
}
/**
* @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 o) {
if (this == o) {
return true;
}
if (!super.equals(o)) {
return false;
}
if (o == null) {
return false;
}
if (o.getClass() != getClass()) {
return false;
}
DoublePeriodicSource castedObj = (DoublePeriodicSource)o;
return ((this.timeValues == null ? castedObj.timeValues == null : this.timeValues.equals(castedObj.timeValues)) && (this.outputValues == null ? castedObj.outputValues == null : this.outputValues
.equals(castedObj.outputValues)));
}
/**
* @see org.mklab.tool.control.system.SystemOperator#hashCode()
*/
@Override
public int hashCode() {
int hashCode = super.hashCode();
hashCode = 31 * hashCode + (this.timeValues == null ? 0 : this.timeValues.hashCode());
hashCode = 31 * hashCode + (this.outputValues == null ? 0 : this.outputValues.hashCode());
return hashCode;
}
}