gk_mkblas.h
Go to the documentation of this file.
1 
10 #ifndef _GK_MKBLAS_H_
11 #define _GK_MKBLAS_H_
12 
13 
14 #define GK_MKBLAS(PRFX, TYPE, OUTTYPE) \
15 /*************************************************************************/\
16 \
17 /*************************************************************************/\
18 TYPE *PRFX ## incset(size_t n, TYPE baseval, TYPE *x)\
19 {\
20  size_t i;\
21 \
22  for (i=0; i<n; i++)\
23  x[i] = baseval+i;\
24 \
25  return x;\
26 }\
27 \
28 /*************************************************************************/\
29 \
30 /*************************************************************************/\
31 TYPE PRFX ## max(size_t n, TYPE *x)\
32 {\
33  size_t i, max=0; \
34 \
35  if (n <= 0) return (TYPE) 0;\
36 \
37  for (i=1; i<n; i++)\
38  max = (x[i] > x[max] ? i : max);\
39 \
40  return x[max];\
41 }\
42 \
43 \
44 /*************************************************************************/\
45 \
46 /*************************************************************************/\
47 TYPE PRFX ## min(size_t n, TYPE *x)\
48 {\
49  size_t i, min=0;\
50 \
51  if (n <= 0) return (TYPE) 0;\
52 \
53  for (i=1; i<n; i++)\
54  min = (x[i] < x[min] ? i : min);\
55 \
56  return x[min];\
57 }\
58 \
59 \
60 /*************************************************************************/\
61 \
62 /*************************************************************************/\
63 size_t PRFX ## argmax(size_t n, TYPE *x)\
64 {\
65  size_t i, max=0;\
66 \
67  for (i=1; i<n; i++)\
68  max = (x[i] > x[max] ? i : max);\
69 \
70  return max;\
71 }\
72 \
73 \
74 /*************************************************************************/\
75 \
76 /*************************************************************************/\
77 size_t PRFX ## argmin(size_t n, TYPE *x)\
78 {\
79  size_t i, min=0;\
80 \
81  for (i=1; i<n; i++)\
82  min = (x[i] < x[min] ? i : min);\
83 \
84  return min;\
85 }\
86 \
87 \
88 /*************************************************************************/\
89 \
90 /*************************************************************************/\
91 size_t PRFX ## argmax_n(size_t n, TYPE *x, size_t k)\
92 {\
93  size_t i, max_n;\
94  PRFX ## kv_t *cand;\
95 \
96  cand = PRFX ## kvmalloc(n, "GK_ARGMAX_N: cand");\
97 \
98  for (i=0; i<n; i++) {\
99  cand[i].val = i;\
100  cand[i].key = x[i];\
101  }\
102  PRFX ## kvsortd(n, cand);\
103 \
104  max_n = cand[k-1].val;\
105 \
106  gk_free((void *)&cand, LTERM);\
107 \
108  return max_n;\
109 }\
110 \
111 \
112 /*************************************************************************/\
113 \
114 /**************************************************************************/\
115 OUTTYPE PRFX ## sum(size_t n, TYPE *x, size_t incx)\
116 {\
117  size_t i;\
118  OUTTYPE sum = 0;\
119 \
120  for (i=0; i<n; i++, x+=incx)\
121  sum += (*x);\
122 \
123  return sum;\
124 }\
125 \
126 \
127 /*************************************************************************/\
128 \
129 /**************************************************************************/\
130 TYPE *PRFX ## scale(size_t n, TYPE alpha, TYPE *x, size_t incx)\
131 {\
132  size_t i;\
133 \
134  for (i=0; i<n; i++, x+=incx)\
135  (*x) *= alpha;\
136 \
137  return x;\
138 }\
139 \
140 \
141 /*************************************************************************/\
142 \
143 /**************************************************************************/\
144 OUTTYPE PRFX ## norm2(size_t n, TYPE *x, size_t incx)\
145 {\
146  size_t i;\
147  OUTTYPE partial = 0;\
148 \
149  for (i=0; i<n; i++, x+=incx)\
150  partial += (*x) * (*x);\
151 \
152  return (partial > 0 ? (OUTTYPE)sqrt((double)partial) : (OUTTYPE)0);\
153 }\
154 \
155 \
156 /*************************************************************************/\
157 \
158 /**************************************************************************/\
159 OUTTYPE PRFX ## dot(size_t n, TYPE *x, size_t incx, TYPE *y, size_t incy)\
160 {\
161  size_t i;\
162  OUTTYPE partial = 0.0;\
163  \
164  for (i=0; i<n; i++, x+=incx, y+=incy)\
165  partial += (*x) * (*y);\
166 \
167  return partial;\
168 }\
169 \
170 \
171 /*************************************************************************/\
172 \
173 /**************************************************************************/\
174 TYPE *PRFX ## axpy(size_t n, TYPE alpha, TYPE *x, size_t incx, TYPE *y, size_t incy)\
175 {\
176  size_t i;\
177  TYPE *y_in = y;\
178 \
179  for (i=0; i<n; i++, x+=incx, y+=incy)\
180  *y += alpha*(*x);\
181 \
182  return y_in;\
183 }\
184 
185 
186 
187 #define GK_MKBLAS_PROTO(PRFX, TYPE, OUTTYPE) \
188  TYPE *PRFX ## incset(size_t n, TYPE baseval, TYPE *x);\
189  TYPE PRFX ## max(size_t n, TYPE *x);\
190  TYPE PRFX ## min(size_t n, TYPE *x);\
191  size_t PRFX ## argmax(size_t n, TYPE *x);\
192  size_t PRFX ## argmin(size_t n, TYPE *x);\
193  size_t PRFX ## argmax_n(size_t n, TYPE *x, size_t k);\
194  OUTTYPE PRFX ## sum(size_t n, TYPE *x, size_t incx);\
195  TYPE *PRFX ## scale(size_t n, TYPE alpha, TYPE *x, size_t incx);\
196  OUTTYPE PRFX ## norm2(size_t n, TYPE *x, size_t incx);\
197  OUTTYPE PRFX ## dot(size_t n, TYPE *x, size_t incx, TYPE *y, size_t incy);\
198  TYPE *PRFX ## axpy(size_t n, TYPE alpha, TYPE *x, size_t incx, TYPE *y, size_t incy);\
199 
200 
201 #endif


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:42:08