DoubleQuantizer.java

/*
 * $Id: Quantizer.java,v 1.15 2008/07/15 15:27:15 koga Exp $
 *
 * Copyright (C) 2004 Masanobu Koga. All rights reserved.
 */
package org.mklab.tool.control.system.discontinuous;

import java.util.ArrayList;
import java.util.List;

import org.mklab.nfc.matrix.DoubleMatrix;
import org.mklab.nfc.ode.DoublePiecewiseContinuousAlgebraicSystem;
import org.mklab.nfc.ode.PiecewiseUtil;
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
 * @version $Revision: 1.15 $
 */
public class DoubleQuantizer extends DoubleBaseContinuousStaticSystem implements DoublePiecewiseContinuousAlgebraicSystem, StringExternalizable {

  /** 量子化間隔 */
  @Parameter(name = "interval", description = "Quantizer.1", internationalization = true)
  private double interval;

  /**
   * コンストラクター
   */
  public DoubleQuantizer() {
    this(0.5);
  }

  /**
   * 新しく生成された<code>Saturation</code>オブジェクトを初期化します。
   * 
   * @param interval 量子化間隔
   */
  public DoubleQuantizer(final double interval) {
    super(-1, -1);
    setAutoSize(true);
    setHasDirectFeedthrough(true);
    setInterval(interval);
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public DoubleMatrix outputEquation( final double t, final DoubleMatrix u) {
    return u.divide(this.interval).fixElementWise().multiply(this.interval);
  }

  /**
   * 量子化間隔を設定します。
   * 
   * @param interval 量子化間隔
   */
  public void setInterval(final double interval) {
    this.interval = interval;
  }

  /**
   * 量子化間隔を返します。
   * 
   * @return 量子化間隔
   */
  public double getInterval() {
    return this.interval;
  }

  /**
   * {@inheritDoc}
   */
  @SuppressWarnings("boxing")
  public List<Integer> getPiece( final double t, final DoubleMatrix u) {
    final int size = u.getRowSize();
    final List<Integer> pieces = new ArrayList<>(size);

    for (int i = 1; i <= size; i++) {
      final double uu = u.getDoubleElement(i, 1) / this.interval;
      final int piece;
      if (0 <= uu) {
        piece = (int)Math.floor(uu);
      } else {
        piece = (int)Math.ceil(uu);
      }
      pieces.add(piece);
    }

    return pieces;
  }

  /**
   * {@inheritDoc}
   */
  @SuppressWarnings("boxing")
  public double getDiscontinuousPoint(final double t1, final DoubleMatrix u1, final double t2, final DoubleMatrix u2) {
    final List<Integer> pieces1 = getPiece(t1, u1);
    final List<Integer> pieces2 = getPiece(t2, u2);

    if (pieces1.equals(pieces2)) {
      return Double.NaN;
    }

    final int number = PiecewiseUtil.getDistinctPiece(pieces1, pieces2);
    final int piece1 = pieces1.get(number - 1);
    final int piece2 = pieces2.get(number - 1);

    int nextPiece;
    if (piece1 < piece2) {
      if (piece1 < 0) {
        nextPiece = piece1;
      } else {
        nextPiece = piece1 + 1;
      }
    } else {
      if (piece1 <= 0) {
        nextPiece = piece1 - 1;
      } else {
        nextPiece = piece1;
      }
    }

    final double uu1 = u1.getDoubleElement(number, 1);
    final double uu2 = u2.getDoubleElement(number, 1);

    final double nextLevel = this.interval * nextPiece;

    return t1 + (nextLevel - uu1) / (uu2 - uu1) * (t2 - t1);
  }

  /**
   * @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.setInputSize(size);
    super.setOutputSize(size);
  }

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