Repository navigation
Expand file tree
/
Copy pathNumPy.cs
More file actions
162 lines (139 loc) · 4.24 KB
/
Copy pathNumPy.cs
File metadata and controls
162 lines (139 loc) · 4.24 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
using System;
using System.Collections.Generic;
using System.Text;
using NumSharp.Core.Extensions;
namespace NumSharp.Core
{
/// <summary>
/// API bridge between NumSharp and Python NumPy
/// </summary>
public class NumPy<T>
{
public NDArray<double> absolute(NDArray<double> np)
{
return np.Absolute();
}
public NDArray<double> amax(NDArray<double> np, int? axis = null)
{
return np.AMax(axis);
}
public NDArray<double> amin(NDArray<double> np, int? axis = null)
{
return np.AMin(axis);
}
public NDArray<T> arange(int stop)
{
return arange(0, stop);
}
public NDArray<T> arange(int start, int stop, int step = 1)
{
if(start > stop)
{
throw new Exception("parameters invalid");
}
switch (typeof(T).Name)
{
case "Int32":
{
var n = new NDArray<int>();
n.arange(stop, start, step);
return n as NDArray<T>;
}
case "Double":
{
var n = new NDArray<double>();
n.arange(stop, start, step);
return n as NDArray<T>;
}
default:
throw new NotImplementedException();
}
}
public NDArray<T> array(T[] data)
{
var n = new NDArray<T>();
n.Data = data;
n.Shape = new Shape(new int[] { data.Length });
return n;
}
public NDArray<T> array(T[][] data)
{
int size = data.Length * data[0].Length;
var all = new T[size];
int idx = 0;
for (int row = 0; row < data.Length; row++)
{
for (int col = 0; col < data[row].Length; col++)
{
all[idx] = data[row][col];
idx++;
}
}
var n = new NDArray<T>();
n.Data = all;
n.Shape = new Shape(new int[] { data.Length, data[0].Length });
return n;
}
public NDArray<double> hstack(params NDArray<double>[] nps)
{
var n = new NDArray<double>();
return n.HStack(nps);
}
/// <summary>
/// Returns num evenly spaced samples, calculated over the interval [start, stop].
/// </summary>
/// <param name="start"></param>
/// <param name="stop"></param>
/// <param name="num"></param>
/// <returns></returns>
public NDArray<double> linspace(double start, double stop, int num = 50)
{
return new NDArray<double>().linspace(start, stop, num);
}
public NDArray<double> max(NDArray<double> nd)
{
return nd.Max();
}
public NDArray<double> power(NDArray<double> nd, double exponent)
{
return nd.power(exponent);
}
public NDArrayRandom random
{
get
{
return new NDArrayRandom();
}
}
public NDArray<int> reshape(NDArray<int> np, params int[] shape)
{
np.Shape = new Shape(shape);
return np;
}
public NDArray<double> vstack(params NDArray<double>[] nps)
{
var n = new NDArray<double>();
return n.VStack(nps);
}
public NDArray<T> zeros(params int[] shape)
{
switch (typeof(T).Name)
{
case "Int32":
{
var n = new NDArray<int>();
n.Zeros(shape);
return n as NDArray<T>;
}
case "Double":
{
var n = new NDArray<double>();
n.Zeros(shape);
return n as NDArray<T>;
}
default:
throw new NotImplementedException();
}
}
}
}