/usr/include/colib/narray-util.h is in libiulib-dev 0.4+is+0.3-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 | // -*- C++ -*-
// Copyright 2006 Deutsches Forschungszentrum fuer Kuenstliche Intelligenz
// or its licensors, as applicable.
// Copyright 1995-2005 Thomas M. Breuel.
//
// You may not use this file except under the terms of the accompanying license.
//
// 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.
//
// Project: iulib -- image understanding library
// File: narray-util.h
// Purpose: miscellaneous array utility functions
// Responsible: tmb
// Reviewer:
// Primary Repository:
// Web Sites: www.iupr.org, www.dfki.de
/// \file narray-util.h
/// \brief Miscellaneous array utility functions
#ifndef h_narray_util__
#define h_narray_util__
#include <math.h>
#include <stdlib.h>
#include "colib/checks.h"
#include "misc.h"
#ifdef WIN32
#include "compat.h"
#endif
///
/// \brief Miscellaneous array utility functions
namespace colib {
/// Make sure the array doesn't contain any NaN values.
template <class T>
void check_nan(narray<T> &v) {
for(int i=0;i<v.length1d();i++)
ASSERT(!isnan(v.unsafe_at1d(i)));
}
/// Compute the global max of the absolute value of the array.
template <class T>
inline T absmax(narray<T> &a) {
T value = a.at1d(0);
for(int i=1;i<a.length1d();i++) {
T nvalue = a.at1d(i);
if(nvalue<0) nvalue = -nvalue;
if(nvalue<=value) continue;
value = nvalue;
}
return value;
}
/// Compute the global max of the array.
template <class T>
inline T max(narray<T> &a) {
T value = a.at1d(0);
for(int i=1;i<a.length1d();i++) {
T nvalue = a.at1d(i);
if(nvalue<=value) continue;
value = nvalue;
}
return value;
}
/// Compute the global min of the array.
template <class T>
inline T min(narray<T> &a) {
T value = a.at1d(0);
for(int i=1;i<a.length1d();i++) {
T nvalue = a.at1d(i);
if(nvalue>=value) continue;
value = nvalue;
}
return value;
}
/// Sum the values of the array.
template <class T>
inline double sum(narray<T> &data) {
double result = 0.0;
int n = data.length1d();
for(int i=0;i<n;i++)
result += data.at1d(i);
return result;
}
/// The product of the values of the array.
template <class T>
inline double product(narray<T> &data) {
double result = 1.0;
int n = data.length1d();
for(int i=0;i<n;i++)
result *= data.at1d(i);
return result;
}
/// Compute the argmax of the rank-1 array.
template <class T>
inline int argmax(narray<T> &a) {
CHECK_ARG(a.rank()==1 && a.dim(0)>0);
T value = a(0);
int index = 0;
for(int i=1;i<a.dim(0);i++) {
T nvalue = a(i);
if(nvalue<=value) continue;
value = nvalue;
index = i;
}
return index;
}
/// Compute the argmin of the rank-1 array.
template <class T>
inline int argmin(narray<T> &a) {
CHECK_ARG(a.rank()==1 && a.dim(0)>0);
T value = a(0);
int index = 0;
for(int i=1;i<a.dim(0);i++) {
T nvalue = a(i);
if(nvalue>=value) continue;
value = nvalue;
index = i;
}
return index;
}
/// Make a unit vector of length n with a 1 in position i.
inline void make_unit_vector(floatarray &a,int n,int i) {
a.resize(n);
fill(a,0);
a(i) = 1;
}
/// Randomly permute the elements of a vector.
template <class T>
inline void randomly_permute(narray<T> &v) {
int n = v.length();
for(int i=0;i<n-1;i++) {
int target = rand()%(n-i)+i;
T temp = v[target];
v[target] = v[i];
v[i] = temp;
}
}
/// Euclidean distance squared.
inline double dist2squared(floatarray &a,floatarray &b) {
CHECK_ARG(samedims(a,b));
double total = 0.0;
for(int i=0;i<a.length1d();i++)
total += sqr(a.at1d(i)-b.at1d(i));
CHECK_ARG(!isnan(total));
return total;
}
/// Euclidean distance.
inline double dist2(floatarray &a,floatarray &b) {
return sqrt(dist2squared(a,b));
}
/// Euclidean norm squared.
inline double norm2squared(floatarray &a) {
double total = 0.0;
for(int i=0;i<a.length1d();i++)
total += sqr(a.at1d(i));
return total;
}
/// Euclidean norm.
inline double norm2(floatarray &a) {
return sqrt(norm2squared(a));
}
/// Normalize the Euclidean norm of the array.
inline void normalize2(floatarray &a) {
double scale = 1.0/norm2(a);
for(int i=0;i<a.length1d();i++)
a.at1d(i) *= scale;
}
/// Euclidean norm.
inline double norm1(floatarray &a) {
double total = 0.0;
int n = a.length1d();
for(int i=0;i<n;i++) total += fabs(a.unsafe_at1d(i));
return total;
}
/// Normalize the 1-norm of the array.
inline void normalize1(floatarray &a) {
double scale = 1.0/norm1(a);
for(int i=0;i<a.length1d();i++)
a.unsafe_at1d(i) *= scale;
}
/// Make a random vector of length n with uniformly random values
/// between -scale and scale.
inline void make_random(floatarray &v,int n,float scale) {
v.resize(n);
for(int i=0;i<n;i++)
v(i) = (double(rand())/RAND_MAX*2-1)*scale;
}
/// Fill an array with random values.
inline void fill_uniform(floatarray &v,float lo,float hi) {
int n = v.length1d();
for(int i=0;i<n;i++)
v.at1d(i) = drand48()*(hi-lo)+lo;
}
/// Perturb the array with uniformly random values between -scale and scale.
inline void perturb(floatarray &v,float scale) {
int n = v.length1d();
for(int i=0;i<n;i++)
v.at1d(i) += (double(rand())/RAND_MAX*2-1)*scale;
}
/// Make a vector of length n with entries from 0 to n-1.
template <class T>
inline void iota(narray<T> &v,int n) {
v.resize(n);
for(int i=0;i<n;i++)
v[i] = i;
}
/// reverse an array
template <class T>
inline void reverse(narray<T> &out,narray<T> &in) {
out.clear();
for(int i=in.length()-1;i>=0;i--)
out.push(in(i));
}
/// reverse an array
template <class T>
inline void remove_left(narray<T> &a,int offset) {
CHECK_ARG(offset>=0);
if(offset==0) return;
narray<T> temp;
for(int i=offset;i<a.length();i++)
temp.push(a(i));
move(a,temp);
}
/// reverse an array in place
template <class T>
inline void reverse(narray<T> &out) {
if(out.length()<1) return;
int n = out.length();
int m = int(n/2);
for(int i=0;i<=m;i++) {
T temp = out(i);
out(i) = out(n-i-1);
out(n-i-1) = temp;
}
}
/// Add the data to the result.
template <class T>
inline void add(narray<T> &result,narray<T> &data) {
CHECK_ARG(samedims(result,data));
int n = result.length1d();
for(int i=0;i<n;i++)
result.at1d(i) += data.at1d(i);
}
/// Multiply the data with scale and add to result.
template <class T>
inline void addscaled(narray<T> &result,narray<T> &data,double scale) {
CHECK_ARG(samedims(result,data));
int n = result.length();
for(int i=0;i<n;i++) result[i] += data[i] * scale;
}
/// Array subscripting with extending boundary conditions.
template <class T>
inline T &ext(narray<T> &a,int i) {
i = max(0,min(i,a.dim(0)-1));
return a.unsafe_at(i);
}
/// Array subscripting with extending boundary conditions.
template <class T>
inline T &ext(narray<T> &a,int i,int j) {
i = max(0,min(i,a.dim(0)-1));
j = max(0,min(j,a.dim(1)-1));
return a.unsafe_at(i,j);
}
/// Array subscripting with fixed boundary conditions.
template <class T,class U>
inline T bat(narray<T> &a,int i,U value) {
if(unsigned(i)>=a.dim(0)) return value;
return a.unsafe_at(i);
}
/// Array subscripting with fixed boundary conditions.
template <class T,class U>
inline T bat(narray<T> &a,int i,int j,U value) {
if(unsigned(i)>=unsigned(a.dim(0))) return value;
if(unsigned(j)>=unsigned(a.dim(1))) return value;
return a.unsafe_at(i,j);
}
template <class T>
void remove_element(narray<T> &a,int index) {
narray<T> result;
for(int i=0;i<a.length();i++)
if(i!=index)
result.push(a[i]);
move(a,result);
}
template <class T,class S>
void remove_value(narray<T> &a,S v) {
intarray result;
for(int i=0;i<a.length();i++)
if(a[i]!=v) result.push(a[i]);
move(a,result);
}
template <class T,class S>
int first_index_of(narray<T> &a,S target) {
for(int i=0;i<a.length();i++)
if(a(i)==target) return i;
return -1;
}
template <class T>
static inline void insert_at(narray<T> &a,int i) {
a.push();
int n = a.length();
for(int j=n-1;j>i;j--) a[j] = a[j-1];
}
template <class T>
static inline void delete_at(narray<T> &a,int i) {
int n = a.length();
for(int j=i+1;j<n;j++) a[j-1] = a[j];
a.pop();
}
template <class T>
static inline void insert_at(narray<T> &a,int i,T value) {
a.push();
int n = a.length();
for(int j=n-1;j>i;j--) a[j] = a[j-1];
a[i] = value;
}
/// Temporary arrays
template <class T>
class check_narray_nomod {
narray<T> &arg;
#ifndef UNSAFE
narray<T> temp;
#endif
public:
check_narray_nomod(narray<T> &arg):arg(arg) {
#ifndef UNSAFE
copy(temp,arg);
#endif
}
~check_narray_nomod() {
#ifndef UNSAFE
ASSERT(equal(temp,arg));
#endif
}
operator narray<T> &() {
return arg;
}
};
}
#endif
|