DoubleConnectionMatrix.java

/*
 * Created on 2006/11/05
 * Copyright (C) 2006 Koga Laboratory. All rights reserved.
 *
 */
package org.mklab.tool.control.system.graph;

import org.mklab.nfc.matrix.BooleanMatrix;
import org.mklab.nfc.matrix.DoubleMatrix;
import org.mklab.nfc.matrix.FundamentalArray;
import org.mklab.tool.control.system.DoubleSystemOperator;


/**
 * 連結行列(Connection Matrix)を表すクラスです。
 * 
 * @author koga
 * @version $Revision: 1.8 $, 2006/11/05
 */
public class DoubleConnectionMatrix {

  /** 連結情報を表わす行列 */
  private BooleanMatrix connectionMatrix;

  /**
   * 新しく生成された<code>ConnectionMatrix</code>オブジェクトを初期化します。
   * 
   * @param adjacencyMatrix 隣接行列
   */
  public DoubleConnectionMatrix(final FundamentalArray<DoubleSystemOperator,?> adjacencyMatrix) {
    final int rowSize = adjacencyMatrix.getRowSize();
    final int columnSize = adjacencyMatrix.getColumnSize();

    this.connectionMatrix = new BooleanMatrix(rowSize, columnSize);

    for (int row = 0; row < rowSize; row++) {
      for (int column = 0; column < columnSize; column++) {
        if (row == column) {
          this.connectionMatrix.setElement(row + 1, column + 1, true);
        } else if (adjacencyMatrix.getElement(row + 1, column + 1).isZero() == false) {
          this.connectionMatrix.setElement(row + 1, column + 1, true);
        }
      }
    }
  }

  /**
   * 新しく生成された<code>ConnectionMatrix</code>オブジェクトを初期化します。
   * 
   * @param adjacencyMatrix 隣接行列
   */
  public DoubleConnectionMatrix(final DoubleMatrix adjacencyMatrix) {
    final int rowSize = adjacencyMatrix.getRowSize();
    final int columnSize = adjacencyMatrix.getColumnSize();

    this.connectionMatrix = new BooleanMatrix(rowSize, columnSize);

    for (int row = 0; row < rowSize; row++) {
      for (int column = 0; column < columnSize; column++) {
        if (row == column) {
          this.connectionMatrix.setElement(row + 1, column + 1, true);
        } else if (adjacencyMatrix.getDoubleElement(row + 1, column + 1) != 0) {
          this.connectionMatrix.setElement(row + 1, column + 1, true);
        }
      }
    }
  }

  /**
   * 連結情報を表わす行列を返します。
   * 
   * @return 連結情報を表わす行列
   */
  public BooleanMatrix getBooleanMatrix() {
    return this.connectionMatrix.createClone();
  }

  /**
   * 行列の次数を返します。
   * 
   * @return 行列の次数
   */
  public int getSize() {
    return this.connectionMatrix.getRowSize();
  }

  /**
   * @see java.lang.Object#hashCode()
   */
  @Override
  public int hashCode() {
    final int PRIME = 31;
    int result = 1;
    result = PRIME * result + ((this.connectionMatrix == null) ? 0 : this.connectionMatrix.hashCode());
    return result;
  }

  /**
   * @see java.lang.Object#equals(java.lang.Object)
   */
  @Override
  public boolean equals(Object obj) {
    if (this == obj) {
      return true;
    }
    if (obj == null) {
      return false;
    }
    if (getClass() != obj.getClass()) {
      return false;
    }
    final DoubleConnectionMatrix other = (DoubleConnectionMatrix)obj;
    if (this.connectionMatrix == null) {
      if (other.connectionMatrix != null) {
        return false;
      }
    } else if (!this.connectionMatrix.equals(other.connectionMatrix)) {
      return false;
    }
    return true;
  }
}