forked from SciSharp/NumSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNdArray.Test.cs
More file actions
146 lines (117 loc) · 3.92 KB
/
NdArray.Test.cs
File metadata and controls
146 lines (117 loc) · 3.92 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
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Text;
using NumSharp.Core.Extensions;
using System.Linq;
using NumSharp.Core;
namespace NumSharp.UnitTest
{
[TestClass]
public class NDArrayTest
{
[TestMethod]
public void IndexAccessorGetter()
{
var np = new NumPy<int>();
var n = np.reshape(np.arange(12), 3, 4);
Assert.IsTrue(n[1, 1] == 5);
Assert.IsTrue(n[2, 0] == 8);
n = np.reshape(np.arange(12), 2, 3, 2);
var n1 = n[new Shape(1)];
Assert.IsTrue(n1[1, 1] == 9);
Assert.IsTrue(n1[2, 1] == 11);
var n2 = n[new Shape(1, 2)];
Assert.IsTrue(n2[0] == 10);
Assert.IsTrue(n2[1] == 11);
}
[TestMethod]
public void IndexAccessorSetter()
{
var np = new NumPy<int>();
var n = np.reshape(np.arange(12), 3, 4);
Assert.IsTrue(n[0, 3] == 3);
Assert.IsTrue(n[1, 3] == 7);
// set value
n.Set(new Shape(0), 10);
Assert.IsTrue(n[0, 0] == 10);
Assert.IsTrue(n[0, 3] == 10);
Assert.IsTrue(n[1, 3] == 7);
}
[TestMethod]
public void StringCheck()
{
var np = new NDArray<double>().arange(9).reshape(3,3);
var random = new Random();
np.Data = np.Data.Select(x => x + random.NextDouble()).ToArray();
np.Data[1] = 1;
np.Data[2] -= 4;
np.Data[3] -= 20;
np.Data[8] += 23;
var stringOfNp = np.ToString();
Assert.IsTrue(stringOfNp.Contains("[[ 0."));
np = new NDArray<double>().arange(9).reshape(3,3);
stringOfNp = np.ToString();
Assert.IsTrue(stringOfNp.Contains("([[ 0,"));
}
[TestMethod]
public void CheckVectorString()
{
var np = new NDArray<double>().arange(9);
var random = new Random();
np.Data = np.Data.Select(x => x + random.NextDouble()).ToArray();
np.Data[1] = 1;
np.Data[2] -= 4;
np.Data[3] -= 20;
np.Data[8] += 23;
var stringOfNp = np.ToString();
}
[TestMethod]
public void DimOrder()
{
NDArray<double> np1 = new NDArray<double>().Zeros(2,2);
np1[0,0] = 0;
np1[1,0] = 10;
np1[0,1] = 1;
np1[1,1] = 11;
// columns first than rows
Assert.IsTrue(Enumerable.SequenceEqual(new double[] {0,1,10,11}, np1.Data ));
}
[TestMethod]
public void ToDotNetArray1D()
{
var np1 = new NDArray<double>().arange(9);
double[] np1_ = np1.ToDotNetArray<double[]>();
Assert.IsTrue(Enumerable.SequenceEqual(np1_,np1.Data));
}
[TestMethod]
public void ToDotNetArray2D()
{
var np1 = new NDArray<double>().arange(9).reshape(3,3);
double[][] np1_ = np1.ToDotNetArray<double[][]>();
for (int idx = 0; idx < 3; idx ++)
{
for (int jdx = 0; jdx < 3; jdx ++)
{
Assert.IsTrue(np1[idx,jdx] == np1_[idx][jdx]);
}
}
}
[TestMethod]
public void ToDotNetArray3D()
{
var np1 = new NDArray<double>().arange(27).reshape(3,3,3);
double[][][] np1_ = np1.ToDotNetArray<double[][][]>();
for (int idx = 0; idx < 3; idx ++)
{
for (int jdx = 0; jdx < 3; jdx ++)
{
for (int kdx = 0; kdx < 3;kdx++)
{
Assert.IsTrue(np1[idx,jdx,kdx] == np1_[idx][jdx][kdx]);
}
}
}
}
}
}