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.sst;
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.exceptions.MatrixException;
033    import org.ujmp.core.interfaces.Wrapper;
034    import org.ujmp.core.util.MathUtil;
035    
036    import shared.array.RealArray;
037    
038    public class SSTDenseDoubleMatrix2D extends AbstractDenseDoubleMatrix2D
039                    implements Wrapper<RealArray> {
040            private static final long serialVersionUID = -9002457298955206969L;
041    
042            private transient RealArray data = null;
043    
044            public SSTDenseDoubleMatrix2D(RealArray data) {
045                    this.data = data;
046            }
047    
048            public SSTDenseDoubleMatrix2D(long... size) {
049                    data = new RealArray(MathUtil.toIntArray(size));
050            }
051    
052            public SSTDenseDoubleMatrix2D(Matrix source) {
053                    data = new RealArray(MathUtil.toIntArray(source.getSize()));
054                    for (long[] c : source.availableCoordinates()) {
055                            setDouble(source.getAsDouble(c), c);
056                    }
057            }
058    
059            public double getDouble(long rows, long columns) throws MatrixException {
060                    return data.get((int) rows, (int) columns);
061            }
062    
063            public double getDouble(int rows, int columns) throws MatrixException {
064                    return data.get(rows, columns);
065            }
066    
067            public void setDouble(double value, long rows, long columns)
068                            throws MatrixException {
069                    data.set(value, (int) rows, (int) columns);
070            }
071    
072            public void setDouble(double value, int rows, int columns)
073                            throws MatrixException {
074                    data.set(value, rows, columns);
075            }
076    
077            public long[] getSize() {
078                    return MathUtil.toLongArray(data.dimensions());
079            }
080    
081            private void readObject(ObjectInputStream s) throws IOException,
082                            ClassNotFoundException {
083                    s.defaultReadObject();
084                    byte[] bytes = (byte[]) s.readObject();
085                    data = RealArray.parse(bytes);
086            }
087    
088            private void writeObject(ObjectOutputStream s) throws IOException,
089                            MatrixException {
090                    s.defaultWriteObject();
091                    s.writeObject(data.getBytes());
092            }
093    
094            public RealArray getWrappedObject() {
095                    return data;
096            }
097    
098            public void setWrappedObject(RealArray object) {
099                    this.data = object;
100            }
101    
102            public Matrix transpose() {
103                    return new SSTDenseDoubleMatrix2D(data.mTranspose());
104            }
105    
106            public Matrix inv() {
107                    return new SSTDenseDoubleMatrix2D(data.mInvert());
108            }
109    
110            public Matrix[] eig() {
111                    RealArray[] eig = data.mEigs();
112                    Matrix v = new SSTDenseDoubleMatrix2D(eig[0]);
113                    Matrix d = new SSTDenseDoubleMatrix2D(eig[1]);
114                    return new Matrix[] { v, d };
115            }
116    
117            public Matrix[] svd() {
118                    RealArray[] svd = data.mSVD();
119                    Matrix u = new SSTDenseDoubleMatrix2D(svd[0]);
120                    Matrix s = new SSTDenseDoubleMatrix2D(svd[1]);
121                    Matrix v = new SSTDenseDoubleMatrix2D(svd[2]);
122                    return new Matrix[] { u, s, v };
123            }
124    
125            public Matrix mtimes(Matrix m) {
126                    if (m instanceof SSTDenseDoubleMatrix2D) {
127                            return new SSTDenseDoubleMatrix2D(data
128                                            .mMul(((SSTDenseDoubleMatrix2D) m).getWrappedObject()));
129                    } else {
130                            return super.mtimes(m);
131                    }
132            }
133    
134            public Matrix plus(double v) {
135                    return new SSTDenseDoubleMatrix2D(data.clone().uAdd(v));
136            }
137    
138            public Matrix plus(Matrix m) {
139                    if (m instanceof SSTDenseDoubleMatrix2D) {
140                            return new SSTDenseDoubleMatrix2D(data.clone().eAdd(
141                                            ((SSTDenseDoubleMatrix2D) m).getWrappedObject()));
142                    } else {
143                            return super.plus(m);
144                    }
145            }
146    
147            public Matrix minus(Matrix m) {
148                    if (m instanceof SSTDenseDoubleMatrix2D) {
149                            return new SSTDenseDoubleMatrix2D(data.clone().eSub(
150                                            ((SSTDenseDoubleMatrix2D) m).getWrappedObject()));
151                    } else {
152                            return super.plus(m);
153                    }
154            }
155    
156            public Matrix minus(double v) {
157                    return new SSTDenseDoubleMatrix2D(data.clone().uAdd(-v));
158            }
159    
160            public Matrix times(double v) {
161                    return new SSTDenseDoubleMatrix2D(data.clone().uMul(v));
162            }
163    
164            public Matrix divide(double v) {
165                    return new SSTDenseDoubleMatrix2D(data.clone().uMul(1 / v));
166            }
167    
168    }