SparseFieldMatrix.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 org.apache.commons.math4.legacy.core.Field;
  19. import org.apache.commons.math4.legacy.core.FieldElement;

  20. /**
  21.  * Sparse matrix implementation based on an open addressed map.
  22.  *
  23.  * <p>
  24.  *  Caveat: This implementation assumes that, for any {@code x},
  25.  *  the equality {@code x * 0d == 0d} holds. But it is is not true for
  26.  *  {@code NaN}. Moreover, zero entries will lose their sign.
  27.  *  Some operations (that involve {@code NaN} and/or infinities) may
  28.  *  thus give incorrect results.
  29.  * </p>
  30.  * @param <T> the type of the field elements
  31.  * @since 2.0
  32.  */
  33. public class SparseFieldMatrix<T extends FieldElement<T>> extends AbstractFieldMatrix<T> {

  34.     /** Storage for (sparse) matrix elements. */
  35.     private final OpenIntToFieldHashMap<T> entries;
  36.     /** Row dimension. */
  37.     private final int rows;
  38.     /** Column dimension. */
  39.     private final int columns;

  40.     /**
  41.      * Create a matrix with no data.
  42.      *
  43.      * @param field Field to which the elements belong.
  44.      */
  45.     public SparseFieldMatrix(final Field<T> field) {
  46.         super(field);
  47.         rows = 0;
  48.         columns= 0;
  49.         entries = new OpenIntToFieldHashMap<>(field);
  50.     }

  51.     /**
  52.      * Create a new {@code SparseFieldMatrix<T>} with the supplied row and column
  53.      * dimensions.
  54.      *
  55.      * @param field Field to which the elements belong.
  56.      * @param rowDimension Number of rows in the new matrix.
  57.      * @param columnDimension Number of columns in the new matrix.
  58.      * @throws org.apache.commons.math4.legacy.exception.NotStrictlyPositiveException
  59.      * if row or column dimension is not positive.
  60.      */
  61.     public SparseFieldMatrix(final Field<T> field,
  62.                              final int rowDimension, final int columnDimension) {
  63.         super(field, rowDimension, columnDimension);
  64.         this.rows = rowDimension;
  65.         this.columns = columnDimension;
  66.         entries = new OpenIntToFieldHashMap<>(field);
  67.     }

  68.     /**
  69.      * Copy constructor.
  70.      *
  71.      * @param other Instance to copy.
  72.      */
  73.     public SparseFieldMatrix(SparseFieldMatrix<T> other) {
  74.         super(other.getField(), other.getRowDimension(), other.getColumnDimension());
  75.         rows = other.getRowDimension();
  76.         columns = other.getColumnDimension();
  77.         entries = new OpenIntToFieldHashMap<>(other.entries);
  78.     }

  79.     /**
  80.      * Generic copy constructor.
  81.      *
  82.      * @param other Instance to copy.
  83.      */
  84.     public SparseFieldMatrix(FieldMatrix<T> other){
  85.         super(other.getField(), other.getRowDimension(), other.getColumnDimension());
  86.         rows = other.getRowDimension();
  87.         columns = other.getColumnDimension();
  88.         entries = new OpenIntToFieldHashMap<>(getField());
  89.         for (int i = 0; i < rows; i++) {
  90.             for (int j = 0; j < columns; j++) {
  91.                 setEntry(i, j, other.getEntry(i, j));
  92.             }
  93.         }
  94.     }

  95.     /** {@inheritDoc} */
  96.     @Override
  97.     public void addToEntry(int row, int column, T increment) {
  98.         checkRowIndex(row);
  99.         checkColumnIndex(column);
  100.         final int key = computeKey(row, column);
  101.         final T value = entries.get(key).add(increment);
  102.         if (getField().getZero().equals(value)) {
  103.             entries.remove(key);
  104.         } else {
  105.             entries.put(key, value);
  106.         }
  107.     }

  108.     /** {@inheritDoc} */
  109.     @Override
  110.     public FieldMatrix<T> copy() {
  111.         return new SparseFieldMatrix<>(this);
  112.     }

  113.     /** {@inheritDoc} */
  114.     @Override
  115.     public FieldMatrix<T> createMatrix(int rowDimension, int columnDimension) {
  116.         return new SparseFieldMatrix<>(getField(), rowDimension, columnDimension);
  117.     }

  118.     /** {@inheritDoc} */
  119.     @Override
  120.     public int getColumnDimension() {
  121.         return columns;
  122.     }

  123.     /** {@inheritDoc} */
  124.     @Override
  125.     public T getEntry(int row, int column) {
  126.         checkRowIndex(row);
  127.         checkColumnIndex(column);
  128.         return entries.get(computeKey(row, column));
  129.     }

  130.     /** {@inheritDoc} */
  131.     @Override
  132.     public int getRowDimension() {
  133.         return rows;
  134.     }

  135.     /** {@inheritDoc} */
  136.     @Override
  137.     public void multiplyEntry(int row, int column, T factor) {
  138.         checkRowIndex(row);
  139.         checkColumnIndex(column);
  140.         final int key = computeKey(row, column);
  141.         final T value = entries.get(key).multiply(factor);
  142.         if (getField().getZero().equals(value)) {
  143.             entries.remove(key);
  144.         } else {
  145.             entries.put(key, value);
  146.         }
  147.     }

  148.     /** {@inheritDoc} */
  149.     @Override
  150.     public void setEntry(int row, int column, T value) {
  151.         checkRowIndex(row);
  152.         checkColumnIndex(column);
  153.         if (getField().getZero().equals(value)) {
  154.             entries.remove(computeKey(row, column));
  155.         } else {
  156.             entries.put(computeKey(row, column), value);
  157.         }
  158.     }

  159.     /**
  160.      * Compute the key to access a matrix element.
  161.      *
  162.      * @param row Row index of the matrix element.
  163.      * @param column Column index of the matrix element.
  164.      * @return the key within the map to access the matrix element.
  165.      */
  166.     private int computeKey(int row, int column) {
  167.         return row * columns + column;
  168.     }
  169. }