File: tour2d_pp.c

package info (click to toggle)
ggobi 2.1.9~20091212-3
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 19,340 kB
  • ctags: 5,083
  • sloc: ansic: 57,242; xml: 30,604; cpp: 833; makefile: 355; java: 225; perl: 201; sh: 122; python: 23
file content (751 lines) | stat: -rw-r--r-- 20,578 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
/* tour2d_pp.c */
/* Copyright (C) 2001 Dianne Cook and Sigbert Klinke and Eun-Kuung Lee

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

The authors can be contacted at the following email addresses:
    dicook@iastate.edu    sigbert@wiwi.hu-berlin.de
*/

#include <gtk/gtk.h>
#ifdef USE_STRINGS_H
#include <strings.h>
#endif

#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#if !defined __APPLE__ && !defined __GNUC__
#endif

#include "vars.h"
#include "externs.h"

#include "tour2d_pp.h"
#include "tour_pp.h"


typedef enum {HOLES, CENTRAL_MASS, LDA, CGINI, CENTROPY}  StandardPPIndexTypes;


#define EXPMINUS1 0.3678794411714423
#define ONEMINUSEXPMINUS1 0.63212056

void t2d_pptemp_set(gfloat slidepos, displayd *dsp, ggobid *gg) {
  dsp->t2d_pp_op.temp_start = slidepos;
}

void t2d_ppcool_set(gfloat slidepos, displayd *dsp, ggobid *gg) {
  dsp->t2d_pp_op.cooling = slidepos;
}

gfloat mean_fn2(gfloat *x1, gfloat *x2, gint n)
{
  gint i;
  gfloat tmean, tmpf1;
  gfloat mean1, mean2;

  tmpf1 = 0.;
  for (i=0; i<n; i++)
    tmpf1 += x1[i];
  mean1 = tmpf1 / (gfloat)n;
  tmpf1 = 0.;
  for (i=0; i<n; i++)
    tmpf1 += x2[i];
  mean2 = tmpf1 / (gfloat)n;
  tmean = 0.;
  for (i=0; i<n; i++) {
    tmean += ((x1[i]-mean1)*(x2[i]-mean2));
  }
  tmean /= ((gfloat)n);
  tmean += (mean1*mean2);

  return(tmean);
}

/*
void
alloc_holes_p(holes_param *hp, gint nrows)
{
  hp->h0 = (gfloat *) g_malloc(
    (guint) nrows*sizeof(gfloat *));
  hp->h1 = (gfloat *) g_malloc(
    (guint) nrows*sizeof(gfloat *));
}

void
free_holes_p(holes_param *hp)
{
  g_free(hp->h0);
  g_free(hp->h1);
}
*/
/********************************************************************
Index          : Holes
Transformation : needs sphered variables/principal components
Purpose        : computes a projection into a normal density fn
Note           : only works for 2d now, could be generalized
*********************************************************************/

/* gint holes(array_f *pdata, void *param, gfloat *val)
{
  holes_param *hp = (holes_param *) param;
  gint i, m;

  * Calculate coefficients *
  for (i=0; i<pdata->nrows; i++)
  {
    m = i;
    hp->h0[m] = exp(-pdata->vals[m][0]*pdata->vals[m][0]/2.) ;
    hp->h1[m] = exp(-pdata->vals[m][1]*pdata->vals[m][1]/2.) ;
  }

  * Calculate index *
  hp->acoefs = mean_fn2(hp->h0,hp->h1,pdata->nrows);
  *val = (1. - hp->acoefs)/(gfloat) ONEMINUSEXPMINUS1 ;
  return(0);
}
*/
/********************************************************************

Index          : Central Mass
Transformation : needs sphered variables/principal components
Purpose        : computes a neg projection into a normal density fn
Note           : only works for 2d now, could be generalized

*********************************************************************/

/* gint
central_mass(array_f *pdata, void *param, gfloat *val)
{
  holes_param *hp = (holes_param *) param;
  gint i, m;

  * Calculate coefficients *
  for (i=0; i<pdata->nrows; i++)
  {
    m = i;
    hp->h0[m] = exp(-pdata->vals[m][0]*pdata->vals[m][0]/2.) ;
    hp->h1[m] = exp(-pdata->vals[m][1]*pdata->vals[m][1]/2.) ;
  }

  * Calculate index *
  hp->acoefs = mean_fn2(hp->h0,hp->h1,pdata->nrows);
  *val = (hp->acoefs - (gfloat)EXPMINUS1)/(float)ONEMINUSEXPMINUS1 ;
  return(0);
}
*/
/***************************************************/
/*  2D Holes index for raw data                    */
/*   holes_raw1                                    */
/*   holes_raw2 : use inverse function             */
/***************************************************/

/* gint holes_raw1(array_f *pdata, void *param, gfloat *val)
{ 
*
   holes_param *hp = (holes_param *) param;
*
   gint i, p=pdata->ncols, n=pdata->nrows;
   gfloat m1, m2, x1, x2, temp;
   gdouble *cov;
   gfloat det,acoefs;

   cov = (gdouble *) g_malloc(p*p*sizeof(gdouble));
   for(i=0; i<(p*p); i++) 
     cov[i] = 0;
   m1=0; 
   m2=0;
   for (i=0; i<n; i++) { 
     m1 += pdata->vals[i][0];
     m2 += pdata->vals[i][1];
   }      
   m1 /= n;
   m2 /= n;

   for (i=0; i<n; i++) {  
     cov[0] += (pdata->vals[i][0]-m1)*(pdata->vals[i][0]-m1)/(n-1);
     cov[1] += (pdata->vals[i][0]-m1)*(pdata->vals[i][1]-m2)/(n-1);
     cov[3] += (pdata->vals[i][1]-m2)*(pdata->vals[i][1]-m2)/(n-1);
   }

   cov[2] = cov[1];
   det = cov[0]*cov[3]-cov[1]*cov[1];
   cov[0] /= det;
   cov[1] /= det;
   cov[2] /= det;
   cov[3] /= det;
   acoefs=0.;

   for(i=0; i<n; i++) {  
     x1 = pdata->vals[i][0]-m1; 
     x2 = pdata->vals[i][1]-m2;
     temp = (cov[3]*x1*x1-2*cov[1]*x1*x2+cov[0]*x2*x2);
     acoefs += exp(-temp/2);
   }

   *val = (1.-acoefs/n)/(gfloat) ONEMINUSEXPMINUS1;

   g_free(cov);
   return(0);
}

gint holes_raw2( array_f *pdata, void *param, gfloat *val)
{ 
   gint i, p=pdata->ncols, n=pdata->nrows;
   gfloat m1, m2,x1,x2,temp;
   gdouble *cov;
*
   holes_param *hp = (holes_param *) param;
   gfloat det;
*
   gfloat acoefs;

   cov = (gdouble *) g_malloc(p*p*sizeof(gdouble));
   for(i=0; i<(p*p); i++) cov[i] = 0;
   m1=0; m2=0;
   for(i=0; i<n; i++)
   { m1 += pdata->vals[i][0]/n;
     m2 += pdata->vals[i][1]/n;
   }      
   for(i=0; i<n; i++)
     {  cov[0] += (pdata->vals[i][0]-m1)*(pdata->vals[i][0]-m1)/(n-1);
        cov[1] += (pdata->vals[i][0]-m1)*(pdata->vals[i][1]-m2)/(n-1);
        cov[3] += (pdata->vals[i][1]-m2)*(pdata->vals[i][1]-m2)/(n-1);
     }
   cov[2]= cov[1];
   inverse(cov,p);
   acoefs=0.;

   for(i=0; i<n; i++)
     {  x1 = pdata->vals[i][0]-m1; 
        x2=pdata->vals[i][1]-m2;
        temp= cov[0]*x1*x1+(cov[1]+cov[2])*x1*x2+cov[3]*x2*x2;
        acoefs +=exp(-temp/2);
   }

   *val = (1.-acoefs/n)/(gfloat) ONEMINUSEXPMINUS1;
   free(cov);
   return(0);
}
*/

/**********************************************************/
/*  2D Central Mass index for raw data                    */
/*   central_mass_raw1                                    */
/*   central_mass_raw2 : use inverse function             */
/**********************************************************/

/* gint central_mass_raw1(array_f *pdata, void *param, gfloat *val)
{
*
   holes_param *hp = (holes_param *) param;
*
   gint i, p=pdata->ncols, n=pdata->nrows;
   gfloat m1, m2,x1,x2,temp;
   gdouble *cov;
   gfloat det,acoefs;

   cov = (gdouble *) g_malloc(p*p*sizeof(gdouble));
   for(i=0; i<(p*p); i++) cov[i] = 0;
   m1=0; m2=0;
   for(i=0; i<n; i++)
   { m1 += pdata->vals[i][0];
     m2 += pdata->vals[i][1];
   }      
   m1 /= n;
   m2 /= n;

   for(i=0; i<n; i++)
     {  cov[0] += (pdata->vals[i][0]-m1)*(pdata->vals[i][0]-m1)/(n-1);
        cov[1] += (pdata->vals[i][0]-m1)*(pdata->vals[i][1]-m2)/(n-1);
        cov[3] += (pdata->vals[i][1]-m2)*(pdata->vals[i][1]-m2)/(n-1);
     }

   cov[2]= cov[1];
   det = cov[0]*cov[3]-cov[1]*cov[1];
   acoefs=0.;

   for(i=0; i<n; i++)
     {  x1 = pdata->vals[i][0]-m1; 
        x2=pdata->vals[i][1]-m2;
        temp= (cov[3]*x1*x1-2*cov[1]*x1*x2+cov[0]*x2*x2)/det;
        acoefs +=exp(-temp/2);
   }

   *val = (acoefs/n-(gfloat)EXPMINUS1)/(gfloat) ONEMINUSEXPMINUS1;
   free(cov);
   return(0);

}

gint central_mass_raw2(array_f *pdata, void *param, gfloat *val)
{
   *holes_param *hp = (holes_param *) param;*
   gint i, p=pdata->ncols, n=pdata->nrows;
   gfloat m1, m2,x1,x2,temp;
   gdouble *cov;
   gfloat acoefs;

   cov = (gdouble *) g_malloc(p*p*sizeof(gdouble));
   for(i=0; i<(p*p); i++) cov[i] = 0;
   m1=0; m2=0;
   for(i=0; i<n; i++)
   { m1 += pdata->vals[i][0]/n;
     m2 += pdata->vals[i][1]/n;
   }      
   for(i=0; i<n; i++)
     {  cov[0] += (pdata->vals[i][0]-m1)*(pdata->vals[i][0]-m1)/(n-1);
        cov[1] += (pdata->vals[i][0]-m1)*(pdata->vals[i][1]-m2)/(n-1);
        cov[3] += (pdata->vals[i][1]-m2)*(pdata->vals[i][1]-m2)/(n-1);
     }
   cov[2]= cov[1];
   inverse(cov,p);
   acoefs=0.;

   for(i=0; i<n; i++)
     {  x1 = pdata->vals[i][0]-m1; 
        x2=pdata->vals[i][1]-m2;
        temp= cov[0]*x1*x1-(cov[1]+cov[2])*x1*x2+cov[3]*x2*x2;
        acoefs +=exp(-temp/2);
   }
   *val = (acoefs/n-(gfloat)EXPMINUS1)/(gfloat) ONEMINUSEXPMINUS1;
   free(cov);
   return(0);

}
*/
/*void
holes_deriv(holes_param *hp, gfloat **data, gfloat **pdata)
{
  gint i, k, m;
  gfloat tmpf;

  for (i=0; i<2; i++)
    for (k=0; k<hp->ncols; k++)
      hp->derivs[i][k] = 0.;

* alpha *
  for (k=0; k<hp->ncols; k++)
  {
    tmpf = 0.;
    for (i=0; i<hp->nrows; i++)
    {
      m = i;
      tmpf += (pdata[0][m]*hp->h0[m]*hp->h1[m]*
        (data[m][k] -
        hp->alpha[k]*pdata[0][m] -
        hp->beta[k]*pdata[1][m]));
    }
    tmpf /= ((float)hp->nrows);
    hp->derivs[0][k] = tmpf;
  }

* beta *
  for (k=0; k<hp->ncols; k++)
  {
    tmpf = 0.;
    for (i=0; i<hp->nrows; i++)
    {
      m = i;
      tmpf += (pdata[1][m]*hp->h0[m]*hp->h1[m]*
        (data[m][k] -
        hp->alpha[k]*pdata[0][m] -
        hp->beta[k]*pdata[1][m]));
    }
    tmpf /= ((float)hp->nrows);
    hp->derivs[1][k] = tmpf;
  }

}
*/


/*void
central_mass_deriv(float **data, float **proj_data, float *alpha, float *beta,
float **derivs, int n, int *rows_in_plot, int p, int nactive, int *active_vars)
{
  int i, k, m;
  float tmpf;

  for (i=0; i<2; i++)
    for (k=0; k<p; k++)
      derivs[i][k] = 0.;

  for (k=0; k<nactive; k++)
  {
    tmpf = 0.;
    for (i=0; i<n; i++)
    {
      m = rows_in_plot[i];
      tmpf -= (proj_data[0][m]*hp->h0[m]*hp->h1[m]*
        (data[m][active_vars[k]] -
        alpha[active_vars[k]]*proj_data[0][m] -
        beta[active_vars[k]]*proj_data[1][m]));
    }
    tmpf /= ((float)n);
    derivs[0][active_vars[k]] = tmpf;
  }

  for (k=0; k<nactive; k++)
  {
    tmpf = 0.;
    for (i=0; i<n; i++)
    {
      m = rows_in_plot[i];
      tmpf -= (proj_data[1][m]*hp->h0[m]*hp->h1[m]*
        (data[m][active_vars[k]] -
        alpha[active_vars[k]]*proj_data[0][m] -
        beta[active_vars[k]]*proj_data[1][m]));
    }
    tmpf /= ((float)n);
    derivs[1][active_vars[k]] = tmpf;
  }

}*/

/* This function interacts with control  buttons in ggobi */
void t2d_optimz(gint optimz_on, gboolean *nt, gint *bm, displayd *dsp) {
  gboolean new_target = *nt;
  gint bas_meth = *bm;
  gint i, j;

  if (optimz_on) 
  {
    for (i=0; i<2; i++)
      for (j=0; j<dsp->t2d.nactive; j++)
        dsp->t2d_pp_op.proj_best.vals[i][j] = 
          dsp->t2d.F.vals[i][dsp->t2d.active_vars.els[j]];
    /*    dsp->t2d.ppval = dsp->t2d_indx_min;*/
    dsp->t2d_pp_op.index_best = dsp->t2d.ppval;
    bas_meth = 1;
  }
  else
    bas_meth = 0;

  new_target = true;

  *nt = new_target;
  *bm = bas_meth;
}

void t2d_clear_pppixmap(displayd *dsp, ggobid *gg)
{
  colorschemed *scheme = gg->activeColorScheme;
  gint margin=10;
  gint wid = dsp->t2d_ppda->allocation.width, 
    hgt = dsp->t2d_ppda->allocation.height;

  /* clear the pixmap */
  gdk_gc_set_foreground (gg->plot_GC, &scheme->rgb_bg);
  gdk_draw_rectangle (dsp->t2d_pp_pixmap, gg->plot_GC,
                      true, 0, 0, wid, hgt);

  gdk_gc_set_foreground (gg->plot_GC, &scheme->rgb_accent);
  gdk_draw_line (dsp->t2d_pp_pixmap, gg->plot_GC,
    margin, hgt - margin,
    wid - margin, hgt - margin);
  gdk_draw_line (dsp->t2d_pp_pixmap, gg->plot_GC,
    margin, hgt - margin, margin, margin);

  gdk_draw_pixmap (dsp->t2d_ppda->window, gg->plot_GC, dsp->t2d_pp_pixmap,
                   0, 0, 0, 0,
                   wid, hgt);
}

void t2d_clear_ppda(displayd *dsp, ggobid *gg)
{
  gint i;

  /* clear the ppindx matrix */
  dsp->t2d_ppindx_count = 0;
  dsp->t2d_indx_min=1000.;
  dsp->t2d_indx_max=-1000.;
  for (i=0; i<100; i++) 
  {
    dsp->t2d_ppindx_mat[i] = 0.0;
  }

  t2d_clear_pppixmap(dsp, gg);
}

void t2d_ppdraw_all(gint wid, gint hgt, gint margin, displayd *dsp, ggobid *gg)
{
  GdkPoint pptrace[100];
  gint i;

  t2d_clear_pppixmap(dsp, gg);

  for (i=0; i<dsp->t2d_ppindx_count; i++) 
  {
    pptrace[i].x = margin+i*2;
    pptrace[i].y = hgt-margin-(gint)((gfloat)((dsp->t2d_ppindx_mat[i]-
      dsp->t2d_indx_min)/(gfloat) (dsp->t2d_indx_max-dsp->t2d_indx_min)) * 
      (gfloat) (hgt - 2*margin));
  }
  gdk_draw_lines (dsp->t2d_pp_pixmap, gg->plot_GC,
    pptrace, dsp->t2d_ppindx_count);

  gdk_draw_pixmap (dsp->t2d_ppda->window, gg->plot_GC, dsp->t2d_pp_pixmap,
    0, 0, 0, 0, wid, hgt);

}

/* This is writes text to the pp window to inform the
user that optimize is finding a new maximum */ 
void t2d_ppdraw_think(displayd *dsp, ggobid *gg)
{
  splotd *sp = (splotd *) g_list_nth_data (dsp->splots, 0);
  colorschemed *scheme = gg->activeColorScheme;
  gint wid = dsp->t2d_ppda->allocation.width, 
    hgt = dsp->t2d_ppda->allocation.height;
  PangoLayout *layout = gtk_widget_create_pango_layout(sp->da, "Thinking...");
  
  gdk_gc_set_foreground (gg->plot_GC, &scheme->rgb_accent);
  gdk_draw_layout(dsp->t2d_pp_pixmap, gg->plot_GC, 10, 10, layout);
  g_object_unref(G_OBJECT(layout));
  /*gdk_text_extents (
    gtk_style_get_font (style),
    varlab, strlen (varlab),
    &lbearing, &rbearing, &width, &ascent, &descent);
    gdk_draw_string (dsp->t2d_pp_pixmap,
    gtk_style_get_font (style),
     gg->plot_GC, 10, 10, varlab);*/
  gdk_draw_pixmap (dsp->t2d_ppda->window, gg->plot_GC, dsp->t2d_pp_pixmap,
    0, 0, 0, 0, wid, hgt);
}

/* This is the pp index plot drawing routine */ 
void t2d_ppdraw(gfloat pp_indx_val, displayd *dsp, ggobid *gg)
{
  colorschemed *scheme = gg->activeColorScheme;
  gint margin=10;
  gint wid = dsp->t2d_ppda->allocation.width, 
    hgt = dsp->t2d_ppda->allocation.height;
  gint j;
  static gboolean init = true;
  gchar *label = g_strdup("PP index: (0.0) 0.0000 (0.0)");

  if (init) {
    t2d_clear_ppda(dsp, gg);
    init = false;
  }

  dsp->t2d_ppindx_mat[dsp->t2d_ppindx_count] = pp_indx_val;

  if (dsp->t2d_indx_min > pp_indx_val)
      dsp->t2d_indx_min = pp_indx_val;
  if (dsp->t2d_indx_max < pp_indx_val)
    dsp->t2d_indx_max = pp_indx_val;

  if (dsp->t2d_indx_min == dsp->t2d_indx_max) dsp->t2d_indx_min *= 0.9999;

  label = g_strdup_printf ("PP index: (%3.1f) %5.3f (%3.1f)",
    dsp->t2d_indx_min, dsp->t2d_ppindx_mat[dsp->t2d_ppindx_count], dsp->t2d_indx_max);
  gtk_label_set_text(GTK_LABEL(dsp->t2d_pplabel),label);

  gdk_gc_set_foreground (gg->plot_GC, &scheme->rgb_accent);
  if (dsp->t2d_ppindx_count == 0) 
  {
    dsp->t2d_ppindx_count++;
  }
  else if (dsp->t2d_ppindx_count > 0 && dsp->t2d_ppindx_count < 80) {
    t2d_ppdraw_all(wid, hgt, margin, dsp, gg);
    dsp->t2d_ppindx_count++;
  }
  else if (dsp->t2d_ppindx_count >= 80) 
  {
    /* cycle values back into array */
    for (j=0; j<=dsp->t2d_ppindx_count; j++)
      dsp->t2d_ppindx_mat[j] = dsp->t2d_ppindx_mat[j+1];
    t2d_ppdraw_all(wid, hgt, margin, dsp, gg);
  }

  g_free (label);
}

void t2d_pp_reinit(displayd *dsp, ggobid *gg)
{
  gint i, j;
  gchar *label = g_strdup("PP index: (0.0) 0.0000 (0.0)");

  for (i=0; i<dsp->t2d_pp_op.proj_best.nrows; i++)
    for (j=0; j<dsp->t2d_pp_op.proj_best.ncols; j++)
      dsp->t2d_pp_op.proj_best.vals[i][j] = 
	  dsp->t2d.F.vals[i][dsp->t2d.active_vars.els[j]];
  dsp->t2d.ppval = 0.0;
  dsp->t2d.oppval = -1.0;
  dsp->t2d_pp_op.index_best = 0.0;
  label = g_strdup_printf ("PP index: (%3.1f) %5.3f (%3.1f)",
			   dsp->t2d_indx_min, dsp->t2d_ppindx_mat[dsp->t2d_ppindx_count], 
			   dsp->t2d_indx_max);
  gtk_label_set_text(GTK_LABEL(dsp->t2d_pplabel),label);

  t2d_clear_ppda(dsp, gg);
  g_free (label);
}

/********************************************************************

                         INDEX CALCULATION

The index function has to be defined as

     gint index (array_f *pdata, void *param, gfloat *val)

with   

Input:  pdata   projected data
        param   additional parameters for the index 
                (will not be touched by the optimization routine)

Output: val     the index-value
        
This function should simply calculate the index value for a provided
projection.

*********************************************************************/

gfloat t2d_calc_indx (array_f pd,
                Tour_PPIndex_f index,
                void *param)
{ 
  gfloat indexval;

  index (&pd, param, &indexval, NULL);

  return(indexval);
}

/* 
 We could call this with the tour t2d object rather than the index type
 since we no longer need the indxtype 
*/
gboolean
t2d_switch_index(Tour2DCPanel controls, gint basismeth, displayd *dsp,
  ggobid *gg)
{
  GGobiData *d = dsp->d;
  gint kout, nrows = d->nrows_in_plot;
  gfloat *gdata;
  gint i, j, k;

  if (d->nrows_in_plot == 1)  /* can't do pp on no data! */
    return(false);

  /* Copy data into pp opt'n data */
  for (i=0; i<d->nrows_in_plot; i++)
    for (j=0; j<dsp->t2d.nactive; j++)
      dsp->t2d_pp_op.data.vals[i][j] = 
        d->tform.vals[d->rows_in_plot.els[i]][dsp->t2d.active_vars.els[j]];

  /* Copy current projection into opt'n projection */
  for (i=0; i<2; i++)
    for (j=0; j<dsp->t2d.nactive; j++) 
      dsp->t2d_pp_op.proj_best.vals[i][j] = 
        dsp->t2d.F.vals[i][dsp->t2d.active_vars.els[j]];

  for (k=0; k<2; k++) 
    for (i=0; i<d->nrows_in_plot; i++) {
      dsp->t2d_pp_op.pdata.vals[i][k] = 
          (d->tform.vals[d->rows_in_plot.els[i]][dsp->t2d.active_vars.els[0]]*
          dsp->t2d_pp_op.proj_best.vals[k][0]);
      for (j=1; j<dsp->t2d.nactive; j++)
        dsp->t2d_pp_op.pdata.vals[i][k] += 
          (d->tform.vals[d->rows_in_plot.els[i]][dsp->t2d.active_vars.els[j]]*
          dsp->t2d_pp_op.proj_best.vals[k][j]);
    }

  gdata  = g_malloc (nrows*sizeof(gfloat));
  if (d->clusterid.els==NULL) printf ("No cluster information found\n");
  for (i = 0 ; i < nrows; i++)
  { 
    if (d->clusterid.els!=NULL)
      gdata[i] = d->clusterid.els[d->rows_in_plot.els[i]];
    else
      gdata[i] = 0;
  }

  if(controls.ppindex.index_f) {
      if(controls.ppindex.checkGroups == false || 
           !compute_groups (dsp->t2d_pp_param.group, dsp->t2d_pp_param.ngroup, &dsp->t2d_pp_param.numgroups, nrows, gdata)) 
      { 
	  controls.ppindex.index_f(&dsp->t2d_pp_op.pdata, &dsp->t2d_pp_param,  &dsp->t2d.ppval, controls.ppindex.userData);
	  if(basismeth == 1) 
	      kout = optimize0 (&dsp->t2d_pp_op, controls.ppindex.index_f, &dsp->t2d_pp_param);
      }
  }

#if 0
  switch (indxtype)
  { 
    case HOLES: 
      dsp->t2d.ppval = t2d_calc_indx (dsp->t2d_pp_op.pdata, 
				      holes_raw, &dsp->t2d_pp_param);
      if (basismeth == 1) {
        kout = optimize0 (&dsp->t2d_pp_op, holes_raw, &dsp->t2d_pp_param);
      }
    break;
    case CENTRAL_MASS: 
      dsp->t2d.ppval = t2d_calc_indx (dsp->t2d_pp_op.pdata,
				      central_mass_raw, &dsp->t2d_pp_param);
      if (basismeth == 1)
        kout = optimize0 (&dsp->t2d_pp_op, central_mass_raw, &dsp->t2d_pp_param);
    break;
    case LDA: 
      if (!compute_groups (dsp->t2d_pp_param.group, dsp->t2d_pp_param.ngroup, 
        &dsp->t2d_pp_param.numgroups, nrows, gdata)) {
        dsp->t2d.ppval = t2d_calc_indx (dsp->t2d_pp_op.pdata,
					discriminant, &dsp->t2d_pp_param);
        if (basismeth == 1)
          kout = optimize0 (&dsp->t2d_pp_op, discriminant, &dsp->t2d_pp_param);
      }
      break;
    case CGINI: 
      if (!compute_groups (dsp->t2d_pp_param.group, dsp->t2d_pp_param.ngroup, 
        &dsp->t2d_pp_param.numgroups, nrows, 
			   gdata)) {
        dsp->t2d.ppval = t2d_calc_indx (dsp->t2d_pp_op.pdata,
					cartgini, &dsp->t2d_pp_param);
        if (basismeth == 1)
          kout = optimize0 (&dsp->t2d_pp_op, cartgini, &dsp->t2d_pp_param);
      }
      break;
    case CENTROPY: 
      if (!compute_groups (dsp->t2d_pp_param.group, dsp->t2d_pp_param.ngroup, 
        &dsp->t2d_pp_param.numgroups, nrows, 
			   gdata)) {
        dsp->t2d.ppval = t2d_calc_indx (dsp->t2d_pp_op.pdata,
          cartentropy, &dsp->t2d_pp_param);
        if (basismeth == 1)
          kout = optimize0 (&dsp->t2d_pp_op, cartentropy, &dsp->t2d_pp_param);
      }
      break;
    break;
    default: 
/* Shouldn't we free gdata. */
      return(true);
    break;
  }
#endif

  g_free (gdata);
  return(false);
}


#undef ONEMINUSEXPMINUS1
#undef EXPMINUS1