Array2DRowFieldMatrix.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.  *      https://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. /*
  18.  * This is not the original file distributed by the Apache Software Foundation
  19.  * It has been modified by the Hipparchus project
  20.  */

  21. package org.hipparchus.linear;

  22. import java.io.Serializable;
  23. import org.hipparchus.Field;
  24. import org.hipparchus.FieldElement;
  25. import org.hipparchus.exception.LocalizedCoreFormats;
  26. import org.hipparchus.exception.MathIllegalArgumentException;
  27. import org.hipparchus.exception.MathIllegalStateException;
  28. import org.hipparchus.exception.NullArgumentException;
  29. import org.hipparchus.util.MathArrays;
  30. import org.hipparchus.util.MathUtils;

  31. /**
  32.  * Implementation of {@link FieldMatrix} using a {@link FieldElement}[][] array to store entries.
  33.  * <p>
  34.  * As specified in the {@link FieldMatrix} interface, matrix element indexing
  35.  * is 0-based -- e.g., {@code getEntry(0, 0)}
  36.  * returns the element in the first row, first column of the matrix
  37.  * </p>
  38.  *
  39.  * @param <T> the type of the field elements
  40.  */
  41. public class Array2DRowFieldMatrix<T extends FieldElement<T>>
  42.     extends AbstractFieldMatrix<T>
  43.     implements Serializable {
  44.     /** Serializable version identifier */
  45.     private static final long serialVersionUID = 7260756672015356458L;
  46.     /** Entries of the matrix */
  47.     private T[][] data;

  48.     /**
  49.      * Creates a matrix with no data
  50.      * @param field field to which the elements belong
  51.      */
  52.     public Array2DRowFieldMatrix(final Field<T> field) {
  53.         super(field);
  54.     }

  55.     /**
  56.      * Create a new {@code FieldMatrix<T>} with the supplied row and column dimensions.
  57.      *
  58.      * @param field Field to which the elements belong.
  59.      * @param rowDimension Number of rows in the new matrix.
  60.      * @param columnDimension Number of columns in the new matrix.
  61.      * @throws MathIllegalArgumentException if row or column dimension is not positive.
  62.      */
  63.     public Array2DRowFieldMatrix(final Field<T> field, final int rowDimension,
  64.                                  final int columnDimension)
  65.         throws MathIllegalArgumentException {
  66.         super(field, rowDimension, columnDimension);
  67.         data = MathArrays.buildArray(field, rowDimension, columnDimension);
  68.     }

  69.     /**
  70.      * Create a new {@code FieldMatrix<T>} using the input array as the underlying
  71.      * data array.
  72.      * <p>The input array is copied, not referenced. This constructor has
  73.      * the same effect as calling {@link #Array2DRowFieldMatrix(FieldElement[][], boolean)}
  74.      * with the second argument set to {@code true}.</p>
  75.      *
  76.      * @param d Data for the new matrix.
  77.      * @throws MathIllegalArgumentException if {@code d} is not rectangular.
  78.      * @throws NullArgumentException if {@code d} is {@code null}.
  79.      * @throws MathIllegalArgumentException if there are not at least one row and one column.
  80.      * @see #Array2DRowFieldMatrix(FieldElement[][], boolean)
  81.      */
  82.     public Array2DRowFieldMatrix(final T[][] d)
  83.         throws MathIllegalArgumentException, NullArgumentException {
  84.         this(extractField(d), d);
  85.     }

  86.     /**
  87.      * Create a new {@code FieldMatrix<T>} using the input array as the underlying
  88.      * data array.
  89.      * <p>The input array is copied, not referenced. This constructor has
  90.      * the same effect as calling {@link #Array2DRowFieldMatrix(FieldElement[][], boolean)}
  91.      * with the second argument set to {@code true}.</p>
  92.      *
  93.      * @param field Field to which the elements belong.
  94.      * @param d Data for the new matrix.
  95.      * @throws MathIllegalArgumentException if {@code d} is not rectangular.
  96.      * @throws NullArgumentException if {@code d} is {@code null}.
  97.      * @throws MathIllegalArgumentException if there are not at least one row and one column.
  98.      * @see #Array2DRowFieldMatrix(FieldElement[][], boolean)
  99.      */
  100.     public Array2DRowFieldMatrix(final Field<T> field, final T[][] d)
  101.         throws MathIllegalArgumentException, NullArgumentException {
  102.         super(field);
  103.         copyIn(d);
  104.     }

  105.     /**
  106.      * Create a new {@code FieldMatrix<T>} using the input array as the underlying
  107.      * data array.
  108.      * <p>If an array is built specially in order to be embedded in a
  109.      * {@code FieldMatrix<T>} and not used directly, the {@code copyArray} may be
  110.      * set to {@code false}. This will prevent the copying and improve
  111.      * performance as no new array will be built and no data will be copied.</p>
  112.      *
  113.      * @param d Data for the new matrix.
  114.      * @param copyArray Whether to copy or reference the input array.
  115.      * @throws MathIllegalArgumentException if {@code d} is not rectangular.
  116.      * @throws MathIllegalArgumentException if there are not at least one row and one column.
  117.      * @throws NullArgumentException if {@code d} is {@code null}.
  118.      * @see #Array2DRowFieldMatrix(FieldElement[][])
  119.      */
  120.     public Array2DRowFieldMatrix(final T[][] d, final boolean copyArray)
  121.         throws MathIllegalArgumentException,
  122.         NullArgumentException {
  123.         this(extractField(d), d, copyArray);
  124.     }

  125.     /**
  126.      * Create a new {@code FieldMatrix<T>} using the input array as the underlying
  127.      * data array.
  128.      * <p>If an array is built specially in order to be embedded in a
  129.      * {@code FieldMatrix<T>} and not used directly, the {@code copyArray} may be
  130.      * set to {@code false}. This will prevent the copying and improve
  131.      * performance as no new array will be built and no data will be copied.</p>
  132.      *
  133.      * @param field Field to which the elements belong.
  134.      * @param d Data for the new matrix.
  135.      * @param copyArray Whether to copy or reference the input array.
  136.      * @throws MathIllegalArgumentException if {@code d} is not rectangular.
  137.      * @throws MathIllegalArgumentException if there are not at least one row and one column.
  138.      * @throws NullArgumentException if {@code d} is {@code null}.
  139.      * @see #Array2DRowFieldMatrix(FieldElement[][])
  140.      */
  141.     public Array2DRowFieldMatrix(final Field<T> field, final T[][] d, final boolean copyArray)
  142.         throws MathIllegalArgumentException, NullArgumentException {
  143.         super(field);
  144.         if (copyArray) {
  145.             copyIn(d);
  146.         } else {
  147.             MathUtils.checkNotNull(d);
  148.             final int nRows = d.length;
  149.             if (nRows == 0) {
  150.                 throw new MathIllegalArgumentException(LocalizedCoreFormats.AT_LEAST_ONE_ROW);
  151.             }
  152.             final int nCols = d[0].length;
  153.             if (nCols == 0) {
  154.                 throw new MathIllegalArgumentException(LocalizedCoreFormats.AT_LEAST_ONE_COLUMN);
  155.             }
  156.             for (int r = 1; r < nRows; r++) {
  157.                 if (d[r].length != nCols) {
  158.                     throw new MathIllegalArgumentException(LocalizedCoreFormats.DIMENSIONS_MISMATCH,
  159.                                                            nCols, d[r].length);
  160.                 }
  161.             }
  162.             data = d; // NOPMD - array copy is taken care of by parameter
  163.         }
  164.     }

  165.     /**
  166.      * Create a new (column) {@code FieldMatrix<T>} using {@code v} as the
  167.      * data for the unique column of the created matrix.
  168.      * The input array is copied.
  169.      *
  170.      * @param v Column vector holding data for new matrix.
  171.      * @throws MathIllegalArgumentException if v is empty
  172.      */
  173.     public Array2DRowFieldMatrix(final T[] v) throws MathIllegalArgumentException {
  174.         this(extractField(v), v);
  175.     }

  176.     /**
  177.      * Create a new (column) {@code FieldMatrix<T>} using {@code v} as the
  178.      * data for the unique column of the created matrix.
  179.      * The input array is copied.
  180.      *
  181.      * @param field Field to which the elements belong.
  182.      * @param v Column vector holding data for new matrix.
  183.      */
  184.     public Array2DRowFieldMatrix(final Field<T> field, final T[] v) {
  185.         super(field);
  186.         final int nRows = v.length;
  187.         data = MathArrays.buildArray(getField(), nRows, 1);
  188.         for (int row = 0; row < nRows; row++) {
  189.             data[row][0] = v[row];
  190.         }
  191.     }

  192.     /** {@inheritDoc} */
  193.     @Override
  194.     public FieldMatrix<T> createMatrix(final int rowDimension,
  195.                                        final int columnDimension)
  196.         throws MathIllegalArgumentException {
  197.         return new Array2DRowFieldMatrix<>(getField(), rowDimension, columnDimension);
  198.     }

  199.     /** {@inheritDoc} */
  200.     @Override
  201.     public FieldMatrix<T> copy() {
  202.         return new Array2DRowFieldMatrix<>(getField(), copyOut(), false);
  203.     }

  204.     /**
  205.      * Add {@code m} to this matrix.
  206.      *
  207.      * @param m Matrix to be added.
  208.      * @return {@code this} + m.
  209.      * @throws MathIllegalArgumentException if {@code m} is not the same
  210.      * size as this matrix.
  211.      */
  212.     public Array2DRowFieldMatrix<T> add(final Array2DRowFieldMatrix<T> m)
  213.         throws MathIllegalArgumentException {
  214.         // safety check
  215.         checkAdditionCompatible(m);

  216.         final int rowCount    = getRowDimension();
  217.         final int columnCount = getColumnDimension();
  218.         final T[][] outData = MathArrays.buildArray(getField(), rowCount, columnCount);
  219.         for (int row = 0; row < rowCount; row++) {
  220.             final T[] dataRow    = data[row];
  221.             final T[] mRow       = m.data[row];
  222.             final T[] outDataRow = outData[row];
  223.             for (int col = 0; col < columnCount; col++) {
  224.                 outDataRow[col] = dataRow[col].add(mRow[col]);
  225.             }
  226.         }

  227.         return new Array2DRowFieldMatrix<>(getField(), outData, false);
  228.     }

  229.     /**
  230.      * Subtract {@code m} from this matrix.
  231.      *
  232.      * @param m Matrix to be subtracted.
  233.      * @return {@code this} + m.
  234.      * @throws MathIllegalArgumentException if {@code m} is not the same
  235.      * size as this matrix.
  236.      */
  237.     public Array2DRowFieldMatrix<T> subtract(final Array2DRowFieldMatrix<T> m)
  238.         throws MathIllegalArgumentException {
  239.         // safety check
  240.         checkSubtractionCompatible(m);

  241.         final int rowCount    = getRowDimension();
  242.         final int columnCount = getColumnDimension();
  243.         final T[][] outData = MathArrays.buildArray(getField(), rowCount, columnCount);
  244.         for (int row = 0; row < rowCount; row++) {
  245.             final T[] dataRow    = data[row];
  246.             final T[] mRow       = m.data[row];
  247.             final T[] outDataRow = outData[row];
  248.             for (int col = 0; col < columnCount; col++) {
  249.                 outDataRow[col] = dataRow[col].subtract(mRow[col]);
  250.             }
  251.         }

  252.         return new Array2DRowFieldMatrix<>(getField(), outData, false);

  253.     }

  254.     /**
  255.      * Postmultiplying this matrix by {@code m}.
  256.      *
  257.      * @param m Matrix to postmultiply by.
  258.      * @return {@code this} * m.
  259.      * @throws MathIllegalArgumentException if the number of columns of this
  260.      * matrix is not equal to the number of rows of {@code m}.
  261.      */
  262.     public Array2DRowFieldMatrix<T> multiply(final Array2DRowFieldMatrix<T> m)
  263.         throws MathIllegalArgumentException {
  264.         // safety check
  265.         checkMultiplicationCompatible(m);

  266.         final int nRows = this.getRowDimension();
  267.         final int nCols = m.getColumnDimension();
  268.         final int nSum = this.getColumnDimension();
  269.         final T[][] outData = MathArrays.buildArray(getField(), nRows, nCols);
  270.         for (int row = 0; row < nRows; row++) {
  271.             final T[] dataRow    = data[row];
  272.             final T[] outDataRow = outData[row];
  273.             for (int col = 0; col < nCols; col++) {
  274.                 T sum = getField().getZero();
  275.                 for (int i = 0; i < nSum; i++) {
  276.                     sum = sum.add(dataRow[i].multiply(m.data[i][col]));
  277.                 }
  278.                 outDataRow[col] = sum;
  279.             }
  280.         }

  281.         return new Array2DRowFieldMatrix<>(getField(), outData, false);

  282.     }

  283.     /**
  284.      * Returns the result of postmultiplying {@code this} by {@code m^T}.
  285.      * @param m matrix to first transpose and second postmultiply by
  286.      * @return {@code this * m^T}
  287.      * @throws MathIllegalArgumentException if
  288.      * {@code columnDimension(this) != columnDimension(m)}
  289.      * @since 1.3
  290.      */
  291.     public FieldMatrix<T> multiplyTransposed(final Array2DRowFieldMatrix<T> m)
  292.         throws MathIllegalArgumentException {
  293.         MatrixUtils.checkSameColumnDimension(this, m);

  294.         final int nRows = this.getRowDimension();
  295.         final int nCols = m.getRowDimension();
  296.         final int nSum  = this.getColumnDimension();

  297.         final FieldMatrix<T> out   = MatrixUtils.createFieldMatrix(getField(), nRows, nCols);
  298.         final T[][]          mData = m.data;

  299.         // Multiply.
  300.         for (int col = 0; col < nCols; col++) {
  301.             for (int row = 0; row < nRows; row++) {
  302.                 final T[] dataRow = data[row];
  303.                 final T[] mRow    = mData[col];
  304.                 T sum = getField().getZero();
  305.                 for (int i = 0; i < nSum; i++) {
  306.                     sum = sum.add(dataRow[i].multiply(mRow[i]));
  307.                 }
  308.                 out.setEntry(row, col, sum);
  309.             }
  310.         }

  311.         return out;

  312.     }

  313.     /** {@inheritDoc} */
  314.     @Override
  315.     public FieldMatrix<T> multiplyTransposed(final FieldMatrix<T> m) {
  316.         if (m instanceof Array2DRowFieldMatrix) {
  317.             return multiplyTransposed((Array2DRowFieldMatrix<T>) m);
  318.         } else {
  319.             MatrixUtils.checkSameColumnDimension(this, m);

  320.             final int nRows = this.getRowDimension();
  321.             final int nCols = m.getRowDimension();
  322.             final int nSum  = this.getColumnDimension();

  323.             final FieldMatrix<T> out = MatrixUtils.createFieldMatrix(getField(), nRows, nCols);

  324.             // Multiply.
  325.             for (int col = 0; col < nCols; col++) {
  326.                 for (int row = 0; row < nRows; row++) {
  327.                     final T[] dataRow = data[row];
  328.                     T sum = getField().getZero();
  329.                     for (int i = 0; i < nSum; i++) {
  330.                         sum = sum.add(dataRow[i].multiply(m.getEntry(col, i)));
  331.                     }
  332.                     out.setEntry(row, col, sum);
  333.                 }
  334.             }

  335.             return out;

  336.         }
  337.     }

  338.     /**
  339.      * Returns the result of postmultiplying {@code this^T} by {@code m}.
  340.      * @param m matrix to postmultiply by
  341.      * @return {@code this^T * m}
  342.      * @throws MathIllegalArgumentException if
  343.      * {@code columnDimension(this) != columnDimension(m)}
  344.      * @since 1.3
  345.      */
  346.     public FieldMatrix<T> transposeMultiply(final Array2DRowFieldMatrix<T> m)
  347.         throws MathIllegalArgumentException {
  348.         MatrixUtils.checkSameRowDimension(this, m);

  349.         final int nRows = this.getColumnDimension();
  350.         final int nCols = m.getColumnDimension();
  351.         final int nSum  = this.getRowDimension();

  352.         final FieldMatrix<T> out   = MatrixUtils.createFieldMatrix(getField(), nRows, nCols);
  353.         final T[][]          mData = m.data;

  354.         // Multiply.
  355.         for (int k = 0; k < nSum; k++) {
  356.             final T[] dataK = data[k];
  357.             final T[] mK    = mData[k];
  358.             for (int row = 0; row < nRows; row++) {
  359.                 final T dataIRow = dataK[row];
  360.                 for (int col = 0; col < nCols; col++) {
  361.                     out.addToEntry(row, col, dataIRow.multiply(mK[col]));
  362.                 }
  363.             }
  364.         }

  365.         return out;
  366.     }

  367.     /** {@inheritDoc} */
  368.     @Override
  369.     public FieldMatrix<T> transposeMultiply(final FieldMatrix<T> m) {
  370.         if (m instanceof Array2DRowFieldMatrix) {
  371.             return transposeMultiply((Array2DRowFieldMatrix<T>) m);
  372.         } else {
  373.             MatrixUtils.checkSameRowDimension(this, m);

  374.             final int nRows = this.getColumnDimension();
  375.             final int nCols = m.getColumnDimension();
  376.             final int nSum  = this.getRowDimension();

  377.             final FieldMatrix<T> out = MatrixUtils.createFieldMatrix(getField(), nRows, nCols);

  378.             // Multiply.
  379.             for (int k = 0; k < nSum; k++) {
  380.                 final T[] dataK = data[k];
  381.                 for (int row = 0; row < nRows; row++) {
  382.                     final T dataIRow = dataK[row];
  383.                     for (int col = 0; col < nCols; col++) {
  384.                         out.addToEntry(row, col, dataIRow.multiply(m.getEntry(k, col)));
  385.                     }
  386.                 }
  387.             }

  388.             return out;

  389.         }
  390.     }

  391.     /** {@inheritDoc} */
  392.     @Override
  393.     public T[][] getData() {
  394.         return copyOut();
  395.     }

  396.     /**
  397.      * Get a reference to the underlying data array.
  398.      * This methods returns internal data, <strong>not</strong> fresh copy of it.
  399.      *
  400.      * @return the 2-dimensional array of entries.
  401.      */
  402.     public T[][] getDataRef() {
  403.         return data; // NOPMD - returning an internal array is intentional and documented here
  404.     }

  405.     /** {@inheritDoc} */
  406.     @Override
  407.     public void setSubMatrix(final T[][] subMatrix, final int row,
  408.                              final int column)
  409.         throws MathIllegalArgumentException, NullArgumentException {
  410.         if (data == null) {
  411.             if (row > 0) {
  412.                 throw new MathIllegalStateException(LocalizedCoreFormats.FIRST_ROWS_NOT_INITIALIZED_YET, row);
  413.             }
  414.             if (column > 0) {
  415.                 throw new MathIllegalStateException(LocalizedCoreFormats.FIRST_COLUMNS_NOT_INITIALIZED_YET, column);
  416.             }
  417.             final int nRows = subMatrix.length;
  418.             if (nRows == 0) {
  419.                 throw new MathIllegalArgumentException(LocalizedCoreFormats.AT_LEAST_ONE_ROW);
  420.             }

  421.             final int nCols = subMatrix[0].length;
  422.             if (nCols == 0) {
  423.                 throw new MathIllegalArgumentException(LocalizedCoreFormats.AT_LEAST_ONE_COLUMN);
  424.             }
  425.             data = MathArrays.buildArray(getField(), subMatrix.length, nCols);
  426.             for (int i = 0; i < data.length; ++i) {
  427.                 if (subMatrix[i].length != nCols) {
  428.                     throw new MathIllegalArgumentException(LocalizedCoreFormats.DIMENSIONS_MISMATCH,
  429.                                                            nCols, subMatrix[i].length);
  430.                 }
  431.                 System.arraycopy(subMatrix[i], 0, data[i + row], column, nCols);
  432.             }
  433.         } else {
  434.             super.setSubMatrix(subMatrix, row, column);
  435.         }

  436.     }

  437.     /** {@inheritDoc} */
  438.     @Override
  439.     public T getEntry(final int row, final int column)
  440.         throws MathIllegalArgumentException {
  441.       try {
  442.         return data[row][column];
  443.       } catch (IndexOutOfBoundsException e) {
  444.         // throw the exact cause of the exception
  445.         checkRowIndex(row);
  446.         checkColumnIndex(column);
  447.         // should never happen
  448.         throw e;
  449.       }
  450.     }

  451.     /** {@inheritDoc} */
  452.     @Override
  453.     public void setEntry(final int row, final int column, final T value)
  454.         throws MathIllegalArgumentException {
  455.         checkRowIndex(row);
  456.         checkColumnIndex(column);

  457.         data[row][column] = value;
  458.     }

  459.     /** {@inheritDoc} */
  460.     @Override
  461.     public void addToEntry(final int row, final int column, final T increment)
  462.         throws MathIllegalArgumentException {
  463.         checkRowIndex(row);
  464.         checkColumnIndex(column);

  465.         data[row][column] = data[row][column].add(increment);
  466.     }

  467.     /** {@inheritDoc} */
  468.     @Override
  469.     public void multiplyEntry(final int row, final int column, final T factor)
  470.         throws MathIllegalArgumentException {
  471.         checkRowIndex(row);
  472.         checkColumnIndex(column);

  473.         data[row][column] = data[row][column].multiply(factor);
  474.     }

  475.     /** {@inheritDoc} */
  476.     @Override
  477.     public int getRowDimension() {
  478.         return (data == null) ? 0 : data.length;
  479.     }

  480.     /** {@inheritDoc} */
  481.     @Override
  482.     public int getColumnDimension() {
  483.         return ((data == null) || (data[0] == null)) ? 0 : data[0].length;
  484.     }

  485.     /** {@inheritDoc} */
  486.     @Override
  487.     public T[] operate(final T[] v) throws MathIllegalArgumentException {
  488.         final int nRows = this.getRowDimension();
  489.         final int nCols = this.getColumnDimension();
  490.         if (v.length != nCols) {
  491.             throw new MathIllegalArgumentException(LocalizedCoreFormats.DIMENSIONS_MISMATCH,
  492.                                                    v.length, nCols);
  493.         }
  494.         final T[] out = MathArrays.buildArray(getField(), nRows);
  495.         for (int row = 0; row < nRows; row++) {
  496.             final T[] dataRow = data[row];
  497.             T sum = getField().getZero();
  498.             for (int i = 0; i < nCols; i++) {
  499.                 sum = sum.add(dataRow[i].multiply(v[i]));
  500.             }
  501.             out[row] = sum;
  502.         }
  503.         return out;
  504.     }

  505.     /** {@inheritDoc} */
  506.     @Override
  507.     public T[] preMultiply(final T[] v) throws MathIllegalArgumentException {
  508.         final int nRows = getRowDimension();
  509.         final int nCols = getColumnDimension();
  510.         if (v.length != nRows) {
  511.             throw new MathIllegalArgumentException(LocalizedCoreFormats.DIMENSIONS_MISMATCH,
  512.                                                    v.length, nRows);
  513.         }

  514.         final T[] out = MathArrays.buildArray(getField(), nCols);
  515.         for (int col = 0; col < nCols; ++col) {
  516.             T sum = getField().getZero();
  517.             for (int i = 0; i < nRows; ++i) {
  518.                 sum = sum.add(data[i][col].multiply(v[i]));
  519.             }
  520.             out[col] = sum;
  521.         }

  522.         return out;
  523.     }

  524.     /** {@inheritDoc} */
  525.     @Override
  526.     public FieldMatrix<T> getSubMatrix(final int startRow, final int endRow,
  527.                                        final int startColumn, final int endColumn)
  528.         throws MathIllegalArgumentException {
  529.         MatrixUtils.checkSubMatrixIndex(this, startRow, endRow, startColumn, endColumn);
  530.         final int rowCount = endRow - startRow + 1;
  531.         final int columnCount = endColumn - startColumn + 1;
  532.         final T[][] outData = MathArrays.buildArray(getField(), rowCount, columnCount);
  533.         for (int i = 0; i < rowCount; ++i) {
  534.             System.arraycopy(data[startRow + i], startColumn, outData[i], 0, columnCount);
  535.         }

  536.         Array2DRowFieldMatrix<T> subMatrix = new Array2DRowFieldMatrix<>(getField());
  537.         subMatrix.data = outData;
  538.         return subMatrix;
  539.     }

  540.     /** {@inheritDoc} */
  541.     @Override
  542.     public T walkInRowOrder(final FieldMatrixChangingVisitor<T> visitor) {
  543.         final int rows    = getRowDimension();
  544.         final int columns = getColumnDimension();
  545.         visitor.start(rows, columns, 0, rows - 1, 0, columns - 1);
  546.         for (int i = 0; i < rows; ++i) {
  547.             final T[] rowI = data[i];
  548.             for (int j = 0; j < columns; ++j) {
  549.                 rowI[j] = visitor.visit(i, j, rowI[j]);
  550.             }
  551.         }
  552.         return visitor.end();
  553.     }

  554.     /** {@inheritDoc} */
  555.     @Override
  556.     public T walkInRowOrder(final FieldMatrixPreservingVisitor<T> visitor) {
  557.         final int rows    = getRowDimension();
  558.         final int columns = getColumnDimension();
  559.         visitor.start(rows, columns, 0, rows - 1, 0, columns - 1);
  560.         for (int i = 0; i < rows; ++i) {
  561.             final T[] rowI = data[i];
  562.             for (int j = 0; j < columns; ++j) {
  563.                 visitor.visit(i, j, rowI[j]);
  564.             }
  565.         }
  566.         return visitor.end();
  567.     }

  568.     /** {@inheritDoc} */
  569.     @Override
  570.     public T walkInRowOrder(final FieldMatrixChangingVisitor<T> visitor,
  571.                             final int startRow, final int endRow,
  572.                             final int startColumn, final int endColumn)
  573.         throws MathIllegalArgumentException {
  574.         checkSubMatrixIndex(startRow, endRow, startColumn, endColumn);
  575.         visitor.start(getRowDimension(), getColumnDimension(),
  576.                       startRow, endRow, startColumn, endColumn);
  577.         for (int i = startRow; i <= endRow; ++i) {
  578.             final T[] rowI = data[i];
  579.             for (int j = startColumn; j <= endColumn; ++j) {
  580.                 rowI[j] = visitor.visit(i, j, rowI[j]);
  581.             }
  582.         }
  583.         return visitor.end();
  584.     }

  585.     /** {@inheritDoc} */
  586.     @Override
  587.     public T walkInRowOrder(final FieldMatrixPreservingVisitor<T> visitor,
  588.                             final int startRow, final int endRow,
  589.                             final int startColumn, final int endColumn)
  590.         throws MathIllegalArgumentException {
  591.         checkSubMatrixIndex(startRow, endRow, startColumn, endColumn);
  592.         visitor.start(getRowDimension(), getColumnDimension(),
  593.                       startRow, endRow, startColumn, endColumn);
  594.         for (int i = startRow; i <= endRow; ++i) {
  595.             final T[] rowI = data[i];
  596.             for (int j = startColumn; j <= endColumn; ++j) {
  597.                 visitor.visit(i, j, rowI[j]);
  598.             }
  599.         }
  600.         return visitor.end();
  601.     }

  602.     /** {@inheritDoc} */
  603.     @Override
  604.     public T walkInColumnOrder(final FieldMatrixChangingVisitor<T> visitor) {
  605.         final int rows    = getRowDimension();
  606.         final int columns = getColumnDimension();
  607.         visitor.start(rows, columns, 0, rows - 1, 0, columns - 1);
  608.         for (int j = 0; j < columns; ++j) {
  609.             for (int i = 0; i < rows; ++i) {
  610.                 final T[] rowI = data[i];
  611.                 rowI[j] = visitor.visit(i, j, rowI[j]);
  612.             }
  613.         }
  614.         return visitor.end();
  615.     }

  616.     /** {@inheritDoc} */
  617.     @Override
  618.     public T walkInColumnOrder(final FieldMatrixPreservingVisitor<T> visitor) {
  619.         final int rows    = getRowDimension();
  620.         final int columns = getColumnDimension();
  621.         visitor.start(rows, columns, 0, rows - 1, 0, columns - 1);
  622.         for (int j = 0; j < columns; ++j) {
  623.             for (int i = 0; i < rows; ++i) {
  624.                 visitor.visit(i, j, data[i][j]);
  625.             }
  626.         }
  627.         return visitor.end();
  628.     }

  629.     /** {@inheritDoc} */
  630.     @Override
  631.     public T walkInColumnOrder(final FieldMatrixChangingVisitor<T> visitor,
  632.                                final int startRow, final int endRow,
  633.                                final int startColumn, final int endColumn)
  634.         throws MathIllegalArgumentException {
  635.     checkSubMatrixIndex(startRow, endRow, startColumn, endColumn);
  636.         visitor.start(getRowDimension(), getColumnDimension(),
  637.                       startRow, endRow, startColumn, endColumn);
  638.         for (int j = startColumn; j <= endColumn; ++j) {
  639.             for (int i = startRow; i <= endRow; ++i) {
  640.                 final T[] rowI = data[i];
  641.                 rowI[j] = visitor.visit(i, j, rowI[j]);
  642.             }
  643.         }
  644.         return visitor.end();
  645.     }

  646.     /** {@inheritDoc} */
  647.     @Override
  648.     public T walkInColumnOrder(final FieldMatrixPreservingVisitor<T> visitor,
  649.                                final int startRow, final int endRow,
  650.                                final int startColumn, final int endColumn)
  651.         throws MathIllegalArgumentException {
  652.         checkSubMatrixIndex(startRow, endRow, startColumn, endColumn);
  653.         visitor.start(getRowDimension(), getColumnDimension(),
  654.                       startRow, endRow, startColumn, endColumn);
  655.         for (int j = startColumn; j <= endColumn; ++j) {
  656.             for (int i = startRow; i <= endRow; ++i) {
  657.                 visitor.visit(i, j, data[i][j]);
  658.             }
  659.         }
  660.         return visitor.end();
  661.     }

  662.     /**
  663.      * Get a fresh copy of the underlying data array.
  664.      *
  665.      * @return a copy of the underlying data array.
  666.      */
  667.     private T[][] copyOut() {
  668.         final int nRows = this.getRowDimension();
  669.         final T[][] out = MathArrays.buildArray(getField(), nRows, getColumnDimension());
  670.         // can't copy 2-d array in one shot, otherwise get row references
  671.         for (int i = 0; i < nRows; i++) {
  672.             System.arraycopy(data[i], 0, out[i], 0, data[i].length);
  673.         }
  674.         return out;
  675.     }

  676.     /**
  677.      * Replace data with a fresh copy of the input array.
  678.      *
  679.      * @param in Data to copy.
  680.      * @throws MathIllegalArgumentException if the input array is empty.
  681.      * @throws MathIllegalArgumentException if the input array is not rectangular.
  682.      * @throws NullArgumentException if the input array is {@code null}.
  683.      */
  684.     private void copyIn(final T[][] in)
  685.         throws MathIllegalArgumentException, NullArgumentException {
  686.         setSubMatrix(in, 0, 0);
  687.     }

  688.     /** {@inheritDoc} */
  689.     @Override
  690.     public T[] getRow(final int row) throws MathIllegalArgumentException {
  691.         MatrixUtils.checkRowIndex(this, row);
  692.         final int nCols = getColumnDimension();
  693.         final T[] out = MathArrays.buildArray(getField(), nCols);
  694.         System.arraycopy(data[row], 0, out, 0, nCols);
  695.         return out;
  696.     }

  697.     /** {@inheritDoc} */
  698.     @Override
  699.     public void setRow(final int row, final T[] array)
  700.         throws MathIllegalArgumentException {
  701.         MatrixUtils.checkRowIndex(this, row);
  702.         final int nCols = getColumnDimension();
  703.         if (array.length != nCols) {
  704.             throw new MathIllegalArgumentException(LocalizedCoreFormats.DIMENSIONS_MISMATCH_2x2,
  705.                                                    1, array.length, 1, nCols);
  706.         }
  707.         System.arraycopy(array, 0, data[row], 0, nCols);
  708.     }

  709. }