-
Notifications
You must be signed in to change notification settings - Fork 1
/
deriv_2.glsl
520 lines (475 loc) · 11.8 KB
/
deriv_2.glsl
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
#ifndef DERIVATIVES_2_H_
#define DERIVATIVES_2_H_
// This file contains methods to compute the first and second order derivative
// of a scalar function using automatic forward differentiation
// The data is stored in a vec3
// v[0] contains the value of the function
// v[1] contains the first derivative
// v[2] contains the second derivative
//--------------------------------
// Prototypes
//--------------------------------
/**
* Creates a constant derivative number
* @param val The current value of the constant
*/
vec3 constD2(in float val);
/**
* Creates a derivative number stored in a vec3
* @param val The current value of the variable
*/
vec3 varD2(in float val);
vec3 add(in vec3 a, in vec3 b);
vec3 add(in vec3 a, in float b);
vec3 add(in float a, in vec3 b);
vec3 sub(in vec3 a, in vec3 b);
vec3 sub(in vec3 a, in float b);
vec3 sub(in float a, in vec3 b);
vec3 mult(in vec3 a, in vec3 b);
vec3 mult(in vec3 a, in float b);
vec3 mult(in float a, in vec3 b);
vec3 neg(in vec3 a);
vec3 div(in vec3 a, in vec3 b);
vec3 div(in vec3 a, in float b);
vec3 div(in float a, in vec3 b);
vec3 inv(in vec3 a);
vec3 a_pow(in vec3 a, in vec3 b);
vec3 a_pow(in vec3 a, in float b);
vec3 a_pow(in float a, in vec3 b);
vec3 a_ipow(in vec3 x, in int n);
vec3 a_min(in vec3 a, in vec3 b);
vec3 a_max(in vec3 a, in vec3 b);
vec3 a_exp2(in vec3 a);
vec3 a_inversesqrt(in vec3 a);
vec3 a_atan(in vec3 a);
vec3 a_sqrt(in vec3 a);
vec3 a_sinh(in vec3 a);
vec3 a_ceil(in vec3 a);
vec3 a_tan(in vec3 a);
vec3 a_asinh(in vec3 a);
vec3 a_asin(in vec3 a);
vec3 a_acosh(in vec3 a);
vec3 a_abs(in vec3 a);
vec3 a_exp(in vec3 a);
vec3 a_cosh(in vec3 a);
vec3 a_floor(in vec3 a);
vec3 a_log(in vec3 a);
vec3 a_atanh(in vec3 a);
vec3 a_log2(in vec3 a);
vec3 a_acos(in vec3 a);
vec3 a_tanh(in vec3 a);
vec3 a_cos(in vec3 a);
vec3 a_sin(in vec3 a);
vec3 a_atan2(in vec3 y, in vec3 x);
vec3 a_atan2(in vec3 y, in float x);
vec3 a_atan2(in float y, in vec3 x);
vec3 a_mix(in vec3 a, in vec3 b, in vec3 t);
vec3 a_mix(in vec3 a, in vec3 b, in float t);
vec3 a_mix(in vec3 a, in float b, in vec3 t);
vec3 a_mix(in vec3 a, in float b, in float t);
vec3 a_mix(in float a, in vec3 b, in vec3 t);
vec3 a_mix(in float a, in vec3 b, in float t);
vec3 a_mix(in float a, in float b, in vec3 t);
//--------------------------------
// Implementation
//--------------------------------
vec3 constD2(in float val)
{
vec3 c = vec3(0.0);
c[0] = val;
return c;
}
//--------------------------------
vec3 varD2(in float val)
{
return vec3(val, 1.0, 0.0);
}
//--------------------------------
vec3 add(in vec3 a, in vec3 b)
{
return a + b;
}
//--------------------------------
vec3 add(in vec3 a, in float b)
{
a[0] += b;
return a;
}
//--------------------------------
vec3 add(in float a, in vec3 b)
{
b[0] += a;
return b;
}
//--------------------------------
vec3 sub(in vec3 a, in vec3 b)
{
return a - b;
}
//--------------------------------
vec3 sub(in vec3 a, in float b)
{
a[0] -= b;
return a;
}
//--------------------------------
vec3 sub(in float a, in vec3 b)
{
b *= -1.0;
b[0] += a;
return b;
}
//--------------------------------
vec3 mult(in vec3 a, in vec3 b)
{
float v = a[0] * b[0];
float da = a[0]*b[1] + b[0]*a[1];
float dda = a[2]*b[0] + 2.0*a[1]*b[1] + a[0]*b[2];
return vec3(v,da,dda);
}
//--------------------------------
vec3 mult(in vec3 a, in float b)
{
return a*b;
}
//--------------------------------
vec3 mult(in float a, in vec3 b)
{
return a*b;
}
//--------------------------------
vec3 neg(in vec3 a)
{
return mult(-1.0,a);
}
//--------------------------------
vec3 div(in vec3 a, in vec3 b)
{
float v = a[0] / b[0];
float b2 = b[0] * b[0];
float da = a[1]/b[0] - a[0]*b[1]/b2;
float b3 = b2 * b[0];
float dda = a[2]/b[0] - 2.0*a[1]*b[1]/b2 - a[0]*b[2]/b2 + 2.0*a[0]*(b[1]*b[1])/b3;
return vec3(v,da,dda);
}
//--------------------------------
vec3 div(in vec3 a, in float b)
{
return a / b;
}
//--------------------------------
vec3 div(in float a, in vec3 b)
{
float v = a / b[0];
float b2 = b[0] * b[0];
float da = - a*b[1]/b2;
float b3 = b2 * b[0];
float dda = - a*b[2]/b2 + 2.0*a*(b[1]*b[1])/b3;
return vec3(v, da, dda);
}
//--------------------------------
vec3 inv(in vec3 a)
{
return div(1.0, a);
}
//--------------------------------
vec3 a_pow(in vec3 a, in vec3 b)
{
return a_exp(mult(b,a_log(a)));
}
//--------------------------------
vec3 a_pow(in vec3 a, in float b)
{
float v = pow(a[0], b); // value f(a(x))
float da = b*pow(a[0],b-1.0); // first derivative f'(a(x))
float dda = b*(b-1.0)*pow(a[0],b-2.0); // second derivative f''(a(x))
return vec3(v, da * a[1], da * a[2] + dda * a[1]*a[1]);
}
//--------------------------------
vec3 a_pow(in float a, in vec3 b)
{
return a_exp(mult(b,log(a)));
}
//--------------------------------
vec3 a_ipow(in vec3 x, in int n)
{
// based on https://en.wikipedia.org/wiki/Exponentiation_by_squaring
if (n < 0)
{
x = div(1.0,x);
n = -n;
}
if (n == 0)
{
return constD2(1.0);
}
vec3 y = constD2(1.0);
while (n > 1)
{
if (n % 2 == 0)
{
x = mult(x,x);
}
else
{
y = mult(x, y);
x = mult(x, x);
}
n = n / 2;
}
return mult(x, y);
}
//--------------------------------
vec3 a_min(in vec3 a, in vec3 b)
{
if(a[0] < b[0])
{
return a;
}
return b;
}
//--------------------------------
vec3 a_max(in vec3 a, in vec3 b)
{
if(a[0] > b[0])
{
return a;
}
return b;
}
//--------------------------------
vec3 a_exp2(in vec3 a)
{
float v = exp2(a[0]);
float da = log(2.0)*exp2(a[0]);
float dda = log(2.0)*log(2.0)*exp2(a[0]);
return vec3(v, da * a[1], da * a[2] + dda * a[1]*a[1]);
}
//--------------------------------
vec3 a_inversesqrt(in vec3 a)
{
float v = inversesqrt(a[0]);
float da = -0.5/pow(sqrt(a[0]),3.0);
float dda = 0.75/pow(sqrt(a[0]),5.0);
return vec3(v, da * a[1], da * a[2] + dda * a[1]*a[1]);
}
//--------------------------------
vec3 a_atan(in vec3 a)
{
float v = atan(a[0]);
float da = 1.0/(1.0 + a[0] * a[0]);
float dda = -2.0*a[0]/pow(1.0 + a[0] * a[0], 2.0);
return vec3(v, da * a[1], da * a[2] + dda * a[1]*a[1]);
}
//--------------------------------
vec3 a_sqrt(in vec3 a)
{
float v = sqrt(a[0]);
float da = 0.5/sqrt(a[0]);
float dda = -0.25/pow(sqrt(a[0]),3.0);
return vec3(v, da * a[1], da * a[2] + dda * a[1]*a[1]);
}
//--------------------------------
vec3 a_sinh(in vec3 a)
{
float v = sinh(a[0]);
float da = cosh(a[0]);
float dda = sinh(a[0]);
return vec3(v, da * a[1], da * a[2] + dda * a[1]*a[1]);
}
//--------------------------------
vec3 a_ceil(in vec3 a)
{
float v = ceil(a[0]);
float da = 0.0;
float dda = 0.0;
return vec3(v, da * a[1], da * a[2] + dda * a[1]*a[1]);
}
//--------------------------------
vec3 a_tan(in vec3 a)
{
float v = tan(a[0]);
float da = 1.0 + pow(tan(a[0]),2.0);
float dda = 2.0*tan(a[0])*(1.0 + pow(tan(a[0]),2.0));
return vec3(v, da * a[1], da * a[2] + dda * a[1]*a[1]);
}
//--------------------------------
vec3 a_asinh(in vec3 a)
{
float v = asinh(a[0]);
float da = 1.0/sqrt(1.0 + a[0] * a[0]);
float dda = -a[0]/pow(sqrt(1.0 + a[0] * a[0]),3.0);
return vec3(v, da * a[1], da * a[2] + dda * a[1]*a[1]);
}
//--------------------------------
vec3 a_asin(in vec3 a)
{
float v = asin(a[0]);
float da = 1.0/sqrt(1.0 - a[0] * a[0]);
float dda = a[0]/pow(sqrt(1.0 - a[0] * a[0]),3.0);
return vec3(v, da * a[1], da * a[2] + dda * a[1]*a[1]);
}
//--------------------------------
vec3 a_acosh(in vec3 a)
{
float v = acosh(a[0]);
float da = 1.0/sqrt(-1.0 + a[0] * a[0]);
float dda = -a[0]/pow(sqrt(-1.0 + a[0] * a[0]),3.0);
return vec3(v, da * a[1], da * a[2] + dda * a[1]*a[1]);
}
//--------------------------------
vec3 a_abs(in vec3 a)
{
float v = abs(a[0]);
float da = a[0] < 0.0 ? -1.0 : 1.0;
float dda = 0.0;
return vec3(v, da * a[1], da * a[2] + dda * a[1]*a[1]);
}
//--------------------------------
vec3 a_exp(in vec3 a)
{
float v = exp(a[0]);
float da = exp(a[0]);
float dda = exp(a[0]);
return vec3(v, da * a[1], da * a[2] + dda * a[1]*a[1]);
}
//--------------------------------
vec3 a_cosh(in vec3 a)
{
float v = cosh(a[0]);
float da = sinh(a[0]);
float dda = cosh(a[0]);
return vec3(v, da * a[1], da * a[2] + dda * a[1]*a[1]);
}
//--------------------------------
vec3 a_floor(in vec3 a)
{
float v = floor(a[0]);
float da = 0.0;
float dda = 0.0;
return vec3(v, da * a[1], da * a[2] + dda * a[1]*a[1]);
}
//--------------------------------
vec3 a_log(in vec3 a)
{
float v = log(a[0]);
float da = 1.0/a[0];
float dda = -1.0/(a[0] * a[0]);
return vec3(v, da * a[1], da * a[2] + dda * a[1]*a[1]);
}
//--------------------------------
vec3 a_atanh(in vec3 a)
{
float v = atanh(a[0]);
float da = 1.0/(1.0 - a[0] * a[0]);
float dda = 2.0*a[0]/pow(1.0 - a[0] * a[0],2.0);
return vec3(v, da * a[1], da * a[2] + dda * a[1]*a[1]);
}
//--------------------------------
vec3 a_log2(in vec3 a)
{
float v = log2(a[0]);
float da = 1.0/(a[0] * log(2.0));
float dda = -1.0/(a[0] * a[0] * log(2.0));
return vec3(v, da * a[1], da * a[2] + dda * a[1]*a[1]);
}
//--------------------------------
vec3 a_acos(in vec3 a)
{
float v = acos(a[0]);
float da = -1.0/sqrt(1.0 - a[0] * a[0]);
float dda = -a[0]/pow(sqrt(1.0 - a[0] * a[0]),3.0);
return vec3(v, da * a[1], da * a[2] + dda * a[1]*a[1]);
}
//--------------------------------
vec3 a_tanh(in vec3 a)
{
float v = tanh(a[0]);
float da = 1.0 - pow(tanh(a[0]),2.0);
float dda = -2.0*tanh(a[0])*(1.0 - pow(tanh(a[0]),2.0));
return vec3(v, da * a[1], da * a[2] + dda * a[1]*a[1]);
}
//--------------------------------
vec3 a_cos(in vec3 a)
{
float v = cos(a[0]);
float da = -sin(a[0]);
float dda = -cos(a[0]);
return vec3(v, da * a[1], da * a[2] + dda * a[1]*a[1]);
}
//--------------------------------
vec3 a_sin(in vec3 a)
{
float v = sin(a[0]);
float da = cos(a[0]);
float dda = -sin(a[0]);
return vec3(v, da * a[1], da * a[2] + dda * a[1]*a[1]);
}
//--------------------------------
vec3 a_atan2(in vec3 y, in vec3 x)
{
const float pi = 3.14159265;
// from https://en.wikipedia.org/wiki/Atan2
if(x[0] > 0.0)
{
vec3 n = a_sqrt(add(mult(x,x),mult(y,y)));
vec3 inner = div(y, add(n,x));
return mult(2.0,a_atan(inner));
}else if(x[0] <= 0.0 && abs(y[0]) > 1E-6)
{
vec3 n = a_sqrt(add(mult(x,x),mult(y,y)));
vec3 inner = div(sub(n,x),y);
return mult(2.0,a_atan(inner));
}else if(x[0] < 0.0 && abs(y[0]) <= 1E-6)
{
return constD2(pi);
}
// return 0 for undefined
return constD2(0.0);
}
//--------------------------------
vec3 a_atan2(in vec3 y, in float x)
{
return a_atan2(y,constD2(x));
}
//--------------------------------
vec3 a_atan2(in float y, in vec3 x)
{
return a_atan2(constD2(y),x);
}
//--------------------------------
vec3 a_mix(in vec3 a, in vec3 b, in vec3 t)
{
return add(mult(a, sub(1.0, t)), mult(b, t));
}
//--------------------------------
vec3 a_mix(in vec3 a, in vec3 b, in float t)
{
return add(mult(a, 1.0 - t), mult(b, t));
}
//--------------------------------
vec3 a_mix(in vec3 a, in float b, in vec3 t)
{
return add(mult(a, sub(1.0, t)), mult(b, t));
}
//--------------------------------
vec3 a_mix(in vec3 a, in float b, in float t)
{
return add(mult(a, 1.0 - t), b*t);
}
//--------------------------------
vec3 a_mix(in float a, in vec3 b, in vec3 t)
{
return add(mult(a, sub(1.0, t)), mult(b, t));
}
//--------------------------------
vec3 a_mix(in float a, in vec3 b, in float t)
{
return add(a * (1.0 - t), mult(b, t));
}
//--------------------------------
vec3 a_mix(in float a, in float b, in vec3 t)
{
return add(mult(a, sub(1.0, t)), mult(b, t));
}
#endif // DERIVATIVES_2_H_