1 // Copyright (c) 2015-2016, Massachusetts Institute of Technology
2 // Copyright (c) 2016-2017 Sandia Corporation
3 // Copyright (c) 2017 NTESS, LLC.
4 
5 // This file is part of the Compressed Continuous Computation (C3) Library
6 // Author: Alex A. Gorodetsky
7 // Contact: alex@alexgorodetsky.com
8 
9 // All rights reserved.
10 
11 // Redistribution and use in source and binary forms, with or without modification,
12 // are permitted provided that the following conditions are met:
13 
14 // 1. Redistributions of source code must retain the above copyright notice,
15 //    this list of conditions and the following disclaimer.
16 
17 // 2. Redistributions in binary form must reproduce the above copyright notice,
18 //    this list of conditions and the following disclaimer in the documentation
19 //    and/or other materials provided with the distribution.
20 
21 // 3. Neither the name of the copyright holder nor the names of its contributors
22 //    may be used to endorse or promote products derived from this software
23 //    without specific prior written permission.
24 
25 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
29 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
33 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 
36 //Code
37 
38 
39 
40 /** \file futil.h
41  * Some macro utilities
42  */
43 
44 #ifndef FUNC_UTIL_H
45 #define FUNC_UTIL_H
46 
47 #define NOT_IMPLEMENTED_MSG(str) fprintf(stderr, "%s not yet implemented\n", str);
48 
49 
50 // MACROS TO AID GENERIC PROGRAMMING
51 
52 #define GF_SWITCH_NO_OUT(operation)                                 \
53     switch (gf->fc){                                                \
54     case PIECEWISE:  piecewise_poly_##operation(gf->f);      break; \
55     case POLYNOMIAL: orth_poly_expansion_##operation(gf->f); break; \
56     case LINELM:     lin_elem_exp_##operation(gf->f);        break; \
57     case CONSTELM:   const_elem_exp_##operation(gf->f);      break; \
58     case KERNEL:     kernel_expansion_##operation(gf->f);    break; \
59     }
60 
61 #define GF_SWITCH_NO_ONEOUT(operation, fc, A, B)                     \
62     switch (fc){                                                     \
63     case PIECEWISE:  piecewise_poly_##operation(A,B);      break;    \
64     case POLYNOMIAL: orth_poly_expansion_##operation(A,B); break;    \
65     case LINELM:     lin_elem_exp_##operation(A,B);        break;    \
66     case CONSTELM:   const_elem_exp_##operation(A,B);      break;    \
67     case KERNEL:     kernel_expansion_##operation(A,B);    break;    \
68     }
69 
70 #define GF_SWITCH_TEMPOUT(operation) \
71     switch (gf->fc){                                                       \
72     case PIECEWISE:  temp = piecewise_poly_##operation(gf->f);      break; \
73     case POLYNOMIAL: temp = orth_poly_expansion_##operation(gf->f); break; \
74     case LINELM:     temp = lin_elem_exp_##operation(gf->f);        break; \
75     case CONSTELM:   temp = const_elem_exp_##operation(gf->f);      break; \
76     case KERNEL:     temp = kernel_expansion_##operation(gf->f);    break; \
77     }
78 
79 #define GF_SWITCH_ONEOUT(operation,fc,O,A)                            \
80     switch (fc){                                                        \
81     case PIECEWISE:  O = piecewise_poly_##operation(A);      break; \
82     case POLYNOMIAL: O = orth_poly_expansion_##operation(A); break; \
83     case LINELM:     O = lin_elem_exp_##operation(A);        break; \
84     case CONSTELM:   O = const_elem_exp_##operation(A);      break; \
85     case KERNEL:     O = kernel_expansion_##operation(A);    break; \
86     }
87 
88 #define GF_OPTS_SWITCH_ONEOUT(operation,fc,O,A)                            \
89     switch (fc){                                                        \
90     case PIECEWISE:  O = pw_poly_opts_##operation(A);      break; \
91     case POLYNOMIAL: O = ope_opts_##operation(A); break; \
92     case LINELM:     O = lin_elem_exp_aopts_##operation(A);        break; \
93     case CONSTELM:   O = const_elem_exp_aopts_##operation(A);      break; \
94     case KERNEL:     O = kernel_approx_opts_##operation(A);    break; \
95     }
96 
97 #define GF_SWITCH_TWOOUT(operation,fc,O,A,B)                            \
98     switch (fc){                                                        \
99     case PIECEWISE:  O = piecewise_poly_##operation(A,B);      break; \
100     case POLYNOMIAL: O = orth_poly_expansion_##operation(A,B); break; \
101     case LINELM:     O = lin_elem_exp_##operation(A,B);        break; \
102     case CONSTELM:   O = const_elem_exp_##operation(A,B);      break; \
103     case KERNEL:     O = kernel_expansion_##operation(A,B);    break; \
104     }
105 
106 #define GF_SWITCH_THREEOUT(operation,fc,O,A,B,C)                       \
107     switch (fc){                                                        \
108     case PIECEWISE:  O = piecewise_poly_##operation(A,B,C);      break;  \
109     case POLYNOMIAL: O = orth_poly_expansion_##operation(A,B,C); break;  \
110     case LINELM:     O = lin_elem_exp_##operation(A,B,C);        break;  \
111     case CONSTELM:   O = const_elem_exp_##operation(A,B,C);      break;  \
112     case KERNEL:     O = kernel_expansion_##operation(A,B,C);    break;  \
113     }
114 
115 #define GF_SWITCH_NO_THREEOUT(operation,fc,A,B,C)                       \
116     switch (fc){                                                        \
117     case PIECEWISE:  piecewise_poly_##operation(A,B,C);      break;  \
118     case POLYNOMIAL: orth_poly_expansion_##operation(A,B,C); break;  \
119     case LINELM:     lin_elem_exp_##operation(A,B,C);        break;  \
120     case CONSTELM:   const_elem_exp_##operation(A,B,C);      break;  \
121     case KERNEL:     kernel_expansion_##operation(A,B,C);    break;  \
122     }
123 
124 #define GF_SWITCH_THREE_FRONT(operation,fc,A,B,C)                  \
125     switch (fc){                                                    \
126     case PIECEWISE:  operation##_piecewise_poly(A,B,C);      break; \
127     case POLYNOMIAL: operation##_orth_poly_expansion(A,B,C); break; \
128     case LINELM:     operation##_lin_elem_exp(A,B,C);        break; \
129     case CONSTELM:   operation##_const_elem_exp(A,B,C);      break; \
130     case KERNEL:     operation##_kernel_expansion(A,B,C);    break; \
131     }
132 
133 
134 #define GF_SWITCH_NO_FOUROUT_FRONT(operation,fc,A,B,C,D)                 \
135     switch (fc){                                                        \
136     case PIECEWISE:  operation##_piecewise_poly(A,B,C,D);      break;    \
137     case POLYNOMIAL: operation##_orth_poly_expansion(A,B,C,D); break;    \
138     case LINELM:     operation##_lin_elem_exp(A,B,C,D);        break;    \
139     case CONSTELM:   operation##_const_elem_exp(A,B,C,D);      break;    \
140     case KERNEL:     operation##_kernel_expansion(A,B,C,D);    break;    \
141     }
142 
143 #define GF_SWITCH_THREEOUT_FRONT(operation,fc,O,A,B,C)                  \
144     switch (fc){                                                        \
145     case PIECEWISE:  O = operation##_piecewise_poly(A,B,C);      break; \
146     case POLYNOMIAL: O = operation##_orth_poly_expansion(A,B,C); break; \
147     case LINELM:     O = operation##_lin_elem_exp(A,B,C);        break; \
148     case CONSTELM:   O = operation##_const_elem_exp(A,B,C);      break; \
149     case KERNEL:     O = operation##_kernel_expansion(A,B,C);    break; \
150     }
151 
152 #define GF_SWITCH_TWOOUT_FRONT(operation,fc,O,A,B)                  \
153     switch (fc){                                                        \
154     case PIECEWISE:  O = operation##_piecewise_poly(A,B);      break; \
155     case POLYNOMIAL: O = operation##_orth_poly_expansion(A,B); break; \
156     case LINELM:     O = operation##_lin_elem_exp(A,B);        break; \
157     case CONSTELM:   O = operation##_const_elem_exp(A,B);      break; \
158     case KERNEL:     O = operation##_kernel_expansion(A,B);    break; \
159     }
160 
161 #define GF_SWITCH_FOUROUT(operation,fc,O,A,B,C,D)                       \
162     switch (fc){                                                        \
163     case PIECEWISE:  O = piecewise_poly_##operation(A,B,C,D);      break; \
164     case POLYNOMIAL: O = orth_poly_expansion_##operation(A,B,C,D); break; \
165     case LINELM:     O = lin_elem_exp_##operation(A,B,C,D);        break; \
166     case CONSTELM:   O = const_elem_exp_##operation(A,B,C,D);      break; \
167     case KERNEL:     O = kernel_expansion_##operation(A,B,C,D);    break; \
168     }
169 
170 #define GF_SWITCH_SIX(operation,fc,A,B,C,D,E,F)                       \
171     switch (fc){                                                        \
172     case PIECEWISE:  piecewise_poly_##operation(A,B,C,D,E,F);      break; \
173     case POLYNOMIAL: orth_poly_expansion_##operation(A,B,C,D,E,F); break; \
174     case LINELM:     lin_elem_exp_##operation(A,B,C,D,E,F);        break; \
175     case CONSTELM:   const_elem_exp_##operation(A,B,C,D,E,F);      break; \
176     case KERNEL:     kernel_expansion_##operation(A,B,C,D,E,F);    break; \
177     }
178 
179 #define GF_IN_OUT(operation) \
180 struct GenericFunction * generic_function_##operation(const struct GenericFunction * gf) \
181 { \
182     struct GenericFunction * out = NULL;                                       \
183     out = generic_function_alloc(gf->dim, gf->fc);                             \
184     switch (gf->fc){                                                           \
185     case PIECEWISE:  out->f = piecewise_poly_##operation(gf->f);      break;   \
186     case POLYNOMIAL: out->f = orth_poly_expansion_##operation(gf->f); break;   \
187     case LINELM:     out->f = lin_elem_exp_##operation(gf->f);        break;   \
188     case CONSTELM:   out->f = const_elem_exp_##operation(gf->f);      break;   \
189     case KERNEL:     out->f = kernel_expansion_##operation(gf->f);    break;   \
190     }                                                                          \
191     return out;                                                                \
192 }
193 
194 #define GF_IN_GENOUT(operation, typeout, init) \
195 typeout generic_function_##operation(const struct GenericFunction * gf) \
196 { \
197     typeout out = init;                            \
198     switch (gf->fc){                                                    \
199     case PIECEWISE:  out = piecewise_poly_##operation(gf->f);      break;   \
200     case POLYNOMIAL: out = orth_poly_expansion_##operation(gf->f); break;   \
201     case LINELM:     out = lin_elem_exp_##operation(gf->f);        break;   \
202     case CONSTELM:   out = const_elem_exp_##operation(gf->f);      break;   \
203     case KERNEL:     out = kernel_expansion_##operation(gf->f);    break;   \
204     }                                                                          \
205     return out;                                                                \
206 }
207 
208 #endif
209