summaryrefslogtreecommitdiffstats
path: root/jni/feature_stab/db_vlvm/db_utilities_indexing.h
blob: 01eeb9ea2b8b7506ab6050bd0e89f13aa23175ef (plain)
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
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/* $Id: db_utilities_indexing.h,v 1.3 2011/06/17 14:03:31 mbansal Exp $ */

#ifndef DB_UTILITIES_INDEXING
#define DB_UTILITIES_INDEXING



/*****************************************************************
*    Lean and mean begins here                                   *
*****************************************************************/

#include "db_utilities.h"

/*!
 * \defgroup LMIndexing (LM) Indexing Utilities (Order Statistics, Matrix Operations)
 */
/*\{*/

inline void db_SetupMatrixRefs(double **ar,long rows,long cols,double *a)
{
    long i;
    for(i=0;i<rows;i++) ar[i]=&a[i*cols];
}

inline void db_SymmetricExtendUpperToLower(double **A,int rows,int cols)
{
    int i,j;
    for(i=1;i<rows;i++) for(j=0;j<i;j++) A[i][j]=A[j][i];
}

void inline db_MultiplyMatrixVectorAtb(double *c,const double * const *At,const double *b,int arows,int acols)
{
    int i,j;
    double acc;

    for(i=0;i<arows;i++)
    {
        acc=0;
        for(j=0;j<acols;j++) acc+=At[j][i]*b[j];
        c[i]=acc;
    }
}

inline void db_MultiplyMatricesAB(double **C,const double * const *A,const double * const *B,int arows,int acols,int bcols)
{
    int i,j,k;
    double acc;

    for(i=0;i<arows;i++) for(j=0;j<bcols;j++)
    {
        acc=0;
        for(k=0;k<acols;k++) acc+=A[i][k]*B[k][j];
        C[i][j]=acc;
    }
}

inline void db_UpperMultiplyMatricesAtB(double **Cu,const double * const *At,const double * const *B,int arows,int acols,int bcols)
{
    int i,j,k;
    double acc;

    for(i=0;i<arows;i++) for(j=i;j<bcols;j++)
    {
        acc=0;
        for(k=0;k<acols;k++) acc+=At[k][i]*B[k][j];
        Cu[i][j]=acc;
    }
}

DB_API void db_Zero(double *d,long nr);

inline int db_MaxIndex2(double s[2])
{
    if(s[0]>=s[1]) return(0);
    return(1);
}

inline int db_MaxIndex3(const double s[3])
{
    double best;
    int pos;

    best=s[0];pos=0;
    if(s[1]>best){best=s[1];pos=1;}
    if(s[2]>best){best=s[2];pos=2;}
    return(pos);
}

inline int db_MaxIndex4(const double s[4])
{
    double best;
    int pos;

    best=s[0];pos=0;
    if(s[1]>best){best=s[1];pos=1;}
    if(s[2]>best){best=s[2];pos=2;}
    if(s[3]>best){best=s[3];pos=3;}
    return(pos);
}

inline int db_MaxIndex5(const double s[5])
{
    double best;
    int pos;

    best=s[0];pos=0;
    if(s[1]>best){best=s[1];pos=1;}
    if(s[2]>best){best=s[2];pos=2;}
    if(s[3]>best){best=s[3];pos=3;}
    if(s[4]>best){best=s[4];pos=4;}
    return(pos);
}

inline int db_MaxIndex6(const double s[6])
{
    double best;
    int pos;

    best=s[0];pos=0;
    if(s[1]>best){best=s[1];pos=1;}
    if(s[2]>best){best=s[2];pos=2;}
    if(s[3]>best){best=s[3];pos=3;}
    if(s[4]>best){best=s[4];pos=4;}
    if(s[5]>best){best=s[5];pos=5;}
    return(pos);
}

inline int db_MaxIndex7(const double s[7])
{
    double best;
    int pos;

    best=s[0];pos=0;
    if(s[1]>best){best=s[1];pos=1;}
    if(s[2]>best){best=s[2];pos=2;}
    if(s[3]>best){best=s[3];pos=3;}
    if(s[4]>best){best=s[4];pos=4;}
    if(s[5]>best){best=s[5];pos=5;}
    if(s[6]>best){best=s[6];pos=6;}
    return(pos);
}

inline int db_MinIndex7(const double s[7])
{
    double best;
    int pos;

    best=s[0];pos=0;
    if(s[1]<best){best=s[1];pos=1;}
    if(s[2]<best){best=s[2];pos=2;}
    if(s[3]<best){best=s[3];pos=3;}
    if(s[4]<best){best=s[4];pos=4;}
    if(s[5]<best){best=s[5];pos=5;}
    if(s[6]<best){best=s[6];pos=6;}
    return(pos);
}

inline int db_MinIndex9(const double s[9])
{
    double best;
    int pos;

    best=s[0];pos=0;
    if(s[1]<best){best=s[1];pos=1;}
    if(s[2]<best){best=s[2];pos=2;}
    if(s[3]<best){best=s[3];pos=3;}
    if(s[4]<best){best=s[4];pos=4;}
    if(s[5]<best){best=s[5];pos=5;}
    if(s[6]<best){best=s[6];pos=6;}
    if(s[7]<best){best=s[7];pos=7;}
    if(s[8]<best){best=s[8];pos=8;}
    return(pos);
}

inline int db_MaxAbsIndex3(const double *s)
{
    double t,best;
    int pos;

    best=fabs(s[0]);pos=0;
    t=fabs(s[1]);if(t>best){best=t;pos=1;}
    t=fabs(s[2]);if(t>best){pos=2;}
    return(pos);
}

inline int db_MaxAbsIndex9(const double *s)
{
    double t,best;
    int pos;

    best=fabs(s[0]);pos=0;
    t=fabs(s[1]);if(t>best){best=t;pos=1;}
    t=fabs(s[2]);if(t>best){best=t;pos=2;}
    t=fabs(s[3]);if(t>best){best=t;pos=3;}
    t=fabs(s[4]);if(t>best){best=t;pos=4;}
    t=fabs(s[5]);if(t>best){best=t;pos=5;}
    t=fabs(s[6]);if(t>best){best=t;pos=6;}
    t=fabs(s[7]);if(t>best){best=t;pos=7;}
    t=fabs(s[8]);if(t>best){best=t;pos=8;}
    return(pos);
}


/*!
Select ordinal pos (zero based) out of nr_elements in s.
temp should point to alloced memory of at least nr_elements*2
Optimized runtimes on 450MHz:
\code
  30 with   3 microsecs
 100 with  11 microsecs
 300 with  30 microsecs
 500 with  40 microsecs
1000 with 100 microsecs
5000 with 540 microsecs
\endcode
so the expected runtime is around
(nr_elements/10) microseconds
The total quickselect cost of splitting 500 hypotheses recursively
is thus around 100 microseconds

Does the same operation as std::nth_element().
*/
DB_API double db_LeanQuickSelect(const double *s,long nr_elements,long pos,double *temp);

/*!
 Median of 3 doubles
 */
inline double db_TripleMedian(double a,double b,double c)
{
    if(a>b)
    {
        if(c>a) return(a);
        else if(c>b) return(c);
        else return(b);
    }
    else
    {
        if(c>b) return(b);
        else if(c>a) return(c);
        else return(a);
    }
}

/*!
Align float pointer to nr_bytes by moving forward
*/
DB_API float* db_AlignPointer_f(float *p,unsigned long nr_bytes);

/*!
Align short pointer to nr_bytes by moving forward
*/
DB_API short* db_AlignPointer_s(short *p,unsigned long nr_bytes);

#endif /* DB_UTILITIES_INDEXING */