예제 #1
0
  @Override
  public ArrayVector matrixVectorMultiplication(Matrix m) {
    if (m.getN() != getSize()) {
      throw new IllegalArgumentException(
          "The matrix's width (N) must " + "be the same as this vector's size");
    }
    ArrayVector result = new ArrayVector(getSize());
    // result[i] = sum_j(this[j] * m[i][j])
    boolean multithread = false; // doesn't exit yet ;-(
    if (!multithread) {
      float temp;
      for (int i = 0; i < m.getM(); i++) {
        temp = 0.0f;
        for (int j : m.getNonZeroRowIndecies(i)) {
          temp += getValue(j) * m.getValue(i, j);
        }
        result.setValue(i, temp);
      }
    } else {
      int numThreads = multiplier.length;
      CountDownLatch mainLatch = new CountDownLatch(numThreads);
      int perThread = m.getM() / numThreads;
      for (int i = 0; i < numThreads; i++) {
        int startIndex = i * perThread;
        int endIndex = startIndex + perThread - 1;
        if (i + 1 == numThreads) {
          endIndex = m.getM() - 1;
        }
        multiplier[i] = new MatrixVectorMultiplier(m, result, startIndex, endIndex);
        multiplier[i].setLatch(mainLatch);
        executor.execute(multiplier[i]);
      }
      try {
        mainLatch.await();
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }

    return result;
  }
예제 #2
0
 @Override
 public ArrayVector vectorSubtraction(Vector v) {
   ArrayVector result = new ArrayVector(vectorValues);
   result.subtract(v);
   return result;
 }
예제 #3
0
 @Override
 public ArrayVector scalarMultiplication(float s) {
   ArrayVector result = new ArrayVector(vectorValues);
   result.multiply(s);
   return result;
 }
예제 #4
0
 @Override
 public ArrayVector vectorAddition(Vector v) {
   ArrayVector result = new ArrayVector(vectorValues);
   result.add(v);
   return result;
 }