apriori-rails 0.2.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (122) hide show
  1. data/History.txt +22 -0
  2. data/License.txt +20 -0
  3. data/Manifest.txt +121 -0
  4. data/README.txt +149 -0
  5. data/Rakefile +17 -0
  6. data/TODO.txt +60 -0
  7. data/attic/c_ext_test1/MyTest/MyTest.c +23 -0
  8. data/attic/c_ext_test1/MyTest/extconf.rb +11 -0
  9. data/attic/c_ext_test1/mytest.rb +10 -0
  10. data/attic/test.c +12 -0
  11. data/config/hoe.rb +88 -0
  12. data/config/requirements.rb +29 -0
  13. data/examples/01_simple_example.rb +39 -0
  14. data/examples/02_small_file_example.rb +17 -0
  15. data/examples/03_large_file_example.rb +22 -0
  16. data/examples/test_data/market_basket_basic_test.dat +9 -0
  17. data/ext/Apriori.c +149 -0
  18. data/ext/Makefile +149 -0
  19. data/ext/apriori/doc/apriori.html +1301 -0
  20. data/ext/apriori/doc/arem.gp +68 -0
  21. data/ext/apriori/doc/c_rev.gp +89 -0
  22. data/ext/apriori/doc/chi2.tex +156 -0
  23. data/ext/apriori/doc/copying +504 -0
  24. data/ext/apriori/doc/line.gif +0 -0
  25. data/ext/apriori/doc/uparrow.gif +0 -0
  26. data/ext/apriori/ex/flg2set +15 -0
  27. data/ext/apriori/ex/hdr2set +13 -0
  28. data/ext/apriori/ex/readme +71 -0
  29. data/ext/apriori/ex/row2set +7 -0
  30. data/ext/apriori/ex/rulesort +24 -0
  31. data/ext/apriori/ex/tab2set +9 -0
  32. data/ext/apriori/ex/test.app +2 -0
  33. data/ext/apriori/ex/test.rul +9 -0
  34. data/ext/apriori/ex/test1.rul +43 -0
  35. data/ext/apriori/ex/test1.tab +10 -0
  36. data/ext/apriori/ex/test2.tab +10 -0
  37. data/ext/apriori/ex/test3.tab +30 -0
  38. data/ext/apriori/ex/test4.tab +11 -0
  39. data/ext/apriori/ex/test5.tab +39 -0
  40. data/ext/apriori/ex/tid2set +23 -0
  41. data/ext/apriori/ex/xhdr2set +33 -0
  42. data/ext/apriori/src/apriori.c +750 -0
  43. data/ext/apriori/src/apriori.dsp +120 -0
  44. data/ext/apriori/src/apriori.dsw +29 -0
  45. data/ext/apriori/src/apriori.mak +99 -0
  46. data/ext/apriori/src/istree.c +1411 -0
  47. data/ext/apriori/src/istree.h +160 -0
  48. data/ext/apriori/src/makefile +105 -0
  49. data/ext/apriori/src/tract.c +870 -0
  50. data/ext/apriori/src/tract.h +261 -0
  51. data/ext/apriori_wrapper.c +757 -0
  52. data/ext/apriori_wrapper.h +10 -0
  53. data/ext/extconf.rb +32 -0
  54. data/ext/math/doc/copying +504 -0
  55. data/ext/math/src/chi2.c +151 -0
  56. data/ext/math/src/chi2.h +27 -0
  57. data/ext/math/src/choose.c +71 -0
  58. data/ext/math/src/choose.h +16 -0
  59. data/ext/math/src/gamma.c +446 -0
  60. data/ext/math/src/gamma.h +39 -0
  61. data/ext/math/src/intexp.c +35 -0
  62. data/ext/math/src/intexp.h +15 -0
  63. data/ext/math/src/makefile +164 -0
  64. data/ext/math/src/math.mak +48 -0
  65. data/ext/math/src/normal.c +387 -0
  66. data/ext/math/src/normal.h +44 -0
  67. data/ext/math/src/radfn.c +113 -0
  68. data/ext/math/src/radfn.h +34 -0
  69. data/ext/math/src/zeta.c +49 -0
  70. data/ext/math/src/zeta.h +15 -0
  71. data/ext/pre-clean.rb +8 -0
  72. data/ext/pre-setup.rb +9 -0
  73. data/ext/util/doc/copying +504 -0
  74. data/ext/util/src/listops.c +76 -0
  75. data/ext/util/src/listops.h +26 -0
  76. data/ext/util/src/makefile +103 -0
  77. data/ext/util/src/memsys.c +84 -0
  78. data/ext/util/src/memsys.h +42 -0
  79. data/ext/util/src/nstats.c +288 -0
  80. data/ext/util/src/nstats.h +69 -0
  81. data/ext/util/src/params.c +86 -0
  82. data/ext/util/src/params.h +19 -0
  83. data/ext/util/src/parse.c +133 -0
  84. data/ext/util/src/parse.h +81 -0
  85. data/ext/util/src/scan.c +767 -0
  86. data/ext/util/src/scan.h +111 -0
  87. data/ext/util/src/symtab.c +443 -0
  88. data/ext/util/src/symtab.h +121 -0
  89. data/ext/util/src/tabscan.c +279 -0
  90. data/ext/util/src/tabscan.h +99 -0
  91. data/ext/util/src/util.mak +91 -0
  92. data/ext/util/src/vecops.c +317 -0
  93. data/ext/util/src/vecops.h +42 -0
  94. data/lib/apriori.rb +133 -0
  95. data/lib/apriori/adapter.rb +13 -0
  96. data/lib/apriori/association_rule.rb +97 -0
  97. data/lib/apriori/version.rb +3 -0
  98. data/script/console +10 -0
  99. data/script/destroy +14 -0
  100. data/script/generate +14 -0
  101. data/script/txt2html +82 -0
  102. data/setup.rb +1585 -0
  103. data/tasks/apriori.rake +20 -0
  104. data/tasks/attic.rake +28 -0
  105. data/tasks/deployment.rake +34 -0
  106. data/tasks/environment.rake +7 -0
  107. data/tasks/install.rake +13 -0
  108. data/tasks/website.rake +17 -0
  109. data/test/apriori_test.rb +13 -0
  110. data/test/fixtures/market_basket_results_test.txt +5 -0
  111. data/test/fixtures/market_basket_string_test.txt +7 -0
  112. data/test/fixtures/results.txt +2 -0
  113. data/test/fixtures/sample.txt +7 -0
  114. data/test/test_helper.rb +5 -0
  115. data/test/unit/test_apriori.rb +68 -0
  116. data/test/unit/test_itemsets_and_parsing.rb +82 -0
  117. data/website/index.html +251 -0
  118. data/website/index.txt +154 -0
  119. data/website/javascripts/rounded_corners_lite.inc.js +285 -0
  120. data/website/stylesheets/screen.css +142 -0
  121. data/website/template.html.erb +49 -0
  122. metadata +267 -0
@@ -0,0 +1,76 @@
1
+ /*----------------------------------------------------------------------
2
+ File : listops.c
3
+ Contents: some special list operations
4
+ Author : Christian Borgelt
5
+ History : 2000.11.02 file created from file lists.h
6
+ ----------------------------------------------------------------------*/
7
+ #include <stdio.h>
8
+ #include "listops.h"
9
+
10
+ /*----------------------------------------------------------------------
11
+ Functions
12
+ ----------------------------------------------------------------------*/
13
+
14
+ void* l_sort (void *list, LCMPFN cmpfn, void *data)
15
+ { /* --- sort a list with mergesort */
16
+ LE *src, *dst; /* list of source/destination lists */
17
+ LE **end; /* end of list of destination lists */
18
+ LE *in1, *in2; /* input lists for merging */
19
+ LE **out; /* output list for merging */
20
+
21
+ if (!list) return list; /* check for an empty list */
22
+ for (src = list; src->succ; ) {
23
+ dst = src; src = src->succ; /* traverse the list and split it */
24
+ dst->succ = NULL; /* into a list (abused pred ptr.) of */
25
+ } /* single element lists (succ ptr.) */
26
+ while (src->pred) { /* while more than one source list */
27
+ end = &dst; /* start list of destination lists */
28
+ do { /* merge pairs of source lists */
29
+ out = end; /* start output list (merged input) */
30
+ in1 = src; /* remove two (one) source list(s) */
31
+ in2 = src->pred; /* and use them as input for merging */
32
+ if (!in2) { /* if there is only one source list */
33
+ *end = in1; end = &in1->pred;
34
+ break; /* append it to the list of */
35
+ } /* output list and abort the loop */
36
+ src = in2->pred; /* remove lists from list of sources */
37
+ while (1) { /* source lists merge loop */
38
+ if (cmpfn(in1, in2, data) < 0) {
39
+ /* if first list's element is smaller */
40
+ *out = in1; /* move element to output list, */
41
+ out = &(in1->succ); /* advance output pointer and */
42
+ in1 = in1->succ; /* remove element from input list */
43
+ if (!in1) break; } /* if the list gets empty, abort loop */
44
+ else { /* if second list's element is smaller */
45
+ *out = in2; /* move element to output list, */
46
+ out = &(in2->succ); /* advance output pointer and */
47
+ in2 = in2->succ; /* remove element from input list */
48
+ if (!in2) break; /* if the list gets empty, abort loop */
49
+ } /* (merge input lists into one) */
50
+ }
51
+ if (in1) *out = in1; /* append remaining elements */
52
+ else *out = in2; /* to the output list */
53
+ end = &(*end)->pred; /* advance destination list pointer */
54
+ } while (src); /* while there is another source list */
55
+ *end = NULL; /* terminate destination list */
56
+ src = dst; /* transfer destination list */
57
+ } /* to source list and start over */
58
+ for (src->pred = NULL; src->succ; src = src->succ)
59
+ src->succ->pred = src; /* set predecessor pointers */
60
+ return dst; /* return a pointer to the first */
61
+ } /* l_sort() */ /* element of the sorted list */
62
+
63
+ /*--------------------------------------------------------------------*/
64
+
65
+ void* l_reverse (void *list)
66
+ { /* --- reverse a list */
67
+ LE *le = NULL; /* to traverse the list elements */
68
+
69
+ while (list) { /* while the list is not empty */
70
+ le = list; /* get the next list element */
71
+ list = le->succ; /* exchange the successor */
72
+ le->succ = le->pred; /* and the predecessor */
73
+ le->pred = list; /* of the list element */
74
+ }
75
+ return le; /* return a pointer to */
76
+ } /* l_reverse() */ /* the new first element */
@@ -0,0 +1,26 @@
1
+ /*----------------------------------------------------------------------
2
+ File : listops.h
3
+ Contents: some special list operations
4
+ Author : Christian Borgelt
5
+ History : 2000.11.02 file created from file lists.h
6
+ ----------------------------------------------------------------------*/
7
+ #ifndef __LISTOPS__
8
+ #define __LISTOPS__
9
+
10
+ /*----------------------------------------------------------------------
11
+ Type Definitions
12
+ ----------------------------------------------------------------------*/
13
+ typedef struct _le { /* --- a list element --- */
14
+ struct _le *succ; /* pointer to successor */
15
+ struct _le *pred; /* pointer to predecessor */
16
+ } LE; /* (list element) */
17
+
18
+ typedef int LCMPFN (const void *p1, const void *p2, void *data);
19
+
20
+ /*----------------------------------------------------------------------
21
+ Functions
22
+ ----------------------------------------------------------------------*/
23
+ extern void* l_sort (void *list, LCMPFN cmpfn, void *data);
24
+ extern void* l_reverse (void *list);
25
+
26
+ #endif
@@ -0,0 +1,103 @@
1
+ #-----------------------------------------------------------------------
2
+ # File : makefile (directory: util)
3
+ # Contents: build utility modules
4
+ # Author : Christian Borgelt
5
+ # History : 2000.11.04 file created from makefile in table directory
6
+ # 2003.06.05 module params added
7
+ # 2003.08.12 module nstats added
8
+ # 2004.12.10 module memsys added
9
+ #-----------------------------------------------------------------------
10
+ CC = gcc
11
+ CFBASE = -ansi -Wall -pedantic $(ADDFLAGS)
12
+ CFLAGS = $(CFBASE) -DNDEBUG -O3
13
+ # CFLAGS = $(CFBASE) -g
14
+ # CFLAGS = $(CFBASE) -g -DSTORAGE $(ADDINC)
15
+ # ADDINC = -I../../misc/src
16
+ INC = -I. -I$(TABLEDIR)
17
+
18
+ #-----------------------------------------------------------------------
19
+ # Build Modules
20
+ #-----------------------------------------------------------------------
21
+ all: memsys.o vecops.o listops.o symtab.o nimap.o \
22
+ tabscan.o scform.o scan.o parse.o params.o
23
+
24
+ #-----------------------------------------------------------------------
25
+ # Memory Management System for Equally Sized Objects
26
+ #-----------------------------------------------------------------------
27
+ memsys.o: memsys.h
28
+ memsys.o: memsys.c makefile
29
+ $(CC) $(CFLAGS) -c memsys.c -o $@
30
+
31
+ #-----------------------------------------------------------------------
32
+ # Vector Operations
33
+ #-----------------------------------------------------------------------
34
+ vecops.o: vecops.h
35
+ vecops.o: vecops.c makefile
36
+ $(CC) $(CFLAGS) -c vecops.c -o $@
37
+
38
+ #-----------------------------------------------------------------------
39
+ # List Operations
40
+ #-----------------------------------------------------------------------
41
+ listops.o: listops.h
42
+ listops.o: listops.c makefile
43
+ $(CC) $(CFLAGS) -c listops.c -o $@
44
+
45
+ #-----------------------------------------------------------------------
46
+ # Symbol Table Management
47
+ #-----------------------------------------------------------------------
48
+ symtab.o: symtab.h
49
+ symtab.o: symtab.c makefile
50
+ $(CC) $(CFLAGS) -c symtab.c -o $@
51
+
52
+ nimap.o: symtab.h vecops.h
53
+ nimap.o: symtab.c makefile
54
+ $(CC) $(CFLAGS) -DNIMAPFN -c symtab.c -o $@
55
+
56
+ #-----------------------------------------------------------------------
57
+ # Numerical Statistics Management
58
+ #-----------------------------------------------------------------------
59
+ nstats.o: nstats.h
60
+ nstats.o: nstats.c makefile
61
+ $(CC) $(CFLAGS) -c nstats.c -o $@
62
+
63
+ nst_pars.o: nstats.h
64
+ nst_pars.o: nstats.c makefile
65
+ $(CC) $(CFLAGS) -DNST_PARSE -c nstats.c -o $@
66
+
67
+ #-----------------------------------------------------------------------
68
+ # Table Scanner Management
69
+ #-----------------------------------------------------------------------
70
+ tabscan.o: tabscan.h
71
+ tabscan.o: tabscan.c makefile
72
+ $(CC) $(CFLAGS) -c tabscan.c -o $@
73
+
74
+ #-----------------------------------------------------------------------
75
+ # Scanner
76
+ #-----------------------------------------------------------------------
77
+ scform.o: scan.h
78
+ scform.o: scan.c makefile
79
+ $(CC) $(CFLAGS) -c scan.c -o $@
80
+
81
+ scan.o: scan.h
82
+ scan.o: scan.c makefile
83
+ $(CC) $(CFLAGS) -DSC_SCAN -c scan.c -o $@
84
+
85
+ #-----------------------------------------------------------------------
86
+ # Parser Utilities
87
+ #-----------------------------------------------------------------------
88
+ parse.o: parse.h
89
+ parse.o: parse.c makefile
90
+ $(CC) $(CFLAGS) -c parse.c -o $@
91
+
92
+ #-----------------------------------------------------------------------
93
+ # Command Line Parameter Retrieval
94
+ #-----------------------------------------------------------------------
95
+ params.o: params.h
96
+ params.o: params.c makefile
97
+ $(CC) $(CFLAGS) -c params.c -o $@
98
+
99
+ #-----------------------------------------------------------------------
100
+ # Clean up
101
+ #-----------------------------------------------------------------------
102
+ clean:
103
+ rm -f *.o *~ *.flc core
@@ -0,0 +1,84 @@
1
+ /*----------------------------------------------------------------------
2
+ File : memsys.c
3
+ Contents: memory management system for equally sized (small) objects
4
+ Author : Christian Borgelt
5
+ History : 2004.12.10 file created from fpgrowth.c
6
+ 2008.01.23 counting of used objects added
7
+ ----------------------------------------------------------------------*/
8
+ #include <stdio.h>
9
+ #include <stdlib.h>
10
+ #include <assert.h>
11
+ #include "memsys.h"
12
+ #ifdef STORAGE
13
+ #include "storage.h"
14
+ #endif
15
+
16
+ /*----------------------------------------------------------------------
17
+ Main Functions
18
+ ----------------------------------------------------------------------*/
19
+
20
+ MEMSYS* ms_create (int size, int cnt)
21
+ { /* --- create a memory system */
22
+ MEMSYS *ms; /* created memory system */
23
+
24
+ assert((cnt > 0) /* check the function arguments */
25
+ && (size > 0) && (size % sizeof(void*) == 0));
26
+ ms = malloc(sizeof(MEMSYS)); /* create a memory management system */
27
+ if (!ms) return NULL; /* with no free objects and */
28
+ ms->cnt = cnt; /* initialize the variables */
29
+ ms->size = size /sizeof(void*);
30
+ ms->blksz = sizeof(MSBLOCK) +ms->cnt *size;
31
+ ms->free = ms->blocks = NULL;
32
+ return ms; /* return the created memory system */
33
+ } /* ms_create() */
34
+
35
+ /*--------------------------------------------------------------------*/
36
+
37
+ void ms_delete (MEMSYS *ms)
38
+ { /* --- delete a memory system */
39
+ MSBLOCK *block; /* to traverse the memory blocks */
40
+
41
+ assert(ms); /* check the function argument */
42
+ while (ms->blocks) { /* while there is another block */
43
+ block = ms->blocks; /* note the memory block and */
44
+ ms->blocks = block->succ; /* remove it from the block list */
45
+ free(block); /* delete the memory block */
46
+ }
47
+ free(ms); /* delete the base structure */
48
+ } /* ms_delete() */
49
+
50
+ /*--------------------------------------------------------------------*/
51
+
52
+ void* ms_alloc (MEMSYS *ms)
53
+ { /* --- allocate an object */
54
+ int i; /* loop variable */
55
+ void **obj, **tmp; /* allocated object, buffer */
56
+ MSBLOCK *block; /* new block */
57
+
58
+ assert(ms); /* check the function argument */
59
+ obj = ms->free; /* get the head of the free list */
60
+ if (!obj) { /* if there is no free node, */
61
+ block = (MSBLOCK*)malloc(ms->blksz);
62
+ if (!block) return NULL; /* allocate a new memory block */
63
+ block->succ = ms->blocks; /* and add it at the head */
64
+ ms->blocks = block; /* of the block list */
65
+ ms->free = obj = (void*)(block +1);
66
+ for (i = ms->cnt; --i > 0; ) {
67
+ tmp = obj; *tmp = obj += ms->size; }
68
+ *obj = NULL; /* traverse the object vector */
69
+ obj = ms->free; /* and link the objects together, */
70
+ } /* then get the next free object */
71
+ ms->used++; /* count the allocated object */
72
+ ms->free = *obj; /* remove object from the free list */
73
+ return (void*)obj; /* and return the retrieved object */
74
+ } /* ms_alloc() */
75
+
76
+ /*--------------------------------------------------------------------*/
77
+
78
+ void ms_free (MEMSYS *ms, void *obj)
79
+ { /* --- deallocate an f.p. tree node */
80
+ assert(ms && obj); /* check the function arguments */
81
+ *(void**)obj = ms->free; /* insert the freed object */
82
+ ms->free = obj; /* at the head of the free list */
83
+ ms->used--; /* count the deallocated object */
84
+ } /* ms_free() */
@@ -0,0 +1,42 @@
1
+ /*----------------------------------------------------------------------
2
+ File : memsys.h
3
+ Contents: memory management system for equally sized (small) objects
4
+ Author : Christian Borgelt
5
+ History : 2004.12.10 file created from fpgrowth.c
6
+ 2008.01.23 counting of used blocks added
7
+ ----------------------------------------------------------------------*/
8
+ #ifndef __MEMSYS__
9
+ #define __MEMSYS__
10
+
11
+ /*----------------------------------------------------------------------
12
+ Type Definitions
13
+ ----------------------------------------------------------------------*/
14
+ typedef struct _msblock { /* --- memory system block --- */
15
+ struct _msblock *succ; /* successor block in list */
16
+ void *rsvd; /* reserved (for alignment) */
17
+ } MSBLOCK; /* (memory management system block) */
18
+
19
+ typedef struct { /* --- memory management system --- */
20
+ int size; /* size of each object */
21
+ int cnt; /* number of objects per block */
22
+ int blksz; /* size of a memory block */
23
+ int used; /* number of used objects */
24
+ void **free; /* list of free objects */
25
+ void *blocks; /* allocated memory blocks */
26
+ } MEMSYS; /* (memory management system) */
27
+
28
+ /*----------------------------------------------------------------------
29
+ Functions
30
+ ----------------------------------------------------------------------*/
31
+ extern MEMSYS* ms_create (int size, int cnt);
32
+ extern void ms_delete (MEMSYS *ms);
33
+ extern void* ms_alloc (MEMSYS *ms);
34
+ extern void ms_free (MEMSYS *ms, void *obj);
35
+ extern int ms_used (MEMSYS *ms);
36
+
37
+ /*----------------------------------------------------------------------
38
+ Preprocessor Definitions
39
+ ----------------------------------------------------------------------*/
40
+ #define ms_used(m) ((m)->used)
41
+
42
+ #endif
@@ -0,0 +1,288 @@
1
+ /*----------------------------------------------------------------------
2
+ File : nstats.c
3
+ Contents: management of normalization statistics
4
+ Author : Christian Borgelt
5
+ History : 2003.08.12 file created
6
+ 2004.08.12 description and parse function added
7
+ ----------------------------------------------------------------------*/
8
+ #include <stdio.h>
9
+ #include <stdlib.h>
10
+ #include <string.h>
11
+ #include <float.h>
12
+ #include <math.h>
13
+ #include <assert.h>
14
+ #include "nstats.h"
15
+
16
+ /*----------------------------------------------------------------------
17
+ Preprocessor Definitions
18
+ ----------------------------------------------------------------------*/
19
+ #define BLKSIZE 64 /* block size for parsing */
20
+
21
+ /*----------------------------------------------------------------------
22
+ Functions
23
+ ----------------------------------------------------------------------*/
24
+
25
+ NSTATS* nst_create (int dim)
26
+ { /* --- create numerical statistics */
27
+ NSTATS *nst; /* created statistics structure */
28
+ double *p; /* to organize the memory */
29
+
30
+ assert(dim > 0); /* check the function argument */
31
+ nst = (NSTATS*)malloc(sizeof(NSTATS) +(6*dim -1) *sizeof(double));
32
+ if (!nst) return NULL; /* create a statistics structure */
33
+ nst->dim = dim; /* and initialize the fields */
34
+ nst->reg = 0;
35
+ nst->offs = p = nst->facs +dim;
36
+ nst->mins = p += dim;
37
+ nst->maxs = p += dim; /* organize the vectors */
38
+ nst->sums = p += dim;
39
+ nst->sqrs = p += dim;
40
+ while (--dim >= 0) { /* traverse the vectors */
41
+ nst->mins[dim] = DBL_MAX; nst->maxs[dim] = -DBL_MAX;
42
+ nst->sums[dim] = nst->sqrs[dim] = nst->offs[dim] = 0;
43
+ nst->facs[dim] = 1; /* initialize the ranges of values */
44
+ } /* and the aggregation variables */
45
+ return nst; /* return created structure */
46
+ } /* nst_create() */
47
+
48
+ /*--------------------------------------------------------------------*/
49
+
50
+ void nst_delete (NSTATS *nst)
51
+ { free(nst); } /* --- delete numerical statistics */
52
+
53
+ /*--------------------------------------------------------------------*/
54
+
55
+ void nst_reg (NSTATS *nst, const double *vec, double weight)
56
+ { /* --- register a data vector */
57
+ int i; /* loop variable */
58
+ double *min, *max; /* to traverse the min./max. values */
59
+ double *sum, *sqr; /* to traverse the value sums */
60
+ double *off, *fac; /* to traverse the offsets/scales */
61
+ double t; /* temporary buffer */
62
+
63
+ assert(nst && vec); /* check the function arguments */
64
+ sum = nst->sums; /* get the vectors for the sums */
65
+ sqr = nst->sqrs; /* and the sums of squares */
66
+ if (!vec) { /* if to terminate registration */
67
+ off = nst->offs; /* get the offsets and */
68
+ fac = nst->facs; /* the scaling factors */
69
+ if (nst->reg <= 0) /* if no patterns are registered */
70
+ for (i = nst->dim; --i >= 0; ) { off[i] = 0; fac[i] = 1; }
71
+ else { /* if patterns have been registered */
72
+ for (i = nst->dim; --i >= 0; ) { /* traverse the vectors */
73
+ off[i] = sum[i] /nst->reg;
74
+ t = sqr[i] -off[i] *sum[i];
75
+ fac[i] = (t > 0) ? sqrt(nst->reg /t) : 1;
76
+ } /* estimate the parameters */
77
+ }
78
+ if (weight < 0) { /* if to reinitialize registration */
79
+ for (i = nst->dim; --i >= 0; )
80
+ sum[i] = sqr[i] = 0; /* reinitialize the vectors */
81
+ nst->reg = 0; /* and the pattern counter */
82
+ } }
83
+ else { /* if to register a data vector */
84
+ min = nst->mins; /* get the minimal */
85
+ max = nst->maxs; /* and the maximal values */
86
+ for (i = nst->dim; --i >= 0; ) {
87
+ if (vec[i] < min[i]) min[i] = vec[i];
88
+ if (vec[i] > max[i]) max[i] = vec[i];
89
+ sum[i] += vec[i]; /* update the ranges of values */
90
+ sqr[i] += vec[i] *vec[i]; /* and sum the values */
91
+ } /* and their squares */
92
+ nst->reg += weight; /* count the pattern */
93
+ }
94
+ } /* nst_reg() */
95
+
96
+ /*--------------------------------------------------------------------*/
97
+
98
+ void nst_range (NSTATS *nst, int idx, double min, double max)
99
+ { /* --- set range of values */
100
+ int i; /* loop variable */
101
+
102
+ assert(nst && (idx < nst->dim)); /* check the arguments */
103
+ if (idx < 0) { i = nst->dim; idx = 0; }
104
+ else { i = idx +1; } /* get index range to set */
105
+ while (--i >= idx) { /* and traverse it */
106
+ nst->mins[i] = min; /* set the minimal */
107
+ nst->maxs[i] = max; /* and the maximal value */
108
+ } /* for all dimensions in range */
109
+ } /* nst_range() */
110
+
111
+ /*--------------------------------------------------------------------*/
112
+
113
+ void nst_expand (NSTATS *nst, int idx, double factor)
114
+ { /* --- expand range of values */
115
+ int i; /* loop variable */
116
+ double t; /* change of minimal/maximal value */
117
+
118
+ assert(nst /* check the function arguments */
119
+ && (idx < nst->dim) && (factor >= 0));
120
+ if (idx < 0) { i = nst->dim; idx = 0; }
121
+ else { i = idx +1; } /* get index range to expand */
122
+ while (--i >= idx) { /* and traverse it */
123
+ t = (nst->maxs[i] -nst->mins[i]) *(factor -1) *0.5;
124
+ nst->mins[i] -= t; /* adapt the minimal */
125
+ nst->maxs[i] += t; /* and the maximal value */
126
+ } /* for all dimensions in range */
127
+ } /* nst_expand() */
128
+
129
+ /*--------------------------------------------------------------------*/
130
+
131
+ void nst_scale (NSTATS *nst, int idx, double off, double fac)
132
+ { /* --- set (linear) scaling */
133
+ int i; /* loop variable */
134
+
135
+ assert(nst && (idx < nst->dim)); /* check the arguments */
136
+ if (idx < 0) { i = nst->dim; idx = 0; }
137
+ else { i = idx +1; } /* get index range to set */
138
+ while (--i >= idx) { /* and traverse it */
139
+ nst->offs[i] = off; /* set the offset */
140
+ nst->facs[i] = fac; /* and the scaling factor */
141
+ } /* for all dimensions in range */
142
+ } /* nst_scale() */
143
+
144
+ /*--------------------------------------------------------------------*/
145
+
146
+ void nst_norm (NSTATS *nst, const double *vec, double *res)
147
+ { /* --- normalize a data vector */
148
+ int i; /* loop variable */
149
+ double *off, *fac; /* to traverse the scaling parameters */
150
+
151
+ assert(nst && vec && res); /* check the function arguments */
152
+ off = nst->offs +(i = nst->dim);
153
+ fac = nst->facs + i; /* get the scaling parameters */
154
+ res += i; vec += i; /* and the data vectors */
155
+ while (--i >= 0) *--res = *--fac * (*--vec - *--off);
156
+ } /* nst_norm() */ /* scale the vector */
157
+
158
+ /*--------------------------------------------------------------------*/
159
+
160
+ void nst_inorm (NSTATS *nst, const double *vec, double *res)
161
+ { /* --- inverse normalize a vector */
162
+ int i; /* loop variable */
163
+ double *off, *fac; /* to traverse the scaling parameters */
164
+
165
+ assert(nst && vec && res); /* check the function arguments */
166
+ off = nst->offs +(i = nst->dim);
167
+ fac = nst->facs + i; /* get the scaling parameters */
168
+ res += i; vec += i; /* and the data vectors */
169
+ while (--i >= 0) *--res = *--vec / *--fac + *--off;
170
+ } /* nst_inorm() */ /* scale the vector */
171
+
172
+ /*--------------------------------------------------------------------*/
173
+
174
+ void nst_center (NSTATS *nst, double *vec)
175
+ { /* --- get center of data space */
176
+ int i; /* loop variable */
177
+ double *min, *max; /* to traverse the ranges */
178
+
179
+ assert(nst && vec); /* check the function arguments */
180
+ min = nst->mins; /* get the range variables, */
181
+ max = nst->maxs; /* traverse the dimensions, */
182
+ for (i = nst->dim; --i >= 0;) /* and compute the center vector */
183
+ vec[i] = 0.5 *(max[i] +min[i]);
184
+ } /* nst_center() */
185
+
186
+ /*--------------------------------------------------------------------*/
187
+
188
+ void nst_spans (NSTATS *nst, double *vec)
189
+ { /* --- get spans of dimensions */
190
+ int i; /* loop variable */
191
+ double *min, *max; /* to traverse the ranges */
192
+
193
+ assert(nst && vec); /* check the function arguments */
194
+ min = nst->mins;
195
+ max = nst->maxs; /* get the range variables, */
196
+ for (i = nst->dim; --i >= 0;) /* traverse the dimensions, */
197
+ vec[i] = max[i] -min[i]; /* and compute the spans */
198
+ } /* nst_spans() */
199
+
200
+ /*--------------------------------------------------------------------*/
201
+
202
+ int nst_desc (NSTATS *nst, FILE *file, const char *indent, int maxlen)
203
+ { /* --- describe norm. statistics */
204
+ int i; /* loop variable */
205
+ int pos, ind; /* position in output line */
206
+ char buf[64]; /* buffer for output */
207
+
208
+ for (i = nst->dim; --i >= 0;) /* check for non-identity scaling */
209
+ if ((nst->offs[i] != 0) || (nst->facs[i] != 1)) break;
210
+ if (i < 0) return 0; /* if all identity scaling, abort */
211
+
212
+ fputs(indent, file); /* write the indentation and */
213
+ fputs("scales = ", file); /* start the scaling parameters */
214
+ for (ind = 0; indent[ind]; ind++);
215
+ pos = ind +9; /* compute the starting position */
216
+ for (i = 0; i < nst->dim; i++) {
217
+ pos += sprintf(buf, "[%g, %g]", nst->offs[i], nst->facs[i]);
218
+ if (i > 0) { /* format the scaling parameters */
219
+ if (pos +3 <= maxlen) { fputs(", ", file); pos += 2; }
220
+ else { fprintf(file, ",\n%s ", indent); pos = ind; }
221
+ } /* print separator and indentation */
222
+ fputs(buf, file); /* print formatted offset and factor */
223
+ }
224
+ fputs(";\n", file); /* terminate the list */
225
+ return ferror(file) ? -1 : 0; /* return the write status */
226
+ } /* nst_desc() */
227
+
228
+ /*--------------------------------------------------------------------*/
229
+ #ifdef NST_PARSE
230
+
231
+ static int _parse (SCAN *scan, int dim, double **buf)
232
+ { /* --- parse normalization statistics */
233
+ int k, n = 0; /* loop variable, counter */
234
+ double *p; /* to access the statistics elements */
235
+
236
+ assert(scan); /* check the function arguments */
237
+ if ((sc_token(scan) != T_ID) /* check whether 'scales' follows */
238
+ || (strcmp(sc_value(scan), "scales") != 0))
239
+ ERR_STR("scales"); /* if not, abort the function */
240
+ GET_TOK(); /* consume 'scales' */
241
+ GET_CHR('='); /* consume '=' */
242
+ for (k = 0; (dim <= 0) || (k < dim); k++) {
243
+ if (k > 0) { GET_CHR(',');} /* if not first, consume ',' */
244
+ if (k >= n) { /* if the statistics vector is full */
245
+ if (dim > 0) n = dim; /* compute the new vector size */
246
+ else n += (n > BLKSIZE) ? n >> 1 : BLKSIZE;
247
+ p = (double*)realloc(*buf, (n+n) *sizeof(double));
248
+ if (!p) ERROR(E_NOMEM); /* enlarge the buffer vector */
249
+ *buf = p; /* and set the new vector, */
250
+ } /* then note factor and offset */
251
+ p = *buf +k +k; /* get the element to set */
252
+ GET_CHR('['); /* consume '[' */
253
+ if (sc_token(scan) != T_NUM) ERROR(E_NUMEXP);
254
+ p[0] = strtod(sc_value(scan), NULL);
255
+ GET_TOK(); /* consume the offset */
256
+ GET_CHR(','); /* consume '[' */
257
+ if (sc_token(scan) != T_NUM) ERROR(E_NUMEXP);
258
+ p[1] = strtod(sc_value(scan), NULL);
259
+ GET_TOK(); /* consume the factor */
260
+ GET_CHR(']'); /* consume '[' */
261
+ if ((dim <= 0) && (sc_token(scan) != ',')) {
262
+ k++; break; } /* check for more scaling params. */
263
+ }
264
+ GET_CHR(';'); /* consume ';' */
265
+ return k; /* return 'ok' */
266
+ } /* _parse() */
267
+
268
+ /*--------------------------------------------------------------------*/
269
+
270
+ NSTATS* nst_parse (SCAN *scan, int dim)
271
+ { /* --- parse normalization statistics */
272
+ NSTATS *nst; /* created normalization statistics */
273
+ double *buf = NULL; /* buffer for reading */
274
+
275
+ assert(scan); /* check the function arguments */
276
+ dim = _parse(scan,dim, &buf); /* parse normalization statistics */
277
+ if (dim < 0) { if (buf) free(buf); return NULL; }
278
+ nst = nst_create(dim); /* create a statistics structure */
279
+ if (!nst) { free(buf); return NULL; }
280
+ for (buf += dim +dim; --dim >= 0; ) {
281
+ nst->facs[dim] = *--buf; /* copy the buffered values */
282
+ nst->offs[dim] = *--buf; /* into the corresponding vectors */
283
+ }
284
+ free(buf); /* delete the read buffer */
285
+ return nst; /* return the created structure */
286
+ } /* nst_parse() */
287
+
288
+ #endif