OpenMapRealMatrix.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.apache.commons.math4.legacy.linear;

  18. import java.io.Serializable;

  19. import org.apache.commons.math4.legacy.exception.DimensionMismatchException;
  20. import org.apache.commons.math4.legacy.exception.NotStrictlyPositiveException;
  21. import org.apache.commons.math4.legacy.exception.NumberIsTooLargeException;
  22. import org.apache.commons.math4.legacy.exception.OutOfRangeException;

  23. /**
  24.  * Sparse matrix implementation based on an open addressed map.
  25.  *
  26.  * <p>
  27.  *  Caveat: This implementation assumes that, for any {@code x},
  28.  *  the equality {@code x * 0d == 0d} holds. But it is is not true for
  29.  *  {@code NaN}. Moreover, zero entries will lose their sign.
  30.  *  Some operations (that involve {@code NaN} and/or infinities) may
  31.  *  thus give incorrect results.
  32.  * </p>
  33.  * @since 2.0
  34.  */
  35. public class OpenMapRealMatrix extends AbstractRealMatrix
  36.     implements SparseRealMatrix, Serializable {
  37.     /** Serializable version identifier. */
  38.     private static final long serialVersionUID = -5962461716457143437L;
  39.     /** Number of rows of the matrix. */
  40.     private final int rows;
  41.     /** Number of columns of the matrix. */
  42.     private final int columns;
  43.     /** Storage for (sparse) matrix elements. */
  44.     private final OpenIntToDoubleHashMap entries;

  45.     /**
  46.      * Build a sparse matrix with the supplied row and column dimensions.
  47.      *
  48.      * @param rowDimension Number of rows of the matrix.
  49.      * @param columnDimension Number of columns of the matrix.
  50.      * @throws NotStrictlyPositiveException if row or column dimension is not
  51.      * positive.
  52.      * @throws NumberIsTooLargeException if the total number of entries of the
  53.      * matrix is larger than {@code Integer.MAX_VALUE}.
  54.      */
  55.     public OpenMapRealMatrix(int rowDimension, int columnDimension)
  56.         throws NotStrictlyPositiveException, NumberIsTooLargeException {
  57.         super(rowDimension, columnDimension);
  58.         long lRow = rowDimension;
  59.         long lCol = columnDimension;
  60.         if (lRow * lCol >= Integer.MAX_VALUE) {
  61.             throw new NumberIsTooLargeException(lRow * lCol, Integer.MAX_VALUE, false);
  62.         }
  63.         this.rows = rowDimension;
  64.         this.columns = columnDimension;
  65.         this.entries = new OpenIntToDoubleHashMap(0.0);
  66.     }

  67.     /**
  68.      * Build a matrix by copying another one.
  69.      *
  70.      * @param matrix matrix to copy.
  71.      */
  72.     public OpenMapRealMatrix(OpenMapRealMatrix matrix) {
  73.         this.rows = matrix.rows;
  74.         this.columns = matrix.columns;
  75.         this.entries = new OpenIntToDoubleHashMap(matrix.entries);
  76.     }

  77.     /** {@inheritDoc} */
  78.     @Override
  79.     public OpenMapRealMatrix copy() {
  80.         return new OpenMapRealMatrix(this);
  81.     }

  82.     /**
  83.      * {@inheritDoc}
  84.      *
  85.      * @throws NumberIsTooLargeException if the total number of entries of the
  86.      * matrix is larger than {@code Integer.MAX_VALUE}.
  87.      */
  88.     @Override
  89.     public OpenMapRealMatrix createMatrix(int rowDimension, int columnDimension)
  90.         throws NotStrictlyPositiveException, NumberIsTooLargeException {
  91.         return new OpenMapRealMatrix(rowDimension, columnDimension);
  92.     }

  93.     /** {@inheritDoc} */
  94.     @Override
  95.     public int getColumnDimension() {
  96.         return columns;
  97.     }

  98.     /**
  99.      * Compute the sum of this matrix and {@code m}.
  100.      *
  101.      * @param m Matrix to be added.
  102.      * @return {@code this} + {@code m}.
  103.      * @throws MatrixDimensionMismatchException if {@code m} is not the same
  104.      * size as {@code this}.
  105.      */
  106.     public OpenMapRealMatrix add(OpenMapRealMatrix m)
  107.         throws MatrixDimensionMismatchException {

  108.         MatrixUtils.checkAdditionCompatible(this, m);

  109.         final OpenMapRealMatrix out = new OpenMapRealMatrix(this);
  110.         for (OpenIntToDoubleHashMap.Iterator iterator = m.entries.iterator(); iterator.hasNext();) {
  111.             iterator.advance();
  112.             final int row = iterator.key() / columns;
  113.             final int col = iterator.key() - row * columns;
  114.             out.setEntry(row, col, getEntry(row, col) + iterator.value());
  115.         }

  116.         return out;
  117.     }

  118.     /** {@inheritDoc} */
  119.     @Override
  120.     public OpenMapRealMatrix subtract(final RealMatrix m)
  121.         throws MatrixDimensionMismatchException {
  122.         if (m instanceof OpenMapRealMatrix) {
  123.             return subtract((OpenMapRealMatrix) m);
  124.         }
  125.         return (OpenMapRealMatrix) super.subtract(m);
  126.     }

  127.     /**
  128.      * Subtract {@code m} from this matrix.
  129.      *
  130.      * @param m Matrix to be subtracted.
  131.      * @return {@code this} - {@code m}.
  132.      * @throws MatrixDimensionMismatchException if {@code m} is not the same
  133.      * size as {@code this}.
  134.      */
  135.     public OpenMapRealMatrix subtract(OpenMapRealMatrix m)
  136.         throws MatrixDimensionMismatchException {
  137.         MatrixUtils.checkAdditionCompatible(this, m);

  138.         final OpenMapRealMatrix out = new OpenMapRealMatrix(this);
  139.         for (OpenIntToDoubleHashMap.Iterator iterator = m.entries.iterator(); iterator.hasNext();) {
  140.             iterator.advance();
  141.             final int row = iterator.key() / columns;
  142.             final int col = iterator.key() - row * columns;
  143.             out.setEntry(row, col, getEntry(row, col) - iterator.value());
  144.         }

  145.         return out;
  146.     }

  147.     /**
  148.      * {@inheritDoc}
  149.      *
  150.      * @throws NumberIsTooLargeException if {@code m} is an
  151.      * {@code OpenMapRealMatrix}, and the total number of entries of the product
  152.      * is larger than {@code Integer.MAX_VALUE}.
  153.      */
  154.     @Override
  155.     public RealMatrix multiply(final RealMatrix m)
  156.         throws DimensionMismatchException, NumberIsTooLargeException {
  157.         if (m instanceof OpenMapRealMatrix) {
  158.             return multiply((OpenMapRealMatrix) m);
  159.         }

  160.         MatrixUtils.checkMultiplicationCompatible(this, m);

  161.         final int outCols = m.getColumnDimension();
  162.         final BlockRealMatrix out = new BlockRealMatrix(rows, outCols);
  163.         for (OpenIntToDoubleHashMap.Iterator iterator = entries.iterator(); iterator.hasNext();) {
  164.             iterator.advance();
  165.             final double value = iterator.value();
  166.             final int key      = iterator.key();
  167.             final int i        = key / columns;
  168.             final int k        = key % columns;
  169.             for (int j = 0; j < outCols; ++j) {
  170.                 out.addToEntry(i, j, value * m.getEntry(k, j));
  171.             }
  172.         }

  173.         return out;
  174.     }

  175.     /**
  176.      * Postmultiply this matrix by {@code m}.
  177.      *
  178.      * @param m Matrix to postmultiply by.
  179.      * @return {@code this} * {@code m}.
  180.      * @throws DimensionMismatchException if the number of rows of {@code m}
  181.      * differ from the number of columns of {@code this} matrix.
  182.      * @throws NumberIsTooLargeException if the total number of entries of the
  183.      * product is larger than {@code Integer.MAX_VALUE}.
  184.      */
  185.     public OpenMapRealMatrix multiply(OpenMapRealMatrix m)
  186.         throws DimensionMismatchException, NumberIsTooLargeException {
  187.         // Safety check.
  188.         MatrixUtils.checkMultiplicationCompatible(this, m);

  189.         final int outCols = m.getColumnDimension();
  190.         OpenMapRealMatrix out = new OpenMapRealMatrix(rows, outCols);
  191.         for (OpenIntToDoubleHashMap.Iterator iterator = entries.iterator(); iterator.hasNext();) {
  192.             iterator.advance();
  193.             final double value = iterator.value();
  194.             final int key      = iterator.key();
  195.             final int i        = key / columns;
  196.             final int k        = key % columns;
  197.             for (int j = 0; j < outCols; ++j) {
  198.                 final int rightKey = m.computeKey(k, j);
  199.                 if (m.entries.containsKey(rightKey)) {
  200.                     final int outKey = out.computeKey(i, j);
  201.                     final double outValue =
  202.                         out.entries.get(outKey) + value * m.entries.get(rightKey);
  203.                     if (outValue == 0.0) {
  204.                         out.entries.remove(outKey);
  205.                     } else {
  206.                         out.entries.put(outKey, outValue);
  207.                     }
  208.                 }
  209.             }
  210.         }

  211.         return out;
  212.     }

  213.     /** {@inheritDoc} */
  214.     @Override
  215.     public double getEntry(int row, int column) throws OutOfRangeException {
  216.         MatrixUtils.checkRowIndex(this, row);
  217.         MatrixUtils.checkColumnIndex(this, column);
  218.         return entries.get(computeKey(row, column));
  219.     }

  220.     /** {@inheritDoc} */
  221.     @Override
  222.     public int getRowDimension() {
  223.         return rows;
  224.     }

  225.     /** {@inheritDoc} */
  226.     @Override
  227.     public void setEntry(int row, int column, double value)
  228.         throws OutOfRangeException {
  229.         MatrixUtils.checkRowIndex(this, row);
  230.         MatrixUtils.checkColumnIndex(this, column);
  231.         if (value == 0.0) {
  232.             entries.remove(computeKey(row, column));
  233.         } else {
  234.             entries.put(computeKey(row, column), value);
  235.         }
  236.     }

  237.     /** {@inheritDoc} */
  238.     @Override
  239.     public void addToEntry(int row, int column, double increment)
  240.         throws OutOfRangeException {
  241.         MatrixUtils.checkRowIndex(this, row);
  242.         MatrixUtils.checkColumnIndex(this, column);
  243.         final int key = computeKey(row, column);
  244.         final double value = entries.get(key) + increment;
  245.         if (value == 0.0) {
  246.             entries.remove(key);
  247.         } else {
  248.             entries.put(key, value);
  249.         }
  250.     }

  251.     /** {@inheritDoc} */
  252.     @Override
  253.     public void multiplyEntry(int row, int column, double factor)
  254.         throws OutOfRangeException {
  255.         MatrixUtils.checkRowIndex(this, row);
  256.         MatrixUtils.checkColumnIndex(this, column);
  257.         final int key = computeKey(row, column);
  258.         final double value = entries.get(key) * factor;
  259.         if (value == 0.0) {
  260.             entries.remove(key);
  261.         } else {
  262.             entries.put(key, value);
  263.         }
  264.     }

  265.     /**
  266.      * Compute the key to access a matrix element.
  267.      * @param row row index of the matrix element
  268.      * @param column column index of the matrix element
  269.      * @return key within the map to access the matrix element
  270.      */
  271.     private int computeKey(int row, int column) {
  272.         return row * columns + column;
  273.     }

  274. }