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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
Option Strict Off
Option Explicit On 

Imports System.Math

Namespace MathEx.MatrixOps

    Public Class MatLib
        Private Shared Sub Find_R_C(ByVal Mat(,) As Double, ByRef Row As Integer, ByRef Col As Integer)
            Row = Mat.GetUpperBound(0)
            Col = Mat.GetUpperBound(1)
        End Sub

#Region "Add Matrices"
        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        ' Add two matrices, their dimensions should be compatible!
        ' Function returns the summation or errors due to
        ' dimensions incompatibility
        ' Example:
        '  Check Main Form !!
        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        Public Shared Function Add(ByVal Mat1(,) As Double, ByVal Mat2(,) As Double) As Double(,)
            Dim sol(,) As Double
            Dim i, j As Integer
            Dim Rows1, Cols1 As Integer
            Dim Rows2, Cols2 As Integer

            On Error GoTo Error_Handler

            Find_R_C(Mat1, Rows1, Cols1)
            Find_R_C(Mat2, Rows2, Cols2)

            If Rows1 <> Rows2 Or Cols1 <> Cols2 Then
                GoTo Error_Dimension
            End If

            ReDim sol(Rows1, Cols1)
            For i = 0 To Rows1
                For j = 0 To Cols1
                    sol(i, j) = Mat1(i, j) + Mat2(i, j)
                Next j
            Next i

            Return sol

Error_Dimension:
            Err.Raise("5005", , "Dimensions of the two matrices do not match !")

Error_Handler:
            If Err.Number = 5005 Then
                Err.Raise("5005", , "Dimensions of the two matrices do not match !")
            Else
                Err.Raise("5022", , "One or both of the matrices are null, this operation cannot be done !!")
            End If

            Return Nothing

        End Function
#End Region

#Region "Subtract Matrices"
        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        ' Subtracts two matrices from each other, their
        ' dimensions should be compatible!
        ' Function returns the solution or errors due to
        ' dimensions incompatibility
        ' Example:
        '  Check Main Form !!
        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        Public Shared Function Subtract(ByVal Mat1(,) As Double, ByVal Mat2(,) As Double) As Double(,)
            Dim i, j As Integer
            Dim sol(,) As Double
            Dim Rows1, Cols1 As Integer
            Dim Rows2, Cols2 As Integer

            On Error GoTo Error_Handler

            Find_R_C(Mat1, Rows1, Cols1)
            Find_R_C(Mat2, Rows2, Cols2)

            If Rows1 <> Rows2 Or Cols1 <> Cols2 Then
                GoTo Error_Dimension
            End If

            ReDim sol(Rows1, Cols1)

            For i = 0 To Rows1
                For j = 0 To Cols1
                    sol(i, j) = Mat1(i, j) - Mat2(i, j)
                Next j
            Next i

            Return sol

Error_Dimension:
            Err.Raise("5007", , "Dimensions of the two matrices do not match !")

Error_Handler:
            If Err.Number = 5007 Then
                Err.Raise("5007", , "Dimensions of the two matrices do not match !")
            Else
                Err.Raise("5022", , "One or both of the matrices are null, this operation cannot be done !!")
            End If
            Return Nothing

        End Function

#End Region

#Region "Multiply Matrices"
        '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        ' Multiply two matrices, their dimensions should be compatible!
        ' Function returns the solution or errors due to
        ' dimensions incompatibility
        ' Example:
        '  Check Main Form !!
        '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        Public Shared Function Multiply(ByVal Mat1(,) As Double, ByVal Mat2(,) As Double) As Double(,)
            Dim l, i, j As Integer
            Dim OptiString As String
            Dim sol(,) As Double, MulAdd As Double
            Dim Rows1, Cols1 As Integer
            Dim Rows2, Cols2 As Integer

            On Error GoTo Error_Handler

            MulAdd = 0

            Find_R_C(Mat1, Rows1, Cols1)
            Find_R_C(Mat2, Rows2, Cols2)

            If Cols1 <> Rows2 Then
                GoTo Error_Dimension
            End If

            ReDim sol(Rows1, Cols2)

            For i = 0 To Rows1
                For j = 0 To Cols2
                    For l = 0 To Cols1
                        MulAdd = MulAdd + Mat1(i, l) * Mat2(l, j)
                    Next l
                    sol(i, j) = MulAdd
                    MulAdd = 0
                Next j
            Next i

            Return sol

Error_Dimension:
            Err.Raise("5009", , "Dimensions of the two matrices not suitable for multiplication !")

Error_Handler:
            If Err.Number = 5009 Then
                Err.Raise("5009", , "Dimensions of the two matrices not suitable for multiplication !")
            Else
                Err.Raise("5022", , "One or both of the matrices are null, this operation cannot be done !!")
            End If
            Return Nothing

        End Function

#End Region

#Region "Determinant of a Matrix"
        '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        ' Determinant of a matrix should be (nxn)
        ' Function returns the solution or errors due to
        ' dimensions incompatibility
        ' Example:
        '  Check Main Form !!
        '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        Public Shared Function Det(ByVal Mat(,) As Double) As Double
            Dim DArray(,) As Double, S As Integer
            Dim k, k1, i, j As Integer
            Dim save, ArrayK As Double
            Dim M1 As String
            Dim Rows, Cols As Integer

            On Error GoTo Error_Handler

            Find_R_C(Mat, Rows, Cols)

            If Rows <> Cols Then GoTo Error_Dimension

            S = Rows
            Det = 1
            DArray = Mat.Clone()

            For k = 0 To S
                If DArray(k, k) = 0 Then
                    j = k
                    Do While ((j < S) And (DArray(k, j) = 0))
                        j = j + 1
                    Loop
                    If DArray(k, j) = 0 Then
                        Det = 0
                        Exit Function
                    Else
                        For i = k To S
                            save = DArray(i, j)
                            DArray(i, j) = DArray(i, k)
                            DArray(i, k) = save
                        Next i
                    End If

                    Det = -Det
                End If
                ArrayK = DArray(k, k)
                Det = Det * ArrayK
                If k < S Then
                    k1 = k + 1
                    For i = k1 To S
                        For j = k1 To S
                            DArray(i, j) = DArray(i, j) - DArray(i, k) * (DArray(k, j) / ArrayK)
                        Next j
                    Next i
                End If
            Next

            Exit Function

Error_Dimension:
            Err.Raise("5011", , "Matrix should be a square matrix !")

Error_Handler:
            If Err.Number = 5011 Then
                Err.Raise("5011", , "Matrix should be a square matrix !")
            Else
                Err.Raise("5022", , "In order to do this operation values must be assigned to the matrix !!")
            End If
        End Function

#End Region

#Region "Inverse of a Matrix"
        '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        ' Inverse of a matrix, should be (nxn) and det(Mat)<>0
        ' Function returns the solution or errors due to
        ' dimensions incompatibility
        ' Example:
        '  Check Main Form !!
        '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        Public Shared Function Inv(ByVal Mat(,) As Double) As Double(,)
            Dim AI(,) As Double, AIN As Double, AF As Double, _
                Mat1(,) As Double
            Dim LL As Integer, LLM As Integer, L1 As Integer, _
                L2 As Integer, LC As Integer, LCA As Integer, _
                LCB As Integer, i As Integer, j As Integer
            Dim Rows, Cols As Integer

            On Error GoTo Error_Handler

            Find_R_C(Mat, Rows, Cols)
            If Rows <> Cols Then GoTo Error_Dimension

            If Det(Mat) = 0 Then GoTo Error_Zero

            LL = Rows
            LLM = Cols
            Mat1 = Mat.Clone()
            ReDim AI(LL, LL)

            For L2 = 0 To LL
                For L1 = 0 To LL
                    AI(L1, L2) = 0
                Next
                AI(L2, L2) = 1
            Next

            For LC = 0 To LL
                If Abs(Mat1(LC, LC)) < 0.0000000001 Then
                    For LCA = LC + 1 To LL
                        If LCA = LC Then GoTo 1090
                        If Abs(Mat1(LC, LCA)) > 0.0000000001 Then
                            For LCB = 0 To LL
                                Mat1(LCB, LC) = Mat1(LCB, LC) + Mat1(LCB, LCA)
                                AI(LCB, LC) = AI(LCB, LC) + AI(LCB, LCA)
                            Next
                            GoTo 1100
                        End If
1090:               Next
                End If

1100:
                AIN = 1 / Mat1(LC, LC)
                For LCA = 0 To LL
                    Mat1(LCA, LC) = AIN * Mat1(LCA, LC)
                    AI(LCA, LC) = AIN * AI(LCA, LC)
                Next

                For LCA = 0 To LL
                    If LCA = LC Then GoTo 1150
                    AF = Mat1(LC, LCA)
                    For LCB = 0 To LL
                        Mat1(LCB, LCA) = Mat1(LCB, LCA) - AF * Mat1(LCB, LC)
                        AI(LCB, LCA) = AI(LCB, LCA) - AF * AI(LCB, LC)
                    Next
1150:           Next

            Next

            Return AI

Error_Zero:
            Err.Raise("5012", , "Determinent equals zero, inverse can't be found !")

Error_Dimension:
            Err.Raise("5014", , "Matrix should be a square matrix !")

Error_Handler:
            If Err.Number = 5012 Then
                Err.Raise("5012", , "Determinent equals zero, inverse can't be found !")
            ElseIf Err.Number = 5014 Then
                Err.Raise("5014", , "Matrix should be a square matrix !")
            End If

            Return Nothing

        End Function

#End Region

#Region "Multiply Vectors"
        '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        ' Multiply two vectors, dimensions should be (3x1)
        ' Function returns the solution or errors due to
        ' dimensions incompatibility
        ' Example:
        '  Check Main Form !!
        '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        Public Shared Function MultiplyVectors(ByVal Mat1(,) As Double, ByVal Mat2(,) As Double) As Double(,)
            Dim i, j, k As Double
            Dim sol(2, 0) As Double
            Dim Rows1, Cols1 As Integer
            Dim Rows2, Cols2 As Integer

            On Error GoTo Error_Handler

            Find_R_C(Mat1, Rows1, Cols1)
            Find_R_C(Mat2, Rows2, Cols2)

            If Rows1 <> 2 Or Cols1 <> 0 Then
                GoTo Error_Dimension
            End If

            If Rows2 <> 2 Or Cols2 <> 0 Then
                GoTo Error_Dimension
            End If

            i = Mat1(1, 0) * Mat2(2, 0) - Mat1(2, 0) * Mat2(1, 0)
            j = Mat1(2, 0) * Mat2(0, 0) - Mat1(0, 0) * Mat2(2, 0)
            k = Mat1(0, 0) * Mat2(1, 0) - Mat1(1, 0) * Mat2(0, 0)

            sol(0, 0) = i : sol(1, 0) = j : sol(2, 0) = k

            Return sol

Error_Dimension:
            Err.Raise("5016", , "Dimension should be (2 x 0) for both matrices in order to do cross multiplication !")

Error_Handler:

            If Err.Number = 5016 Then
                Err.Raise("5016", , "Dimension should be (2 x 0) for both matrices in order to do cross multiplication !")
            Else
                Err.Raise("5022", , "One or both of the matrices are null, this operation cannot be done !!")
            End If

            Return Nothing

        End Function

#End Region

#Region "Magnitude of a Vector"

        '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        ' Magnitude of a Vector, vector should be (3x1)
        ' Function returns the solution or errors due to
        ' dimensions incompatibility
        ' Example:
        '  Check Main Form !!
        '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        Public Shared Function VectorMagnitude(ByVal Mat(,) As Double) As Double

            Dim Rows, Cols As Integer

            On Error GoTo Error_Handler

            Find_R_C(Mat, Rows, Cols)

            If Rows <> 2 Or Cols <> 0 Then
                GoTo Error_Dimension
            End If

            Return Sqrt(Mat(0, 0) * Mat(0, 0) + Mat(1, 0) * Mat(1, 0) + Mat(2, 0) * Mat(2, 0))

Error_Dimension:
            Err.Raise("5018", , "Dimension of the matrix should be (2 x 0) in order to find the vector's norm !")

Error_Handler:
            If Err.Number = 5018 Then
                Err.Raise("5018", , "Dimension of the matrix should be (2 x 0) in order to find the vector's magnitude !")
            Else
                Err.Raise("5022", , "In order to do this operation values must be assigned to the matrix !!")
            End If

        End Function
#End Region

#Region "Transpose of a Matrix"
        '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        ' Transpose of a matrix
        ' Function returns the solution or errors
        ' Example:
        '  Check Main Form !!
        '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        Public Shared Function Transpose(ByVal Mat(,) As Double) As Double(,)
            Dim Tr_Mat(,) As Double
            Dim i, j, Rows, Cols As Integer

            On Error GoTo Error_Handler

            Find_R_C(Mat, Rows, Cols)

            ReDim Tr_Mat(Cols, Rows)

            For i = 0 To Cols
                For j = 0 To Rows
                    Tr_Mat(j, i) = Mat(i, j)
                Next j
            Next i

            Return Tr_Mat

Error_Handler:
            Err.Raise("5028", , "In order to do this operation values must be assigned to the matrix !!")

        End Function
#End Region

#Region "Multiply a matrix or a vector with a scalar quantity"

        '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        ' Multiply a matrix or a vector with a scalar quantity
        ' Function returns the solution or errors
        ' Example:
        '  Check Main Form !!
        '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        Public Shared Function ScalarMultiply(ByVal Value As Double, ByVal Mat(,) As Double) As Double(,)
            Dim i, j, Rows, Cols As Integer
            Dim sol(,) As Double

            On Error GoTo Error_Handler

            Find_R_C(Mat, Rows, Cols)
            ReDim sol(Rows, Cols)

            For i = 0 To Rows
                For j = 0 To Cols
                    sol(i, j) = Mat(i, j) * Value
                Next j
            Next i

            Return (sol)

Error_Handler:
            Err.Raise("5022", , "Matrix was not assigned")
        End Function

#End Region

#Region "Divide a matrix or a vector with a scalar quantity"
        '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        ' Divide matrix elements or a vector by a scalar quantity
        ' Function returns the solution or errors
        ' Example:
        '  Check Main Form !!
        '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        Public Shared Function ScalarDivide(ByVal Value As Double, ByVal Mat(,) As Double) As Double(,)
            Dim i, j, Rows, Cols As Integer
            Dim sol(,) As Double

            On Error GoTo Error_Handler

            Find_R_C(Mat, Rows, Cols)
            ReDim sol(Rows, Cols)

            For i = 0 To Rows
                For j = 0 To Cols
                    sol(i, j) = Mat(i, j) / Value
                Next j
            Next i

            Return sol

            Exit Function

Error_Handler:
            Err.Raise("5022", , "Matrix was not assigned")
        End Function

#End Region


#Region "Print Matrix"

        '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        ' Print a matrix to multitext text box
        ' Function returns the solution or errors
        ' Example:
        '  Check Main Form !!
        '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        Public Shared Function PrintMat(ByVal Mat(,) As Double) As String
            Dim N_Rows As Integer, N_Columns, k As Integer, _
                i As Integer, j As Integer, m As Integer
            Dim StrElem As String, StrLen As Long, _
                Greatest() As Integer, LarString As String = ""
            Dim OptiString As String, sol As String

            Find_R_C(Mat, N_Rows, N_Columns)

            sol = ""
            OptiString = ""

            ReDim Greatest(N_Columns)

            For i = 0 To N_Rows
                For j = 0 To N_Columns
                    If i = 0 Then
                        Greatest(j) = 0
                        For m = 0 To N_Rows
                            StrElem = Format$(Mat(m, j), "0.0000")
                            StrLen = Len(StrElem)
                            If Greatest(j) < StrLen Then
                                Greatest(j) = StrLen
                                LarString = StrElem
                            End If
                        Next m
                        If Mid$(LarString, 1, 1) = "-" Then Greatest(j) = Greatest(j) + 1
                    End If
                    StrElem = Format$(Mat(i, j), "0.0000")
                    If Mid$(StrElem, 1, 1) = "-" Then
                        StrLen = Len(StrElem)
                        If Greatest(j) >= StrLen Then
                            For k = 1 To (Greatest(j) - StrLen)
                                OptiString = OptiString & "  "
                            Next k
                            OptiString = OptiString & " "
                        End If
                    Else
                        StrLen = Len(StrElem)
                        If Greatest(j) > StrLen Then
                            For k = 1 To (Greatest(j) - StrLen)
                                OptiString = OptiString & "  "
                            Next k
                        End If
                    End If
                    OptiString = OptiString & "  " & Format$(Mat(i, j), "0.0000")
                Next j
                If i <> N_Rows Then
                    sol = sol & OptiString & vbCrLf
                    OptiString = ""
                End If
                sol = sol & OptiString
                OptiString = ""
            Next i

            PrintMat = sol

            Exit Function
        End Function
#End Region

    End Class

End Namespace