Skip to content

Commit 196af9b

Browse files
committed
Optimized code
1 parent f067dbc commit 196af9b

File tree

1 file changed

+22
-34
lines changed

1 file changed

+22
-34
lines changed

src/VBAexpressions.cls

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -950,7 +950,6 @@ Public Function ArrayFromString(ByRef strArray As String) As String()
950950
Dim StrLen As Long
951951
Dim tmpStr() As Variant
952952
Dim VectorEndPos As Long
953-
Dim VectorOpenFlag As Boolean
954953
Dim VectorStartPos As Long
955954

956955
StrCopy = ReconstructLiteralStrings(strArray, Join$(Split(strArray, d_Space), vbNullString))
@@ -5006,8 +5005,6 @@ Private Function LUsolve(ByRef expression As String, ByRef fName As String) As S
50065005
Dim mUB As Long
50075006
Dim Xarray() As String
50085007
Dim BArray() As Double
5009-
Dim pivotingIdx() As Long
5010-
Dim d As Double 'Interchages parity
50115008
Dim tmpElement As String
50125009

50135010
aArray() = ToDblArray(ArrayFromString(tmpData(LB)))
@@ -5016,8 +5013,7 @@ Private Function LUsolve(ByRef expression As String, ByRef fName As String) As S
50165013
mUB = UBound(aArray)
50175014

50185015
eqCount = mUB - mLB + 1
5019-
LUdecomp aArray, pivotingIdx, d 'LU decomposition
5020-
LUbkSub aArray, pivotingIdx, BArray 'Back substitution, returning in bArray
5016+
LUsolve_ aArray, BArray
50215017
If argsCount = 4 Then
50225018
Xarray() = ArrayFromString(tmpData(LB + 1))
50235019
includeNames = CBool(tmpData(UB))
@@ -5049,6 +5045,15 @@ err_handler:
50495045
d_lParenthesis & err.Description & d_rParenthesis
50505046
End Function
50515047

5048+
Private Function LUsolve_(ByRef aArray() As Double, ByRef BArray() As Double) As Double()
5049+
Dim pivotingIdx() As Long
5050+
Dim d As Double 'Interchages parity
5051+
5052+
LUdecomp aArray, pivotingIdx, d 'LU decomposition
5053+
LUbkSub aArray, pivotingIdx, BArray 'Back substitution, returning in bArray
5054+
LUsolve_ = BArray
5055+
End Function
5056+
50525057
Private Function Max(ByRef expression As String, ByRef fName As String) As Double
50535058
Dim g As Long
50545059
Dim tmpData() As String
@@ -5402,18 +5407,14 @@ err_handler:
54025407
End Function
54035408

54045409
Private Function MMULT_(ByRef A() As Double, ByRef b As Variant) As Variant
5405-
Dim ai As Long, ai2 As Long
5406-
Dim aIsNxMmatrix As Boolean
54075410
Dim aIsVector As Boolean
54085411
Dim aLB As Long, aLB2 As Long
54095412
Dim aUB As Long, aUB2 As Long
5410-
Dim bi As Long, bi2 As Long
5411-
Dim bIsNxMmatrix As Boolean
54125413
Dim bIsVector As Boolean
54135414
Dim bLB As Long, bLB2 As Long
54145415
Dim bUB As Long, bUB2 As Long
54155416
Dim c() As Double
5416-
Dim i As Long, ii As Long
5417+
Dim i As Long
54175418
Dim j As Long, jj As Long
54185419
Dim m As Long 'Columns of the output
54195420
Dim n As Long 'Rows of the output
@@ -6214,13 +6215,9 @@ Private Function PolyFit_(ByRef samplesArr() As Double, pDegree As Long, _
62146215
Dim mLB As Long
62156216
Dim mUB As Long
62166217
Dim Xarray() As Double
6217-
Dim strXArray As String
62186218
Dim BArray() As Double
6219-
Dim strBarray As String
62206219
Dim cnArray() As String
6221-
Dim SolverExpr As String
6222-
Dim SolverResult() As String
6223-
Dim FittedPolynomial As String
6220+
Dim SolverResult() As Double
62246221

62256222
mLB = LBound(samplesArr)
62266223
mUB = UBound(samplesArr)
@@ -6258,31 +6255,24 @@ Private Function PolyFit_(ByRef samplesArr() As Double, pDegree As Long, _
62586255
Next jCounter
62596256
Next iCounter
62606257
'Create B vector
6261-
ReDim BArray(0, 0 To pDegree)
6258+
ReDim BArray(0 To pDegree)
62626259
For iCounter = 0 To pDegree '2*n + 1 < i < 3*n
62636260
If iCounter = 0 Then 'Sum y
6264-
BArray(0, iCounter) = accumulatorArray(1)
6261+
BArray(iCounter) = accumulatorArray(1)
62656262
Else
6266-
BArray(0, iCounter) = accumulatorArray(iCounter + 2 * pDegree)
6263+
BArray(iCounter) = accumulatorArray(iCounter + 2 * pDegree)
62676264
End If
62686265
Next iCounter
62696266
'Create vector of coefficient names
62706267
ReDim cnArray(0, 0 To pDegree)
62716268
For iCounter = 0 To pDegree
62726269
cnArray(0, iCounter) = ToLiteralString(ChrW(97 + iCounter))
62736270
Next iCounter
6274-
'Get linear eq. system
6275-
strXArray = ArrayToString(Xarray)
6276-
strBarray = ArrayToString(BArray)
6277-
SolverExpr = strXArray & P_SEPARATORCHAR & _
6278-
ArrayToString(cnArray) & P_SEPARATORCHAR & _
6279-
strBarray & P_SEPARATORCHAR & "False"
6280-
'Solve
6281-
SolverResult = ArrayFromString(LUsolve(SolverExpr, "LUsolve"))
6271+
'Solve linear eq. system
6272+
SolverResult = LUsolve_(Xarray, BArray)
62826273
If stringOutput Then
62836274
'Format
6284-
FittedPolynomial = PolyString(SolverResult, pDegree)
6285-
PolyFit_ = FittedPolynomial
6275+
PolyFit_ = PolyString(SolverResult, pDegree)
62866276
Else
62876277
PolyFit_ = SolverResult
62886278
End If
@@ -6292,19 +6282,19 @@ End Function
62926282
''' Creates a polynomial string representation from solver coefficients
62936283
''' NOTE: PolyFit helper
62946284
''' </summary>
6295-
Private Function PolyString(ByRef SolverResult() As String, ByRef pDegree As Long) As String
6285+
Private Function PolyString(ByRef SolverResult() As Double, ByRef pDegree As Long) As String
62966286
Dim i As Long
62976287
Dim tmpResult As String
62986288

62996289
For i = 0 To pDegree
63006290
If i = 0 Then
6301-
tmpResult = CStr(Round(CDbl(SolverResult(i)), 4))
6291+
tmpResult = CStr(Round(SolverResult(i), 4))
63026292
Else
63036293
If CDbl(SolverResult(i)) > 0 Then
63046294
tmpResult = tmpResult & d_Space & op_plus _
6305-
& d_Space & Round(CDbl(SolverResult(i)), 4)
6295+
& d_Space & Round(SolverResult(i), 4)
63066296
Else
6307-
tmpResult = tmpResult & d_Space & Round(CDbl(SolverResult(i)), 4)
6297+
tmpResult = tmpResult & d_Space & Round(SolverResult(i), 4)
63086298
End If
63096299
If i > 1 Then
63106300
tmpResult = tmpResult & "*x^" & i
@@ -7823,9 +7813,7 @@ End Sub
78237813
''' <param name="tVector">The vertor to transform.</param>
78247814
Private Function VectorFormat(ByRef tVector As Variant) As Double()
78257815
Dim vLB As Long
7826-
Dim vLB2 As Long
78277816
Dim vUB As Long
7828-
Dim fromRowToCol As Boolean
78297817
Dim i As Long, j As Long
78307818
Dim tmpResult() As Double
78317819

0 commit comments

Comments
 (0)