-
Notifications
You must be signed in to change notification settings - Fork 1
/
autodiff.glsl
4205 lines (3881 loc) · 104 KB
/
autodiff.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
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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef AUTODIFF_H_
#define AUTODIFF_H_
// This file contains methods to compute the first and second order derivatives, gradient and hessian
// of a scalar valued multivariate (2-4 variables) and univariate function using automatic forward differentiation
//--------------------------------
// Types
//--------------------------------
//--------------------------------
// Hessian
//--------------------------------
// Data type to hold information about a scalar valued 2 dimensional function
// These should be created by the constH2 (for constants) and varH2 (for variables) helpers
struct HNum2
{
// The current value
float val;
// The current gradient
vec2 g;
// The current hessian
mat2 h;
};
//--------------------------------
// Data type to hold information about a scalar valued 3 dimensional function
// These should be created by the constH3 (for constants) and varH3 (for variables) helpers
struct HNum3
{
// The current value
float val;
// The current gradient
vec3 g;
// The current hessian
mat3 h;
};
//--------------------------------
// Data type to hold information about a scalar valued 4 dimensional function
// These should be created by the constH4 (for constants) and varH4 (for variables) helpers
struct HNum4
{
// The current value
float val;
// The current gradient
vec4 g;
// The current hessian
mat4 h;
};
//--------------------------------
// Gradient
//--------------------------------
// Data type to hold information about a scalar valued 2 dimensional function
// These should be created by the constG2 (for constants) and varG2 (for variables) helpers
struct GNum2
{
// The current value
float val;
// The current gradient
vec2 g;
};
//--------------------------------
// Data type to hold information about a scalar valued 3 dimensional function
// These should be created by the constG3 (for constants) and varG3 (for variables) helpers
struct GNum3
{
// The current value
float val;
// The current gradient
vec3 g;
};
//--------------------------------
// Data type to hold information about a scalar valued 4 dimensional function
// These should be created by the constG4 (for constants) and varG4 (for variables) helpers
struct GNum4
{
// The current value
float val;
// The current gradient
vec4 g;
};
//--------------------------------
// Prototypes
//--------------------------------
//--------------------------------
// Hessian
//--------------------------------
/**
* Creates a constant HNum2
* @param val The current value of the constant
*/
HNum2 constH2(in float val);
/**
* Creates a HNum2 corresponding to the variable with the given index
* @param val The current value of the variable
* @param index The variable's index
*/
HNum2 varH2(in float val, in int index);
/**
* Creates a HNum2 corresponding to the variable x (index = 0)
* @param val The current value of the variable
*/
HNum2 varH2x(in float val);
/**
* Creates a HNum2 corresponding to the variable y (index = 1)
* @param val The current value of the variable
*/
HNum2 varH2y(in float val);
HNum2 add(in HNum2 a, in HNum2 b);
HNum2 add(in HNum2 a, in float b);
HNum2 add(in float a, in HNum2 b);
HNum2 sub(in HNum2 a, in HNum2 b);
HNum2 sub(in HNum2 a, in float b);
HNum2 sub(in float a, in HNum2 b);
HNum2 mult(in HNum2 a, in HNum2 b);
HNum2 mult(in HNum2 a, in float b);
HNum2 mult(in float a, in HNum2 b);
HNum2 neg(in HNum2 a);
HNum2 div(in HNum2 a, in HNum2 b);
HNum2 div(in HNum2 a, in float b);
HNum2 div(in float a, in HNum2 b);
HNum2 inv(in HNum2 a);
HNum2 a_pow(in HNum2 a, in HNum2 b);
HNum2 a_pow(in HNum2 a, in float b);
HNum2 a_pow(in float a, in HNum2 b);
HNum2 a_ipow(in HNum2 x, in int n);
HNum2 a_min(in HNum2 a, in HNum2 b);
HNum2 a_max(in HNum2 a, in HNum2 b);
HNum2 a_exp2(in HNum2 a);
HNum2 a_inversesqrt(in HNum2 a);
HNum2 a_atan(in HNum2 a);
HNum2 a_sqrt(in HNum2 a);
HNum2 a_sinh(in HNum2 a);
HNum2 a_ceil(in HNum2 a);
HNum2 a_tan(in HNum2 a);
HNum2 a_asinh(in HNum2 a);
HNum2 a_asin(in HNum2 a);
HNum2 a_acosh(in HNum2 a);
HNum2 a_abs(in HNum2 a);
HNum2 a_exp(in HNum2 a);
HNum2 a_cosh(in HNum2 a);
HNum2 a_floor(in HNum2 a);
HNum2 a_log(in HNum2 a);
HNum2 a_atanh(in HNum2 a);
HNum2 a_log2(in HNum2 a);
HNum2 a_acos(in HNum2 a);
HNum2 a_tanh(in HNum2 a);
HNum2 a_cos(in HNum2 a);
HNum2 a_sin(in HNum2 a);
HNum2 a_atan2(in HNum2 y, in HNum2 x);
HNum2 a_atan2(in HNum2 y, in float x);
HNum2 a_atan2(in float y, in HNum2 x);
HNum2 a_mix(in HNum2 a, in HNum2 b, in HNum2 t);
HNum2 a_mix(in HNum2 a, in HNum2 b, in float t);
HNum2 a_mix(in HNum2 a, in float b, in HNum2 t);
HNum2 a_mix(in HNum2 a, in float b, in float t);
HNum2 a_mix(in float a, in HNum2 b, in HNum2 t);
HNum2 a_mix(in float a, in HNum2 b, in float t);
HNum2 a_mix(in float a, in float b, in HNum2 t);
/**
* Creates a constant HNum3
* @param val The current value of the constant
*/
HNum3 constH3(in float val);
/**
* Creates a HNum3 corresponding to the variable with the given index
* @param val The current value of the variable
* @param index The variable's index
*/
HNum3 varH3(in float val, in int index);
/**
* Creates a HNum3 corresponding to the variable x (index = 0)
* @param val The current value of the variable
*/
HNum3 varH3x(in float val);
/**
* Creates a HNum3 corresponding to the variable y (index = 1)
* @param val The current value of the variable
*/
HNum3 varH3y(in float val);
/**
* Creates a HNum3 corresponding to the variable z (index = 2)
* @param val The current value of the variable
*/
HNum3 varH3z(in float val);
HNum3 add(in HNum3 a, in HNum3 b);
HNum3 add(in HNum3 a, in float b);
HNum3 add(in float a, in HNum3 b);
HNum3 sub(in HNum3 a, in HNum3 b);
HNum3 sub(in HNum3 a, in float b);
HNum3 sub(in float a, in HNum3 b);
HNum3 mult(in HNum3 a, in HNum3 b);
HNum3 mult(in HNum3 a, in float b);
HNum3 mult(in float a, in HNum3 b);
HNum3 neg(in HNum3 a);
HNum3 div(in HNum3 a, in HNum3 b);
HNum3 div(in HNum3 a, in float b);
HNum3 div(in float a, in HNum3 b);
HNum3 inv(in HNum3 a);
HNum3 a_pow(in HNum3 a, in HNum3 b);
HNum3 a_pow(in HNum3 a, in float b);
HNum3 a_pow(in float a, in HNum3 b);
HNum3 a_ipow(in HNum3 x, in int n);
HNum3 a_min(in HNum3 a, in HNum3 b);
HNum3 a_max(in HNum3 a, in HNum3 b);
HNum3 a_exp2(in HNum3 a);
HNum3 a_inversesqrt(in HNum3 a);
HNum3 a_atan(in HNum3 a);
HNum3 a_sqrt(in HNum3 a);
HNum3 a_sinh(in HNum3 a);
HNum3 a_ceil(in HNum3 a);
HNum3 a_tan(in HNum3 a);
HNum3 a_asinh(in HNum3 a);
HNum3 a_asin(in HNum3 a);
HNum3 a_acosh(in HNum3 a);
HNum3 a_abs(in HNum3 a);
HNum3 a_exp(in HNum3 a);
HNum3 a_cosh(in HNum3 a);
HNum3 a_floor(in HNum3 a);
HNum3 a_log(in HNum3 a);
HNum3 a_atanh(in HNum3 a);
HNum3 a_log2(in HNum3 a);
HNum3 a_acos(in HNum3 a);
HNum3 a_tanh(in HNum3 a);
HNum3 a_cos(in HNum3 a);
HNum3 a_sin(in HNum3 a);
HNum3 a_atan2(in HNum3 y, in HNum3 x);
HNum3 a_atan2(in HNum3 y, in float x);
HNum3 a_atan2(in float y, in HNum3 x);
HNum3 a_mix(in HNum3 a, in HNum3 b, in HNum3 t);
HNum3 a_mix(in HNum3 a, in HNum3 b, in float t);
HNum3 a_mix(in HNum3 a, in float b, in HNum3 t);
HNum3 a_mix(in HNum3 a, in float b, in float t);
HNum3 a_mix(in float a, in HNum3 b, in HNum3 t);
HNum3 a_mix(in float a, in HNum3 b, in float t);
HNum3 a_mix(in float a, in float b, in HNum3 t);
/**
* Creates a constant HNum4
* @param val The current value of the constant
*/
HNum4 constH4(in float val);
/**
* Creates a HNum4 corresponding to the variable with the given index
* @param val The current value of the variable
* @param index The variable's index
*/
HNum4 varH4(in float val, in int index);
/**
* Creates a HNum4 corresponding to the variable x (index = 0)
* @param val The current value of the variable
*/
HNum4 varH4x(in float val);
/**
* Creates a HNum4 corresponding to the variable y (index = 1)
* @param val The current value of the variable
*/
HNum4 varH4y(in float val);
/**
* Creates a HNum4 corresponding to the variable z (index = 2)
* @param val The current value of the variable
*/
HNum4 varH4z(in float val);
/**
* Creates a HNum4 corresponding to the variable w (index = 3)
* @param val The current value of the variable
*/
HNum4 varH4w(in float val);
HNum4 add(in HNum4 a, in HNum4 b);
HNum4 add(in HNum4 a, in float b);
HNum4 add(in float a, in HNum4 b);
HNum4 sub(in HNum4 a, in HNum4 b);
HNum4 sub(in HNum4 a, in float b);
HNum4 sub(in float a, in HNum4 b);
HNum4 mult(in HNum4 a, in HNum4 b);
HNum4 mult(in HNum4 a, in float b);
HNum4 mult(in float a, in HNum4 b);
HNum4 neg(in HNum4 a);
HNum4 div(in HNum4 a, in HNum4 b);
HNum4 div(in HNum4 a, in float b);
HNum4 div(in float a, in HNum4 b);
HNum4 inv(in HNum4 a);
HNum4 a_pow(in HNum4 a, in HNum4 b);
HNum4 a_pow(in HNum4 a, in float b);
HNum4 a_pow(in float a, in HNum4 b);
HNum4 a_ipow(in HNum4 x, in int n);
HNum4 a_min(in HNum4 a, in HNum4 b);
HNum4 a_max(in HNum4 a, in HNum4 b);
HNum4 a_exp2(in HNum4 a);
HNum4 a_inversesqrt(in HNum4 a);
HNum4 a_atan(in HNum4 a);
HNum4 a_sqrt(in HNum4 a);
HNum4 a_sinh(in HNum4 a);
HNum4 a_ceil(in HNum4 a);
HNum4 a_tan(in HNum4 a);
HNum4 a_asinh(in HNum4 a);
HNum4 a_asin(in HNum4 a);
HNum4 a_acosh(in HNum4 a);
HNum4 a_abs(in HNum4 a);
HNum4 a_exp(in HNum4 a);
HNum4 a_cosh(in HNum4 a);
HNum4 a_floor(in HNum4 a);
HNum4 a_log(in HNum4 a);
HNum4 a_atanh(in HNum4 a);
HNum4 a_log2(in HNum4 a);
HNum4 a_acos(in HNum4 a);
HNum4 a_tanh(in HNum4 a);
HNum4 a_cos(in HNum4 a);
HNum4 a_sin(in HNum4 a);
HNum4 a_atan2(in HNum4 y, in HNum4 x);
HNum4 a_atan2(in HNum4 y, in float x);
HNum4 a_atan2(in float y, in HNum4 x);
HNum4 a_mix(in HNum4 a, in HNum4 b, in HNum4 t);
HNum4 a_mix(in HNum4 a, in HNum4 b, in float t);
HNum4 a_mix(in HNum4 a, in float b, in HNum4 t);
HNum4 a_mix(in HNum4 a, in float b, in float t);
HNum4 a_mix(in float a, in HNum4 b, in HNum4 t);
HNum4 a_mix(in float a, in HNum4 b, in float t);
HNum4 a_mix(in float a, in float b, in HNum4 t);
//--------------------------------
// Macros
//--------------------------------
#define HESSIAN2(f,x, y,result) { result = f(varH2x(x), varH2y(y)); }
//--------------------------------
#define HESSIAN3(f,x, y, z,result) { result = f(varH3x(x), varH3y(y), varH3z(z)); }
//--------------------------------
#define HESSIAN4(f,x, y, z, w,result) { result = f(varH4x(x), varH4y(y), varH4z(z), varH4w(w)); }
//--------------------------------
// Gradient
//--------------------------------
/**
* Creates a constant GNum2
* @param val The current value of the constant
*/
GNum2 constG2(in float val);
GNum2 varG2(in float val, in int index);
GNum2 varG2x(in float val);
GNum2 varG2y(in float val);
GNum2 add(in GNum2 a, in GNum2 b);
GNum2 add(in GNum2 a, in float b);
GNum2 add(in float a, in GNum2 b);
GNum2 sub(in GNum2 a, in GNum2 b);
GNum2 sub(in GNum2 a, in float b);
GNum2 sub(in float a, in GNum2 b);
GNum2 mult(in GNum2 a, in GNum2 b);
GNum2 mult(in GNum2 a, in float b);
GNum2 mult(in float a, in GNum2 b);
GNum2 neg(in GNum2 a);
GNum2 div(in GNum2 a, in GNum2 b);
GNum2 div(in GNum2 a, in float b);
GNum2 div(in float a, in GNum2 b);
GNum2 inv(in GNum2 a);
GNum2 a_pow(in GNum2 a, in GNum2 b);
GNum2 a_pow(in GNum2 a, in float b);
GNum2 a_pow(in float a, in GNum2 b);
GNum2 a_ipow(in GNum2 x, in int n);
GNum2 a_min(in GNum2 a, in GNum2 b);
GNum2 a_max(in GNum2 a, in GNum2 b);
GNum2 a_exp2(in GNum2 a);
GNum2 a_inversesqrt(in GNum2 a);
GNum2 a_atan(in GNum2 a);
GNum2 a_sqrt(in GNum2 a);
GNum2 a_sinh(in GNum2 a);
GNum2 a_ceil(in GNum2 a);
GNum2 a_tan(in GNum2 a);
GNum2 a_asinh(in GNum2 a);
GNum2 a_asin(in GNum2 a);
GNum2 a_acosh(in GNum2 a);
GNum2 a_abs(in GNum2 a);
GNum2 a_exp(in GNum2 a);
GNum2 a_cosh(in GNum2 a);
GNum2 a_floor(in GNum2 a);
GNum2 a_log(in GNum2 a);
GNum2 a_atanh(in GNum2 a);
GNum2 a_log2(in GNum2 a);
GNum2 a_acos(in GNum2 a);
GNum2 a_tanh(in GNum2 a);
GNum2 a_cos(in GNum2 a);
GNum2 a_sin(in GNum2 a);
GNum2 a_atan2(in GNum2 y, in GNum2 x);
GNum2 a_atan2(in GNum2 y, in float x);
GNum2 a_atan2(in float y, in GNum2 x);
GNum2 a_mix(in GNum2 a, in GNum2 b, in GNum2 t);
GNum2 a_mix(in GNum2 a, in GNum2 b, in float t);
GNum2 a_mix(in GNum2 a, in float b, in GNum2 t);
GNum2 a_mix(in GNum2 a, in float b, in float t);
GNum2 a_mix(in float a, in GNum2 b, in GNum2 t);
GNum2 a_mix(in float a, in GNum2 b, in float t);
GNum2 a_mix(in float a, in float b, in GNum2 t);
/**
* Creates a constant GNum3
* @param val The current value of the constant
*/
GNum3 constG3(in float val);
GNum3 varG3(in float val, in int index);
GNum3 varG3x(in float val);
GNum3 varG3y(in float val);
GNum3 varG3z(in float val);
GNum3 add(in GNum3 a, in GNum3 b);
GNum3 add(in GNum3 a, in float b);
GNum3 add(in float a, in GNum3 b);
GNum3 sub(in GNum3 a, in GNum3 b);
GNum3 sub(in GNum3 a, in float b);
GNum3 sub(in float a, in GNum3 b);
GNum3 mult(in GNum3 a, in GNum3 b);
GNum3 mult(in GNum3 a, in float b);
GNum3 mult(in float a, in GNum3 b);
GNum3 neg(in GNum3 a);
GNum3 div(in GNum3 a, in GNum3 b);
GNum3 div(in GNum3 a, in float b);
GNum3 div(in float a, in GNum3 b);
GNum3 inv(in GNum3 a);
GNum3 a_pow(in GNum3 a, in GNum3 b);
GNum3 a_pow(in GNum3 a, in float b);
GNum3 a_pow(in float a, in GNum3 b);
GNum3 a_ipow(in GNum3 x, in int n);
GNum3 a_min(in GNum3 a, in GNum3 b);
GNum3 a_max(in GNum3 a, in GNum3 b);
GNum3 a_exp2(in GNum3 a);
GNum3 a_inversesqrt(in GNum3 a);
GNum3 a_atan(in GNum3 a);
GNum3 a_sqrt(in GNum3 a);
GNum3 a_sinh(in GNum3 a);
GNum3 a_ceil(in GNum3 a);
GNum3 a_tan(in GNum3 a);
GNum3 a_asinh(in GNum3 a);
GNum3 a_asin(in GNum3 a);
GNum3 a_acosh(in GNum3 a);
GNum3 a_abs(in GNum3 a);
GNum3 a_exp(in GNum3 a);
GNum3 a_cosh(in GNum3 a);
GNum3 a_floor(in GNum3 a);
GNum3 a_log(in GNum3 a);
GNum3 a_atanh(in GNum3 a);
GNum3 a_log2(in GNum3 a);
GNum3 a_acos(in GNum3 a);
GNum3 a_tanh(in GNum3 a);
GNum3 a_cos(in GNum3 a);
GNum3 a_sin(in GNum3 a);
GNum3 a_atan2(in GNum3 y, in GNum3 x);
GNum3 a_atan2(in GNum3 y, in float x);
GNum3 a_atan2(in float y, in GNum3 x);
GNum3 a_mix(in GNum3 a, in GNum3 b, in GNum3 t);
GNum3 a_mix(in GNum3 a, in GNum3 b, in float t);
GNum3 a_mix(in GNum3 a, in float b, in GNum3 t);
GNum3 a_mix(in GNum3 a, in float b, in float t);
GNum3 a_mix(in float a, in GNum3 b, in GNum3 t);
GNum3 a_mix(in float a, in GNum3 b, in float t);
GNum3 a_mix(in float a, in float b, in GNum3 t);
/**
* Creates a constant GNum4
* @param val The current value of the constant
*/
GNum4 constG4(in float val);
GNum4 varG4(in float val, in int index);
GNum4 varG4x(in float val);
GNum4 varG4y(in float val);
GNum4 varG4z(in float val);
GNum4 varG4w(in float val);
GNum4 add(in GNum4 a, in GNum4 b);
GNum4 add(in GNum4 a, in float b);
GNum4 add(in float a, in GNum4 b);
GNum4 sub(in GNum4 a, in GNum4 b);
GNum4 sub(in GNum4 a, in float b);
GNum4 sub(in float a, in GNum4 b);
GNum4 mult(in GNum4 a, in GNum4 b);
GNum4 mult(in GNum4 a, in float b);
GNum4 mult(in float a, in GNum4 b);
GNum4 neg(in GNum4 a);
GNum4 div(in GNum4 a, in GNum4 b);
GNum4 div(in GNum4 a, in float b);
GNum4 div(in float a, in GNum4 b);
GNum4 inv(in GNum4 a);
GNum4 a_pow(in GNum4 a, in GNum4 b);
GNum4 a_pow(in GNum4 a, in float b);
GNum4 a_pow(in float a, in GNum4 b);
GNum4 a_ipow(in GNum4 x, in int n);
GNum4 a_min(in GNum4 a, in GNum4 b);
GNum4 a_max(in GNum4 a, in GNum4 b);
GNum4 a_exp2(in GNum4 a);
GNum4 a_inversesqrt(in GNum4 a);
GNum4 a_atan(in GNum4 a);
GNum4 a_sqrt(in GNum4 a);
GNum4 a_sinh(in GNum4 a);
GNum4 a_ceil(in GNum4 a);
GNum4 a_tan(in GNum4 a);
GNum4 a_asinh(in GNum4 a);
GNum4 a_asin(in GNum4 a);
GNum4 a_acosh(in GNum4 a);
GNum4 a_abs(in GNum4 a);
GNum4 a_exp(in GNum4 a);
GNum4 a_cosh(in GNum4 a);
GNum4 a_floor(in GNum4 a);
GNum4 a_log(in GNum4 a);
GNum4 a_atanh(in GNum4 a);
GNum4 a_log2(in GNum4 a);
GNum4 a_acos(in GNum4 a);
GNum4 a_tanh(in GNum4 a);
GNum4 a_cos(in GNum4 a);
GNum4 a_sin(in GNum4 a);
GNum4 a_atan2(in GNum4 y, in GNum4 x);
GNum4 a_atan2(in GNum4 y, in float x);
GNum4 a_atan2(in float y, in GNum4 x);
GNum4 a_mix(in GNum4 a, in GNum4 b, in GNum4 t);
GNum4 a_mix(in GNum4 a, in GNum4 b, in float t);
GNum4 a_mix(in GNum4 a, in float b, in GNum4 t);
GNum4 a_mix(in GNum4 a, in float b, in float t);
GNum4 a_mix(in float a, in GNum4 b, in GNum4 t);
GNum4 a_mix(in float a, in GNum4 b, in float t);
GNum4 a_mix(in float a, in float b, in GNum4 t);
//--------------------------------
// Univariate
//--------------------------------
/**
* Creates a constant derivative number
* @param val The current value of the constant
*/
vec2 constD1(in float val);
/**
* Creates a derivative number stored in a vec2
* @param val The current value of the variable
*/
vec2 varD1(in float val);
vec2 add(in vec2 a, in vec2 b);
vec2 add(in vec2 a, in float b);
vec2 add(in float a, in vec2 b);
vec2 sub(in vec2 a, in vec2 b);
vec2 sub(in vec2 a, in float b);
vec2 sub(in float a, in vec2 b);
vec2 mult(in vec2 a, in vec2 b);
vec2 mult(in vec2 a, in float b);
vec2 mult(in float a, in vec2 b);
vec2 neg(in vec2 a);
vec2 div(in vec2 a, in vec2 b);
vec2 div(in vec2 a, in float b);
vec2 div(in float a, in vec2 b);
vec2 inv(in vec2 a);
vec2 a_pow(in vec2 a, in vec2 b);
vec2 a_pow(in vec2 a, in float b);
vec2 a_pow(in float a, in vec2 b);
vec2 a_ipow(in vec2 x, in int n);
vec2 a_min(in vec2 a, in vec2 b);
vec2 a_max(in vec2 a, in vec2 b);
vec2 a_exp2(in vec2 a);
vec2 a_inversesqrt(in vec2 a);
vec2 a_atan(in vec2 a);
vec2 a_sqrt(in vec2 a);
vec2 a_sinh(in vec2 a);
vec2 a_ceil(in vec2 a);
vec2 a_tan(in vec2 a);
vec2 a_asinh(in vec2 a);
vec2 a_asin(in vec2 a);
vec2 a_acosh(in vec2 a);
vec2 a_abs(in vec2 a);
vec2 a_exp(in vec2 a);
vec2 a_cosh(in vec2 a);
vec2 a_floor(in vec2 a);
vec2 a_log(in vec2 a);
vec2 a_atanh(in vec2 a);
vec2 a_log2(in vec2 a);
vec2 a_acos(in vec2 a);
vec2 a_tanh(in vec2 a);
vec2 a_cos(in vec2 a);
vec2 a_sin(in vec2 a);
vec2 a_atan2(in vec2 y, in vec2 x);
vec2 a_atan2(in vec2 y, in float x);
vec2 a_atan2(in float y, in vec2 x);
vec2 a_mix(in vec2 a, in vec2 b, in vec2 t);
vec2 a_mix(in vec2 a, in vec2 b, in float t);
vec2 a_mix(in vec2 a, in float b, in vec2 t);
vec2 a_mix(in vec2 a, in float b, in float t);
vec2 a_mix(in float a, in vec2 b, in vec2 t);
vec2 a_mix(in float a, in vec2 b, in float t);
vec2 a_mix(in float a, in float b, in vec2 t);
/**
* 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);
//--------------------------------
// Utilities prototypes
//--------------------------------
mat2 a_outerProduct(in vec2 a, in vec2 b);
mat3 a_outerProduct(in vec3 a, in vec3 b);
mat4 a_outerProduct(in vec4 a, in vec4 b);
//--------------------------------
// Implementation
//--------------------------------
//--------------------------------
// Hessian
//--------------------------------
HNum2 constH2(in float val)
{
return HNum2(val, vec2(0.0), mat2(0.0));
}
//--------------------------------
HNum2 varH2(in float val, in int index)
{
vec2 g = vec2(0.0);
g[index] = 1.0;
return HNum2(val, g, mat2(0.0));
}
//--------------------------------
HNum2 varH2x(in float val)
{
vec2 g = vec2(0.0);
g[0] = 1.0;
return HNum2(val, g, mat2(0.0));
}
//--------------------------------
HNum2 varH2y(in float val)
{
vec2 g = vec2(0.0);
g[1] = 1.0;
return HNum2(val, g, mat2(0.0));
}
//--------------------------------
HNum2 add(in HNum2 a, in HNum2 b)
{
return HNum2(a.val + b.val , a.g + b.g, a.h + b.h);
}
//--------------------------------
HNum2 add(in HNum2 a, in float b)
{
return HNum2(a.val + b , a.g, a.h);
}
//--------------------------------
HNum2 add(in float a, in HNum2 b)
{
return HNum2(a + b.val , b.g, b.h);
}
//--------------------------------
HNum2 sub(in HNum2 a, in HNum2 b)
{
return HNum2(a.val - b.val , a.g - b.g, a.h - b.h);
}
//--------------------------------
HNum2 sub(in HNum2 a, in float b)
{
return HNum2(a.val - b , a.g, a.h);
}
//--------------------------------
HNum2 sub(in float a, in HNum2 b)
{
return HNum2(a - b.val , - b.g, - b.h);
}
//--------------------------------
HNum2 mult(in HNum2 a, in HNum2 b)
{
return HNum2(a.val * b.val,
a.val*b.g + b.val*a.g,
a.val*b.h + b.val*a.h + a_outerProduct(b.g,a.g) + a_outerProduct(a.g,b.g)
);
}
//--------------------------------
HNum2 mult(in HNum2 a, in float b)
{
return HNum2(a.val * b, b*a.g, b*a.h);
}
//--------------------------------
HNum2 mult(in float a, in HNum2 b)
{
return HNum2(a * b.val, a*b.g, a*b.h);
}
//--------------------------------
HNum2 neg(in HNum2 a)
{
return mult(-1.0,a);
}
//--------------------------------
HNum2 div(in HNum2 a, in HNum2 b)
{
float b1 = b.val;
float b2 = b1*b1;
float b3 = b2*b1;
return HNum2(a.val / b.val ,
(b.val*a.g - a.val*b.g)/b2,
2.0*a.val/b3*a_outerProduct(b.g,b.g)
- a.val/b2*b.h
+ a.h/b1
- a_outerProduct(b.g/b2, a.g)
- a_outerProduct(a.g/b2, b.g)
);
}
//--------------------------------
HNum2 div(in HNum2 a, in float b)
{
return HNum2(a.val / b, a.g/b, a.h/b);
}
//--------------------------------
HNum2 div(in float a, in HNum2 b)
{
float b1 = b.val;
float b2 = b1*b1;
float b3 = b2*b1;
return HNum2(a / b.val,
-a*b.g/b2,
2.0*a/b3*a_outerProduct(b.g,b.g) - a/b2*b.h
);
}
//--------------------------------
HNum2 inv(in HNum2 a)
{
return div(1.0, a);
}
//--------------------------------
HNum2 a_pow(in HNum2 a, in HNum2 b)
{
return a_exp(mult(b,a_log(a)));
}
//--------------------------------
HNum2 a_pow(in HNum2 a, in float b)
{
// constant exponent -> make special case
float v = pow(a.val, b); // value f(a(x))
float da = b*pow(a.val,b-1.0); // first derivative f'(a(x))
float dda = b*(b-1.0)*pow(a.val,b-2.0); // second derivative f''(a(x))
return HNum2(v , da * a.g, da * a.h + dda * a_outerProduct(a.g,a.g));
}
//--------------------------------
HNum2 a_pow(in float a, in HNum2 b)
{
return a_exp(mult(b,log(a)));
}
//--------------------------------
HNum2 a_ipow(in HNum2 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 constH2(1.0);
}
HNum2 y = constH2(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);
}
//--------------------------------
HNum2 a_min(in HNum2 a, in HNum2 b)
{
if(a.val < b.val)
{
return a;
}
return b;
}
//--------------------------------
HNum2 a_max(in HNum2 a, in HNum2 b)
{
if(a.val > b.val)
{
return a;
}
return b;
}
//--------------------------------
HNum2 a_exp2(in HNum2 a)
{
float v = exp2(a.val); // value f(a(x))
float da = log(2.0)*exp2(a.val); // first derivative f'(a(x))
float dda = log(2.0)*log(2.0)*exp2(a.val); // second derivative f''(a(x))
return HNum2(v , da * a.g, da * a.h + dda * a_outerProduct(a.g,a.g));
}
//--------------------------------
HNum2 a_inversesqrt(in HNum2 a)
{
float v = inversesqrt(a.val); // value f(a(x))
float da = -0.5/pow(sqrt(a.val),3.0); // first derivative f'(a(x))
float dda = 0.75/pow(sqrt(a.val),5.0); // second derivative f''(a(x))
return HNum2(v , da * a.g, da * a.h + dda * a_outerProduct(a.g,a.g));
}
//--------------------------------
HNum2 a_atan(in HNum2 a)
{
float v = atan(a.val); // value f(a(x))
float da = 1.0/(1.0 + a.val * a.val); // first derivative f'(a(x))
float dda = -2.0*a.val/pow(1.0 + a.val * a.val, 2.0); // second derivative f''(a(x))
return HNum2(v , da * a.g, da * a.h + dda * a_outerProduct(a.g,a.g));
}
//--------------------------------
HNum2 a_sqrt(in HNum2 a)
{
float v = sqrt(a.val); // value f(a(x))
float da = 0.5/sqrt(a.val); // first derivative f'(a(x))
float dda = -0.25/pow(sqrt(a.val),3.0); // second derivative f''(a(x))
return HNum2(v , da * a.g, da * a.h + dda * a_outerProduct(a.g,a.g));
}
//--------------------------------
HNum2 a_sinh(in HNum2 a)
{
float v = sinh(a.val); // value f(a(x))
float da = cosh(a.val); // first derivative f'(a(x))
float dda = sinh(a.val); // second derivative f''(a(x))
return HNum2(v , da * a.g, da * a.h + dda * a_outerProduct(a.g,a.g));
}
//--------------------------------
HNum2 a_ceil(in HNum2 a)
{
float v = ceil(a.val); // value f(a(x))
float da = 0.0; // first derivative f'(a(x))
float dda = 0.0; // second derivative f''(a(x))
return HNum2(v , da * a.g, da * a.h + dda * a_outerProduct(a.g,a.g));
}
//--------------------------------
HNum2 a_tan(in HNum2 a)
{
float v = tan(a.val); // value f(a(x))
float da = 1.0 + pow(tan(a.val),2.0); // first derivative f'(a(x))
float dda = 2.0*tan(a.val)*(1.0 + pow(tan(a.val),2.0)); // second derivative f''(a(x))
return HNum2(v , da * a.g, da * a.h + dda * a_outerProduct(a.g,a.g));
}
//--------------------------------
HNum2 a_asinh(in HNum2 a)
{
float v = asinh(a.val); // value f(a(x))
float da = 1.0/sqrt(1.0 + a.val * a.val); // first derivative f'(a(x))
float dda = -a.val/pow(sqrt(1.0 + a.val * a.val),3.0); // second derivative f''(a(x))
return HNum2(v , da * a.g, da * a.h + dda * a_outerProduct(a.g,a.g));
}
//--------------------------------
HNum2 a_asin(in HNum2 a)
{
float v = asin(a.val); // value f(a(x))
float da = 1.0/sqrt(1.0 - a.val * a.val); // first derivative f'(a(x))
float dda = a.val/pow(sqrt(1.0 - a.val * a.val),3.0); // second derivative f''(a(x))
return HNum2(v , da * a.g, da * a.h + dda * a_outerProduct(a.g,a.g));
}
//--------------------------------
HNum2 a_acosh(in HNum2 a)
{
float v = acosh(a.val); // value f(a(x))
float da = 1.0/sqrt(-1.0 + a.val * a.val); // first derivative f'(a(x))
float dda = -a.val/pow(sqrt(-1.0 + a.val * a.val),3.0); // second derivative f''(a(x))
return HNum2(v , da * a.g, da * a.h + dda * a_outerProduct(a.g,a.g));
}
//--------------------------------
HNum2 a_abs(in HNum2 a)
{
float v = abs(a.val); // value f(a(x))
float da = a.val < 0.0 ? -1.0 : 1.0; // first derivative f'(a(x))
float dda = 0.0; // second derivative f''(a(x))
return HNum2(v , da * a.g, da * a.h + dda * a_outerProduct(a.g,a.g));
}
//--------------------------------
HNum2 a_exp(in HNum2 a)
{
float v = exp(a.val); // value f(a(x))
float da = exp(a.val); // first derivative f'(a(x))
float dda = exp(a.val); // second derivative f''(a(x))
return HNum2(v , da * a.g, da * a.h + dda * a_outerProduct(a.g,a.g));
}
//--------------------------------
HNum2 a_cosh(in HNum2 a)
{
float v = cosh(a.val); // value f(a(x))
float da = sinh(a.val); // first derivative f'(a(x))
float dda = cosh(a.val); // second derivative f''(a(x))
return HNum2(v , da * a.g, da * a.h + dda * a_outerProduct(a.g,a.g));
}
//--------------------------------
HNum2 a_floor(in HNum2 a)
{
float v = floor(a.val); // value f(a(x))
float da = 0.0; // first derivative f'(a(x))
float dda = 0.0; // second derivative f''(a(x))
return HNum2(v , da * a.g, da * a.h + dda * a_outerProduct(a.g,a.g));
}
//--------------------------------
HNum2 a_log(in HNum2 a)
{
float v = log(a.val); // value f(a(x))
float da = 1.0/a.val; // first derivative f'(a(x))
float dda = -1.0/(a.val * a.val); // second derivative f''(a(x))
return HNum2(v , da * a.g, da * a.h + dda * a_outerProduct(a.g,a.g));
}
//--------------------------------
HNum2 a_atanh(in HNum2 a)
{
float v = atanh(a.val); // value f(a(x))
float da = 1.0/(1.0 - a.val * a.val); // first derivative f'(a(x))
float dda = 2.0*a.val/pow(1.0 - a.val * a.val,2.0); // second derivative f''(a(x))
return HNum2(v , da * a.g, da * a.h + dda * a_outerProduct(a.g,a.g));
}
//--------------------------------
HNum2 a_log2(in HNum2 a)
{
float v = log2(a.val); // value f(a(x))
float da = 1.0/(a.val * log(2.0)); // first derivative f'(a(x))
float dda = -1.0/(a.val * a.val * log(2.0)); // second derivative f''(a(x))