-
Notifications
You must be signed in to change notification settings - Fork 976
Expand file tree
/
Copy pathArrayExtensions.cs
More file actions
128 lines (113 loc) · 4.12 KB
/
Copy pathArrayExtensions.cs
File metadata and controls
128 lines (113 loc) · 4.12 KB
1
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ArrayExtensions.cs" company="OxyPlot">
// Copyright (c) 2014 OxyPlot contributors
// </copyright>
// <summary>
// Provides useful extension methods for arrays.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
#nullable enable
namespace OxyPlot
{
using System;
using System.Collections.Generic;
/// <summary>
/// Provides useful extension methods for arrays.
/// </summary>
public static class ArrayExtensions
{
/// <summary>
/// Finds the maximum value in the sequence, or returns a default value if the sequence is empty.
/// </summary>
/// <param name="sequence">The sequence.</param>
/// <param name="defaultValue">The default value.</param>
/// <returns>The maximum value of the sequence, or the default value if the sequency is empty.</returns>
public static double MaxOrDefault(this IEnumerable<double> sequence, double defaultValue)
{
if (sequence == null)
{
throw new ArgumentNullException(nameof(sequence));
}
using var e = sequence.GetEnumerator();
if (!e.MoveNext())
{
return defaultValue;
}
var max = e.Current;
while (e.MoveNext())
{
max = Math.Max(max, e.Current);
}
return max;
}
/// <summary>
/// Finds the minimum value in the sequence, or returns a default value if the sequence is empty.
/// </summary>
/// <param name="sequence">The sequence.</param>
/// <param name="defaultValue">The default value.</param>
/// <returns>The minimum value of the sequence, or the default value if the sequency is empty.</returns>
public static double MinOrDefault(this IEnumerable<double> sequence, double defaultValue)
{
if (sequence == null)
{
throw new ArgumentNullException(nameof(sequence));
}
using var e = sequence.GetEnumerator();
if (!e.MoveNext())
{
return defaultValue;
}
var min = e.Current;
while (e.MoveNext())
{
min = Math.Min(min, e.Current);
}
return min;
}
/// <summary>
/// Finds the maximum value in the specified 2D array (NaN values not included).
/// </summary>
/// <param name="array">The array.</param>
/// <returns>The maximum value.</returns>
public static double Max2D(this double[,] array)
{
var max = double.MinValue;
for (var i = 0; i < array.GetLength(0); i++)
{
for (var j = 0; j < array.GetLength(1); j++)
{
if (array[i, j].CompareTo(max) > 0)
{
max = array[i, j];
}
}
}
return max;
}
/// <summary>
/// Finds the minimum value in the specified 2D array.
/// </summary>
/// <param name="array">The array.</param>
/// <param name="excludeNaN">Exclude NaN values if set to <c>true</c>.</param>
/// <returns>The minimum value.</returns>
public static double Min2D(this double[,] array, bool excludeNaN = false)
{
var min = double.MaxValue;
for (var i = 0; i < array.GetLength(0); i++)
{
for (var j = 0; j < array.GetLength(1); j++)
{
if (excludeNaN && double.IsNaN(array[i, j]))
{
continue;
}
if (array[i, j].CompareTo(min) < 0)
{
min = array[i, j];
}
}
}
return min;
}
}
}