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.mantissa;
025    
026    import org.spaceroots.mantissa.linalg.GeneralMatrix;
027    import org.spaceroots.mantissa.linalg.GeneralSquareMatrix;
028    import org.spaceroots.mantissa.linalg.SquareMatrix;
029    import org.ujmp.core.Coordinates;
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.UJMPSettings;
035    
036    public class MantissaDenseDoubleMatrix2D extends AbstractDenseDoubleMatrix2D
037                    implements Wrapper<org.spaceroots.mantissa.linalg.Matrix> {
038            private static final long serialVersionUID = 6954233090244549806L;
039    
040            private org.spaceroots.mantissa.linalg.Matrix matrix = null;
041    
042            public MantissaDenseDoubleMatrix2D(org.spaceroots.mantissa.linalg.Matrix m) {
043                    this.matrix = m;
044            }
045    
046            public MantissaDenseDoubleMatrix2D(long... size) {
047                    if (size[ROW] > 0 && size[COLUMN] > 0) {
048                            if (size[ROW] == size[COLUMN]) {
049                                    this.matrix = new GeneralSquareMatrix((int) size[ROW]);
050                            } else {
051                                    this.matrix = new GeneralMatrix((int) size[ROW],
052                                                    (int) size[COLUMN]);
053                            }
054                    }
055            }
056    
057            public MantissaDenseDoubleMatrix2D(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.getElement((int) row, (int) column);
066            }
067    
068            public double getDouble(int row, int column) {
069                    return matrix.getElement(row, column);
070            }
071    
072            public long[] getSize() {
073                    return matrix == null ? Coordinates.ZERO2D : new long[] {
074                                    matrix.getRows(), matrix.getColumns() };
075            }
076    
077            public void setDouble(double value, long row, long column) {
078                    matrix.setElement((int) row, (int) column, value);
079            }
080    
081            public void setDouble(double value, int row, int column) {
082                    matrix.setElement(row, column, value);
083            }
084    
085            public Matrix transpose() {
086                    return new MantissaDenseDoubleMatrix2D(matrix.getTranspose());
087            }
088    
089            public Matrix inv() {
090                    if (matrix instanceof SquareMatrix) {
091                            try {
092                                    return new MantissaDenseDoubleMatrix2D(((SquareMatrix) matrix)
093                                                    .getInverse(UJMPSettings.getTolerance()));
094                            } catch (Exception e) {
095                                    throw new MatrixException(e);
096                            }
097                    } else {
098                            throw new MatrixException("only allowed for square matrices");
099                    }
100            }
101    
102            public Matrix solve(Matrix b) {
103                    if (matrix instanceof SquareMatrix
104                                    && b instanceof MantissaDenseDoubleMatrix2D) {
105                            try {
106                                    org.spaceroots.mantissa.linalg.Matrix b2 = ((MantissaDenseDoubleMatrix2D) b)
107                                                    .getWrappedObject();
108                                    return new MantissaDenseDoubleMatrix2D(((SquareMatrix) matrix)
109                                                    .solve(b2, UJMPSettings.getTolerance()));
110                            } catch (Exception e) {
111                                    throw new MatrixException(e);
112                            }
113                    } else {
114                            throw new MatrixException("only allowed for square matrices");
115                    }
116            }
117    
118            public Matrix mtimes(Matrix m) {
119                    if (m instanceof MantissaDenseDoubleMatrix2D) {
120                            return new MantissaDenseDoubleMatrix2D(matrix
121                                            .mul(((MantissaDenseDoubleMatrix2D) m).getWrappedObject()));
122                    } else {
123                            return super.mtimes(m);
124                    }
125            }
126    
127            public org.spaceroots.mantissa.linalg.Matrix getWrappedObject() {
128                    return matrix;
129            }
130    
131            public void setWrappedObject(org.spaceroots.mantissa.linalg.Matrix object) {
132                    this.matrix = object;
133            }
134    
135    }