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 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
|
// Copyright (c) 2017-2023 California Institute of Technology ("Caltech"). U.S.
// Government sponsorship acknowledged. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
#pragma once
/*
Automatic differentiation routines. Used in poseutils-uses-autodiff.cc. See
that file for usage examples
*/
// Apparently I need this in MSVC to get constants
#define _USE_MATH_DEFINES
#include <math.h>
#include <string.h>
#include "strides.h"
template<int NGRAD, int NVEC> struct vec_withgrad_t;
template<int NGRAD>
struct val_withgrad_t
{
double x;
double j[NGRAD];
__attribute__ ((visibility ("hidden")))
val_withgrad_t(double _x = 0.0) : x(_x)
{
for(int i=0; i<NGRAD; i++) j[i] = 0.0;
}
__attribute__ ((visibility ("hidden")))
val_withgrad_t<NGRAD> operator+( const val_withgrad_t<NGRAD>& b ) const
{
val_withgrad_t<NGRAD> y = *this;
y.x += b.x;
for(int i=0; i<NGRAD; i++)
y.j[i] += b.j[i];
return y;
}
__attribute__ ((visibility ("hidden")))
val_withgrad_t<NGRAD> operator+( double b ) const
{
val_withgrad_t<NGRAD> y = *this;
y.x += b;
return y;
}
__attribute__ ((visibility ("hidden")))
void operator+=( const val_withgrad_t<NGRAD>& b )
{
*this = (*this) + b;
}
__attribute__ ((visibility ("hidden")))
val_withgrad_t<NGRAD> operator-( const val_withgrad_t<NGRAD>& b ) const
{
val_withgrad_t<NGRAD> y = *this;
y.x -= b.x;
for(int i=0; i<NGRAD; i++)
y.j[i] -= b.j[i];
return y;
}
__attribute__ ((visibility ("hidden")))
val_withgrad_t<NGRAD> operator-( double b ) const
{
val_withgrad_t<NGRAD> y = *this;
y.x -= b;
return y;
}
__attribute__ ((visibility ("hidden")))
void operator-=( const val_withgrad_t<NGRAD>& b )
{
*this = (*this) - b;
}
__attribute__ ((visibility ("hidden")))
val_withgrad_t<NGRAD> operator-() const
{
return (*this) * (-1);
}
__attribute__ ((visibility ("hidden")))
val_withgrad_t<NGRAD> operator*( const val_withgrad_t<NGRAD>& b ) const
{
val_withgrad_t<NGRAD> y;
y.x = x * b.x;
for(int i=0; i<NGRAD; i++)
y.j[i] = j[i]*b.x + x*b.j[i];
return y;
}
__attribute__ ((visibility ("hidden")))
val_withgrad_t<NGRAD> operator*( double b ) const
{
val_withgrad_t<NGRAD> y;
y.x = x * b;
for(int i=0; i<NGRAD; i++)
y.j[i] = j[i]*b;
return y;
}
__attribute__ ((visibility ("hidden")))
void operator*=( const val_withgrad_t<NGRAD>& b )
{
*this = (*this) * b;
}
__attribute__ ((visibility ("hidden")))
val_withgrad_t<NGRAD> operator/( const val_withgrad_t<NGRAD>& b ) const
{
val_withgrad_t<NGRAD> y;
y.x = x / b.x;
for(int i=0; i<NGRAD; i++)
y.j[i] = (j[i]*b.x - x*b.j[i]) / (b.x*b.x);
return y;
}
__attribute__ ((visibility ("hidden")))
val_withgrad_t<NGRAD> operator/( double b ) const
{
return (*this) * (1./b);
}
__attribute__ ((visibility ("hidden")))
val_withgrad_t<NGRAD> sqrt(void) const
{
val_withgrad_t<NGRAD> y;
y.x = ::sqrt(x);
for(int i=0; i<NGRAD; i++)
y.j[i] = j[i] / (2. * y.x);
return y;
}
__attribute__ ((visibility ("hidden")))
val_withgrad_t<NGRAD> square(void) const
{
val_withgrad_t<NGRAD> s;
s.x = x*x;
for(int i=0; i<NGRAD; i++)
s.j[i] = 2. * x * j[i];
return s;
}
__attribute__ ((visibility ("hidden")))
val_withgrad_t<NGRAD> sin(void) const
{
const double s = ::sin(x);
const double c = ::cos(x);
val_withgrad_t<NGRAD> y;
y.x = s;
for(int i=0; i<NGRAD; i++)
y.j[i] = c*j[i];
return y;
}
__attribute__ ((visibility ("hidden")))
val_withgrad_t<NGRAD> cos(void) const
{
const double s = ::sin(x);
const double c = ::cos(x);
val_withgrad_t<NGRAD> y;
y.x = c;
for(int i=0; i<NGRAD; i++)
y.j[i] = -s*j[i];
return y;
}
__attribute__ ((visibility ("hidden")))
vec_withgrad_t<NGRAD, 2> sincos(void) const
{
const double s = ::sin(x);
const double c = ::cos(x);
vec_withgrad_t<NGRAD, 2> sc;
sc.v[0].x = s;
sc.v[1].x = c;
for(int i=0; i<NGRAD; i++)
{
sc.v[0].j[i] = c*j[i];
sc.v[1].j[i] = -s*j[i];
}
return sc;
}
__attribute__ ((visibility ("hidden")))
val_withgrad_t<NGRAD> tan(void) const
{
const double s = ::sin(x);
const double c = ::cos(x);
val_withgrad_t<NGRAD> y;
y.x = s/c;
for(int i=0; i<NGRAD; i++)
y.j[i] = j[i] / (c*c);
return y;
}
__attribute__ ((visibility ("hidden")))
val_withgrad_t<NGRAD> atan2(val_withgrad_t<NGRAD>& x) const
{
val_withgrad_t<NGRAD> th;
const val_withgrad_t<NGRAD>& y = *this;
th.x = ::atan2(y.x, x.x);
// dth/dv = d/dv atan2(y,x)
// = d/dv atan(y/x)
// = 1 / (1 + y^2/x^2) d/dv (y/x)
// = x^2 / (x^2 + y^2) / x^2 * (dy/dv x - y dx/dv)
// = 1 / (x^2 + y^2) * (dy/dv x - y dx/dv)
double norm2 = y.x*y.x + x.x*x.x;
for(int i=0; i<NGRAD; i++)
th.j[i] = (y.j[i]*x.x - y.x*x.j[i]) / norm2;
return th;
}
__attribute__ ((visibility ("hidden")))
val_withgrad_t<NGRAD> acos(void) const
{
val_withgrad_t<NGRAD> th;
th.x = ::acos(x);
double dacos_dx = -1. / ::sqrt( 1. - x*x );
for(int i=0; i<NGRAD; i++)
th.j[i] = dacos_dx * j[i];
return th;
}
__attribute__ ((visibility ("hidden")))
val_withgrad_t<NGRAD> sinx_over_x(// To avoid recomputing it
const val_withgrad_t<NGRAD>& sinx) const
{
// For small x I need special-case logic. In the limit as x->0 I have
// sin(x)/x -> 1. But I'm propagating gradients, so I need to capture
// that. I have
//
// d(sin(x)/x)/dx =
// (x cos(x) - sin(x))/x^2
//
// As x -> 0 this is
//
// (cos(x) - x sin(x) - cos(x)) / (2x) =
// (- x sin(x)) / (2x) =
// -sin(x) / 2 =
// 0
//
// So for small x the gradient is 0
if(fabs(x) < 1e-8)
return val_withgrad_t<NGRAD>(1.0);
return sinx / (*this);
}
};
template<int NGRAD, int NVEC>
struct vec_withgrad_t
{
val_withgrad_t<NGRAD> v[NVEC];
vec_withgrad_t() {}
__attribute__ ((visibility ("hidden")))
void init_vars(const double* x_in, int ivar0, int Nvars, int i_gradvec0 = -1,
int stride = sizeof(double))
{
// Initializes vector entries ivar0..ivar0+Nvars-1 inclusive using the
// data in x_in[]. x_in[0] corresponds to vector entry ivar0. If
// i_gradvec0 >= 0 then vector ivar0 corresponds to gradient index
// i_gradvec0, with all subsequent entries being filled-in
// consecutively. It's very possible that NGRAD > Nvars. Initially the
// subset of the gradient array corresponding to variables
// i_gradvec0..i_gradvec0+Nvars-1 is an identity, with the rest being 0
memset((char*)&v[ivar0], 0, Nvars*sizeof(v[0]));
for(int i=ivar0; i<ivar0+Nvars; i++)
{
v[i].x = _P1(x_in,stride, i-ivar0);
if(i_gradvec0 >= 0)
v[i].j[i_gradvec0+i-ivar0] = 1.0;
}
}
__attribute__ ((visibility ("hidden")))
vec_withgrad_t(const double* x_in, int i_gradvec0 = -1,
int stride = sizeof(double))
{
init_vars(x_in, 0, NVEC, i_gradvec0, stride);
}
__attribute__ ((visibility ("hidden")))
val_withgrad_t<NGRAD>& operator[](int i)
{
return v[i];
}
__attribute__ ((visibility ("hidden")))
const val_withgrad_t<NGRAD>& operator[](int i) const
{
return v[i];
}
__attribute__ ((visibility ("hidden")))
void operator+=( const vec_withgrad_t<NGRAD,NVEC>& x )
{
(*this) = (*this) + x;
}
__attribute__ ((visibility ("hidden")))
vec_withgrad_t<NGRAD,NVEC> operator+( const vec_withgrad_t<NGRAD,NVEC>& x ) const
{
vec_withgrad_t<NGRAD,NVEC> p;
for(int i=0; i<NVEC; i++)
p[i] = v[i] + x.v[i];
return p;
}
__attribute__ ((visibility ("hidden")))
void operator+=( const val_withgrad_t<NGRAD>& x )
{
(*this) = (*this) + x;
}
__attribute__ ((visibility ("hidden")))
vec_withgrad_t<NGRAD,NVEC> operator+( const val_withgrad_t<NGRAD>& x ) const
{
vec_withgrad_t<NGRAD,NVEC> p;
for(int i=0; i<NVEC; i++)
p[i] = v[i] + x;
return p;
}
__attribute__ ((visibility ("hidden")))
void operator+=( double x )
{
(*this) = (*this) + x;
}
__attribute__ ((visibility ("hidden")))
vec_withgrad_t<NGRAD,NVEC> operator+( double x ) const
{
vec_withgrad_t<NGRAD,NVEC> p;
for(int i=0; i<NVEC; i++)
p[i] = v[i] + x;
return p;
}
__attribute__ ((visibility ("hidden")))
void operator-=( const vec_withgrad_t<NGRAD,NVEC>& x )
{
(*this) = (*this) - x;
}
__attribute__ ((visibility ("hidden")))
vec_withgrad_t<NGRAD,NVEC> operator-( const vec_withgrad_t<NGRAD,NVEC>& x ) const
{
vec_withgrad_t<NGRAD,NVEC> p;
for(int i=0; i<NVEC; i++)
p[i] = v[i] - x.v[i];
return p;
}
__attribute__ ((visibility ("hidden")))
void operator-=( const val_withgrad_t<NGRAD>& x )
{
(*this) = (*this) - x;
}
__attribute__ ((visibility ("hidden")))
vec_withgrad_t<NGRAD,NVEC> operator-( const val_withgrad_t<NGRAD>& x ) const
{
vec_withgrad_t<NGRAD,NVEC> p;
for(int i=0; i<NVEC; i++)
p[i] = v[i] - x;
return p;
}
__attribute__ ((visibility ("hidden")))
void operator-=( double x )
{
(*this) = (*this) - x;
}
__attribute__ ((visibility ("hidden")))
vec_withgrad_t<NGRAD,NVEC> operator-( double x ) const
{
vec_withgrad_t<NGRAD,NVEC> p;
for(int i=0; i<NVEC; i++)
p[i] = v[i] - x;
return p;
}
__attribute__ ((visibility ("hidden")))
void operator*=( const vec_withgrad_t<NGRAD,NVEC>& x )
{
(*this) = (*this) * x;
}
__attribute__ ((visibility ("hidden")))
vec_withgrad_t<NGRAD,NVEC> operator*( const vec_withgrad_t<NGRAD,NVEC>& x ) const
{
vec_withgrad_t<NGRAD,NVEC> p;
for(int i=0; i<NVEC; i++)
p[i] = v[i] * x.v[i];
return p;
}
__attribute__ ((visibility ("hidden")))
void operator*=( const val_withgrad_t<NGRAD>& x )
{
(*this) = (*this) * x;
}
__attribute__ ((visibility ("hidden")))
vec_withgrad_t<NGRAD,NVEC> operator*( const val_withgrad_t<NGRAD>& x ) const
{
vec_withgrad_t<NGRAD,NVEC> p;
for(int i=0; i<NVEC; i++)
p[i] = v[i] * x;
return p;
}
__attribute__ ((visibility ("hidden")))
void operator*=( double x )
{
(*this) = (*this) * x;
}
__attribute__ ((visibility ("hidden")))
vec_withgrad_t<NGRAD,NVEC> operator*( double x ) const
{
vec_withgrad_t<NGRAD,NVEC> p;
for(int i=0; i<NVEC; i++)
p[i] = v[i] * x;
return p;
}
__attribute__ ((visibility ("hidden")))
void operator/=( const vec_withgrad_t<NGRAD,NVEC>& x )
{
(*this) = (*this) / x;
}
__attribute__ ((visibility ("hidden")))
vec_withgrad_t<NGRAD,NVEC> operator/( const vec_withgrad_t<NGRAD,NVEC>& x ) const
{
vec_withgrad_t<NGRAD,NVEC> p;
for(int i=0; i<NVEC; i++)
p[i] = v[i] / x.v[i];
return p;
}
__attribute__ ((visibility ("hidden")))
void operator/=( const val_withgrad_t<NGRAD>& x )
{
(*this) = (*this) / x;
}
__attribute__ ((visibility ("hidden")))
vec_withgrad_t<NGRAD,NVEC> operator/( const val_withgrad_t<NGRAD>& x ) const
{
vec_withgrad_t<NGRAD,NVEC> p;
for(int i=0; i<NVEC; i++)
p[i] = v[i] / x;
return p;
}
__attribute__ ((visibility ("hidden")))
void operator/=( double x )
{
(*this) = (*this) / x;
}
__attribute__ ((visibility ("hidden")))
vec_withgrad_t<NGRAD,NVEC> operator/( double x ) const
{
vec_withgrad_t<NGRAD,NVEC> p;
for(int i=0; i<NVEC; i++)
p[i] = v[i] / x;
return p;
}
__attribute__ ((visibility ("hidden")))
val_withgrad_t<NGRAD> dot( const vec_withgrad_t<NGRAD,NVEC>& x) const
{
val_withgrad_t<NGRAD> d; // initializes to 0
for(int i=0; i<NVEC; i++)
{
val_withgrad_t<NGRAD> e = x.v[i]*v[i];
d += e;
}
return d;
}
__attribute__ ((visibility ("hidden")))
vec_withgrad_t<NGRAD,NVEC> cross( const vec_withgrad_t<NGRAD,NVEC>& x) const
{
vec_withgrad_t<NGRAD,NVEC> c;
c[0] = v[1]*x.v[2] - v[2]*x.v[1];
c[1] = v[2]*x.v[0] - v[0]*x.v[2];
c[2] = v[0]*x.v[1] - v[1]*x.v[0];
return c;
}
__attribute__ ((visibility ("hidden")))
val_withgrad_t<NGRAD> norm2(void) const
{
return dot(*this);
}
__attribute__ ((visibility ("hidden")))
val_withgrad_t<NGRAD> mag(void) const
{
val_withgrad_t<NGRAD> l2 = norm2();
return l2.sqrt();
}
__attribute__ ((visibility ("hidden")))
void extract_value(double* out,
int stride = sizeof(double),
int ivar0 = 0, int Nvars = NVEC) const
{
for(int i=ivar0; i<ivar0+Nvars; i++)
_P1(out,stride, i-ivar0) = v[i].x;
}
__attribute__ ((visibility ("hidden")))
void extract_grad(double* J,
int i_gradvec0, int N_gradout,
int ivar0,
int J_stride0, int J_stride1,
int Nvars = NVEC) const
{
for(int i=ivar0; i<ivar0+Nvars; i++)
for(int j=0; j<N_gradout; j++)
_P2(J,J_stride0,J_stride1, i-ivar0,j) = v[i].j[i_gradvec0+j];
}
};
template<int NGRAD>
static
vec_withgrad_t<NGRAD, 3>
cross( const vec_withgrad_t<NGRAD, 3>& a,
const vec_withgrad_t<NGRAD, 3>& b )
{
vec_withgrad_t<NGRAD, 3> c;
c.v[0] = a.v[1]*b.v[2] - a.v[2]*b.v[1];
c.v[1] = a.v[2]*b.v[0] - a.v[0]*b.v[2];
c.v[2] = a.v[0]*b.v[1] - a.v[1]*b.v[0];
return c;
}
template<int NGRAD>
static
val_withgrad_t<NGRAD>
cross_norm2( const vec_withgrad_t<NGRAD, 3>& a,
const vec_withgrad_t<NGRAD, 3>& b )
{
vec_withgrad_t<NGRAD, 3> c = cross(a,b);
return c.norm2();
}
template<int NGRAD>
static
val_withgrad_t<NGRAD>
cross_mag( const vec_withgrad_t<NGRAD, 3>& a,
const vec_withgrad_t<NGRAD, 3>& b )
{
vec_withgrad_t<NGRAD, 3> c = cross(a,b);
return c.mag();
}
|