Debian Codegraph @

src/cubicsolve.f90

raw
1 !-----------------------------------------------------------------
2 !
3 ! This file is (or was) part of SPLASH, a visualisation tool
4 ! for Smoothed Particle Hydrodynamics written by Daniel Price:
5 !
6 ! http://users.monash.edu.au/~dprice/splash
7 !
8 ! SPLASH comes with ABSOLUTELY NO WARRANTY.
9 ! This is free software; and you are welcome to redistribute
10 ! it under the terms of the GNU General Public License
11 ! (see LICENSE file for details) and the provision that
12 ! this notice remains intact. If you modify this file, please
13 ! note section 2a) of the GPLv2 states that:
14 !
15 ! a) You must cause the modified files to carry prominent notices
16 ! stating that you changed the files and the date of any change.
17 !
18 ! Copyright (C) 2005-2011 Daniel Price. All rights reserved.
19 ! Contact: [email protected]
20 !
21 !-----------------------------------------------------------------
22 module cubic
23 implicit none
24
25 contains
26 !-------------------------------------------------------------
27 ! this subroutine finds the real solutions to
28 ! a cubic equation of the form
29 !
30 ! a*x^3 + b*x^2 + c*x + d
31 !
32 ! formulae taken from:
33 ! Woan, The Cambridge Handbook of Physics Formulas, 2000, p51
34 !
35 ! input : a,b,c,d : coefficients of cubic polynomial
36 ! output : x(3) : array containing up to 3 real solutions
37 ! => x(1:nreal) non-zero, rest set to zero
38 ! nreal : number of real solutions
39 !
40 ! Daniel Price, 22/2/07
41 ! [email protected]
42 !-------------------------------------------------------------
43 subroutine cubicsolve(a,b,c,d,x,nreal,check)
44 real, intent(in) :: a,b,c,d
45 real, intent(out), dimension(3) :: x
46 integer, intent(out) :: nreal
47 logical, intent(in), optional :: check
48 real :: p,q,det,sqrtdet
49 real :: a2,b2,u,v,y1,y2,y3,term,phi
50 real, parameter :: eps = 1000.*epsilon(0.)
51 real, parameter :: pi = 3.14159265358979323846
52 integer :: i
53
54 x = 0.
55 !
56 !--handle all trivial cases (quadratic, linear, all zero)
57 !
58 if (abs(a) < eps) then
59 det = c**2 - 4.*b*d
60 if (det < 0.) then ! no solutions to quadratic
61 nreal = 0
62 else
63 if (abs(b) < eps) then
64 !--no solutions if a = 0, b = 0, c = 0
65 if (abs(c) < eps) then
66 nreal = 0
67 else
68 !--solve linear equation if a = 0, b = 0
69 nreal = 1
70 x(1) = -d/c
71 endif
72 else
73 !--solve quadratic for a = 0
74 nreal = 2
75 sqrtdet = sqrt(det)
76 x(1) = 0.5*(-c + sqrtdet)/b
77 x(2) = 0.5*(-c - sqrtdet)/b
78 endif
79 endif
80 else
81 !
82 !--cubic solution
83 !
84 a2 = a**2
85 b2 = b**2
86 p = (c/a - b2/(3.*a2))
87 q = (2.*b**3/(27.*a2*a) - b*c/(3.*a2) + d/a)
88 det = (p**3)/27. + 0.25*q**2
89 !
90 !--determine number of solutions
91 !
92 if (det < 0.) then
93 !--3 distinct real roots
94 nreal = 3
95 term = sqrt(abs(p)/3.)
96 phi = acos(-0.5*q*term**(-3))
97
98 !--these are the solutions to the reduced cubic
99 ! y^3 + py + q = 0
100 y1 = 2.*term*cos(phi/3.)
101 y2 = -2.*term*cos((phi + pi)/3.)
102 y3 = -2.*term*cos((phi - pi)/3.)
103 else
104 !--1 real, 2 complex roots
105 term = -0.5*q + sqrt(det)
106 !--must take cube root of positive quantity, then give sign later
107 ! (otherwise gives NaNs)
108 u = (abs(term))**(1/3.)*SIGN(1.0,term)
109 term = -0.5*q - sqrt(det)
110 v = (abs(term))**(1/3.)*SIGN(1.0,term)
111 nreal = 1
112 y1 = u + v
113 !--if det=0, 3 real roots, but at least 2 equal, so max of 2 unique roots)
114 if (abs(det) < tiny(det)) then
115 nreal = 2
116 y2 = -(u + v)/2.
117 endif
118 y3 = 0.
119 endif
120 !--return solutions to original cubic, not reduced cubic
121 term = b/(3.*a)
122 if (nreal >= 1) x(1) = y1 - term
123 if (nreal >= 2) x(2) = y2 - term
124 if (nreal >= 3) x(3) = y3 - term
125
126 endif
127
128 if (present(check)) then
129 if (check) then
130 !--verify the cubic solution
131 print*,'verifying: ',a,'x^3 + ',b,'x^2 + ',c,'x + ',d
132 do i=1,nreal
133 term = a*x(i)**3 + b*x(i)**2 + c*x(i) + d
134 if (abs(term) < eps) then
135 print*,'root ',i,':',x(i),'f=',term,': OK'
136 else
137 print*,'root ',i,':',x(i),'f=',term,': FAILED',eps
138 endif
139 enddo
140 endif
141 endif
142 return
143
144 end subroutine cubicsolve
145
146 !-------------------------------------------------------------
147 ! this subroutine returns both the real and complex
148 ! solutions to a cubic equation of the form
149 !
150 ! x^3 + b*x^2 + c*x + d
151 !
152 ! input : b,c,d : coefficients of cubic polynomial
153 ! output : x(3) : array of 3 COMPLEX solutions
154 ! nreal : number of real solutions
155 !
156 ! The form of the equation above means that we
157 ! do not need to handle trivial cases (quadratic, etc.)
158 ! and that there will always be 3 solutions.
159 !
160 ! Daniel Price, [email protected] 21/01/2011
161 !
162 !-------------------------------------------------------------
163 subroutine cubicsolve_complex(b,c,d,x,nreal,check)
164 real, intent(in) :: b,c,d
165 complex, intent(out), dimension(3) :: x
166 integer, intent(out), optional :: nreal
167 logical, intent(in), optional :: check
168 double precision :: p,q,q2,xi
169 double precision :: b2,term,termA,det,phi
170 real :: termr,termi
171 double precision :: fx,dfx
172 real, parameter :: eps = 1000.*epsilon(0.)
173 double precision, parameter :: pi = 3.14159265358979323846d0
174 integer :: i,j,nroots
175
176 x = (0.,0.)
177 !
178 !--preliminaries
179 !
180 b2 = b*b
181 p = (c - b2/3.)
182 q = (2.*b2*b - 9.*b*c + 27.*d)/27.
183 q2 = q*q
184 det = (p*p*p)/27. + 0.25*q2
185 if (det < 0) then
186 !--3 distinct real roots
187 nroots = 3
188 term = sqrt(abs(p)/3.)
189 phi = acos(-0.5*q*term**(-3))
190
191 !--these are the solutions to the reduced cubic
192 ! y^3 + py + q = 0
193 x(1) = real(2.d0*term*cos(phi/3.d0))
194 x(2) = real(-2.d0*term*cos((phi + pi)/3.d0))
195 x(3) = real(-2.d0*term*cos((phi - pi)/3.d0))
196 else
197 !--1 real, two complex
198 nroots = 1
199 if (abs(det) < tiny(det)) nroots = 2
200 term = -0.5*q + sqrt(det)
201 termA = (abs(term))**(1.d0/3.d0)*SIGN(1.0d0,term)
202
203 x(1) = real(termA - p/(3.*termA))
204 termr = real(-0.5*termA + p/(6.*termA)) ! convert from double prec.
205 termi = real(0.5*sqrt(3.d0)*(termA + p/(3.d0*termA)))
206 x(2) = cmplx(termr,termi)
207 x(3) = cmplx(termr,-termi)
208 endif
209
210 !--return solutions to original cubic, not reduced cubic
211 x(:) = x(:) - b/3.
212
213 !--if determinant is small, take a couple of Newton-Raphson iterations
214 ! to beat down the error
215 if (abs(det) < eps) then
216 do i=1,nroots
217 xi = dble(x(i))
218 do j=1,3
219 fx = xi*(xi*(xi + b) + c) + d
220 dfx = xi*(3.d0*xi + 2.d0*b) + c
221 if (abs(dfx) > 0.) xi = xi - fx/dfx
222 enddo
223 x(i) = real(xi)
224 enddo
225 endif
226
227 if (present(nreal)) nreal = nroots
228
229 !--the following lines can be used for debugging
230 if (present(check)) then
231 if (check) then
232 !--verify the cubic solution
233 print*,'verifying: x^3 + ',b,'x^2 + ',c,'x + ',d
234 do i=1,3
235 term = real(x(i)**3 + b*x(i)**2 + c*x(i) + d)
236 if (abs(term) < eps) then
237 print*,'root ',i,':',x(i),'f=',term,': OK'
238 else
239 print*,'root ',i,':',x(i),'f=',term,': FAILED',eps
240 endif
241 enddo
242 endif
243 endif
244
245 return
246 end subroutine cubicsolve_complex
247
248 end module cubic