001package ch.akuhn.matrix;
002
003import java.util.Arrays;
004import java.util.Iterator;
005
006/**
007 * A Dense matrix.
008 * 
009 * @author Adrian Kuhn
010 * 
011 */
012public class DenseMatrix extends Matrix {
013
014        protected double[][] values;
015
016        /**
017         * Construct with the given values
018         * 
019         * @param values
020         *            the values
021         */
022        public DenseMatrix(double[][] values) {
023                this.values = values;
024                this.assertInvariant();
025        }
026
027        protected void assertInvariant() throws IllegalArgumentException {
028                if (values.length == 0)
029                        return;
030                final int m = values[0].length;
031                for (int n = 0; n < values.length; n++) {
032                        if (values[n].length != m)
033                                throw new IllegalArgumentException();
034                }
035        }
036
037        /**
038         * Construct with the given size
039         * 
040         * @param rows
041         * @param columns
042         */
043        public DenseMatrix(int rows, int columns) {
044                this.values = makeValues(rows, columns);
045                this.assertInvariant();
046        }
047
048        protected double[][] makeValues(int rows, int columns) {
049                return new double[rows][columns];
050        }
051
052        @Override
053        public double add(int row, int column, double value) {
054                return values[row][column] += value;
055        }
056
057        @Override
058        public int columnCount() {
059                return values[0].length;
060        }
061
062        @Override
063        public double get(int row, int column) {
064                return values[row][column];
065        }
066
067        @Override
068        public double put(int row, int column, double value) {
069                return values[row][column] = value;
070        }
071        
072        @Override
073        public Iterable<Vector> rows() {
074                return new Iterable<Vector>() {
075                        
076                        @Override
077                        public Iterator<Vector> iterator() {
078                                return new Iterator<Vector>() {
079                                        int i = 0;
080                                        @Override
081                                        public void remove() {
082                                                throw new UnsupportedOperationException();
083                                        }
084                                        
085                                        @Override
086                                        public Vector next() {
087                                                return new DenseVector(DenseMatrix.this.values[i++]);
088                                        }
089                                        
090                                        @Override
091                                        public boolean hasNext() {
092                                                return i < DenseMatrix.this.values.length;
093                                        }
094                                };
095                        }
096                };
097        }
098        
099        @Override
100        public int rowCount() {
101                return values.length;
102        }
103
104        @Override
105        public int used() {
106                // TODO Auto-generated method stub
107                throw null;
108        }
109
110        @Override
111        public double[][] unwrap() {
112                return values;
113        }
114
115        /**
116         * Fill with a constant
117         * 
118         * @param constant
119         */
120        public void fill(double constant) {
121                for (final double[] row : values)
122                        Arrays.fill(row, constant);
123        }
124
125        /**
126         * Inline multiplication by a constant
127         * 
128         * @param d
129         */
130        public void applyMultiplication(double d) {
131                Util.times(values, d);
132        }
133
134        @Override
135        public Matrix newInstance(int rows, int cols) {
136                return new DenseMatrix(rows, cols);
137        }
138
139}