You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by er...@apache.org on 2019/12/27 11:56:04 UTC

[commons-math] branch master updated (a81b218 -> 80df02f)

This is an automated email from the ASF dual-hosted git repository.

erans pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/commons-math.git.


    from a81b218  MATH-1388: Avoid risk of overflow.
     new ebf9085  [MATH-1408]: Replace try/catch with CCE by if/else
     new dac1532  [MATH-1408]: Add changes.xml entry
     new 80df02f  Downgrade CP: Missing HTML contents for some of the generated files.

The 3 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 pom.xml                                            |   2 +-
 src/changes/changes.xml                            |   3 +
 .../commons/math4/linear/AbstractFieldMatrix.java  |  64 +++----
 .../commons/math4/linear/AbstractRealMatrix.java   |  63 +++----
 .../commons/math4/linear/ArrayFieldVector.java     | 116 ++++++------
 .../commons/math4/linear/ArrayRealVector.java      |   5 +-
 .../commons/math4/linear/BlockFieldMatrix.java     | 203 ++++++++++----------
 .../commons/math4/linear/BlockRealMatrix.java      | 210 ++++++++++-----------
 .../commons/math4/linear/FieldLUDecomposition.java |  58 +++---
 .../commons/math4/linear/OpenMapRealMatrix.java    |  40 ++--
 10 files changed, 379 insertions(+), 385 deletions(-)


[commons-math] 03/03: Downgrade CP: Missing HTML contents for some of the generated files.

Posted by er...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

erans pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-math.git

commit 80df02f2d2a3180a30a6748a640588735ce8aaaa
Author: Gilles Sadowski <gi...@harfang.homelinux.org>
AuthorDate: Fri Dec 27 12:53:36 2019 +0100

    Downgrade CP: Missing HTML contents for some of the generated files.
---
 pom.xml | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/pom.xml b/pom.xml
index 353b325..8c5a3ef 100644
--- a/pom.xml
+++ b/pom.xml
@@ -20,7 +20,7 @@
   <parent>
     <groupId>org.apache.commons</groupId>
     <artifactId>commons-parent</artifactId>
-    <version>50</version>
+    <version>49</version>
   </parent>
   <groupId>org.apache.commons</groupId>
   <artifactId>commons-math4</artifactId>


[commons-math] 02/03: [MATH-1408]: Add changes.xml entry

Posted by er...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

erans pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-math.git

commit dac15322b8aa0dfdd761c8a2a53a6e9bec5d44c5
Author: Bruno P. Kinoshita <ki...@users.noreply.github.com>
AuthorDate: Fri Dec 27 21:31:42 2019 +1300

    [MATH-1408]: Add changes.xml entry
---
 src/changes/changes.xml | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index dce0411..cd27ece 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -54,6 +54,9 @@ If the output is not quite correct, check for invisible trailing spaces!
     </release>
 
     <release version="4.0" date="XXXX-XX-XX" description="">
+      <action dev="kinow" type="update" issue="MATH-1408">
+        Do not use exceptions for control flow
+      </action>
       <action dev="erans" type="fix" issue="MATH-1388">
         Avoid overflow.
       </action>


[commons-math] 01/03: [MATH-1408]: Replace try/catch with CCE by if/else

Posted by er...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

erans pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-math.git

commit ebf908570ae58bd3020c771ba20a60feb93c50ec
Author: Bruno P. Kinoshita <ki...@users.noreply.github.com>
AuthorDate: Fri Dec 27 21:10:29 2019 +1300

    [MATH-1408]: Replace try/catch with CCE by if/else
---
 .../commons/math4/linear/AbstractFieldMatrix.java  |  64 +++----
 .../commons/math4/linear/AbstractRealMatrix.java   |  63 +++----
 .../commons/math4/linear/ArrayFieldVector.java     | 116 ++++++------
 .../commons/math4/linear/ArrayRealVector.java      |   5 +-
 .../commons/math4/linear/BlockFieldMatrix.java     | 203 ++++++++++----------
 .../commons/math4/linear/BlockRealMatrix.java      | 210 ++++++++++-----------
 .../commons/math4/linear/FieldLUDecomposition.java |  58 +++---
 .../commons/math4/linear/OpenMapRealMatrix.java    |  40 ++--
 8 files changed, 375 insertions(+), 384 deletions(-)

diff --git a/src/main/java/org/apache/commons/math4/linear/AbstractFieldMatrix.java b/src/main/java/org/apache/commons/math4/linear/AbstractFieldMatrix.java
index 1b337b5..bdab7cc 100644
--- a/src/main/java/org/apache/commons/math4/linear/AbstractFieldMatrix.java
+++ b/src/main/java/org/apache/commons/math4/linear/AbstractFieldMatrix.java
@@ -711,26 +711,26 @@ public abstract class AbstractFieldMatrix<T extends FieldElement<T>>
     @Override
     public FieldVector<T> operate(final FieldVector<T> v)
         throws DimensionMismatchException {
-        try {
+        if (v instanceof ArrayFieldVector) {
             return new ArrayFieldVector<>(field, operate(((ArrayFieldVector<T>) v).getDataRef()), false);
-        } catch (ClassCastException cce) {
-            final int nRows = getRowDimension();
-            final int nCols = getColumnDimension();
-            if (v.getDimension() != nCols) {
-                throw new DimensionMismatchException(v.getDimension(), nCols);
-            }
+        }
 
-            final T[] out = MathArrays.buildArray(field, nRows);
-            for (int row = 0; row < nRows; ++row) {
-                T sum = field.getZero();
-                for (int i = 0; i < nCols; ++i) {
-                    sum = sum.add(getEntry(row, i).multiply(v.getEntry(i)));
-                }
-                out[row] = sum;
-            }
+        final int nRows = getRowDimension();
+        final int nCols = getColumnDimension();
+        if (v.getDimension() != nCols) {
+            throw new DimensionMismatchException(v.getDimension(), nCols);
+        }
 
-            return new ArrayFieldVector<>(field, out, false);
+        final T[] out = MathArrays.buildArray(field, nRows);
+        for (int row = 0; row < nRows; ++row) {
+            T sum = field.getZero();
+            for (int i = 0; i < nCols; ++i) {
+                sum = sum.add(getEntry(row, i).multiply(v.getEntry(i)));
+            }
+            out[row] = sum;
         }
+
+        return new ArrayFieldVector<>(field, out, false);
     }
 
     /** {@inheritDoc} */
@@ -759,26 +759,26 @@ public abstract class AbstractFieldMatrix<T extends FieldElement<T>>
     @Override
     public FieldVector<T> preMultiply(final FieldVector<T> v)
         throws DimensionMismatchException {
-        try {
+        if (v instanceof ArrayFieldVector) {
             return new ArrayFieldVector<>(field, preMultiply(((ArrayFieldVector<T>) v).getDataRef()), false);
-        } catch (ClassCastException cce) {
-            final int nRows = getRowDimension();
-            final int nCols = getColumnDimension();
-            if (v.getDimension() != nRows) {
-                throw new DimensionMismatchException(v.getDimension(), nRows);
-            }
+        }
 
-            final T[] out = MathArrays.buildArray(field, nCols);
-            for (int col = 0; col < nCols; ++col) {
-                T sum = field.getZero();
-                for (int i = 0; i < nRows; ++i) {
-                    sum = sum.add(getEntry(i, col).multiply(v.getEntry(i)));
-                }
-                out[col] = sum;
-            }
+        final int nRows = getRowDimension();
+        final int nCols = getColumnDimension();
+        if (v.getDimension() != nRows) {
+            throw new DimensionMismatchException(v.getDimension(), nRows);
+        }
 
-            return new ArrayFieldVector<>(field, out, false);
+        final T[] out = MathArrays.buildArray(field, nCols);
+        for (int col = 0; col < nCols; ++col) {
+            T sum = field.getZero();
+            for (int i = 0; i < nRows; ++i) {
+                sum = sum.add(getEntry(i, col).multiply(v.getEntry(i)));
+            }
+            out[col] = sum;
         }
+
+        return new ArrayFieldVector<>(field, out, false);
     }
 
     /** {@inheritDoc} */
diff --git a/src/main/java/org/apache/commons/math4/linear/AbstractRealMatrix.java b/src/main/java/org/apache/commons/math4/linear/AbstractRealMatrix.java
index c5d8d7c..94888f5 100644
--- a/src/main/java/org/apache/commons/math4/linear/AbstractRealMatrix.java
+++ b/src/main/java/org/apache/commons/math4/linear/AbstractRealMatrix.java
@@ -699,26 +699,26 @@ public abstract class AbstractRealMatrix
     @Override
     public RealVector operate(final RealVector v)
         throws DimensionMismatchException {
-        try {
+        if (v instanceof ArrayRealVector) {
             return new ArrayRealVector(operate(((ArrayRealVector) v).getDataRef()), false);
-        } catch (ClassCastException cce) {
-            final int nRows = getRowDimension();
-            final int nCols = getColumnDimension();
-            if (v.getDimension() != nCols) {
-                throw new DimensionMismatchException(v.getDimension(), nCols);
-            }
+        }
 
-            final double[] out = new double[nRows];
-            for (int row = 0; row < nRows; ++row) {
-                double sum = 0;
-                for (int i = 0; i < nCols; ++i) {
-                    sum += getEntry(row, i) * v.getEntry(i);
-                }
-                out[row] = sum;
-            }
+        final int nRows = getRowDimension();
+        final int nCols = getColumnDimension();
+        if (v.getDimension() != nCols) {
+            throw new DimensionMismatchException(v.getDimension(), nCols);
+        }
 
-            return new ArrayRealVector(out, false);
+        final double[] out = new double[nRows];
+        for (int row = 0; row < nRows; ++row) {
+            double sum = 0;
+            for (int i = 0; i < nCols; ++i) {
+                sum += getEntry(row, i) * v.getEntry(i);
+            }
+            out[row] = sum;
         }
+
+        return new ArrayRealVector(out, false);
     }
 
     /** {@inheritDoc} */
@@ -746,27 +746,26 @@ public abstract class AbstractRealMatrix
     /** {@inheritDoc} */
     @Override
     public RealVector preMultiply(final RealVector v) throws DimensionMismatchException {
-        try {
+        if (v instanceof ArrayRealVector) {
             return new ArrayRealVector(preMultiply(((ArrayRealVector) v).getDataRef()), false);
-        } catch (ClassCastException cce) {
+        }
 
-            final int nRows = getRowDimension();
-            final int nCols = getColumnDimension();
-            if (v.getDimension() != nRows) {
-                throw new DimensionMismatchException(v.getDimension(), nRows);
-            }
+        final int nRows = getRowDimension();
+        final int nCols = getColumnDimension();
+        if (v.getDimension() != nRows) {
+            throw new DimensionMismatchException(v.getDimension(), nRows);
+        }
 
-            final double[] out = new double[nCols];
-            for (int col = 0; col < nCols; ++col) {
-                double sum = 0;
-                for (int i = 0; i < nRows; ++i) {
-                    sum += getEntry(i, col) * v.getEntry(i);
-                }
-                out[col] = sum;
+        final double[] out = new double[nCols];
+        for (int col = 0; col < nCols; ++col) {
+            double sum = 0;
+            for (int i = 0; i < nRows; ++i) {
+                sum += getEntry(i, col) * v.getEntry(i);
             }
-
-            return new ArrayRealVector(out, false);
+            out[col] = sum;
         }
+
+        return new ArrayRealVector(out, false);
     }
 
     /** {@inheritDoc} */
diff --git a/src/main/java/org/apache/commons/math4/linear/ArrayFieldVector.java b/src/main/java/org/apache/commons/math4/linear/ArrayFieldVector.java
index b468bb7..4b8664c 100644
--- a/src/main/java/org/apache/commons/math4/linear/ArrayFieldVector.java
+++ b/src/main/java/org/apache/commons/math4/linear/ArrayFieldVector.java
@@ -395,16 +395,16 @@ public class ArrayFieldVector<T extends FieldElement<T>> implements FieldVector<
     @Override
     public FieldVector<T> add(FieldVector<T> v)
         throws DimensionMismatchException {
-        try {
+        if (v instanceof ArrayFieldVector) {
             return add((ArrayFieldVector<T>) v);
-        } catch (ClassCastException cce) {
-            checkVectorDimensions(v);
-            T[] out = MathArrays.buildArray(field, data.length);
-            for (int i = 0; i < data.length; i++) {
-                out[i] = data[i].add(v.getEntry(i));
-            }
-            return new ArrayFieldVector<>(field, out, false);
         }
+
+        checkVectorDimensions(v);
+        T[] out = MathArrays.buildArray(field, data.length);
+        for (int i = 0; i < data.length; i++) {
+            out[i] = data[i].add(v.getEntry(i));
+        }
+        return new ArrayFieldVector<>(field, out, false);
     }
 
     /**
@@ -428,16 +428,16 @@ public class ArrayFieldVector<T extends FieldElement<T>> implements FieldVector<
     @Override
     public FieldVector<T> subtract(FieldVector<T> v)
         throws DimensionMismatchException {
-        try {
+        if (v instanceof ArrayFieldVector) {
             return subtract((ArrayFieldVector<T>) v);
-        } catch (ClassCastException cce) {
-            checkVectorDimensions(v);
-            T[] out = MathArrays.buildArray(field, data.length);
-            for (int i = 0; i < data.length; i++) {
-                out[i] = data[i].subtract(v.getEntry(i));
-            }
-            return new ArrayFieldVector<>(field, out, false);
         }
+
+        checkVectorDimensions(v);
+        T[] out = MathArrays.buildArray(field, data.length);
+        for (int i = 0; i < data.length; i++) {
+            out[i] = data[i].subtract(v.getEntry(i));
+        }
+        return new ArrayFieldVector<>(field, out, false);
     }
 
     /**
@@ -570,16 +570,16 @@ public class ArrayFieldVector<T extends FieldElement<T>> implements FieldVector<
     @Override
     public FieldVector<T> ebeMultiply(FieldVector<T> v)
         throws DimensionMismatchException {
-        try {
+        if (v instanceof ArrayFieldVector) {
             return ebeMultiply((ArrayFieldVector<T>) v);
-        } catch (ClassCastException cce) {
-            checkVectorDimensions(v);
-            T[] out = MathArrays.buildArray(field, data.length);
-            for (int i = 0; i < data.length; i++) {
-                out[i] = data[i].multiply(v.getEntry(i));
-            }
-            return new ArrayFieldVector<>(field, out, false);
         }
+
+        checkVectorDimensions(v);
+        T[] out = MathArrays.buildArray(field, data.length);
+        for (int i = 0; i < data.length; i++) {
+            out[i] = data[i].multiply(v.getEntry(i));
+        }
+        return new ArrayFieldVector<>(field, out, false);
     }
 
     /**
@@ -603,20 +603,20 @@ public class ArrayFieldVector<T extends FieldElement<T>> implements FieldVector<
     @Override
     public FieldVector<T> ebeDivide(FieldVector<T> v)
         throws DimensionMismatchException, MathArithmeticException {
-        try {
+        if (v instanceof ArrayFieldVector) {
             return ebeDivide((ArrayFieldVector<T>) v);
-        } catch (ClassCastException cce) {
-            checkVectorDimensions(v);
-            T[] out = MathArrays.buildArray(field, data.length);
-            for (int i = 0; i < data.length; i++) {
-                try {
-                    out[i] = data[i].divide(v.getEntry(i));
-                } catch (final MathArithmeticException e) {
-                    throw new MathArithmeticException(LocalizedFormats.INDEX, i);
-                }
+        }
+
+        checkVectorDimensions(v);
+        T[] out = MathArrays.buildArray(field, data.length);
+        for (int i = 0; i < data.length; i++) {
+            try {
+                out[i] = data[i].divide(v.getEntry(i));
+            } catch (final MathArithmeticException e) {
+                throw new MathArithmeticException(LocalizedFormats.INDEX, i);
             }
-            return new ArrayFieldVector<>(field, out, false);
         }
+        return new ArrayFieldVector<>(field, out, false);
     }
 
     /**
@@ -654,16 +654,16 @@ public class ArrayFieldVector<T extends FieldElement<T>> implements FieldVector<
     @Override
     public T dotProduct(FieldVector<T> v)
         throws DimensionMismatchException {
-        try {
+        if (v instanceof ArrayFieldVector) {
             return dotProduct((ArrayFieldVector<T>) v);
-        } catch (ClassCastException cce) {
-            checkVectorDimensions(v);
-            T dot = field.getZero();
-            for (int i = 0; i < data.length; i++) {
-                dot = dot.add(data[i].multiply(v.getEntry(i)));
-            }
-            return dot;
         }
+
+        checkVectorDimensions(v);
+        T dot = field.getZero();
+        for (int i = 0; i < data.length; i++) {
+            dot = dot.add(data[i].multiply(v.getEntry(i)));
+        }
+        return dot;
     }
 
     /**
@@ -705,19 +705,19 @@ public class ArrayFieldVector<T extends FieldElement<T>> implements FieldVector<
     /** {@inheritDoc} */
     @Override
     public FieldMatrix<T> outerProduct(FieldVector<T> v) {
-        try {
+        if (v instanceof ArrayFieldVector) {
             return outerProduct((ArrayFieldVector<T>) v);
-        } catch (ClassCastException cce) {
-            final int m = data.length;
-            final int n = v.getDimension();
-            final FieldMatrix<T> out = new Array2DRowFieldMatrix<>(field, m, n);
-            for (int i = 0; i < m; i++) {
-                for (int j = 0; j < n; j++) {
-                    out.setEntry(i, j, data[i].multiply(v.getEntry(j)));
-                }
+        }
+
+        final int m = data.length;
+        final int n = v.getDimension();
+        final FieldMatrix<T> out = new Array2DRowFieldMatrix<>(field, m, n);
+        for (int i = 0; i < m; i++) {
+            for (int j = 0; j < n; j++) {
+                out.setEntry(i, j, data[i].multiply(v.getEntry(j)));
             }
-            return out;
         }
+        return out;
     }
 
     /**
@@ -752,11 +752,11 @@ public class ArrayFieldVector<T extends FieldElement<T>> implements FieldVector<
     /** {@inheritDoc} */
     @Override
     public FieldVector<T> append(FieldVector<T> v) {
-        try {
+        if (v instanceof ArrayFieldVector) {
             return append((ArrayFieldVector<T>) v);
-        } catch (ClassCastException cce) {
-            return new ArrayFieldVector<>(this,new ArrayFieldVector<>(v));
         }
+
+        return new ArrayFieldVector<>(this,new ArrayFieldVector<>(v));
     }
 
     /**
@@ -808,9 +808,9 @@ public class ArrayFieldVector<T extends FieldElement<T>> implements FieldVector<
     @Override
     public void setSubVector(int index, FieldVector<T> v) throws OutOfRangeException {
         try {
-            try {
+            if (v instanceof ArrayFieldVector) {
                 set(index, (ArrayFieldVector<T>) v);
-            } catch (ClassCastException cce) {
+            } else {
                 for (int i = index; i < index + v.getDimension(); ++i) {
                     data[i] = v.getEntry(i-index);
                 }
diff --git a/src/main/java/org/apache/commons/math4/linear/ArrayRealVector.java b/src/main/java/org/apache/commons/math4/linear/ArrayRealVector.java
index 1dfc220..0294006 100644
--- a/src/main/java/org/apache/commons/math4/linear/ArrayRealVector.java
+++ b/src/main/java/org/apache/commons/math4/linear/ArrayRealVector.java
@@ -612,11 +612,10 @@ public class ArrayRealVector extends RealVector implements Serializable {
     /** {@inheritDoc} */
     @Override
     public RealVector append(RealVector v) {
-        try {
+        if (v instanceof ArrayRealVector) {
             return new ArrayRealVector(this, (ArrayRealVector) v);
-        } catch (ClassCastException cce) {
-            return new ArrayRealVector(this, v);
         }
+        return new ArrayRealVector(this, v);
     }
 
     /**
diff --git a/src/main/java/org/apache/commons/math4/linear/BlockFieldMatrix.java b/src/main/java/org/apache/commons/math4/linear/BlockFieldMatrix.java
index dda300a..c65fd1a 100644
--- a/src/main/java/org/apache/commons/math4/linear/BlockFieldMatrix.java
+++ b/src/main/java/org/apache/commons/math4/linear/BlockFieldMatrix.java
@@ -316,43 +316,42 @@ public class BlockFieldMatrix<T extends FieldElement<T>> extends AbstractFieldMa
     @Override
     public FieldMatrix<T> add(final FieldMatrix<T> m)
         throws MatrixDimensionMismatchException {
-        try {
+        if (m instanceof BlockFieldMatrix) {
             return add((BlockFieldMatrix<T>) m);
-        } catch (ClassCastException cce) {
+        }
 
-            // safety check
-            checkAdd(m);
+        // safety check
+        checkAdd(m);
 
-            final BlockFieldMatrix<T> out = new BlockFieldMatrix<>(getField(), rows, columns);
+        final BlockFieldMatrix<T> out = new BlockFieldMatrix<>(getField(), rows, columns);
 
-            // perform addition block-wise, to ensure good cache behavior
-            int blockIndex = 0;
-            for (int iBlock = 0; iBlock < out.blockRows; ++iBlock) {
-                for (int jBlock = 0; jBlock < out.blockColumns; ++jBlock) {
+        // perform addition block-wise, to ensure good cache behavior
+        int blockIndex = 0;
+        for (int iBlock = 0; iBlock < out.blockRows; ++iBlock) {
+            for (int jBlock = 0; jBlock < out.blockColumns; ++jBlock) {
 
-                    // perform addition on the current block
-                    final T[] outBlock = out.blocks[blockIndex];
-                    final T[] tBlock   = blocks[blockIndex];
-                    final int      pStart   = iBlock * BLOCK_SIZE;
-                    final int      pEnd     = FastMath.min(pStart + BLOCK_SIZE, rows);
-                    final int      qStart   = jBlock * BLOCK_SIZE;
-                    final int      qEnd     = FastMath.min(qStart + BLOCK_SIZE, columns);
-                    int k = 0;
-                    for (int p = pStart; p < pEnd; ++p) {
-                        for (int q = qStart; q < qEnd; ++q) {
-                            outBlock[k] = tBlock[k].add(m.getEntry(p, q));
-                            ++k;
-                        }
+                // perform addition on the current block
+                final T[] outBlock = out.blocks[blockIndex];
+                final T[] tBlock   = blocks[blockIndex];
+                final int      pStart   = iBlock * BLOCK_SIZE;
+                final int      pEnd     = FastMath.min(pStart + BLOCK_SIZE, rows);
+                final int      qStart   = jBlock * BLOCK_SIZE;
+                final int      qEnd     = FastMath.min(qStart + BLOCK_SIZE, columns);
+                int k = 0;
+                for (int p = pStart; p < pEnd; ++p) {
+                    for (int q = qStart; q < qEnd; ++q) {
+                        outBlock[k] = tBlock[k].add(m.getEntry(p, q));
+                        ++k;
                     }
+                }
 
-                    // go to next block
-                    ++blockIndex;
+                // go to next block
+                ++blockIndex;
 
-                }
             }
-
-            return out;
         }
+
+        return out;
     }
 
     /**
@@ -388,43 +387,42 @@ public class BlockFieldMatrix<T extends FieldElement<T>> extends AbstractFieldMa
     @Override
     public FieldMatrix<T> subtract(final FieldMatrix<T> m)
         throws MatrixDimensionMismatchException {
-        try {
+        if (m instanceof BlockFieldMatrix) {
             return subtract((BlockFieldMatrix<T>) m);
-        } catch (ClassCastException cce) {
+        }
 
-            // safety check
-            checkAdd(m);
+        // safety check
+        checkAdd(m);
 
-            final BlockFieldMatrix<T> out = new BlockFieldMatrix<>(getField(), rows, columns);
+        final BlockFieldMatrix<T> out = new BlockFieldMatrix<>(getField(), rows, columns);
 
-            // perform subtraction block-wise, to ensure good cache behavior
-            int blockIndex = 0;
-            for (int iBlock = 0; iBlock < out.blockRows; ++iBlock) {
-                for (int jBlock = 0; jBlock < out.blockColumns; ++jBlock) {
+        // perform subtraction block-wise, to ensure good cache behavior
+        int blockIndex = 0;
+        for (int iBlock = 0; iBlock < out.blockRows; ++iBlock) {
+            for (int jBlock = 0; jBlock < out.blockColumns; ++jBlock) {
 
-                    // perform subtraction on the current block
-                    final T[] outBlock = out.blocks[blockIndex];
-                    final T[] tBlock   = blocks[blockIndex];
-                    final int      pStart   = iBlock * BLOCK_SIZE;
-                    final int      pEnd     = FastMath.min(pStart + BLOCK_SIZE, rows);
-                    final int      qStart   = jBlock * BLOCK_SIZE;
-                    final int      qEnd     = FastMath.min(qStart + BLOCK_SIZE, columns);
-                    int k = 0;
-                    for (int p = pStart; p < pEnd; ++p) {
-                        for (int q = qStart; q < qEnd; ++q) {
-                            outBlock[k] = tBlock[k].subtract(m.getEntry(p, q));
-                            ++k;
-                        }
+                // perform subtraction on the current block
+                final T[] outBlock = out.blocks[blockIndex];
+                final T[] tBlock   = blocks[blockIndex];
+                final int      pStart   = iBlock * BLOCK_SIZE;
+                final int      pEnd     = FastMath.min(pStart + BLOCK_SIZE, rows);
+                final int      qStart   = jBlock * BLOCK_SIZE;
+                final int      qEnd     = FastMath.min(qStart + BLOCK_SIZE, columns);
+                int k = 0;
+                for (int p = pStart; p < pEnd; ++p) {
+                    for (int q = qStart; q < qEnd; ++q) {
+                        outBlock[k] = tBlock[k].subtract(m.getEntry(p, q));
+                        ++k;
                     }
+                }
 
-                    // go to next block
-                    ++blockIndex;
+                // go to next block
+                ++blockIndex;
 
-                }
             }
-
-            return out;
         }
+
+        return out;
     }
 
     /**
@@ -493,61 +491,60 @@ public class BlockFieldMatrix<T extends FieldElement<T>> extends AbstractFieldMa
     @Override
     public FieldMatrix<T> multiply(final FieldMatrix<T> m)
         throws DimensionMismatchException {
-        try {
+        if (m instanceof BlockFieldMatrix) {
             return multiply((BlockFieldMatrix<T>) m);
-        } catch (ClassCastException cce) {
+        }
 
-            // safety check
-            checkMultiply(m);
+        // safety check
+        checkMultiply(m);
 
-            final BlockFieldMatrix<T> out = new BlockFieldMatrix<>(getField(), rows, m.getColumnDimension());
-            final T zero = getField().getZero();
+        final BlockFieldMatrix<T> out = new BlockFieldMatrix<>(getField(), rows, m.getColumnDimension());
+        final T zero = getField().getZero();
 
-            // perform multiplication block-wise, to ensure good cache behavior
-            int blockIndex = 0;
-            for (int iBlock = 0; iBlock < out.blockRows; ++iBlock) {
+        // perform multiplication block-wise, to ensure good cache behavior
+        int blockIndex = 0;
+        for (int iBlock = 0; iBlock < out.blockRows; ++iBlock) {
 
-                final int pStart = iBlock * BLOCK_SIZE;
-                final int pEnd   = FastMath.min(pStart + BLOCK_SIZE, rows);
+            final int pStart = iBlock * BLOCK_SIZE;
+            final int pEnd   = FastMath.min(pStart + BLOCK_SIZE, rows);
 
-                for (int jBlock = 0; jBlock < out.blockColumns; ++jBlock) {
+            for (int jBlock = 0; jBlock < out.blockColumns; ++jBlock) {
 
-                    final int qStart = jBlock * BLOCK_SIZE;
-                    final int qEnd   = FastMath.min(qStart + BLOCK_SIZE, m.getColumnDimension());
-
-                    // select current block
-                    final T[] outBlock = out.blocks[blockIndex];
-
-                    // perform multiplication on current block
-                    for (int kBlock = 0; kBlock < blockColumns; ++kBlock) {
-                        final int kWidth      = blockWidth(kBlock);
-                        final T[] tBlock = blocks[iBlock * blockColumns + kBlock];
-                        final int rStart      = kBlock * BLOCK_SIZE;
-                        int k = 0;
-                        for (int p = pStart; p < pEnd; ++p) {
-                            final int lStart = (p - pStart) * kWidth;
-                            final int lEnd   = lStart + kWidth;
-                            for (int q = qStart; q < qEnd; ++q) {
-                                T sum = zero;
-                                int r = rStart;
-                                for (int l = lStart; l < lEnd; ++l) {
-                                    sum = sum.add(tBlock[l].multiply(m.getEntry(r, q)));
-                                    ++r;
-                                }
-                                outBlock[k] = outBlock[k].add(sum);
-                                ++k;
+                final int qStart = jBlock * BLOCK_SIZE;
+                final int qEnd   = FastMath.min(qStart + BLOCK_SIZE, m.getColumnDimension());
+
+                // select current block
+                final T[] outBlock = out.blocks[blockIndex];
+
+                // perform multiplication on current block
+                for (int kBlock = 0; kBlock < blockColumns; ++kBlock) {
+                    final int kWidth      = blockWidth(kBlock);
+                    final T[] tBlock = blocks[iBlock * blockColumns + kBlock];
+                    final int rStart      = kBlock * BLOCK_SIZE;
+                    int k = 0;
+                    for (int p = pStart; p < pEnd; ++p) {
+                        final int lStart = (p - pStart) * kWidth;
+                        final int lEnd   = lStart + kWidth;
+                        for (int q = qStart; q < qEnd; ++q) {
+                            T sum = zero;
+                            int r = rStart;
+                            for (int l = lStart; l < lEnd; ++l) {
+                                sum = sum.add(tBlock[l].multiply(m.getEntry(r, q)));
+                                ++r;
                             }
+                            outBlock[k] = outBlock[k].add(sum);
+                            ++k;
                         }
                     }
+                }
 
-                    // go to next block
-                    ++blockIndex;
+                // go to next block
+                ++blockIndex;
 
-                }
             }
-
-            return out;
         }
+
+        return out;
     }
 
     /**
@@ -865,9 +862,9 @@ public class BlockFieldMatrix<T extends FieldElement<T>> extends AbstractFieldMa
     @Override
     public void setRowMatrix(final int row, final FieldMatrix<T> matrix)
         throws MatrixDimensionMismatchException, OutOfRangeException {
-        try {
+        if (matrix instanceof BlockFieldMatrix) {
             setRowMatrix(row, (BlockFieldMatrix<T>) matrix);
-        } catch (ClassCastException cce) {
+        } else {
             super.setRowMatrix(row, matrix);
         }
     }
@@ -949,9 +946,9 @@ public class BlockFieldMatrix<T extends FieldElement<T>> extends AbstractFieldMa
     @Override
     public void setColumnMatrix(final int column, final FieldMatrix<T> matrix)
         throws MatrixDimensionMismatchException, OutOfRangeException {
-        try {
+        if (matrix instanceof BlockFieldMatrix) {
             setColumnMatrix(column, (BlockFieldMatrix<T>) matrix);
-        } catch (ClassCastException cce) {
+        } else {
             super.setColumnMatrix(column, matrix);
         }
     }
@@ -1023,9 +1020,9 @@ public class BlockFieldMatrix<T extends FieldElement<T>> extends AbstractFieldMa
     @Override
     public void setRowVector(final int row, final FieldVector<T> vector)
         throws MatrixDimensionMismatchException, OutOfRangeException {
-        try {
+        if (vector instanceof ArrayFieldVector) {
             setRow(row, ((ArrayFieldVector<T>) vector).getDataRef());
-        } catch (ClassCastException cce) {
+        } else {
             super.setRowVector(row, vector);
         }
     }
@@ -1057,9 +1054,9 @@ public class BlockFieldMatrix<T extends FieldElement<T>> extends AbstractFieldMa
     @Override
     public void setColumnVector(final int column, final FieldVector<T> vector)
         throws OutOfRangeException, MatrixDimensionMismatchException {
-        try {
+        if (vector instanceof ArrayFieldVector) {
             setColumn(column, ((ArrayFieldVector<T>) vector).getDataRef());
-        } catch (ClassCastException cce) {
+        } else {
             super.setColumnVector(column, vector);
         }
     }
diff --git a/src/main/java/org/apache/commons/math4/linear/BlockRealMatrix.java b/src/main/java/org/apache/commons/math4/linear/BlockRealMatrix.java
index 1dff0eb..d28fe5c 100644
--- a/src/main/java/org/apache/commons/math4/linear/BlockRealMatrix.java
+++ b/src/main/java/org/apache/commons/math4/linear/BlockRealMatrix.java
@@ -300,40 +300,40 @@ public class BlockRealMatrix extends AbstractRealMatrix implements Serializable
     @Override
     public BlockRealMatrix add(final RealMatrix m)
         throws MatrixDimensionMismatchException {
-        try {
+        if (m instanceof BlockRealMatrix) {
             return add((BlockRealMatrix) m);
-        } catch (ClassCastException cce) {
-            // safety check
-            checkAdd(m);
-
-            final BlockRealMatrix out = new BlockRealMatrix(rows, columns);
-
-            // perform addition block-wise, to ensure good cache behavior
-            int blockIndex = 0;
-            for (int iBlock = 0; iBlock < out.blockRows; ++iBlock) {
-                for (int jBlock = 0; jBlock < out.blockColumns; ++jBlock) {
-
-                    // perform addition on the current block
-                    final double[] outBlock = out.blocks[blockIndex];
-                    final double[] tBlock   = blocks[blockIndex];
-                    final int pStart = iBlock * BLOCK_SIZE;
-                    final int pEnd = FastMath.min(pStart + BLOCK_SIZE, rows);
-                    final int qStart = jBlock * BLOCK_SIZE;
-                    final int qEnd = FastMath.min(qStart + BLOCK_SIZE, columns);
-                    int k = 0;
-                    for (int p = pStart; p < pEnd; ++p) {
-                        for (int q = qStart; q < qEnd; ++q) {
-                            outBlock[k] = tBlock[k] + m.getEntry(p, q);
-                            ++k;
-                        }
+        }
+
+        // safety check
+        checkAdd(m);
+
+        final BlockRealMatrix out = new BlockRealMatrix(rows, columns);
+
+        // perform addition block-wise, to ensure good cache behavior
+        int blockIndex = 0;
+        for (int iBlock = 0; iBlock < out.blockRows; ++iBlock) {
+            for (int jBlock = 0; jBlock < out.blockColumns; ++jBlock) {
+
+                // perform addition on the current block
+                final double[] outBlock = out.blocks[blockIndex];
+                final double[] tBlock   = blocks[blockIndex];
+                final int pStart = iBlock * BLOCK_SIZE;
+                final int pEnd = FastMath.min(pStart + BLOCK_SIZE, rows);
+                final int qStart = jBlock * BLOCK_SIZE;
+                final int qEnd = FastMath.min(qStart + BLOCK_SIZE, columns);
+                int k = 0;
+                for (int p = pStart; p < pEnd; ++p) {
+                    for (int q = qStart; q < qEnd; ++q) {
+                        outBlock[k] = tBlock[k] + m.getEntry(p, q);
+                        ++k;
                     }
-                    // go to next block
-                    ++blockIndex;
                 }
+                // go to next block
+                ++blockIndex;
             }
-
-            return out;
         }
+
+        return out;
     }
 
     /**
@@ -368,40 +368,40 @@ public class BlockRealMatrix extends AbstractRealMatrix implements Serializable
     @Override
     public BlockRealMatrix subtract(final RealMatrix m)
         throws MatrixDimensionMismatchException {
-        try {
+        if (m instanceof BlockRealMatrix) {
             return subtract((BlockRealMatrix) m);
-        } catch (ClassCastException cce) {
-            // safety check
-            checkAdd(m);
-
-            final BlockRealMatrix out = new BlockRealMatrix(rows, columns);
-
-            // perform subtraction block-wise, to ensure good cache behavior
-            int blockIndex = 0;
-            for (int iBlock = 0; iBlock < out.blockRows; ++iBlock) {
-                for (int jBlock = 0; jBlock < out.blockColumns; ++jBlock) {
-
-                    // perform subtraction on the current block
-                    final double[] outBlock = out.blocks[blockIndex];
-                    final double[] tBlock = blocks[blockIndex];
-                    final int pStart = iBlock * BLOCK_SIZE;
-                    final int pEnd = FastMath.min(pStart + BLOCK_SIZE, rows);
-                    final int qStart = jBlock * BLOCK_SIZE;
-                    final int qEnd = FastMath.min(qStart + BLOCK_SIZE, columns);
-                    int k = 0;
-                    for (int p = pStart; p < pEnd; ++p) {
-                        for (int q = qStart; q < qEnd; ++q) {
-                            outBlock[k] = tBlock[k] - m.getEntry(p, q);
-                            ++k;
-                        }
+        }
+
+        // safety check
+        checkAdd(m);
+
+        final BlockRealMatrix out = new BlockRealMatrix(rows, columns);
+
+        // perform subtraction block-wise, to ensure good cache behavior
+        int blockIndex = 0;
+        for (int iBlock = 0; iBlock < out.blockRows; ++iBlock) {
+            for (int jBlock = 0; jBlock < out.blockColumns; ++jBlock) {
+
+                // perform subtraction on the current block
+                final double[] outBlock = out.blocks[blockIndex];
+                final double[] tBlock = blocks[blockIndex];
+                final int pStart = iBlock * BLOCK_SIZE;
+                final int pEnd = FastMath.min(pStart + BLOCK_SIZE, rows);
+                final int qStart = jBlock * BLOCK_SIZE;
+                final int qEnd = FastMath.min(qStart + BLOCK_SIZE, columns);
+                int k = 0;
+                for (int p = pStart; p < pEnd; ++p) {
+                    for (int q = qStart; q < qEnd; ++q) {
+                        outBlock[k] = tBlock[k] - m.getEntry(p, q);
+                        ++k;
                     }
-                    // go to next block
-                    ++blockIndex;
                 }
+                // go to next block
+                ++blockIndex;
             }
-
-            return out;
         }
+
+        return out;
     }
 
     /**
@@ -471,55 +471,55 @@ public class BlockRealMatrix extends AbstractRealMatrix implements Serializable
     @Override
     public BlockRealMatrix multiply(final RealMatrix m)
         throws DimensionMismatchException {
-        try {
+        if (m instanceof BlockRealMatrix) {
             return multiply((BlockRealMatrix) m);
-        } catch (ClassCastException cce) {
-            // safety check
-            checkMultiply(m);
+        }
 
-            final BlockRealMatrix out = new BlockRealMatrix(rows, m.getColumnDimension());
+        // safety check
+        checkMultiply(m);
 
-            // perform multiplication block-wise, to ensure good cache behavior
-            int blockIndex = 0;
-            for (int iBlock = 0; iBlock < out.blockRows; ++iBlock) {
-                final int pStart = iBlock * BLOCK_SIZE;
-                final int pEnd = FastMath.min(pStart + BLOCK_SIZE, rows);
+        final BlockRealMatrix out = new BlockRealMatrix(rows, m.getColumnDimension());
 
-                for (int jBlock = 0; jBlock < out.blockColumns; ++jBlock) {
-                    final int qStart = jBlock * BLOCK_SIZE;
-                    final int qEnd = FastMath.min(qStart + BLOCK_SIZE, m.getColumnDimension());
-
-                    // select current block
-                    final double[] outBlock = out.blocks[blockIndex];
-
-                    // perform multiplication on current block
-                    for (int kBlock = 0; kBlock < blockColumns; ++kBlock) {
-                        final int kWidth = blockWidth(kBlock);
-                        final double[] tBlock = blocks[iBlock * blockColumns + kBlock];
-                        final int rStart = kBlock * BLOCK_SIZE;
-                        int k = 0;
-                        for (int p = pStart; p < pEnd; ++p) {
-                            final int lStart = (p - pStart) * kWidth;
-                            final int lEnd = lStart + kWidth;
-                            for (int q = qStart; q < qEnd; ++q) {
-                                double sum = 0;
-                                int r = rStart;
-                                for (int l = lStart; l < lEnd; ++l) {
-                                    sum += tBlock[l] * m.getEntry(r, q);
-                                    ++r;
-                                }
-                                outBlock[k] += sum;
-                                ++k;
+        // perform multiplication block-wise, to ensure good cache behavior
+        int blockIndex = 0;
+        for (int iBlock = 0; iBlock < out.blockRows; ++iBlock) {
+            final int pStart = iBlock * BLOCK_SIZE;
+            final int pEnd = FastMath.min(pStart + BLOCK_SIZE, rows);
+
+            for (int jBlock = 0; jBlock < out.blockColumns; ++jBlock) {
+                final int qStart = jBlock * BLOCK_SIZE;
+                final int qEnd = FastMath.min(qStart + BLOCK_SIZE, m.getColumnDimension());
+
+                // select current block
+                final double[] outBlock = out.blocks[blockIndex];
+
+                // perform multiplication on current block
+                for (int kBlock = 0; kBlock < blockColumns; ++kBlock) {
+                    final int kWidth = blockWidth(kBlock);
+                    final double[] tBlock = blocks[iBlock * blockColumns + kBlock];
+                    final int rStart = kBlock * BLOCK_SIZE;
+                    int k = 0;
+                    for (int p = pStart; p < pEnd; ++p) {
+                        final int lStart = (p - pStart) * kWidth;
+                        final int lEnd = lStart + kWidth;
+                        for (int q = qStart; q < qEnd; ++q) {
+                            double sum = 0;
+                            int r = rStart;
+                            for (int l = lStart; l < lEnd; ++l) {
+                                sum += tBlock[l] * m.getEntry(r, q);
+                                ++r;
                             }
+                            outBlock[k] += sum;
+                            ++k;
                         }
                     }
-                    // go to next block
-                    ++blockIndex;
                 }
+                // go to next block
+                ++blockIndex;
             }
-
-            return out;
         }
+
+        return out;
     }
 
     /**
@@ -870,9 +870,9 @@ public class BlockRealMatrix extends AbstractRealMatrix implements Serializable
     @Override
     public void setRowMatrix(final int row, final RealMatrix matrix)
         throws OutOfRangeException, MatrixDimensionMismatchException {
-        try {
+        if (matrix instanceof BlockRealMatrix) {
             setRowMatrix(row, (BlockRealMatrix) matrix);
-        } catch (ClassCastException cce) {
+        } else {
             super.setRowMatrix(row, matrix);
         }
     }
@@ -954,9 +954,9 @@ public class BlockRealMatrix extends AbstractRealMatrix implements Serializable
     @Override
     public void setColumnMatrix(final int column, final RealMatrix matrix)
         throws OutOfRangeException, MatrixDimensionMismatchException {
-        try {
+        if (matrix instanceof BlockRealMatrix) {
             setColumnMatrix(column, (BlockRealMatrix) matrix);
-        } catch (ClassCastException cce) {
+        } else {
             super.setColumnMatrix(column, matrix);
         }
     }
@@ -1028,9 +1028,9 @@ public class BlockRealMatrix extends AbstractRealMatrix implements Serializable
     @Override
     public void setRowVector(final int row, final RealVector vector)
         throws OutOfRangeException, MatrixDimensionMismatchException {
-        try {
+        if (vector instanceof ArrayRealVector) {
             setRow(row, ((ArrayRealVector) vector).getDataRef());
-        } catch (ClassCastException cce) {
+        } else {
             super.setRowVector(row, vector);
         }
     }
@@ -1062,9 +1062,9 @@ public class BlockRealMatrix extends AbstractRealMatrix implements Serializable
     @Override
     public void setColumnVector(final int column, final RealVector vector)
         throws OutOfRangeException, MatrixDimensionMismatchException {
-        try {
+        if (vector instanceof ArrayRealVector) {
             setColumn(column, ((ArrayRealVector) vector).getDataRef());
-        } catch (ClassCastException cce) {
+        } else {
             super.setColumnVector(column, vector);
         }
     }
diff --git a/src/main/java/org/apache/commons/math4/linear/FieldLUDecomposition.java b/src/main/java/org/apache/commons/math4/linear/FieldLUDecomposition.java
index e733f42..873da4e 100644
--- a/src/main/java/org/apache/commons/math4/linear/FieldLUDecomposition.java
+++ b/src/main/java/org/apache/commons/math4/linear/FieldLUDecomposition.java
@@ -297,44 +297,42 @@ public class FieldLUDecomposition<T extends FieldElement<T>> {
         /** {@inheritDoc} */
         @Override
         public FieldVector<T> solve(FieldVector<T> b) {
-            try {
+            if (b instanceof ArrayFieldVector) {
                 return solve((ArrayFieldVector<T>) b);
-            } catch (ClassCastException cce) {
+            }
 
-                final int m = pivot.length;
-                if (b.getDimension() != m) {
-                    throw new DimensionMismatchException(b.getDimension(), m);
-                }
-                if (singular) {
-                    throw new SingularMatrixException();
-                }
+            final int m = pivot.length;
+            if (b.getDimension() != m) {
+                throw new DimensionMismatchException(b.getDimension(), m);
+            }
+            if (singular) {
+                throw new SingularMatrixException();
+            }
 
-                // Apply permutations to b
-                final T[] bp = MathArrays.buildArray(field, m);
-                for (int row = 0; row < m; row++) {
-                    bp[row] = b.getEntry(pivot[row]);
-                }
+            // Apply permutations to b
+            final T[] bp = MathArrays.buildArray(field, m);
+            for (int row = 0; row < m; row++) {
+                bp[row] = b.getEntry(pivot[row]);
+            }
 
-                // Solve LY = b
-                for (int col = 0; col < m; col++) {
-                    final T bpCol = bp[col];
-                    for (int i = col + 1; i < m; i++) {
-                        bp[i] = bp[i].subtract(bpCol.multiply(lu[i][col]));
-                    }
+            // Solve LY = b
+            for (int col = 0; col < m; col++) {
+                final T bpCol = bp[col];
+                for (int i = col + 1; i < m; i++) {
+                    bp[i] = bp[i].subtract(bpCol.multiply(lu[i][col]));
                 }
+            }
 
-                // Solve UX = Y
-                for (int col = m - 1; col >= 0; col--) {
-                    bp[col] = bp[col].divide(lu[col][col]);
-                    final T bpCol = bp[col];
-                    for (int i = 0; i < col; i++) {
-                        bp[i] = bp[i].subtract(bpCol.multiply(lu[i][col]));
-                    }
+            // Solve UX = Y
+            for (int col = m - 1; col >= 0; col--) {
+                bp[col] = bp[col].divide(lu[col][col]);
+                final T bpCol = bp[col];
+                for (int i = 0; i < col; i++) {
+                    bp[i] = bp[i].subtract(bpCol.multiply(lu[i][col]));
                 }
-
-                return new ArrayFieldVector<>(field, bp, false);
-
             }
+
+            return new ArrayFieldVector<>(field, bp, false);
         }
 
         /** Solve the linear equation A &times; X = B.
diff --git a/src/main/java/org/apache/commons/math4/linear/OpenMapRealMatrix.java b/src/main/java/org/apache/commons/math4/linear/OpenMapRealMatrix.java
index a8382eb..20b730c 100644
--- a/src/main/java/org/apache/commons/math4/linear/OpenMapRealMatrix.java
+++ b/src/main/java/org/apache/commons/math4/linear/OpenMapRealMatrix.java
@@ -135,11 +135,10 @@ public class OpenMapRealMatrix extends AbstractRealMatrix
     @Override
     public OpenMapRealMatrix subtract(final RealMatrix m)
         throws MatrixDimensionMismatchException {
-        try {
+        if (m instanceof OpenMapRealMatrix) {
             return subtract((OpenMapRealMatrix) m);
-        } catch (ClassCastException cce) {
-            return (OpenMapRealMatrix) super.subtract(m);
         }
+        return (OpenMapRealMatrix) super.subtract(m);
     }
 
     /**
@@ -175,27 +174,26 @@ public class OpenMapRealMatrix extends AbstractRealMatrix
     @Override
     public RealMatrix multiply(final RealMatrix m)
         throws DimensionMismatchException, NumberIsTooLargeException {
-        try {
+        if (m instanceof OpenMapRealMatrix) {
             return multiply((OpenMapRealMatrix) m);
-        } catch (ClassCastException cce) {
-
-            MatrixUtils.checkMultiplicationCompatible(this, m);
-
-            final int outCols = m.getColumnDimension();
-            final BlockRealMatrix out = new BlockRealMatrix(rows, outCols);
-            for (OpenIntToDoubleHashMap.Iterator iterator = entries.iterator(); iterator.hasNext();) {
-                iterator.advance();
-                final double value = iterator.value();
-                final int key      = iterator.key();
-                final int i        = key / columns;
-                final int k        = key % columns;
-                for (int j = 0; j < outCols; ++j) {
-                    out.addToEntry(i, j, value * m.getEntry(k, j));
-                }
-            }
+        }
+
+        MatrixUtils.checkMultiplicationCompatible(this, m);
 
-            return out;
+        final int outCols = m.getColumnDimension();
+        final BlockRealMatrix out = new BlockRealMatrix(rows, outCols);
+        for (OpenIntToDoubleHashMap.Iterator iterator = entries.iterator(); iterator.hasNext();) {
+            iterator.advance();
+            final double value = iterator.value();
+            final int key      = iterator.key();
+            final int i        = key / columns;
+            final int k        = key % columns;
+            for (int j = 0; j < outCols; ++j) {
+                out.addToEntry(i, j, value * m.getEntry(k, j));
+            }
         }
+
+        return out;
     }
 
     /**