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.colt;
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.impl.DenseDoubleMatrix2D;
038    import cern.colt.matrix.impl.SparseDoubleMatrix2D;
039    import cern.colt.matrix.linalg.Algebra;
040    import cern.jet.math.Functions;
041    
042    public class ColtSparseDoubleMatrix2D extends AbstractSparseDoubleMatrix2D
043                    implements Wrapper<SparseDoubleMatrix2D> {
044            private static final long serialVersionUID = -3223474248020842822L;
045    
046            private SparseDoubleMatrix2D matrix = null;
047    
048            public ColtSparseDoubleMatrix2D(long... size) {
049                    this.matrix = new SparseDoubleMatrix2D((int) size[ROW],
050                                    (int) size[COLUMN]);
051            }
052    
053            public ColtSparseDoubleMatrix2D(SparseDoubleMatrix2D m) {
054                    this.matrix = m;
055            }
056    
057            public ColtSparseDoubleMatrix2D(Matrix source) 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 ColtDenseDoubleMatrix2D((DenseDoubleMatrix2D) new Algebra()
094                                    .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    
120            public Matrix transpose() {
121                    return new ColtSparseDoubleMatrix2D((SparseDoubleMatrix2D) matrix
122                                    .viewDice().copy());
123            }
124    
125            public Matrix plus(double value) {
126                    return new ColtSparseDoubleMatrix2D((SparseDoubleMatrix2D) matrix
127                                    .copy().assign(Functions.plus(value)));
128            }
129    
130            public Matrix times(double value) {
131                    return new ColtSparseDoubleMatrix2D((SparseDoubleMatrix2D) matrix
132                                    .copy().assign(Functions.mult(value)));
133            }
134    
135            public Matrix copy() {
136                    Matrix m = new ColtSparseDoubleMatrix2D((SparseDoubleMatrix2D) matrix
137                                    .copy());
138                    if (getAnnotation() != null) {
139                            m.setAnnotation(getAnnotation().clone());
140                    }
141                    return m;
142            }
143    
144            public Matrix mtimes(Matrix m) {
145                    if (m instanceof ColtSparseDoubleMatrix2D) {
146                            SparseDoubleMatrix2D ret = new SparseDoubleMatrix2D(
147                                            (int) getRowCount(), (int) m.getColumnCount());
148                            matrix.zMult(((ColtSparseDoubleMatrix2D) m).matrix, ret);
149                            return new ColtSparseDoubleMatrix2D(ret);
150                    } else {
151                            return super.mtimes(m);
152                    }
153            }
154    
155    }