001: package uk.org.ponder.doubleutil;
002:
003: import uk.org.ponder.util.Constants;
004:
005: public class DoubleArrayUtil {
006:
007: public static double[] select(double[] from, int[] indices) {
008: double[] togo = new double[indices.length];
009: for (int i = 0; i < indices.length; ++i) {
010: togo[i] = from[indices[i]];
011: }
012: return togo;
013: }
014:
015: public static double[] getEmptyArray(int size) {
016: double[] togo = new double[size];
017: for (int i = 0; i < size; ++i) {
018: togo[i] = Constants.MissingValue;
019: }
020: return togo;
021: }
022:
023: public static double[] resize(double[] toexpand, int newsize) {
024: double[] togo = new double[newsize];
025: System.arraycopy(toexpand, 0, togo, 0, toexpand.length);
026: for (int i = toexpand.length; i < newsize; ++i) {
027: togo[i] = Constants.MissingValue;
028: }
029: return togo;
030: }
031:
032: public static boolean equals(double[] array1, double[] array2) {
033: if (array1 == null && array2 == null)
034: return true;
035: if (array1 == null && array2 != null || array1 != null
036: && array2 == null)
037: return false;
038: if (array1.length != array2.length)
039: return false;
040: for (int i = 0; i < array1.length; ++i) {
041: if (array1[i] != array2[i])
042: return false;
043: }
044: return true;
045: }
046:
047: public static void copy(double[] target, double[] source) {
048: System.arraycopy(source, 0, target, 0, source.length);
049: }
050:
051: public static double[] copy(double[] tocopy) {
052: double[] togo = new double[tocopy.length];
053: copy(togo, tocopy);
054: return togo;
055: }
056:
057: public static double[] fill(double[] tofill, double value) {
058: for (int i = 0; i < tofill.length; ++i) {
059: tofill[i] = value;
060: }
061: return tofill;
062: }
063:
064: public static void bounds(double[] toscan, double[] bounds) {
065: double min = bounds[0];
066: double max = bounds[1];
067: for (int i = 0; i < toscan.length; ++i) {
068: double this num = toscan[i];
069: if (this num != Constants.MissingValue) {
070: if (this num < min)
071: min = this num;
072: if (this num > max)
073: max = this num;
074: }
075: }
076: bounds[0] = min;
077: bounds[1] = max;
078: }
079:
080: public static double sum(double[] tosum) {
081: double total = 0;
082: for (int i = tosum.length - 1; i >= 0; --i) {
083: total += tosum[i];
084: }
085: return total;
086: }
087:
088: public static double dotprod(double[] vec1, double[] vec2) {
089: double total = 0;
090: for (int i = vec1.length - 1; i >= 0; --i) {
091: total += vec1[i] * vec2[i];
092: }
093: return total;
094: }
095:
096: public static void multiplyhorr(double[] target, double[][] A,
097: double[] x) {
098: //double[] togo = new double[A.length];
099: for (int i = 0; i < A.length; ++i) {
100: target[i] = dotprod(A[i], x);
101: }
102: //return togo;
103: }
104:
105: public static double[] addInto(double[] target, double[] operand) {
106: for (int i = 0; i < target.length; ++i) {
107: target[i] += operand[i];
108: }
109: return target;
110: }
111:
112: public static double[] scale(double[] toscale, double factor) {
113: for (int i = 0; i < toscale.length; ++i) {
114: toscale[i] *= factor;
115: }
116: return toscale;
117: }
118:
119: public static double[] addScale(double[] target, double factor,
120: double[] operand) {
121: for (int i = 0; i < target.length; ++i) {
122: target[i] += factor * operand[i];
123: }
124: return target;
125: }
126:
127: private static int shift(double v) {
128: int shift = 0;
129:
130: double power = 1;
131: double abs = v < 0 ? -v : v;
132: while (abs >= power) {
133: power *= 10;
134: shift++;
135: }
136: while (abs < (power *= .1))
137: shift--;
138:
139: return shift;
140: }
141:
142: public static void append(StringBuffer buffer, double d) {
143: if (d == 0)
144: buffer.append('0');
145: else
146: append(buffer, d, 15 - shift(d));
147: }
148:
149: public static void append(StringBuffer buffer, double d, int digits) {
150: boolean negative = d < 0;
151: if (negative)
152: d = -d;
153:
154: if (d == Double.POSITIVE_INFINITY)
155: throw new IllegalArgumentException(
156: "trying to print infinity");
157:
158: double lowest = 1;
159: while (digits-- > 0)
160: lowest *= .1;
161:
162: d += .5 * lowest;
163:
164: if (d < lowest) {
165: buffer.append(0);
166: return;
167: }
168:
169: if (negative)
170: buffer.append('-');
171:
172: double power = 1;
173: while (power <= d)
174: power *= 10;
175:
176: while (d >= lowest || power > 1) {
177: if (power == 1)
178: buffer.append('.');
179: power *= .1;
180: int digit = (int) (d / power);
181: buffer.append((char) ('0' + digit));
182: d -= digit * power;
183: }
184: }
185:
186: /**
187: * @param buffer
188: * @param ds
189: */
190: public static StringBuffer append(StringBuffer buffer, double[] arr) {
191: for (int i = 0; i < arr.length; i++) {
192: if (i != 0)
193: buffer.append(' ');
194: append(buffer, arr[i]);
195: }
196: return buffer;
197: }
198:
199: public String toString(double[] arr) {
200: return append(new StringBuffer(), arr).toString();
201: }
202: }
|