File size: 6,468 Bytes
b1b3bae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
'    Property Package Auxiliary Calculations Base Classes 
'    Copyright 2008-2014 Daniel Wagner O. de Medeiros
'
'    This file is part of DWSIM.
'
'    DWSIM is free software: you can redistribute it and/or modify
'    it under the terms of the GNU General Public License as published by
'    the Free Software Foundation, either version 3 of the License, or
'    (at your option) any later version.
'
'    DWSIM is distributed in the hope that it will be useful,
'    but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    GNU General Public License for more details.
'
'    You should have received a copy of the GNU General Public License
'    along with DWSIM.  If not, see <http://www.gnu.org/licenses/>.

Imports System.Numerics
Imports MathNet.Numerics

Namespace MathEx

    Public Class PolySolve

        Shared Function Poly_Roots(ByVal Coeff As Double()) As Double(,)

            Return CalcRoots(Coeff(3), Coeff(2), Coeff(1), Coeff(0))

        End Function

        Shared Function Poly_Roots3(ByVal Coeff As Double()) As Double()

            Return CalcRoots3(Coeff(3), Coeff(2), Coeff(1), Coeff(0))

        End Function

        Shared Function CalcRoots2(ByVal a As Double, ByVal b As Double, ByVal c As Double, ByVal d As Double) As Double(,)

            Dim roots0 = FindRoots.Cubic(d, c, b, a)
            Dim root1 = roots0.Item1
            Dim root2 = roots0.Item2
            Dim root3 = roots0.Item3

            Dim roots(2, 1), real1, im1 As Double

            roots(0, 0) = root1.Real
            If Math.Abs(root1.Imaginary) > 0.0000000001 Then
                roots(0, 1) = root1.Imaginary
            Else
                real1 = root1.Real
                im1 = root1.Imaginary
            End If
            roots(1, 0) = root2.Real
            If Math.Abs(root2.Imaginary) > 0.0000000001 Then
                roots(1, 1) = root2.Imaginary
            Else
                real1 = root2.Real
                im1 = root2.Imaginary
            End If
            roots(2, 0) = root3.Real
            If Math.Abs(root3.Imaginary) > 0.0000000001 Then
                roots(2, 1) = root3.Imaginary
            Else
                real1 = root3.Real
                im1 = root3.Imaginary
            End If

            If roots(0, 0) < 0.0000000001 Then
                roots(0, 0) = real1
                roots(0, 1) = im1
            End If
            If roots(1, 0) < 0.0000000001 Then
                roots(1, 0) = real1
                roots(1, 1) = im1
            End If
            If roots(2, 0) < 0.0000000001 Then
                roots(2, 0) = real1
                roots(2, 1) = im1
            End If

            Return roots

        End Function

        Shared Function CalcRoots3(ByVal a As Double, ByVal b As Double, ByVal c As Double, ByVal d As Double) As Double()

            Dim roots0 = FindRoots.Cubic(d, c, b, a)
            Dim root1 = roots0.Item1
            Dim root2 = roots0.Item2
            Dim root3 = roots0.Item3

            Dim roots(2) As Double
            Dim real1 As Double

            If Math.Abs(root1.Imaginary) < 0.00000001 Then
                roots(0) = root1.Real
                real1 = roots(0)
            End If
            If Math.Abs(root2.Imaginary) < 0.00000001 Then
                roots(1) = root2.Real
                real1 = roots(1)
            End If
            If Math.Abs(root3.Imaginary) < 0.00000001 Then
                roots(2) = root3.Real
                real1 = roots(2)
            End If

            If Math.Abs(roots(0)) < 0.0000000001 Then roots(0) = real1
            If Math.Abs(roots(1)) < 0.0000000001 Then roots(1) = real1
            If Math.Abs(roots(2)) < 0.0000000001 Then roots(2) = real1

            Array.Sort(roots)

            Return roots

        End Function

        Shared Function CalcRoots(ByVal a As Double, ByVal b As Double, ByVal c As Double, ByVal d As Double) As Double(,)

            Dim cnt As Integer = 0
            Dim r, rant, rant2, fi, fi_ant, fi_ant2, dfidr As Double

            fi_ant = 0.0#
            fi = 0.0#

            r = 0.01
            rant = r
            Do
                fi_ant2 = fi_ant
                fi_ant = fi
                fi = a * r * r * r + b * r * r + c * r + d
                dfidr = 3 * a * r * r + 2 * b * r + c
                rant = r
                r = r - fi / dfidr
                If Math.Abs(fi - fi_ant2) = 0.0# Then r = rant * 1.01
                cnt += 1
            Loop Until Math.Abs(fi) < 0.00000001 Or cnt >= 1000

            Dim r1, i1, r2, i2, r3, i3 As Double

            If cnt >= 1000 Then
                r1 = r
                i1 = -1
            Else
                r1 = r
                i1 = 0
            End If

            fi_ant = 0
            fi = 0

            cnt = 0

            r = 0.99999999
            rant = r
            Do
                fi_ant2 = fi_ant
                fi_ant = fi
                fi = a * r * r * r + b * r * r + c * r + d
                dfidr = 3 * a * r * r + 2 * b * r + c
                rant = r
                r = r - fi / dfidr
                If Math.Abs(fi - fi_ant2) = 0 Then r = rant * 0.999
                cnt += 1
            Loop Until Math.Abs(fi) < 0.00000001 Or cnt >= 1000

            If cnt >= 1000 Then
                r2 = r
                i2 = -1
            Else
                r2 = r
                i2 = 0
            End If

            fi_ant = 0
            fi = 0

            cnt = 0

            r = 0.5
            rant = r
            Do
                fi_ant2 = fi_ant
                fi_ant = fi
                fi = a * r * r * r + b * r * r + c * r + d
                dfidr = 3 * a * r * r + 2 * b * r + c
                rant = r
                r = r - fi / dfidr
                If Math.Abs(fi - fi_ant2) = 0 Then r = rant * 0.999
                cnt += 1
            Loop Until Math.Abs(fi) < 0.00000001 Or cnt >= 1000

            If cnt >= 1000 Then
                r3 = r
                i3 = -1
            Else
                r3 = r
                i3 = 0
            End If

            Dim roots(2, 1) As Double

            roots(0, 0) = r1
            roots(0, 1) = i1
            roots(1, 0) = r2
            roots(1, 1) = i2
            roots(2, 0) = r3
            roots(2, 1) = i3

            Return roots

        End Function

    End Class

End Namespace