00001
00020 #include "newutil.h"
00021
00022 MyReturnMatrix sqrt(const MyMatrix &mat) {
00023 Real *v_mat = mat.Store();
00024 int n = mat.Storage();
00025 Matrix result(mat.Nrows(),mat.Ncols());
00026 Real v_result[n];
00027
00028 std::transform(v_mat, v_mat+n, v_result, sqrt_double);
00029 result <<v_result;
00030 result.Release();
00031 return result;
00032 }
00033
00034 double log_double(double x) {
00035 return log(x);
00036 }
00037
00038
00039 MyReturnMatrix log(const MyMatrix &mat) {
00040 Real *v_mat = mat.Store();
00041 int n = mat.Storage();
00042 Matrix result(mat.Nrows(),mat.Ncols());
00043 Real v_result[n];
00044
00045 std::transform(v_mat, v_mat+n, v_result, log_double);
00046 result <<v_result;
00047 result.Release();
00048 return result;
00049 }
00050
00051 MyReturnMatrix pow(const ColumnVector &mat, double exp) {
00052 int n=mat.Nrows();
00053 ColumnVector result(n);
00054 int i;
00055
00056 for (i = 0; i < n; i++) {
00057 result[i] = pow(mat[i], exp);
00058 }
00059
00060 result.Release();
00061 return result;
00062 }
00063
00064 MyReturnMatrix pow2_m(const MyMatrix &mat) {
00065 Real *v_mat = mat.Store();
00066 int n = mat.Storage();
00067 Matrix result(mat.Nrows(),mat.Ncols());
00068 Real v_result[n];
00069
00070 std::transform(v_mat, v_mat+n, v_result, pow2_double);
00071 result <<v_result;
00072 result.Release();
00073 return result;
00074 }
00075
00076
00077
00078 Real mean(const ColumnVector &mat) {
00079 int i, n=mat.Nrows();
00080 Real sum;
00081
00082 if (n == 0) {
00083 return 0;
00084 }
00085
00086 for (i = 0, sum = 0; i < n; i++) {
00087 sum += mat[i];
00088 }
00089
00090 return sum/n;
00091 }
00092
00093 Real median(const ColumnVector &mat) {
00094 int n=mat.Nrows();
00095
00096 if ( (n % 2) == 1) {
00097 return mat[n/2];
00098 }
00099 else {
00100 return (mat[n/2-1]+mat[n/2])/2;
00101 }
00102 }
00103
00104 Real median(const RowVector &mat) {
00105 int n=mat.Ncols();
00106
00107 if ( (n % 2) == 1) {
00108 return mat[n/2];
00109 }
00110 else {
00111 return (mat[n/2-1]+mat[n/2])/2;
00112 }
00113 }
00114
00115
00116 static Matrix *mat_sort;
00117
00118 void set_sort_matrix(Matrix *mat) {
00119 mat_sort = mat;
00120 }
00121
00122 bool sort_index_matrix(int i, int j) {
00123 Real val1, val2;
00124
00125 val1 = (*mat_sort)[0][i-1];
00126 val2 = (*mat_sort)[0][j-1];
00127
00128 if (val1 < val2) {
00129 return true;
00130 }
00131 else {
00132 return false;
00133 }
00134 }
00135
00136
00137 MyReturnMatrix myprctile(RowVector &inar, int *perc, int nperc) {
00138 int N = inar.Ncols();
00139
00140 int arindex[N];
00141 RowVector result(nperc);
00142 int i;
00143
00144 range(1, N, arindex);
00145
00146 mat_sort = &inar;
00147 sort(arindex, arindex+N, sort_index_matrix);
00148
00149 for (i = 0; i < nperc; i++) {
00150 int p = perc[i];
00151
00152 if (p <= 100*(0.5/N)) {
00153 result[i] = inar[arindex[0]-1];
00154 }
00155 else if (p >= 100*((N-0.5)/N)) {
00156 result[i] = inar[arindex[N-1]-1];
00157 }
00158 else {
00159 int pos = N*p/100;
00160 result[i] = inar[arindex[pos]-1];
00161 }
00162 }
00163
00164 return result;
00165 }
00166
00167
00168
00169 Real mean_diag(const DiagonalMatrix &mat) {
00170 int i, n=mat.Nrows();
00171 Real sum;
00172
00173 if (n == 0) {
00174 return 0;
00175 }
00176
00177 for (i = 0, sum = 0; i < n; i++) {
00178 sum += mat[i];
00179 }
00180
00181 return sum/n;
00182 }
00183
00184 double pow2(double x) {
00185 return x*x;
00186 }
00187
00188 double pow2_double(double x) {
00189 return pow2(x);
00190 }
00191
00192 double sqrt_double(double x) {
00193 return sqrt(x);
00194 }
00195
00196
00197 void copyRow(queue<Real> num, RowVector &row) {
00198 Real val;
00199 int i=0;
00200
00201 do {
00202 val = num.front();
00203 num.pop();
00204 row[i] = val;
00205 i += 1;
00206 } while (num.size() > 0);
00207 }
00208
00209 MyReturnMatrix DotVectors(ColumnVector &A, const ColumnVector &B) {
00210 int N = A.Nrows();
00211 ColumnVector col(N);
00212 int i;
00213
00214 for (i = 0; i < N; i++) {
00215 col[i] = A[i]*B[i];
00216 }
00217
00218 col.Release();
00219 return col;
00220 }
00221
00222 MyReturnMatrix DivVectors(ColumnVector &A, const ColumnVector &B) {
00223 int N = A.Nrows();
00224 ColumnVector col(N);
00225 int i;
00226
00227 for (i = 0; i < N; i++) {
00228 col[i] = A[i]/B[i];
00229 }
00230
00231 col.Release();
00232 return col;
00233 }
00234
00235
00236 void copyColumn(DiagonalMatrix diag, ColumnVector &row) {
00237 int n = diag.Nrows();
00238 int i;
00239
00240 for (i = 0; i < n; i++) {
00241 row[i] = diag[i];
00242 }
00243 }
00244
00245 void copyFromColumn(ColumnVector &row, DiagonalMatrix &diag) {
00246 int n = row.Nrows();
00247 int i;
00248
00249 for (i = 0; i < n; i++) {
00250 diag[i] = row[i];
00251 }
00252 }
00253
00254 Real min_positive(queue<Real> num) {
00255 Real result, val;
00256 int init=1;
00257
00258 result = -1;
00259
00260 do {
00261 val = num.front();
00262 num.pop();
00263
00264 if (val > 0 && (val < result || init) ) {
00265 result = val;
00266 init = 0;
00267 }
00268
00269 } while (num.size() > 0);
00270
00271 if (init == 1) {
00272 throw string("Error, min_positive no encontró valor != 0");
00273 }
00274 else {
00275 return result;
00276 }
00277 }
00278
00279 void range(int min, int max, int *Rang) {
00280 int i, value;
00281 int num=max-min+1;
00282
00283 for (i =0, value=min; i < num; i++) {
00284 Rang[i] = value;
00285 value += 1;
00286 }
00287
00288 }
00289
00290 void getColumns(Matrix &m, int *cols, int num, Matrix &result) {
00291 int i, value;
00292
00293 for (i = 0; i < num; i++) {
00294 value=cols[i];
00295 result.Column(i+1) = m.Column(value);
00296 }
00297 }
00298
00299 double norm(const MyMatrix &mat) {
00300 DiagonalMatrix D;
00301
00302 SVD(mat,D);
00303 return D.Maximum();
00304 }
00305
00306 MyReturnMatrix eye(int N) {
00307 MyMatrix A(N,N);
00308 IdentityMatrix I(N);
00309 A << I;
00310 A.Release();
00311 return A;
00312 }
00313
00314 MyReturnMatrix pow2(ColumnVector &weights) {
00315 int N = weights.Nrows();
00316 ColumnVector col(N);
00317 int i;
00318
00319 for (i = 0; i < N; i++) {
00320 col[i] = pow2(weights[i]);
00321 }
00322
00323 col.Release();
00324 return col;
00325 }
00326
00327 MyReturnMatrix pow2(RowVector &weights) {
00328 int N = weights.Nrows();
00329 ColumnVector col(N);
00330 int i;
00331
00332 for (i = 0; i < N; i++) {
00333 col[i] = pow2(weights[i]);
00334 }
00335
00336 col.Release();
00337 return col;
00338 }
00339
00340
00341 Real min(ColumnVector &mat) {
00342 return mat.Minimum();
00343 }
00344
00345 Real max(ColumnVector &mat) {
00346 return mat.Maximum();
00347 }
00348