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