Blame view

fvnlib.f90 98.2 KB
d32a47033   daniau   git-svn-id: https...
1
2
3
4
5
6
7
8
  
  module fvn
  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  !
  ! fvn : a f95 module replacement for some imsl routines
  ! it uses lapack for linear algebra
  ! it uses modified quadpack for integration
  !
42591138e   daniau   git-svn-id: https...
9
  ! William Daniau 2007->today
d32a47033   daniau   git-svn-id: https...
10
11
12
13
14
15
16
17
18
19
20
21
  ! william.daniau@femto-st.fr
  !
  ! Routines naming scheme :
  !
  !           fvn_x_name
  !           where x can be  s : real 
  !                           d : real double precision
  !                           c : complex
  !                           z : double complex
  !
  !
  ! This piece of code is totally free! Do whatever you want with it. However
422234dc3   daniau   git-svn-id: https...
22
  ! if you find it usefull it would be kind to give credits ;-) 
d32a47033   daniau   git-svn-id: https...
23
  !
f61865f46   daniau   git-svn-id: https...
24
  ! svn version
38581db0c   daniau   git-svn-id: https...
25
26
27
  ! February 2008 : added fnlib to repository so there's no use to have
  !                 special functions and trigonometry here. Some functions
  !                 are then removed
42591138e   daniau   git-svn-id: https...
28
29
30
  ! January 2008 : added quadratic interpolation, gamma/factorial function,
  !                 a function which return identity matrix,
  !                 evaluation of nterm chebyshev series
422234dc3   daniau   git-svn-id: https...
31
  ! September 2007 : added sparse system solving by interfacing umfpack
f61865f46   daniau   git-svn-id: https...
32
  ! June 2007 : added some complex trigonometric functions
d32a47033   daniau   git-svn-id: https...
33
34
35
36
37
38
39
40
41
42
43
44
45
  !
  ! TO DO LIST :
  ! + Order eigenvalues and vectors in decreasing eigenvalue's modulus order -> atm 
  ! eigenvalues are given with no particular order.
  ! + Generic interface for fvn_x_name family  -> fvn_name
  ! + Make some parameters optional, status for example
  ! + use f95 kinds "double complex" -> complex(kind=8)
  ! + unify quadpack routines
  ! + ...
  !
  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  
  implicit none
f61865f46   daniau   git-svn-id: https...
46
47
48
  ! We define pi and i for the module
  real(kind=8),parameter :: fvn_pi = 3.141592653589793_8
  complex(kind=8),parameter :: fvn_i = (0._8,1._8)
d32a47033   daniau   git-svn-id: https...
49
50
51
52
53
  ! All quadpack routines are private to the module
  private ::  d1mach,dqag,dqag_2d_inner,dqag_2d_outer,dqage,dqage_2d_inner, &
              dqage_2d_outer,dqk15,dqk15_2d_inner,dqk15_2d_outer,dqk21,dqk21_2d_inner,dqk21_2d_outer, &
              dqk31,dqk31_2d_inner,dqk31_2d_outer,dqk41,dqk41_2d_inner,dqk41_2d_outer, &
              dqk51,dqk51_2d_inner,dqk51_2d_outer,dqk61,dqk61_2d_inner,dqk61_2d_outer,dqpsrt
38581db0c   daniau   git-svn-id: https...
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
  
  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  ! Generic interface Definition
  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  
  ! Identity Matrix
  interface fvn_ident
        module procedure fvn_s_ident,fvn_d_ident,fvn_c_ident,fvn_z_ident
  end interface fvn_ident
  
  ! Matrix inversion
  interface fvn_matinv
        module procedure fvn_s_matinv,fvn_d_matinv,fvn_c_matinv,fvn_z_matinv
  end interface fvn_matinv
  
  ! Determinant
  interface fvn_det
      module procedure fvn_s_det,fvn_d_det,fvn_c_det,fvn_z_det
  end interface fvn_det
  
  ! Condition
  interface fvn_matcon
      module procedure fvn_s_matcon,fvn_d_matcon,fvn_c_matcon,fvn_z_matcon
  end interface fvn_matcon
  
  ! Eigen
  interface fvn_matev
      module procedure fvn_s_matev,fvn_d_matev,fvn_c_matev,fvn_z_matev
  end interface fvn_matev
  
  ! Utility procedure find interval
  interface fvn_find_interval
      module procedure fvn_s_find_interval,fvn_d_find_interval
  end interface fvn_find_interval
  
  ! Quadratic 1D interpolation
  interface fvn_quad_interpol
      module procedure fvn_s_quad_interpol,fvn_d_quad_interpol
  end interface fvn_quad_interpol
  
  ! Quadratic 2D interpolation
  interface fvn_quad_2d_interpol
      module procedure fvn_s_quad_2d_interpol,fvn_d_quad_2d_interpol
  end interface fvn_quad_2d_interpol
  
  ! Quadratic 3D interpolation
  interface fvn_quad_3d_interpol
      module procedure fvn_s_quad_3d_interpol,fvn_d_quad_3d_interpol
  end interface fvn_quad_3d_interpol
  
  ! Akima interpolation
  interface fvn_akima
      module procedure fvn_s_akima,fvn_d_akima
  end interface fvn_akima
  
  ! Akima evaluation
  interface fvn_spline_eval
      module procedure fvn_s_spline_eval,fvn_d_spline_eval
  end interface fvn_spline_eval
  
  ! Least square polynomial
  interface fvn_lspoly
      module procedure fvn_s_lspoly,fvn_d_lspoly
  end interface fvn_lspoly
  
  ! Muller
  interface fvn_muller
      module procedure fvn_z_muller
  end interface fvn_muller
  
  ! Gauss legendre
  interface fvn_gauss_legendre
      module procedure fvn_d_gauss_legendre
  end interface fvn_gauss_legendre
  
  ! Simple Gauss Legendre integration
  interface fvn_gl_integ
      module procedure fvn_d_gl_integ
  end interface fvn_gl_integ
  
  ! Adaptative Gauss Kronrod integration f(x)
  interface fvn_integ_1_gk
      module procedure fvn_d_integ_1_gk
  end interface fvn_integ_1_gk
  
  ! Adaptative Gauss Kronrod integration f(x,y)
  interface fvn_integ_2_gk
      module procedure fvn_d_integ_2_gk
  end interface fvn_integ_2_gk
  
  ! Sparse solving
  interface fvn_sparse_solve
      module procedure fvn_zl_sparse_solve,fvn_zi_sparse_solve,fvn_dl_sparse_solve,fvn_di_sparse_solve
  end interface fvn_sparse_solve
d32a47033   daniau   git-svn-id: https...
150
  contains 
42591138e   daniau   git-svn-id: https...
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
  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  !
  ! Identity Matrix
  !
  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  function fvn_d_ident(n)
        implicit none
        integer(kind=4) :: n
        real(kind=8), dimension(n,n) :: fvn_d_ident
  
        real(kind=8),dimension(n*n) :: vect
        integer(kind=4) :: i
  
        vect=0._8
        vect(1:n*n:n+1) = 1._8
        fvn_d_ident=reshape(vect, shape = (/ n,n /))
  end function
  
  function fvn_s_ident(n)
        implicit none
        integer(kind=4) :: n
        real(kind=4), dimension(n,n) :: fvn_s_ident
  
        real(kind=4),dimension(n*n) :: vect
        integer(kind=4) :: i
  
       vect=0._4
        vect(1:n*n:n+1) = 1._4
        fvn_s_ident=reshape(vect, shape = (/ n,n /))
  end function
  
  function fvn_c_ident(n)
        implicit none
        integer(kind=4) :: n
        complex(kind=4), dimension(n,n) :: fvn_c_ident
  
        complex(kind=4),dimension(n*n) :: vect
        integer(kind=4) :: i
  
        vect=(0._4,0._4)
        vect(1:n*n:n+1) = (1._4,0._4)
        fvn_c_ident=reshape(vect, shape = (/ n,n /))
  end function
  
  function fvn_z_ident(n)
        implicit none
        integer(kind=4) :: n
        complex(kind=8), dimension(n,n) :: fvn_z_ident
  
        complex(kind=8),dimension(n*n) :: vect
        integer(kind=4) :: i
  
        vect=(0._8,0._8)
        vect(1:n*n:n+1) = (1._8,0._8)
        fvn_z_ident=reshape(vect, shape = (/ n,n /))
  end function
d32a47033   daniau   git-svn-id: https...
207
208
209
210
211
  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  !
  ! Matrix inversion subroutines
  !
  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
38581db0c   daniau   git-svn-id: https...
212

d32a47033   daniau   git-svn-id: https...
213
214
215
216
217
218
219
220
221
  subroutine fvn_s_matinv(d,a,inva,status)
      !
      ! Matrix inversion of a real matrix using BLAS and LAPACK
      !
      ! d (in) : matrix rank
      ! a (in) : input matrix
      ! inva (out) : inversed matrix
      ! status (ou) : =0 if something failed
      !
8a085e035   daniau   git-svn-id: https...
222
      implicit none
d32a47033   daniau   git-svn-id: https...
223
224
225
      integer, intent(in) :: d
      real, intent(in) :: a(d,d)
      real, intent(out) :: inva(d,d)
38581db0c   daniau   git-svn-id: https...
226
      integer, intent(out),optional :: status
d32a47033   daniau   git-svn-id: https...
227
228
229
230
231
232
  
      integer, allocatable :: ipiv(:)
      real, allocatable :: work(:)
      real twork(1)
      integer :: info
      integer :: lwork
38581db0c   daniau   git-svn-id: https...
233
      if (present(status)) status=1
d32a47033   daniau   git-svn-id: https...
234
235
236
237
238
239
240
241
242
  
      allocate(ipiv(d))
      ! copy a into inva using BLAS
      !call scopy(d*d,a,1,inva,1)
      inva(:,:)=a(:,:)
      ! LU factorization using LAPACK
      call sgetrf(d,d,inva,d,ipiv,info)
      ! if info is not equal to 0, something went wrong we exit setting status to 0
      if (info /= 0) then
38581db0c   daniau   git-svn-id: https...
243
          if (present(status)) status=0
d32a47033   daniau   git-svn-id: https...
244
245
246
247
248
249
250
251
252
253
254
          deallocate(ipiv)
          return
      end if
      ! we use the query fonction of xxxtri to obtain the optimal workspace size
      call sgetri(d,inva,d,ipiv,twork,-1,info)
      lwork=int(twork(1))
      allocate(work(lwork))
      ! Matrix inversion using LAPACK
      call sgetri(d,inva,d,ipiv,work,lwork,info)
      ! again if info is not equal to 0, we exit setting status to 0
      if (info /= 0) then
38581db0c   daniau   git-svn-id: https...
255
          if (present(status)) status=0
d32a47033   daniau   git-svn-id: https...
256
257
258
259
260
261
262
263
264
265
266
267
268
269
      end if
      deallocate(work)
      deallocate(ipiv)
  end subroutine
  
  subroutine fvn_d_matinv(d,a,inva,status)
      !
      ! Matrix inversion of a double precision matrix using BLAS and LAPACK
      !
      ! d (in) : matrix rank
      ! a (in) : input matrix
      ! inva (out) : inversed matrix
      ! status (ou) : =0 if something failed
      !
8a085e035   daniau   git-svn-id: https...
270
      implicit none
38581db0c   daniau   git-svn-id: https...
271
      integer, intent(in), optional :: d
d32a47033   daniau   git-svn-id: https...
272
273
      double precision, intent(in) :: a(d,d)
      double precision, intent(out) :: inva(d,d)
38581db0c   daniau   git-svn-id: https...
274
      integer, intent(out),optional :: status
d32a47033   daniau   git-svn-id: https...
275
276
277
278
279
280
  
      integer, allocatable :: ipiv(:)
      double precision, allocatable :: work(:)
      double precision :: twork(1)
      integer :: info
      integer :: lwork
38581db0c   daniau   git-svn-id: https...
281
      if (present(status)) status=1
d32a47033   daniau   git-svn-id: https...
282
283
284
285
286
287
288
289
290
  
      allocate(ipiv(d))
      ! copy a into inva using BLAS
      !call dcopy(d*d,a,1,inva,1)
      inva(:,:)=a(:,:)
      ! LU factorization using LAPACK
      call dgetrf(d,d,inva,d,ipiv,info)
      ! if info is not equal to 0, something went wrong we exit setting status to 0
      if (info /= 0) then
38581db0c   daniau   git-svn-id: https...
291
          if (present(status)) status=0
d32a47033   daniau   git-svn-id: https...
292
293
294
295
296
297
298
299
300
301
302
          deallocate(ipiv)
          return
      end if
      ! we use the query fonction of xxxtri to obtain the optimal workspace size
      call dgetri(d,inva,d,ipiv,twork,-1,info)
      lwork=int(twork(1))
      allocate(work(lwork))
      ! Matrix inversion using LAPACK
      call dgetri(d,inva,d,ipiv,work,lwork,info)
      ! again if info is not equal to 0, we exit setting status to 0
      if (info /= 0) then
38581db0c   daniau   git-svn-id: https...
303
          if (present(status)) status=0
d32a47033   daniau   git-svn-id: https...
304
305
306
307
308
309
310
311
312
313
314
315
316
317
      end if
      deallocate(work)
      deallocate(ipiv)
  end subroutine
  
  subroutine fvn_c_matinv(d,a,inva,status)
      !
      ! Matrix inversion of a complex matrix using BLAS and LAPACK
      !
      ! d (in) : matrix rank
      ! a (in) : input matrix
      ! inva (out) : inversed matrix
      ! status (ou) : =0 if something failed
      !
8a085e035   daniau   git-svn-id: https...
318
      implicit none
d32a47033   daniau   git-svn-id: https...
319
320
321
      integer, intent(in) :: d
      complex, intent(in) :: a(d,d)
      complex, intent(out) :: inva(d,d)
38581db0c   daniau   git-svn-id: https...
322
      integer, intent(out),optional :: status
d32a47033   daniau   git-svn-id: https...
323
324
325
326
327
328
  
      integer, allocatable :: ipiv(:)
      complex, allocatable :: work(:)
      complex :: twork(1)
      integer :: info
      integer :: lwork
38581db0c   daniau   git-svn-id: https...
329
       if (present(status)) status=1
d32a47033   daniau   git-svn-id: https...
330
331
332
333
334
335
336
337
338
339
  
      allocate(ipiv(d))
      ! copy a into inva using BLAS
      !call ccopy(d*d,a,1,inva,1)
      inva(:,:)=a(:,:)
      
      ! LU factorization using LAPACK
      call cgetrf(d,d,inva,d,ipiv,info)
      ! if info is not equal to 0, something went wrong we exit setting status to 0
      if (info /= 0) then
38581db0c   daniau   git-svn-id: https...
340
          if (present(status)) status=0
d32a47033   daniau   git-svn-id: https...
341
342
343
344
345
346
347
348
349
350
351
          deallocate(ipiv)
          return
      end if
      ! we use the query fonction of xxxtri to obtain the optimal workspace size
      call cgetri(d,inva,d,ipiv,twork,-1,info)
      lwork=int(twork(1))
      allocate(work(lwork))
      ! Matrix inversion using LAPACK
      call cgetri(d,inva,d,ipiv,work,lwork,info)
      ! again if info is not equal to 0, we exit setting status to 0
      if (info /= 0) then
38581db0c   daniau   git-svn-id: https...
352
          if (present(status)) status=0
d32a47033   daniau   git-svn-id: https...
353
354
355
356
357
358
359
360
361
362
363
364
365
366
      end if
      deallocate(work)
      deallocate(ipiv)
  end subroutine
  
  subroutine fvn_z_matinv(d,a,inva,status)
      !
      ! Matrix inversion of a double complex matrix using BLAS and LAPACK
      !
      ! d (in) : matrix rank
      ! a (in) : input matrix
      ! inva (out) : inversed matrix
      ! status (ou) : =0 if something failed
      !
8a085e035   daniau   git-svn-id: https...
367
      implicit none
d32a47033   daniau   git-svn-id: https...
368
369
370
      integer, intent(in) :: d
      double complex, intent(in) :: a(d,d)
      double complex, intent(out) :: inva(d,d)
38581db0c   daniau   git-svn-id: https...
371
      integer, intent(out),optional :: status
d32a47033   daniau   git-svn-id: https...
372
373
374
375
376
377
  
      integer, allocatable :: ipiv(:)
      double complex, allocatable :: work(:)
      double complex :: twork(1)
      integer :: info
      integer :: lwork
38581db0c   daniau   git-svn-id: https...
378
       if (present(status)) status=1
d32a47033   daniau   git-svn-id: https...
379
380
381
382
383
384
385
386
387
388
  
      allocate(ipiv(d))
      ! copy a into inva using BLAS
      !call zcopy(d*d,a,1,inva,1)
      inva(:,:)=a(:,:)
      
      ! LU factorization using LAPACK
      call zgetrf(d,d,inva,d,ipiv,info)
      ! if info is not equal to 0, something went wrong we exit setting status to 0
      if (info /= 0) then
38581db0c   daniau   git-svn-id: https...
389
          if (present(status)) status=0
d32a47033   daniau   git-svn-id: https...
390
391
392
393
394
395
396
397
398
399
400
          deallocate(ipiv)
          return
      end if
      ! we use the query fonction of xxxtri to obtain the optimal workspace size
      call zgetri(d,inva,d,ipiv,twork,-1,info)
      lwork=int(twork(1))
      allocate(work(lwork))
      ! Matrix inversion using LAPACK
      call zgetri(d,inva,d,ipiv,work,lwork,info)
      ! again if info is not equal to 0, we exit setting status to 0
      if (info /= 0) then
38581db0c   daniau   git-svn-id: https...
401
          if (present(status)) status=0
d32a47033   daniau   git-svn-id: https...
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
      end if
      deallocate(work)
      deallocate(ipiv)
  end subroutine
  
  
  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  !
  ! Determinants
  !
  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  function fvn_s_det(d,a,status)
      !
      ! Evaluate the determinant of a square matrix using lapack LU factorization
      !
      ! d (in) : matrix rank
      ! a (in) : The Matrix
      ! status (out) : =0 if LU factorization failed
      !
8a085e035   daniau   git-svn-id: https...
421
      implicit none
d32a47033   daniau   git-svn-id: https...
422
423
      integer, intent(in) :: d
      real, intent(in) :: a(d,d)
38581db0c   daniau   git-svn-id: https...
424
      integer, intent(out), optional :: status
d32a47033   daniau   git-svn-id: https...
425
426
427
428
429
430
      real :: fvn_s_det
      
      real, allocatable :: wc_a(:,:)
      integer, allocatable :: ipiv(:)
      integer :: info,i
      
38581db0c   daniau   git-svn-id: https...
431
       if (present(status)) status=1
d32a47033   daniau   git-svn-id: https...
432
433
434
435
436
      allocate(wc_a(d,d))
      allocate(ipiv(d))
      wc_a(:,:)=a(:,:)
      call sgetrf(d,d,wc_a,d,ipiv,info)
      if (info/= 0) then
38581db0c   daniau   git-svn-id: https...
437
           if (present(status)) status=0
d32a47033   daniau   git-svn-id: https...
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
          fvn_s_det=0.e0
          deallocate(ipiv)
          deallocate(wc_a)
          return
      end if
      fvn_s_det=1.e0
      do i=1,d
          if (ipiv(i)==i) then
              fvn_s_det=fvn_s_det*wc_a(i,i)
          else
              fvn_s_det=-fvn_s_det*wc_a(i,i)
          end if
      end do
      deallocate(ipiv)
      deallocate(wc_a)
  
  end function
  
  function fvn_d_det(d,a,status)
      !
      ! Evaluate the determinant of a square matrix using lapack LU factorization
      !
      ! d (in) : matrix rank
      ! a (in) : The Matrix
      ! status (out) : =0 if LU factorization failed
      !
8a085e035   daniau   git-svn-id: https...
464
      implicit none
d32a47033   daniau   git-svn-id: https...
465
466
      integer, intent(in) :: d
      double precision, intent(in) :: a(d,d)
38581db0c   daniau   git-svn-id: https...
467
      integer, intent(out), optional :: status
d32a47033   daniau   git-svn-id: https...
468
469
470
471
472
473
      double precision :: fvn_d_det
      
      double precision, allocatable :: wc_a(:,:)
      integer, allocatable :: ipiv(:)
      integer :: info,i
      
38581db0c   daniau   git-svn-id: https...
474
       if (present(status)) status=1
d32a47033   daniau   git-svn-id: https...
475
476
477
478
479
      allocate(wc_a(d,d))
      allocate(ipiv(d))
      wc_a(:,:)=a(:,:)
      call dgetrf(d,d,wc_a,d,ipiv,info)
      if (info/= 0) then
38581db0c   daniau   git-svn-id: https...
480
           if (present(status)) status=0
d32a47033   daniau   git-svn-id: https...
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
          fvn_d_det=0.d0
          deallocate(ipiv)
          deallocate(wc_a)
          return
      end if
      fvn_d_det=1.d0
      do i=1,d
          if (ipiv(i)==i) then
              fvn_d_det=fvn_d_det*wc_a(i,i)
          else
              fvn_d_det=-fvn_d_det*wc_a(i,i)
          end if
      end do
      deallocate(ipiv)
      deallocate(wc_a)
  
  end function
  
  function fvn_c_det(d,a,status)    !
      ! Evaluate the determinant of a square matrix using lapack LU factorization
      !
      ! d (in) : matrix rank
      ! a (in) : The Matrix
      ! status (out) : =0 if LU factorization failed
      !
8a085e035   daniau   git-svn-id: https...
506
      implicit none
d32a47033   daniau   git-svn-id: https...
507
508
      integer, intent(in) :: d
      complex, intent(in) :: a(d,d)
38581db0c   daniau   git-svn-id: https...
509
      integer, intent(out), optional :: status
d32a47033   daniau   git-svn-id: https...
510
511
512
513
514
515
      complex :: fvn_c_det
      
      complex, allocatable :: wc_a(:,:)
      integer, allocatable :: ipiv(:)
      integer :: info,i
      
38581db0c   daniau   git-svn-id: https...
516
       if (present(status)) status=1
d32a47033   daniau   git-svn-id: https...
517
518
519
520
521
      allocate(wc_a(d,d))
      allocate(ipiv(d))
      wc_a(:,:)=a(:,:)
      call cgetrf(d,d,wc_a,d,ipiv,info)
      if (info/= 0) then
38581db0c   daniau   git-svn-id: https...
522
           if (present(status)) status=0
d32a47033   daniau   git-svn-id: https...
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
          fvn_c_det=(0.e0,0.e0)
          deallocate(ipiv)
          deallocate(wc_a)
          return
      end if
      fvn_c_det=(1.e0,0.e0)
      do i=1,d
          if (ipiv(i)==i) then
              fvn_c_det=fvn_c_det*wc_a(i,i)
          else
              fvn_c_det=-fvn_c_det*wc_a(i,i)
          end if
      end do
      deallocate(ipiv)
      deallocate(wc_a)
  
  end function
  
  function fvn_z_det(d,a,status)
      !
      ! Evaluate the determinant of a square matrix using lapack LU factorization
      !
      ! d (in) : matrix rank
      ! a (in) : The Matrix
      ! det (out) : determinant
      ! status (out) : =0 if LU factorization failed
      !
8a085e035   daniau   git-svn-id: https...
550
      implicit none
d32a47033   daniau   git-svn-id: https...
551
552
      integer, intent(in) :: d
      double complex, intent(in) :: a(d,d)
38581db0c   daniau   git-svn-id: https...
553
      integer, intent(out), optional :: status
d32a47033   daniau   git-svn-id: https...
554
555
556
557
558
559
      double complex :: fvn_z_det
      
      double complex, allocatable :: wc_a(:,:)
      integer, allocatable :: ipiv(:)
      integer :: info,i
      
38581db0c   daniau   git-svn-id: https...
560
       if (present(status)) status=1
d32a47033   daniau   git-svn-id: https...
561
562
563
564
565
      allocate(wc_a(d,d))
      allocate(ipiv(d))
      wc_a(:,:)=a(:,:)
      call zgetrf(d,d,wc_a,d,ipiv,info)
      if (info/= 0) then
38581db0c   daniau   git-svn-id: https...
566
           if (present(status)) status=0
d32a47033   daniau   git-svn-id: https...
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
          fvn_z_det=(0.d0,0.d0)
          deallocate(ipiv)
          deallocate(wc_a)
          return
      end if
      fvn_z_det=(1.d0,0.d0)
      do i=1,d
          if (ipiv(i)==i) then
              fvn_z_det=fvn_z_det*wc_a(i,i)
          else
              fvn_z_det=-fvn_z_det*wc_a(i,i)
          end if
      end do
      deallocate(ipiv)
      deallocate(wc_a)
  
  end function
  
  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  !
  ! Condition test
  !
  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  ! 1-norm 
  ! fonction lapack slange,dlange,clange,zlange pour obtenir la 1-norm
  ! fonction lapack sgecon,dgecon,cgecon,zgecon pour calculer la rcond
  !
  subroutine fvn_s_matcon(d,a,rcond,status)
      ! Matrix condition (reciprocal of condition number)
      ! 
      ! d (in) : matrix rank
      ! a (in) : The Matrix
      ! rcond (out) : guess what
      ! status (out) : =0 if something went wrong
      !
8a085e035   daniau   git-svn-id: https...
602
      implicit none
d32a47033   daniau   git-svn-id: https...
603
604
605
      integer, intent(in) :: d
      real, intent(in) :: a(d,d)
      real, intent(out) :: rcond
38581db0c   daniau   git-svn-id: https...
606
      integer, intent(out), optional :: status
d32a47033   daniau   git-svn-id: https...
607
608
609
610
611
612
613
614
615
616
617
      
      real, allocatable :: work(:)
      integer, allocatable :: iwork(:)
      real :: anorm
      real, allocatable :: wc_a(:,:) ! working copy of a
      integer :: info
      integer, allocatable :: ipiv(:)
      
      real, external :: slange
      
      
38581db0c   daniau   git-svn-id: https...
618
       if (present(status)) status=1
d32a47033   daniau   git-svn-id: https...
619
620
621
622
623
624
625
626
627
628
      
      anorm=slange('1',d,d,a,d,work) ! work is unallocated as it is only used when computing infinity norm
      
      allocate(wc_a(d,d))
      !call scopy(d*d,a,1,wc_a,1)
      wc_a(:,:)=a(:,:)
      
      allocate(ipiv(d))
      call sgetrf(d,d,wc_a,d,ipiv,info)
      if (info /= 0) then
38581db0c   daniau   git-svn-id: https...
629
           if (present(status)) status=0
d32a47033   daniau   git-svn-id: https...
630
631
632
633
634
635
636
637
          deallocate(ipiv)
          deallocate(wc_a)
          return
      end if
      allocate(work(4*d))
      allocate(iwork(d))
      call sgecon('1',d,wc_a,d,anorm,rcond,work,iwork,info)
      if (info /= 0) then
38581db0c   daniau   git-svn-id: https...
638
           if (present(status)) status=0
d32a47033   daniau   git-svn-id: https...
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
      end if
      deallocate(iwork)
      deallocate(work)
      deallocate(ipiv)
      deallocate(wc_a)
  
  end subroutine
  
  subroutine fvn_d_matcon(d,a,rcond,status)
      ! Matrix condition (reciprocal of condition number)
      ! 
      ! d (in) : matrix rank
      ! a (in) : The Matrix
      ! rcond (out) : guess what
      ! status (out) : =0 if something went wrong
      !
8a085e035   daniau   git-svn-id: https...
655
      implicit none
d32a47033   daniau   git-svn-id: https...
656
657
658
      integer, intent(in) :: d
      double precision, intent(in) :: a(d,d)
      double precision, intent(out) :: rcond
38581db0c   daniau   git-svn-id: https...
659
      integer, intent(out), optional :: status
d32a47033   daniau   git-svn-id: https...
660
661
662
663
664
665
666
667
668
669
670
      
      double precision, allocatable :: work(:)
      integer, allocatable :: iwork(:)
      double precision :: anorm
      double precision, allocatable :: wc_a(:,:) ! working copy of a
      integer :: info
      integer, allocatable :: ipiv(:)
      
      double precision, external :: dlange
      
      
38581db0c   daniau   git-svn-id: https...
671
       if (present(status)) status=1
d32a47033   daniau   git-svn-id: https...
672
673
674
675
676
677
678
679
680
681
      
      anorm=dlange('1',d,d,a,d,work) ! work is unallocated as it is only used when computing infinity norm
      
      allocate(wc_a(d,d))
      !call dcopy(d*d,a,1,wc_a,1)
      wc_a(:,:)=a(:,:)
      
      allocate(ipiv(d))
      call dgetrf(d,d,wc_a,d,ipiv,info)
      if (info /= 0) then
38581db0c   daniau   git-svn-id: https...
682
           if (present(status)) status=0
d32a47033   daniau   git-svn-id: https...
683
684
685
686
687
688
689
690
691
          deallocate(ipiv)
          deallocate(wc_a)
          return
      end if
  
      allocate(work(4*d))
      allocate(iwork(d))
      call dgecon('1',d,wc_a,d,anorm,rcond,work,iwork,info)
      if (info /= 0) then
38581db0c   daniau   git-svn-id: https...
692
           if (present(status)) status=0
d32a47033   daniau   git-svn-id: https...
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
      end if
      deallocate(iwork)
      deallocate(work)
      deallocate(ipiv)
      deallocate(wc_a)
  
  end subroutine
  
  subroutine fvn_c_matcon(d,a,rcond,status)
      ! Matrix condition (reciprocal of condition number)
      ! 
      ! d (in) : matrix rank
      ! a (in) : The Matrix
      ! rcond (out) : guess what
      ! status (out) : =0 if something went wrong
      !
8a085e035   daniau   git-svn-id: https...
709
      implicit none
d32a47033   daniau   git-svn-id: https...
710
711
712
      integer, intent(in) :: d
      complex, intent(in) :: a(d,d)
      real, intent(out) :: rcond
38581db0c   daniau   git-svn-id: https...
713
      integer, intent(out), optional :: status
d32a47033   daniau   git-svn-id: https...
714
715
716
717
718
719
720
721
722
723
724
725
      
      real, allocatable :: rwork(:)
      complex, allocatable :: work(:)
      integer, allocatable :: iwork(:)
      real :: anorm
      complex, allocatable :: wc_a(:,:) ! working copy of a
      integer :: info
      integer, allocatable :: ipiv(:)
      
      real, external :: clange
      
      
38581db0c   daniau   git-svn-id: https...
726
       if (present(status)) status=1
d32a47033   daniau   git-svn-id: https...
727
728
729
730
731
732
733
734
735
736
      
      anorm=clange('1',d,d,a,d,rwork) ! rwork is unallocated as it is only used when computing infinity norm
      
      allocate(wc_a(d,d))
      !call ccopy(d*d,a,1,wc_a,1)
      wc_a(:,:)=a(:,:)
      
      allocate(ipiv(d))
      call cgetrf(d,d,wc_a,d,ipiv,info)
      if (info /= 0) then
38581db0c   daniau   git-svn-id: https...
737
           if (present(status)) status=0
d32a47033   daniau   git-svn-id: https...
738
739
740
741
742
743
744
745
          deallocate(ipiv)
          deallocate(wc_a)
          return
      end if
      allocate(work(2*d))
      allocate(rwork(2*d))
      call cgecon('1',d,wc_a,d,anorm,rcond,work,rwork,info)
      if (info /= 0) then
38581db0c   daniau   git-svn-id: https...
746
           if (present(status)) status=0
d32a47033   daniau   git-svn-id: https...
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
      end if
      deallocate(rwork)
      deallocate(work)
      deallocate(ipiv)
      deallocate(wc_a)
  end subroutine
  
  subroutine fvn_z_matcon(d,a,rcond,status)
      ! Matrix condition (reciprocal of condition number)
      ! 
      ! d (in) : matrix rank
      ! a (in) : The Matrix
      ! rcond (out) : guess what
      ! status (out) : =0 if something went wrong
      !
8a085e035   daniau   git-svn-id: https...
762
      implicit none
d32a47033   daniau   git-svn-id: https...
763
764
765
      integer, intent(in) :: d
      double complex, intent(in) :: a(d,d)
      double precision, intent(out) :: rcond
38581db0c   daniau   git-svn-id: https...
766
      integer, intent(out), optional :: status
d32a47033   daniau   git-svn-id: https...
767
768
769
770
771
772
773
774
775
776
777
      
      double complex, allocatable :: work(:)
      double precision, allocatable :: rwork(:)
      double precision :: anorm
      double complex, allocatable :: wc_a(:,:) ! working copy of a
      integer :: info
      integer, allocatable :: ipiv(:)
      
      double precision, external :: zlange
      
      
38581db0c   daniau   git-svn-id: https...
778
       if (present(status)) status=1
d32a47033   daniau   git-svn-id: https...
779
780
781
782
783
784
785
786
787
788
      
      anorm=zlange('1',d,d,a,d,rwork) ! rwork is unallocated as it is only used when computing infinity norm
      
      allocate(wc_a(d,d))
      !call zcopy(d*d,a,1,wc_a,1)
      wc_a(:,:)=a(:,:)
      
      allocate(ipiv(d))
      call zgetrf(d,d,wc_a,d,ipiv,info)
      if (info /= 0) then
38581db0c   daniau   git-svn-id: https...
789
           if (present(status)) status=0
d32a47033   daniau   git-svn-id: https...
790
791
792
793
794
795
796
797
798
          deallocate(ipiv)
          deallocate(wc_a)
          return
      end if
  
      allocate(work(2*d))
      allocate(rwork(2*d))
      call zgecon('1',d,wc_a,d,anorm,rcond,work,rwork,info)
      if (info /= 0) then
38581db0c   daniau   git-svn-id: https...
799
           if (present(status)) status=0
d32a47033   daniau   git-svn-id: https...
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
      end if
      deallocate(rwork)
      deallocate(work)
      deallocate(ipiv)
      deallocate(wc_a)
  end subroutine
  
  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  !
  ! Valeurs propres/ Vecteurs propre
  !
  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  
  subroutine fvn_s_matev(d,a,evala,eveca,status)
      ! 
      ! integer d (in) : matrice rank
      ! real a(d,d) (in) : The Matrix
      ! complex evala(d) (out) : eigenvalues
      ! complex eveca(d,d) (out) : eveca(:,j) = jth eigenvector
      ! integer (out) : status =0 if something went wrong
      !
      ! interfacing Lapack routine SGEEV
8a085e035   daniau   git-svn-id: https...
822
      implicit none
d32a47033   daniau   git-svn-id: https...
823
824
825
826
      integer, intent(in) :: d
      real, intent(in) :: a(d,d)
      complex, intent(out) :: evala(d)
      complex, intent(out) :: eveca(d,d)
38581db0c   daniau   git-svn-id: https...
827
      integer, intent(out), optional :: status
d32a47033   daniau   git-svn-id: https...
828
829
830
831
832
833
834
835
836
837
838
839
      
      real, allocatable :: wc_a(:,:)  ! a working copy of a
      integer :: info
      integer :: lwork
      real, allocatable :: wr(:),wi(:)
      real :: vl      ! unused but necessary for the call
      real, allocatable :: vr(:,:)
      real, allocatable :: work(:)
      real :: twork(1)
      integer i
      integer j
      
38581db0c   daniau   git-svn-id: https...
840
       if (present(status)) status=1
d32a47033   daniau   git-svn-id: https...
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
      ! making a working copy of a
      allocate(wc_a(d,d))
      !call scopy(d*d,a,1,wc_a,1)
      wc_a(:,:)=a(:,:)
      
      allocate(wr(d))
      allocate(wi(d))
      allocate(vr(d,d))
      ! query optimal work size
      call sgeev('N','V',d,wc_a,d,wr,wi,vl,1,vr,d,twork,-1,info)
      lwork=int(twork(1))
      allocate(work(lwork))
      call sgeev('N','V',d,wc_a,d,wr,wi,vl,1,vr,d,work,lwork,info)
  
      if (info /= 0) then
38581db0c   daniau   git-svn-id: https...
856
           if (present(status)) status=0
d32a47033   daniau   git-svn-id: https...
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
          deallocate(work)
          deallocate(vr)
          deallocate(wi)
          deallocate(wr)
          deallocate(wc_a)
          return
      end if
  
      ! now fill in the results
      i=1
      do while(i<=d)
          evala(i)=cmplx(wr(i),wi(i))
          if (wi(i) == 0.) then ! eigenvalue is real
              eveca(:,i)=cmplx(vr(:,i),0.)
          else ! eigenvalue is complex
              evala(i+1)=cmplx(wr(i+1),wi(i+1))
              eveca(:,i)=cmplx(vr(:,i),vr(:,i+1))
              eveca(:,i+1)=cmplx(vr(:,i),-vr(:,i+1))
              i=i+1
          end if
          i=i+1
      enddo
      deallocate(work)
      deallocate(vr)
      deallocate(wi)
      deallocate(wr)
      deallocate(wc_a)
  
  end subroutine
  
  subroutine fvn_d_matev(d,a,evala,eveca,status)
      ! 
      ! integer d (in) : matrice rank
      ! double precision a(d,d) (in) : The Matrix
      ! double complex evala(d) (out) : eigenvalues
      ! double complex eveca(d,d) (out) : eveca(:,j) = jth eigenvector
      ! integer (out) : status =0 if something went wrong
      !
      ! interfacing Lapack routine DGEEV
8a085e035   daniau   git-svn-id: https...
896
      implicit none
d32a47033   daniau   git-svn-id: https...
897
898
899
900
      integer, intent(in) :: d
      double precision, intent(in) :: a(d,d)
      double complex, intent(out) :: evala(d)
      double complex, intent(out) :: eveca(d,d)
38581db0c   daniau   git-svn-id: https...
901
      integer, intent(out), optional :: status
d32a47033   daniau   git-svn-id: https...
902
903
904
905
906
907
908
909
910
911
912
913
      
      double precision, allocatable :: wc_a(:,:)  ! a working copy of a
      integer :: info
      integer :: lwork
      double precision, allocatable :: wr(:),wi(:)
      double precision :: vl      ! unused but necessary for the call
      double precision, allocatable :: vr(:,:)
      double precision, allocatable :: work(:)
      double precision :: twork(1)
      integer i
      integer j
      
38581db0c   daniau   git-svn-id: https...
914
      if (present(status)) status=1
d32a47033   daniau   git-svn-id: https...
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
      ! making a working copy of a
      allocate(wc_a(d,d))
      !call dcopy(d*d,a,1,wc_a,1)
      wc_a(:,:)=a(:,:)
      
      allocate(wr(d))
      allocate(wi(d))
      allocate(vr(d,d))
      ! query optimal work size
      call dgeev('N','V',d,wc_a,d,wr,wi,vl,1,vr,d,twork,-1,info)
      lwork=int(twork(1))
      allocate(work(lwork))
      call dgeev('N','V',d,wc_a,d,wr,wi,vl,1,vr,d,work,lwork,info)
  
      if (info /= 0) then
38581db0c   daniau   git-svn-id: https...
930
           if (present(status)) status=0
d32a47033   daniau   git-svn-id: https...
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
          deallocate(work)
          deallocate(vr)
          deallocate(wi)
          deallocate(wr)
          deallocate(wc_a)
          return
      end if
  
      ! now fill in the results
      i=1
      do while(i<=d)
          evala(i)=dcmplx(wr(i),wi(i))
          if (wi(i) == 0.) then ! eigenvalue is real
              eveca(:,i)=dcmplx(vr(:,i),0.)
          else ! eigenvalue is complex
              evala(i+1)=dcmplx(wr(i+1),wi(i+1))
              eveca(:,i)=dcmplx(vr(:,i),vr(:,i+1))
              eveca(:,i+1)=dcmplx(vr(:,i),-vr(:,i+1))
              i=i+1
          end if
          i=i+1
      enddo
  
      deallocate(work)
      deallocate(vr)
      deallocate(wi)
      deallocate(wr)
      deallocate(wc_a)
  
  end subroutine
  
  subroutine fvn_c_matev(d,a,evala,eveca,status)
      ! 
      ! integer d (in) : matrice rank
      ! complex a(d,d) (in) : The Matrix
      ! complex evala(d) (out) : eigenvalues
      ! complex eveca(d,d) (out) : eveca(:,j) = jth eigenvector
      ! integer (out) : status =0 if something went wrong
      !
      ! interfacing Lapack routine CGEEV
8a085e035   daniau   git-svn-id: https...
971
      implicit none
d32a47033   daniau   git-svn-id: https...
972
973
974
975
      integer, intent(in) :: d
      complex, intent(in) :: a(d,d)
      complex, intent(out) :: evala(d)
      complex, intent(out) :: eveca(d,d)
38581db0c   daniau   git-svn-id: https...
976
      integer, intent(out), optional :: status
d32a47033   daniau   git-svn-id: https...
977
978
979
980
981
982
983
984
  
      complex, allocatable :: wc_a(:,:) ! a working copy of a
      integer :: info
      integer :: lwork
      complex, allocatable :: work(:)
      complex :: twork(1)
      real, allocatable :: rwork(:)
      complex :: vl   ! unused but necessary for the call
38581db0c   daniau   git-svn-id: https...
985
       if (present(status)) status=1
d32a47033   daniau   git-svn-id: https...
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
  
      ! making a working copy of a
      allocate(wc_a(d,d))
      !call ccopy(d*d,a,1,wc_a,1)
      wc_a(:,:)=a(:,:)
      
      
      ! query optimal work size
      call cgeev('N','V',d,wc_a,d,evala,vl,1,eveca,d,twork,-1,rwork,info)
      lwork=int(twork(1))
      allocate(work(lwork))
      allocate(rwork(2*d))
      call cgeev('N','V',d,wc_a,d,evala,vl,1,eveca,d,work,lwork,rwork,info)
      
      if (info /= 0) then
38581db0c   daniau   git-svn-id: https...
1001
           if (present(status)) status=0
d32a47033   daniau   git-svn-id: https...
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
      end if
      deallocate(rwork)
      deallocate(work)
      deallocate(wc_a)
  
  end subroutine
  
  subroutine fvn_z_matev(d,a,evala,eveca,status)
      ! 
      ! integer d (in) : matrice rank
      ! double complex a(d,d) (in) : The Matrix
      ! double complex evala(d) (out) : eigenvalues
      ! double complex eveca(d,d) (out) : eveca(:,j) = jth eigenvector
      ! integer (out) : status =0 if something went wrong
      !
      ! interfacing Lapack routine ZGEEV
8a085e035   daniau   git-svn-id: https...
1018
      implicit none
d32a47033   daniau   git-svn-id: https...
1019
1020
1021
1022
      integer, intent(in) :: d
      double complex, intent(in) :: a(d,d)
      double complex, intent(out) :: evala(d)
      double complex, intent(out) :: eveca(d,d)
38581db0c   daniau   git-svn-id: https...
1023
      integer, intent(out), optional :: status
d32a47033   daniau   git-svn-id: https...
1024
1025
1026
1027
1028
1029
1030
1031
1032
  
      double complex, allocatable :: wc_a(:,:) ! a working copy of a
      integer :: info
      integer :: lwork
      double complex, allocatable :: work(:)
      double complex :: twork(1)
      double precision, allocatable :: rwork(:)
      double complex :: vl   ! unused but necessary for the call
      
38581db0c   daniau   git-svn-id: https...
1033
       if (present(status)) status=1
d32a47033   daniau   git-svn-id: https...
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
  
      ! making a working copy of a
      allocate(wc_a(d,d))
      !call zcopy(d*d,a,1,wc_a,1)
      wc_a(:,:)=a(:,:)
      
      ! query optimal work size
      call zgeev('N','V',d,wc_a,d,evala,vl,1,eveca,d,twork,-1,rwork,info)
      lwork=int(twork(1))
      allocate(work(lwork))
      allocate(rwork(2*d))
      call zgeev('N','V',d,wc_a,d,evala,vl,1,eveca,d,work,lwork,rwork,info)
      
      if (info /= 0) then
38581db0c   daniau   git-svn-id: https...
1048
           if (present(status)) status=0
d32a47033   daniau   git-svn-id: https...
1049
1050
1051
1052
1053
1054
      end if
      deallocate(rwork)
      deallocate(work)
      deallocate(wc_a)
  
  end subroutine
15adbf52b   daniau   git-svn-id: https...
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
  
  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  !
  ! Quadratic interpolation of tabulated function of 1,2 or 3 variables
  !
  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  
  subroutine fvn_s_find_interval(x,i,xdata,n)
        implicit none
        ! This routine find the indice i where xdata(i) <= x < xdata(i+1)
        ! xdata(n) must contains a set of increasingly ordered values 
        ! if x < xdata(1) i=0 is returned
        ! if x > xdata(n) i=n is returned
        ! special case is where x=xdata(n) then n-1 is returned so 
        ! we will not exclude the upper limit
        ! a simple dichotomy method is used
  
        real(kind=4), intent(in) :: x
        real(kind=4), intent(in), dimension(n) :: xdata
        integer(kind=4), intent(in) :: n
        integer(kind=4), intent(out) :: i
  
        integer(kind=4) :: imin,imax,imoyen
  
        ! special case is where x=xdata(n) then n-1 is returned so 
        ! we will not exclude the upper limit
        if (x == xdata(n)) then
              i=n-1
              return
        end if
  
        ! if x < xdata(1) i=0 is returned
        if (x < xdata(1)) then
              i=0
              return
        end if
  
        ! if x > xdata(n) i=n is returned
        if (x > xdata(n)) then
              i=n
              return
        end if
  
        ! here xdata(1) <= x <= xdata(n)
        imin=0
        imax=n+1
  
        do while((imax-imin) > 1)
              imoyen=(imax+imin)/2
              if (x >= xdata(imoyen)) then
                    imin=imoyen
              else
                    imax=imoyen
              end if
        end do
  
        i=imin
  
  end subroutine
  
  
  subroutine fvn_d_find_interval(x,i,xdata,n)
        implicit none
        ! This routine find the indice i where xdata(i) <= x < xdata(i+1)
        ! xdata(n) must contains a set of increasingly ordered values 
        ! if x < xdata(1) i=0 is returned
        ! if x > xdata(n) i=n is returned
        ! special case is where x=xdata(n) then n-1 is returned so 
        ! we will not exclude the upper limit
        ! a simple dichotomy method is used
  
        real(kind=8), intent(in) :: x
        real(kind=8), intent(in), dimension(n) :: xdata
        integer(kind=4), intent(in) :: n
        integer(kind=4), intent(out) :: i
  
        integer(kind=4) :: imin,imax,imoyen
  
        ! special case is where x=xdata(n) then n-1 is returned so 
        ! we will not exclude the upper limit
        if (x == xdata(n)) then
              i=n-1
              return
        end if
  
        ! if x < xdata(1) i=0 is returned
        if (x < xdata(1)) then
              i=0
              return
        end if
  
        ! if x > xdata(n) i=n is returned
        if (x > xdata(n)) then
              i=n
              return
        end if
  
        ! here xdata(1) <= x <= xdata(n)
        imin=0
        imax=n+1
  
        do while((imax-imin) > 1)
              imoyen=(imax+imin)/2
              if (x >= xdata(imoyen)) then
                    imin=imoyen
              else
                    imax=imoyen
              end if
        end do
  
        i=imin
  
  end subroutine
  
  
  function fvn_s_quad_interpol(x,n,xdata,ydata)
        implicit none
        ! This function evaluate the value of a function defined by a set of points
        ! and values, using a quadratic interpolation
        ! xdata must be increasingly ordered
        ! x must be within xdata(1) and xdata(n) to actually do interpolation
        ! otherwise extrapolation is done
        integer(kind=4), intent(in) :: n
        real(kind=4), intent(in), dimension(n) :: xdata,ydata
        real(kind=4), intent(in) :: x
        real(kind=4) ::  fvn_s_quad_interpol
  
        integer(kind=4) :: iinf,base,i,j
        real(kind=4) :: p
  
        call fvn_s_find_interval(x,iinf,xdata,n)
  
        ! Settings for extrapolation
        if (iinf==0) then
              ! TODO -> Lower bound extrapolation warning
              iinf=1
        end if
  
        if (iinf==n) then
              ! TODO -> Higher bound extrapolation warning
              iinf=n-1
        end if
  
        ! The three points we will use are iinf-1,iinf and iinf+1 with the
        ! exception of the first interval, where iinf=1 we will use 1,2 and 3
        if (iinf==1) then
              base=0
        else
              base=iinf-2
        end if
  
        ! The three points we will use are :
        ! xdata/ydata(base+1),xdata/ydata(base+2),xdata/ydata(base+3)
  
        ! Straight forward Lagrange polynomial
        fvn_s_quad_interpol=0.
        do i=1,3
              !  polynome i
              p=ydata(base+i)
              do j=1,3
                    if (j /= i) then
                          p=p*(x-xdata(base+j))/(xdata(base+i)-xdata(base+j))
                    end if
              end do
              fvn_s_quad_interpol=fvn_s_quad_interpol+p
        end do
  
  end function
  
  
  function fvn_d_quad_interpol(x,n,xdata,ydata)
        implicit none
        ! This function evaluate the value of a function defined by a set of points
        ! and values, using a quadratic interpolation
        ! xdata must be increasingly ordered
        ! x must be within xdata(1) and xdata(n) to actually do interpolation
        ! otherwise extrapolation is done
        integer(kind=4), intent(in) :: n
        real(kind=8), intent(in), dimension(n) :: xdata,ydata
        real(kind=8), intent(in) :: x
        real(kind=8) ::  fvn_d_quad_interpol
  
        integer(kind=4) :: iinf,base,i,j
        real(kind=8) :: p
  
        call fvn_d_find_interval(x,iinf,xdata,n)
  
        ! Settings for extrapolation
        if (iinf==0) then
              ! TODO -> Lower bound extrapolation warning
              iinf=1
        end if
  
        if (iinf==n) then
              ! TODO Higher bound extrapolation warning
              iinf=n-1
        end if
  
        ! The three points we will use are iinf-1,iinf and iinf+1 with the
        ! exception of the first interval, where iinf=1 we will use 1,2 and 3
        if (iinf==1) then
              base=0
        else
              base=iinf-2
        end if
  
        ! The three points we will use are :
        ! xdata/ydata(base+1),xdata/ydata(base+2),xdata/ydata(base+3)
  
        ! Straight forward Lagrange polynomial
        fvn_d_quad_interpol=0.
        do i=1,3
              !  polynome i
              p=ydata(base+i)
              do j=1,3
                    if (j /= i) then
                          p=p*(x-xdata(base+j))/(xdata(base+i)-xdata(base+j))
                    end if
              end do
              fvn_d_quad_interpol=fvn_d_quad_interpol+p
        end do
  
  end function
  
  
  function fvn_s_quad_2d_interpol(x,y,nx,xdata,ny,ydata,zdata)
        implicit none
        ! This function evaluate the value of a two variable function defined by a
        ! set of points and values, using a quadratic interpolation
        ! xdata and ydata must be increasingly ordered
        ! the couple (x,y) must be as x within xdata(1) and xdata(nx) and
        ! y within ydata(1) and ydata(ny) to actually do interpolation
        ! otherwise extrapolation is done
        integer(kind=4), intent(in) :: nx,ny
        real(kind=4), intent(in) :: x,y
        real(kind=4), intent(in), dimension(nx) :: xdata
        real(kind=4), intent(in), dimension(ny) :: ydata
        real(kind=4), intent(in), dimension(nx,ny) :: zdata
        real(kind=4) :: fvn_s_quad_2d_interpol
  
        integer(kind=4) :: ixinf,iyinf,basex,basey,i
        real(kind=4),dimension(3) :: ztmp
        !real(kind=4), external :: fvn_s_quad_interpol
  
        call fvn_s_find_interval(x,ixinf,xdata,nx)
        call fvn_s_find_interval(y,iyinf,ydata,ny)
  
        ! Settings for extrapolation
        if (ixinf==0) then
              ! TODO -> Lower x  bound extrapolation warning
              ixinf=1
        end if
  
        if (ixinf==nx) then
              ! TODO -> Higher x bound extrapolation warning
              ixinf=nx-1
        end if
  
        if (iyinf==0) then
              ! TODO -> Lower y  bound extrapolation warning
              iyinf=1
        end if
  
        if (iyinf==ny) then
              ! TODO -> Higher y bound extrapolation warning
              iyinf=ny-1
        end if
  
        ! The three points we will use are iinf-1,iinf and iinf+1 with the
        ! exception of the first interval, where iinf=1 we will use 1,2 and 3
        if (ixinf==1) then
              basex=0
        else
              basex=ixinf-2
        end if
  
        if (iyinf==1) then
              basey=0
        else
              basey=iyinf-2
        end if
  
        ! First we make 3 interpolations for x at y(base+1),y(base+2),y(base+3)
        ! stored in ztmp(1:3)
        do i=1,3
              ztmp(i)=fvn_s_quad_interpol(x,nx,xdata,zdata(:,basey+i))
        end do
  
        ! Then we make an interpolation for y using previous interpolations
        fvn_s_quad_2d_interpol=fvn_s_quad_interpol(y,3,ydata(basey+1:basey+3),ztmp)
  end function
  
  
  function fvn_d_quad_2d_interpol(x,y,nx,xdata,ny,ydata,zdata)
        implicit none
        ! This function evaluate the value of a two variable function defined by a
        ! set of points and values, using a quadratic interpolation
        ! xdata and ydata must be increasingly ordered
        ! the couple (x,y) must be as x within xdata(1) and xdata(nx) and
        ! y within ydata(1) and ydata(ny) to actually do interpolation
        ! otherwise extrapolation is done
        integer(kind=4), intent(in) :: nx,ny
        real(kind=8), intent(in) :: x,y
        real(kind=8), intent(in), dimension(nx) :: xdata
        real(kind=8), intent(in), dimension(ny) :: ydata
        real(kind=8), intent(in), dimension(nx,ny) :: zdata
        real(kind=8) :: fvn_d_quad_2d_interpol
  
        integer(kind=4) :: ixinf,iyinf,basex,basey,i
        real(kind=8),dimension(3) :: ztmp
        !real(kind=8), external :: fvn_d_quad_interpol
  
        call fvn_d_find_interval(x,ixinf,xdata,nx)
        call fvn_d_find_interval(y,iyinf,ydata,ny)
  
        ! Settings for extrapolation
        if (ixinf==0) then
              ! TODO -> Lower x  bound extrapolation warning
              ixinf=1
        end if
  
        if (ixinf==nx) then
              ! TODO -> Higher x bound extrapolation warning
              ixinf=nx-1
        end if
  
        if (iyinf==0) then
              ! TODO -> Lower y  bound extrapolation warning
              iyinf=1
        end if
  
        if (iyinf==ny) then
              ! TODO -> Higher y bound extrapolation warning
              iyinf=ny-1
        end if
  
        ! The three points we will use are iinf-1,iinf and iinf+1 with the
        ! exception of the first interval, where iinf=1 we will use 1,2 and 3
        if (ixinf==1) then
              basex=0
        else
              basex=ixinf-2
        end if
  
        if (iyinf==1) then
              basey=0
        else
              basey=iyinf-2
        end if
  
        ! First we make 3 interpolations for x at y(base+1),y(base+2),y(base+3)
        ! stored in ztmp(1:3)
        do i=1,3
              ztmp(i)=fvn_d_quad_interpol(x,nx,xdata,zdata(:,basey+i))
        end do
  
        ! Then we make an interpolation for y using previous interpolations
        fvn_d_quad_2d_interpol=fvn_d_quad_interpol(y,3,ydata(basey+1:basey+3),ztmp)
  end function
  
  
  function fvn_s_quad_3d_interpol(x,y,z,nx,xdata,ny,ydata,nz,zdata,tdata)
        implicit none
        ! This function evaluate the value of a 3 variables function defined by a
        ! set of points and values, using a quadratic interpolation
        ! xdata, ydata and zdata must be increasingly ordered
        ! The triplet (x,y,z) must be within xdata,ydata and zdata to actually 
        ! perform an interpolation, otherwise extrapolation is done
        integer(kind=4), intent(in) :: nx,ny,nz
        real(kind=4), intent(in) :: x,y,z
        real(kind=4), intent(in), dimension(nx) :: xdata
        real(kind=4), intent(in), dimension(ny) :: ydata
        real(kind=4), intent(in), dimension(nz) :: zdata
        real(kind=4), intent(in), dimension(nx,ny,nz) :: tdata
        real(kind=4) :: fvn_s_quad_3d_interpol
  
        integer(kind=4) :: ixinf,iyinf,izinf,basex,basey,basez,i,j
        !real(kind=4), external :: fvn_s_quad_interpol,fvn_s_quad_2d_interpol
        real(kind=4),dimension(3,3) :: ttmp
  
        call fvn_s_find_interval(x,ixinf,xdata,nx)
        call fvn_s_find_interval(y,iyinf,ydata,ny)
        call fvn_s_find_interval(z,izinf,zdata,nz)
  
        ! Settings for extrapolation
        if (ixinf==0) then
              ! TODO -> Lower x  bound extrapolation warning
              ixinf=1
        end if
  
        if (ixinf==nx) then
              ! TODO -> Higher x bound extrapolation warning
              ixinf=nx-1
        end if
  
        if (iyinf==0) then
              ! TODO -> Lower y  bound extrapolation warning
              iyinf=1
        end if
  
        if (iyinf==ny) then
              ! TODO -> Higher y bound extrapolation warning
              iyinf=ny-1
        end if
  
        if (izinf==0) then
              ! TODO -> Lower z bound extrapolation warning
              izinf=1
        end if
  
        if (izinf==nz) then
              ! TODO -> Higher z bound extrapolation warning
              izinf=nz-1
        end if
  
        ! The three points we will use are iinf-1,iinf and iinf+1 with the
        ! exception of the first interval, where iinf=1 we will use 1,2 and 3
        if (ixinf==1) then
              basex=0
        else
              basex=ixinf-2
        end if
  
        if (iyinf==1) then
              basey=0
        else
              basey=iyinf-2
        end if
  
        if (izinf==1) then
              basez=0
        else
              basez=izinf-2
        end if
  
        ! We first make 9 one dimensional interpolation on variable x.
        ! results are stored in ttmp
        do i=1,3
              do j=1,3
                    ttmp(i,j)=fvn_s_quad_interpol(x,nx,xdata,tdata(:,basey+i,basez+j))
              end do
        end do
  
        ! We then make a 2 dimensionnal interpolation on variables y and z
        fvn_s_quad_3d_interpol=fvn_s_quad_2d_interpol(y,z, &
              3,ydata(basey+1:basey+3),3,zdata(basez+1:basez+3),ttmp)
  end function
  
  
  function fvn_d_quad_3d_interpol(x,y,z,nx,xdata,ny,ydata,nz,zdata,tdata)
        implicit none
        ! This function evaluate the value of a 3 variables function defined by a
        ! set of points and values, using a quadratic interpolation
        ! xdata, ydata and zdata must be increasingly ordered
        ! The triplet (x,y,z) must be within xdata,ydata and zdata to actually 
        ! perform an interpolation, otherwise extrapolation is done
        integer(kind=4), intent(in) :: nx,ny,nz
        real(kind=8), intent(in) :: x,y,z
        real(kind=8), intent(in), dimension(nx) :: xdata
        real(kind=8), intent(in), dimension(ny) :: ydata
        real(kind=8), intent(in), dimension(nz) :: zdata
        real(kind=8), intent(in), dimension(nx,ny,nz) :: tdata
        real(kind=8) :: fvn_d_quad_3d_interpol
  
        integer(kind=4) :: ixinf,iyinf,izinf,basex,basey,basez,i,j
        !real(kind=8), external :: fvn_d_quad_interpol,fvn_d_quad_2d_interpol
        real(kind=8),dimension(3,3) :: ttmp
  
        call fvn_d_find_interval(x,ixinf,xdata,nx)
        call fvn_d_find_interval(y,iyinf,ydata,ny)
        call fvn_d_find_interval(z,izinf,zdata,nz)
  
        ! Settings for extrapolation
        if (ixinf==0) then
              ! TODO -> Lower x  bound extrapolation warning
              ixinf=1
        end if
  
        if (ixinf==nx) then
              ! TODO -> Higher x bound extrapolation warning
              ixinf=nx-1
        end if
  
        if (iyinf==0) then
              ! TODO -> Lower y  bound extrapolation warning
              iyinf=1
        end if
  
        if (iyinf==ny) then
              ! TODO -> Higher y bound extrapolation warning
              iyinf=ny-1
        end if
  
        if (izinf==0) then
              ! TODO -> Lower z bound extrapolation warning
              izinf=1
        end if
  
        if (izinf==nz) then
              ! TODO -> Higher z bound extrapolation warning
              izinf=nz-1
        end if
  
        ! The three points we will use are iinf-1,iinf and iinf+1 with the
        ! exception of the first interval, where iinf=1 we will use 1,2 and 3
        if (ixinf==1) then
              basex=0
        else
              basex=ixinf-2
        end if
  
        if (iyinf==1) then
              basey=0
        else
              basey=iyinf-2
        end if
  
        if (izinf==1) then
              basez=0
        else
              basez=izinf-2
        end if
  
        ! We first make 9 one dimensional interpolation on variable x.
        ! results are stored in ttmp
        do i=1,3
              do j=1,3
                    ttmp(i,j)=fvn_d_quad_interpol(x,nx,xdata,tdata(:,basey+i,basez+j))
              end do
        end do
  
        ! We then make a 2 dimensionnal interpolation on variables y and z
        fvn_d_quad_3d_interpol=fvn_d_quad_2d_interpol(y,z, &
              3,ydata(basey+1:basey+3),3,zdata(basez+1:basez+3),ttmp)
  end function
d32a47033   daniau   git-svn-id: https...
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  !
  ! Akima spline interpolation and spline evaluation
  !
  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  
  ! Single precision
  subroutine fvn_s_akima(n,x,y,br,co)
      implicit none
      integer, intent(in)  :: n
      real, intent(in) :: x(n)
      real, intent(in) :: y(n)
      real, intent(out) :: br(n)
      real, intent(out) :: co(4,n)
      
      real, allocatable :: var(:),z(:)
      real :: wi_1,wi
      integer :: i
      real :: dx,a,b
  
      ! br is just a copy of x
      br(:)=x(:)
      
      allocate(var(n))
      allocate(z(n))
      ! evaluate the variations
      do i=1, n-1
          var(i+2)=(y(i+1)-y(i))/(x(i+1)-x(i))
      end do
      var(n+2)=2.e0*var(n+1)-var(n)
      var(n+3)=2.e0*var(n+2)-var(n+1)
      var(2)=2.e0*var(3)-var(4)
      var(1)=2.e0*var(2)-var(3)
    
      do i = 1, n
      wi_1=abs(var(i+3)-var(i+2))
      wi=abs(var(i+1)-var(i))
      if ((wi_1+wi).eq.0.e0) then
          z(i)=(var(i+2)+var(i+1))/2.e0
      else
          z(i)=(wi_1*var(i+1)+wi*var(i+2))/(wi_1+wi)
      end if
      end do
      
      do i=1, n-1
          dx=x(i+1)-x(i)
          a=(z(i+1)-z(i))*dx                      ! coeff intermediaires pour calcul wd
          b=y(i+1)-y(i)-z(i)*dx                   ! coeff intermediaires pour calcul wd
          co(1,i)=y(i)
          co(2,i)=z(i)
          !co(3,i)=-(a-3.*b)/dx**2                ! méthode wd
          !co(4,i)=(a-2.*b)/dx**3                 ! méthode wd
          co(3,i)=(3.e0*var(i+2)-2.e0*z(i)-z(i+1))/dx   ! méthode JP Moreau
          co(4,i)=(z(i)+z(i+1)-2.e0*var(i+2))/dx**2  !
          ! les coefficients donnés par imsl sont co(3,i)*2 et co(4,i)*6
          ! etrangement la fonction csval corrige et donne la bonne valeur ...
      end do
      co(1,n)=y(n)
      co(2,n)=z(n)
      co(3,n)=0.e0
      co(4,n)=0.e0
  
      deallocate(z)
      deallocate(var)
  
  end subroutine
  
  ! Double precision
  subroutine fvn_d_akima(n,x,y,br,co)
  
      implicit none
      integer, intent(in)  :: n
      double precision, intent(in) :: x(n)
      double precision, intent(in) :: y(n)
      double precision, intent(out) :: br(n)
      double precision, intent(out) :: co(4,n)
      
      double precision, allocatable :: var(:),z(:)
      double precision :: wi_1,wi
      integer :: i
      double precision :: dx,a,b
      
      ! br is just a copy of x
      br(:)=x(:)
  
      allocate(var(n))
      allocate(z(n))
      ! evaluate the variations
      do i=1, n-1
          var(i+2)=(y(i+1)-y(i))/(x(i+1)-x(i))
      end do
      var(n+2)=2.d0*var(n+1)-var(n)
      var(n+3)=2.d0*var(n+2)-var(n+1)
      var(2)=2.d0*var(3)-var(4)
      var(1)=2.d0*var(2)-var(3)
    
      do i = 1, n
      wi_1=dabs(var(i+3)-var(i+2))
      wi=dabs(var(i+1)-var(i))
      if ((wi_1+wi).eq.0.d0) then
          z(i)=(var(i+2)+var(i+1))/2.d0
      else
          z(i)=(wi_1*var(i+1)+wi*var(i+2))/(wi_1+wi)
      end if
      end do
      
      do i=1, n-1
          dx=x(i+1)-x(i)
          a=(z(i+1)-z(i))*dx                      ! coeff intermediaires pour calcul wd
          b=y(i+1)-y(i)-z(i)*dx                   ! coeff intermediaires pour calcul wd
          co(1,i)=y(i)
          co(2,i)=z(i)
          !co(3,i)=-(a-3.*b)/dx**2                ! méthode wd
          !co(4,i)=(a-2.*b)/dx**3                 ! méthode wd
          co(3,i)=(3.d0*var(i+2)-2.d0*z(i)-z(i+1))/dx   ! méthode JP Moreau
          co(4,i)=(z(i)+z(i+1)-2.d0*var(i+2))/dx**2  !
          ! les coefficients donnés par imsl sont co(3,i)*2 et co(4,i)*6
          ! etrangement la fonction csval corrige et donne la bonne valeur ...
      end do
      co(1,n)=y(n)
      co(2,n)=z(n)
      co(3,n)=0.d0
      co(4,n)=0.d0
  
      deallocate(z)
      deallocate(var)
  
  end subroutine
  
  !
  ! Single precision spline evaluation
  !
  function fvn_s_spline_eval(x,n,br,co)
      implicit none
      real, intent(in) :: x           ! x must be br(1)<= x <= br(n+1) otherwise value is extrapolated
      integer, intent(in) :: n        ! number of intervals
      real, intent(in) :: br(n+1)     ! breakpoints
      real, intent(in) :: co(4,n+1)   ! spline coeeficients
      real :: fvn_s_spline_eval
      
      integer :: i
      real :: dx
      
      if (x<=br(1)) then
          i=1
      else if (x>=br(n+1)) then
          i=n
      else
      i=1
      do while(x>=br(i))
          i=i+1
      end do
      i=i-1
      end if
      dx=x-br(i)
      fvn_s_spline_eval=co(1,i)+co(2,i)*dx+co(3,i)*dx**2+co(4,i)*dx**3
  
  end function
  
  ! Double precision spline evaluation
  function fvn_d_spline_eval(x,n,br,co)
      implicit none
      double precision, intent(in) :: x           ! x must be br(1)<= x <= br(n+1) otherwise value is extrapolated
      integer, intent(in) :: n        ! number of intervals
      double precision, intent(in) :: br(n+1)     ! breakpoints
      double precision, intent(in) :: co(4,n+1)   ! spline coeeficients
      double precision :: fvn_d_spline_eval
      
      integer :: i
      double precision :: dx
      
      
      if (x<=br(1)) then
          i=1
      else if (x>=br(n+1)) then
          i=n
      else
      i=1
      do while(x>=br(i))
          i=i+1
      end do
      i=i-1
      end if
      
      dx=x-br(i)
      fvn_d_spline_eval=co(1,i)+co(2,i)*dx+co(3,i)*dx**2+co(4,i)*dx**3
  
  end function
8a085e035   daniau   git-svn-id: https...
1778
1779
1780
1781
1782
1783
1784
  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  ! 
  ! Least square problem
  !
  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  !
  !
126a3aed0   daniau   git-svn-id: https...
1785

8a085e035   daniau   git-svn-id: https...
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
  subroutine fvn_d_lspoly(np,x,y,deg,coeff,status)
  !
  !   Least square polynomial fitting
  !
  !   Find the coefficients of the least square polynomial of a given degree
  !   for a set of coordinates.
  !
  !   The degree must be lower than the number of points
  !
  !   np (in)             : number of points
  !   x(np) (in)          : x data
  !   y(np) (in)          : y data
  !   deg (in)            : polynomial's degree
  !   coeff(deg+1) (out)  : polynomial coefficients
  !   status (out)        : =0 if a problem occurs
  !
  implicit none
  
  integer, intent(in) :: np,deg
  real(kind=8), intent(in), dimension(np) :: x,y
  real(kind=8), intent(out), dimension(deg+1) :: coeff
38581db0c   daniau   git-svn-id: https...
1807
  integer, intent(out), optional :: status
8a085e035   daniau   git-svn-id: https...
1808

126a3aed0   daniau   git-svn-id: https...
1809
1810
1811
1812
1813
1814
  real(kind=8), allocatable, dimension(:,:) :: mat,bmat
  real(kind=8),dimension(:),allocatable :: work
  real(kind=8),dimension(1) :: twork
  integer :: lwork,info
  
  integer :: i,j
38581db0c   daniau   git-svn-id: https...
1815
   if (present(status)) status=1
126a3aed0   daniau   git-svn-id: https...
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
  allocate(mat(np,deg+1),bmat(np,1))
  
  ! Design matrix valorisation
  mat=reshape( (/ ((x(i)**(j-1),i=1,np),j=1,deg+1) /),shape=(/ np,deg+1 /) )
  
  ! second member valorisation
  bmat=reshape ( (/  (y(i),i=1,np)   /)  ,shape = (/ np,1 /))
  
  ! query workspace size
  call dgels('N',np,deg+1,1,mat,np,bmat,np,twork,-1,info)
  lwork=twork(1)
  allocate(work(int(lwork)))
  ! real call
  call dgels('N',np,deg+1,1,mat,np,bmat,np,work,lwork,info)
  
  if (info /= 0) then
38581db0c   daniau   git-svn-id: https...
1832
       if (present(status)) status=0
126a3aed0   daniau   git-svn-id: https...
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
  end if
  
   coeff = (/ (bmat(i,1),i=1,deg+1) /)
  
  deallocate(work)
  deallocate(mat,bmat)
  end subroutine
  
  subroutine fvn_s_lspoly(np,x,y,deg,coeff,status)
  !
  !   Least square polynomial fitting
  !
  !   Find the coefficients of the least square polynomial of a given degree
  !   for a set of coordinates.
  !
  !   The degree must be lower than the number of points
  !
  !   np (in)             : number of points
  !   x(np) (in)          : x data
  !   y(np) (in)          : y data
  !   deg (in)            : polynomial's degree
  !   coeff(deg+1) (out)  : polynomial coefficients
  !   status (out)        : =0 if a problem occurs
  !
  implicit none
  
  integer, intent(in) :: np,deg
  real(kind=4), intent(in), dimension(np) :: x,y
  real(kind=4), intent(out), dimension(deg+1) :: coeff
38581db0c   daniau   git-svn-id: https...
1862
  integer, intent(out), optional :: status
126a3aed0   daniau   git-svn-id: https...
1863
1864
1865
1866
1867
1868
1869
  
  real(kind=4), allocatable, dimension(:,:) :: mat,bmat
  real(kind=4),dimension(:),allocatable :: work
  real(kind=4),dimension(1) :: twork
  integer :: lwork,info
  
  integer :: i,j
38581db0c   daniau   git-svn-id: https...
1870
   if (present(status)) status=1
126a3aed0   daniau   git-svn-id: https...
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
  allocate(mat(np,deg+1),bmat(np,1))
  
  ! Design matrix valorisation
  mat=reshape( (/ ((x(i)**(j-1),i=1,np),j=1,deg+1) /),shape=(/ np,deg+1 /) )
  
  ! second member valorisation
  bmat=reshape ( (/  (y(i),i=1,np)   /)  ,shape = (/ np,1 /))
  
  ! query workspace size
  call sgels('N',np,deg+1,1,mat,np,bmat,np,twork,-1,info)
  lwork=twork(1)
  allocate(work(int(lwork)))
  ! real call
  call sgels('N',np,deg+1,1,mat,np,bmat,np,work,lwork,info)
  
  if (info /= 0) then
38581db0c   daniau   git-svn-id: https...
1887
       if (present(status)) status=0
126a3aed0   daniau   git-svn-id: https...
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
  end if
  
   coeff = (/ (bmat(i,1),i=1,deg+1) /)
  
  deallocate(work)
  deallocate(mat,bmat)
  end subroutine
  
  
  
  
  
  
  
  
  subroutine fvn_d_lspoly_svd(np,x,y,deg,coeff,status)
  !
38581db0c   daniau   git-svn-id: https...
1905
  !   Least square polynomial fitting using singular value decomposition
126a3aed0   daniau   git-svn-id: https...
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
  !
  !   Find the coefficients of the least square polynomial of a given degree
  !   for a set of coordinates.
  !
  !   The degree must be lower than the number of points
  !
  !   np (in)             : number of points
  !   x(np) (in)          : x data
  !   y(np) (in)          : y data
  !   deg (in)            : polynomial's degree
  !   coeff(deg+1) (out)  : polynomial coefficients
  !   status (out)        : =0 if a problem occurs
  !
  implicit none
  
  integer, intent(in) :: np,deg
  real(kind=8), intent(in), dimension(np) :: x,y
  real(kind=8), intent(out), dimension(deg+1) :: coeff
38581db0c   daniau   git-svn-id: https...
1924
  integer, intent(out), optional :: status
126a3aed0   daniau   git-svn-id: https...
1925

8a085e035   daniau   git-svn-id: https...
1926
1927
1928
1929
1930
1931
  real(kind=8), allocatable, dimension(:,:) :: mat,bmat
  real(kind=8),dimension(:),allocatable :: work,singval
  real(kind=8),dimension(1) :: twork
  integer :: lwork,info,rank
  
  integer :: i,j
38581db0c   daniau   git-svn-id: https...
1932
   if (present(status)) status=1
8a085e035   daniau   git-svn-id: https...
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
  allocate(mat(np,deg+1),bmat(np,1),singval(deg+1))
  
  ! Design matrix valorisation
  mat=reshape( (/ ((x(i)**(j-1),i=1,np),j=1,deg+1) /),shape=(/ np,deg+1 /) )
  
  ! second member valorisation
  bmat=reshape ( (/  (y(i),i=1,np)   /)  ,shape = (/ np,1 /))
  
  ! query workspace size
  call dgelss(np,deg+1,1,mat,np,bmat,np,singval,-1.,rank,twork,-1,info)
  lwork=twork(1)
  allocate(work(int(lwork)))
  ! real call
  call dgelss(np,deg+1,1,mat,np,bmat,np,singval,-1.,rank,work,lwork,info)
  
  if (info /= 0) then
38581db0c   daniau   git-svn-id: https...
1949
       if (present(status)) status=0
8a085e035   daniau   git-svn-id: https...
1950
1951
1952
1953
1954
1955
1956
  end if
  
   coeff = (/ (bmat(i,1),i=1,deg+1) /)
  
  deallocate(work)
  deallocate(mat,bmat,singval)
  end subroutine
126a3aed0   daniau   git-svn-id: https...
1957
  subroutine fvn_s_lspoly_svd(np,x,y,deg,coeff,status)
8a085e035   daniau   git-svn-id: https...
1958
  !
38581db0c   daniau   git-svn-id: https...
1959
  !   Least square polynomial fitting using singular value decomposition
8a085e035   daniau   git-svn-id: https...
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
  !
  !   Find the coefficients of the least square polynomial of a given degree
  !   for a set of coordinates.
  !
  !   The degree must be lower than the number of points
  !
  !   np (in)             : number of points
  !   x(np) (in)          : x data
  !   y(np) (in)          : y data
  !   deg (in)            : polynomial's degree
  !   coeff(deg+1) (out)  : polynomial coefficients
  !   status (out)        : =0 if a problem occurs
  !
  implicit none
  
  integer, intent(in) :: np,deg
  real(kind=4), intent(in), dimension(np) :: x,y
  real(kind=4), intent(out), dimension(deg+1) :: coeff
38581db0c   daniau   git-svn-id: https...
1978
  integer, intent(out), optional :: status
8a085e035   daniau   git-svn-id: https...
1979
1980
1981
1982
1983
1984
1985
  
  real(kind=4), allocatable, dimension(:,:) :: mat,bmat
  real(kind=4),dimension(:),allocatable :: work,singval
  real(kind=4),dimension(1) :: twork
  integer :: lwork,info,rank
  
  integer :: i,j
38581db0c   daniau   git-svn-id: https...
1986
   if (present(status)) status=1
8a085e035   daniau   git-svn-id: https...
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
  allocate(mat(np,deg+1),bmat(np,1),singval(deg+1))
  
  ! Design matrix valorisation
  mat=reshape( (/ ((x(i)**(j-1),i=1,np),j=1,deg+1) /),shape=(/ np,deg+1 /) )
  
  ! second member valorisation
  bmat=reshape ( (/  (y(i),i=1,np)   /)  ,shape = (/ np,1 /))
  
  ! query workspace size
  call sgelss(np,deg+1,1,mat,np,bmat,np,singval,-1.,rank,twork,-1,info)
  lwork=twork(1)
  allocate(work(int(lwork)))
  ! real call
  call sgelss(np,deg+1,1,mat,np,bmat,np,singval,-1.,rank,work,lwork,info)
  
  if (info /= 0) then
38581db0c   daniau   git-svn-id: https...
2003
       if (present(status)) status=0
8a085e035   daniau   git-svn-id: https...
2004
2005
2006
2007
2008
2009
2010
  end if
  
   coeff = (/ (bmat(i,1),i=1,deg+1) /)
  
  deallocate(work)
  deallocate(mat,bmat,singval)
  end subroutine
d32a47033   daniau   git-svn-id: https...
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
  !
  ! Muller
  !
  !
  !
  ! William Daniau 2007
  !
  ! This routine is a fortran 90 port of Hans D. Mittelmann's routine muller.f
  ! http://plato.asu.edu/ftp/other_software/muller.f
  !
  ! it can be used as a replacement for imsl routine dzanly with minor changes
  !
  !-----------------------------------------------------------------------
  !
  !   purpose             - zeros of an analytic complex function
  !                           using the muller method with deflation
  !
  !   usage               - call fvn_z_muller (f,eps,eps1,kn,n,nguess,x,itmax,
  !                           infer,ier)
  !
  !   arguments    f      - a complex function subprogram, f(z), written
  !                           by the user specifying the equation whose
  !                           roots are to be found.  f must appear in
  !                           an external statement in the calling pro-
  !                           gram.
  !                eps    - 1st stopping criterion.  let fp(z)=f(z)/p
  !                           where p = (z-z(1))*(z-z(2))*,,,*(z-z(k-1))
  !                           and z(1),...,z(k-1) are previously found
  !                           roots.  if ((cdabs(f(z)).le.eps) .and.
  !                           (cdabs(fp(z)).le.eps)), then z is accepted
  !                           as a root. (input)
  !                eps1   - 2nd stopping criterion.  a root is accepted
  !                           if two successive approximations to a given
  !                           root agree within eps1. (input)
  !                             note. if either or both of the stopping
  !                             criteria are fulfilled, the root is
  !                             accepted.
  !                kn     - the number of known roots which must be stored
  !                           in x(1),...,x(kn), prior to entry to muller
  !                nguess - the number of initial guesses provided. these
  !                           guesses must be stored in x(kn+1),...,
  !                           x(kn+nguess).  nguess must be set equal
  !                           to zero if no guesses are provided. (input)
  !                n      - the number of new roots to be found by
  !                           muller (input)
  !                x      - a complex vector of length kn+n.  x(1),...,
  !                           x(kn) on input must contain any known
  !                           roots.  x(kn+1),..., x(kn+n) on input may,
  !                           on user option, contain initial guesses for
  !                           the n new roots which are to be computed.
  !                           if the user does not provide an initial
  !                           guess, zero is used.
  !                           on output, x(kn+1),...,x(kn+n) contain the
  !                           approximate roots found by muller.
  !                itmax  - the maximum allowable number of iterations
  !                           per root (input)
  !                infer  - an integer vector of length kn+n.  on
  !                           output infer(j) contains the number of
  !                           iterations used in finding the j-th root
  !                           when convergence was achieved.  if
  !                           convergence was not obtained in itmax
  !                           iterations, infer(j) will be greater than
  !                           itmax (output).
  !                ier    - error parameter (output)
  !                         warning error
  !                           ier = 33 indicates failure to converge with-
  !                             in itmax iterations for at least one of
  !                             the (n) new roots.
  !
  !
  !   remarks      muller always returns the last approximation for root j
  !                in x(j). if the convergence criterion is satisfied,
  !                then infer(j) is less than or equal to itmax. if the
  !                convergence criterion is not satisified, then infer(j)
  !                is set to either itmax+1 or itmax+k, with k greater
  !                than 1. infer(j) = itmax+1 indicates that muller did
  !                not obtain convergence in the allowed number of iter-
  !                ations. in this case, the user may wish to set itmax
  !                to a larger value. infer(j) = itmax+k means that con-
  !                vergence was obtained (on iteration k) for the defla-
  !                ted function
  !                              fp(z) = f(z)/((z-z(1)...(z-z(j-1)))
  !
  !                but failed for f(z). in this case, better initial
  !                guesses might help or, it might be necessary to relax
  !                the convergence criterion.
  !
  !-----------------------------------------------------------------------
  !
  subroutine fvn_z_muller (f,eps,eps1,kn,nguess,n,x,itmax,infer,ier)
       implicit none
        double precision :: rzero,rten,rhun,rp01,ax,eps1,qz,eps,tpq
        double complex ::   d,dd,den,fprt,frt,h,rt,t1,t2,t3, &
                            tem,z0,z1,z2,bi,xx,xl,y0,y1,y2,x0, &
                            zero,p1,one,four,p5
        
        double complex, external :: f
        integer :: ickmax,kn,nguess,n,itmax,ier,knp1,knpn,i,l,ic, &
                      knpng,jk,ick,nn,lm1,errcode
        double complex :: x(kn+n)
        integer :: infer(kn+n)
        
        
        data                zero/(0.0,0.0)/,p1/(0.1,0.0)/, &
                            one/(1.0,0.0)/,four/(4.0,0.0)/, &
                            p5/(0.5,0.0)/, &
                            rzero/0.0/,rten/10.0/,rhun/100.0/, &
                            ax/0.1/,ickmax/3/,rp01/0.01/
  
              ier = 0
              if (n .lt. 1) then ! What the hell are doing here then ...
                  return
              end if
              !eps1 = rten **(-nsig)
              eps1 = min(eps1,rp01)
              
              knp1 = kn+1
              knpn = kn+n
              knpng = kn+nguess
              do i=1,knpn
                  infer(i) = 0
                  if (i .gt. knpng) x(i) = zero
              end do
              l= knp1
  
              ic=0
  rloop:      do while (l<=knpn)   ! Main loop over new roots
                  jk = 0
                  ick = 0
                  xl = x(l)
  icloop:         do
                      ic = 0
                      h = ax
                      h = p1*h
                      if (cdabs(xl) .gt. ax) h = p1*xl
  !                                  first three points are
  !                                    xl+h,  xl-h,  xl
                      rt = xl+h
                      call deflated_work(errcode)
                      if (errcode == 1) then
                          exit icloop
                      end if
  
                      z0 = fprt
                      y0 = frt
                      x0 = rt
                      rt = xl-h
                      call deflated_work(errcode)
                      if (errcode == 1) then
                          exit icloop
                      end if
  
                      z1 = fprt
                      y1 = frt
                      h = xl-rt
                      d = h/(rt-x0)
                      rt = xl
  
                      call deflated_work(errcode)
                      if (errcode == 1) then
                          exit icloop
                      end if
  
     
                      z2 = fprt
                      y2 = frt
  !                                  begin main algorithm
   iloop:             do
                          dd = one + d
                          t1 = z0*d*d
                          t2 = z1*dd*dd
                          xx = z2*dd
                          t3 = z2*d
                          bi = t1-t2+xx+t3
                          den = bi*bi-four*(xx*t1-t3*(t2-xx))
  !                                  use denominator of maximum amplitude 
                          t1 = cdsqrt(den)
                          qz = rhun*max(cdabs(bi),cdabs(t1))
                          t2 = bi + t1
                          tpq = cdabs(t2)+qz
                          if (tpq .eq. qz) t2 = zero
                          t3 = bi - t1
                          tpq = cdabs(t3) + qz
                          if (tpq .eq. qz) t3 = zero
                          den = t2
                          qz = cdabs(t3)-cdabs(t2)
                          if (qz .gt. rzero) den = t3
  !                                  test for zero denominator            
                          if (cdabs(den) .eq. rzero) then
                              call trans_rt()
                              call deflated_work(errcode)
                              if (errcode == 1) then
                                  exit icloop
                              end if
                              z2 = fprt
                              y2 = frt
                              cycle iloop
                          end if
  
  
                          d = -xx/den
                          d = d+d
                          h = d*h
                          rt = rt + h
  !                                  check convergence of the first kind  
                          if (cdabs(h) .le. eps1*max(cdabs(rt),ax)) then
                              if (ic .ne. 0) then
                                  exit icloop
                              end if
                              ic = 1
                              z0 = y1
                              z1 = y2
                              z2 = f(rt)
                              xl = rt
                              ick = ick+1
                              if (ick .le. ickmax) then
                                  cycle iloop 
                              end if
  !                                  warning error, itmax = maximum
                              jk = itmax + jk
                              ier = 33
                          end if
                          if (ic .ne. 0) then
                              cycle icloop
                          end if
                          call deflated_work(errcode)
                          if (errcode == 1) then
                              exit icloop
                          end if
  
                          do while ( (cdabs(fprt)-cdabs(z2)*rten) .ge. rzero)
                              !   take remedial action to induce
                              !   convergence
                              d = d*p5
                              h = h*p5
                              rt = rt-h
                              call deflated_work(errcode)
                              if (errcode == 1) then
                                  exit icloop
                              end if
                          end do
                          z0 = z1
                          z1 = z2
                          z2 = fprt
                          y0 = y1
                          y1 = y2
                          y2 = frt
                      end do iloop
                  end do icloop
          x(l) = rt
          infer(l) = jk
          l = l+1
        end do rloop
        
        contains
          subroutine trans_rt()
             tem = rten*eps1
             if (cdabs(rt) .gt. ax) tem = tem*rt
             rt = rt+tem
             d = (h+tem)*d/h
             h = h+tem
          end subroutine trans_rt
          
          subroutine deflated_work(errcode)
              ! errcode=0 => no errors
              ! errcode=1 => jk>itmax or convergence of second kind achieved
              integer :: errcode,flag
              
              flag=1
      loop1:  do while(flag==1)
                  errcode=0
                  jk = jk+1
                  if (jk .gt. itmax) then
                      ier=33
                      errcode=1
                      return
                  end if
                  frt = f(rt)
                  fprt = frt
                  if (l /= 1) then
                      lm1 = l-1
                      do i=1,lm1
                          tem = rt - x(i)
                          if (cdabs(tem) .eq. rzero) then
                          !if (ic .ne. 0) go to 15 !! ?? possible?
                              call trans_rt()
                              cycle loop1
                          end if
                          fprt = fprt/tem
                      end do
                  end if
                  flag=0
              end do loop1
   
              if (cdabs(fprt) .le. eps .and. cdabs(frt) .le. eps) then
                  errcode=1
                  return
              end if
              
          end subroutine deflated_work
        
        end subroutine
  
  
  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  !
  !   Integration
  !
  !   Only double precision coded atm
  !
  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  
  
  subroutine fvn_d_gauss_legendre(n,qx,qw)
  !
  ! This routine compute the n Gauss Legendre abscissas and weights
  ! Adapted from Numerical Recipes routine gauleg
  !
  ! n (in) : number of points
  ! qx(out) : abscissas
  ! qw(out) : weights
  !
  implicit none
  double precision,parameter :: pi=3.141592653589793d0
  integer, intent(in) :: n
  double precision, intent(out) :: qx(n),qw(n)
  
  integer :: m,i,j
  double precision :: z,z1,p1,p2,p3,pp
  m=(n+1)/2
  do i=1,m
      z=cos(pi*(dble(i)-0.25d0)/(dble(n)+0.5d0))
  iloop:  do 
              p1=1.d0
              p2=0.d0
              do j=1,n
                  p3=p2
                  p2=p1
                  p1=((2.d0*dble(j)-1.d0)*z*p2-(dble(j)-1.d0)*p3)/dble(j)
              end do
              pp=dble(n)*(z*p1-p2)/(z*z-1.d0)
              z1=z
              z=z1-p1/pp
              if (dabs(z-z1)<=epsilon(z)) then
                  exit iloop
              end if
          end do iloop
      qx(i)=-z
      qx(n+1-i)=z
      qw(i)=2.d0/((1.d0-z*z)*pp*pp)
      qw(n+1-i)=qw(i)
  end do
  end subroutine
  
  
  
  subroutine fvn_d_gl_integ(f,a,b,n,res)
  !
  ! This is a simple non adaptative integration routine 
  ! using n gauss legendre abscissas and weights
  !
  !   f(in)   : the function to integrate
  !   a(in)   : lower bound
  !   b(in)   : higher bound
  !   n(in)   : number of gauss legendre pairs
  !   res(out): the evaluation of the integral
  !
  double precision,external :: f
  double precision, intent(in) :: a,b
  integer, intent(in):: n
  double precision, intent(out) :: res
  
  double precision, allocatable :: qx(:),qw(:)
  double precision :: xm,xr
  integer :: i
  
  ! First compute n gauss legendre abs and weight
  allocate(qx(n))
  allocate(qw(n))
  call fvn_d_gauss_legendre(n,qx,qw)
  
  xm=0.5d0*(b+a)
  xr=0.5d0*(b-a)
  
  res=0.d0
  
  do i=1,n
      res=res+qw(i)*f(xm+xr*qx(i))
  end do
  
  res=xr*res
  
  deallocate(qw)
  deallocate(qx)
  
  end subroutine
  
  !!!!!!!!!!!!!!!!!!!!!!!!
  !
  ! Simple and double adaptative Gauss Kronrod integration based on
  ! a modified version of quadpack ( http://www.netlib.org/quadpack
  !
  ! Common parameters :
  !
  !       key (in)
  !       epsabs
  !       epsrel
  !
  !
  !!!!!!!!!!!!!!!!!!!!!!!!
  
  subroutine fvn_d_integ_1_gk(f,a,b,epsabs,epsrel,key,res,abserr,ier,limit)
  !
  ! Evaluate the integral of function f(x) between a and b
  !
  ! f(in) : the function
  ! a(in) : lower bound
  ! b(in) : higher bound
  ! epsabs(in) : desired absolute error
  ! epsrel(in) : desired relative error
  ! key(in) : gauss kronrod rule
  !                     1:   7 - 15 points
  !                     2:  10 - 21 points
  !                     3:  15 - 31 points
  !                     4:  20 - 41 points
  !                     5:  25 - 51 points
  !                     6:  30 - 61 points
  !
  ! limit(in) : maximum number of subintervals in the partition of the 
  !               given integration interval (a,b). A value of 500 will give the same
  !               behaviour as the imsl routine dqdag
  !
  ! res(out) : estimated integral value
  ! abserr(out) : estimated absolute error
  ! ier(out) : error flag from quadpack routines
  !               0 : no error
  !               1 : maximum number of subdivisions allowed
  !                   has been achieved. one can allow more
  !                   subdivisions by increasing the value of
  !                   limit (and taking the according dimension
  !                   adjustments into account). however, if
  !                   this yield no improvement it is advised
  !                   to analyze the integrand in order to
  !                   determine the integration difficulaties.
  !                   if the position of a local difficulty can
  !                   be determined (i.e.singularity,
  !                   discontinuity within the interval) one
  !                   will probably gain from splitting up the
  !                   interval at this point and calling the
  !                   integrator on the subranges. if possible,
  !                   an appropriate special-purpose integrator
  !                   should be used which is designed for
  !                   handling the type of difficulty involved.
  !               2 : the occurrence of roundoff error is
  !                   detected, which prevents the requested
  !                   tolerance from being achieved.
  !               3 : extremely bad integrand behaviour occurs
  !                   at some points of the integration
  !                   interval.
  !               6 : the input is invalid, because
  !                   (epsabs.le.0 and
  !                   epsrel.lt.max(50*rel.mach.acc.,0.5d-28))
  !                   or limit.lt.1 or lenw.lt.limit*4.
  !                   result, abserr, neval, last are set
  !                   to zero.
  !                   except when lenw is invalid, iwork(1),
  !                   work(limit*2+1) and work(limit*3+1) are
  !                   set to zero, work(1) is set to a and
  !                   work(limit+1) to b.
  
  implicit none
  double precision, external :: f
  double precision, intent(in) :: a,b,epsabs,epsrel
  integer, intent(in) :: key
  integer, intent(in) :: limit
  double precision, intent(out) :: res,abserr
  integer, intent(out) :: ier
  
  double precision, allocatable :: work(:)
  integer, allocatable :: iwork(:)
  integer :: lenw,neval,last
  
  ! imsl value for limit is 500
  lenw=limit*4
  
  allocate(iwork(limit))
  allocate(work(lenw))
  
  call dqag(f,a,b,epsabs,epsrel,key,res,abserr,neval,ier,limit,lenw,last,iwork,work)
  
  deallocate(work)
  deallocate(iwork)
  
  end subroutine
  
  
  
  subroutine fvn_d_integ_2_gk(f,a,b,g,h,epsabs,epsrel,key,res,abserr,ier,limit)
  !
  ! Evaluate the double integral of function f(x,y) for x between a and b and y between g(x) and h(x)
  !
  ! f(in) : the function
  ! a(in) : lower bound
  ! b(in) : higher bound
  ! g(in) : external function describing lower bound for y
  ! h(in) : external function describing higher bound for y
  ! epsabs(in) : desired absolute error
  ! epsrel(in) : desired relative error
  ! key(in) : gauss kronrod rule
  !                     1:   7 - 15 points
  !                     2:  10 - 21 points
  !                     3:  15 - 31 points
  !                     4:  20 - 41 points
  !                     5:  25 - 51 points
  !                     6:  30 - 61 points
  !
  ! limit(in) : maximum number of subintervals in the partition of the 
  !               given integration interval (a,b). A value of 500 will give the same
  !               behaviour as the imsl routine dqdag
  !
  ! res(out) : estimated integral value
  ! abserr(out) : estimated absolute error
  ! ier(out) : error flag from quadpack routines
  !               0 : no error
  !               1 : maximum number of subdivisions allowed
  !                   has been achieved. one can allow more
  !                   subdivisions by increasing the value of
  !                   limit (and taking the according dimension
  !                   adjustments into account). however, if
  !                   this yield no improvement it is advised
  !                   to analyze the integrand in order to
  !                   determine the integration difficulaties.
  !                   if the position of a local difficulty can
  !                   be determined (i.e.singularity,
  !                   discontinuity within the interval) one
  !                   will probably gain from splitting up the
  !                   interval at this point and calling the
  !                   integrator on the subranges. if possible,
  !                   an appropriate special-purpose integrator
  !                   should be used which is designed for
  !                   handling the type of difficulty involved.
  !               2 : the occurrence of roundoff error is
  !                   detected, which prevents the requested
  !                   tolerance from being achieved.
  !               3 : extremely bad integrand behaviour occurs
  !                   at some points of the integration
  !                   interval.
  !               6 : the input is invalid, because
  !                   (epsabs.le.0 and
  !                   epsrel.lt.max(50*rel.mach.acc.,0.5d-28))
  !                   or limit.lt.1 or lenw.lt.limit*4.
  !                   result, abserr, neval, last are set
  !                   to zero.
  !                   except when lenw is invalid, iwork(1),
  !                   work(limit*2+1) and work(limit*3+1) are
  !                   set to zero, work(1) is set to a and
  !                   work(limit+1) to b.
  
  implicit none
  double precision, external:: f,g,h
  double precision, intent(in) :: a,b,epsabs,epsrel
  integer, intent(in) :: key,limit
  integer, intent(out) :: ier
  double precision, intent(out) :: res,abserr
  
  
  double precision, allocatable :: work(:)
  integer, allocatable :: iwork(:)
  integer :: lenw,neval,last
  
  ! imsl value for limit is 500
  lenw=limit*4
  allocate(work(lenw))
  allocate(iwork(limit))
  
  call dqag_2d_outer(f,a,b,g,h,epsabs,epsrel,key,res,abserr,neval,ier,limit,lenw,last,iwork,work)
  
  deallocate(iwork)
  deallocate(work)
  end subroutine
  
  
  
  subroutine fvn_d_integ_2_inner_gk(f,x,a,b,epsabs,epsrel,key,res,abserr,ier,limit)
  !
  ! Evaluate the single integral of function f(x,y) for y between a and b with a
  ! given x value
  !
  ! This function is used for the evaluation of the double integral fvn_d_integ_2_gk
  !
  ! f(in) : the function
  ! x(in) : x
  ! a(in) : lower bound
  ! b(in) : higher bound
  ! epsabs(in) : desired absolute error
  ! epsrel(in) : desired relative error
  ! key(in) : gauss kronrod rule
  !                     1:   7 - 15 points
  !                     2:  10 - 21 points
  !                     3:  15 - 31 points
  !                     4:  20 - 41 points
  !                     5:  25 - 51 points
  !                     6:  30 - 61 points
  !
  ! limit(in) : maximum number of subintervals in the partition of the 
  !               given integration interval (a,b). A value of 500 will give the same
  !               behaviour as the imsl routine dqdag
  !
  ! res(out) : estimated integral value
  ! abserr(out) : estimated absolute error
  ! ier(out) : error flag from quadpack routines
  !               0 : no error
  !               1 : maximum number of subdivisions allowed
  !                   has been achieved. one can allow more
  !                   subdivisions by increasing the value of
  !                   limit (and taking the according dimension
  !                   adjustments into account). however, if
  !                   this yield no improvement it is advised
  !                   to analyze the integrand in order to
  !                   determine the integration difficulaties.
  !                   if the position of a local difficulty can
  !                   be determined (i.e.singularity,
  !                   discontinuity within the interval) one
  !                   will probably gain from splitting up the
  !                   interval at this point and calling the
  !                   integrator on the subranges. if possible,
  !                   an appropriate special-purpose integrator
  !                   should be used which is designed for
  !                   handling the type of difficulty involved.
  !               2 : the occurrence of roundoff error is
  !                   detected, which prevents the requested
  !                   tolerance from being achieved.
  !               3 : extremely bad integrand behaviour occurs
  !                   at some points of the integration
  !                   interval.
  !               6 : the input is invalid, because
  !                   (epsabs.le.0 and
  !                   epsrel.lt.max(50*rel.mach.acc.,0.5d-28))
  !                   or limit.lt.1 or lenw.lt.limit*4.
  !                   result, abserr, neval, last are set
  !                   to zero.
  !                   except when lenw is invalid, iwork(1),
  !                   work(limit*2+1) and work(limit*3+1) are
  !                   set to zero, work(1) is set to a and
  !                   work(limit+1) to b.
  
  implicit none
  double precision, external:: f
  double precision, intent(in) :: x,a,b,epsabs,epsrel
  integer, intent(in) :: key,limit
  integer, intent(out) :: ier
  double precision, intent(out) :: res,abserr
  
  
  double precision, allocatable :: work(:)
  integer, allocatable :: iwork(:)
  integer :: lenw,neval,last
  
  ! imsl value for limit is 500
  lenw=limit*4
  allocate(work(lenw))
  allocate(iwork(limit))
  
  call dqag_2d_inner(f,x,a,b,epsabs,epsrel,key,res,abserr,neval,ier,limit,lenw,last,iwork,work)
  
  deallocate(iwork)
  deallocate(work)
  end subroutine
  
  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  ! Include the modified quadpack files
  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  include "fvn_quadpack/dqag_2d_inner.f"
  include "fvn_quadpack/dqk15_2d_inner.f"
  include "fvn_quadpack/dqk31_2d_outer.f"
  include "fvn_quadpack/d1mach.f"
  include "fvn_quadpack/dqk31_2d_inner.f"
  include "fvn_quadpack/dqage.f"
  include "fvn_quadpack/dqk15.f"
  include "fvn_quadpack/dqk21.f"
  include "fvn_quadpack/dqk31.f"
  include "fvn_quadpack/dqk41.f"
  include "fvn_quadpack/dqk51.f"
  include "fvn_quadpack/dqk61.f"
  include "fvn_quadpack/dqk41_2d_outer.f"
  include "fvn_quadpack/dqk41_2d_inner.f"
  include "fvn_quadpack/dqag_2d_outer.f"
  include "fvn_quadpack/dqpsrt.f"
  include "fvn_quadpack/dqag.f"
  include "fvn_quadpack/dqage_2d_outer.f"
  include "fvn_quadpack/dqage_2d_inner.f"
  include "fvn_quadpack/dqk51_2d_outer.f"
  include "fvn_quadpack/dqk51_2d_inner.f"
  include "fvn_quadpack/dqk61_2d_outer.f"
  include "fvn_quadpack/dqk21_2d_outer.f"
  include "fvn_quadpack/dqk61_2d_inner.f"
  include "fvn_quadpack/dqk21_2d_inner.f"
  include "fvn_quadpack/dqk15_2d_outer.f"
f61865f46   daniau   git-svn-id: https...
2709
2710
2711
2712
2713
2714
2715
2716
2717
  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  !
  ! Trigonometric functions
  !
  ! fvn_z_acos, fvn_z_asin : complex arc cosine and sine
  ! fvn_d_acosh : arc cosinus hyperbolic
  ! fvn_d_asinh : arc sinus hyperbolic
  !
  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
38581db0c   daniau   git-svn-id: https...
2718
2719
  ! February 2008 
  ! All Trigonometric functions removed due to implementation of fnlib
f61865f46   daniau   git-svn-id: https...
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
  function fvn_z_acos(z)
      ! double complex arccos function adapted from 
      ! the c gsl library
      ! http://www.gnu.org/software/gsl/
      implicit none
      complex(kind=8) :: fvn_z_acos
      complex(kind=8) :: z
      real(kind=8) :: rz,iz,x,y,a,b,y2,r,s,d,apx,am1
      real(kind=8),parameter :: a_crossover=1.5_8,b_crossover = 0.6417_8
      complex(kind=8),parameter :: i=(0._8,1._8)
      real(kind=8) :: r_res,i_res
8a085e035   daniau   git-svn-id: https...
2731
2732
      rz=dreal(z)
      iz=dimag(z)
f61865f46   daniau   git-svn-id: https...
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
      if ( iz == 0._8 ) then
          fvn_z_acos=fvn_z_acos_real(rz)
          return
      end if
  
      x=dabs(rz)
      y=dabs(iz)
      r=fvn_d_hypot(x+1.,y)
      s=fvn_d_hypot(x-1.,y)
      a=0.5*(r + s)
      b=x/a
      y2=y*y
  
      if (b <= b_crossover) then
          r_res=dacos(b)
      else
          if (x <= 1.) then
              d=0.5*(a+x)*(y2/(r+x+1)+(s + (1 - x)))
              r_res=datan(dsqrt(d)/x)
          else
              apx=a+x
              d=0.5*(apx/(r+x+1)+apx/(s + (x - 1)))
              r_res=datan((y*dsqrt(d))/x);
          end if
      end if
  
      if (a <= a_crossover) then
          if (x < 1.) then
              am1=0.5*(y2 / (r + (x + 1)) + y2 / (s + (1 - x)))
          else
              am1=0.5*(y2 / (r + (x + 1)) + (s + (x - 1)))
          end if
          i_res = dlog(1.+(am1 + sqrt (am1 * (a + 1))));
      else
          i_res = dlog (a + dsqrt (a*a - 1.));
      end if
      if (rz <0.) then
          r_res=fvn_pi-r_res
      end if
      i_res=-sign(1._8,iz)*i_res
      fvn_z_acos=dcmplx(r_res)+fvn_i*dcmplx(i_res)
  
  end function fvn_z_acos
  
  function fvn_z_acos_real(r)
      ! return the double complex arc cosinus for a 
      ! double precision argument
      implicit none
      real(kind=8) :: r
      complex(kind=8) :: fvn_z_acos_real
  
      if (dabs(r)<=1._8) then
          fvn_z_acos_real=dcmplx(dacos(r))
          return
      end if
      if (r < 0._8) then
          fvn_z_acos_real=dcmplx(fvn_pi)-fvn_i*dcmplx(fvn_d_acosh(-r))
      else
          fvn_z_acos_real=fvn_i*dcmplx(fvn_d_acosh(r))
      end if
  end function
  
  
  function fvn_z_asin(z)
      ! double complex arcsin function derived from 
      ! the c gsl library
      ! http://www.gnu.org/software/gsl/
      implicit none
      complex(kind=8) :: fvn_z_asin
      complex(kind=8) :: z
      real(kind=8) :: rz,iz,x,y,a,b,y2,r,s,d,apx,am1
      real(kind=8),parameter :: a_crossover=1.5_8,b_crossover = 0.6417_8
      real(kind=8) :: r_res,i_res
8a085e035   daniau   git-svn-id: https...
2806
2807
      rz=dreal(z)
      iz=dimag(z)
f61865f46   daniau   git-svn-id: https...
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
      if ( iz == 0._8 ) then
          ! z is real
          fvn_z_asin=fvn_z_asin_real(rz)
          return
      end if
      
      x=dabs(rz)
      y=dabs(iz)
      r=fvn_d_hypot(x+1.,y)
      s=fvn_d_hypot(x-1.,y)
      a=0.5*(r + s)
      b=x/a
      y2=y*y
  
      if (b <= b_crossover) then
          r_res=dasin(b)
      else
          if (x <= 1.) then
              d=0.5*(a+x)*(y2/(r+x+1)+(s + (1 - x)))
              r_res=datan(x/dsqrt(d))
          else
              apx=a+x
              d=0.5*(apx/(r+x+1)+apx/(s + (x - 1)))
              r_res=datan(x/(y*dsqrt(d)));
          end if
      end if
  
      if (a <= a_crossover) then
          if (x < 1.) then
              am1=0.5*(y2 / (r + (x + 1)) + y2 / (s + (1 - x)))
          else
              am1=0.5*(y2 / (r + (x + 1)) + (s + (x - 1)))
          end if
          i_res = dlog(1.+(am1 + sqrt (am1 * (a + 1))));
      else
          i_res = dlog (a + dsqrt (a*a - 1.));
      end if
      r_res=sign(1._8,rz)*r_res
      i_res=sign(1._8,iz)*i_res
      fvn_z_asin=dcmplx(r_res)+fvn_i*dcmplx(i_res)
  
  end function fvn_z_asin
  
  function fvn_z_asin_real(r)
      ! return the double complex arc sinus for a 
      ! double precision argument
      implicit none
      real(kind=8) :: r
      complex(kind=8) :: fvn_z_asin_real
  
      if (dabs(r)<=1._8) then
          fvn_z_asin_real=dcmplx(dasin(r))
          return
      end if
      if (r < 0._8) then
          fvn_z_asin_real=dcmplx(-fvn_pi/2._8)+fvn_i*dcmplx(fvn_d_acosh(-r))
      else
          fvn_z_asin_real=dcmplx(fvn_pi/2._8)-fvn_i*dcmplx(fvn_d_acosh(r))
      end if
  end function fvn_z_asin_real
  
  function fvn_d_acosh(r)
      ! return the arc hyperbolic cosine
      implicit none
      real(kind=8) :: r
      real(kind=8) :: fvn_d_acosh
      if (r >=1) then
          fvn_d_acosh=dlog(r+dsqrt(r*r-1))
      else
          !! TODO : Better error handling!!!!!!
          stop "Argument to fvn_d_acosh lesser than 1"
      end if
  end function fvn_d_acosh
  
  function fvn_d_asinh(r)
      ! return the arc hyperbolic sine
      implicit none
      real(kind=8) :: r
      real(kind=8) :: fvn_d_asinh
      fvn_d_asinh=dlog(r+dsqrt(r*r+1))
  end function fvn_d_asinh
  
  function fvn_d_hypot(a,b)
      implicit none
      ! return the euclidian norm of vector(a,b)
      real(kind=8) :: a,b
      real(kind=8) :: fvn_d_hypot
      fvn_d_hypot=dsqrt(a*a+b*b)
  end function
d32a47033   daniau   git-svn-id: https...
2897

422234dc3   daniau   git-svn-id: https...
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  !
  ! SPARSE RESOLUTION
  !
  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  ! 
  ! Sparse resolution is done by interfaçing Tim Davi's UMFPACK
  ! http://www.cise.ufl.edu/research/sparse/SuiteSparse/
  ! Used packages from SuiteSparse : AMD,UMFPACK,UFconfig
  !
  ! Solve Ax=B using UMFPACK
  !
  ! Where A is a sparse matrix given in its triplet form 
  ! T -> non zero elements
  ! Ti,Tj -> row and column index (1-based) of the given elt
  ! n : rank of matrix A
  ! nz : number of non zero elts
  !
  ! fvn_*_sparse_solve
  ! * = zl : double complex + integer(8)
  ! * = zi : double complex + integer(4)
  !
  subroutine fvn_zl_sparse_solve(n,nz,T,Ti,Tj,B,x,status)
  implicit none
  integer(8), intent(in) :: n,nz
  complex(8),dimension(nz),intent(in) :: T
  integer(8),dimension(nz),intent(in)  :: Ti,Tj
  complex(8),dimension(n),intent(in) :: B
  complex(8),dimension(n),intent(out) :: x
  integer(8), intent(out) :: status
  
  integer(8),dimension(:),allocatable :: wTi,wTj
  real(8),dimension(:),allocatable :: Tx,Tz
  real(8),dimension(:),allocatable :: Ax,Az
  integer(8),dimension(:),allocatable :: Ap,Ai
  integer(8) :: symbolic,numeric
  real(8),dimension(:),allocatable :: xx,xz,bx,bz
  real(8),dimension(90) :: info
  real(8),dimension(20) :: control
  integer(8) :: sys
  
  
  status=0
  
  ! we use a working copy of Ti and Tj to perform 1-based to 0-based translation
  ! Tx and Tz are the real and imaginary parts of T
  allocate(wTi(nz),wTj(nz))
  allocate(Tx(nz),Tz(nz))
  Tx=dble(T)
  Tz=aimag(T)
  wTi=Ti-1
  wTj=Tj-1
  allocate(Ax(nz),Az(nz))
  allocate(Ap(n+1),Ai(nz))
  
  ! perform the triplet to compressed column form -> Ap,Ai,Ax,Az
  call umfpack_zl_triplet_to_col(n,n,nz,wTi,wTj,Tx,Tz,Ap,Ai,Ax,Az,status)
  ! if status is not zero a problem has occured
  if (status /= 0) then
      write(*,*) "Problem during umfpack_zl_triplet_to_col"
  endif
  
  ! Define defaults control values
  call umfpack_zl_defaults(control)
  
  ! Symbolic analysis
  call umfpack_zl_symbolic(n,n,Ap,Ai,Ax,Az,symbolic, control, info)
  ! info(1) should be zero
  if (info(1) /= 0) then
      write(*,*) "Problem during symbolic analysis"
      status=info(1)
  endif
  
  ! Numerical factorization
  call umfpack_zl_numeric (Ap, Ai, Ax, Az, symbolic, numeric, control, info)
  ! info(1) should be zero
  if (info(1) /= 0) then
      write(*,*) "Problem during numerical factorization"
      status=info(1)
  endif
  
  ! free the C symbolic pointer
  call umfpack_zl_free_symbolic (symbolic)
  
  allocate(bx(n),bz(n),xx(n),xz(n))
  bx=dble(B)
  bz=aimag(B)
  sys=0
  ! sys may be used to define type of solving -> see umfpack.h
  
  ! Solving
  call umfpack_zl_solve (sys, Ap, Ai, Ax,Az, xx,xz, bx,bz, numeric, control, info)
  ! info(1) should be zero
  if (info(1) /= 0) then
      write(*,*) "Problem during solving"
      status=info(1)
  endif
  
  
  ! free the C numeric pointer
  call umfpack_zl_free_numeric (numeric)
  
  x=dcmplx(xx,xz)
  
  deallocate(bx,bz,xx,xz)
  deallocate(Ax,Az)
  deallocate(Tx,Tz)
  deallocate(wTi,wTj)
  end subroutine
d32a47033   daniau   git-svn-id: https...
3007

422234dc3   daniau   git-svn-id: https...
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
  subroutine fvn_zi_sparse_solve(n,nz,T,Ti,Tj,B,x,status)
  implicit none
  integer(4), intent(in) :: n,nz
  complex(8),dimension(nz),intent(in) :: T
  integer(4),dimension(nz),intent(in)  :: Ti,Tj
  complex(8),dimension(n),intent(in) :: B
  complex(8),dimension(n),intent(out) :: x
  integer(4), intent(out) :: status
  
  integer(4),dimension(:),allocatable :: wTi,wTj
  real(8),dimension(:),allocatable :: Tx,Tz
  real(8),dimension(:),allocatable :: Ax,Az
  integer(4),dimension(:),allocatable :: Ap,Ai
  !integer(8) :: symbolic,numeric
  integer(4),dimension(2) :: symbolic,numeric
  ! As symbolic and numeric are used to store a C pointer, it is necessary to
  ! still use an integer(8) for 64bits machines
  ! An other possibility : integer(4),dimension(2) :: symbolic,numeric
  real(8),dimension(:),allocatable :: xx,xz,bx,bz
  real(8),dimension(90) :: info
  real(8),dimension(20) :: control
  integer(4) :: sys
  
  status=0
  ! we use a working copy of Ti and Tj to perform 1-based to 0-based translation
  ! Tx and Tz are the real and imaginary parts of T
  allocate(wTi(nz),wTj(nz))
  allocate(Tx(nz),Tz(nz))
  Tx=dble(T)
  Tz=aimag(T)
  wTi=Ti-1
  wTj=Tj-1
  allocate(Ax(nz),Az(nz))
  allocate(Ap(n+1),Ai(nz))
  
  ! perform the triplet to compressed column form -> Ap,Ai,Ax,Az
  call umfpack_zi_triplet_to_col(n,n,nz,wTi,wTj,Tx,Tz,Ap,Ai,Ax,Az,status)
  ! if status is not zero a problem has occured
  if (status /= 0) then
      write(*,*) "Problem during umfpack_zl_triplet_to_col"
  endif
  
  ! Define defaults control values
  call umfpack_zi_defaults(control)
  
  ! Symbolic analysis
  call umfpack_zi_symbolic(n,n,Ap,Ai,Ax,Az,symbolic, control, info)
  ! info(1) should be zero
  if (info(1) /= 0) then
      write(*,*) "Problem during symbolic analysis"
      status=info(1)
  endif
  
  ! Numerical factorization
  call umfpack_zi_numeric (Ap, Ai, Ax, Az, symbolic, numeric, control, info)
  ! info(1) should be zero
  if (info(1) /= 0) then
      write(*,*) "Problem during numerical factorization"
      status=info(1)
  endif
  
  ! free the C symbolic pointer
  call umfpack_zi_free_symbolic (symbolic)
  
  allocate(bx(n),bz(n),xx(n),xz(n))
  bx=dble(B)
  bz=aimag(B)
  sys=0
  ! sys may be used to define type of solving -> see umfpack.h
  
  ! Solving
  call umfpack_zi_solve (sys, Ap, Ai, Ax,Az, xx,xz, bx,bz, numeric, control, info)
  ! info(1) should be zero
  if (info(1) /= 0) then
      write(*,*) "Problem during solving"
      status=info(1)
  endif
  
  ! free the C numeric pointer
  call umfpack_zi_free_numeric (numeric)
  
  x=dcmplx(xx,xz)
  
  deallocate(bx,bz,xx,xz)
  deallocate(Ax,Az)
  deallocate(Tx,Tz)
  deallocate(wTi,wTj)
  end subroutine
d32a47033   daniau   git-svn-id: https...
3096

f85d3b317   daniau   git-svn-id: https...
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
  
  
  
  
  subroutine fvn_dl_sparse_solve(n,nz,T,Ti,Tj,B,x,status)
  implicit none
  integer(8), intent(in) :: n,nz
  real(8),dimension(nz),intent(in) :: T
  integer(8),dimension(nz),intent(in)  :: Ti,Tj
  real(8),dimension(n),intent(in) :: B
  real(8),dimension(n),intent(out) :: x
  integer(8), intent(out) :: status
  
  integer(8),dimension(:),allocatable :: wTi,wTj
  real(8),dimension(:),allocatable :: A
  integer(8),dimension(:),allocatable :: Ap,Ai
  !integer(8) :: symbolic,numeric
  integer(8) :: symbolic,numeric
  real(8),dimension(90) :: info
  real(8),dimension(20) :: control
  integer(8) :: sys
  
  status=0
  ! we use a working copy of Ti and Tj to perform 1-based to 0-based translation
  allocate(wTi(nz),wTj(nz))
  wTi=Ti-1
  wTj=Tj-1
  allocate(A(nz))
  allocate(Ap(n+1),Ai(nz))
  
  ! perform the triplet to compressed column form -> Ap,Ai,Ax,Az
  call umfpack_dl_triplet_to_col(n,n,nz,wTi,wTj,T,Ap,Ai,A,status)
  ! if status is not zero a problem has occured
  if (status /= 0) then
      write(*,*) "Problem during umfpack_dl_triplet_to_col"
  endif
  
  ! Define defaults control values
  call umfpack_dl_defaults(control)
  
  ! Symbolic analysis
  call umfpack_dl_symbolic(n,n,Ap,Ai,A,symbolic, control, info)
  ! info(1) should be zero
  if (info(1) /= 0) then
      write(*,*) "Problem during symbolic analysis"
      status=info(1)
  endif
  
  ! Numerical factorization
  call umfpack_dl_numeric (Ap, Ai, A, symbolic, numeric, control, info)
  ! info(1) should be zero
  if (info(1) /= 0) then
      write(*,*) "Problem during numerical factorization"
      status=info(1)
  endif
  
  ! free the C symbolic pointer
  call umfpack_dl_free_symbolic (symbolic)
  
  sys=0
  ! sys may be used to define type of solving -> see umfpack.h
  
  ! Solving
  call umfpack_dl_solve (sys, Ap, Ai, A, x, B, numeric, control, info)
  ! info(1) should be zero
  if (info(1) /= 0) then
      write(*,*) "Problem during solving"
      status=info(1)
  endif
  
  ! free the C numeric pointer
  call umfpack_dl_free_numeric (numeric)
  
  deallocate(A)
  deallocate(wTi,wTj)
  end subroutine
  
  
  
  
  
  
  subroutine fvn_di_sparse_solve(n,nz,T,Ti,Tj,B,x,status)
  implicit none
  integer(4), intent(in) :: n,nz
  real(8),dimension(nz),intent(in) :: T
  integer(4),dimension(nz),intent(in)  :: Ti,Tj
  real(8),dimension(n),intent(in) :: B
  real(8),dimension(n),intent(out) :: x
  integer(4), intent(out) :: status
  
  integer(4),dimension(:),allocatable :: wTi,wTj
  real(8),dimension(:),allocatable :: A
  integer(4),dimension(:),allocatable :: Ap,Ai
  !integer(8) :: symbolic,numeric
  integer(4),dimension(2) :: symbolic,numeric
  ! As symbolic and numeric are used to store a C pointer, it is necessary to
  ! still use an integer(8) for 64bits machines
  ! An other possibility : integer(4),dimension(2) :: symbolic,numeric
  real(8),dimension(90) :: info
  real(8),dimension(20) :: control
  integer(4) :: sys
  
  status=0
  ! we use a working copy of Ti and Tj to perform 1-based to 0-based translation
  allocate(wTi(nz),wTj(nz))
  wTi=Ti-1
  wTj=Tj-1
  allocate(A(nz))
  allocate(Ap(n+1),Ai(nz))
  
  ! perform the triplet to compressed column form -> Ap,Ai,Ax,Az
  call umfpack_di_triplet_to_col(n,n,nz,wTi,wTj,T,Ap,Ai,A,status)
  ! if status is not zero a problem has occured
  if (status /= 0) then
      write(*,*) "Problem during umfpack_di_triplet_to_col"
  endif
  
  ! Define defaults control values
  call umfpack_di_defaults(control)
  
  ! Symbolic analysis
  call umfpack_di_symbolic(n,n,Ap,Ai,A,symbolic, control, info)
  ! info(1) should be zero
  if (info(1) /= 0) then
      write(*,*) "Problem during symbolic analysis"
      status=info(1)
  endif
  
  ! Numerical factorization
  call umfpack_di_numeric (Ap, Ai, A, symbolic, numeric, control, info)
  ! info(1) should be zero
  if (info(1) /= 0) then
      write(*,*) "Problem during numerical factorization"
      status=info(1)
  endif
  
  ! free the C symbolic pointer
  call umfpack_di_free_symbolic (symbolic)
  
  sys=0
  ! sys may be used to define type of solving -> see umfpack.h
  
  ! Solving
  call umfpack_di_solve (sys, Ap, Ai, A, x, B, numeric, control, info)
  ! info(1) should be zero
  if (info(1) /= 0) then
      write(*,*) "Problem during solving"
      status=info(1)
  endif
  
  ! free the C numeric pointer
  call umfpack_di_free_numeric (numeric)
  
  deallocate(A)
  deallocate(wTi,wTj)
  end subroutine
42591138e   daniau   git-svn-id: https...
3254
3255
3256
3257
3258
  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  !
  ! Special Functions
  !
  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
38581db0c   daniau   git-svn-id: https...
3259
3260
3261
  ! February 2008 
  ! All Special functions removed due to implementation of fnlib
  !
42591138e   daniau   git-svn-id: https...
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
  function fvn_d_lngamma(x)
        ! This function returns ln(gamma(x))
        ! adapted from Numerical Recipes
        implicit none
        real(kind=8) :: x
        real(kind=8) :: fvn_d_lngamma
  
        real(kind=8) :: ser,stp,tmp,y,cof(6)
        integer(kind=4) :: i
  
        cof = (/ 76.18009172947146d0,-86.50532032941677d0, &
                    24.01409824083091d0,-1.231739572450155d0, &
                    .1208650973866179d-2,-.5395239384953d-5 /)
        stp = 2.5066282746310005d0
  
        tmp=x+5.5d0
        tmp=(x+0.5d0)*log(tmp)-tmp
f85d3b317   daniau   git-svn-id: https...
3279

42591138e   daniau   git-svn-id: https...
3280
        ser=1.000000000190015d0
f85d3b317   daniau   git-svn-id: https...
3281

42591138e   daniau   git-svn-id: https...
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
        y=x
  
        do i=1,6
              y=y+1.d0
              ser=ser+cof(i)/y
        end do
        fvn_d_lngamma=tmp+log(stp*ser/x)
  end function
  
  function fvn_d_factorial(n)
        ! This function returns factorial(n) as a real(8)
        ! adapted from Numerical Recipes
        ! real value is calculated for integers lower than 32
        implicit none
        integer(kind=4) :: n
        real(kind=8) :: fvn_d_factorial
  
        integer(kind=4) :: j
  
        fvn_d_factorial=1.
  
        if (n < 0) then
              write(*,*) "Factorial of a negative integer"
              stop
        end if
  
        if (n == 0) then
              return
        end if
  
        if (n <= 32) then 
              do j=1,n
                    fvn_d_factorial=fvn_d_factorial*j
              end do
              return
        else
              fvn_d_factorial=exp(fvn_d_lngamma(dble(n)+1.))
              return
        end if
  end function
  
  function fvn_d_csevl(x,a,n)
        implicit none
        ! This function evaluate the n-term chebyshev series a at x
        ! directly adapted from http://www.netlib.org/fn
        real(kind=8) :: x
        real(kind=8), dimension(n) :: a
        integer(kind=4) :: n
        real(kind=8) :: fvn_d_csevl
  
        real(kind=8) :: twox, b0, b1, b2
        integer(kind=4) :: i,ni
  
        twox = 2.0d0*x
        b1 = 0.d0
        b0 = 0.d0
        do i=1,n
          b2 = b1
          b1 = b0
          ni = n - i + 1
          b0 = twox*b1 - b2 + a(ni)
        end do
  
        fvn_d_csevl = 0.5d0 * (b0-b2)
  
  end function
  
  function fvn_s_csevl(x,a,n)
        implicit none
        ! This function evaluate the n-term chebyshev series a at x
        ! directly adapted from http://www.netlib.org/fn
        real(kind=4) :: x
        real(kind=4), dimension(n) :: a
        integer(kind=4) :: n
        real(kind=4) :: fvn_s_csevl
  
        real(kind=4) :: twox, b0, b1, b2
        integer(kind=4) :: i,ni
  
        twox = 2.0d0*x
        b1 = 0.d0
        b0 = 0.d0
        do i=1,n
          b2 = b1
          b1 = b0
          ni = n - i + 1
          b0 = twox*b1 - b2 + a(ni)
        end do
  
        fvn_s_csevl = 0.5d0 * (b0-b2)
  
  end function
f85d3b317   daniau   git-svn-id: https...
3374

d32a47033   daniau   git-svn-id: https...
3375
  end module fvn