View Javadoc

1   package org.textensor.stochdiff.numeric.math;
2   
3   import org.textensor.report.E;
4   
5   
6   public final class Column {
7   
8       int n;
9       double[] dat;
10  
11      public Column(int n0) {
12          n = n0;
13          dat = new double[n];
14      }
15  
16      public Column(double[] d) {
17          dat = d;
18          n = d.length;
19      }
20  
21  
22      public Column plus(double d) {
23          double[] ret = new double[n];
24          for (int i = 0; i < n; i++) {
25              ret[i] = dat[i] + d;
26          }
27          return new Column(ret);
28      }
29  
30      public Column times(double d) {
31          double[] ret = new double[n];
32          for (int i = 0; i < n; i++) {
33              ret[i] = dat[i] * d;
34          }
35          return new Column(ret);
36      }
37  
38  
39      public void incrementBy(double d) {
40          for (int i = 0; i < n; i++) {
41              dat[i] += d;
42          }
43      }
44  
45  
46      public void incrementBy(double[] r) {
47          for (int i = 0; i < n; i++) {
48              dat[i] += r[i];
49          }
50      }
51  
52      public void incrementBy(Column v) {
53          for (int i = 0; i < n; i++) {
54              dat[i] += v.dat[i];
55          }
56      }
57  
58      public void multiuplyBy(double d) {
59          for (int i = 0; i < n; i++) {
60              dat[i] *= d;
61          }
62      }
63  
64      public double[] getData() {
65          return dat;
66      }
67  
68      public int size() {
69          return n;
70      }
71  
72      public double avgAbs() {
73          double r = 0.;
74          for (int i = 0; i < n; i++) {
75              r += Math.abs(dat[i]);
76          }
77          r /= n;
78          return r;
79      }
80  
81  
82      public void decrementBy(Column v) {
83          for (int i = 0; i < n; i++) {
84              dat[i] -= v.dat[i];
85          }
86      }
87  
88      public Column copy() {
89          double[] d = new double[n];
90          for (int i = 0; i < n; i++) {
91              d[i] = dat[i];
92          }
93          return new Column(d);
94      }
95  
96      public void increment(int i, double d) {
97          dat[i] += d;
98      }
99  
100     public Column plus(Column vdc) {
101         Column ret = copy();
102         ret.incrementBy(vdc);
103         return ret;
104     }
105 
106     public void print() {
107         StringBuffer sb = new StringBuffer();
108         sb.append("(");
109         for (int i = 0; i < n; i++) {
110             sb.append("" + dat[i]);
111             if (i < n-1) {
112                 sb.append(", ");
113             }
114         }
115         sb.append(")");
116         E.info("column: " + sb.toString());
117     }
118 
119     public void positivize() {
120         for (int i = 0; i < n; i++) {
121             if (dat[i] < 0.) {
122                 dat[i] = 0.;
123             }
124         }
125 
126     }
127 
128 
129     public void writeTo(double[] dout) {
130         if (dat == dout) {
131             // warn?
132         } else {
133             for (int i = 0; i < n; i++) {
134                 dout[i] = dat[i];
135             }
136         }
137     }
138 
139 
140 }