| 1 |
/* |
| 2 |
* Copyright 2011-2014, by Vladimir Kostyukov and Contributors. |
| 3 |
* |
| 4 |
* This file is part of la4j project (http://la4j.org) |
| 5 |
* |
| 6 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 |
* You may not use this file except in compliance with the License. |
| 8 |
* You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, software |
| 13 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 14 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 |
* See the License for the specific language governing permissions and |
| 16 |
* limitations under the License. |
| 17 |
* |
| 18 |
* Contributor(s): - |
| 19 |
* |
| 20 |
*/ |
| 21 |
|
| 22 |
package org.la4j.operation.inplace; |
| 23 |
|
| 24 |
import org.la4j.iterator.MatrixIterator; |
| 25 |
import org.la4j.Matrix; |
| 26 |
import org.la4j.matrix.DenseMatrix; |
| 27 |
import org.la4j.operation.SimpleMatrixMatrixOperation; |
| 28 |
import org.la4j.matrix.ColumnMajorSparseMatrix; |
| 29 |
import org.la4j.matrix.RowMajorSparseMatrix; |
| 30 |
import org.la4j.matrix.SparseMatrix; |
| 31 |
|
| 32 |
public class InPlaceCopyMatrixToMatrix extends SimpleMatrixMatrixOperation<Matrix> { |
| 33 |
|
| 34 |
@Override |
| 35 |
public Matrix applySimple(DenseMatrix a, SparseMatrix b) { |
| 36 |
MatrixIterator it = b.iterator(); |
| 37 |
while (it.hasNext()) { |
| 38 |
it.next(); |
| 39 |
int i = it.rowIndex(); |
| 40 |
int j = it.columnIndex(); |
| 41 |
double x = a.get(i, j); |
| 42 |
if (x != 0.0) { |
| 43 |
it.set(x); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
return b; |
| 48 |
} |
| 49 |
|
| 50 |
@Override |
| 51 |
public Matrix applySimple(SparseMatrix a, DenseMatrix b) { |
| 52 |
return fromSparseToMatrix(a, b); |
| 53 |
} |
| 54 |
|
| 55 |
@Override |
| 56 |
public Matrix applySimple(SparseMatrix a, SparseMatrix b) { |
| 57 |
return fromSparseToMatrix(a, b); |
| 58 |
} |
| 59 |
|
| 60 |
@Override |
| 61 |
public Matrix apply(DenseMatrix a, DenseMatrix b) { |
| 62 |
for (int i = 0; i < a.rows(); i++) { |
| 63 |
for (int j = 0; j < a.columns(); j++) { |
| 64 |
b.set(i, j, a.get(i, j)); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
return b; |
| 69 |
} |
| 70 |
|
| 71 |
@Override |
| 72 |
public Matrix apply(RowMajorSparseMatrix a, ColumnMajorSparseMatrix b) { |
| 73 |
return fromSparseToMatrix(a, b); |
| 74 |
} |
| 75 |
|
| 76 |
@Override |
| 77 |
public Matrix apply(ColumnMajorSparseMatrix a, RowMajorSparseMatrix b) { |
| 78 |
return fromSparseToMatrix(a, b); |
| 79 |
} |
| 80 |
|
| 81 |
private Matrix fromSparseToMatrix(SparseMatrix a, Matrix b) { |
| 82 |
MatrixIterator it = a.nonZeroIterator(); |
| 83 |
while (it.hasNext()) { |
| 84 |
double x = it.next(); |
| 85 |
int i = it.rowIndex(); |
| 86 |
int j = it.columnIndex(); |
| 87 |
b.set(i, j, x); |
| 88 |
} |
| 89 |
|
| 90 |
return b; |
| 91 |
} |
| 92 |
} |