Skip to content

Commit 098ab34

Browse files
committed
Merge branch 'master' of https://github.com/Oceania2018/NumSharp
2 parents 24d5cbe + 7211ef5 commit 098ab34

6 files changed

Lines changed: 308 additions & 30 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ PM> Install-Package NumSharp
7474

7575
### How to run benchmark
7676
```
77-
C: \> dotnet NumSharp.Benchmark.dll
77+
C: \> dotnet NumSharp.Benchmark.dll nparange
7878
```
7979
Reference the [documents](https://scisharp.github.io/NumSharp) generated by DocFX.
8080

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.ComponentModel;
4+
using System.Linq;
5+
using System.Numerics;
6+
using System.Text;
7+
8+
namespace NumSharp
9+
{
10+
public partial class NDArrayWithDType
11+
{
12+
public NDArrayWithDType arange(int stop, int start = 0, int step = 1, Type dtype = null)
13+
{
14+
var list = new int[(int)Math.Ceiling((stop - start + 0.0) / step)];
15+
int index = 0;
16+
17+
if(dtype == null)
18+
{
19+
dtype = typeof(int);
20+
}
21+
22+
switch (dtype.Name)
23+
{
24+
case "Int32":
25+
storageForInt32 = new int[list.Length];
26+
for (int i = start; i < stop; i += step)
27+
storageForInt32[index++] = i;
28+
break;
29+
30+
/*case "double":
31+
32+
for (int i = start; i < stop; i += step)
33+
storageForInt32[index++] = i + 0.0;
34+
break;
35+
36+
case double[] dataArray :
37+
{
38+
for(int idx = 0; idx < dataArray.Length;idx++)
39+
dataArray[idx] = list[idx];
40+
break;
41+
}
42+
case float[] dataArray :
43+
{
44+
for(int idx = 0; idx < dataArray.Length;idx++)
45+
dataArray[idx] = list[idx];
46+
break;
47+
}
48+
case Complex[] dataArray :
49+
{
50+
// no performance critial operation
51+
dataArray = list.Select(x => (Complex) x ).ToArray();
52+
break;
53+
}
54+
case Quaternion[] dataArray :
55+
{
56+
// no performance critial operation
57+
dataArray = list.Select(x => new Quaternion(new Vector3(0,0,0),x) ).ToArray();
58+
break;
59+
}
60+
default :
61+
{
62+
throw new Exception("This method was not yet implemented for this type" + typeof(T).Name);
63+
}*/
64+
}
65+
66+
this.Shape = new Shape(list.Length);
67+
68+
return this;
69+
}
70+
}
71+
}

src/NumSharp/Creation/NdArray.ARange.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,19 @@ public NDArray<T> arange(int stop, int start = 0, int step = 1)
1414
var list = new int[(int)Math.Ceiling((stop - start + 0.0) / step)];
1515
int index = 0;
1616
Data = new T[list.Length];
17-
18-
for (int i = start; i < stop; i += step)
19-
list[index++] = i;
2017

2118
switch (Data)
2219
{
2320
case int[] dataArray :
2421
{
25-
for(int idx = 0; idx < dataArray.Length;idx++)
26-
dataArray[idx] = list[idx];
22+
for (int i = start; i < stop; i += step)
23+
dataArray[index++] = i;
2724
break;
2825
}
2926
case long[] dataArray :
3027
{
31-
for(int idx = 0; idx < dataArray.Length;idx++)
32-
dataArray[idx] = list[idx];
28+
for (int i = start; i < stop; i += step)
29+
dataArray[index++] = i;
3330
break;
3431
}
3532
case double[] dataArray :

src/NumSharp/NDArrayWithDType.cs

Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
/*
2+
* NumSharp
3+
* Copyright (C) 2018 Haiping Chen
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the Apache License 2.0 as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the Apache License 2.0
16+
* along with this program. If not, see <http://www.apache.org/licenses/LICENSE-2.0/>.
17+
*/
18+
19+
using System;
20+
using System.Collections.Generic;
21+
using System.ComponentModel;
22+
using System.Linq;
23+
using System.Text;
24+
using System.Globalization;
25+
using System.Collections;
26+
27+
namespace NumSharp
28+
{
29+
/// <summary>
30+
/// A powerful N-dimensional array object
31+
/// Inspired from https://www.numpy.org/devdocs/user/quickstart.html
32+
/// </summary>
33+
public partial class NDArrayWithDType
34+
{
35+
public Type int16 = typeof(short);
36+
37+
/// <summary>
38+
/// storage by data type
39+
/// </summary>
40+
public int[] storageForInt32 { get; set; }
41+
42+
private Shape shape;
43+
/// <summary>
44+
/// Data length of every dimension
45+
/// </summary>
46+
public Shape Shape
47+
{
48+
get
49+
{
50+
return shape;
51+
}
52+
set
53+
{
54+
shape = value;
55+
}
56+
}
57+
58+
/// <summary>
59+
/// Dimension count
60+
/// </summary>
61+
public int NDim => Shape.Length;
62+
63+
/// <summary>
64+
/// Total of elements
65+
/// </summary>
66+
public int Size => Shape.Size;
67+
68+
public NDArrayWithDType()
69+
{
70+
// set default shape as 1 dim and 0 elements.
71+
Shape = new Shape(new int[] { 0 });
72+
}
73+
74+
public override string ToString()
75+
{
76+
string output = "";
77+
78+
if (this.NDim == 2)
79+
{
80+
output = this._ToMatrixString();
81+
}
82+
else
83+
{
84+
output = this._ToVectorString();
85+
}
86+
87+
return output;
88+
}
89+
90+
public override bool Equals(object obj)
91+
{
92+
return storageForInt32[0].Equals(obj);
93+
}
94+
95+
public static bool operator ==(NDArrayWithDType np, object obj)
96+
{
97+
return np.storageForInt32[0].Equals(obj);
98+
}
99+
100+
public static bool operator !=(NDArrayWithDType np, object obj)
101+
{
102+
return np.storageForInt32[0].Equals(obj);
103+
}
104+
105+
public override int GetHashCode()
106+
{
107+
unchecked
108+
{
109+
var result = 1337;
110+
result = (result * 397) ^ this.NDim;
111+
result = (result * 397) ^ this.Size;
112+
return result;
113+
}
114+
}
115+
116+
protected string _ToVectorString()
117+
{
118+
string returnValue = "array([";
119+
120+
int digitBefore = 0;
121+
int digitAfter = 0;
122+
123+
var dataParsed = storageForInt32.Select(x => _ParseNumber(x,ref digitBefore,ref digitAfter)).ToArray();
124+
125+
string elementFormatStart = "{0:";
126+
127+
string elementFormatEnd = "";
128+
for(int idx = 0; idx < digitAfter;idx++)
129+
elementFormatEnd += "0";
130+
131+
elementFormatEnd += "}";
132+
133+
int missingDigits;
134+
string elementFormat;
135+
136+
for (int idx = 0; idx < (storageForInt32.Length-1);idx++)
137+
{
138+
missingDigits = digitBefore - dataParsed[idx].Replace(" ","").Split('.')[0].Length;
139+
140+
elementFormat = elementFormatStart + new string(Enumerable.Repeat<char>(' ',missingDigits).ToArray()) + "0." + elementFormatEnd;
141+
142+
returnValue += (String.Format(new CultureInfo("en-us"),elementFormat, storageForInt32[idx]) + ", ");
143+
}
144+
missingDigits = digitBefore - dataParsed.Last().Replace(" ","").Split('.')[0].Length;
145+
146+
elementFormat = elementFormatStart + new string(Enumerable.Repeat<char>(' ',missingDigits).ToArray()) + "." + elementFormatEnd;
147+
148+
returnValue += (String.Format(new CultureInfo("en-us"),elementFormat, storageForInt32.Last()) + "])");
149+
150+
return returnValue;
151+
}
152+
protected string _ToMatrixString()
153+
{
154+
string returnValue = "array([[";
155+
156+
int digitBefore = 0;
157+
int digitAfter = 0;
158+
159+
var dataParsed = storageForInt32.Select(x => _ParseNumber(x,ref digitBefore,ref digitAfter)).ToArray();
160+
161+
string elementFormatStart = "{0:";
162+
163+
string elementFormatEnd = "";
164+
for(int idx = 0; idx < digitAfter;idx++)
165+
elementFormatEnd += "0";
166+
167+
elementFormatEnd += "}";
168+
169+
int missingDigits;
170+
string elementFormat;
171+
172+
for (int idx = 0; idx < (storageForInt32.Length-1);idx++)
173+
{
174+
missingDigits = digitBefore - dataParsed[idx].Replace(" ","").Split('.')[0].Length;
175+
176+
elementFormat = elementFormatStart + new string(Enumerable.Repeat<char>(' ',missingDigits).ToArray()) + "0." + elementFormatEnd;
177+
178+
if( ((idx+1) % Shape.Shapes[1] ) == 0 )
179+
{
180+
returnValue += (String.Format(new CultureInfo("en-us"),elementFormat, storageForInt32[idx]) + "], \n [");
181+
}
182+
else
183+
{
184+
returnValue += (String.Format(new CultureInfo("en-us"),elementFormat, storageForInt32[idx]) + ", ");
185+
}
186+
187+
}
188+
missingDigits = digitBefore - dataParsed.Last().Replace(" ","").Split('.')[0].Length;
189+
190+
elementFormat = elementFormatStart + new string(Enumerable.Repeat<char>(' ',missingDigits).ToArray()) + "." + elementFormatEnd;
191+
192+
returnValue += (String.Format(new CultureInfo("en-us"),elementFormat, storageForInt32.Last()) + "]])");
193+
194+
return returnValue;
195+
}
196+
protected string _ParseNumber(object number, ref int noBefore,ref int noAfter)
197+
{
198+
string parsed = string.Format(new CultureInfo("en-us"),"{0:0.00000000}",number);
199+
200+
parsed = (parsed.StartsWith("-")) ? parsed : (" " + parsed);
201+
202+
int noBefore_local = parsed.Split('.')[0].Length;
203+
int noAfter_local = parsed.Split('.')[1].ToCharArray().Reverse().SkipWhile(x => x == '0').ToArray().Length;
204+
205+
noBefore = (noBefore_local > noBefore) ? noBefore_local : noBefore;
206+
noAfter = (noAfter_local > noAfter ) ? noAfter_local : noAfter;
207+
208+
return parsed;
209+
}
210+
}
211+
}

test/NumSharp.Benchmark/Program.cs

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,17 @@ namespace NumSharp.Benchmark
88
{
99
class Program
1010
{
11+
/// <summary>
12+
/// dotnet NumSharp.Benchmark.dll (Benchmark Class Name)
13+
/// dotnet NumSharp.Benchmark.dll nparange
14+
/// </summary>
15+
/// <param name="args"></param>
1116
static void Main(string[] args)
1217
{
13-
BenchmarkDotNet.Reports.Summary accessSummary;
14-
15-
if (args.Contains("Linq"))
16-
{
17-
accessSummary = BenchmarkRunner.Run<LinqTesterInt>();
18-
accessSummary = BenchmarkRunner.Run<LinqTesterDouble>();
19-
accessSummary = BenchmarkRunner.Run<LinqTesterQuaternion>();
20-
21-
}
22-
if (args.Contains("NDArray"))
23-
{
24-
accessSummary = BenchmarkRunner.Run<NDArrayTester1D>();
25-
accessSummary = BenchmarkRunner.Run<NDArrayTester2D>();
26-
}
27-
if (args.Contains("amin"))
28-
{
29-
accessSummary = BenchmarkRunner.Run<npamin>();
30-
}
18+
string method = $"NumSharp.Benchmark.{args[0]}";
19+
Console.WriteLine(method);
20+
Type type = Type.GetType(method);
21+
var accessSummary = BenchmarkRunner.Run(type);
3122

3223
Console.WriteLine("Please press any key to continue.");
3324
Console.ReadKey();
Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88

99
namespace NumSharp.Benchmark
1010
{
11-
[SimpleJob(RunStrategy.ColdStart, targetCount: 5)]
11+
[SimpleJob(RunStrategy.ColdStart, targetCount: 100)]
1212
[MinColumn, MaxColumn, MeanColumn, MedianColumn]
13-
public class nparrange
13+
public class nparange
1414
{
1515
private NumPy<int> np;
1616
private NDArray<int> nd;
17+
1718
private int start;
1819
private int step;
1920
private int length;
@@ -34,11 +35,18 @@ public void arange()
3435
np.arange(length);
3536
}
3637

38+
[Benchmark]
39+
public void arange_ndarraywithdtype()
40+
{
41+
var nd2 = new NDArrayWithDType();
42+
var nd3 = nd2.arange(length, start, step);
43+
}
44+
3745
[Benchmark]
3846
public void arange_ndarray()
3947
{
40-
var n = new NDArray<int>();
41-
n.arange(length, start, step);
48+
var nd2 = new NDArray<int>();
49+
var nd3 = nd2.arange(length, start, step);
4250
}
4351

4452
[Benchmark]

0 commit comments

Comments
 (0)