001    /*
002     * Copyright (C) 2008-2010 by Holger Arndt
003     *
004     * This file is part of the Universal Java Matrix Package (UJMP).
005     * See the NOTICE file distributed with this work for additional
006     * information regarding copyright ownership and licensing.
007     *
008     * UJMP is free software; you can redistribute it and/or modify
009     * it under the terms of the GNU Lesser General Public License as
010     * published by the Free Software Foundation; either version 2
011     * of the License, or (at your option) any later version.
012     *
013     * UJMP is distributed in the hope that it will be useful,
014     * but WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016     * GNU Lesser General Public License for more details.
017     *
018     * You should have received a copy of the GNU Lesser General Public
019     * License along with UJMP; if not, write to the
020     * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
021     * Boston, MA  02110-1301  USA
022     */
023    
024    package org.ujmp.parallelcolt;
025    
026    import java.util.HashSet;
027    import java.util.Iterator;
028    import java.util.Set;
029    
030    import org.ujmp.core.Coordinates;
031    import org.ujmp.core.Matrix;
032    import org.ujmp.core.doublematrix.stub.AbstractSparseDoubleMatrix2D;
033    import org.ujmp.core.exceptions.MatrixException;
034    import org.ujmp.core.interfaces.Wrapper;
035    import org.ujmp.core.util.CoordinateSetToLongWrapper;
036    
037    import cern.colt.matrix.tdouble.algo.DenseDoubleAlgebra;
038    import cern.colt.matrix.tdouble.impl.DenseDoubleMatrix2D;
039    import cern.colt.matrix.tdouble.impl.SparseDoubleMatrix2D;
040    
041    public class ParallelColtSparseDoubleMatrix2D extends
042                    AbstractSparseDoubleMatrix2D implements Wrapper<SparseDoubleMatrix2D> {
043            private static final long serialVersionUID = -3223474248020842822L;
044    
045            private SparseDoubleMatrix2D matrix = null;
046    
047            public ParallelColtSparseDoubleMatrix2D(long... size) {
048                    this.matrix = new SparseDoubleMatrix2D((int) size[ROW],
049                                    (int) size[COLUMN]);
050            }
051    
052            public ParallelColtSparseDoubleMatrix2D(SparseDoubleMatrix2D m) {
053                    this.matrix = m;
054            }
055    
056            public ParallelColtSparseDoubleMatrix2D(Matrix source)
057                            throws MatrixException {
058                    this(source.getSize());
059                    for (long[] c : source.availableCoordinates()) {
060                            setDouble(source.getAsDouble(c), c);
061                    }
062            }
063    
064            public double getDouble(long row, long column) {
065                    return matrix.getQuick((int) row, (int) column);
066            }
067    
068            public double getDouble(int row, int column) {
069                    return matrix.getQuick(row, column);
070            }
071    
072            public long[] getSize() {
073                    return new long[] { matrix.rows(), matrix.columns() };
074            }
075    
076            public void setDouble(double value, long row, long column) {
077                    matrix.setQuick((int) row, (int) column, value);
078            }
079    
080            public void setDouble(double value, int row, int column) {
081                    matrix.setQuick(row, column, value);
082            }
083    
084            public SparseDoubleMatrix2D getWrappedObject() {
085                    return matrix;
086            }
087    
088            public void setWrappedObject(SparseDoubleMatrix2D object) {
089                    this.matrix = object;
090            }
091    
092            public Matrix inv() {
093                    return new ParallelColtDenseDoubleMatrix2D(
094                                    (DenseDoubleMatrix2D) new DenseDoubleAlgebra().inverse(matrix));
095            }
096    
097            public Iterable<long[]> availableCoordinates() {
098                    return new AvailableCoordinateIterable();
099            }
100    
101            class AvailableCoordinateIterable implements Iterable<long[]> {
102    
103                    public Iterator<long[]> iterator() {
104                            Set<Coordinates> cset = new HashSet<Coordinates>();
105                            for (long r = getRowCount() - 1; r >= 0; r--) {
106                                    for (long c = getColumnCount() - 1; c >= 0; c--) {
107                                            if (getDouble(r, c) != 0.0) {
108                                                    cset.add(new Coordinates(r, c));
109                                            }
110                                    }
111                            }
112                            return new CoordinateSetToLongWrapper(cset).iterator();
113                    }
114            }
115    
116            public final boolean contains(long... coordinates) {
117                    return getAsDouble(coordinates) != 0.0;
118            }
119    }