A matrix operation to multiply m0 and m1. - Android java.lang

Android examples for java.lang:Math Matrix

Description

A matrix operation to multiply m0 and m1.

Demo Code

/*//from   ww  w  . j a  v  a2 s. c  om
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import android.util.Log;
import java.util.Arrays;

public class Main{
    /**
     * A matrix operation to multiply m0 and m1.
     */

    public static void multiply(final float[][] m0, final float[][] m1,
            final float[][] retval) throws MatrixOperationFailedException {
        if (m0[0].length != m1.length) {
            throw new MatrixOperationFailedException(
                    "--- invalid length for multiply " + m0[0].length
                            + ", " + m1.length);
        }
        final int m0h = m0.length;
        final int m0w = m0[0].length;
        final int m1w = m1[0].length;
        if (retval.length != m0h || retval[0].length != m1w) {
            throw new MatrixOperationFailedException(
                    "--- invalid length of retval " + retval.length + ", "
                            + retval[0].length);
        }

        for (int i = 0; i < m0h; i++) {
            Arrays.fill(retval[i], 0);
            for (int j = 0; j < m1w; j++) {
                for (int k = 0; k < m0w; k++) {
                    retval[i][j] += m0[i][k] * m1[k][j];
                }
            }
        }
    }
}

Related Tutorials