Skip to content

Commit b773b94

Browse files
author
Oren (electricessence)
committed
Updated to 2.1.1
1 parent cb0da2e commit b773b94

3 files changed

Lines changed: 115 additions & 66 deletions

File tree

Open.Numeric.csproj

Lines changed: 47 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,51 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

3-
<PropertyGroup>
4-
<TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks>
5-
<Authors>electricessence</Authors>
6-
<Company />
7-
<Product />
8-
<Description>Extensions for simplifying working with numbers.
9-
Includes precision extensions for floating point numbers.
10-
11-
Part of the "Open" set of libraries.</Description>
12-
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
13-
<PackageLicenseUrl></PackageLicenseUrl>
14-
<Copyright>https://github.com/electricessence/Open.Numeric/blob/master/LISCENSE.md</Copyright>
15-
<RepositoryUrl>https://github.com/electricessence/Open.Numeric/</RepositoryUrl>
16-
<PackageProjectUrl>https://github.com/electricessence/Open.Numeric/</PackageProjectUrl>
17-
<RepositoryType>git</RepositoryType>
18-
<PackageTags>dotnet, dotnetcore, cs, numbers, double, float, precision, extensions</PackageTags>
19-
<Version>2.0.1</Version>
20-
<AssemblyVersion>2.0.1.0</AssemblyVersion>
21-
<FileVersion>2.0.1.0</FileVersion>
22-
<PackageReleaseNotes>Added .NET Standard 2.1 support.</PackageReleaseNotes>
23-
<PackageLicenseExpression>MIT</PackageLicenseExpression>
24-
</PropertyGroup>
25-
26-
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
27-
<Optimize>True</Optimize>
28-
<LangVersion>latest</LangVersion>
29-
</PropertyGroup>
30-
31-
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
32-
<LangVersion>latest</LangVersion>
33-
</PropertyGroup>
34-
35-
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
36-
<PackageReference Include="System.Memory" Version="4.5.3" />
37-
</ItemGroup>
38-
39-
<ItemGroup>
40-
<None Remove=".git" />
41-
<None Remove=".gitignore" />
42-
<None Remove="LISCENSE.md" />
43-
<None Remove="README.md" />
44-
</ItemGroup>
3+
<PropertyGroup>
4+
<TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks>
5+
<Authors>electricessence</Authors>
6+
<Company />
7+
<Product />
8+
<Description>
9+
Extensions for simplifying working with numbers.
10+
Includes precision extensions for floating point numbers.
11+
12+
Part of the "Open" set of libraries.
13+
</Description>
14+
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
15+
<PackageLicenseUrl></PackageLicenseUrl>
16+
<Copyright>https://github.com/electricessence/Open.Numeric/blob/master/LISCENSE.md</Copyright>
17+
<RepositoryUrl>https://github.com/electricessence/Open.Numeric/</RepositoryUrl>
18+
<PackageProjectUrl>https://github.com/electricessence/Open.Numeric/</PackageProjectUrl>
19+
<RepositoryType>git</RepositoryType>
20+
<PackageTags>dotnet, dotnetcore, cs, numbers, double, float, precision, extensions</PackageTags>
21+
<Version>2.1.1</Version>
22+
<PackageReleaseNotes>Added .NET Standard 2.1 support.</PackageReleaseNotes>
23+
<PackageLicenseExpression>MIT</PackageLicenseExpression>
24+
<Nullable>enable</Nullable>
25+
</PropertyGroup>
26+
27+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
28+
<Optimize>True</Optimize>
29+
<LangVersion>latest</LangVersion>
30+
</PropertyGroup>
31+
32+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
33+
<LangVersion>latest</LangVersion>
34+
</PropertyGroup>
35+
36+
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
37+
<PackageReference Include="System.Memory" Version="4.5.3" />
38+
</ItemGroup>
39+
40+
<ItemGroup>
41+
<None Remove=".git" />
42+
<None Remove=".gitignore" />
43+
<None Remove="LISCENSE.md" />
44+
<None Remove="README.md" />
45+
</ItemGroup>
46+
47+
<ItemGroup>
48+
<PackageReference Include="System.Collections.Immutable" Version="1.7.0" />
49+
</ItemGroup>
4550

4651
</Project>

Precision.cs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -163,17 +163,13 @@ public static bool IsNearEqual(this IComparable a, IComparable b, IComparable to
163163
if (a.Equals(b))
164164
return true;
165165

166-
switch (a)
166+
return a switch
167167
{
168-
case float f:
169-
return IsNearEqual(f, (float)b, (float)tolerance);
170-
case double d:
171-
return IsNearEqual(d, (double)b, (double)tolerance);
172-
case decimal @decimal:
173-
return IsNearEqual(@decimal, (decimal)b, (decimal)tolerance);
174-
}
175-
176-
throw new InvalidCastException();
168+
float f => IsNearEqual(f, (float)b, (float)tolerance),
169+
double d => IsNearEqual(d, (double)b, (double)tolerance),
170+
decimal @decimal => IsNearEqual(@decimal, (decimal)b, (decimal)tolerance),
171+
_ => throw new InvalidCastException(),
172+
};
177173
}
178174

179175

ProcedureResults.cs

Lines changed: 62 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,87 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
4+
using System.Collections.Immutable;
35

46
namespace Open.Numeric
57
{
6-
public class ProcedureResults : IProcedureResult<ReadOnlyMemory<double>>
8+
public class ProcedureResults : IProcedureResult<ImmutableArray<double>>
79
{
810
public int Count { get; }
9-
public ReadOnlyMemory<double> Sum { get; }
10-
public ReadOnlyMemory<double> Average { get; }
11-
public ProcedureResults(in ReadOnlySpan<double> sum, int count)
11+
public ImmutableArray<double> Sum { get; }
12+
public ImmutableArray<double> Average { get; }
13+
14+
public static ProcedureResults Empty { get; }
15+
= new ProcedureResults();
16+
17+
protected ProcedureResults()
1218
{
19+
Count = 0;
20+
Sum = ImmutableArray<double>.Empty;
21+
Average = ImmutableArray<double>.Empty;
22+
}
23+
24+
public ProcedureResults(ImmutableArray<double> sum, int count)
25+
{
26+
Sum = sum;
1327
Count = count;
14-
var s = sum.ToArray();
15-
Sum = s;
16-
Average = s.Select(v => count == 0 ? double.NaN : v / count).ToArray();
28+
Average = sum.Select(v => count == 0 ? double.NaN : v / count).ToImmutableArray();
29+
}
30+
31+
public ProcedureResults(IEnumerable<double> sum, int count)
32+
:this(sum is ImmutableArray<double> s ? s : sum.ToImmutableArray(), count)
33+
{
34+
}
35+
36+
37+
public ProcedureResults(in ReadOnlySpan<double> sum, int count)
38+
: this(ToImmutableArray(in sum), count)
39+
{
1740
}
1841

19-
static double[] SumValues(in ReadOnlySpan<double> a, in ReadOnlySpan<double> b)
42+
static ImmutableArray<T> ToImmutableArray<T>(in ReadOnlySpan<T> source)
2043
{
21-
if (a.Length != b.Length)
44+
var len = source.Length;
45+
var builder = ImmutableArray.CreateBuilder<T>(len);
46+
for (var i = 0; i < len; i++)
47+
builder[i] = source[i];
48+
return builder.MoveToImmutable();
49+
}
50+
51+
static ImmutableArray<double> SumValues(IList<double> a, IList<double> b)
52+
{
53+
var len = a.Count;
54+
if (len != b.Count)
2255
throw new ArgumentException("Length mismatch.");
2356

24-
var len = a.Length;
25-
var result = new double[len];
57+
var result = ImmutableArray.CreateBuilder<double>(len);
2658
for (var i = 0; i < len; i++)
2759
result[i] = a[i] + b[i];
2860

29-
return result;
61+
return result.MoveToImmutable();
3062
}
3163

64+
static ImmutableArray<double> SumValues(IList<double> a, in ReadOnlySpan<double> b)
65+
{
66+
var len = a.Count;
67+
if (len != b.Length)
68+
throw new ArgumentException("Length mismatch.");
69+
70+
var result = ImmutableArray.CreateBuilder<double>(len);
71+
for (var i = 0; i < len; i++)
72+
result[i] = a[i] + b[i];
73+
74+
return result.MoveToImmutable();
75+
}
76+
77+
public ProcedureResults Add(IList<double> values, int count = 1)
78+
=> new ProcedureResults(SumValues(Sum, values), Count + count);
79+
3280
public ProcedureResults Add(in ReadOnlySpan<double> values, int count = 1)
33-
=> new ProcedureResults(SumValues(Sum.Span, in values), Count + count);
81+
=> new ProcedureResults(SumValues(Sum, in values), Count + count);
3482

3583
public static ProcedureResults operator +(ProcedureResults a, ProcedureResults b)
36-
=> new ProcedureResults(SumValues(a.Sum.Span, b.Sum.Span), a.Count + b.Count);
84+
=> new ProcedureResults(SumValues(a.Sum, b.Sum), a.Count + b.Count);
3785

3886
}
3987
}

0 commit comments

Comments
 (0)