PeriodicSource.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.ComplexNumericalMatrix;
import org.mklab.nfc.matrix.RealNumericalMatrix;
import org.mklab.nfc.ode.PiecewiseContinuousAlgebraicSystem;
import org.mklab.nfc.scalar.ComplexNumericalScalar;
import org.mklab.nfc.scalar.RealNumericalScalar;
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
* @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 PeriodicSource<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 ContinuousSource<RS,RM,CS,CM> implements PiecewiseContinuousAlgebraicSystem<RS,RM,CS,CM>, ParameterUpdator, StringExternalizable {
/** 時刻の系列 */
@Parameter(name = "timeValues", description = "PeriodicSource.1", internationalization = true)
private RM timeValues;
/** 出力の系列 */
@Parameter(name = "outputValues", description = "PeriodicSource.3", update = true, internationalization = true)
private RM outputValues;
/**
* 新しく生成された<code>PeriodicSource</code>オブジェクトを初期化します。
*
* @param timeValues 時刻の系列
* @param outputValues 出力の系列
* @param sunit unit of scalar
*/
public PeriodicSource(final RM timeValues, final RM outputValues, RS sunit) {
super(outputValues.getRowSize(), sunit);
this.timeValues = timeValues;
this.outputValues = outputValues;
}
/**
* 新しく生成された<code>PeriodicSource</code>オブジェクトを初期化します。
* @param sunit unit of scalar
*/
public PeriodicSource(RS sunit) {
super(1, sunit);
RS[] tt = sunit.createArray(2);
tt[0]= sunit.create(0);
tt[1]= sunit.create(2);
this.timeValues = sunit.createGrid(tt);
RS[] oo = sunit.createArray(2);
oo[0]= sunit.create(0);
oo[1]= sunit.create(2);
this.outputValues = sunit.createGrid(oo);
}
/**
* {@inheritDoc}
*/
@Override
public RM outputEquation(final RS t) {
final RS period = this.timeValues.max();
final RS time = t.subtract(t.divide(period).floor().multiply(period));
if (time.isLessThan(this.timeValues.min())) {
return this.sunit.createZeroGrid(getOutputSize(), 1);
}
final int size = this.timeValues.length();
for (int piece = 1; piece < size; piece++) {
final RS timeValue1 = this.timeValues.getElement(piece);
final RS timeValue2 = this.timeValues.getElement(piece + 1);
if (timeValue1.isLessThanOrEquals(time) && time.isLessThan(timeValue2)) {
final RM outputValue1 = this.outputValues.getColumnVector(piece);
final RM outputValue2 = this.outputValues.getColumnVector(piece + 1);
final RM slope = outputValue2.subtract(outputValue1).divide(timeValue2.subtract(timeValue1));
return outputValue1.add(slope.multiply(time.subtract(timeValue1)));
}
}
assert false : "never reached"; //$NON-NLS-1$
return this.sunit.createZeroGrid(getOutputSize(), 1);
}
/**
* {@inheritDoc}
*/
@SuppressWarnings("boxing")
public List<Integer> getPiece(final RS t, final RM u) {
final RS period = this.timeValues.max();
final RS time = t.subtract(t.divide(period).floor().multiply(period));
if (time.isLessThan(this.timeValues.min())) {
return new ArrayList<>(Arrays.asList(new Integer[] {0}));
}
final int size = this.timeValues.length();
for (int piece = 1; piece < size; piece++) {
final RS timeValue1 = this.timeValues.getElement(piece);
final RS timeValue2 = this.timeValues.getElement(piece + 1);
if (timeValue1.isLessThanOrEquals(time) && time.isLessThan(timeValue2)) {
return new ArrayList<>(Arrays.asList(new Integer[] {piece}));
}
}
return new ArrayList<>(Arrays.asList(new Integer[] {size}));
}
/**
* {@inheritDoc}
*/
public RS getDiscontinuousPoint(final RS t1, final RM u1, final RS t2, final RM u2) {
final List<Integer> piece1 = getPiece(t1, u1);
final List<Integer> piece2 = getPiece(t2, u2);
if (piece1.equals(piece2) && 2 < this.timeValues.length()) {
return this.sunit.getNaN();
}
final RS period = this.timeValues.max();
final RS cycle1 = t1.divide(period).floor();
final RS cycle2 = t2.divide(period).floor();
if (cycle1 != cycle2) {
return cycle2.multiply(period);
}
final RS time1 = t1.subtract(cycle1.multiply(period));
final RS time2 = t2.subtract(cycle2.multiply(period));
final int size = this.timeValues.length();
for (int piece = 1; piece <= size; piece++) {
final RS timeValue = this.timeValues.getElement(piece);
if (time1.isLessThanOrEquals(timeValue) && timeValue.isLessThan(time2)) {
return cycle1.multiply(period).add(timeValue);
}
}
assert false : "never reached"; //$NON-NLS-1$
return this.sunit.getNaN();
}
/**
* @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;
}
PeriodicSource<RS,RM,CS,CM> castedObj = (PeriodicSource<RS,RM,CS,CM>)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;
}
}