1 package baseCode.dataStructure.matrix;
2
3 import cern.colt.function.DoubleFunction;
4 import cern.colt.list.DoubleArrayList;
5 import cern.colt.list.IntArrayList;
6 import cern.colt.matrix.DoubleMatrix1D;
7 import cern.colt.matrix.DoubleMatrix2D;
8 import cern.colt.matrix.impl.DenseDoubleMatrix2D;
9
10 /***
11 * A row-compressed 1D matrix. The only deviation from the contract of DoubleMatrix1D is in apply(), which only operates
12 * on the non-empty elements. This implementation has a highly optimized dot product computer. If you need to compute
13 * the dot product of a RCDoubleMatrix1D with another DoubleMatrix1D, call zDotProduct on this, not on the other. This
14 * is because getQuick() and setQuick() are not very fast for this.
15 * <hr>
16 * <p>
17 * Copyright (c) 2004 Columbia University
18 *
19 * @author pavlidis
20 * @version $Id: RCDoubleMatrix1D.java,v 1.14 2005/01/05 02:01:02 pavlidis Exp $
21 */
22 public class RCDoubleMatrix1D extends DoubleMatrix1D {
23
24 protected IntArrayList indexes;
25 protected DoubleArrayList values;
26
27 /***
28 * @param values
29 */
30 public RCDoubleMatrix1D( double[] values ) {
31 this( values.length );
32 assign( values );
33 }
34
35 /***
36 * @param length
37 */
38 public RCDoubleMatrix1D( int length ) {
39 setUp( length );
40 this.indexes = new IntArrayList( length );
41 this.values = new DoubleArrayList( length );
42 }
43
44 /***
45 * @param indexes These MUST be in sorted order.
46 * @param values These MuST be in the same order as the indexes, meaning that indexes[0] is the column for values[0].
47 */
48 public RCDoubleMatrix1D( IntArrayList indexes, DoubleArrayList values ) {
49 int k = indexes.size();
50 int s = 0;
51 if ( k > 0 ) {
52 s = indexes.get( k - 1 ) + 1;
53 }
54
55 setUp( s );
56 this.indexes = indexes;
57 this.values = values;
58 }
59
60
61
62
63
64
65 public double getQuick( int index ) {
66
67 int location = indexes.binarySearch( index );
68
69 if ( location >= 0 ) {
70 return values.get( location );
71 }
72 return 0.0;
73 }
74
75
76
77
78
79
80 public DoubleMatrix1D like( int s ) {
81 return new RCDoubleMatrix1D( s );
82 }
83
84
85
86
87
88
89 public DoubleMatrix2D like2D( int rows, int columns ) {
90 return new DenseDoubleMatrix2D( rows, columns );
91 }
92
93
94
95
96
97
98
99
100 public void setQuick( int column, double value ) {
101 int location = indexes.binarySearch( column );
102
103 if ( location >= 0 ) {
104 values.set( location, value );
105 } else {
106 location = -location - 1;
107 indexes.beforeInsert( location, column );
108 values.beforeInsert( location, value );
109 }
110 }
111
112
113
114
115
116
117 protected DoubleMatrix1D viewSelectionLike( int[] offsets ) {
118 throw new UnsupportedOperationException();
119 }
120
121 /***
122 * Apply the given function to each element non-zero element in the matrix.
123 *
124 * @param function
125 * @return
126 */
127 public DoubleMatrix1D forEachNonZero(
128 final cern.colt.function.DoubleFunction function ) {
129
130 double[] elements = values.elements();
131 for ( int i = elements.length; --i >= 0; ) {
132 elements[i] = function.apply( elements[i] );
133 }
134 return this;
135
136 }
137
138
139
140
141
142
143 public double zDotProduct( DoubleMatrix1D y ) {
144
145 int[] idx = indexes.elements();
146 double[] el = values.elements();
147 double[] other = y.toArray();
148 double returnVal = 0.0;
149 int otherSize = y.size();
150 for ( int i = idx.length; --i >= 0; ) {
151 int index = idx[i];
152 if ( index >= otherSize ) continue;
153 returnVal += el[i] * other[index];
154 }
155 return returnVal;
156 }
157
158 /***
159 * WARNING this only even assigns to the non-empty values, for performance reasons. If you need to assign to any
160 * index, you have to use another way.
161 *
162 * @see cern.colt.matrix.DoubleMatrix1D#assign(cern.colt.function.DoubleFunction)
163 */
164 public DoubleMatrix1D assign( DoubleFunction function ) {
165
166 double[] elements = values.elements();
167 for ( int i = elements.length; --i >= 0; ) {
168 elements[i] = function.apply( elements[i] );
169 }
170 return this;
171 }
172
173
174
175
176
177
178 public double zSum() {
179 double sum = 0.0;
180 double[] elements = values.elements();
181 for ( int i = elements.length; --i >= 0; ) {
182 sum += elements[i];
183 }
184 return sum;
185 }
186
187
188
189
190
191
192 public String toString() {
193 return new cern.colt.matrix.doublealgo.Formatter().toString( this );
194 }
195 }