RoundingFunction.java
/*
* $Id: RoundingFunction.java,v 1.14 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.List;
import org.mklab.nfc.matrix.ComplexNumericalMatrix;
import org.mklab.nfc.matrix.RealNumericalMatrix;
import org.mklab.nfc.ode.PiecewiseContinuousAlgebraicSystem;
import org.mklab.nfc.ode.PiecewiseUtil;
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.14 $, 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 RoundingFunction<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 PiecewiseContinuousAlgebraicSystem<RS,RM,CS,CM>, StringExternalizable {
/** 丸め関数の種類 */
@Parameter(name = "functionType", description = "RoundingFunction.0", internationalization = true)
private RoundingFunctionType type;
/**
* 新しく生成された<code>RoundingFunction</code>オブジェクトを初期化します。
* @param sunit unit of scalar
*/
public RoundingFunction(RS sunit) {
this(RoundingFunctionType.ROUND, sunit);
}
/**
* @see org.mklab.tool.control.system.SystemOperator#toString()
*/
@Override
public String toString() {
return this.type.toString();
}
/**
* 新しく生成された<code>RoundingFunction</code>オブジェクトを初期化します。
*
* @param type 丸め関数の種類
* @param sunit unit of scalar
*/
public RoundingFunction(final RoundingFunctionType type, RS sunit) {
super(-1, -1, sunit);
this.type = type;
setAutoSize(true);
setHasDirectFeedthrough(true);
}
/**
* {@inheritDoc}
*/
@Override
public RM outputEquation( final RS t, final RM u) {
switch (this.type) {
case FLOOR:
return u.floorElementWise();
case CEIL:
return u.ceilElementWise();
case ROUND:
return u.roundElementWise();
case FIX:
return u.fixElementWise();
default:
assert false : "never reached"; //$NON-NLS-1$
}
return this.sunit.createZeroGrid(u.getRowSize(), 1);
}
/**
* @see org.mklab.tool.control.system.SystemOperator#setInputSize(int)
*/
@Override
public void setInputSize(final int size) {
super.setInputSize(size);
super.setOutputSize(size);
}
/**
* @see org.mklab.tool.control.system.SystemOperator#setOutputSize(int)
*/
@Override
public void setOutputSize(final int size) {
super.setOutputSize(size);
super.setInputSize(size);
}
/**
* @see org.mklab.tool.control.system.parameter.StringExternalizable#getString(java.lang.String)
*/
public String getString(String key) {
return Messages.getString(key);
}
/**
* 関数の式を返します。
*
* @return 関数の式
*/
public String getFunction() {
return this.type.getFunction();
}
/**
* @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;
}
RoundingFunction<RS,RM,CS,CM> castedObj = (RoundingFunction<RS,RM,CS,CM>)o;
return ((this.type == null ? castedObj.type == null : this.type.equals(castedObj.type)));
}
/**
* @see org.mklab.tool.control.system.SystemOperator#hashCode()
*/
@Override
public int hashCode() {
int hashCode = super.hashCode();
hashCode = 31 * hashCode + (this.type == null ? 0 : this.type.hashCode());
return hashCode;
}
/**
* @see org.mklab.tool.control.system.SystemOperator#clone()
*/
@Override
public RoundingFunction<RS,RM,CS,CM> clone() {
RoundingFunction<RS,RM,CS,CM> inst = (RoundingFunction<RS,RM,CS,CM>)super.clone();
inst.type = this.type;
return inst;
}
/**
* {@inheritDoc}
*/
@Override
public List<Integer> getPiece(RS t, RM u) {
final int size = u.getRowSize();
final List<Integer> pieces = new ArrayList<>(size);
for (int i = 1; i <= size; i++) {
final RS y = outputEquation(t, u).getElement(i);
pieces.add(Integer.valueOf((int)y.toDouble()));
}
return pieces;
}
/**
* {@inheritDoc}
*/
@SuppressWarnings("boxing")
@Override
public RS getDiscontinuousPoint(RS t1, RM u1, RS t2, RM u2) {
final List<Integer> pieces1 = getPiece(t1, u1);
final List<Integer> pieces2 = getPiece(t2, u2);
if (pieces1.equals(pieces2)) {
return this.sunit.getNaN();
}
final int number = PiecewiseUtil.getDistinctPiece(pieces1, pieces2);
final int piece1 = pieces1.get(number - 1);
final int piece2 = pieces2.get(number - 1);
final RS uu1 = u1.getElement(number);
final RS uu2 = u2.getElement(number);
if (piece1 < piece2) {
final RS halfValue = uu1.floor().add(uu2.ceil()).divide(2);
final RS halfValueTime = t1.add((halfValue.subtract(uu1)).divide((uu2.subtract(uu1))).multiply((t2.subtract(t1))));
if (t2.isLessThan(halfValueTime)) {
return t1.add(t2).divide(2);
}
return halfValueTime;
}
final RS halfValue = uu2.floor() .add(uu1.ceil()).divide(2);
final RS halfValueTime = t1.add((uu1.subtract(halfValue)).divide((uu1.subtract(uu2))).multiply(t2.subtract(t1)));
if (t2.isLessThan(halfValueTime)) {
return t1.add(t2).divide(2);
}
return halfValueTime;
}
}