-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdnad.F90
executable file
·1818 lines (1388 loc) · 52 KB
/
dnad.F90
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
!******************************************************************************
!* Dual Number Automatic Differentiation (DNAD) of Fortran Codes
!*-----------------------------------------------------------------------------
!* COPYRIGHT (c) Joshua Hodson, All rights reserved, you are free to copy,
!* modify, or translate this code to other languages such as c/c++. This is a
!* fork of the original Fortran DNAD module developed by Dr. Wenbin Yu. See
!* original copyright information below. You can download the original version
!* at https://cdmhub.org/resources/374
!*
!* COPYRIGHT (c) Wenbin Yu, All rights reserved, you are free to copy,
!* modify or translate this code to other languages such as c/c++. If
!* you find a bug please let me know through [email protected]. If
!* you added new functions and want to share with others, please let me know
!* too. You are welcome to share your successful stories with us through
!* http://groups.google.com/group/hifi-comp.
!******************************************************************************
!* Acknowledgements
!*-----------------------------------------------------------------------------
!* The development of DNAD is supported, in part, by the Chief Scientist
!* Innovative Research Fund at AFRL/RB WPAFB, and by Department of Army
!* SBIR (Topic A08-022) through Advanced Dynamics Inc. The views and
!* conclusions contained herein are those of the authors and should not be
!* interpreted as necessarily representing the official policies or
!* endorsement, either expressed or implied, of the funding agency.
!*
!* Additional development of DNAD has been supported under a Department of
!* Energy (DOE) Nuclear Energy University Program (NEUP) Graduate Fellowship.
!* Any opinions, findings, conclusions or recommendations expressed in this
!* publication are those of the authors and do not necessarily reflect the
!* views of the Department of Energy Office of Nuclear Energy.
!******************************************************************************
!* Citation
!*-----------------------------------------------------------------------------
!* Your citation of the following two papers is appreciated:
!* Yu, W. and Blair, M.: "DNAD, a Simple Tool for Automatic Differentiation of
!* Fortran Codes Using Dual Numbers," Computer Physics Communications, vol.
!* 184, 2013, pp. 1446-1452.
!*
!* Spall, R. and Yu, W.: "Imbedded Dual-Number Automatic Differentiation for
!* CFD Sensitivity Analysis," Journal of Fluids Engineering, vol. 135, 2013,
!* 014501.
!******************************************************************************
!* Quick Start Guide
!*-----------------------------------------------------------------------------
!* To integrate DNAD into an existing Fortran program, do the following:
!*
!* 1. Include the DNAD module in the source files by adding "use dnadmod" to
!* the beginning of all modules, global functions, and global subroutines
!* that include definitions of floating-point variables.
!* 2. Redefine all floating-point variables as type(dual). This can be done
!* using precompiler directives so that the integration can be turned on
!* or off at compile-time, eliminating the need for maintaining two
!* separate code bases for the same project.
!* 3. All I/O involving floating-point variables will need to be examined.
!* A method will need to be determined for inputting and outputting
!* derivative values. This customization is typically unique for each
!* piece of software and needs to be determined on a case-by-case basis.
!* 4. When compiling DNAD, use the compiler option "-Dndv=#", where # is the
!* number of design variables desired. This sizes the derivative array
!* that is stored with each floating point number.
!* 5. When compiling DNAD, use compiler options to specify precision. If no
!* compiler options are specified, DNAD will default to single-precision
!* floating-point arithmetic. Most popular Fortran compilers provide
!* options for specifying precision at compile-time so that it does not
!* have to be hard-coded into the source code. For example, use the
!* "-fdefault-real-8" compiler in gfortran or the "-r8" compiler option
!* with Intel Fortran to compile DNAD as double-precision.
!* 6. Modify the compilation process for the target software to include the
!* DNAD module in the resulting executable or library.
!******************************************************************************
!* Change Log
!*-----------------------------------------------------------------------------
!*
!* 2016-04-29 Joshua Hodson
!* - Updated copyright, acknowledgments, and quick start guide.
!* - Removed overloads for single-precision reals.
!* - Added tan, dtan, atan, and atan2 intrinsic function overloads.
!* - Removed macro for precision and defined all floating-point variables as
!* default real. Compiler options can now be used to set precision.
!* - Added checks for undefined derivatives when only constants are used in
!* the calculation (i.e. all partial derivatives are zero). This limits the
!* perpetuation of NaN values in the code.
!* - Combined the header and source files into a single file.
!*
!* 2015-07-29 Joshua Hodson
!* - Added maxloc intrinsic function overload.
!* - Converted UPPERCASE to lowercase for readability.
!* - Added macros for defining precision and number of design variables.
!* - Renamed module from Dual_Num_Auto_Diff to dnadmod
!* - Renamed dual number type from DUAL_NUM to dual
!* - Renamed components of dual number type from (xp_ad_, xp_ad_) to (x, dx)
!*
!* 2014-06-05 Wenbin Yu
!* - Forked from original DNAD repository, see https://cdmhub.org/resources/374
!*
!******************************************************************************
! Number of design variables (default = 1)
#ifndef ndv
#define ndv 1
#endif
module dnadmod
implicit none
private
real :: negative_one = -1.0
type,public:: dual ! make this private will create difficulty to use the
! original write/read commands, hence x and dx are
! variables which can be accessed using D%x and D%dx in
! other units using this module in which D is defined
! as type(dual).
sequence
real :: x ! functional value
real :: dx(ndv) ! derivative
end type dual
!******** Interfaces for operator overloading
public assignment (=)
interface assignment (=)
module procedure assign_di ! dual=integer, elemental
module procedure assign_dr ! dual=real, elemental
module procedure assign_id ! integer=dual, elemental
end interface
public operator (+)
interface operator (+)
module procedure add_d ! +dual number, elemental
module procedure add_dd ! dual + dual, elemental
module procedure add_di ! dual + integer, elemental
module procedure add_dr ! dual + real, elemental
module procedure add_id ! integer + dual, elemental
module procedure add_rd ! real + dual, elemental
end interface
public operator (-)
interface operator (-)
module procedure minus_d ! negate a dual number,elemental
module procedure minus_dd ! dual -dual,elemental
module procedure minus_di ! dual-integer,elemental
module procedure minus_dr ! dual-real,elemental
module procedure minus_id ! integer-dual,elemental
module procedure minus_rd ! real-dual,elemental
end interface
public operator (*)
interface operator (*)
module procedure mult_dd ! dual*dual, elemental
module procedure mult_di ! dual*integer,elemental
module procedure mult_dr ! dual*real,elemental
module procedure mult_id ! integer*dual,elemental
module procedure mult_rd ! real*dual,elemental
end interface
public operator (/)
interface operator (/)
module procedure div_dd ! dual/dual,elemental
module procedure div_di ! dual/integer, elemental
module procedure div_dr ! dual/real,emental
module procedure div_id ! integer/dual, elemental
module procedure div_rd ! real/dual, elemental
end interface
public operator (**)
interface operator (**)
module procedure pow_i ! dual number to an integer power,elemental
module procedure pow_r ! dual number to a real power, elemental
module procedure pow_d ! dual number to a dual power, elemental
end interface
public operator (==)
interface operator (==)
module procedure eq_dd ! compare two dual numbers, elemental
module procedure eq_di ! compare a dual and an integer, elemental
module procedure eq_dr ! compare a dual and a real, elemental
module procedure eq_id ! compare integer with a dual number, elemental
module procedure eq_rd ! compare a real with a dual number, elemental
end interface
public operator (<=)
interface operator (<=)
module procedure le_dd ! compare two dual numbers, elemental
module procedure le_di ! compare a dual and an integer, elemental
module procedure le_dr ! compare a dual and a real,elemental
module procedure le_id ! compare integer with a dual number, elemental
module procedure le_rd ! compare a real with a dual number, elemental
end interface
public operator (<)
interface operator (<)
module procedure lt_dd !compare two dual numbers, elemental
module procedure lt_di !compare a dual and an integer, elemental
module procedure lt_dr !compare dual with a real, elemental
module procedure lt_id ! compare integer with a dual number, elemental
module procedure lt_rd ! compare a real with a dual number, elemental
end interface
public operator (>=)
interface operator (>=)
module procedure ge_dd ! compare two dual numbers, elemental
module procedure ge_di ! compare dual with integer, elemental
module procedure ge_dr ! compare dual with a real number, elemental
module procedure ge_id ! compare integer with a dual number, elemental
module procedure ge_rd ! compare a real with a dual number, elemental
end interface
public operator (>)
interface operator (>)
module procedure gt_dd !compare two dual numbers, elemental
module procedure gt_di !compare a dual and an integer, elemental
module procedure gt_dr !compare dual with a real, elemental
module procedure gt_id ! compare integer with a dual number, elemental
module procedure gt_rd ! compare a real with a dual number, elemental
end interface
public operator (/=)
interface operator (/=)
module procedure ne_dd !compare two dual numbers, elemental
module procedure ne_di !compare a dual and an integer, elemental
module procedure ne_dr !compare dual with a real, elemental
module procedure ne_id ! compare integer with a dual number, elemental
module procedure ne_rd ! compare a real with a dual number, elemental
end interface
!------------------------------------------------
! Interfaces for intrinsic functions overloading
!------------------------------------------------
public abs
interface abs
module procedure abs_d ! absolute value of a dual number, elemental
end interface
public dabs
interface dabs
module procedure abs_d ! same as abs, used for some old fortran commands
end interface
public acos
interface acos
module procedure acos_d ! arccosine of a dual number, elemental
end interface
public asin
interface asin
module procedure asin_d ! arcsine of a dual number, elemental
end interface
public atan
interface atan
module procedure atan_d ! arctan of a dual number, elemental
end interface
public atan2
interface atan2
module procedure atan2_d ! arctan of a dual number, elemental
end interface
public cos
interface cos
module procedure cos_d ! cosine of a dual number, elemental
end interface
public dcos
interface dcos
module procedure cos_d ! cosine of a dual number, elemental
end interface
public dot_product
interface dot_product
module procedure dot_product_dd ! dot product two dual number vectors
end interface
public exp
interface exp
module procedure exp_d ! exponential of a dual number, elemental
end interface
public int
interface int
module procedure int_d ! integer part of a dual number, elemental
end interface
public log
interface log
module procedure log_d ! log of a dual number, elemental
end interface
public log10
interface log10
module procedure log10_d ! log of a dual number, elemental
end interface
public matmul
interface matmul
module procedure matmul_dd ! multiply two dual matrices
module procedure matmul_dv ! multiply a dual matrix with a dual vector
module procedure matmul_vd ! multiply a dual vector with a dual matrix
end interface
public max
interface max
module procedure max_dd ! max of from two to four dual numbers, elemental
module procedure max_di ! max of a dual number and an integer, elemental
module procedure max_dr ! max of a dual number and a real, elemental
module procedure max_rd ! max of a real,and a dual number, elemental
end interface
public dmax1
interface dmax1
module procedure max_dd ! max of from two to four dual numbers, elemental
end interface
public maxval
interface maxval
module procedure maxval_d ! maxval of a dual number vector
end interface
public min
interface min
module procedure min_dd ! min of from two to four dual numbers, elemental
module procedure min_dr ! min of a dual and a real, elemental
end interface
public dmin1
interface dmin1
module procedure min_dd ! min of from two to four dual numbers, elemental
end interface
public minval
interface minval
module procedure minval_d ! obtain the maxval of a dual number vectgor
end interface
public nint
interface nint
module procedure nint_d ! nearest integer to the argument, elemental
end interface
public sign
interface sign
module procedure sign_dd ! sign(a,b) with two dual numbers, elemental
module procedure sign_rd ! sign(a,b) with a real and a dual, elemental
end interface
public sin
interface sin
module procedure sin_d ! obtain sine of a dual number, elemental
end interface
public dsin
interface dsin
module procedure sin_d ! obtain sine of a dual number, elemental
end interface
public tan
interface tan
module procedure tan_d ! obtain sine of a dual number, elemental
end interface
public dtan
interface dtan
module procedure tan_d ! obtain sine of a dual number, elemental
end interface
public sqrt
interface sqrt
module procedure sqrt_d ! obtain the sqrt of a dual number, elemental
end interface
public sum
interface sum
module procedure sum_d ! sum a dual array
end interface
public maxloc
interface maxloc
module procedure maxloc_d ! location of max in a dual array
end interface
contains
!*********Begin: functions/subroutines for overloading operators
!******* Begin: (=)
!---------------------
!-----------------------------------------
! dual = integer
! <u, du> = <i, 0>
!-----------------------------------------
elemental subroutine assign_di(u, i)
type(dual), intent(out) :: u
integer, intent(in) :: i
u%x = real(i) ! This is faster than direct assignment
u%dx = 0.0
end subroutine assign_di
!-----------------------------------------
! dual = real(double)
! <u, du> = <r, 0>
!-----------------------------------------
elemental subroutine assign_dr(u, r)
type(dual), intent(out) :: u
real, intent(in) :: r
u%x = r
u%dx = 0.0
end subroutine assign_dr
!-----------------------------------------
! integer = dual
! i = <u, du>
!-----------------------------------------
elemental subroutine assign_id(i, v)
type(dual), intent(in) :: v
integer, intent(out) :: i
i = int(v%x)
end subroutine assign_id
!******* End: (=)
!---------------------
!******* Begin: (+)
!---------------------
!-----------------------------------------
! Unary positive
! <res, dres> = +<u, du>
!-----------------------------------------
elemental function add_d(u) result(res)
type(dual), intent(in) :: u
type(dual) :: res
res = u ! Faster than assigning component wise
end function add_d
!-----------------------------------------
! dual + dual
! <res, dres> = <u, du> + <v, dv> = <u + v, du + dv>
!-----------------------------------------
elemental function add_dd(u, v) result(res)
type(dual), intent(in) :: u, v
type(dual) :: res
res%x = u%x + v%x
res%dx = u%dx + v%dx
end function add_dd
!-----------------------------------------
! dual + integer
! <res, dres> = <u, du> + i = <u + i, du>
!-----------------------------------------
elemental function add_di(u, i) result(res)
type(dual), intent(in) :: u
integer, intent(in) :: i
type(dual) :: res
res%x = real(i) + u%x
res%dx = u%dx
end function add_di
!-----------------------------------------
! dual + double
! <res, dres> = <u, du> + <r, 0> = <u + r, du>
!-----------------------------------------
elemental function add_dr(u, r) result(res)
type(dual), intent(in) :: u
real, intent(in) :: r
type(dual) :: res
res%x = r + u%x
res%dx = u%dx
end function add_dr
!-----------------------------------------
! integer + dual
! <res, dres> = <i, 0> + <v, dv> = <i + v, dv>
!-----------------------------------------
elemental function add_id(i, v) result(res)
integer, intent(in) :: i
type(dual), intent(in) :: v
type(dual) :: res
res%x = real(i) + v%x
res%dx = v%dx
end function add_id
!-----------------------------------------
! double + dual
! <res, dres> = <r, 0> + <v, dv> = <r + v, dv>
!-----------------------------------------
elemental function add_rd(r, v) result(res)
real, intent(in) :: r
type(dual), intent(in) :: v
type(dual) :: res
res%x = r + v%x
res%dx = v%dx
end function add_rd
!******* End: (+)
!---------------------
!******* Begin: (-)
!---------------------
!-------------------------------------------------
! negate a dual
! <res, dres> = -<u, du>
!-------------------------------------------------
elemental function minus_d(u) result(res)
type(dual), intent(in) :: u
type(dual) :: res
res%x = -u%x
res%dx = -u%dx
end function minus_d
!-------------------------------------------------
! dual - dual
! <res, dres> = <u, du> - <v, dv> = <u - v, du - dv>
!-------------------------------------------------
elemental function minus_dd(u, v) result(res)
type(dual), intent(in) :: u, v
type(dual) :: res
res%x = u%x - v%x
res%dx = u%dx - v%dx
end function minus_dd
!-------------------------------------------------
! dual - integer
! <res, dres> = <u, du> - i = <u - i, du>
!-------------------------------------------------
elemental function minus_di(u, i) result(res)
type(dual), intent(in) :: u
integer, intent(in) :: i
type(dual) :: res
res%x = u%x - real(i)
res%dx = u%dx
end function minus_di
!-------------------------------------------------
! dual - double
! <res, dres> = <u, du> - r = <u - r, du>
!-------------------------------------------------
elemental function minus_dr(u, r) result(res)
type (dual), intent(in) :: u
real,intent(in) :: r
type(dual) :: res
res%x = u%x - r
res%dx = u%dx
end function minus_dr
!-------------------------------------------------
! integer - dual
! <res, dres> = i - <v, dv> = <i - v, -dv>
!-------------------------------------------------
elemental function minus_id(i, v) result(res)
integer, intent(in) :: i
type(dual), intent(in) :: v
type(dual) :: res
res%x = real(i) - v%x
res%dx = -v%dx
end function minus_id
!-------------------------------------------------
! double - dual
! <res, dres> = r - <v, dv> = <r - v, -dv>
!-------------------------------------------------
elemental function minus_rd(r, v) result(res)
real, intent(in) :: r
type(dual), intent(in) :: v
type(dual) :: res
res%x = r - v%x
res%dx = -v%dx
end function minus_rd
!******* END: (-)
!---------------------
!******* BEGIN: (*)
!---------------------
!----------------------------------------
! dual * dual
! <res, dres> = <u, du> * <v, dv> = <u * v, u * dv + v * du>
!----------------------------------------
elemental function mult_dd(u, v) result(res)
type(dual), intent(in) :: u, v
type(dual) :: res
res%x = u%x * v%x
res%dx = u%x * v%dx + v%x * u%dx
end function mult_dd
!-----------------------------------------
! dual * integer
! <res, dres> = <u, du> * i = <u * i, du * i>
!-----------------------------------------
elemental function mult_di(u, i) result(res)
type(dual), intent(in) :: u
integer, intent(in) :: i
type(dual) :: res
real :: r
r = real(i)
res%x = r * u%x
res%dx = r * u%dx
end function mult_di
!-----------------------------------------
! dual * double
! <res, dres> = <u, du> * r = <u * r, du * r>
!----------------------------------------
elemental function mult_dr(u, r) result(res)
type(dual), intent(in) :: u
real, intent(in) :: r
type(dual) :: res
res%x = u%x * r
res%dx = u%dx * r
end function mult_dr
!-----------------------------------------
! integer * dual
! <res, dres> = i * <v, dv> = <i * v, i * dv>
!-----------------------------------------
elemental function mult_id(i, v) result(res)
integer, intent(in) :: i
type(dual), intent(in) :: v
type(dual) :: res
real :: r
r = real(i)
res%x = r * v%x
res%dx = r * v%dx
end function mult_id
!-----------------------------------------
! double * dual
! <res, dres> = r * <v, dv> = <r * v, r * dv>
!-----------------------------------------
elemental function mult_rd(r, v) result(res)
real, intent(in) :: r
type(dual), intent(in) :: v
type(dual) :: res
res%x = r * v%x
res%dx = r * v%dx
end function mult_rd
!******* END: (*)
!---------------------
!******* BEGIN: (/)
!---------------------
!-----------------------------------------
! dual / dual
! <res, dres> = <u, du> / <v, dv> = <u / v, du / v - u * dv / v^2>
!-----------------------------------------
elemental function div_dd(u, v) result(res)
type(dual), intent(in) :: u, v
type(dual) :: res
real :: inv
inv = 1.0 / v%x
res%x = u%x * inv
res%dx = (u%dx - res%x * v%dx) * inv
end function div_dd
!-----------------------------------------
! dual / integer
! <res, dres> = <u, du> / i = <u / i, du / i>
!-----------------------------------------
elemental function div_di(u, i) result(res)
type(dual), intent(in) :: u
integer, intent(in) :: i
type(dual) :: res
real :: inv
inv = 1.0 / real(i)
res%x = u%x * inv
res%dx = u%dx * inv
end function div_di
!-----------------------------------------
! dual / double
! <res, dres> = <u, du> / r = <u / r, du / r>
!----------------------------------------
elemental function div_dr(u, r) result(res)
type(dual), intent(in) :: u
real, intent(in) :: r
type(dual):: res
real :: inv
inv = 1.0 / r
res%x = u%x * inv
res%dx = u%dx * inv
end function div_dr
!-----------------------------------------
! integer / dual
! <res, dres> = i / <v, dv> = <i / v, -i / v^2 * du>
!-----------------------------------------
elemental function div_id(i, v) result(res)
integer, intent(in) :: i
type(dual), intent(in) :: v
type(dual) :: res
real :: inv
inv = 1.0 / v%x
res%x = real(i) * inv
res%dx = -res%x * inv * v%dx
end function div_id
!-----------------------------------------
! double / dual
! <res, dres> = r / <u, du> = <r / u, -r / u^2 * du>
!-----------------------------------------
elemental function div_rd(r, v) result(res)
real, intent(in) :: r
type(dual), intent(in) :: v
type(dual) :: res
real :: inv
inv = 1.0 / v%x
res%x = r * inv
res%dx = -res%x * inv * v%dx
end function div_rd
!******* END: (/)
!---------------------
!******* BEGIN: (**)
!---------------------
!-----------------------------------------
! power(dual, integer)
! <res, dres> = <u, du> ^ i = <u ^ i, i * u ^ (i - 1) * du>
!-----------------------------------------
elemental function pow_i(u, i) result(res)
type(dual), intent(in) :: u
integer, intent(in) :: i
type(dual) :: res
real :: pow_x
pow_x = u%x ** (i - 1)
res%x = u%x * pow_x
res%dx = real(i) * pow_x * u%dx
end function pow_i
!-----------------------------------------
! power(dual, double)
! <res, dres> = <u, du> ^ r = <u ^ r, r * u ^ (r - 1) * du>
!-----------------------------------------
elemental function pow_r(u, r) result(res)
type(dual), intent(in) :: u
real, intent(in) :: r
type(dual) :: res
real :: pow_x
pow_x = u%x ** (r - 1.0)
res%x = u%x * pow_x
res%dx = r * pow_x * u%dx
end function pow_r
!-----------------------------------------
! POWER dual numbers to a dual power
! <res, dres> = <u, du> ^ <v, dv>
! = <u ^ v, u ^ v * (v / u * du + Log(u) * dv)>
!-----------------------------------------
elemental function pow_d(u, v) result(res)
type(dual), intent(in)::u, v
type(dual) :: res
res%x = u%x ** v%x
res%dx = res%x * (v%x / u%x * u%dx + log(u%x) * v%dx)
end function pow_d
!******* END: (**)
!---------------------
!******* BEGIN: (==)
!---------------------
!-----------------------------------------
! compare two dual numbers,
! simply compare the functional value.
!-----------------------------------------
elemental function eq_dd(lhs, rhs) result(res)
type(dual), intent(in) :: lhs, rhs
logical :: res
res = (lhs%x == rhs%x)
end function eq_dd
!-----------------------------------------
! compare a dual with an integer,
! simply compare the functional value.
!-----------------------------------------
elemental function eq_di(lhs, rhs) result(res)
type(dual), intent(in) :: lhs
integer, intent(in) :: rhs
logical :: res
res = (lhs%x == real(rhs))
end function eq_di
!-----------------------------------------
! compare a dual number with a real number,
! simply compare the functional value.
!-----------------------------------------
elemental function eq_dr(lhs, rhs) result(res)
type(dual), intent(in) :: lhs
real, intent(in) :: rhs
logical::res
res = (lhs%x == rhs)
end function eq_dr
!-----------------------------------------
! compare an integer with a dual,
! simply compare the functional value.
!----------------------------------------
elemental function eq_id(lhs, rhs) result(res)
integer, intent(in) :: lhs
type(dual), intent(in) :: rhs
logical :: res
res = (lhs == rhs%x)
end function eq_id
!-----------------------------------------
! compare a real with a dual,
! simply compare the functional value.
!-----------------------------------------
elemental function eq_rd(lhs, rhs) result(res)
real, intent(in) :: lhs
type(dual), intent(in) :: rhs
logical :: res
res = (lhs == rhs%x)
end function eq_rd
!******* END: (==)
!---------------------
!******* BEGIN: (<=)
!---------------------
!-----------------------------------------
! compare two dual numbers, simply compare
! the functional value.
!-----------------------------------------
elemental function le_dd(lhs, rhs) result(res)
type(dual), intent(in) :: lhs, rhs
logical :: res
res = (lhs%x <= rhs%x)
end function le_dd
!-----------------------------------------
! compare a dual with an integer,
! simply compare the functional value.
!-----------------------------------------
elemental function le_di(lhs, rhs) result(res)
type(dual), intent(in) :: lhs
integer, intent(in) :: rhs
logical :: res
res = (lhs%x <= rhs)
end function le_di
!-----------------------------------------
! compare a dual number with a real number,
! simply compare the functional value.
!-----------------------------------------
elemental function le_dr(lhs, rhs) result(res)
type(dual), intent(in) :: lhs
real, intent(in) :: rhs
logical :: res
res = (lhs%x <= rhs)
end function le_dr
!-----------------------------------------
! compare a dual number with an integer,
! simply compare the functional value.
!-----------------------------------------
elemental function le_id(i, rhs) result(res)
integer, intent(in) :: i
type(dual), intent(in) :: rhs
logical :: res
res = (i <= rhs%x)
end function le_id
!-----------------------------------------
! compare a real with a dual,
! simply compare the functional value.
!-----------------------------------------
elemental function le_rd(lhs, rhs) result(res)
real, intent(in) :: lhs
type(dual), intent(in) :: rhs
logical :: res
res = (lhs <= rhs%x)
end function le_rd