-
Notifications
You must be signed in to change notification settings - Fork 976
Expand file tree
/
Copy pathArrayBuilder.cs
More file actions
110 lines (100 loc) · 3.5 KB
/
Copy pathArrayBuilder.cs
File metadata and controls
110 lines (100 loc) · 3.5 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
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ArrayBuilder.cs" company="OxyPlot">
// Copyright (c) 2014 OxyPlot contributors
// </copyright>
// <summary>
// Provides functionality to build arrays.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
#nullable enable
namespace OxyPlot
{
using System;
/// <summary>
/// Provides functionality to build arrays.
/// </summary>
public static class ArrayBuilder
{
/// <summary>
/// Creates a vector.
/// </summary>
/// <param name="x0">The first value.</param>
/// <param name="x1">The last value.</param>
/// <param name="n">The number of steps.</param>
/// <returns>A vector.</returns>
public static double[] CreateVector(double x0, double x1, int n)
{
var result = new double[n];
for (int i = 0; i < n; i++)
{
result[i] = Math.Round(x0 + ((x1 - x0) * i / (n - 1)), 8);
}
return result;
}
/// <summary>
/// Creates a vector.
/// </summary>
/// <param name="x0">The first value.</param>
/// <param name="x1">The last value.</param>
/// <param name="dx">The step size.</param>
/// <returns>A vector.</returns>
public static double[] CreateVector(double x0, double x1, double dx)
{
var n = (int)Math.Round((x1 - x0) / dx);
var result = new double[n + 1];
for (int i = 0; i <= n; i++)
{
result[i] = Math.Round(x0 + (i * dx), 8);
}
return result;
}
/// <summary>
/// Evaluates the specified function.
/// </summary>
/// <param name="f">The function.</param>
/// <param name="x">The x values.</param>
/// <param name="y">The y values.</param>
/// <returns>Array of evaluations. The value of f(x_i,y_j) will be placed at index [i, j].</returns>
public static double[,] Evaluate(Func<double, double, double> f, double[] x, double[] y)
{
int m = x.Length;
int n = y.Length;
var result = new double[m, n];
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
result[i, j] = f(x[i], y[j]);
}
}
return result;
}
/// <summary>
/// Fills the array with the specified value.
/// </summary>
/// <param name="array">The array to fill.</param>
/// <param name="value">The value.</param>
public static void Fill(this double[] array, double value)
{
for (var i = 0; i < array.Length; i++)
{
array[i] = value;
}
}
/// <summary>
/// Fills the two-dimensional array with the specified value.
/// </summary>
/// <param name="array">The two-dimensional array.</param>
/// <param name="value">The value.</param>
public static void Fill2D(this double[,] array, double value)
{
for (var i = 0; i < array.GetLength(0); i++)
{
for (var j = 0; j < array.GetLength(1); j++)
{
array[i, j] = value;
}
}
}
}
}