Skip to content

Commit d0583cb

Browse files
committed
Sealing providers
1 parent a1293d7 commit d0583cb

32 files changed

Lines changed: 2903 additions & 527 deletions

MathNet.Numerics.sln.DotSettings

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
5454
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
5555
OTHER DEALINGS IN THE SOFTWARE.
5656
&lt;/copyright&gt;</s:String>
57+
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=BLAS/@EntryIndexedValue">BLAS</s:String>
5758
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=CDF/@EntryIndexedValue">CDF</s:String>
59+
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=CUDA/@EntryIndexedValue">CUDA</s:String>
5860
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=DFT/@EntryIndexedValue">DFT</s:String>
5961
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=FFT/@EntryIndexedValue">FFT</s:String>
6062
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=ILU/@EntryIndexedValue">ILU</s:String>

src/Numerics.Tests/IntegralTransformsTests/MatchingReferenceTransformTest.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// http://numerics.mathdotnet.com
44
// http://github.com/mathnet/mathnet-numerics
55
//
6-
// Copyright (c) 2009-2018 Math.NET
6+
// Copyright (c) 2009-2021 Math.NET
77
//
88
// Permission is hereby granted, free of charge, to any person
99
// obtaining a copy of this software and associated documentation
@@ -321,45 +321,45 @@ public void ProviderMatchesManagedProviderPowerOfTwoLarge32()
321321
{
322322
// 65536 = 2^16
323323
var samples = Generate.RandomComplex32(65536, GetUniform(1));
324-
Verify(samples, 5, FourierTransformScaling.NoScaling, FourierTransformControl.CreateManaged().Forward, FourierTransformControl.Provider.Forward);
324+
Verify(samples, 5, FourierTransformScaling.NoScaling, ManagedFourierTransformProvider.Instance.Forward, FourierTransformControl.Provider.Forward);
325325
}
326326

327327
[Test]
328328
public void ProviderMatchesManagedProviderPowerOfTwoLarge64()
329329
{
330330
// 65536 = 2^16
331331
var samples = Generate.RandomComplex(65536, GetUniform(1));
332-
Verify(samples, 10, FourierTransformScaling.NoScaling, FourierTransformControl.CreateManaged().Forward, FourierTransformControl.Provider.Forward);
332+
Verify(samples, 10, FourierTransformScaling.NoScaling, ManagedFourierTransformProvider.Instance.Forward, FourierTransformControl.Provider.Forward);
333333
}
334334

335335
[Test]
336336
public void ProviderMatchesManagedProviderArbitraryLarge32()
337337
{
338338
// 30870 = 2*3*3*5*7*7*7
339339
var samples = Generate.RandomComplex32(30870, GetUniform(1));
340-
Verify(samples, 5, FourierTransformScaling.NoScaling, FourierTransformControl.CreateManaged().Forward, FourierTransformControl.Provider.Forward);
340+
Verify(samples, 5, FourierTransformScaling.NoScaling, ManagedFourierTransformProvider.Instance.Forward, FourierTransformControl.Provider.Forward);
341341
}
342342

343343
[Test]
344344
public void ProviderMatchesManagedProviderArbitraryLarge64()
345345
{
346346
// 30870 = 2*3*3*5*7*7*7
347347
var samples = Generate.RandomComplex(30870, GetUniform(1));
348-
Verify(samples, 10, FourierTransformScaling.NoScaling, FourierTransformControl.CreateManaged().Forward, FourierTransformControl.Provider.Forward);
348+
Verify(samples, 10, FourierTransformScaling.NoScaling, ManagedFourierTransformProvider.Instance.Forward, FourierTransformControl.Provider.Forward);
349349
}
350350

351351
[Test]
352352
public void ProviderMatchesManagedProviderArbitraryLarge32_GH286()
353353
{
354354
var samples = Generate.RandomComplex32(46500, GetUniform(1));
355-
Verify(samples, 5, FourierTransformScaling.NoScaling, FourierTransformControl.CreateManaged().Forward, FourierTransformControl.Provider.Forward);
355+
Verify(samples, 5, FourierTransformScaling.NoScaling, ManagedFourierTransformProvider.Instance.Forward, FourierTransformControl.Provider.Forward);
356356
}
357357

358358
[Test]
359359
public void ProviderMatchesManagedProviderArbitraryLarge64_GH286()
360360
{
361361
var samples = Generate.RandomComplex(46500, GetUniform(1));
362-
Verify(samples, 10, FourierTransformScaling.NoScaling, FourierTransformControl.CreateManaged().Forward, FourierTransformControl.Provider.Forward);
362+
Verify(samples, 10, FourierTransformScaling.NoScaling, ManagedFourierTransformProvider.Instance.Forward, FourierTransformControl.Provider.Forward);
363363
}
364364

365365
[Test, Explicit("Long-Running")]

src/Numerics/Providers/FourierTransform/FourierTransformControl.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,7 @@ public static IFourierTransformProvider Provider
7979
}
8080
}
8181

82-
public static IFourierTransformProvider CreateManaged() => new ManagedFourierTransformProvider();
83-
public static void UseManaged() => Provider = CreateManaged();
82+
public static void UseManaged() => Provider = ManagedFourierTransformProvider.Instance;
8483

8584
public static void UseNativeMKL() => Provider = MklProbe.Create();
8685
public static bool TryUseNativeMKL() => TryUse(MklProbe.TryCreate());

src/Numerics/Providers/FourierTransform/ManagedFourierTransformProvider.Bluestein.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Math.NET Numerics, part of the Math.NET Project
33
// https://numerics.mathdotnet.com
44
//
5-
// Copyright (c) 2009-2018 Math.NET
5+
// Copyright (c) 2009-2021 Math.NET
66
//
77
// Permission is hereby granted, free of charge, to any person
88
// obtaining a copy of this software and associated documentation
@@ -32,7 +32,7 @@
3232

3333
namespace MathNet.Numerics.Providers.FourierTransform
3434
{
35-
internal partial class ManagedFourierTransformProvider
35+
public partial class ManagedFourierTransformProvider
3636
{
3737
/// <summary>
3838
/// Sequences with length greater than Math.Sqrt(Int32.MaxValue) + 1
@@ -45,7 +45,7 @@ internal partial class ManagedFourierTransformProvider
4545
/// </summary>
4646
/// <param name="n">Number of samples.</param>
4747
/// <returns>Bluestein sequence exp(I*Pi*k^2/N)</returns>
48-
private static Complex32[] BluesteinSequence32(int n)
48+
static Complex32[] BluesteinSequence32(int n)
4949
{
5050
double s = Constants.Pi / n;
5151
var sequence = new Complex32[n];
@@ -77,7 +77,7 @@ private static Complex32[] BluesteinSequence32(int n)
7777
/// </summary>
7878
/// <param name="n">Number of samples.</param>
7979
/// <returns>Bluestein sequence exp(I*Pi*k^2/N)</returns>
80-
private static Complex[] BluesteinSequence(int n)
80+
static Complex[] BluesteinSequence(int n)
8181
{
8282
double s = Constants.Pi / n;
8383
var sequence = new Complex[n];
@@ -108,7 +108,7 @@ private static Complex[] BluesteinSequence(int n)
108108
/// Convolution with the bluestein sequence (Parallel Version).
109109
/// </summary>
110110
/// <param name="samples">Sample Vector.</param>
111-
private static void BluesteinConvolutionParallel(Complex32[] samples)
111+
static void BluesteinConvolutionParallel(Complex32[] samples)
112112
{
113113
int n = samples.Length;
114114
Complex32[] sequence = BluesteinSequence32(n);
@@ -163,7 +163,7 @@ private static void BluesteinConvolutionParallel(Complex32[] samples)
163163
/// Convolution with the bluestein sequence (Parallel Version).
164164
/// </summary>
165165
/// <param name="samples">Sample Vector.</param>
166-
private static void BluesteinConvolutionParallel(Complex[] samples)
166+
static void BluesteinConvolutionParallel(Complex[] samples)
167167
{
168168
int n = samples.Length;
169169
Complex[] sequence = BluesteinSequence(n);
@@ -218,7 +218,7 @@ private static void BluesteinConvolutionParallel(Complex[] samples)
218218
/// Swap the real and imaginary parts of each sample.
219219
/// </summary>
220220
/// <param name="samples">Sample Vector.</param>
221-
private static void SwapRealImaginary(Complex32[] samples)
221+
static void SwapRealImaginary(Complex32[] samples)
222222
{
223223
for (int i = 0; i < samples.Length; i++)
224224
{
@@ -230,7 +230,7 @@ private static void SwapRealImaginary(Complex32[] samples)
230230
/// Swap the real and imaginary parts of each sample.
231231
/// </summary>
232232
/// <param name="samples">Sample Vector.</param>
233-
private static void SwapRealImaginary(Complex[] samples)
233+
static void SwapRealImaginary(Complex[] samples)
234234
{
235235
for (int i = 0; i < samples.Length; i++)
236236
{
@@ -241,15 +241,15 @@ private static void SwapRealImaginary(Complex[] samples)
241241
/// <summary>
242242
/// Bluestein generic FFT for arbitrary sized sample vectors.
243243
/// </summary>
244-
private static void BluesteinForward(Complex[] samples)
244+
static void BluesteinForward(Complex[] samples)
245245
{
246246
BluesteinConvolutionParallel(samples);
247247
}
248248

249249
/// <summary>
250250
/// Bluestein generic FFT for arbitrary sized sample vectors.
251251
/// </summary>
252-
private static void BluesteinInverse(Complex[] spectrum)
252+
static void BluesteinInverse(Complex[] spectrum)
253253
{
254254
SwapRealImaginary(spectrum);
255255
BluesteinConvolutionParallel(spectrum);
@@ -259,15 +259,15 @@ private static void BluesteinInverse(Complex[] spectrum)
259259
/// <summary>
260260
/// Bluestein generic FFT for arbitrary sized sample vectors.
261261
/// </summary>
262-
private static void BluesteinForward(Complex32[] samples)
262+
static void BluesteinForward(Complex32[] samples)
263263
{
264264
BluesteinConvolutionParallel(samples);
265265
}
266266

267267
/// <summary>
268268
/// Bluestein generic FFT for arbitrary sized sample vectors.
269269
/// </summary>
270-
private static void BluesteinInverse(Complex32[] spectrum)
270+
static void BluesteinInverse(Complex32[] spectrum)
271271
{
272272
SwapRealImaginary(spectrum);
273273
BluesteinConvolutionParallel(spectrum);

src/Numerics/Providers/FourierTransform/ManagedFourierTransformProvider.Radix2.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Math.NET Numerics, part of the Math.NET Project
33
// https://numerics.mathdotnet.com
44
//
5-
// Copyright (c) 2009-2018 Math.NET
5+
// Copyright (c) 2009-2021 Math.NET
66
//
77
// Permission is hereby granted, free of charge, to any person
88
// obtaining a copy of this software and associated documentation
@@ -33,14 +33,14 @@
3333

3434
namespace MathNet.Numerics.Providers.FourierTransform
3535
{
36-
internal partial class ManagedFourierTransformProvider
36+
public partial class ManagedFourierTransformProvider
3737
{
3838
/// <summary>
3939
/// Radix-2 Reorder Helper Method
4040
/// </summary>
4141
/// <typeparam name="T">Sample type</typeparam>
4242
/// <param name="samples">Sample vector</param>
43-
private static void Radix2Reorder<T>(T[] samples)
43+
static void Radix2Reorder<T>(T[] samples)
4444
{
4545
var j = 0;
4646
for (var i = 0; i < samples.Length - 1; i++)
@@ -73,7 +73,7 @@ private static void Radix2Reorder<T>(T[] samples)
7373
#if !NET40
7474
[MethodImpl(MethodImplOptions.AggressiveInlining)]
7575
#endif
76-
private static void Radix2Step(Complex32[] samples, int exponentSign, int levelSize, int k)
76+
static void Radix2Step(Complex32[] samples, int exponentSign, int levelSize, int k)
7777
{
7878
// Twiddle Factor
7979
var exponent = (exponentSign * k) * Constants.Pi / levelSize;
@@ -99,7 +99,7 @@ private static void Radix2Step(Complex32[] samples, int exponentSign, int levelS
9999
#if !NET40
100100
[MethodImpl(MethodImplOptions.AggressiveInlining)]
101101
#endif
102-
private static void Radix2Step(Complex[] samples, int exponentSign, int levelSize, int k)
102+
static void Radix2Step(Complex[] samples, int exponentSign, int levelSize, int k)
103103
{
104104
// Twiddle Factor
105105
var exponent = (exponentSign * k) * Constants.Pi / levelSize;
@@ -118,7 +118,7 @@ private static void Radix2Step(Complex[] samples, int exponentSign, int levelSiz
118118
/// <summary>
119119
/// Radix-2 generic FFT for power-of-two sized sample vectors.
120120
/// </summary>
121-
private static void Radix2Forward(Complex32[] data)
121+
static void Radix2Forward(Complex32[] data)
122122
{
123123
Radix2Reorder(data);
124124
for (var levelSize = 1; levelSize < data.Length; levelSize *= 2)
@@ -133,7 +133,7 @@ private static void Radix2Forward(Complex32[] data)
133133
/// <summary>
134134
/// Radix-2 generic FFT for power-of-two sized sample vectors.
135135
/// </summary>
136-
private static void Radix2Forward(Complex[] data)
136+
static void Radix2Forward(Complex[] data)
137137
{
138138
Radix2Reorder(data);
139139
for (var levelSize = 1; levelSize < data.Length; levelSize *= 2)
@@ -148,7 +148,7 @@ private static void Radix2Forward(Complex[] data)
148148
/// <summary>
149149
/// Radix-2 generic FFT for power-of-two sized sample vectors.
150150
/// </summary>
151-
private static void Radix2Inverse(Complex32[] data)
151+
static void Radix2Inverse(Complex32[] data)
152152
{
153153
Radix2Reorder(data);
154154
for (var levelSize = 1; levelSize < data.Length; levelSize *= 2)
@@ -163,7 +163,7 @@ private static void Radix2Inverse(Complex32[] data)
163163
/// <summary>
164164
/// Radix-2 generic FFT for power-of-two sized sample vectors.
165165
/// </summary>
166-
private static void Radix2Inverse(Complex[] data)
166+
static void Radix2Inverse(Complex[] data)
167167
{
168168
Radix2Reorder(data);
169169
for (var levelSize = 1; levelSize < data.Length; levelSize *= 2)
@@ -178,7 +178,7 @@ private static void Radix2Inverse(Complex[] data)
178178
/// <summary>
179179
/// Radix-2 generic FFT for power-of-two sample vectors (Parallel Version).
180180
/// </summary>
181-
private static void Radix2ForwardParallel(Complex32[] data)
181+
static void Radix2ForwardParallel(Complex32[] data)
182182
{
183183
Radix2Reorder(data);
184184
for (var levelSize = 1; levelSize < data.Length; levelSize *= 2)
@@ -198,7 +198,7 @@ private static void Radix2ForwardParallel(Complex32[] data)
198198
/// <summary>
199199
/// Radix-2 generic FFT for power-of-two sample vectors (Parallel Version).
200200
/// </summary>
201-
private static void Radix2ForwardParallel(Complex[] data)
201+
static void Radix2ForwardParallel(Complex[] data)
202202
{
203203
Radix2Reorder(data);
204204
for (var levelSize = 1; levelSize < data.Length; levelSize *= 2)
@@ -218,7 +218,7 @@ private static void Radix2ForwardParallel(Complex[] data)
218218
/// <summary>
219219
/// Radix-2 generic FFT for power-of-two sample vectors (Parallel Version).
220220
/// </summary>
221-
private static void Radix2InverseParallel(Complex32[] data)
221+
static void Radix2InverseParallel(Complex32[] data)
222222
{
223223
Radix2Reorder(data);
224224
for (var levelSize = 1; levelSize < data.Length; levelSize *= 2)
@@ -238,7 +238,7 @@ private static void Radix2InverseParallel(Complex32[] data)
238238
/// <summary>
239239
/// Radix-2 generic FFT for power-of-two sample vectors (Parallel Version).
240240
/// </summary>
241-
private static void Radix2InverseParallel(Complex[] data)
241+
static void Radix2InverseParallel(Complex[] data)
242242
{
243243
Radix2Reorder(data);
244244
for (var levelSize = 1; levelSize < data.Length; levelSize *= 2)
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// <copyright file="ManagedFourierTransformProvider.Scaling.cs" company="Math.NET">
2+
// Math.NET Numerics, part of the Math.NET Project
3+
// https://numerics.mathdotnet.com
4+
//
5+
// Copyright (c) 2009-2021 Math.NET
6+
//
7+
// Permission is hereby granted, free of charge, to any person
8+
// obtaining a copy of this software and associated documentation
9+
// files (the "Software"), to deal in the Software without
10+
// restriction, including without limitation the rights to use,
11+
// copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
// copies of the Software, and to permit persons to whom the
13+
// Software is furnished to do so, subject to the following
14+
// conditions:
15+
//
16+
// The above copyright notice and this permission notice shall be
17+
// included in all copies or substantial portions of the Software.
18+
//
19+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20+
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
21+
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22+
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
23+
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
24+
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25+
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
26+
// OTHER DEALINGS IN THE SOFTWARE.
27+
// </copyright>
28+
29+
using System;
30+
using Complex = System.Numerics.Complex;
31+
32+
namespace MathNet.Numerics.Providers.FourierTransform
33+
{
34+
public partial class ManagedFourierTransformProvider
35+
{
36+
37+
38+
/// <summary>
39+
/// Fully rescale the FFT result.
40+
/// </summary>
41+
/// <param name="samples">Sample Vector.</param>
42+
static void FullRescale(Complex32[] samples)
43+
{
44+
var scalingFactor = (float)1.0 / samples.Length;
45+
for (int i = 0; i < samples.Length; i++)
46+
{
47+
samples[i] *= scalingFactor;
48+
}
49+
}
50+
51+
/// <summary>
52+
/// Fully rescale the FFT result.
53+
/// </summary>
54+
/// <param name="samples">Sample Vector.</param>
55+
static void FullRescale(Complex[] samples)
56+
{
57+
var scalingFactor = 1.0 / samples.Length;
58+
for (int i = 0; i < samples.Length; i++)
59+
{
60+
samples[i] *= scalingFactor;
61+
}
62+
}
63+
64+
/// <summary>
65+
/// Half rescale the FFT result (e.g. for symmetric transforms).
66+
/// </summary>
67+
/// <param name="samples">Sample Vector.</param>
68+
static void HalfRescale(Complex32[] samples)
69+
{
70+
var scalingFactor = (float)Math.Sqrt(1.0 / samples.Length);
71+
for (int i = 0; i < samples.Length; i++)
72+
{
73+
samples[i] *= scalingFactor;
74+
}
75+
}
76+
77+
/// <summary>
78+
/// Fully rescale the FFT result (e.g. for symmetric transforms).
79+
/// </summary>
80+
/// <param name="samples">Sample Vector.</param>
81+
static void HalfRescale(Complex[] samples)
82+
{
83+
var scalingFactor = Math.Sqrt(1.0 / samples.Length);
84+
for (int i = 0; i < samples.Length; i++)
85+
{
86+
samples[i] *= scalingFactor;
87+
}
88+
}
89+
}
90+
}

0 commit comments

Comments
 (0)