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.owlpack;
025    
026    import java.io.IOException;
027    import java.io.ObjectInputStream;
028    import java.io.ObjectOutputStream;
029    
030    import org.ujmp.core.Matrix;
031    import org.ujmp.core.doublematrix.stub.AbstractDenseDoubleMatrix2D;
032    import org.ujmp.core.interfaces.Wrapper;
033    
034    import edu.rice.linpack.Matrix.DMatrix.DFull;
035    
036    public class OwlpackDenseDoubleMatrix2D extends AbstractDenseDoubleMatrix2D
037                    implements Wrapper<DFull> {
038            private static final long serialVersionUID = 1341952001270932703L;
039    
040            private transient DFull matrix = null;
041    
042            public OwlpackDenseDoubleMatrix2D(final long... size) {
043                    matrix = new DFull((int) size[ROW], (int) size[COLUMN]);
044            }
045    
046            public OwlpackDenseDoubleMatrix2D(final Matrix m) {
047                    this(m.getSize());
048                    for (final long[] c : m.availableCoordinates()) {
049                            setDouble(m.getAsDouble(c), c);
050                    }
051            }
052    
053            public OwlpackDenseDoubleMatrix2D(final DFull matrix) {
054                    this.matrix = matrix;
055            }
056    
057            public double getDouble(final int row, final int column) {
058                    return matrix.getElem(row, column);
059            }
060    
061            public double getDouble(final long row, final long column) {
062                    return matrix.getElem((int) row, (int) column);
063            }
064    
065            public long[] getSize() {
066                    return new long[] { matrix.numofRows(), matrix.numofCols() };
067            }
068    
069            public DFull getWrappedObject() {
070                    return matrix;
071            }
072    
073            @Override
074            public Matrix mtimes(final Matrix m) {
075                    if (m instanceof OwlpackDenseDoubleMatrix2D) {
076                            final DFull mo = ((OwlpackDenseDoubleMatrix2D) m)
077                                            .getWrappedObject();
078                            final DFull result = matrix.matMult(mo);
079                            return new OwlpackDenseDoubleMatrix2D(result);
080                    } else {
081                            return super.mtimes(m);
082                    }
083            }
084    
085            public void setDouble(final double value, final int row, final int column) {
086                    matrix.setElem(row, column, value);
087            }
088    
089            public void setDouble(final double value, final long row, final long column) {
090                    matrix.setElem((int) row, (int) column, value);
091            }
092    
093            public void setWrappedObject(final DFull object) {
094                    matrix = object;
095            }
096    
097            @Override
098            public Matrix transpose() {
099                    DFull result = new DFull((int) getColumnCount(), (int) getRowCount());
100                    matrix.transpose(result);
101                    return new OwlpackDenseDoubleMatrix2D(result);
102            }
103    
104            @Override
105            public Matrix inv() {
106                    DFull result = new DFull(matrix);
107                    result.inverse();
108                    return new OwlpackDenseDoubleMatrix2D(result);
109            }
110    
111            @Override
112            public Matrix[] svd() {
113                    int p = (int) getRowCount();
114                    int n = (int) getColumnCount();
115                    double[] S = new double[Math.min(n + 1, p)];
116                    double[] E = new double[p];
117                    DFull U = new DFull(n, Math.min(n, p));
118                    DFull V = new DFull(p, p);
119                    matrix.svDecompose(S, E, U, V, 2);
120                    Matrix u = new OwlpackDenseDoubleMatrix2D(U);
121                    Matrix v = new OwlpackDenseDoubleMatrix2D(V);
122                    Matrix s = new OwlpackDenseDoubleMatrix2D(S.length, S.length);
123                    for (int i = 0; i < S.length; i++) {
124                            s.setAsDouble(S[i], i, i);
125                    }
126                    return new Matrix[] { u, s, v };
127            }
128    
129            private void readObject(final ObjectInputStream s) throws IOException,
130                            ClassNotFoundException {
131                    s.defaultReadObject();
132                    final double[][] data = (double[][]) s.readObject();
133                    int rows = data.length;
134                    int cols = data[0].length;
135                    matrix = new DFull(rows, cols);
136                    for (int r = 0; r < rows; r++) {
137                            for (int c = 0; c < cols; c++) {
138                                    setDouble(data[r][c], r, c);
139                            }
140                    }
141            }
142    
143            private void writeObject(final ObjectOutputStream s) throws IOException {
144                    s.defaultWriteObject();
145                    s.writeObject(this.toDoubleArray());
146            }
147    
148    }