Java Array Multiply multiplySparse(int[][][] as, int A, int[][][] bs, int B, int[][][] cs)

Here you can find the source of multiplySparse(int[][][] as, int A, int[][][] bs, int B, int[][][] cs)

Description

sparse multiplication of two matrices, c = a * b' or c = a' * b (if transpose).

License

Common Public License

Parameter

Parameter Description
as sparse indices + weights [2 x M x A]
A a parameter
bs sparse indices + weights [2 x N x B]
B a parameter
cs sparse indices + weights [2 x A x B] or [2 x M x N], must be allocated as int[2][A][] or int[2][M][], respectively

Declaration


public static double multiplySparse(int[][][] as, int A, int[][][] bs,
        int B, int[][][] cs) 

Method Source Code

//package com.java2s;
//License from project: Common Public License 

import java.util.HashMap;

import java.util.Map;
import java.util.Map.Entry;

public class Main {
    /**/*w  w w .j  a v  a2  s .  c  om*/
     * sparse multiplication of two matrices, c = a * b' or c = a' * b (if
     * transpose).
     * 
     * @param as sparse indices + weights [2 x M x A]
     * @param A
     * @param bs sparse indices + weights [2 x N x B]
     * @param B
     * @param cs sparse indices + weights [2 x A x B] or [2 x M x N], must be
     *        allocated as int[2][A][] or int[2][M][], respectively
     */
    // TODO: * @param transp use transposes
    public static double multiplySparse(int[][][] as, int A, int[][][] bs,
            int B, int[][][] cs) {

        int[][] ax = as[0], aw = as[1], bx = bs[0], bw = bs[1];
        int M = ax.length;
        Map<Integer, Integer>[] x = new Map[A];
        long elements = 0;

        for (int m = 0; m < M; m++) {
            for (int i = 0; i < ax[m].length; i++) {
                int ii = ax[m][i];
                if (x[ii] == null) {
                    x[ii] = new HashMap<Integer, Integer>();
                }
                // we are in sparse row a (which iterates differently
                // with m), now iterate sparse colume b (also different per m)
                for (int j = 0; j < bx[m].length; j++) {
                    // product c_ij = a_mi * b_mj
                    int prod = aw[m][i] * bw[m][j];
                    // get element in c_ij
                    Integer ij = x[ii].get(bx[m][j]);
                    if (ij == null) {
                        x[ii].put(bx[m][j], prod);
                        // Print.fln(
                        // "%d sparse m = %d, a = %d, b = %d, full a = %d, b = %d, prod = %d",
                        // (elements++), m, i, j, ax[m][i], bx[m][j], prod);
                    } else {
                        // add new value to element
                        x[ii].put(bx[m][j], prod + x[ii].get(bx[m][j]));
                    }
                }
            }
        }
        // now fill the sparse array
        for (int i = 0; i < A; i++) {
            if (x[i] != null) {
                cs[0][i] = new int[x[i].size()];
                cs[1][i] = new int[x[i].size()];
                int j = 0;
                for (Entry<Integer, Integer> e : x[i].entrySet()) {
                    // Print.fln("i = %d, s = %d, val = %d", i, e.getKey(),
                    // e.getValue());
                    cs[0][i][j] = e.getKey();
                    cs[1][i][j] = e.getValue();
                    j++;
                }
            } else {
                cs[0][i] = new int[0];
                cs[1][i] = new int[0];
            }
        }
        return elements / (double) A / (double) B;
    }
}

Related

  1. MultiplyPointSimilarityInhomogenous(double[] xp, int idx, double[] H, double[] x, int idx2)
  2. multiplyRange(double[] accumulator, int offset, double[] modulator)
  3. multiplyScalar(double[] a, double value)
  4. multiplyScalarInPlace(double[] a, double value)
  5. multiplySelf(double[] dest, double source)
  6. multiplySquares(float[] mat1, float[] mat2, int sidelength)
  7. multiplyValues(byte[] array, int multiplier)
  8. multiplyVecorWithScalar(double[] v, double s)
  9. multiplyVector(float[] mat, float[] vec, float[] dest)