Initial snark14m import
[snark14.git] / src / snark / effpick.cpp
1 /*
2  **********************************************************************
3  $SNARK_Header: S N A R K  1 4 - A PICTURE RECONSTRUCTION PROGRAM $
4  $HeadURL: svn://dig.cs.gc.cuny.edu/snark/trunk/src/snark/effpick.cpp $
5  $LastChangedRevision: 79 $
6  $Date: 2014-07-01 15:33:39 -0400 (Tue, 01 Jul 2014) $
7  $Author: agulati $
8  **********************************************************************
9  */
10
11 #include <cstdio>
12 #include <cmath>
13
14 #include "uiod.h"
15 #include "blkdta.h"
16 #include "primfc.h"
17 #include "effpick.h"
18
19 void factorization(INTEGER num, INTEGER** decomp, INTEGER *num_dig)
20 {
21         // factorizes num and put the factors into vector decomp of size num_dig
22
23         INTEGER m2s, nipf, mnipf, k;
24
25         INTEGER* ipf = NULL;
26
27
28         if (num == 1)
29         {  //bug 144 earlier version gets *num_dig=0 when num == 1
30                 *num_dig = 1;
31                 *decomp = new INTEGER[1];
32                 *decomp[0] = 1;
33                 return;
34         }
35
36         else
37         { //num > 1
38
39                 mnipf = (INTEGER) ceil(log((REAL) num) / log(2.0));
40
41                 ipf = new INTEGER[mnipf];
42                 primfc(num, &m2s, &nipf, ipf, mnipf);
43
44                 *num_dig = m2s + nipf;
45                 *decomp = new INTEGER[*num_dig];
46
47                 for (k = 0; k < m2s; k++)
48                 {
49                         (*decomp)[k] = 2;
50                 }
51
52                 for (k = 0; k < nipf; k++)
53                 {
54                         (*decomp)[k + m2s] = ipf[k];
55                 }
56
57                 delete[] ipf;  //wei 3/2005
58                 return;
59         }
60 }
61
62 void tau_map(INTEGER num_dig, INTEGER* decomp, INTEGER* old_num,
63                 INTEGER* new_num)
64 {
65         // see the definition of the tau mapping in "Herman and Meyer. Algebraic reconstruction techniques can be made computationally efficient. IEEE Trans. on Medical Imaging 12: 600--609. 1993"
66         static INTEGER cont, ind;
67
68         if (old_num[0] == -1)
69         {
70                 for (cont = 0; cont < num_dig; cont++)
71                 {
72                         old_num[cont] = 0;
73                         new_num[cont] = 0;
74                 }
75                 return;
76         }
77
78         for (cont = 0; cont < num_dig; cont++)
79         {
80                 if (old_num[cont] != decomp[cont] - 1)
81                 {
82                         ind = cont;
83                         break;
84                 }
85         }
86
87         for (cont = 0; cont < num_dig; cont++)
88         {
89                 if (cont < ind)
90                 {
91                         new_num[cont] = 0;
92                 }
93                 else if (cont == ind)
94                 {
95                         new_num[cont] = old_num[cont] + 1;
96                 }
97                 else
98                 {
99                         new_num[cont] = old_num[cont];
100                 }
101         }
102
103         for (cont = 0; cont < num_dig; cont++)
104                 old_num[cont] = new_num[cont];
105 }
106
107 void get_base(INTEGER* decomp, INTEGER num_dig, INTEGER** base)
108 {
109         // given the factorization vector decomp (of size num_dig) in increasing order, the routine returns another vector whose components are the accumulative product of decomp in the reversed order
110         INTEGER cont, aux;
111
112         aux = 1;
113
114         *base = new INTEGER[num_dig];
115
116         (*base)[num_dig - 1] = 1;
117
118         for (cont = 0; cont < num_dig - 1; cont++)
119         {
120                 aux = aux * decomp[num_dig - cont - 1];
121                 (*base)[num_dig - cont - 2] = aux;
122         }
123
124         return;
125
126 }
127
128 void nu_map(INTEGER num_dig, INTEGER* base, INTEGER* num, INTEGER* result)
129 {
130         // given the the vector num whose components are the digits of a number written in base "base", this routines returns that number as "result"
131         INTEGER cont;
132
133         *result = 0;
134
135         for (cont = 0; cont < num_dig; cont++)
136         {
137                 *result = *result + base[cont] * num[cont];
138         }
139
140 }
141
142 void pi_map(INTEGER num_dig, INTEGER* decomp, INTEGER* base, INTEGER** old_num,
143                 INTEGER** new_num, INTEGER* res)
144 {
145         // given the factorizaton vector decomp and the vector base, this routines is the composition of nu_map and tau_map
146
147         tau_map(num_dig, decomp, (*old_num), (*new_num));
148         nu_map(num_dig, base, (*new_num), res);
149
150
151         return;
152 }
153