Skip to content

Commit

Permalink
truncate the range
Browse files Browse the repository at this point in the history
  • Loading branch information
xieguigang committed Jan 3, 2024
1 parent acb468d commit 9745b29
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
11 changes: 10 additions & 1 deletion Data_science/Mathematica/Math/DataFittings/GaussNewtonSolver.vb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ Public Class GaussNewtonSolver
Dim rmse = residuals.RMS
Dim temp_rmse As Double
Dim success As Boolean = False
Dim truncate As Double = 100

For i As Integer = 0 To MAX_ITERATIONS - 1
Dim lJ = CalcJacobian(data, beta)
Expand All @@ -69,7 +70,7 @@ Public Class GaussNewtonSolver
' the internal LU decomposition has bug
' bigJ = bigJ.Inverse()
' use the LU decompositon function inside current module
Invert(bigJ, tol:=0).Set(success, bigJ)
Invert(bigJ, tol:=0, truncate:=truncate).Set(success, bigJ)

If Not success Then
Exit For
Expand All @@ -78,6 +79,14 @@ Public Class GaussNewtonSolver
beta -= bigJ * rB
End If

For offset As Integer = 0 To beta.ColumnDimension - 1
If beta(offset, 0) > truncate Then
beta(offset, 0) = truncate
ElseIf beta(offset, 0) < -truncate Then
beta(offset, 0) = -truncate
End If
Next

residuals = CalcResiduals(data, beta)

For j As Integer = 0 To residuals.Length - 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ Namespace EmGaussian
Dim opts As Opts
Dim peaks As Variable()
Dim kernel As KernelFunction = AddressOf Variable.Multi_gaussian
Dim sine_kernel As Boolean

Public Delegate Function KernelFunction(x As Double, peaks As Variable(), offset As Double) As Double

Sub New(opts As Opts, Optional sine_kernel As Boolean = False)
Me.sine_kernel = sine_kernel
Me.opts = opts

If sine_kernel Then
Expand All @@ -37,13 +39,14 @@ Namespace EmGaussian
End Function

Public Function fit(x As Double(), samples As Double(), Optional npeaks As Integer = 6) As Variable()
Dim ymax As Double = samples.Max
Dim random As Variable() = Enumerable.Range(0, npeaks) _
.Select(Function(v, i)
Return New Variable With {
.height = samples.Max / 10000,
.height = ymax * randf(0.5, 1.1),
.center = x((i / npeaks) * (x.Length - 1)),
.width = 0.000005,
.offset = 0.0001
.width = randf(0.01, 0.1),
.offset = 0.01
}
End Function) _
.ToArray
Expand Down

0 comments on commit 9745b29

Please sign in to comment.