forked from DanWBR/dwsim5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSysLin.vb
1785 lines (1585 loc) · 69 KB
/
SysLin.vb
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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
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
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
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
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Namespace MathEx.SysLin
Public Class rsolve
'/*************************************************************************
' Solving a system of linear equations with a system matrix given by its
' LU decomposition.
' The algorithm solves a system of linear equations whose matrix is given by
' its LU decomposition. In case of a singular matrix, the algorithm returns
' False.
' The algorithm solves systems with a square matrix only.
' Input parameters:
' A - LU decomposition of a system matrix in compact form (the
' result of the RMatrixLU subroutine).
' Pivots - row permutation table (the result of a
' RMatrixLU subroutine).
' B - right side of a system.
' Array whose index ranges within [0..N-1].
' N - size of matrix A.
' Output parameters:
' X - solution of a system.
' Array whose index ranges within [0..N-1].
' Result:
' True, if the matrix is not singular.
' False, if the matrux is singular. In this case, X doesn't contain a
' solution.
' -- ALGLIB --
' Copyright 2005-2008 by Bochkanov Sergey
'*************************************************************************/
' Methods
Public Shared Function rmatrixlusolve(ByRef a As Double(,), ByRef pivots As Integer(), ByVal b As Double(), ByVal n As Integer, ByRef x As Double()) As Boolean
Dim result As Boolean = False
Dim y As Double() = New Double(0) {}
Dim i As Integer = 0
Dim v As Double = 0
Dim i_ As Integer = 0
b = DirectCast(b.Clone, Double())
y = New Double(((n - 1) + 1)) {}
x = New Double(((n - 1) + 1)) {}
result = True
i = 0
Do While (i <= (n - 1))
If (a(i, i) = 0) Then
Return False
End If
i += 1
Loop
i = 0
Do While (i <= (n - 1))
If (pivots(i) <> i) Then
v = b(i)
b(i) = b(pivots(i))
b(pivots(i)) = v
End If
i += 1
Loop
y(0) = b(0)
i = 1
Do While (i <= (n - 1))
v = 0
i_ = 0
Do While (i_ <= (i - 1))
v = (v + (a(i, i_) * y(i_)))
i_ += 1
Loop
y(i) = (b(i) - v)
i += 1
Loop
x((n - 1)) = (y((n - 1)) / a((n - 1), (n - 1)))
i = (n - 2)
Do While (i >= 0)
v = 0
i_ = (i + 1)
Do While (i_ <= (n - 1))
v = (v + (a(i, i_) * x(i_)))
i_ += 1
Loop
x(i) = ((y(i) - v) / a(i, i))
i -= 1
Loop
Return result
End Function
'/*************************************************************************
' Solving a system of linear equations.
' The algorithm solves a system of linear equations by using the
' LU decomposition. The algorithm solves systems with a square matrix only.
' Input parameters:
' A - system matrix.
' Array whose indexes range within [0..N-1, 0..N-1].
' B - right side of a system.
' Array whose indexes range within [0..N-1].
' N - size of matrix A.
' Output parameters:
' X - solution of a system.
' Array whose index ranges within [0..N-1].
' Result:
' True, if the matrix is not singular.
' False, if the matrix is singular. In this case, X doesn't contain a
' solution.
' -- ALGLIB --
' Copyright 2005-2008 by Bochkanov Sergey
'*************************************************************************/
Public Shared Function rmatrixsolve(ByVal a As Double(,), ByVal b As Double(), ByVal n As Integer, ByRef x As Double()) As Boolean
Dim pivots As Integer() = New Integer(0) {}
a = DirectCast(a.Clone, Double(,))
b = DirectCast(b.Clone, Double())
lu.rmatrixlu(a, n, n, pivots)
Return rsolve.rmatrixlusolve(a, pivots, b, n, x)
End Function
Public Shared Function solvesystem(ByVal a As Double(,), ByVal b As Double(), ByVal n As Integer, ByRef x As Double()) As Boolean
Dim pivots As Integer() = New Integer(0) {}
a = DirectCast(a.Clone, Double(,))
b = DirectCast(b.Clone, Double())
lu.ludecomposition(a, n, n, pivots)
Return rsolve.solvesystemlu(a, pivots, b, n, x)
End Function
Public Shared Function solvesystemlu(ByRef a As Double(,), ByRef pivots As Integer(), ByVal b As Double(), ByVal n As Integer, ByRef x As Double()) As Boolean
Dim result As Boolean = False
Dim y As Double() = New Double(0) {}
Dim i As Integer = 0
Dim v As Double = 0
Dim ip1 As Integer = 0
Dim im1 As Integer = 0
Dim i_ As Integer = 0
b = DirectCast(b.Clone, Double())
y = New Double(n + 1) {}
x = New Double(n + 1) {}
result = True
i = 1
Do While (i <= n)
If (a(i, i) = 0) Then
Return False
End If
i += 1
Loop
i = 1
Do While (i <= n)
If (pivots(i) <> i) Then
v = b(i)
b(i) = b(pivots(i))
b(pivots(i)) = v
End If
i += 1
Loop
y(1) = b(1)
i = 2
Do While (i <= n)
im1 = (i - 1)
v = 0
i_ = 1
Do While (i_ <= im1)
v = (v + (a(i, i_) * y(i_)))
i_ += 1
Loop
y(i) = (b(i) - v)
i += 1
Loop
x(n) = (y(n) / a(n, n))
i = (n - 1)
Do While (i >= 1)
ip1 = (i + 1)
v = 0
i_ = ip1
Do While (i_ <= n)
v = (v + (a(i, i_) * x(i_)))
i_ += 1
Loop
x(i) = ((y(i) - v) / a(i, i))
i -= 1
Loop
Return result
End Function
End Class
Public Class lu
'/*************************************************************************
' LU decomposition of a general matrix of size MxN
' The subroutine calculates the LU decomposition of a rectangular general
' matrix with partial pivoting (with row permutations).
' Input parameters:
' A - matrix A whose indexes range within [0..M-1, 0..N-1].
' M - number of rows in matrix A.
' N - number of columns in matrix A.
' Output parameters:
' A - matrices L and U in compact form (see below).
' Array whose indexes range within [0..M-1, 0..N-1].
' Pivots - permutation matrix in compact form (see below).
' Array whose index ranges within [0..Min(M-1,N-1)].
' Matrix A is represented as A = P * L * U, where P is a permutation matrix,
' matrix L - lower triangular (or lower trapezoid, if M>N) matrix,
' U - upper triangular (or upper trapezoid, if M<N) matrix.
' Let M be equal to 4 and N be equal to 3:
' ( 1 ) ( U11 U12 U13 )
' A = P1 * P2 * P3 * ( L21 1 ) * ( U22 U23 )
' ( L31 L32 1 ) ( U33 )
' ( L41 L42 L43 )
' Matrix L has size MxMin(M,N), matrix U has size Min(M,N)xN, matrix P(i) is
' a permutation of the identity matrix of size MxM with numbers I and Pivots[I].
' The algorithm returns array Pivots and the following matrix which replaces
' matrix A and contains matrices L and U in compact form (the example applies
' to M=4, N=3).
' ( U11 U12 U13 )
' ( L21 U22 U23 )
' ( L31 L32 U33 )
' ( L41 L42 L43 )
' As we can see, the unit diagonal isn't stored.
' -- LAPACK routine (version 3.0) --
' Univ. of Tennessee, Univ. of California Berkeley, NAG Ltd.,
' Courant Institute, Argonne National Lab, and Rice University
' June 30, 1992
' *************************************************************************/
' Methods
Public Shared Sub ludecomposition(ByRef a As Double(,), ByVal m As Integer, ByVal n As Integer, ByRef pivots As Integer())
Dim i As Integer = 0
Dim j As Integer = 0
Dim jp As Integer = 0
Dim t1 As Double() = New Double(0) {}
Dim s As Double = 0
Dim i_ As Integer = 0
pivots = New Integer((Math.Min(m, n) + 1)) {}
t1 = New Double((Math.Max(m, n) + 1)) {}
If Not ((m = 0) Or (n = 0)) Then
j = 1
Do While (j <= Math.Min(m, n))
jp = j
i = (j + 1)
Do While (i <= m)
If (Math.Abs(a(i, j)) > Math.Abs(a(jp, j))) Then
jp = i
End If
i += 1
Loop
pivots(j) = jp
If (a(jp, j) <> 0) Then
If (jp <> j) Then
i_ = 1
Do While (i_ <= n)
t1(i_) = a(j, i_)
i_ += 1
Loop
i_ = 1
Do While (i_ <= n)
a(j, i_) = a(jp, i_)
i_ += 1
Loop
i_ = 1
Do While (i_ <= n)
a(jp, i_) = t1(i_)
i_ += 1
Loop
End If
If (j < m) Then
jp = (j + 1)
s = (1 / a(j, j))
i_ = jp
Do While (i_ <= m)
a(i_, j) = (s * a(i_, j))
i_ += 1
Loop
End If
End If
If (j < Math.Min(m, n)) Then
jp = (j + 1)
i = (j + 1)
Do While (i <= m)
s = a(i, j)
i_ = jp
Do While (i_ <= n)
a(i, i_) = (a(i, i_) - (s * a(j, i_)))
i_ += 1
Loop
i += 1
Loop
End If
j += 1
Loop
End If
End Sub
Public Shared Sub ludecompositionunpacked(ByVal a As Double(,), ByVal m As Integer, ByVal n As Integer, ByRef l As Double(,), ByRef u As Double(,), ByRef pivots As Integer())
Dim i As Integer = 0
Dim j As Integer = 0
Dim minmn As Integer = 0
a = DirectCast(a.Clone, Double(,))
If Not ((m = 0) Or (n = 0)) Then
minmn = Math.Min(m, n)
l = New Double((m + 1), (minmn + 1)) {}
u = New Double((minmn + 1), (n + 1)) {}
lu.ludecomposition(a, m, n, pivots)
i = 1
Do While (i <= m)
j = 1
Do While (j <= minmn)
If (j > i) Then
l(i, j) = 0
End If
If (j = i) Then
l(i, j) = 1
End If
If (j < i) Then
l(i, j) = a(i, j)
End If
j += 1
Loop
i += 1
Loop
i = 1
Do While (i <= minmn)
j = 1
Do While (j <= n)
If (j < i) Then
u(i, j) = 0
End If
If (j >= i) Then
u(i, j) = a(i, j)
End If
j += 1
Loop
i += 1
Loop
End If
End Sub
Public Shared Sub rmatrixlu(ByRef a As Double(,), ByVal m As Integer, ByVal n As Integer, ByRef pivots As Integer())
Dim b(,) As Double = New Double(0, 0) {}
Dim t As Double() = New Double(0) {}
Dim bp As Integer() = New Integer(0) {}
Dim minmn As Integer = 0
Dim i As Integer = 0
Dim ip As Integer = 0
Dim j As Integer = 0
Dim j1 As Integer = 0
Dim j2 As Integer = 0
Dim cb As Integer = 0
Dim nb As Integer = 0
Dim v As Double = 0
Dim i_ As Integer = 0
Dim i1_ As Integer = 0
nb = 8
If (((n <= 1) Or (Math.Min(m, n) <= nb)) Or (nb = 1)) Then
lu.rmatrixlu2(a, m, n, pivots)
Else
b = New Double(((m - 1) + 1), ((nb - 1) + 1)) {}
t = New Double(((n - 1) + 1)) {}
pivots = New Integer(((Math.Min(m, n) - 1) + 1)) {}
minmn = Math.Min(m, n)
j1 = 0
j2 = (Math.Min(minmn, nb) - 1)
Do While (j1 < minmn)
cb = ((j2 - j1) + 1)
i = j1
Do While (i <= (m - 1))
i1_ = j1
i_ = 0
Do While (i_ <= (cb - 1))
b((i - j1), i_) = a(i, (i_ + i1_))
i_ += 1
Loop
i += 1
Loop
lu.rmatrixlu2(b, (m - j1), cb, bp)
i = j1
Do While (i <= (m - 1))
i1_ = -j1
i_ = j1
Do While (i_ <= j2)
a(i, i_) = b((i - j1), (i_ + i1_))
i_ += 1
Loop
i += 1
Loop
i = 0
Do While (i <= (cb - 1))
ip = bp(i)
pivots((j1 + i)) = (j1 + ip)
If (bp(i) <> i) Then
If (j1 <> 0) Then
i_ = 0
Do While (i_ <= (j1 - 1))
t(i_) = a((j1 + i), i_)
i_ += 1
Loop
i_ = 0
Do While (i_ <= (j1 - 1))
a((j1 + i), i_) = a((j1 + ip), i_)
i_ += 1
Loop
i_ = 0
Do While (i_ <= (j1 - 1))
a((j1 + ip), i_) = t(i_)
i_ += 1
Loop
End If
If (j2 < (n - 1)) Then
i_ = (j2 + 1)
Do While (i_ <= (n - 1))
t(i_) = a((j1 + i), i_)
i_ += 1
Loop
i_ = (j2 + 1)
Do While (i_ <= (n - 1))
a((j1 + i), i_) = a((j1 + ip), i_)
i_ += 1
Loop
i_ = (j2 + 1)
Do While (i_ <= (n - 1))
a((j1 + ip), i_) = t(i_)
i_ += 1
Loop
End If
End If
i += 1
Loop
If (j2 < (n - 1)) Then
i = (j1 + 1)
Do While (i <= j2)
j = j1
Do While (j <= (i - 1))
v = a(i, j)
i_ = (j2 + 1)
Do While (i_ <= (n - 1))
a(i, i_) = (a(i, i_) - (v * a(j, i_)))
i_ += 1
Loop
j += 1
Loop
i += 1
Loop
End If
If (j2 < (n - 1)) Then
i = (j2 + 1)
Do While (i <= (m - 1))
j = j1
Do While (j <= j2)
v = a(i, j)
i_ = (j2 + 1)
Do While (i_ <= (n - 1))
a(i, i_) = (a(i, i_) - (v * a(j, i_)))
i_ += 1
Loop
j += 1
Loop
i += 1
Loop
End If
j1 = (j2 + 1)
j2 = (Math.Min(minmn, (j1 + nb)) - 1)
Loop
End If
End Sub
Private Shared Sub rmatrixlu2(ByRef a As Double(,), ByVal m As Integer, ByVal n As Integer, ByRef pivots As Integer())
Dim i As Integer = 0
Dim j As Integer = 0
Dim jp As Integer = 0
Dim t1 As Double() = New Double(0) {}
Dim s As Double = 0
Dim i_ As Integer = 0
pivots = New Integer((Math.Min(Convert.ToInt32((m - 1)), Convert.ToInt32((n - 1))) + 1)) {}
t1 = New Double((Math.Max(Convert.ToInt32((m - 1)), Convert.ToInt32((n - 1))) + 1)) {}
If Not ((m = 0) Or (n = 0)) Then
j = 0
Do While (j <= Math.Min(Convert.ToInt32((m - 1)), Convert.ToInt32((n - 1))))
jp = j
i = (j + 1)
Do While (i <= (m - 1))
If (Math.Abs(a(i, j)) > Math.Abs(a(jp, j))) Then
jp = i
End If
i += 1
Loop
pivots(j) = jp
If (a(jp, j) <> 0) Then
If (jp <> j) Then
i_ = 0
Do While (i_ <= (n - 1))
t1(i_) = a(j, i_)
i_ += 1
Loop
i_ = 0
Do While (i_ <= (n - 1))
a(j, i_) = a(jp, i_)
i_ += 1
Loop
i_ = 0
Do While (i_ <= (n - 1))
a(jp, i_) = t1(i_)
i_ += 1
Loop
End If
If (j < m) Then
jp = (j + 1)
s = (1 / a(j, j))
i_ = jp
Do While (i_ <= (m - 1))
a(i_, j) = (s * a(i_, j))
i_ += 1
Loop
End If
End If
If (j < (Math.Min(m, n) - 1)) Then
jp = (j + 1)
i = (j + 1)
Do While (i <= (m - 1))
s = a(i, j)
i_ = jp
Do While (i_ <= (n - 1))
a(i, i_) = (a(i, i_) - (s * a(j, i_)))
i_ += 1
Loop
i += 1
Loop
End If
j += 1
Loop
End If
End Sub
' Fields
Public Const lunb As Integer = 8
End Class
Public Class yves
'Author: Yves Vander Haeghen ([email protected])
'Version: 1.0
'VersionDate": 13 june 2003
'Class of helper functions for simple algebra operations on 1 and 2 dimensional single arrays
'Although speed is not essential, we try to avoid recreating and reallocating output arrays
'on every call as this could slow things down a lot. This means that usually the output arrays MUST
'be allocated and passed to the functions, except when they are passed on by reference.
'All matrices are supposedly ordered ROW x COLUMN
'August 2003: Added non-linear optimization (Nelder-Mead simplex algorithm)
Enum NormOrder As Integer
AbsoluteValue = 1
Euclidean = 2
Max = 16
End Enum
'Defines for NMS algorithm
Private Const NMSMAX = 30000
Private Const NMSTINY = 0.000000001
Private Const NMSTOL = 1.0E-23 'Machine precision?
'Helper function for NMS algorithm
Private Shared Sub Swap(ByRef sA As Single, ByRef sB As Single)
Dim sTemp As Single
sTemp = sA
sA = sB
sB = sTemp
End Sub
'Prototype for the function to be optimized
Delegate Function SolveNonLinearError(ByVal sX() As Single) As Single
Public Shared Function SolveNonLinear(ByVal sX(,) As Single, _
ByVal sY() As Single, _
ByVal lNrIterations As Long, _
ByVal ErrorFunction As SolveNonLinearError) As Boolean
'Minimize a function of iNrDim dimensions using the Nelder-Mead
'simplex algorythm (NMS). sX is a (iNrDim + 1) by iNrDim matrix
'initialized with a starting simplex. sY is a iNrDim vector with
'function values at the simplex points.
Dim iNrDims As Integer
Dim iNrPts As Integer, iLo As Integer, iHi As Integer
Dim i As Integer, j As Integer, iNHi As Integer
Dim sSum() As Single, sYSave As Single, sYTry As Single
Dim sRTol As Single, iDisplayCounter As Integer
SolveNonLinear = False
iNrDims = sX.GetUpperBound(1) + 1
iNrPts = iNrDims + 1
ReDim sSum(iNrDims - 1)
lNrIterations = 0
iDisplayCounter = 0
Sum(sX, sSum)
Do
'Rank vertices of simplex by function value
iLo = 0
If sY(0) > sY(1) Then
iNHi = 1
iHi = 0
Else
iNHi = 0
iHi = 1
End If
For i = 0 To iNrPts - 1
If sY(i) <= sY(iLo) Then iLo = i
If sY(i) > sY(iHi) Then
iNHi = iHi
iHi = i
ElseIf (sY(i) > sY(iNHi)) And (i <> iHi) Then
iNHi = i
End If
Next i
'TEST
'Debug.Print "Highest vertex: " & iHi & ", next " & iNHi
'Debug.Print "Lowest vertex: " & iLo
iDisplayCounter = iDisplayCounter + 1
sRTol = 2.0# * Math.Abs(sY(iHi) - sY(iLo)) / (Math.Abs(sY(iHi)) + Math.Abs(sY(iLo)) + NMSTINY)
If iDisplayCounter Mod 200 = 0 Then
Console.WriteLine("Iteration " & lNrIterations & ", best solution has error: " & sY(iLo))
End If
'Convergence criterium
If sRTol < NMSTOL Then
Swap(sY(0), sY(iLo))
For i = 0 To iNrDims - 1
Swap(sX(0, i), sX(iLo, i))
Next i
Dim sCoef(iNrDims - 1) As Single
Console.WriteLine("Convergence after " & lNrIterations & " iterations, with error " & sY(iLo))
GetMatrixRow(sX, sCoef, iLo)
Console.WriteLine("Parameters are " & ToString(sCoef))
Exit Do
End If
If lNrIterations > NMSMAX Then
'Do not raise error, result is useful most of the time!
'NMSErrorType = NMSTooManyIterations
'GoTo ErrorHandler
Exit Function
End If
lNrIterations = lNrIterations + 2
sYTry = SolveNonLinearAdjustSimplex(sX, sY, sSum, iNrDims, iHi, -1.0#, ErrorFunction)
If sYTry < sY(iLo) Then
sYTry = SolveNonLinearAdjustSimplex(sX, sY, sSum, iNrDims, iHi, 2.0#, ErrorFunction)
ElseIf sYTry > sY(iNHi) Then
sYSave = sY(iHi)
sYTry = SolveNonLinearAdjustSimplex(sX, sY, sSum, iNrDims, iHi, 0.5, ErrorFunction)
If sYTry >= sYSave Then
For i = 0 To iNrPts - 1
If i <> iLo Then
For j = 0 To iNrDims - 1
sX(i, j) = 0.5 * (sX(i, j) + sX(iLo, j))
sSum(j) = sX(i, j)
Next j
sY(i) = ErrorFunction(sSum)
End If
Next i
lNrIterations = lNrIterations + iNrDims
Sum(sX, sSum)
Else
lNrIterations = lNrIterations - 1
End If
End If
Loop While True
SolveNonLinear = True
End Function
Private Shared Function SolveNonLinearAdjustSimplex(ByVal sX(,) As Single, _
ByVal sY() As Single, _
ByVal sSum() As Single, _
ByVal iNrDims As Integer, _
ByVal iHi As Integer, _
ByVal sFactor As Single, _
ByVal ErrorFunction As SolveNonLinearError) As Single
Dim i As Integer, sFactor1 As Single, sFactor2 As Single
Dim sYTry As Single, sXTry(iNrDims - 1) As Single
'Debug.Print "Try adjustment simplex with factor " & sFactor
sFactor1 = (1.0# - sFactor) / iNrDims
sFactor2 = sFactor1 - sFactor
For i = 0 To iNrDims - 1
sXTry(i) = sSum(i) * sFactor1 - sX(iHi, i) * sFactor2
Next i
sYTry = ErrorFunction(sXTry)
'Console.WriteLine("Proposed vertex " & ToString(sXTry) & "Value " & sYTry)
If sYTry < sY(iHi) Then
sY(iHi) = sYTry
For i = 0 To iNrDims - 1
sSum(i) = sSum(i) + sXTry(i) - sX(iHi, i)
sX(iHi, i) = sXTry(i)
Next i
'DisplayMatrix "New simplex", sX()
Else
'Debug.Print "Vertex rejected"
End If
SolveNonLinearAdjustSimplex = sYTry
End Function
Public Shared Sub SolveNonLinearTest(ByVal iNrDims As Integer)
Dim sCoef() As Single, iVertexNr As Integer
Dim sSimplex(,) As Single, iNrVertices As Integer
Dim sSimplexVal() As Single, lNrIterations As Long
Dim i As Integer
Randomize()
iNrVertices = iNrDims + 1
ReDim sCoef(iNrDims - 1)
ReDim sSimplex(iNrVertices - 1, iNrDims - 1)
ReDim sSimplexVal(iNrVertices - 1)
For iVertexNr = 0 To iNrVertices - 1
For i = 0 To iNrDims - 1
If iVertexNr > 0 And i = iVertexNr - 1 Then
sCoef(i) = 1.0 * Rnd()
Else
sCoef(i) = 0.0#
End If
Next i
'Put in simplex and compute function value
SetMatrixRow(sSimplex, sCoef, iVertexNr)
sSimplexVal(iVertexNr) = SolveNonLinearTestError(sCoef)
Next iVertexNr
'Optimize
SolveNonLinear(sSimplex, sSimplexVal, lNrIterations, AddressOf SolveNonLinearTestError)
End Sub
Public Shared Function SolveNonLinearTestError(ByVal sCoef() As Single) As Single
'Return the error
Dim i, iNrDims As Integer, sError As Single = 100
iNrDims = sCoef.GetUpperBound(0) + 1
For i = 0 To iNrDims - 1
If i Mod 2 = 0 Then
sError += sCoef(i) * i
Else
sError -= sCoef(i) * i
End If
Next i
Return Math.Abs(sError)
End Function
Public Overloads Shared Function Solve(ByVal sA(,) As Single, ByVal sX(,) As Single, ByVal sY(,) As Single) As Boolean
'Solve A.X = Y, FOR every column of Y!!!
'This is useful because we only have to decompose A once,
'and then use this decomposition to compute X = inv(A).Y for every column of Y
'The results are stored in the corresponding columns of X
'See overloaded Solve for general explanation about the solver.
Dim sU(,) As Single = New Single(,) {}, sW() As Single = New Single() {}, sV(,) As Single = New Single(,) {}, i As Integer
Dim strError As String = ""
If SVDDecomposition(sA, sU, sW, sV, strError) = False Then
MsgBox("Algebra.Solve: SVD gives error '" & strError & "'", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly)
Return False
End If
SVDRemoveSingularValues(sW, 0.0001)
'Run though every column of sY, compute the result, and store it in the corresponding column of sX.
Dim iNrEquationSets As Integer = sY.GetUpperBound(1) + 1
Dim iNrVariables As Integer = sA.GetUpperBound(1) + 1
Dim iNrEquationsPerSet As Integer = sA.GetUpperBound(0) + 1
Dim sXCol(iNrVariables - 1), sYCol(iNrEquationsPerSet - 1) As Single
For i = 0 To iNrEquationSets - 1
GetMatrixColumn(sY, sYCol, i)
Solve(sA, sXCol, sYCol)
SetMatrixColumn(sX, sXCol, i)
Next
Return False
End Function
Public Overloads Shared Function Solve(ByVal sA(,) As Single, ByVal sX() As Single, ByVal sY() As Single) As Boolean
'Solve the set of linear equations represented by A.x = y.
'The number of equations can be larger than the number of variables (overdetermined):
'i.e. the number of rows in A > number of cols in A. In that case the solution is
'a solution in the least-squares sense.
'This routine uses singular value decomposition, translated from "Numerical recipes in C"
Dim sU(,) As Single = New Single(,) {}, sW() As Single = New Single() {}, sV(,) As Single = New Single(,) {}
Dim strError As String = ""
Console.WriteLine("Solving linear set of equations A.x = y with A" & _
vbNewLine & yves.ToString(sA) & _
vbNewLine & "y" & _
vbNewLine & yves.ToString(sY))
If SVDDecomposition(sA, sU, sW, sV, strError) = False Then
'MsgBox("Algebra.Solve: SVD gives error '" & strError & "'", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly)
Return False
End If
SVDRemoveSingularValues(sW, 0.0001)
'Compute pseudo-inverse multiplied with sY
SVDInvert(sU, sW, sV, sY, sX)
Return True
End Function
Private Shared Sub SVDRemoveSingularValues(ByVal sW() As Single, ByVal sThresholdFactor As Single)
'Set singular values to zero by compairing them to
'the highest value in w.
Dim iNrVariables As Integer = sW.GetUpperBound(0) + 1
Dim i As Integer, sWMax As Single = 0.0
For i = 0 To iNrVariables - 1
If sW(i) > sWMax Then sWMax = sW(i)
Next i
Dim sThreshold As Single = sThresholdFactor * sWMax
For i = 0 To iNrVariables - 1
If sW(i) < sThreshold Then sW(i) = 0.0
Next i
End Sub
Private Shared Sub SVDInvert(ByVal sU(,) As Single, _
ByVal sW() As Single, _
ByVal sV(,) As Single, _
ByVal sY() As Single, _
ByVal sX() As Single)
'Computes Y = inv(A).Y using the SVD decomposition of A = U.W.Vt
Dim jj, j, i, m, n As Integer
Dim s As Single
m = sU.GetUpperBound(0) + 1
n = sU.GetUpperBound(1) + 1
Dim tmp(n - 1) As Single
For j = 1 To n
s = 0.0
If sW(j - 1) <> 0.0 Then
For i = 1 To m
s = s + sU(i - 1, j - 1) * sY(i - 1)
Next i
s = s / sW(j - 1)
End If
tmp(j - 1) = s
Next j
For j = 1 To n
s = 0.0
For jj = 1 To n
s = s + sV(j - 1, jj - 1) * tmp(jj - 1)
Next jj
sX(j - 1) = s
Next j
End Sub
Private Shared Function SVDDecomposition(ByVal sA(,) As Single, _
ByRef sU(,) As Single, _
ByRef sW() As Single, _
ByRef sV(,) As Single, _
ByVal strError As String) As Boolean
'Compute the singular value decomposition of
'an m sx n matrix A: A = U.W.Vt
'None of the byref matrices must be allocated here.
'If something goes wrong it returns false with a message in strError
Dim Flag As Boolean, i As Integer, its As Integer
Dim j As Integer, jj As Integer, k As Integer
Dim l As Integer, nm As Integer
Dim c As Single, f As Single, h As Single, s As Single
Dim sX As Single, sY As Single, sz As Single, rv1() As Single
Dim anorm As Single, g As Single, hhscale As Single
'Extra variables for VBasic.
Dim sTemp1 As Single, n As Integer, m As Integer
m = sA.GetUpperBound(0) + 1
n = sA.GetUpperBound(1) + 1
If m < n Then
strError = "Not enough rows in A (underdetermined system)"
Return False
End If
ReDim sU(m - 1, n - 1)
ReDim sW(n - 1)
ReDim sV(n - 1, n - 1)
ReDim rv1(n - 1)
'Copy the matrix A in U.
Array.Copy(sA, sU, sA.Length)
'Householder reduction to bidiagonal form
anorm = 0.0#
For i = 1 To n
l = i + 1
rv1(i - 1) = hhscale * g
g = 0.0#
s = 0.0#
hhscale = 0.0#
If i <= m Then
For k = i To m
hhscale = hhscale + Math.Abs(sU(k - 1, i - 1))
Next k
If hhscale <> 0.0# Then
For k = i To m
sU(k - 1, i - 1) = sU(k - 1, i - 1) / hhscale
s = s + sU(k - 1, i - 1) * sU(k - 1, i - 1)
Next k
f = sU(i - 1, i - 1)
If f >= 0 Then
g = -Math.Sqrt(s)
Else
g = Math.Sqrt(s)
End If
h = f * g - s
sU(i - 1, i - 1) = f - g
If i <> n Then
For j = l To n
s = 0.0#
For k = i To m
s = s + sU(k - 1, i - 1) * sU(k - 1, j - 1)
Next k
f = s / h
For k = i To m
sU(k - 1, j - 1) = sU(k - 1, j - 1) + f * sU(k - 1, i - 1)
Next k
Next j
End If
For k = i To m
sU(k - 1, i - 1) = sU(k - 1, i - 1) * hhscale
Next k
End If
End If
sW(i - 1) = hhscale * g
g = 0.0#
s = 0.0#
hhscale = 0.0#
If i <= m And i <> n Then
For k = l To n
hhscale = hhscale + Math.Abs(sU(i - 1, k - 1))
Next k
If hhscale <> 0.0# Then
For k = l To n
sU(i - 1, k - 1) = sU(i - 1, k - 1) / hhscale
s = s + sU(i - 1, k - 1) * sU(i - 1, k - 1)
Next k
f = sU(i - 1, l - 1)
If f >= 0 Then
g = -Math.Sqrt(s)
Else
g = Math.Sqrt(s)
End If
h = f * g - s
sU(i - 1, l - 1) = f - g
For k = l To n
rv1(k - 1) = sU(i - 1, k - 1) / h
Next k
If i <> m Then
For j = l To m
s = 0.0#
For k = l To n
s = s + sU(j - 1, k - 1) * sU(i - 1, k - 1)
Next k
For k = l To n
sU(j - 1, k - 1) = sU(j - 1, k - 1) + s * rv1(k - 1)
Next k
Next j
End If