001/**
002 * Copyright (c) 2011, The University of Southampton and the individual contributors.
003 * All rights reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without modification,
006 * are permitted provided that the following conditions are met:
007 *
008 *   *  Redistributions of source code must retain the above copyright notice,
009 *      this list of conditions and the following disclaimer.
010 *
011 *   *  Redistributions in binary form must reproduce the above copyright notice,
012 *      this list of conditions and the following disclaimer in the documentation
013 *      and/or other materials provided with the distribution.
014 *
015 *   *  Neither the name of the University of Southampton nor the names of its
016 *      contributors may be used to endorse or promote products derived from this
017 *      software without specific prior written permission.
018 *
019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029 */
030package org.openimaj.math.matrix;
031
032import java.util.Arrays;
033
034import Jama.Matrix;
035
036/**
037 * Dense matrix wrapper for a JAMA matrix.
038 * 
039 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
040 * 
041 */
042public class JamaDenseMatrix extends ch.akuhn.matrix.Matrix {
043        /** The underlying matrix */
044        public Matrix matrix;
045
046        /**
047         * Construct with matrix. The matrix is retained.
048         * 
049         * @param matrix
050         *            The matrix.
051         */
052        public JamaDenseMatrix(Matrix matrix) {
053                this.matrix = matrix;
054        }
055
056        /**
057         * Construct with 2d array of data.
058         * 
059         * @param values
060         *            The data.
061         */
062        public JamaDenseMatrix(double[][] values) {
063                this.matrix = new Matrix(values);
064                this.assertInvariant();
065        }
066
067        protected void assertInvariant() throws IllegalArgumentException {
068                if (matrix.getArray().length == 0)
069                        return;
070                final int m = matrix.getArray()[0].length;
071                for (int n = 0; n < matrix.getArray().length; n++) {
072                        if (matrix.getArray()[n].length != m)
073                                throw new IllegalArgumentException();
074                }
075        }
076
077        /**
078         * Construct with given dimensions.
079         * 
080         * @param rows
081         *            number of rows.
082         * @param columns
083         *            number of columns.
084         */
085        public JamaDenseMatrix(int rows, int columns) {
086                this.matrix = new Matrix(rows, columns);
087        }
088
089        @Override
090        public double add(int row, int column, double value) {
091                return matrix.getArray()[row][column] += value;
092        }
093
094        @Override
095        public int columnCount() {
096                return matrix.getColumnDimension();
097        }
098
099        @Override
100        public double get(int row, int column) {
101                return matrix.get(row, column);
102        }
103
104        @Override
105        public double put(int row, int column, double value) {
106                matrix.set(row, column, value);
107                return value;
108        }
109
110        @Override
111        public int rowCount() {
112                return matrix.getRowDimension();
113        }
114
115        @Override
116        public int used() {
117                throw null;
118        }
119
120        @Override
121        public double[][] unwrap() {
122                return matrix.getArray();
123        }
124
125        /**
126         * Fill the elements with a constant value.
127         * 
128         * @param constant
129         *            the value to set the elements to.
130         */
131        public void fill(double constant) {
132                for (final double[] row : matrix.getArray())
133                        Arrays.fill(row, constant);
134        }
135
136        /**
137         * Multiply all elements by a constant.
138         * 
139         * @param d
140         *            the multiplication factor.
141         */
142        public void applyMultiplication(double d) {
143                matrix.timesEquals(d);
144        }
145
146        /**
147         * @return the wrapped JAMA matrix
148         */
149        public Matrix getMatrix() {
150                return matrix;
151        }
152
153        @Override
154        public double[][] asArray() {
155                return matrix.getArray();
156        }
157
158        @Override
159        public ch.akuhn.matrix.Matrix newInstance(int rows, int cols) {
160                return new JamaDenseMatrix(rows, cols);
161        }
162}