Repository navigation
Expand file tree
/
Copy pathnp.math.cs
More file actions
169 lines (163 loc) · 6.93 KB
/
Copy pathnp.math.cs
File metadata and controls
169 lines (163 loc) · 6.93 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using Numpy;
using Numpy.Models;
using Python.Runtime;
namespace Numpy
{
/// <summary>
/// Manual type conversions
/// </summary>
public static partial class np
{
/// <summary>
/// Return the gradient of an N-dimensional array.<br></br>
///
/// The gradient is computed using second order accurate central differences
/// in the interior points and either first or second order accurate one-sides
/// (forward or backwards) differences at the boundaries.<br></br>
///
/// The returned gradient hence has the same shape as the input array.<br></br>
///
/// Notes
///
/// Assuming that (i.e., has at least 3 continuous
/// derivatives) and let be a non-homogeneous stepsize, we
/// minimize the “consistency error” between the true gradient
/// and its estimate from a linear combination of the neighboring grid-points:
///
/// By substituting and
/// with their Taylor series expansion, this translates into solving
/// the following the linear system:
///
/// The resulting approximation of is the following:
///
/// It is worth noting that if
/// (i.e., data are evenly spaced)
/// we find the standard second order approximation:
///
/// With a similar procedure the forward/backward approximations used for
/// boundaries can be derived.<br></br>
///
/// References
/// </summary>
/// <param name="f">
/// An N-dimensional array containing samples of a scalar function.
/// </param>
/// <param name="varargs">
/// Spacing between f values.<br></br>
/// Default unitary spacing for all dimensions.<br></br>
///
/// Spacing can be specified using:
///
/// If axis is given, the number of varargs must equal the number of axes.<br></br>
///
/// Default: 1.
/// </param>
/// <param name="edge_order">
/// Gradient is calculated using N-th order accurate differences
/// at the boundaries.<br></br>
/// Default: 1.
/// </param>
/// <param name="axis">
/// Gradient is calculated only along the given axis or axes
/// The default (axis = None) is to calculate the gradient for all the axes
/// of the input array.<br></br>
/// axis may be negative, in which case it counts from
/// the last to the first axis.
/// </param>
/// <returns>
/// A set of ndarrays (or a single ndarray if there is only one dimension)
/// corresponding to the derivatives of f with respect to each dimension.<br></br>
///
/// Each derivative has the same shape as f.
/// </returns>
public static NDarray gradient(NDarray f, int? edge_order = null, Axis axis = null)
{
//auto-generated code, do not change
var __self__ = self;
var pyargs = new PyObject[] { f.PyObject };
var kwargs = new PyDict();
if (edge_order != null) kwargs["edge_order"] = ToPython(edge_order);
if (axis != null) kwargs["axis"] = ToPython(axis);
dynamic py = __self__.InvokeMethod("gradient", pyargs, kwargs);
return ToCsharp<NDarray>(py);
}
/// <summary>
/// Return the gradient of an N-dimensional array.<br></br>
///
/// The gradient is computed using second order accurate central differences
/// in the interior points and either first or second order accurate one-sides
/// (forward or backwards) differences at the boundaries.<br></br>
///
/// The returned gradient hence has the same shape as the input array.<br></br>
///
/// Notes
///
/// Assuming that (i.e., has at least 3 continuous
/// derivatives) and let be a non-homogeneous stepsize, we
/// minimize the “consistency error” between the true gradient
/// and its estimate from a linear combination of the neighboring grid-points:
///
/// By substituting and
/// with their Taylor series expansion, this translates into solving
/// the following the linear system:
///
/// The resulting approximation of is the following:
///
/// It is worth noting that if
/// (i.e., data are evenly spaced)
/// we find the standard second order approximation:
///
/// With a similar procedure the forward/backward approximations used for
/// boundaries can be derived.<br></br>
///
/// References
/// </summary>
/// <param name="f">
/// An N-dimensional array containing samples of a scalar function.
/// </param>
/// <param name="varargs">
/// Spacing between f values.<br></br>
/// Default unitary spacing for all dimensions.<br></br>
///
/// Spacing can be specified using:
///
/// If axis is given, the number of varargs must equal the number of axes.<br></br>
///
/// Default: 1.
/// </param>
/// <param name="edge_order">
/// Gradient is calculated using N-th order accurate differences
/// at the boundaries.<br></br>
/// Default: 1.
/// </param>
/// <param name="axis">
/// Gradient is calculated only along the given axis or axes
/// The default (axis = None) is to calculate the gradient for all the axes
/// of the input array.<br></br>
/// axis may be negative, in which case it counts from
/// the last to the first axis.
/// </param>
/// <returns>
/// A set of ndarrays (or a single ndarray if there is only one dimension)
/// corresponding to the derivatives of f with respect to each dimension.<br></br>
///
/// Each derivative has the same shape as f.
/// </returns>
public static NDarray gradient(NDarray f, List<double> varargs, int? edge_order = null, Axis axis = null)
{
//auto-generated code, do not change
var __self__ = self;
var pyargs = new PyObject[] { f.PyObject }.Concat(varargs.Select(x => new PyFloat(x))).ToArray();
var kwargs = new PyDict();
if (edge_order != null) kwargs["edge_order"] = ToPython(edge_order);
if (axis != null) kwargs["axis"] = ToPython(axis);
dynamic py = __self__.InvokeMethod("gradient", pyargs, kwargs);
return ToCsharp<NDarray>(py);
}
}
}