Skip to content

Commit 10d526c

Browse files
authored
Merge pull request #159 from dotChris90/master
Restructure NDStorage and Shape + Tidy up old code
2 parents 0315e89 + cf3dc9e commit 10d526c

File tree

91 files changed

+1335
-2499
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

91 files changed

+1335
-2499
lines changed
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
using System;
2+
using NumSharp.Core;
3+
using System.Linq;
4+
using System.Globalization;
5+
using System.Collections.Generic;
6+
7+
namespace NumSharp.Core
8+
{
9+
public partial class NDArray
10+
{
11+
public override string ToString()
12+
{
13+
string output = "";
14+
15+
if (this.ndim == 2)
16+
{
17+
if(dtype == typeof(int))
18+
{
19+
output = this._ToMatrixString<int>();
20+
}
21+
else if(dtype == typeof(double))
22+
{
23+
output = this._ToMatrixString<double>();
24+
}
25+
26+
}
27+
else
28+
{
29+
if (dtype == typeof(int))
30+
{
31+
//output = this._ToVectorString<int>();
32+
}
33+
else if (dtype == typeof(double))
34+
{
35+
//output = this._ToVectorString<double>();
36+
}
37+
}
38+
39+
return output;
40+
}
41+
protected string _ToVectorString<T>()
42+
{
43+
string returnValue = "array([";
44+
45+
int digitBefore = 0;
46+
int digitAfter = 0;
47+
48+
var dataParsed = Storage.GetData<T>().Select(x => _ParseNumber(x,ref digitBefore,ref digitAfter)).ToArray();
49+
50+
string elementFormatStart = "{0:";
51+
52+
string elementFormatEnd = "";
53+
for(int idx = 0; idx < digitAfter;idx++)
54+
elementFormatEnd += "0";
55+
56+
elementFormatEnd += "}";
57+
58+
int missingDigits;
59+
string elementFormat;
60+
61+
for (int idx = 0; idx < (Storage.Shape.Size-1);idx++)
62+
{
63+
missingDigits = digitBefore - dataParsed[idx].Replace(" ","").Split('.')[0].Length;
64+
65+
elementFormat = elementFormatStart + new string(Enumerable.Repeat<char>(' ',missingDigits).ToArray()) + "0." + elementFormatEnd;
66+
67+
returnValue += (String.Format(new CultureInfo("en-us"),elementFormat, Storage.GetData<T>()[idx]) + ", ");
68+
}
69+
missingDigits = digitBefore - dataParsed.Last().Replace(" ","").Split('.')[0].Length;
70+
71+
elementFormat = elementFormatStart + new string(Enumerable.Repeat<char>(' ',missingDigits).ToArray()) + "." + elementFormatEnd;
72+
73+
returnValue += (String.Format(new CultureInfo("en-us"),elementFormat, Storage.GetData<T>().Last()) + "])");
74+
75+
return returnValue;
76+
}
77+
protected string _ToMatrixString<T>()
78+
{
79+
string returnValue = "array([[";
80+
81+
int digitBefore = 0;
82+
int digitAfter = 0;
83+
84+
string[] dataParsed = Storage.GetData<T>().Select(x => _ParseNumber(x, ref digitBefore, ref digitAfter)).ToArray();
85+
86+
string elementFormatStart = "{0:";
87+
88+
string elementFormatEnd = "";
89+
for(int idx = 0; idx < digitAfter;idx++)
90+
elementFormatEnd += "0";
91+
92+
elementFormatEnd += "}";
93+
94+
int missingDigits;
95+
string elementFormat;
96+
97+
for (int idx = 0; idx < shape.NDim - 1; idx++)
98+
{
99+
missingDigits = digitBefore - dataParsed[idx].Replace(" ", "").Split('.')[0].Length;
100+
101+
elementFormat = elementFormatStart + new string(Enumerable.Repeat<char>(' ', missingDigits).ToArray()) + "0." + elementFormatEnd;
102+
103+
if (((idx + 1) % shape.Dimensions[1]) == 0)
104+
{
105+
returnValue += (String.Format(new CultureInfo("en-us"), elementFormat, Storage.GetData<T>()[idx]) + "], \n [");
106+
}
107+
else
108+
{
109+
returnValue += (String.Format(new CultureInfo("en-us"), elementFormat, Storage.GetData<T>()[idx]) + ", ");
110+
}
111+
}
112+
113+
missingDigits = digitBefore - dataParsed.Last().Replace(" ","").Split('.')[0].Length;
114+
115+
elementFormat = elementFormatStart + new string(Enumerable.Repeat<char>(' ',missingDigits).ToArray()) + "." + elementFormatEnd;
116+
117+
returnValue += (String.Format(new CultureInfo("en-us"),elementFormat, Storage.GetData<T>().Last()) + "]])");
118+
119+
return returnValue;
120+
}
121+
protected string _ParseNumber(object number, ref int noBefore,ref int noAfter)
122+
{
123+
string parsed = string.Format(new CultureInfo("en-us"),"{0:0.00000000}",number);
124+
125+
parsed = (parsed.StartsWith("-")) ? parsed : (" " + parsed);
126+
127+
int noBefore_local = parsed.Split('.')[0].Length;
128+
int noAfter_local = parsed.Split('.')[1].ToCharArray().Reverse().SkipWhile(x => x == '0').ToArray().Length;
129+
130+
noBefore = (noBefore_local > noBefore) ? noBefore_local : noBefore;
131+
noAfter = (noAfter_local > noAfter ) ? noAfter_local : noAfter;
132+
133+
return parsed;
134+
}
135+
}
136+
137+
}
138+

src/NumSharp.Core/Casting/NdArrayToJaggedArray.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@ public Array ToJaggedArray<T>()
4242
}
4343
case 2 :
4444
{
45-
T[] data = Storage.GetData<T>();
46-
T[][] dotNetArrayPuffer = new T[shape.Shapes[0]][];
45+
T[][] dotNetArrayPuffer = new T[shape.Dimensions[0]][];
4746
for (int idx = 0; idx < dotNetArrayPuffer.Length;idx++)
48-
dotNetArrayPuffer[idx] = new T[shape.Shapes[1]];
47+
dotNetArrayPuffer[idx] = new T[shape.Dimensions[1]];
4948

50-
for (int idx = 0;idx < data.Length;idx++ )
51-
dotNetArrayPuffer[idx/shape.Shapes[1]][idx%shape.Shapes[1]] = data[idx];
49+
for (int idx = 0;idx < dotNetArrayPuffer.Length;idx++ )
50+
for (int jdx = 0; jdx < dotNetArrayPuffer[0].Length;jdx++)
51+
dotNetArrayPuffer[idx][jdx] = (T) this[idx,jdx];
5252

5353
dotNetArray = dotNetArrayPuffer;
5454

@@ -57,19 +57,19 @@ public Array ToJaggedArray<T>()
5757
case 3 :
5858
{
5959
T[] data = Storage.GetData<T>();
60-
T[][][] dotNetArrayPuffer = new T[shape.Shapes[0]][][];
60+
T[][][] dotNetArrayPuffer = new T[shape.Dimensions[0]][][];
6161
for (int idx = 0; idx < dotNetArrayPuffer.Length;idx++)
6262
{
63-
dotNetArrayPuffer[idx] = new T[shape.Shapes[1]][];
63+
dotNetArrayPuffer[idx] = new T[shape.Dimensions[1]][];
6464
for (int jdx = 0; jdx < dotNetArrayPuffer[idx].Length;jdx++)
65-
dotNetArrayPuffer[idx][jdx] = new T[shape.Shapes[2]];
65+
dotNetArrayPuffer[idx][jdx] = new T[shape.Dimensions[2]];
6666
}
6767

68-
for (int idx = 0; idx < shape.Shapes[0];idx++)
69-
for (int jdx = 0;jdx < shape.Shapes[1];jdx++)
70-
for(int kdx = 0; kdx < shape.Shapes[2];kdx++)
68+
for (int idx = 0; idx < shape.Dimensions[0];idx++)
69+
for (int jdx = 0;jdx < shape.Dimensions[1];jdx++)
70+
for(int kdx = 0; kdx < shape.Dimensions[2];kdx++)
7171
dotNetArrayPuffer[idx][jdx][kdx] = (T) this[idx,jdx,kdx];
72-
72+
7373
dotNetArray = dotNetArrayPuffer;
7474

7575
break;

src/NumSharp.Core/Casting/NdArrayToMultiDimArray.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ public Array ToMuliDimArray<T>()
4343
case 2 :
4444
{
4545
T[] data = Storage.GetData<T>();
46-
T[,] dotNetArrayPuffer = new T[shape.Shapes[0],shape.Shapes[1]];
46+
T[,] dotNetArrayPuffer = new T[shape.Dimensions[0],shape.Dimensions[1]];
4747
for (int idx = 0;idx < data.Length;idx++ )
48-
dotNetArrayPuffer[idx/shape.Shapes[1],idx%shape.Shapes[1]] = data[idx];
48+
dotNetArrayPuffer[idx/shape.Dimensions[1],idx%shape.Dimensions[1]] = data[idx];
4949

5050
dotNetArray = dotNetArrayPuffer;
5151

src/NumSharp.Core/Creation/NdArray.ARange.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public NDArray arange(int stop, int start = 0, int step = 1)
6565
}
6666
}
6767

68-
this.Storage.Shape = new Shape(list.Length);
68+
this.Storage.Reshape(list.Length);
6969

7070
return this;
7171
}

src/NumSharp.Core/Extensions/NdArray.AsMatrix.cs renamed to src/NumSharp.Core/Creation/NdArray.AsMatrix.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public static matrix AsMatrix(this NDArray nd)
1212
{
1313
var npAsMatrix = new matrix(nd);
1414

15-
npAsMatrix.reshape(nd.shape);
15+
npAsMatrix.reshape((Shape)nd.shape);
1616

1717
return npAsMatrix;
1818
}

src/NumSharp.Core/Creation/NdArray.Eye.cs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,17 @@ public NDArray eye(int dim, int diagonalIndex = 0)
2020
}
2121
else
2222
{
23-
puffer = new NDArray(this.dtype,this.shape.Shapes.ToArray());
23+
puffer = new NDArray(this.dtype,this.shape.Dimensions.ToArray());
2424
}
25-
26-
puffer.Storage.SetData(Array.CreateInstance(dtype,puffer.size));
27-
28-
Array storageArr = puffer.Storage.GetData();
2925

30-
if (diagonalIndex > -1)
26+
puffer.Storage.SetData(Array.CreateInstance(dtype,puffer.size));
27+
28+
if (diagonalIndex >= 0)
3129
for(int idx = 0; idx < noOfDiagElement;idx++ )
32-
storageArr.SetValue(1,diagonalIndex + idx + idx * puffer.shape.Shapes[1]);
30+
puffer.Storage.SetData(1,idx,idx+diagonalIndex);
3331
else
34-
for(int idx = dim - 1; idx > dim - noOfDiagElement - 1;idx-- )
35-
storageArr.SetValue(1,diagonalIndex + idx + idx * puffer.shape.Shapes[1]); // = 1;
36-
37-
this.Storage = puffer.Storage;
32+
for(int idx = puffer.Storage.Shape.Dimensions[0]-1; idx > puffer.Storage.Shape.Dimensions[0]-1 - noOfDiagElement;idx-- )
33+
puffer.Storage.SetData(1,idx,idx+diagonalIndex);
3834

3935
return puffer;
4036
}

src/NumSharp.Core/Creation/NdArray.HStack.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,14 @@ public NDArray hstack(params NDArray[] nps)
3939
}
4040
else
4141
{
42+
for(int idx = 0; idx < npAll.Length;idx++)
43+
npAll[idx].Storage.ChangeTensorLayout(2);
44+
4245
var list = new List<object>();
4346

44-
int total = npAll[0].ndim == 1 ? 1 : npAll[0].shape.Shapes[0];
47+
int total = npAll[0].ndim == 1 ? 1 : npAll[0].shape.Dimensions[0];
4548

46-
int pageSize = npAll[0].ndim == 1 ? npAll[0].shape.Shapes[0] : npAll[0].shape.DimOffset[0];
49+
int pageSize = npAll[0].ndim == 1 ? npAll[0].shape.Dimensions[0] : npAll[0].shape.DimOffset[0];
4750

4851
for (int i = 0; i < total; i++)
4952
{
@@ -54,18 +57,17 @@ public NDArray hstack(params NDArray[] nps)
5457
}
5558
}
5659

57-
nd.Storage.SetData( list.ToArray());
58-
59-
int[] shapes = npAll[0].shape.Shapes.ToArray();
60+
int[] shapes = npAll[0].shape.Dimensions.ToArray();
6061

6162
if (shapes.Length == 1)
6263
shapes[0] *= npAll.Length;
6364
else
6465
shapes[1] *= npAll.Length;
6566

66-
nd.Storage.Shape = new Shape(shapes);
67+
nd.Storage.Allocate(nd.Storage.DType,new Shape(shapes),2);
68+
nd.Storage.SetData(list.ToArray());
69+
nd.Storage.ChangeTensorLayout(1);
6770

68-
6971
}
7072

7173
return nd;

src/NumSharp.Core/Creation/NdArray.LinSpace.cs

Lines changed: 2 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -66,71 +66,9 @@ public NDArray linspace(double start, double stop,int num, bool entdpoint = true
6666
}
6767
}
6868

69-
this.Storage.Shape = new Shape(doubleArray.Length);
69+
this.Storage.Reshape(doubleArray.Length);
7070

7171
return this;
7272
}
7373
}
74-
public partial class NDArrayGeneric<T>
75-
{
76-
public NDArrayGeneric<T> linspace(double start, double stop,int num, bool entdpoint = true)
77-
{
78-
double steps = (stop - start)/((entdpoint) ? (double) num - 1.0 : (double) num);
79-
80-
double[] doubleArray = new double[num];
81-
82-
for (int idx = 0; idx < doubleArray.Length;idx++)
83-
doubleArray[idx] = start + idx * steps;
84-
85-
Data = new T[doubleArray.Length];
86-
87-
switch (Data)
88-
{
89-
case int[] dataArray :
90-
{
91-
for(int idx = 0; idx < dataArray.Length;idx++)
92-
dataArray[idx] = (int)doubleArray[idx];
93-
break;
94-
}
95-
case long[] dataArray :
96-
{
97-
for(int idx = 0; idx < dataArray.Length;idx++)
98-
dataArray[idx] = (long) doubleArray[idx];
99-
break;
100-
}
101-
case double[] dataArray :
102-
{
103-
for(int idx = 0; idx < dataArray.Length;idx++)
104-
dataArray[idx] = doubleArray[idx];
105-
break;
106-
}
107-
case float[] dataArray :
108-
{
109-
for(int idx = 0; idx < dataArray.Length;idx++)
110-
dataArray[idx] = (float)doubleArray[idx];
111-
break;
112-
}
113-
case Complex[] dataArray :
114-
{
115-
for(int idx = 0; idx < dataArray.Length;idx++)
116-
dataArray[idx] = (Complex)doubleArray[idx];
117-
break;
118-
}
119-
case Quaternion[] dataArray :
120-
{
121-
for(int idx = 0; idx < dataArray.Length;idx++)
122-
dataArray[idx] = new Quaternion(new Vector3(0,0,0),(float)doubleArray[idx]);
123-
break;
124-
}
125-
default :
126-
{
127-
throw new Exception("This method was not yet implemented for this type" + typeof(T).Name);
128-
}
129-
}
130-
131-
this.Shape = new Shape(doubleArray.Length);
132-
133-
return this;
134-
}
135-
}
136-
}
74+
}

src/NumSharp.Core/Creation/NdArray.Ones.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ public NDArray ones(Type dtype = null, params int[] shapes)
1717
for (int idx = 0; idx < dataLength;idx++)
1818
dataArray.SetValue(1,idx);
1919

20-
this.Storage = NDStorage.CreateByShapeAndType(dtype,new Shape(shapes));
20+
this.Storage = new NDStorage(dtype);
21+
this.Storage.Allocate(dtype,new Shape(shapes));
22+
2123
this.Storage.SetData(dataArray);
2224

2325
return this;

0 commit comments

Comments
 (0)