00001
00002 #include <stdio.h>
00003 #include <string.h>
00004 #include <stdlib.h>
00005 #include <unistd.h>
00006 #include <math.h>
00007
00008 #include <it/fastica.h>
00009
00010 #include <it/math.h>
00011 #include <it/source.h>
00012 #include <it/random.h>
00013 #include <it/linalg.h>
00014 #include <it/cplx.h>
00015 #include <it/vec.h>
00016 #include <it/mat.h>
00017 #include <it/io.h>
00018
00019
00020 static double
00021 min (double x, double y)
00022 {
00023 if (x >= y)
00024 return x;
00025 else
00026 return y;
00027 }
00028
00029 static int
00030 vec_is_negative (vec v)
00031 {
00032 int cmpt, i;
00033 cmpt = 0;
00034 for (i = 0; i < vec_length (v); i++)
00035 {
00036 if (v[i] < 0)
00037 cmpt++;
00038 }
00039
00040 return cmpt;
00041 }
00042
00043
00044 static vec
00045 remmean (mat vectors, mat newVectors)
00046 {
00047 int i, j;
00048 int m = mat_height (vectors);
00049 int n = mat_width (vectors);
00050 mat mat_int = mat_new (m, n);
00051 vec meanValue = vec_new (m);
00052 vec vec_temp_1;
00053
00054 for (i = 0; i < m; i++)
00055 {
00056 vec_temp_1 = mat_get_row (vectors, i);
00057 meanValue[i] = vec_mean (vec_temp_1);
00058 vec_delete (vec_temp_1);
00059 }
00060
00061 for (j = 0; j < n; j++)
00062 mat_set_col (mat_int, j, meanValue);
00063 mat_copy (newVectors, vectors);
00064 mat_sub (newVectors, mat_int);
00065
00066 mat_delete (mat_int);
00067
00068
00069 return meanValue;
00070
00071 }
00072
00073 static mat
00074 selcol (mat oldMatrix, ivec maskVector)
00075 {
00076
00077
00078
00079 int i;
00080 int j = 0;
00081 mat newMatrix;
00082 vec v;
00083
00084 it_assert( ivec_length( maskVector ) == mat_width( oldMatrix ), "The mask vector and matrix are of uncompatible size.");
00085
00086
00087
00088
00089
00090
00091
00092 newMatrix = mat_new_zeros (mat_height (oldMatrix), ivec_sum (maskVector));
00093 for (i = 0; i < ivec_length (maskVector); i++)
00094 {
00095 if (maskVector[i] == 1)
00096 {
00097 v = mat_get_col (oldMatrix, i);
00098 mat_set_col (newMatrix, j, v);
00099 j++;
00100 vec_delete (v);
00101 }
00102 }
00103
00104 return newMatrix;
00105 }
00106
00107 static mat
00108 mat_tanh (mat m)
00109 {
00110 int i, j;
00111 mat res = mat_new (mat_height (m), mat_width (m));
00112 for (i = 0; i < mat_height (m); i++)
00113 for (j = 0; j < mat_width (m); j++)
00114 res[i][j] = tanh (m[i][j]);
00115
00116
00117 return res;
00118 }
00119
00120 static vec
00121 vec_tanh (vec v)
00122 {
00123 int i;
00124 vec res = vec_new (vec_length (v));
00125 for (i = 0; i < vec_length (v); i++)
00126 res[i] = tanh (v[i]);
00127
00128
00129 return res;
00130 }
00131
00132 static ivec
00133 getSamples (int max, double percentage)
00134 {
00135 int i;
00136 double p;
00137 ivec mask = ivec_new (max);
00138 for (i = 0; i < max; i++)
00139 {
00140 p = mt19937_rand_real1 ();
00141 mask[i] = (p < percentage) ? 1 : 0;
00142 }
00143
00144 return mask;
00145 }
00146
00147 static mat
00148 mat_pow_one_by_two (mat m)
00149 {
00150 vec d;
00151 mat D;
00152 mat P_inv;
00153 mat Dtmp, Dtmp2;
00154 mat P = mat_new (mat_height (m), mat_width (m));
00155
00156 d = mat_eig_sym (m, P);
00157
00158 vec_sqrt (d);
00159
00160 P_inv = mat_new_inv (P);
00161
00162 D = mat_new_diag (d);
00163
00164 Dtmp = mat_new_mul (P, D);
00165 Dtmp2 = mat_new_mul (Dtmp, P_inv);
00166
00167 vec_delete (d);
00168 mat_delete (P);
00169 mat_delete (D);
00170 mat_delete (Dtmp);
00171 mat_delete (P_inv);
00172
00173 return Dtmp2;
00174 }
00175
00176 static int
00177 whitenv (mat vectors, mat E, mat D, int verbose, mat newVectors,
00178 mat whiteningMatrix, mat dewhiteningMatrix)
00179 {
00180
00181 int sum_diag;
00182 mat D_sqrt, D_sqrt_inv, covarianceMatrix, mat_temp_1;
00183 vec vec_temp_1;
00184
00185 vec_temp_1 = mat_get_diag (D);
00186
00187 sum_diag = vec_is_negative (vec_temp_1);
00188 vec_delete (vec_temp_1);
00189
00190 it_assert( sum_diag, "Negative eigenvalues, reduce the number of dimensions in the data." );
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214 D_sqrt = mat_clone( D );
00215 mat_elem_sqrt (D_sqrt);
00216 D_sqrt_inv = mat_clone (D_sqrt);
00217 mat_inv (D_sqrt_inv);
00218
00219
00220
00221 mat_mul_transpose_right (whiteningMatrix, D_sqrt_inv, E);
00222 mat_mul (dewhiteningMatrix, E, D_sqrt);
00223
00224
00225
00226
00227 if (verbose)
00228 fprintf (stderr, "Whitening...\n");
00229
00230 mat_mul (newVectors, whiteningMatrix, vectors);
00231
00232
00233
00234 if (verbose)
00235 {
00236 mat temp = mat_new_eye (mat_height (newVectors));
00237
00238 covarianceMatrix = mat_cov (newVectors);
00239
00240 mat_sub (covarianceMatrix, temp);
00241 mat_temp_1 = mat_clone( covarianceMatrix );
00242 mat_elem_abs (mat_temp_1);
00243 fprintf (stderr, "Check: covariance differs from identity by [ %f ].\n",
00244 mat_max (mat_temp_1));
00245 mat_delete (mat_temp_1);
00246 mat_delete (covarianceMatrix);
00247 mat_delete (temp);
00248 }
00249
00250 mat_delete (D_sqrt);
00251 mat_delete (D_sqrt_inv);
00252
00253
00254 return 0;
00255
00256
00257 }
00258
00259 static ivec
00260 PCAmat (mat vectors, int firstEig, int lastEig, int verbose, mat D, mat E)
00261 {
00262
00263 int i, nb_eigenvalues;
00264 int oldDimension = mat_height (vectors);
00265 int maxLastEig = 0;
00266
00267 double rankTolerance, lowerLimitValue, higherLimitValue, sumAll, sumUsed,
00268 retained;
00269 double sum_diag = 0;
00270
00271 mat covarianceMatrix;
00272 vec eigenvalues, diag_D;
00273 ivec lowerColumns, higherColumns, selectedColumns;
00274
00275
00276 if ((lastEig < 1) || (lastEig > oldDimension))
00277 {
00278 fprintf (stderr, "Illegal value [ %d ] for parameter 'lastEig'\n", lastEig);
00279 }
00280
00281 if ((firstEig < 1) || (firstEig > lastEig))
00282 {
00283 fprintf (stderr, "Illegal value [ %d ] for parameter 'firstEig'\n", firstEig);
00284 }
00285
00286
00287
00288 if (verbose)
00289 fprintf (stderr, "Calculating covariance...\n");
00290
00291 covarianceMatrix = mat_cov (vectors);
00292
00293
00294
00295 eigenvalues = mat_eig_sym (covarianceMatrix, E);
00296
00297
00298 diag_D = vec_clone (eigenvalues);
00299 mat_diag (D, eigenvalues);
00300 nb_eigenvalues = vec_length (eigenvalues);
00301
00302
00303
00304
00305
00306 rankTolerance = 1e-7;
00307 for (i = 0; i < vec_length (eigenvalues); i++)
00308 if (eigenvalues[i] > rankTolerance)
00309 maxLastEig++;
00310
00311
00312 it_assert( maxLastEig, "Covariance matrix eigenvalues too small. Try resizing the data matrix." );
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329 vec_sort (eigenvalues);
00330 vec_reverse (eigenvalues);
00331
00332
00333 if (lastEig > maxLastEig)
00334 {
00335 lastEig = maxLastEig;
00336 if (verbose)
00337 fprintf(stderr, "Dimension reduced to %d due to the singularity of covariance matrix\n",lastEig - firstEig + 1);
00338 }
00339 else
00340 {
00341 if (verbose)
00342 {
00343 if (oldDimension == (lastEig - firstEig + 1))
00344 fprintf (stderr, "Dimension not reduced.\n");
00345 else
00346 fprintf (stderr, "Reducing dimension...\n");
00347 }
00348 }
00349
00350
00351 if (lastEig < oldDimension)
00352 lowerLimitValue = (eigenvalues[lastEig - 1] + eigenvalues[lastEig]) / 2;
00353 else
00354 lowerLimitValue = eigenvalues[oldDimension - 1] - 1;
00355
00356 lowerColumns = ivec_new_zeros (nb_eigenvalues);
00357 for (i = 0; i < nb_eigenvalues; i++)
00358 lowerColumns[i] = (diag_D[i] > lowerLimitValue) ? 1 : 0;
00359
00360
00361
00362 if (firstEig > 1)
00363 higherLimitValue =
00364 (eigenvalues[firstEig - 2] + eigenvalues[firstEig - 1]) / 2;
00365 else
00366 higherLimitValue = eigenvalues[0] + 1;
00367
00368 higherColumns = ivec_new_zeros (nb_eigenvalues);
00369 for (i = 0; i < nb_eigenvalues; i++)
00370 higherColumns[i] = (diag_D[i] < higherLimitValue) ? 1 : 0;
00371
00372
00373
00374 selectedColumns = ivec_new_zeros (nb_eigenvalues);
00375 for (i = 0; i < nb_eigenvalues; i++)
00376 selectedColumns[i] = (lowerColumns[i]) & (higherColumns[i]);
00377
00378
00379
00380 if (verbose)
00381 fprintf (stderr, "Selected [ %d ] dimensions.\n", ivec_sum (selectedColumns));
00382
00383 it_assert( ivec_sum(selectedColumns)==lastEig-firstEig+1, "Selected a wrong number of dimensions" );
00384
00385
00386
00387
00388
00389
00390 if (verbose)
00391 {
00392 fprintf (stderr, "Smallest remaining (non-zero) eigenvalue [ %f ]\n", eigenvalues[lastEig - 1]);
00393 fprintf (stderr, "Largest remaining (non-zero) eigenvalue [ %f ]\n", eigenvalues[firstEig - 1]);
00394 for (i = 0; i < nb_eigenvalues; i++)
00395 if (selectedColumns[i] == 0)
00396 sum_diag += diag_D[i];
00397 fprintf (stderr, "Sum of removed eigenvalues [ %f ]\n", sum_diag);
00398
00399 }
00400
00401
00402
00403 if (verbose)
00404 {
00405 sumAll = vec_sum (eigenvalues);
00406 sumUsed = mat_diag_sum (D);
00407 retained = (sumUsed / sumAll) * 100;
00408 fprintf (stderr, "[ %f ] %% of (non-zero) eigenvalues retained.\n", retained);
00409 }
00410
00411
00412
00413 mat_delete (covarianceMatrix);
00414 vec_delete (eigenvalues);
00415 vec_delete (diag_D);
00416 ivec_delete (lowerColumns);
00417 ivec_delete (higherColumns);
00418
00419 return selectedColumns;
00420 }
00421
00422 static int
00423 fpICA (mat X, mat whiteningMatrix, mat dewhiteningMatrix, char *approach,
00424 int numOfIC, char *g, char *finetune, int a1, int a2, int myy,
00425 int stabilization, double epsilon, int maxNumIterations,
00426 int maxFinetune, char *initState, mat guess, double sampleSize,
00427 int verbose, mat A, mat W)
00428 {
00429
00430 int i, j, round;
00431 int vectorSize = mat_height (X);
00432 int numSamples = mat_width (X);
00433 int failureLimit;
00434 int stabilizationEnabled = 0;
00435 int gOrig = 0;
00436 int gFine = 0;
00437 int finetuningEnabled;
00438 int usedNlinearity;
00439 int stroke;
00440 int notFine;
00441 int longe;
00442 int numFailures, endFinetuning;
00443 int approachMode = 2;
00444 int initialStateMode = 0;
00445 int gabba;
00446
00447 double myyK;
00448 double myyOrig;
00449 double minAbsCos, minAbsCos2;
00450 double Beta_double;
00451
00452
00453 mat B;
00454 mat BOld, BOld2;
00455 mat mat_temp_1, mat_temp_2, mat_temp_3;
00456 mat Y, Gpow3, D, Xsub, Ysub, hypTan, U, Usquared, ex, gauss, dGauss, Gskew;
00457
00458 vec vec_temp_1, vec_temp_2, Beta;
00459 vec w, wOld, wOld2, w_minus_wOld, w_plus_wOld, w_minus_wOld2, w_plus_wOld2;
00460 vec EXGpow3, vec_hypTan, u, u2, vec_ex, vec_gauss, vec_dGauss, EXGskew;
00461
00462 ivec mask;
00463
00464
00465
00466
00467 if (!strncmp (approach, "symm", 4) || !strncmp (approach, "defl", 4))
00468 {
00469 if (!strncmp (approach, "symm", 4))
00470 approachMode = 1;
00471 if (!strncmp (approach, "defl", 4))
00472 approachMode = 2;
00473
00474 if (verbose)
00475 fprintf (stderr, "Used approach [ %s ].\n", approach);
00476 }
00477 else
00478 {
00479 if(verbose)
00480 fprintf (stderr, "Illegal value [ %s ] for parameter: 'approach' (forcing deflation)\n", approach);
00481 approachMode = 2;
00482 }
00483
00484
00485
00486
00487 it_assert( vectorSize >= numOfIC, "Must have numOfIC >= dimension!" );
00488
00489
00490
00491
00492
00493
00494
00495
00496 if (sampleSize > 1)
00497 {
00498 sampleSize = 1;
00499 if (verbose)
00500 fprintf (stderr, "Warning: Setting 'sampleSize' to 1.\n");
00501 }
00502 else if (sampleSize < 1)
00503 {
00504 if ((sampleSize * numSamples) < 1000)
00505 {
00506 sampleSize = min (1000 / numSamples, 1);
00507
00508 if (verbose)
00509 fprintf(stderr, "Warning: Setting 'sampleSize' to %0.3f (%f samples).\n", sampleSize, floor (sampleSize * numSamples));
00510 }
00511 }
00512
00513 if (verbose && (sampleSize < 1))
00514 fprintf(stderr, "Using about %2.2f%% of the samples in random order in every step.\n", sampleSize * 100);
00515
00516
00517
00518
00519
00520 if (verbose)
00521 fprintf (stderr, "Used nonlinearity [ %s ].\n", g);
00522
00523 if (!strncmp (g, "pow3", 4))
00524 gOrig = 10;
00525 else if (!strncmp (g, "tanh", 4))
00526 gOrig = 20;
00527 else if (!strncmp (g, "gaus", 4))
00528 gOrig = 30;
00529 else if (!strncmp (g, "skew", 4))
00530 gOrig = 40;
00531 else
00532 {
00533 gOrig = 30;
00534 fprintf (stderr, "Warning: Illegal value [ %s ] for parameter: 'g' (forcing 'gaus')\n", g);
00535 }
00536
00537 if (sampleSize != 1)
00538 gOrig = gOrig + 2;
00539 if (myy != 1)
00540 gOrig++;
00541
00542 finetuningEnabled = 1;
00543 if (!strncmp (finetune, "pow3", 4))
00544 gFine = 10 + 1;
00545 else if (!strncmp (finetune, "tanh", 4))
00546 gFine = 20 + 1;
00547 else if (!strncmp (finetune, "gaus", 4))
00548 gFine = 30 + 1;
00549 else if (!strncmp (finetune, "skew", 4))
00550 gFine = 40 + 1;
00551 else if (!strncmp (finetune, "off", 3))
00552 {
00553 if (myy != 1)
00554 gFine = gOrig;
00555 else
00556 gFine = gOrig + 1;
00557 finetuningEnabled = 0;
00558 }
00559 else
00560 {
00561 gFine = 30 + 1;
00562 if ( verbose )
00563 fprintf (stderr, "Warning: Illegal value [ %s ] for parameter: 'finetune' (forcing 'gaus')\n", finetune);
00564 }
00565
00566 if (verbose && finetuningEnabled)
00567 fprintf (stderr, "Finetuning enabled (nonlinearity: [ %s ]).\n", finetune);
00568
00569 if (stabilization)
00570 stabilizationEnabled = 1;
00571 else if (!stabilization)
00572 {
00573 if (myy != 1)
00574 stabilizationEnabled = 1;
00575 else
00576 stabilizationEnabled = 0;
00577 }
00578 else
00579 {
00580 stabilizationEnabled = 1;
00581 fprintf (stderr, "Warning: Illegal value [ %d ] for parameter: 'stabilization' (forcing stabilization)\n", stabilization);
00582 }
00583
00584 if (verbose && stabilizationEnabled)
00585 fprintf (stderr, "Using stabilized algorithm.\n");
00586
00587
00588
00589
00590
00591 myyOrig = myy;
00592
00593 myyK = 0.01;
00594
00595 failureLimit = 5;
00596
00597
00598 usedNlinearity = gOrig;
00599 stroke = 0;
00600 notFine = 1;
00601 longe = 0;
00602
00603
00604
00605 if (!strncmp (initState, "rand", 4))
00606 initialStateMode = 0;
00607 else if (!strncmp (initState, "guess", 4))
00608 {
00609 if ((mat_height (guess) != mat_width (whiteningMatrix)) || (mat_width (guess) != numOfIC))
00610 {
00611 initialStateMode = 0;
00612 if (verbose)
00613 fprintf(stderr, "Warning: size of initial guess is incorrect. Using random initial guess.\n");
00614 }
00615 else
00616 {
00617 initialStateMode = 1;
00618 if (verbose)
00619 fprintf (stderr, "Using initial guess.\n");
00620 }
00621
00622 }
00623 else
00624 fprintf (stderr, "Illegal value [ %s ] for parameter: 'initState'\n", initState);
00625
00626
00627 if (verbose)
00628 fprintf (stderr, "Starting ICA calculation...\n");
00629
00630
00631
00632
00633 if (approachMode)
00634 {
00635
00636 usedNlinearity = gOrig;
00637 stroke = 0;
00638 notFine = 1;
00639 longe = 0;
00640 B = mat_new_zeros (vectorSize, numOfIC);
00641 BOld = mat_new_zeros (vectorSize, numOfIC);
00642 BOld2 = mat_new_zeros (vectorSize, numOfIC);
00643
00644 mat_zeros (A);
00645 mat_zeros (W);
00646 if (!initialStateMode)
00647 {
00648
00649 for (i = 0; i < vectorSize; i++)
00650 for (j = 0; j < numOfIC; j++)
00651 B[i][j] = it_randn ();
00652 mat_gs (B);
00653 }
00654 else if (initialStateMode)
00655 {
00656
00657
00658 mat_mul (B, whiteningMatrix, guess);
00659 }
00660
00661
00662
00663
00664
00665
00666 for (round = 1; round <= maxNumIterations + 1; round++)
00667 {
00668 if (round == (maxNumIterations + 1))
00669 {
00670 fprintf (stderr, "FastICA: No convergence after %d steps\n", maxNumIterations);
00671 if (B != NULL)
00672 {
00673
00674
00675
00676 mat_temp_1 = mat_new (numOfIC, numOfIC);
00677 mat_mul_transpose_left (mat_temp_1, B, B);
00678 mat_inv (mat_temp_1);
00679 mat_temp_2 = mat_pow_one_by_two (mat_temp_1);
00680 mat_temp_3 = mat_clone (B);
00681 mat_mul (B, mat_temp_3, mat_temp_2);
00682
00683 mat_delete (mat_temp_1);
00684 mat_delete (mat_temp_2);
00685 mat_delete (mat_temp_3);
00686
00687
00688 mat_mul_transpose_left (W, B, whiteningMatrix);
00689
00690
00691 mat_mul (A, dewhiteningMatrix, B);
00692 }
00693 else
00694 {
00695
00696
00697 mat_void (W);
00698 mat_void (A);
00699 }
00700
00701 return 1;
00702 }
00703
00704
00705
00706 mat_temp_1 = mat_new (numOfIC, numOfIC);
00707 mat_mul_transpose_left (mat_temp_1, B, B);
00708 mat_inv (mat_temp_1);
00709 mat_temp_2 = mat_pow_one_by_two (mat_temp_1);
00710 mat_temp_3 = mat_clone (B);
00711 mat_mul (B, mat_temp_3, mat_temp_2);
00712
00713 mat_delete (mat_temp_1);
00714 mat_delete (mat_temp_2);
00715 mat_delete (mat_temp_3);
00716
00717
00718
00719
00720
00721 mat_temp_1 = mat_new (numOfIC, numOfIC);
00722 mat_mul_transpose_left (mat_temp_1, B, BOld);
00723 vec_temp_1 = mat_get_diag (mat_temp_1);
00724 mat_delete (mat_temp_1);
00725 vec_abs (vec_temp_1);
00726 minAbsCos = vec_min (vec_temp_1);
00727 vec_delete (vec_temp_1);
00728
00729
00730 mat_temp_1 = mat_new (numOfIC, numOfIC);
00731 mat_mul_transpose_left (mat_temp_1, B, BOld2);
00732 vec_temp_1 = mat_get_diag (mat_temp_1);
00733 mat_delete (mat_temp_1);
00734 vec_abs (vec_temp_1);
00735 minAbsCos2 = vec_min (vec_temp_1);
00736 vec_delete (vec_temp_1);
00737
00738 if ((1.0 - minAbsCos) < epsilon)
00739 {
00740 if (finetuningEnabled && notFine)
00741 {
00742 if (verbose)
00743 fprintf (stderr, "Initial convergence, fine-tuning: \n");
00744 notFine = 0;
00745 usedNlinearity = gFine;
00746 myy = myyK * myyOrig;
00747 mat_zeros (BOld);
00748 mat_zeros (BOld2);
00749 }
00750 else
00751 {
00752 if (verbose)
00753 fprintf (stderr, "Convergence after %d steps\n", round);
00754
00755
00756 mat_mul (A, dewhiteningMatrix, B);
00757 break;
00758 }
00759 }
00760
00761 else if (stabilizationEnabled)
00762 {
00763 if (!stroke && ((1 - minAbsCos2) < epsilon))
00764 {
00765 if (verbose)
00766 fprintf (stderr, "Stroke!\n");
00767 stroke = myy;
00768 myy = .5 * myy;
00769 if (usedNlinearity % 2 == 0)
00770 usedNlinearity = usedNlinearity + 1;
00771 }
00772 else if (stroke)
00773 {
00774 myy = stroke;
00775 stroke = 0;
00776 if ((myy==1) && ((usedNlinearity % 2) != 0))
00777 usedNlinearity = usedNlinearity - 1;
00778 }
00779 else if ((longe == 0) && (round > (maxNumIterations / 2)))
00780 {
00781 if (verbose)
00782 fprintf (stderr, "Taking long (reducing step size)\n");
00783 longe = 1;
00784 myy = .5 * myy;
00785 if (usedNlinearity % 2 == 0)
00786 usedNlinearity = usedNlinearity + 1;
00787 }
00788 }
00789
00790 mat_copy (BOld2, BOld);
00791 mat_copy (BOld, B);
00792
00793
00794 if (verbose)
00795 {
00796 if (round == 1)
00797 fprintf (stderr, "Step no. %d\n", round);
00798 else
00799 fprintf (stderr, "Step no. %d, change in value of estimate: %.3f \n", round, 1 - minAbsCos);
00800 }
00801
00802 switch (usedNlinearity)
00803 {
00804 case 10:
00805
00806 mat_temp_1 = mat_clone (B);
00807 mat_temp_2 = mat_new (numSamples, numOfIC);
00808 mat_mul_transpose_left (mat_temp_2, X, B);
00809 mat_temp_3 = mat_clone( mat_temp_2 );
00810 mat_elem_pow (mat_temp_3, 3);
00811
00812 mat_delete (mat_temp_2);
00813
00814 mat_mul (B, X, mat_temp_3);
00815
00816 mat_delete (mat_temp_3);
00817
00818 mat_div_by (B, numSamples);
00819 mat_mul_by (mat_temp_1, 3);
00820 mat_sub (B, mat_temp_1);
00821
00822 mat_delete (mat_temp_1);
00823
00824 break;
00825
00826
00827 case 11:
00828
00829 Y = mat_new (numSamples, numOfIC);
00830 mat_mul_transpose_left (Y, X, B);
00831
00832
00833
00834 Gpow3 = mat_clone( Y );
00835 mat_elem_pow (Gpow3, 3);
00836 mat_temp_1 = mat_clone (Y);
00837 mat_elem_mul (mat_temp_1, Gpow3);
00838 Beta = mat_cols_sum (mat_temp_1);
00839 mat_delete (mat_temp_1);
00840
00841
00842
00843 vec_temp_1 = vec_clone (Beta);
00844 vec_temp_2 = vec_new_set (3 * numSamples, vec_length (Beta));
00845 vec_sub (vec_temp_1, vec_temp_2);
00846 vec_set (vec_temp_2, 1);
00847 vec_div (vec_temp_2, vec_temp_1);
00848
00849 vec_delete (vec_temp_1);
00850
00851 D = mat_new_diag (vec_temp_2);
00852
00853 vec_delete (vec_temp_2);
00854
00855
00856
00857 mat_temp_2 = mat_new (numOfIC, numOfIC);
00858 mat_mul_transpose_left (mat_temp_2, Y, Gpow3);
00859 mat_temp_1 = mat_new_diag (Beta);
00860 mat_sub (mat_temp_2, mat_temp_1);
00861
00862 mat_delete (mat_temp_1);
00863
00864 mat_temp_1 = mat_new_mul (mat_temp_2, D);
00865
00866 mat_delete (mat_temp_2);
00867
00868 mat_temp_2 = mat_new_mul (B, mat_temp_1);
00869
00870 mat_delete (mat_temp_1);
00871
00872 mat_mul_by (mat_temp_2, myy);
00873 mat_add (B, mat_temp_2);
00874
00875 mat_delete (mat_temp_2);
00876
00877 mat_delete (Y);
00878 mat_delete (Gpow3);
00879 vec_delete (Beta);
00880 mat_delete (D);
00881
00882 break;
00883
00884
00885 case 12:
00886
00887 mask = getSamples (numSamples, sampleSize);
00888 Xsub = selcol (X, mask);
00889
00890
00891 mat_temp_1 = mat_new (ivec_sum (mask), numOfIC);
00892 mat_mul_transpose_left (mat_temp_1, Xsub, B);
00893
00894 mat_temp_3 = mat_clone (mat_temp_1);
00895 mat_elem_pow (mat_temp_3, 3);
00896
00897 mat_delete (mat_temp_1);
00898
00899 mat_temp_2 = mat_clone (B);
00900 mat_mul (B, Xsub, mat_temp_3);
00901
00902 mat_delete (mat_temp_3);
00903
00904 mat_div_by (B, ivec_sum (mask));
00905 mat_mul_by (mat_temp_2, 3);
00906 mat_sub (B, mat_temp_2);
00907
00908 mat_delete (mat_temp_2);
00909 ivec_delete (mask);
00910 mat_delete (Xsub);
00911
00912 break;
00913
00914
00915
00916 case 13:
00917
00918
00919 mask = getSamples (numSamples, sampleSize);
00920 Ysub = mat_new (ivec_sum (mask), numOfIC);
00921 mat_temp_1 = selcol (X, mask);
00922 mat_mul_transpose_left (Ysub, mat_temp_1, B);
00923
00924 mat_delete (mat_temp_1);
00925
00926
00927 Gpow3 = mat_clone( Ysub );
00928 mat_elem_pow (Gpow3, 3);
00929
00930
00931 mat_temp_1 = mat_clone (Ysub);
00932 mat_elem_mul (mat_temp_1, Gpow3);
00933 Beta = mat_cols_sum (mat_temp_1);
00934
00935 mat_delete (mat_temp_1);
00936
00937
00938 vec_temp_1 = vec_clone (Beta);
00939 vec_temp_2 = vec_new_set (3 * ivec_sum (mask), numOfIC);
00940 vec_sub (vec_temp_1, vec_temp_2);
00941 vec_set (vec_temp_2, 1);
00942 vec_div (vec_temp_2, vec_temp_1);
00943
00944 vec_delete (vec_temp_1);
00945
00946 D = mat_new_diag (vec_temp_2);
00947
00948 vec_delete (vec_temp_2);
00949
00950
00951
00952 mat_temp_2 = mat_new (numOfIC, numOfIC);
00953 mat_mul_transpose_left (mat_temp_2, Ysub, Gpow3);
00954 mat_temp_1 = mat_new_diag (Beta);
00955 mat_sub (mat_temp_2, mat_temp_1);
00956
00957 mat_delete (mat_temp_1);
00958
00959 mat_temp_1 = mat_new_mul (mat_temp_2, D);
00960
00961 mat_delete (mat_temp_2);
00962
00963 mat_temp_2 = mat_new_mul (B, mat_temp_1);
00964
00965 mat_delete (mat_temp_1);
00966
00967 mat_mul_by (mat_temp_2, myy);
00968 mat_add (B, mat_temp_2);
00969
00970 mat_delete (mat_temp_2);
00971 ivec_delete (mask);
00972 mat_delete (Ysub);
00973 mat_delete (Gpow3);
00974 vec_delete (Beta);
00975 mat_delete (D);
00976
00977 break;
00978
00979
00980
00981 case 20:
00982
00983 mat_temp_1 = mat_new (numSamples, numOfIC);
00984 mat_mul_transpose_left (mat_temp_1, X, B);
00985 mat_mul_by (mat_temp_1, a1);
00986 hypTan = mat_tanh (mat_temp_1);
00987
00988 mat_delete (mat_temp_1);
00989
00990
00991 mat_temp_1 = mat_new_ones (numSamples, numOfIC);
00992 mat_temp_2 = mat_clone (hypTan);
00993 mat_elem_pow (mat_temp_2, 2);
00994 mat_sub (mat_temp_1, mat_temp_2);
00995
00996 mat_delete (mat_temp_2);
00997
00998 vec_temp_1 = mat_cols_sum (mat_temp_1);
00999
01000 mat_delete (mat_temp_1);
01001
01002 mat_temp_1 = mat_new (vectorSize, numOfIC);
01003 for (i = 0; i < vectorSize; i++)
01004 mat_set_row (mat_temp_1, i, vec_temp_1);
01005
01006 vec_delete (vec_temp_1);
01007
01008 mat_elem_mul (mat_temp_1, B);
01009 mat_div_by (mat_temp_1, numSamples);
01010 mat_mul_by (mat_temp_1, a1);
01011 mat_mul (B, X, hypTan);
01012 mat_div_by (B, numSamples);
01013 mat_sub (B, mat_temp_1);
01014
01015 mat_delete (mat_temp_1);
01016 mat_delete (hypTan);
01017
01018 break;
01019
01020 case 21:
01021
01022
01023 Y = mat_new (numSamples, numOfIC);
01024 mat_mul_transpose_left (Y, X, B);
01025
01026
01027
01028 mat_temp_1 = mat_clone (Y);
01029 mat_mul_by (mat_temp_1, a1);
01030 hypTan = mat_tanh (mat_temp_1);
01031
01032 mat_delete (mat_temp_1);
01033
01034
01035 mat_temp_1 = mat_clone (Y);
01036 mat_elem_mul (mat_temp_1, hypTan);
01037 Beta = mat_cols_sum (mat_temp_1);
01038
01039 mat_delete (mat_temp_1);
01040
01041
01042 mat_temp_1 = mat_new_ones (numSamples, numOfIC);
01043 mat_temp_2 = mat_clone (hypTan);
01044 mat_elem_pow (mat_temp_2, 2);
01045 mat_sub (mat_temp_1, mat_temp_2);
01046
01047 mat_delete (mat_temp_2);
01048
01049 vec_temp_1 = mat_cols_sum (mat_temp_1);
01050
01051 mat_delete (mat_temp_1);
01052
01053 vec_temp_2 = vec_clone (Beta);
01054 vec_mul_by (vec_temp_1, a1);
01055 vec_sub (vec_temp_2, vec_temp_1);
01056 vec_set (vec_temp_1, 1);
01057 vec_div (vec_temp_1, vec_temp_2);
01058
01059 vec_delete (vec_temp_2);
01060
01061 D = mat_new_diag (vec_temp_1);
01062
01063 vec_delete (vec_temp_1);
01064
01065
01066 mat_temp_1 = mat_new (numOfIC, numOfIC);
01067 mat_mul_transpose_left (mat_temp_1, Y, hypTan);
01068 mat_temp_2 = mat_new_diag (Beta);
01069 mat_sub (mat_temp_1, mat_temp_2);
01070
01071 mat_delete (mat_temp_2);
01072
01073 mat_temp_2 = mat_new_mul (mat_temp_1, D);
01074
01075 mat_delete (mat_temp_1);
01076
01077 mat_temp_1 = mat_new (vectorSize, numOfIC);
01078 mat_mul (mat_temp_1, B, mat_temp_2);
01079
01080 mat_delete (mat_temp_2);
01081
01082 mat_mul_by (mat_temp_1, myy);
01083 mat_add (B, mat_temp_1);
01084
01085 mat_delete (mat_temp_1);
01086 mat_delete (Y);
01087 mat_delete (hypTan);
01088 vec_delete (Beta);
01089 mat_delete (D);
01090
01091 break;
01092
01093 case 22:
01094
01095 mask = getSamples (numSamples, sampleSize);
01096 Xsub = selcol (X, mask);
01097
01098
01099 mat_temp_1 = mat_new (ivec_sum (mask), numOfIC);
01100 mat_mul_transpose_left (mat_temp_1, Xsub, B);
01101 mat_mul_by (mat_temp_1, a1);
01102 hypTan = mat_tanh (mat_temp_1);
01103
01104 mat_delete (mat_temp_1);
01105
01106
01107
01108
01109 mat_temp_1 = mat_new_ones (ivec_sum (mask), numOfIC);
01110 mat_temp_2 = mat_clone (hypTan);
01111 mat_elem_pow (mat_temp_2, 2);
01112 mat_sub (mat_temp_1, mat_temp_2);
01113
01114 mat_delete (mat_temp_2);
01115
01116 vec_temp_1 = mat_cols_sum (mat_temp_1);
01117
01118 mat_delete (mat_temp_1);
01119
01120 mat_temp_1 = mat_new (vectorSize, numOfIC);
01121 for (i = 0; i < vectorSize; i++)
01122 mat_set_row (mat_temp_1, i, vec_temp_1);
01123
01124 vec_delete (vec_temp_1);
01125
01126 mat_elem_mul (mat_temp_1, B);
01127 mat_div_by (mat_temp_1, ivec_sum (mask));
01128 mat_mul_by (mat_temp_1, a1);
01129 mat_mul (B, Xsub, hypTan);
01130 mat_div_by (B, ivec_sum (mask));
01131 mat_sub (B, mat_temp_1);
01132
01133 mat_delete (mat_temp_1);
01134 ivec_delete (mask);
01135 mat_delete (hypTan);
01136
01137 break;
01138
01139 case 23:
01140
01141
01142 mask = getSamples (numSamples, sampleSize);
01143 Y = mat_new (ivec_sum (mask), numOfIC);
01144 mat_temp_1 = selcol (X, mask);
01145 mat_mul_transpose_left (Y, mat_temp_1, B);
01146
01147 mat_delete (mat_temp_1);
01148
01149
01150 mat_temp_1 = mat_clone (Y);
01151 mat_mul_by (mat_temp_1, a1);
01152 hypTan = mat_tanh (mat_temp_1);
01153
01154 mat_delete (mat_temp_1);
01155
01156
01157 mat_temp_1 = mat_clone (Y);
01158 mat_elem_mul (mat_temp_1, hypTan);
01159 Beta = mat_cols_sum (mat_temp_1);
01160
01161 mat_delete (mat_temp_1);
01162
01163
01164 mat_temp_1 = mat_new_ones (ivec_sum (mask), numOfIC);
01165 mat_temp_2 = mat_clone (hypTan);
01166 mat_elem_pow (mat_temp_2, 2);
01167 mat_sub (mat_temp_1, mat_temp_2);
01168
01169 mat_delete (mat_temp_2);
01170
01171 vec_temp_1 = mat_cols_sum (mat_temp_1);
01172
01173 mat_delete (mat_temp_1);
01174
01175 vec_temp_2 = vec_clone (Beta);
01176 vec_mul_by (vec_temp_1, a1);
01177 vec_sub (vec_temp_2, vec_temp_1);
01178 vec_set (vec_temp_1, 1);
01179 vec_div (vec_temp_1, vec_temp_2);
01180
01181 vec_delete (vec_temp_2);
01182
01183 D = mat_new_diag (vec_temp_1);
01184
01185 vec_delete (vec_temp_1);
01186
01187
01188
01189 mat_temp_1 = mat_new (numOfIC, numOfIC);
01190 mat_mul_transpose_left (mat_temp_1, Y, hypTan);
01191 mat_temp_2 = mat_new_diag (Beta);
01192 mat_sub (mat_temp_1, mat_temp_2);
01193
01194 mat_delete (mat_temp_2);
01195
01196 mat_temp_2 = mat_new_mul (mat_temp_1, D);
01197
01198 mat_delete (mat_temp_1);
01199
01200 mat_temp_1 = mat_new (vectorSize, numOfIC);
01201 mat_mul (mat_temp_1, B, mat_temp_2);
01202
01203 mat_delete (mat_temp_2);
01204
01205 mat_mul_by (mat_temp_1, myy);
01206 mat_add (B, mat_temp_1);
01207
01208 mat_delete (mat_temp_1);
01209 mat_delete (Y);
01210 mat_delete (hypTan);
01211 vec_delete (Beta);
01212 mat_delete (D);
01213
01214 break;
01215
01216
01217
01218
01219 case 30:
01220
01221 U = mat_new (numSamples, numOfIC);
01222 mat_mul_transpose_left (U, X, B);
01223
01224
01225 Usquared = mat_clone( U );
01226 mat_elem_pow (Usquared, 2);
01227
01228
01229 mat_temp_1 = mat_clone (Usquared);
01230 mat_mul_by (mat_temp_1, -a2);
01231 mat_div_by (mat_temp_1, 2);
01232 ex = mat_clone(mat_temp_1);
01233 mat_elem_exp (ex);
01234
01235 mat_delete (mat_temp_1);
01236
01237
01238 gauss = mat_clone (U);
01239 mat_elem_mul (gauss, ex);
01240
01241
01242 dGauss = mat_new_ones (numSamples, numOfIC);
01243 mat_temp_1 = mat_clone (Usquared);
01244 mat_mul_by (mat_temp_1, a2);
01245 mat_sub (dGauss, mat_temp_1);
01246
01247 mat_delete (mat_temp_1);
01248
01249 mat_elem_mul (dGauss, ex);
01250
01251
01252 vec_temp_1 = mat_cols_sum (dGauss);
01253 mat_temp_1 = mat_new (vectorSize, numOfIC);
01254 for (i = 0; i < vectorSize; i++)
01255 mat_set_row (mat_temp_1, i, vec_temp_1);
01256
01257 vec_delete (vec_temp_1);
01258
01259 mat_elem_mul (mat_temp_1, B);
01260 mat_div_by (mat_temp_1, numSamples);
01261
01262 mat_mul (B, X, gauss);
01263 mat_div_by (B, numSamples);
01264 mat_sub (B, mat_temp_1);
01265
01266 mat_delete (mat_temp_1);
01267 mat_delete (U);
01268 mat_delete (Usquared);
01269 mat_delete (ex);
01270 mat_delete (gauss);
01271 mat_delete (dGauss);
01272
01273 break;
01274
01275 case 31:
01276
01277
01278 Y = mat_new (numSamples, numOfIC);
01279 mat_mul_transpose_left (Y, X, B);
01280
01281
01282
01283 mat_temp_1 = mat_clone (Y);
01284 mat_temp_2 = mat_clone (mat_temp_1);
01285 mat_elem_pow (mat_temp_2, 2);
01286
01287 mat_delete (mat_temp_1);
01288
01289 mat_mul_by (mat_temp_2, -a2);
01290 mat_div_by (mat_temp_2, 2);
01291 ex = mat_clone( mat_temp_2 );
01292 mat_elem_exp (ex);
01293
01294 mat_delete (mat_temp_2);
01295
01296
01297
01298 gauss = mat_clone (Y);
01299 mat_elem_mul (gauss, ex);
01300
01301
01302
01303 mat_temp_1 = mat_clone (Y);
01304 mat_elem_mul (mat_temp_1, gauss);
01305 Beta = mat_cols_sum (mat_temp_1);
01306
01307 mat_delete (mat_temp_1);
01308
01309
01310
01311 mat_temp_1 = mat_new_ones (numSamples, numOfIC);
01312 mat_temp_2 = mat_clone (Y);
01313 mat_elem_pow (mat_temp_2, 2);
01314 mat_mul_by (mat_temp_2, a2);
01315 mat_sub (mat_temp_1, mat_temp_2);
01316
01317 mat_delete (mat_temp_2);
01318
01319 mat_elem_mul (mat_temp_1, ex);
01320 vec_temp_1 = vec_clone (Beta);
01321 vec_temp_2 = mat_cols_sum (mat_temp_1);
01322
01323 mat_delete (mat_temp_1);
01324
01325 vec_sub (vec_temp_1, vec_temp_2);
01326 vec_ones (vec_temp_2);
01327 vec_div (vec_temp_2, vec_temp_1);
01328
01329 vec_delete (vec_temp_1);
01330
01331 D = mat_new_diag (vec_temp_2);
01332
01333 vec_delete (vec_temp_2);
01334
01335
01336
01337 mat_temp_1 = mat_new (numOfIC, numOfIC);
01338 mat_mul_transpose_left (mat_temp_1, Y, gauss);
01339 mat_temp_2 = mat_new_diag (Beta);
01340 mat_sub (mat_temp_1, mat_temp_2);
01341
01342 mat_delete (mat_temp_2);
01343
01344 mat_temp_2 = mat_new_mul (mat_temp_1, D);
01345
01346 mat_delete (mat_temp_1);
01347
01348 mat_temp_1 = mat_new_mul (B, mat_temp_2);
01349
01350 mat_delete (mat_temp_2);
01351
01352 mat_mul_by (mat_temp_1, myy);
01353 mat_add (B, mat_temp_1);
01354
01355 mat_delete (mat_temp_1);
01356 mat_delete (Y);
01357 mat_delete (D);
01358 mat_delete (ex);
01359 mat_delete (gauss);
01360 vec_delete (Beta);
01361
01362 break;
01363
01364 case 32:
01365
01366 mask = getSamples (numSamples, sampleSize);
01367 Xsub = selcol (X, mask);
01368
01369
01370 U = mat_new (ivec_sum (mask), numOfIC);
01371 mat_mul_transpose_left (U, Xsub, B);
01372
01373
01374 Usquared = mat_clone (U);
01375 mat_elem_pow (Usquared, 2);
01376
01377
01378 mat_temp_1 = mat_clone (Usquared);
01379 mat_mul_by (mat_temp_1, -a2);
01380 mat_div_by (mat_temp_1, 2);
01381 ex = mat_clone( mat_temp_1 );
01382 mat_elem_exp (ex);
01383
01384
01385 gauss = mat_clone (U);
01386 mat_elem_mul (gauss, ex);
01387
01388
01389 dGauss = mat_new_ones (ivec_sum (mask), numOfIC);
01390 mat_temp_1 = mat_clone (Usquared);
01391 mat_mul_by (mat_temp_1, a2);
01392 mat_sub (dGauss, mat_temp_1);
01393
01394 mat_delete (mat_temp_1);
01395
01396 mat_elem_mul (dGauss, ex);
01397
01398
01399 vec_temp_1 = mat_cols_sum (dGauss);
01400 mat_temp_1 = mat_new (vectorSize, numOfIC);
01401 for (i = 0; i < vectorSize; i++)
01402 mat_set_row (mat_temp_1, i, vec_temp_1);
01403
01404 vec_delete (vec_temp_1);
01405
01406 mat_elem_mul (mat_temp_1, B);
01407 mat_div_by (mat_temp_1, ivec_sum (mask));
01408 mat_mul (B, Xsub, gauss);
01409 mat_div_by (B, ivec_sum (mask));
01410 mat_sub (B, mat_temp_1);
01411
01412 mat_delete (mat_temp_1);
01413 ivec_delete (mask);
01414 mat_delete (Xsub);
01415 mat_delete (U);
01416 mat_delete (Usquared);
01417 mat_delete (gauss);
01418 mat_delete (dGauss);
01419 mat_delete (ex);
01420
01421 break;
01422
01423 case 33:
01424
01425
01426
01427 mask = getSamples (numSamples, sampleSize);
01428 Y = mat_new (ivec_sum (mask), numOfIC);
01429 mat_temp_1 = selcol (X, mask);
01430 mat_mul_transpose_left (Y, mat_temp_1, B);
01431
01432 mat_delete (mat_temp_1);
01433
01434
01435
01436 mat_temp_1 = mat_clone (Y);
01437 mat_temp_2 = mat_clone (mat_temp_1);
01438 mat_elem_pow (mat_temp_2, 2);
01439
01440 mat_delete (mat_temp_1);
01441
01442 mat_mul_by (mat_temp_2, -a2);
01443 mat_div_by (mat_temp_2, 2);
01444 ex = mat_clone( mat_temp_2 );
01445 mat_elem_exp (ex);
01446
01447 mat_delete (mat_temp_2);
01448
01449
01450
01451 gauss = mat_clone (Y);
01452 mat_elem_mul (gauss, ex);
01453
01454
01455
01456 mat_temp_1 = mat_clone (Y);
01457 mat_elem_mul (mat_temp_1, gauss);
01458 Beta = mat_cols_sum (mat_temp_1);
01459
01460 mat_delete (mat_temp_1);
01461
01462
01463
01464 mat_temp_1 = mat_new_ones (ivec_sum (mask), numOfIC);
01465 mat_temp_2 = mat_clone (Y);
01466 mat_elem_pow (mat_temp_2, 2);
01467 mat_mul_by (mat_temp_2, a2);
01468 mat_sub (mat_temp_1, mat_temp_2);
01469
01470 mat_delete (mat_temp_2);
01471
01472 mat_elem_mul (mat_temp_1, ex);
01473
01474 vec_temp_1 = vec_clone (Beta);
01475 vec_temp_2 = mat_cols_sum (mat_temp_1);
01476
01477 mat_delete (mat_temp_1);
01478
01479 vec_sub (vec_temp_1, vec_temp_2);
01480 vec_ones (vec_temp_2);
01481 vec_div (vec_temp_2, vec_temp_1);
01482
01483 vec_delete (vec_temp_1);
01484
01485 D = mat_new_diag (vec_temp_2);
01486
01487 vec_delete (vec_temp_2);
01488
01489
01490 mat_temp_1 = mat_new (numOfIC, numOfIC);
01491 mat_mul_transpose_left (mat_temp_1, Y, gauss);
01492 mat_temp_2 = mat_new_diag (Beta);
01493 mat_sub (mat_temp_1, mat_temp_2);
01494
01495 mat_delete (mat_temp_2);
01496
01497 mat_temp_2 = mat_new_mul (mat_temp_1, D);
01498
01499 mat_delete (mat_temp_1);
01500
01501 mat_temp_1 = mat_new_mul (B, mat_temp_2);
01502
01503 mat_delete (mat_temp_2);
01504
01505 mat_mul_by (mat_temp_1, myy);
01506 mat_add (B, mat_temp_1);
01507
01508 mat_delete (mat_temp_1);
01509 ivec_delete (mask);
01510 mat_delete (Y);
01511 mat_delete (ex);
01512 mat_delete (gauss);
01513 vec_delete (Beta);
01514 mat_delete (D);
01515
01516 break;
01517
01518
01519
01520 case 40:
01521
01522 mat_temp_1 = mat_new (numSamples, numOfIC);
01523 mat_mul_transpose_left (mat_temp_1, X, B);
01524 mat_temp_2 = mat_clone (mat_temp_1);
01525 mat_elem_pow (mat_temp_2, 2);
01526
01527 mat_delete (mat_temp_1);
01528
01529 mat_mul (B, X, mat_temp_2);
01530
01531 mat_delete (mat_temp_2);
01532
01533 mat_div_by (B, numSamples);
01534
01535
01536 break;
01537
01538 case 41:
01539
01540
01541 Y = mat_new (numSamples, numOfIC);
01542 mat_mul_transpose_left (Y, X, B);
01543
01544
01545 Gskew = mat_clone (Y);
01546 mat_elem_pow (Gskew, 2);
01547
01548
01549 mat_temp_1 = mat_clone (Y);
01550 mat_elem_mul (mat_temp_1, Gskew);
01551 Beta = mat_cols_sum (mat_temp_1);
01552
01553 mat_delete (mat_temp_1);
01554
01555
01556 vec_temp_1 = vec_new_ones (numOfIC);
01557 vec_div (vec_temp_1, Beta);
01558 D = mat_new_diag (vec_temp_1);
01559
01560 vec_delete (vec_temp_1);
01561
01562
01563 mat_temp_1 = mat_new (numOfIC, numOfIC);
01564 mat_mul_transpose_left (mat_temp_1, Y, Gskew);
01565 mat_temp_2 = mat_new_diag (Beta);
01566 mat_sub (mat_temp_1, mat_temp_2);
01567
01568 mat_delete (mat_temp_2);
01569
01570 mat_temp_2 = mat_new_mul (mat_temp_1, D);
01571
01572 mat_delete (mat_temp_1);
01573
01574 mat_temp_1 = mat_new (vectorSize, numOfIC);
01575 mat_mul (mat_temp_1, B, mat_temp_2);
01576
01577 mat_delete (mat_temp_2);
01578
01579 mat_mul_by (mat_temp_1, myy);
01580 mat_add (B, mat_temp_1);
01581
01582 mat_delete (mat_temp_1);
01583 mat_delete (Y);
01584 mat_delete (Gskew);
01585 vec_delete (Beta);
01586 mat_delete (D);
01587
01588 break;
01589
01590 case 42:
01591
01592 mask = getSamples (numSamples, sampleSize);
01593 Xsub = selcol (X, mask);
01594
01595
01596 mat_temp_1 = mat_new (ivec_sum (mask), numOfIC);
01597 mat_mul_transpose_left (mat_temp_1, Xsub, B);
01598 mat_temp_2 = mat_clone (mat_temp_1);
01599 mat_elem_pow (mat_temp_2, 2);
01600
01601 mat_delete (mat_temp_1);
01602
01603 mat_mul (B, Xsub, mat_temp_2);
01604
01605 mat_delete (mat_temp_2);
01606
01607 mat_div_by (B, ivec_sum (mask));
01608
01609 mat_delete (Xsub);
01610 ivec_delete (mask);
01611
01612 break;
01613
01614 case 43:
01615
01616
01617 mask = getSamples (numSamples, sampleSize);
01618 mat Y = mat_new (ivec_sum (mask), numOfIC);
01619 mat_temp_1 = selcol (X, mask);
01620 mat_mul_transpose_left (Y, mat_temp_1, B);
01621
01622 mat_delete (mat_temp_1);
01623
01624
01625 Gskew = mat_clone (Y);
01626 mat_elem_pow (Gskew, 2);
01627
01628
01629 mat_temp_1 = mat_clone (Y);
01630 mat_elem_mul (mat_temp_1, Gskew);
01631 Beta = mat_cols_sum (mat_temp_1);
01632
01633 mat_delete (mat_temp_1);
01634
01635
01636 vec_temp_1 = vec_new_ones (numOfIC);
01637 vec_div (vec_temp_1, Beta);
01638 D = mat_new_diag (vec_temp_1);
01639
01640 vec_delete (vec_temp_1);
01641
01642
01643 mat_temp_1 = mat_new (numOfIC, numOfIC);
01644 mat_mul_transpose_left (mat_temp_1, Y, Gskew);
01645 mat_temp_2 = mat_new_diag (Beta);
01646 mat_sub (mat_temp_1, mat_temp_2);
01647
01648 mat_delete (mat_temp_2);
01649
01650 mat_temp_2 = mat_new_mul (mat_temp_1, D);
01651
01652 mat_delete (mat_temp_1);
01653
01654 mat_temp_1 = mat_new (vectorSize, numOfIC);
01655 mat_mul (mat_temp_1, B, mat_temp_2);
01656
01657 mat_delete (mat_temp_2);
01658
01659 mat_mul_by (mat_temp_1, myy);
01660 mat_add (B, mat_temp_1);
01661
01662 mat_delete (mat_temp_1);
01663 mat_delete (Y);
01664 mat_delete (Gskew);
01665 vec_delete (Beta);
01666 ivec_delete (mask);
01667 mat_delete (D);
01668
01669 break;
01670
01671 default:
01672 fprintf (stderr, "Warning: Code for desired nonlinearity not found!\n");
01673 break;
01674 }
01675 }
01676
01677
01678
01679 mat_mul_transpose_left (W, B, whiteningMatrix);
01680 mat_delete (B);
01681 mat_delete (BOld);
01682 mat_delete (BOld2);
01683 }
01684
01685
01686
01687
01688 if (approachMode == 2)
01689 {
01690
01691 mat_zeros (A);
01692 mat_zeros (W);
01693
01694 B = mat_new_zeros (vectorSize, vectorSize);
01695
01696 w = vec_new (vectorSize);
01697
01698 wOld = vec_new (vectorSize);
01699 wOld2 = vec_new (vectorSize);
01700 w_minus_wOld = vec_new (vectorSize);
01701 w_plus_wOld = vec_new (vectorSize);
01702 w_minus_wOld2 = vec_new (vectorSize);
01703 w_plus_wOld2 = vec_new (vectorSize);
01704
01705 round = 0;
01706
01707 numFailures = 0;
01708
01709 while (round < numOfIC)
01710 {
01711 myy = myyOrig;
01712 usedNlinearity = gOrig;
01713 stroke = 0;
01714 notFine = 1;
01715 longe = 0;
01716 endFinetuning = 0;
01717
01718
01719 if (verbose)
01720 fprintf (stderr, "IC %d ", round + 1);
01721
01722
01723
01724 if (!initialStateMode)
01725 {
01726 vec_randn (w);
01727 }
01728 else if (initialStateMode == 1)
01729 {
01730 vec_temp_1 = mat_get_col (guess, round);
01731 w = mat_vec_mul (whiteningMatrix, vec_temp_1);
01732 vec_delete (vec_temp_1);
01733 }
01734
01735
01736 mat_temp_1 = mat_new_transpose (B);
01737 vec_temp_1 = mat_vec_mul (mat_temp_1, w);
01738
01739 mat_delete (mat_temp_1);
01740
01741 vec_temp_2 = mat_vec_mul (B, vec_temp_1);
01742
01743 vec_delete (vec_temp_1);
01744
01745 vec_sub (w, vec_temp_2);
01746
01747 vec_delete (vec_temp_2);
01748
01749
01750 vec_div_by (w, vec_norm (w, 2));
01751
01752 vec_zeros (wOld);
01753 vec_zeros (wOld2);
01754
01755
01756
01757
01758 i = 1;
01759 gabba = 1;
01760 while (i <= maxNumIterations + gabba)
01761 {
01762
01763
01764
01765
01766
01767
01768 mat_temp_1 = mat_new_transpose (B);
01769 vec_temp_1 = mat_vec_mul (mat_temp_1, w);
01770
01771 mat_delete (mat_temp_1);
01772
01773 vec_temp_2 = mat_vec_mul (B, vec_temp_1);
01774
01775 vec_delete (vec_temp_1);
01776
01777 vec_sub (w, vec_temp_2);
01778
01779 vec_delete (vec_temp_2);
01780
01781
01782 vec_div_by (w, vec_norm (w, 2));
01783
01784 if (notFine)
01785 {
01786 if (i == maxNumIterations + 1)
01787 {
01788 if (verbose)
01789 fprintf(stderr, "\nComponent number %d did not converge in %d iterations.\n", round + 1, maxNumIterations);
01790
01791 round--;
01792 numFailures++;
01793 if (numFailures > failureLimit)
01794 {
01795 if (verbose)
01796 fprintf(stderr, "Warning: Too many failures to converge (%d). Giving up.\n", numFailures);
01797
01798 if (round == 0)
01799 {
01800 mat_void (A);
01801 mat_void (W);
01802
01803
01804 }
01805
01806 return 3;
01807 }
01808 break;
01809 }
01810 }
01811 else
01812 {
01813 if (i >= endFinetuning)
01814 vec_copy (wOld, w);
01815 }
01816
01817
01818 if (verbose)
01819 fprintf (stderr, ".");
01820
01821
01822
01823
01824 vec_copy (w_minus_wOld, w);
01825 vec_copy (w_plus_wOld, w);
01826 vec_sub (w_minus_wOld, wOld);
01827 vec_add (w_plus_wOld, wOld);
01828
01829 if ((vec_norm (w_minus_wOld, 2) < epsilon) || (vec_norm (w_plus_wOld, 2) < epsilon))
01830 {
01831 if (finetuningEnabled && notFine)
01832 {
01833 if (verbose)
01834 fprintf (stderr, "Initial convergence, fine-tuning: ");
01835 notFine = 0;
01836 gabba = maxFinetune;
01837 vec_zeros (wOld);
01838 vec_zeros (wOld2);
01839 usedNlinearity = gFine;
01840 myy = myyK * myyOrig;
01841
01842 endFinetuning = maxFinetune + i;
01843 }
01844 else
01845 {
01846 numFailures = 0;
01847
01848
01849 mat_set_col (B, round, w);
01850
01851
01852
01853
01854 vec_temp_1 = mat_vec_mul (dewhiteningMatrix, w);
01855 mat_set_col (A, round, vec_temp_1);
01856
01857 vec_delete (vec_temp_1);
01858
01859
01860
01861 mat_temp_1 = mat_new_transpose (whiteningMatrix);
01862 vec_temp_1 = mat_vec_mul (mat_temp_1, w);
01863
01864 mat_delete (mat_temp_1);
01865
01866 mat_set_row (W, round, vec_temp_1);
01867
01868 vec_delete (vec_temp_1);
01869
01870
01871 if (verbose)
01872 fprintf (stderr, "computed ( %d steps ) \n", i);
01873
01874 break;
01875 }
01876 }
01877 else if (stabilizationEnabled)
01878 {
01879 vec_copy (w_minus_wOld2, w);
01880 vec_copy (w_plus_wOld2, w);
01881 vec_sub (w_minus_wOld2, wOld2);
01882 vec_add (w_plus_wOld2, wOld2);
01883
01884 if (!stroke && ((vec_norm (w_minus_wOld2, 2) < epsilon) || (vec_norm (w_plus_wOld2, 2) < epsilon)))
01885 {
01886 stroke = myy;
01887 if (verbose)
01888 fprintf (stderr, "Stroke!");
01889 myy = .5 * myy;
01890 if (usedNlinearity % 2 == 0)
01891 usedNlinearity = usedNlinearity + 1;
01892 }
01893 else if (stroke)
01894 {
01895 myy = stroke;
01896 stroke = 0;
01897 if ((myy == 1) && ((usedNlinearity % 2) != 0))
01898 usedNlinearity = usedNlinearity - 1;
01899 }
01900 else if (notFine && (longe == 0) && (i > (maxNumIterations / 2)))
01901 {
01902 if (verbose)
01903 fprintf (stderr, "Taking long (reducing step size) ");
01904 longe = 1;
01905 myy = .5 * myy;
01906 if ((usedNlinearity % 2) == 0)
01907 usedNlinearity = usedNlinearity + 1;
01908 }
01909 }
01910
01911 vec_copy (wOld2, wOld);
01912 vec_copy (wOld, w);
01913
01914 switch (usedNlinearity)
01915 {
01916
01917
01918 case 10:
01919
01920 mat_temp_1 = mat_new_transpose (X);
01921 vec_temp_1 = mat_vec_mul (mat_temp_1, w);
01922
01923 mat_delete (mat_temp_1);
01924
01925 vec_temp_2 = vec_new_pow (vec_temp_1, 3);
01926
01927 vec_delete (vec_temp_1);
01928
01929 vec_temp_1 = mat_vec_mul (X, vec_temp_2);
01930
01931 vec_delete (vec_temp_2);
01932
01933 vec_div_by (vec_temp_1, numSamples);
01934 vec_mul_by (w, 3);
01935 vec_sub (vec_temp_1, w);
01936 vec_copy (w, vec_temp_1);
01937
01938 vec_delete (vec_temp_1);
01939
01940 break;
01941
01942 case 11:
01943
01944 mat_temp_1 = mat_new_transpose (X);
01945 vec_temp_1 = mat_vec_mul (mat_temp_1, w);
01946
01947 mat_delete (mat_temp_1);
01948
01949 vec_temp_2 = vec_new_pow (vec_temp_1, 3);
01950
01951 vec_delete (vec_temp_1);
01952
01953 EXGpow3 = mat_vec_mul (X, vec_temp_2);
01954
01955 vec_delete (vec_temp_2);
01956
01957 vec_div_by (EXGpow3, numSamples);
01958
01959
01960 Beta_double = vec_inner_product (w, EXGpow3);
01961
01962
01963 vec_temp_1 = vec_clone (w);
01964 vec_mul_by (vec_temp_1, Beta_double);
01965 vec_sub (EXGpow3, vec_temp_1);
01966
01967 vec_delete (vec_temp_1);
01968
01969 vec_mul_by (EXGpow3, myy);
01970 vec_div_by (EXGpow3, (3.0 - Beta_double));
01971 vec_sub (w, EXGpow3);
01972
01973 vec_delete (EXGpow3);
01974
01975 break;
01976
01977 case 12:
01978
01979 mask = getSamples (numSamples, sampleSize);
01980 Xsub = selcol (X, mask);
01981
01982
01983 mat_temp_1 = mat_new_transpose (Xsub);
01984 vec_temp_1 = mat_vec_mul (mat_temp_1, w);
01985
01986 mat_delete (mat_temp_1);
01987
01988 vec_temp_2 = vec_new_pow (vec_temp_1, 3);
01989
01990 vec_delete (vec_temp_1);
01991
01992 vec_temp_1 = mat_vec_mul (Xsub, vec_temp_2);
01993
01994 vec_delete (vec_temp_2);
01995
01996 vec_div_by (vec_temp_1, ivec_sum (mask));
01997 vec_mul_by (w, 3);
01998 vec_sub (vec_temp_1, w);
01999 vec_copy (w, vec_temp_1);
02000
02001 vec_delete (vec_temp_1);
02002 ivec_delete (mask);
02003 mat_delete (Xsub);
02004
02005 break;
02006
02007 case 13:
02008
02009 mask = getSamples (numSamples, sampleSize);
02010 Xsub = selcol (X, mask);
02011
02012
02013 mat_temp_1 = mat_new_transpose (Xsub);
02014 vec_temp_1 = mat_vec_mul (mat_temp_1, w);
02015
02016 mat_delete (mat_temp_1);
02017
02018 vec_temp_2 = vec_new_pow (vec_temp_1, 3);
02019
02020 vec_delete (vec_temp_1);
02021
02022 EXGpow3 = mat_vec_mul (Xsub, vec_temp_2);
02023
02024 vec_delete (vec_temp_2);
02025
02026 vec_div_by (EXGpow3, ivec_sum (mask));
02027
02028
02029 Beta_double = vec_inner_product (w, EXGpow3);
02030
02031
02032 vec_temp_1 = vec_clone (w);
02033 vec_mul_by (vec_temp_1, Beta_double);
02034
02035 vec_sub (EXGpow3, vec_temp_1);
02036
02037 vec_delete (vec_temp_1);
02038
02039 vec_mul_by (EXGpow3, myy);
02040 vec_div_by (EXGpow3, (3.0 - Beta_double));
02041 vec_sub (w, EXGpow3);
02042
02043 mat_delete (Xsub);
02044 ivec_delete (mask);
02045 vec_delete (EXGpow3);
02046
02047 break;
02048
02049
02050
02051 case 20:
02052
02053 mat_temp_1 = mat_new_transpose (X);
02054 vec_temp_1 = mat_vec_mul (mat_temp_1, w);
02055
02056 mat_delete (mat_temp_1);
02057
02058 vec_mul_by (vec_temp_1, a1);
02059 vec_hypTan = vec_tanh (vec_temp_1);
02060
02061 vec_delete (vec_temp_1);
02062
02063
02064 vec_temp_1 = mat_vec_mul (X, vec_hypTan);
02065
02066 vec_mul_by (w, numSamples - vec_sum_sqr (vec_hypTan));
02067 vec_mul_by (w, a1);
02068
02069 vec_sub (vec_temp_1, w);
02070 vec_div_by (vec_temp_1, numSamples);
02071 vec_copy (w, vec_temp_1);
02072
02073 vec_delete (vec_temp_1);
02074 vec_delete (vec_hypTan);
02075
02076 break;
02077
02078 case 21:
02079
02080 mat_temp_1 = mat_new_transpose (X);
02081 vec_temp_1 = mat_vec_mul (mat_temp_1, w);
02082
02083 mat_delete (mat_temp_1);
02084
02085 vec_mul_by (vec_temp_1, a1);
02086 vec_hypTan = vec_tanh (vec_temp_1);
02087
02088 vec_delete (vec_temp_1);
02089
02090
02091 vec_temp_1 = mat_vec_mul (X, vec_hypTan);
02092 Beta_double = vec_inner_product (w, vec_temp_1);
02093
02094 vec_delete (vec_temp_1);
02095
02096
02097 vec_temp_1 = vec_clone (w);
02098 vec_mul_by (vec_temp_1, Beta_double);
02099 vec_temp_2 = mat_vec_mul (X, vec_hypTan);
02100 vec_sub (vec_temp_2, vec_temp_1);
02101
02102 vec_delete (vec_temp_1);
02103
02104 vec_mul_by (vec_temp_2, myy);
02105 vec_div_by (vec_temp_2,
02106 (a1 * (numSamples - vec_sum_sqr (vec_hypTan)) -
02107 Beta_double));
02108 vec_sub (w, vec_temp_2);
02109
02110 vec_delete (vec_temp_2);
02111 vec_delete (vec_hypTan);
02112
02113 break;
02114
02115 case 22:
02116
02117 mask = getSamples (numSamples, sampleSize);
02118 Xsub = selcol (X, mask);
02119
02120
02121 mat_temp_1 = mat_new_transpose (Xsub);
02122 vec_temp_1 = mat_vec_mul (mat_temp_1, w);
02123
02124 mat_delete (mat_temp_1);
02125
02126 vec_mul_by (vec_temp_1, a1);
02127 vec_hypTan = vec_tanh (vec_temp_1);
02128
02129 vec_delete (vec_temp_1);
02130
02131
02132 vec_temp_1 = mat_vec_mul (Xsub, vec_hypTan);
02133
02134
02135 vec_mul_by (w, ivec_sum (mask) - vec_sum_sqr (vec_hypTan));
02136 vec_mul_by (w, a1);
02137
02138 vec_sub (vec_temp_1, w);
02139 vec_div_by (vec_temp_1, ivec_sum (mask));
02140 vec_copy (w, vec_temp_1);
02141
02142 vec_delete (vec_temp_1);
02143 ivec_delete (mask);
02144 mat_delete (Xsub);
02145 vec_delete (vec_hypTan);
02146
02147 break;
02148
02149 case 23:
02150
02151 mask = getSamples (numSamples, sampleSize);
02152 Xsub = selcol (X, mask);
02153
02154
02155 mat_temp_1 = mat_new_transpose (Xsub);
02156 vec_temp_1 = mat_vec_mul (mat_temp_1, w);
02157
02158 mat_delete (mat_temp_1);
02159
02160 vec_mul_by (vec_temp_1, a1);
02161 vec_hypTan = vec_tanh (vec_temp_1);
02162
02163 vec_delete (vec_temp_1);
02164
02165
02166 vec_temp_1 = mat_vec_mul (Xsub, vec_hypTan);
02167 Beta_double = vec_inner_product (w, vec_temp_1);
02168
02169 vec_delete (vec_temp_1);
02170
02171
02172 vec_temp_1 = vec_clone (w);
02173 vec_mul_by (vec_temp_1, Beta_double);
02174 vec_temp_2 = mat_vec_mul (Xsub, vec_hypTan);
02175 vec_sub (vec_temp_2, vec_temp_1);
02176
02177 vec_delete (vec_temp_1);
02178
02179 vec_mul_by (vec_temp_2, myy);
02180 vec_div_by (vec_temp_2,
02181 (a1 *
02182 (ivec_sum (mask) - vec_sum_sqr (vec_hypTan)) -
02183 Beta_double));
02184 vec_sub (w, vec_temp_2);
02185
02186 vec_delete (vec_temp_2);
02187
02188 mat_delete (Xsub);
02189 ivec_delete (mask);
02190 vec_delete (vec_hypTan);
02191
02192 break;
02193
02194
02195
02196 case 30:
02197
02198
02199 mat_temp_1 = mat_new_transpose (X);
02200 u = mat_vec_mul (mat_temp_1, w);
02201
02202 mat_delete (mat_temp_1);
02203
02204
02205 u2 = vec_new_pow (u, 2);
02206
02207
02208 vec_ex = vec_clone (u2);
02209 vec_mul_by (vec_ex, -a2);
02210 vec_div_by (vec_ex, 2);
02211 vec_exp (vec_ex);
02212
02213
02214
02215 vec_gauss = vec_clone (u);
02216 vec_mul (vec_gauss, vec_ex);
02217
02218
02219 vec_dGauss = vec_new_ones (numSamples);
02220 vec_mul_by (u2, a2);
02221 vec_sub (vec_dGauss, u2);
02222 vec_mul (vec_dGauss, vec_ex);
02223
02224
02225 vec_temp_1 = mat_vec_mul (X, vec_gauss);
02226 vec_mul_by (w, vec_sum (vec_dGauss));
02227 vec_sub (vec_temp_1, w);
02228 vec_div_by (vec_temp_1, numSamples);
02229 vec_copy (w, vec_temp_1);
02230
02231 vec_delete (vec_temp_1);
02232 vec_delete (u);
02233 vec_delete (u2);
02234 vec_delete (vec_ex);
02235 vec_delete (vec_gauss);
02236 vec_delete (vec_dGauss);
02237
02238 break;
02239
02240 case 31:
02241
02242 mat_temp_1 = mat_new_transpose (X);
02243 u = mat_vec_mul (mat_temp_1, w);
02244
02245 mat_delete (mat_temp_1);
02246
02247
02248 u2 = vec_new_pow (u, 2);
02249
02250
02251 vec_ex = vec_clone (u2);
02252 vec_mul_by (vec_ex, -a2);
02253 vec_div_by (vec_ex, 2);
02254 vec_exp (vec_ex);
02255
02256
02257 vec_gauss = vec_clone (u);
02258 vec_mul (vec_gauss, vec_ex);
02259
02260
02261 vec_dGauss = vec_new_ones (numSamples);
02262 vec_mul_by (u2, a2);
02263 vec_sub (vec_dGauss, u2);
02264 vec_mul (vec_dGauss, vec_ex);
02265
02266
02267 vec_temp_1 = mat_vec_mul (X, vec_gauss);
02268 Beta_double = vec_inner_product (w, vec_temp_1);
02269
02270 vec_delete (vec_temp_1);
02271
02272
02273 vec_temp_1 = vec_clone (w);
02274 vec_mul_by (vec_temp_1, Beta_double);
02275 vec_temp_2 = mat_vec_mul (X, vec_gauss);
02276 vec_sub (vec_temp_2, vec_temp_1);
02277
02278 vec_delete (vec_temp_1);
02279
02280 vec_mul_by (vec_temp_2, myy);
02281 vec_div_by (vec_temp_2,
02282 (vec_sum (vec_dGauss) - Beta_double));
02283 vec_sub (w, vec_temp_2);
02284
02285 vec_delete (vec_temp_2);
02286 vec_delete (u);
02287 vec_delete (u2);
02288 vec_delete (vec_ex);
02289 vec_delete (vec_gauss);
02290 vec_delete (vec_dGauss);
02291
02292 break;
02293
02294 case 32:
02295
02296 mask = getSamples (numSamples, sampleSize);
02297 Xsub = selcol (X, mask);
02298
02299
02300 mat_temp_1 = mat_new_transpose (Xsub);
02301 u = mat_vec_mul (mat_temp_1, w);
02302
02303 mat_delete (mat_temp_1);
02304
02305
02306 u2 = vec_new_pow (u, 2);
02307
02308
02309 vec_ex = vec_clone (u2);
02310 vec_mul_by (vec_ex, -a2);
02311 vec_div_by (vec_ex, 2);
02312 vec_exp (vec_ex);
02313
02314
02315 vec_gauss = vec_clone (u);
02316 vec_mul (vec_gauss, vec_ex);
02317
02318
02319 vec_dGauss = vec_new_ones (ivec_sum (mask));
02320 vec_mul_by (u2, a2);
02321 vec_sub (vec_dGauss, u2);
02322 vec_mul (vec_dGauss, vec_ex);
02323
02324
02325 vec_temp_1 = mat_vec_mul (Xsub, vec_gauss);
02326 vec_mul_by (w, vec_sum (vec_dGauss));
02327 vec_sub (vec_temp_1, w);
02328 vec_div_by (vec_temp_1, ivec_sum (mask));
02329 vec_copy (w, vec_temp_1);
02330
02331 vec_delete (vec_temp_1);
02332
02333 ivec_delete (mask);
02334 mat_delete (Xsub);
02335 vec_delete (u);
02336 vec_delete (u2);
02337 vec_delete (vec_ex);
02338 vec_delete (vec_gauss);
02339 vec_delete (vec_dGauss);
02340
02341 break;
02342
02343 case 33:
02344
02345 mask = getSamples (numSamples, sampleSize);
02346 Xsub = selcol (X, mask);
02347
02348
02349 mat_temp_1 = mat_new_transpose (Xsub);
02350 u = mat_vec_mul (mat_temp_1, w);
02351 mat_delete (mat_temp_1);
02352
02353
02354 u2 = vec_new_pow (u, 2);
02355
02356
02357 vec_ex = vec_clone (u2);
02358 vec_mul_by (vec_ex, -a2);
02359 vec_div_by (vec_ex, 2);
02360 vec_exp (vec_ex);
02361
02362
02363 vec_gauss = vec_clone (u);
02364 vec_mul (vec_gauss, vec_ex);
02365
02366
02367 vec_dGauss = vec_new_ones (ivec_sum (mask));
02368 vec_mul_by (u2, a2);
02369 vec_sub (vec_dGauss, u2);
02370 vec_mul (vec_dGauss, vec_ex);
02371
02372
02373 vec_temp_1 = mat_vec_mul (Xsub, vec_gauss);
02374 Beta_double = vec_inner_product (w, vec_temp_1);
02375
02376 vec_delete (vec_temp_1);
02377
02378
02379 vec_temp_1 = vec_clone (w);
02380 vec_mul_by (vec_temp_1, Beta_double);
02381 vec_temp_2 = mat_vec_mul (Xsub, vec_gauss);
02382 vec_sub (vec_temp_2, vec_temp_1);
02383
02384 vec_delete (vec_temp_1);
02385
02386 vec_mul_by (vec_temp_2, myy);
02387 vec_div_by (vec_temp_2,
02388 (vec_sum (vec_dGauss) - Beta_double));
02389 vec_sub (w, vec_temp_2);
02390
02391 vec_delete (vec_temp_2);
02392 ivec_delete (mask);
02393 mat_delete (Xsub);
02394 vec_delete (u);
02395 vec_delete (u2);
02396 vec_delete (vec_ex);
02397 vec_delete (vec_gauss);
02398 vec_delete (vec_dGauss);
02399
02400 break;
02401
02402
02403
02404 case 40:
02405
02406 mat_temp_1 = mat_new_transpose (X);
02407 vec_temp_1 = mat_vec_mul (mat_temp_1, w);
02408
02409 mat_delete (mat_temp_1);
02410
02411 vec_temp_2 = vec_new_pow (vec_temp_1, 2);
02412
02413 vec_delete (vec_temp_1);
02414
02415 vec_temp_1 = mat_vec_mul (X, vec_temp_2);
02416 vec_copy (w, vec_temp_2);
02417
02418 vec_delete (vec_temp_2);
02419
02420 vec_div_by (w, numSamples);
02421
02422 break;
02423
02424 case 41:
02425
02426 mat_temp_1 = mat_new_transpose (X);
02427 vec_temp_1 = mat_vec_mul (mat_temp_1, w);
02428
02429 mat_delete (mat_temp_1);
02430
02431 vec_temp_2 = vec_new_pow (vec_temp_1, 2);
02432
02433 vec_delete (vec_temp_1);
02434
02435 EXGskew = mat_vec_mul (X, vec_temp_2);
02436
02437 vec_delete (vec_temp_2);
02438
02439 vec_div_by (EXGskew, numSamples);
02440
02441
02442 Beta_double = vec_inner_product (w, EXGskew);
02443
02444
02445 vec_temp_1 = vec_clone (w);
02446 vec_mul_by (vec_temp_1, Beta_double);
02447 vec_sub (EXGskew, vec_temp_1);
02448
02449 vec_delete (vec_temp_1);
02450
02451 vec_mul_by (EXGskew, myy);
02452 vec_div_by (EXGskew, Beta_double);
02453 vec_add (w, EXGskew);
02454
02455 vec_delete (EXGskew);
02456
02457 break;
02458
02459 case 42:
02460
02461 mask = getSamples (numSamples, sampleSize);
02462 Xsub = selcol (X, mask);
02463
02464
02465 mat_temp_1 = mat_new_transpose (Xsub);
02466 vec_temp_1 = mat_vec_mul (mat_temp_1, w);
02467
02468 mat_delete (mat_temp_1);
02469
02470 vec_temp_2 = vec_new_pow (vec_temp_1, 2);
02471
02472 vec_delete (vec_temp_1);
02473
02474 vec_temp_1 = mat_vec_mul (Xsub, vec_temp_2);
02475
02476 vec_delete (vec_temp_2);
02477
02478 vec_div_by (vec_temp_1, ivec_sum (mask));
02479 vec_copy (w, vec_temp_1);
02480
02481 vec_delete (vec_temp_1);
02482 ivec_delete (mask);
02483 mat_delete (Xsub);
02484
02485 break;
02486
02487 case 43:
02488
02489 mask = getSamples (numSamples, sampleSize);
02490 Xsub = selcol (X, mask);
02491
02492
02493 mat_temp_1 = mat_new_transpose (Xsub);
02494 vec_temp_1 = mat_vec_mul (mat_temp_1, w);
02495
02496 mat_delete (mat_temp_1);
02497
02498 vec_temp_2 = vec_new_pow (vec_temp_1, 2);
02499
02500 vec_delete (vec_temp_1);
02501
02502 EXGskew = mat_vec_mul (Xsub, vec_temp_2);
02503
02504 vec_delete (vec_temp_2);
02505
02506 vec_div_by (EXGskew, ivec_sum (mask));
02507
02508
02509 Beta_double = vec_inner_product (w, EXGskew);
02510
02511
02512 vec_temp_1 = vec_clone (w);
02513 vec_mul_by (vec_temp_1, Beta_double);
02514 vec_sub (EXGskew, vec_temp_1);
02515
02516 vec_delete (vec_temp_1);
02517
02518 vec_mul_by (EXGskew, myy);
02519 vec_div_by (EXGskew, Beta_double);
02520 vec_add (w, EXGskew);
02521
02522 mat_delete (Xsub);
02523 ivec_delete (mask);
02524 vec_delete (EXGskew);
02525
02526 break;
02527
02528 case '?':
02529 printf ("Code for desired nonlinearity not found!\n");
02530 break;
02531 }
02532
02533
02534
02535 vec_div_by (w, vec_norm (w, 2));
02536 i++;
02537 }
02538
02539 round++;
02540 }
02541
02542 if (verbose)
02543 fprintf (stderr, "Done.\n");
02544
02545 mat_delete (B);
02546 vec_delete (w);
02547 vec_delete (wOld);
02548 vec_delete (wOld2);
02549 vec_delete (w_plus_wOld);
02550 vec_delete (w_minus_wOld);
02551 vec_delete (w_plus_wOld2);
02552 vec_delete (w_minus_wOld2);
02553
02554 }
02555
02556
02557 return 0;
02558
02559 }
02560
02561 int
02562 it_ica (mat X, char *approach, int numOfIC, char *g, char *finetune, int a1,
02563 int a2, double myy, int stabilization, double epsilon,
02564 int maxNumIterations, int maxFinetune, char *initState, mat guess,
02565 int sampleSize, int verbose, int jumpPCA, int jumpWhitening,
02566 int only, int userNumOfIC, int firstEIG, int lastEIG, mat * icasig,
02567 mat * newE, mat * newD, mat * whitesig, mat * whiteningMatrix,
02568 mat * dewhiteningMatrix, mat * A, mat * W)
02569 {
02570
02571 int i;
02572 int Dim = mat_height (X);
02573 int NumOfSampl = mat_width (X);
02574 int newDim = 0;
02575
02576 ivec selectedColumns;
02577 mat mat_temp_1;
02578 vec vec_temp_1;
02579 mat D = mat_new (Dim, Dim);
02580 mat E = mat_new (Dim, Dim);
02581
02582 vec mixedmean;
02583
02584 mat mixedsig = mat_new (Dim, NumOfSampl);
02585 mixedmean = remmean (X, mixedsig);
02586
02587 if (verbose)
02588 {
02589 fprintf (stderr, "Number of signals: %d\n", mat_height (mixedsig));
02590 fprintf (stderr, "Number of samples: %d\n", mat_width (mixedsig));
02591 }
02592
02593
02594
02595
02596 if (Dim > NumOfSampl)
02597 {
02598 if (verbose)
02599 {
02600 fprintf (stderr, "Warning: ");
02601 fprintf (stderr, "The signal matrix may be oriented in the wrong way.\n");
02602 fprintf (stderr, "In that case transpose the matrix.\n\n");
02603 }
02604
02605 return 1;
02606 }
02607
02608
02609
02610
02611 if (jumpWhitening == 3)
02612 {
02613 if (verbose)
02614 {
02615 fprintf (stderr, "Whitened signal and corresponding matrises supplied\n");
02616 fprintf (stderr, "PCA calculations not needed\n");
02617 }
02618 }
02619
02620
02621
02622 else
02623 {
02624 if (jumpPCA == 2)
02625 {
02626 if (verbose)
02627 {
02628 fprintf (stderr, "Values for PCA calculations supplied\n");
02629 fprintf (stderr, "PCA calculations not needed\n");
02630 }
02631 }
02632 else
02633 {
02634
02635 if ((jumpPCA > 0) && verbose)
02636 {
02637 fprintf (stderr, "You must suply all of these in order to jump PCA:\n");
02638 fprintf (stderr, "'pcaE', 'pcaD'.\n");
02639 }
02640
02641
02642 selectedColumns =
02643 PCAmat (mixedsig, firstEIG, lastEIG, verbose, D, E);
02644 newDim = ivec_sum (selectedColumns);
02645 *(newE) = selcol (E, selectedColumns);
02646 mat_temp_1 = selcol (D, selectedColumns);
02647 mat_transpose (mat_temp_1);
02648 *(newD) = selcol (mat_temp_1, selectedColumns);
02649 mat_delete (mat_temp_1);
02650 ivec_delete (selectedColumns);
02651 }
02652 }
02653
02654
02655 if (only > 1)
02656 {
02657
02658 *(whitesig) = mat_new (newDim, NumOfSampl);
02659 *(whiteningMatrix) = mat_new (newDim, Dim);
02660 *(dewhiteningMatrix) = mat_new (Dim, newDim);
02661
02662
02663 if (jumpWhitening == 3)
02664 {
02665 if (verbose)
02666 fprintf (stderr, "Whitening not needed.\n");
02667 }
02668 else
02669 {
02670
02671
02672 if ((jumpWhitening > 0) && verbose)
02673 {
02674 fprintf(stderr, "You must suply all of these in order to jump whitening:\n");
02675 fprintf(stderr, "'whiteSig', 'whiteMat', 'dewhiteMat'.\n");
02676 }
02677
02678
02679 whitenv (mixedsig, *(newE), *(newD), verbose, *(whitesig),
02680 *(whiteningMatrix), *(dewhiteningMatrix));
02681 }
02682 }
02683 if (only > 2)
02684 {
02685
02686
02687
02688
02689
02690
02691
02692
02693
02694 if (numOfIC > newDim)
02695 numOfIC = newDim;
02696
02697 if (verbose && (userNumOfIC == 1))
02698 {
02699 fprintf(stderr, "Warning: estimating only %d independent components\n", numOfIC);
02700 fprintf(stderr, "(Can't estimate more independent components than dimension of data)\n");
02701 }
02702
02703 *(W) = mat_new_zeros (numOfIC, Dim);
02704 *(A) = mat_new_zeros (Dim, numOfIC);
02705
02706
02707 *icasig = mat_new_zeros (numOfIC, NumOfSampl);
02708
02709 fpICA (*(whitesig), *(whiteningMatrix), *(dewhiteningMatrix), approach,
02710 numOfIC, g, finetune, a1, a2, myy, stabilization, epsilon,
02711 maxNumIterations, maxFinetune, initState, guess, sampleSize,
02712 verbose, *A, *W);
02713
02714
02715 if (W)
02716
02717 {
02718 if (verbose)
02719 fprintf(stderr, "Adding the mean back to the data.\n");
02720
02721
02722 mat_mul (*icasig, *W, mixedsig);
02723 mat_temp_1 = mat_new (numOfIC, NumOfSampl);
02724 vec_temp_1 = mat_vec_mul (*W, mixedmean);
02725
02726 for (i = 0; i < NumOfSampl; i++)
02727 mat_set_col (mat_temp_1, i, vec_temp_1);
02728 vec_delete (vec_temp_1);
02729 mat_add (*icasig, mat_temp_1);
02730 mat_delete (mat_temp_1);
02731
02732
02733 }
02734 else
02735 mat_void (*icasig);
02736
02737 }
02738
02739 mat_delete (D);
02740 mat_delete (E);
02741
02742 mat_delete (mixedsig);
02743 vec_delete (mixedmean);
02744
02745 return 0;
02746 }
02747