| 1 |
#Region "Microsoft.VisualBasic::b6b463990d38f7ac9a00f014387bd5d7, Microsoft.VisualBasic.Core\Extensions\Math\ScaleMaps.vb"
|
| 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 |
#End Region
|
| 43 |
|
| 44 |
Imports System.Runtime.CompilerServices
|
| 45 |
Imports Microsoft.VisualBasic.CommandLine.Reflection
|
| 46 |
Imports Microsoft.VisualBasic.ComponentModel.Collection.Generic
|
| 47 |
Imports Microsoft.VisualBasic.Language
|
| 48 |
Imports Microsoft.VisualBasic.Linq
|
| 49 |
Imports Microsoft.VisualBasic.Linq.Extensions
|
| 50 |
Imports Microsoft.VisualBasic.Scripting.MetaData
|
| 51 |
Imports sys = System.Math
|
| 52 |
|
| 53 |
Namespace Math
|
| 54 |
|
| 55 |
<Package("ScaleMaps",
|
| 56 |
Category:=APICategories.UtilityTools,
|
| 57 |
Publisher:="xie.guigang@live.com")>
|
| 58 |
Public Module ScaleMaps
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
<Extension> Public Function TrimRanges(Dbl As Double(), min As Double, max As Double) As Double()
|
| 71 |
If Dbl.IsNullOrEmpty Then
|
| 72 |
Return New Double() {}
|
| 73 |
End If
|
| 74 |
|
| 75 |
For i As Integer = 0 To Dbl.Length - 1
|
| 76 |
Dim n As Double = Dbl(i)
|
| 77 |
|
| 78 |
If n < min Then
|
| 79 |
n = min
|
| 80 |
ElseIf n > max Then
|
| 81 |
n = max
|
| 82 |
End If
|
| 83 |
|
| 84 |
Dbl(i) = n
|
| 85 |
Next
|
| 86 |
|
| 87 |
Return Dbl
|
| 88 |
End Function
|
| 89 |
|
| 90 |
<ExportAPI("Ranks.Mapping")>
|
| 91 |
<Extension> Public Function GenerateMapping(Of T As INamedValue)(
|
| 92 |
data As IEnumerable(Of T),
|
| 93 |
getSample As Func(Of T, Double),
|
| 94 |
Optional Level As Integer = 10) As Dictionary(Of String, Integer)
|
| 95 |
|
| 96 |
Dim samples As Double() = data.Select(getSample).ToArray
|
| 97 |
Dim levels As Integer() = samples.GenerateMapping(Level)
|
| 98 |
Dim hash = data _
|
| 99 |
.SeqIterator _
|
| 100 |
.Select(Function(x) (x.value.Key, levels(x.i))) _
|
| 101 |
.ToArray
|
| 102 |
|
| 103 |
Return hash.ToDictionary(
|
| 104 |
Function(tp) tp.Item1,
|
| 105 |
Function(tp) tp.Item2)
|
| 106 |
End Function
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
<ExportAPI("Ranks.Mapping")>
|
| 124 |
<Extension> Public Function GenerateMapping(data As IEnumerable(Of Double), Optional Level As Integer = 10, Optional offset As Integer = 1) As Integer()
|
| 125 |
Dim array As Double() = data.ToArray
|
| 126 |
|
| 127 |
If array.Length = 0 Then
|
| 128 |
Return {}
|
| 129 |
End If
|
| 130 |
|
| 131 |
Dim MinValue As Double = array.Min
|
| 132 |
Dim MaxValue As Double = array.Max
|
| 133 |
Dim d As Double = MaxValue - MinValue
|
| 134 |
|
| 135 |
If d = 0R Then
|
| 136 |
Return 1.Repeats(array.Length)
|
| 137 |
End If
|
| 138 |
|
| 139 |
Dim chunkBuf As Integer() = New Integer(array.Length - 1) {}
|
| 140 |
Dim i As int = 0
|
| 141 |
|
| 142 |
For Each x As Double In array
|
| 143 |
Dim lv As Integer = Fix(Level * (x - MinValue) / d)
|
| 144 |
chunkBuf(++i) = lv + offset
|
| 145 |
Next
|
| 146 |
|
| 147 |
Return chunkBuf
|
| 148 |
End Function
|
| 149 |
|
| 150 |
<Extension>
|
| 151 |
Public Function LogLevels(data As IEnumerable(Of Double), base%, Optional level As Integer = 100) As Integer()
|
| 152 |
Dim logvalues = data.Select(Function(x) sys.Log(x, base)).ToArray
|
| 153 |
Return logvalues.GenerateMapping(level)
|
| 154 |
End Function
|
| 155 |
|
| 156 |
<ExportAPI("Ranks.Log2")>
|
| 157 |
<Extension> Public Function Log2Ranks(data As IEnumerable(Of Double), Optional Level As Integer = 100) As Integer()
|
| 158 |
Dim log2Value = data.Select(Function(x) sys.Log(x, 2)).ToArray
|
| 159 |
Return log2Value.GenerateMapping(Level)
|
| 160 |
End Function
|
| 161 |
|
| 162 |
<ExportAPI("Ranks.Log2")>
|
| 163 |
<Extension> Public Function Log2Ranks(data As IEnumerable(Of Integer), Optional Level As Integer = 10) As Integer()
|
| 164 |
Return data.Select(Function(d) CDbl(d)).Log2Ranks
|
| 165 |
End Function
|
| 166 |
|
| 167 |
<ExportAPI("Ranks.Log2")>
|
| 168 |
<Extension> Public Function Log2Ranks(data As IEnumerable(Of Long), Optional Level As Integer = 10) As Integer()
|
| 169 |
Return data.Select(Function(d) CDbl(d)).Log2Ranks
|
| 170 |
End Function
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
<ExportAPI("Ranks.Mapping")>
|
| 180 |
<Extension> Public Function GenerateMapping(data As IEnumerable(Of Integer), Optional Level As Integer = 10, Optional offset% = 1) As Integer()
|
| 181 |
Return GenerateMapping((From n In data Select CDbl(n)).ToArray, Level, offset)
|
| 182 |
End Function
|
| 183 |
|
| 184 |
<ExportAPI("Ranks.Mapping")>
|
| 185 |
<Extension> Public Function GenerateMapping(data As IEnumerable(Of Long), Optional Level As Integer = 10) As Integer()
|
| 186 |
Return GenerateMapping((From n In data Select CDbl(n)).ToArray, Level)
|
| 187 |
End Function
|
| 188 |
|
| 189 |
|
| 190 |
Function centers and/or scales the columns of a numeric matrix.
|
| 191 |
|
| 192 |
<param name="data">numeric matrix</param>
|
| 193 |
<param name="center">either a logical value or a numeric vector of length equal to the number of columns of x</param>
|
| 194 |
<param name="isScale">either a logical value or a numeric vector of length equal to the number of columns of x</param>
|
| 195 |
|
| 196 |
<ExportAPI("Scale", Info:="function centers and/or scales the columns of a numeric matrix.")>
|
| 197 |
Public Function Scale(<Parameter("x", "numeric matrix")> data As IEnumerable(Of Double),
|
| 198 |
<Parameter("center", "either a logical value or a numeric vector of length equal to the number of columns of x")>
|
| 199 |
Optional center As Boolean = True,
|
| 200 |
<Parameter("scale", "either a logical value or a numeric vector of length equal to the number of columns of x")>
|
| 201 |
Optional isScale As Boolean = True) As Double()
|
| 202 |
|
| 203 |
Dim avg As Double = data.Average
|
| 204 |
Dim rms As Double = VBMath.RMS(data)
|
| 205 |
|
| 206 |
If center Then
|
| 207 |
data = (From n In data Select n - avg).ToArray
|
| 208 |
End If
|
| 209 |
|
| 210 |
If isScale Then
|
| 211 |
data = (From n In data Select n / rms).ToArray
|
| 212 |
End If
|
| 213 |
|
| 214 |
Return data.ToArray
|
| 215 |
End Function
|
| 216 |
|
| 217 |
<Extension>
|
| 218 |
Public Function MapHelper(Of T)(data As IEnumerable(Of T)) As Func(Of Integer, T)
|
| 219 |
Dim source As T() = data.ToArray
|
| 220 |
|
| 221 |
Return Function(level%)
|
| 222 |
If level < 0 Then
|
| 223 |
level = 0
|
| 224 |
ElseIf level >= source.Length Then
|
| 225 |
level = source.Length - 1
|
| 226 |
End If
|
| 227 |
|
| 228 |
Return source(level)
|
| 229 |
End Function
|
| 230 |
End Function
|
| 231 |
End Module
|
| 232 |
End Namespace
|