-
Notifications
You must be signed in to change notification settings - Fork 0
/
91-math.f90
661 lines (521 loc) · 17.1 KB
/
91-math.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
module mo_math
implicit none
interface swap
module procedure :: swap_real8, swap_integer
end interface
interface atan2
module procedure :: atan2_complex
end interface
interface qsort
module procedure :: qsort_real8, qsort_integer
end interface
contains
subroutine init_rand(seed)
implicit none
! para list
integer :: seed
! local
integer :: n, i
integer(8) :: tmp
integer, allocatable, dimension(:) :: seed_array
call random_seed(size=n)
allocate( seed_array(n) )
tmp = int(seed,8) * 1000_8
do i=1, n
seed_array(i) = lcg(tmp)
end do
call random_seed(put=seed_array)
contains
function lcg(s)
implicit none
integer :: lcg
integer(8) :: s
if (s == 0) then
s = 104729
else
s = mod(s, 4294967296_8)
end if
s = mod(s * 279470273_8, 4294967291_8)
lcg = int(mod(s, int(huge(0), 8)), kind(0))
end function lcg
end subroutine
function randperm(n) result(redata)
implicit none
! para list
integer, intent(in) :: n
! result
integer, dimension(n) :: redata
! local
integer :: i, itemp
real(8) :: rtemp
redata(1) = 1
do i=2, n
call random_number(rtemp)
itemp = floor(rtemp*i) + 1
if ( i /= itemp ) then
redata(i) = redata(itemp)
redata(itemp) = i
else
redata(i) = i
end if
end do
end function
function randuvec(n) result(uvec)
implicit none
! para list
integer, intent(in) :: n
! result
real(8) :: uvec(n)
call random_number(uvec)
uvec = 2 * uvec - 1.d0
do while ( norm2(uvec) > 1.d0 )
call random_number(uvec)
uvec = 2 * uvec - 1.d0
end do
uvec = uvec / norm2(uvec)
end function
! swap >
subroutine swap_real8(x, y)
implicit none
! para list
real(8), intent(inout) :: x, y
! local
real(8) :: tmp
tmp = x
x = y
y = tmp
end subroutine
subroutine swap_integer(x, y)
implicit none
! para list
integer, intent(inout) :: x, y
! local
integer :: tmp
tmp = x
x = y
y = tmp
end subroutine
! swap <
pure function mean(a) result(re)
implicit none
! para list
real(8), intent(in), dimension(:) :: a
! result
real(8) :: re
re = sum(a) / size(a)
end function
pure function std(a) result(re)
implicit none
! para list
real(8), intent(in), dimension(:) :: a
! result
real(8) :: re
integer :: ilen
real(8) :: mean_of_a
ilen = size(a)
mean_of_a = sum(a) / ilen
re = sum( (a-mean_of_a)**2 ) / ilen
re = sqrt(re)
end function
pure function corr(a, b) result(re)
implicit none
! para list
real(8), intent(in), dimension(:) :: a, b
! result
real(8) :: re
! local
integer :: ilen
real(8) :: sigma_of_a, sigma_of_b
ilen = size(a)
sigma_of_a = std(a)
sigma_of_b = std(b)
re = sum(a*b)/ilen - mean(a)*mean(b)
re = re / std(a) / std(b)
end function
pure function dot(a,b) result(re)
implicit none
! para list
real(8), intent(in), dimension(:) :: a, b
! result
real(8) :: re
re = sum(a*b)
end function
pure function times2(a,b) result(re)
implicit none
! para list
real(8), intent(in) :: a(2), b(2)
! result
real(8) :: re
re = a(1) * b(2) - a(2) * b(1)
end function
pure function times3(a,b) result(re)
implicit none
! para list
real(8), intent(in) :: a(3), b(3)
! result
real(8) :: re(3)
re(1) = a(2) * b(3) - a(3) * b(2)
re(2) = a(3) * b(1) - a(1) * b(3)
re(3) = a(1) * b(2) - a(2) * b(1)
end function
pure function unitv(vector) result(uvector)
implicit none
! para list
real(8), intent(in), dimension(:) :: vector
! result
real(8), allocatable, dimension(:) :: uvector
uvector = vector / norm2(vector)
end function
function sortperm(n, data) result(index)
implicit none
!===================================================================
!
! sortrx -- sort, real input, index output
!
!
! input: n integer
! data real
!
! output: index integer (dimension n)
!
! this routine performs an in-memory sort of the first n elements of
! array data, returning into array index the indices of elements of
! data arranged in ascending order. thus,
!
! data(index(1)) will be the smallest number in array data;
! data(index(n)) will be the largest number in data.
!
! the original data is not physically rearranged. the original order
! of equal input values is not necessarily preserved.
!
!===================================================================
!
! sortrx uses a hybrid quicksort algorithm, based on several
! suggestions in knuth, volume 3, section 5.2.2. in particular, the
! "pivot key" [my term] for dividing each subsequence is chosen to be
! the median of the first, last, and middle values of the subsequence;
! and the quicksort is cut off when a subsequence has 9 or fewer
! elements, and a straight insertion sort of the entire array is done
! at the end. the result is comparable to a pure insertion sort for
! very short arrays, and very fast for very large arrays (of order 12
! micro-sec/element on the 3081k for arrays of 10k elements). it is
! also not subject to the poor performance of the pure quicksort on
! partially ordered data.
!
! created: 15 jul 1986 len moss
!
!===================================================================
integer, intent(in) :: n
real(8), intent(in) :: data(n)
integer :: index(n)
integer,dimension(31) :: lstk,rstk
integer :: istk
integer :: l,r,i,j,p,indexp,indext
real(8) :: datap
! quicksort cutoff
!
! quit quicksort-ing when a subsequence contains m or fewer
! elements and finish off at end with straight insertion sort.
! according to knuth, v.3, the optimum value of m is around 9.
integer, parameter :: m = 9
!===================================================================
!
! make initial guess for index
do i=1,n
index(i)=i
end do
! if array is short, skip quicksort and go directly to
! the straight insertion sort.
if (n .le. m) goto 900
!===================================================================
!
! quicksort
!
! the "qn:"s correspond roughly to steps in algorithm q,
! knuth, v.3, pp.116-117, modified to select the median
! of the first, last, and middle elements as the "pivot
! key" (in knuth's notation, "k"). also modified to leave
! data in place and produce an index array. to simplify
! comments, let data[i]=data(index(i)).
! q1: initialize
istk=0
l=1
r=n
200 continue
! q2: sort the subsequence data[l]..data[r].
!
! at this point, data[l] <= data[m] <= data[r] for all l < l,
! r > r, and l <= m <= r. (first time through, there is no
! data for l < l or r > r.)
i=l
j=r
! q2.5: select pivot key
!
! let the pivot, p, be the midpoint of this subsequence,
! p=(l+r)/2; then rearrange index(l), index(p), and index(r)
! so the corresponding data values are in increasing order.
! the pivot key, datap, is then data[p].
p=(l+r)/2
indexp=index(p)
datap=data(indexp)
if (data(index(l)) .gt. datap) then
index(p)=index(l)
index(l)=indexp
indexp=index(p)
datap=data(indexp)
end if
if (datap .gt. data(index(r))) then
if (data(index(l)) .gt. data(index(r))) then
index(p)=index(l)
index(l)=index(r)
else
index(p)=index(r)
end if
index(r)=indexp
indexp=index(p)
datap=data(indexp)
end if
! now we swap values between the right and left sides and/or
! move datap until all smaller values are on the left and all
! larger values are on the right. neither the left or right
! side will be internally ordered yet; however, datap will be
! in its final position.
300 continue
! q3: search for datum on left >= datap
!
! at this point, data[l] <= datap. we can therefore start scanning
! up from l, looking for a value >= datap (this scan is guaranteed
! to terminate since we initially placed datap near the middle of
! the subsequence).
i=i+1
if (data(index(i)).lt.datap) goto 300
400 continue
! q4: search for datum on right <= datap
!
! at this point, data[r] >= datap. we can therefore start scanning
! down from r, looking for a value <= datap (this scan is guaranteed
! to terminate since we initially placed datap near the middle of
! the subsequence).
j=j-1
if (data(index(j)).gt.datap) goto 400
! q5: have the two scans collided?
if (i.lt.j) then
! q6: no, interchange data[i] <--> data[j] and continue
indext=index(i)
index(i)=index(j)
index(j)=indext
goto 300
else
! q7: yes, select next subsequence to sort
!
! at this point, i >= j and data[l] <= data[i] == datap <= data[r],
! for all l <= l < i and j < r <= r. if both subsequences are
! more than m elements long, push the longer one on the stack and
! go back to quicksort the shorter; if only one is more than m
! elements long, go back and quicksort it; otherwise, pop a
! subsequence off the stack and quicksort it.
if (r-j .ge. i-l .and. i-l .gt. m) then
istk=istk+1
lstk(istk)=j+1
rstk(istk)=r
r=i-1
else if (i-l .gt. r-j .and. r-j .gt. m) then
istk=istk+1
lstk(istk)=l
rstk(istk)=i-1
l=j+1
else if (r-j .gt. m) then
l=j+1
else if (i-l .gt. m) then
r=i-1
else
! q8: pop the stack, or terminate quicksort if empty
if (istk.lt.1) goto 900
l=lstk(istk)
r=rstk(istk)
istk=istk-1
end if
goto 200
end if
900 continue
!===================================================================
!
! q9: straight insertion sort
do 950 i=2,n
if (data(index(i-1)) .gt. data(index(i))) then
indexp=index(i)
datap=data(indexp)
p=i-1
920 continue
index(p+1) = index(p)
p=p-1
if (p.gt.0) then
if (data(index(p)).gt.datap) goto 920
end if
index(p+1) = indexp
end if
950 continue
!===================================================================
!
! all done
end function
! qsort >
! for real
recursive subroutine qsort_real8(data)
implicit none
! para list
real(8), intent(inout) :: data(:)
! local
integer :: iq
if ( size(data) > 1 ) then
call partition_data(data, iq)
call qsort(data(:iq-1))
call qsort(data(iq:))
end if
contains
subroutine partition_data(pdata, marker)
implicit none
! para list
real(8), intent(inout) :: pdata(:)
integer, intent(out) :: marker
! local
integer :: i, j
real(8) :: pivot
i=0
j=size(pdata)+1
pivot = pdata((i+j)/2)
do while (.true.)
i = i + 1
j = j - 1
do while ( pdata(i) < pivot )
i = i + 1
end do
do while ( pdata(j) > pivot )
j = j - 1
end do
if ( i < j ) then
call swap(pdata(i),pdata(j))
else if ( i == j ) then
marker = i + 1; exit
else ! i>j
marker = i; exit
end if
end do
end subroutine
end subroutine
! for integer
recursive subroutine qsort_integer(data)
implicit none
! para list
integer, intent(inout) :: data(:)
! local
integer :: iq
if ( size(data) > 1 ) then
call partition_data(data, iq)
call qsort(data(:iq-1))
call qsort(data(iq:))
end if
contains
subroutine partition_data(pdata, marker)
implicit none
! para list
integer, intent(inout) :: pdata(:)
integer, intent(out) :: marker
! local
integer :: i, j
integer :: pivot
i=0
j=size(pdata)+1
pivot = pdata((i+j)/2)
do while (.true.)
i = i + 1
j = j - 1
do while ( pdata(i) < pivot )
i = i + 1
end do
do while ( pdata(j) > pivot )
j = j - 1
end do
if ( i < j ) then
call swap(pdata(i),pdata(j))
else if ( i == j ) then
marker = i + 1; exit
else ! i>j
marker = i; exit
end if
end do
end subroutine
end subroutine
! swap <
pure function atan2_complex(x) result(re)
implicit none
! para list
complex(16), intent(in) :: x
! result
real(8) :: re
re = atan2(aimag(x),real(x))
end function
pure function vec_angle( vec1, vec2 ) result(angle)
! calc angle of vec1 and vec2
implicit none
! para list
real(8), dimension(2), intent(in) :: vec1, vec2
! result
real(8) :: angle
! local
complex(16) :: v1, v2
v1 = cmplx(vec1(1),vec1(2))
v2 = cmplx(vec2(1),vec2(2))
angle = atan2( v2/v1 )
!angle = atan2(aimag(v3),real(v3))
end function
pure function dvec_angle( vec1, vec2 ) result(angle)
! calc angle of vec1 and (vec2-vec1)
implicit none
! para list
real(8), dimension(2), intent(in) :: vec1, vec2
! result
real(8) :: angle
! local
real(8), dimension(2) :: vec3
complex(16) :: v1, v2
vec3 = vec2 - vec1
v1 = cmplx(vec1(1),vec1(2))
v2 = cmplx(vec3(1),vec3(2))
angle = atan2( v2/v1 )
!v3 = v2 / v1
!angle = atan2(aimag(v3),real(v3))
end function
pure function findfirst(data,x) result(iloc)
! find first value in data equal to x
! return index
implicit none
! para list
integer, intent(in) :: data(:)
integer, intent(in) :: x
! result
integer :: iloc
! local
integer :: n, istart, imed, iend, i
n = size(data)
iloc = 0
istart = 1
iend = n
imed = (istart+n)/2
do while ( iend - istart > 10 )
if ( x <= data(imed) ) then
iend = imed
imed = (istart+iend)/2
else
istart = imed
imed = (istart+iend)/2
end if
end do
do i=istart, iend
if ( data(i)==x ) then
iloc = i
exit
end if
end do
end function
end module