File size: 5,007 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
'    Miscelaneous Math Functions for DWSIM
'    Copyright 2008 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.Linq

Namespace MathEx

    Public Class Common

        Shared Function CopyToVector(ByVal arr As ArrayList, ByVal index As Integer) As Double()

            Dim i As Integer = 0
            Dim values(arr.Count - 1) As Double

            For i = 0 To arr.Count - 1
                values(i) = arr(i)(index)
            Next

            Return values

        End Function

        Shared Function Max(ByVal Vv As Array, ByVal Vz As Array)

            Dim n = UBound(Vv)
            Dim mx As Double

            If n >= 1 Then
                Dim i As Integer = 1

                mx = Vv(i - 1)
                i = 0
                Do
                    If Vv(i) > mx And Vz(i) <> 0 Then
                        mx = Vv(i)
                    End If
                    i += 1
                Loop Until i = n + 1

                Return mx
            Else
                If Vv.Length > 0 Then Return Vv(0) Else Return 0
            End If

        End Function

        Shared Function Max(ByVal Vv As Array)

            Dim n = UBound(Vv)
            Dim mx As Double

            If n >= 1 Then
                Dim i As Integer = 1

                mx = Vv(i - 1)
                i = 0
                Do
                    If Vv(i) > mx Then
                        mx = Vv(i)
                    End If
                    i += 1
                Loop Until i = n + 1

                Return mx
            Else
                If Vv.Length > 0 Then Return Vv(0) Else Return 0
            End If

        End Function

        Shared Function Min(ByVal Vv As Array, ByVal Vz As Array)

            Dim n = UBound(Vv)
            Dim mx As Double

            If n >= 1 Then
                Dim i As Integer = 1

                'Do
                '    mx = Vv(i - 1)
                '    i += 1
                'Loop Until i = n + 1

                mx = Vv(i - 1)
                i = 0
                Do
                    If Vv(i) < mx And Vz(i) <> 0 Then
                        mx = Vv(i)
                    End If
                    i += 1
                Loop Until i = n + 1

                Return mx
            Else
                If Vv.Length > 0 Then Return Vv(0) Else Return 0
            End If

        End Function

        Shared Function Min(ByVal Vv As Array)

            Dim n = UBound(Vv)
            Dim mx As Double

            If n >= 1 Then
                Dim i As Integer = 1

                Do
                    If Vv(i - 1) <> 0 Then
                        mx = Vv(i - 1)
                        Exit Do
                    End If
                    i += 1
                Loop Until i = n + 2

                i = i - 1
                Do
                    If Vv(i) < mx Then
                        mx = Vv(i)
                    End If
                    i += 1
                Loop Until i = n + 1

                Return mx
            Else
                If Vv.Length > 0 Then Return Vv(0) Else Return 0
            End If

        End Function

        Shared Function Sum(ByVal vx() As Double) As Double
            If vx.Length = 0 Then Return 0

            Return vx.Sum()

        End Function

        Shared Function AbsSum(ByVal vx() As Double) As Double
            Dim n = UBound(vx)
            Dim i As Integer
            Dim sumv As Double = 0

            For i = 0 To n
                sumv += Math.Abs(vx(i))
            Next

            Return sumv

        End Function

        Shared Function WgtAvg(ByVal vw() As Double, ByVal vx() As Double) As Double

            Dim n = UBound(vx)
            Dim i As Integer
            Dim sumv As Double = 0
            Dim total As Double = 0.0#
            For i = 0 To n
                total += vw(i)
            Next
            For i = 0 To n
                sumv += vw(i) * vx(i) / total
            Next

            Return sumv

        End Function

        Shared Function SumSqr(ByVal vx() As Double) As Double

            Dim n = UBound(vx)
            Dim i As Integer
            Dim sumv As Double = 0.0#

            For i = 0 To n
                sumv += Math.Pow(vx(i), 2)
            Next

            Return sumv

        End Function

    End Class

End Namespace