Skip to content

Commit f59912b

Browse files
committed
disable layout
1 parent d6987ce commit f59912b

18 files changed

Lines changed: 81 additions & 187 deletions

src/NumSharp.Core/Casting/NdArrayFromJaggedArr.cs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,21 @@ public void FromJaggedArray(Array dotNetArray)
5858

5959
Array ndStrg = nd.Storage.GetData();
6060

61-
for (int idx = 0; idx < shape.Size;idx++)
61+
if(dims.Length == 1)
6262
{
63-
int[] indexes = shape.GetDimIndexOutShape(idx);
64-
65-
Array puffer = (Array) dotNetArray.GetValue(indexes[0]);
66-
67-
for (int jdx = 1; jdx < indexes.Length-1;jdx++)
63+
throw new NotImplementedException("FromJaggedArray dims.Length == 1");
64+
}
65+
else if (dims.Length == 2)
66+
{
67+
switch (dotNetArray)
6868
{
69-
puffer = (Array) puffer.GetValue(indexes[jdx]);
69+
case double[][] array:
70+
for (int i = 0; i < dims[0]; i++)
71+
for (int j = 0; j < dims[1]; j++)
72+
nd[i, j] = array[i][j];
73+
break;
7074
}
71-
72-
ndStrg.SetValue(puffer.GetValue(indexes[indexes.Length-1]),nd.Storage.Shape.GetIndexInShape(indexes));
75+
7376
}
7477

7578
this.Storage = nd.Storage;

src/NumSharp.Core/Interfaces/IStorage.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@ public interface IStorage : ICloneable
5353
/// By this method the layout is changed if layout is not columnwise
5454
/// </summary>
5555
/// <returns>reference to storage (transformed or not)</returns>
56-
IStorage GetColumWiseStorage();
56+
//IStorage GetColumWiseStorage();
5757
/// <summary>
5858
/// Get Back Storage with row wise tensor Layout
5959
/// By this method the layout is changed if layout is not row wise
6060
/// </summary>
6161
/// <returns>reference to storage (transformed or not)</returns>
62-
IStorage GetRowWiseStorage();
62+
//IStorage GetRowWiseStorage();
6363
/// <summary>
6464
/// Get reference to internal data storage
6565
/// </summary>

src/NumSharp.Core/LinearAlgebra/NdArray.Dot.cs

Lines changed: 19 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -18,147 +18,34 @@ public partial class NDArray
1818
/// <returns>Scalarproduct or matrix prod</returns>
1919
public NDArray dot(NDArray nd2)
2020
{
21-
var pufferShape = nd2.Storage.Shape;
22-
23-
// in case must do a reshape
24-
var oldStorage1 = this.Storage;
25-
var oldStorage2 = nd2.Storage;
26-
if ((this.ndim == 0) & (nd2.ndim == 0))
21+
if(ndim == 2 && nd2.ndim == 1)
2722
{
28-
if (this.dtype == typeof(int))
23+
var nd = new NDArray(dtype, new Shape(shape[0]));
24+
switch (dtype.Name)
2925
{
30-
var ret = this.Data<int>()[0] * nd2.Data<int>()[0];
31-
return new NDArray(new int[] { ret }).reshape();
26+
case "Int32":
27+
for (int i = 0; i < shape[0]; i++)
28+
for (int j = 0; j < nd2.shape[0]; j++)
29+
nd.Data<int>()[i] += Data<int>(i, j) * nd2.Data<int>(j);
30+
break;
3231
}
33-
32+
return nd;
3433
}
35-
36-
if ((this.ndim == 1 ) & (nd2.ndim == 1))
37-
if (this.shape[0] != nd2.shape[0])
38-
throw new IncorrectShapeException();
39-
else
40-
{
41-
this.Storage = new NDStorage();
42-
this.Storage.Allocate(oldStorage1.DType,new Shape(1,oldStorage1.GetData().Length),1);
43-
this.Storage.SetData(oldStorage1.GetData());
44-
45-
nd2.Storage = new NDStorage();
46-
nd2.Storage.Allocate(oldStorage2.DType, new Shape(oldStorage2.GetData().Length,1),1);
47-
nd2.Storage.SetData(oldStorage2.GetData());
48-
}
49-
else
50-
if (this.shape[1] != nd2.shape[0])
51-
throw new IncorrectShapeException();
52-
53-
if ((this.ndim == 2) & (nd2.ndim == 1))
34+
else if (ndim == 2 && nd2.ndim == 2)
5435
{
55-
var pufferList = pufferShape.Dimensions.ToList();
56-
pufferList.Add(1);
57-
nd2.Storage.Reshape(pufferList.ToArray());
58-
}
59-
60-
int iterator = this.shape[1];
61-
int dim0 = this.shape[0];
62-
int dim1 = nd2.shape[1];
63-
64-
var prod = new NDArray(this.Storage.DType, new Shape(dim0, dim1));
65-
66-
Array nd1SystemArray = this.Storage.GetData();
67-
68-
switch (nd1SystemArray)
69-
{
70-
case int[] nd1Array :
36+
var nd = new NDArray(dtype, new Shape(shape[0], nd2.shape[1]));
37+
switch (dtype.Name)
7138
{
72-
int[] result = prod.Storage.GetData<int>();
73-
int[] nd2Array = nd2.Storage.GetData<int>();
74-
75-
for (int idx = 0; idx < prod.size; idx++)
76-
{
77-
int puffer1 = idx % dim0;
78-
int puffer2 = idx / dim0;
79-
int puffer3 = puffer2 * iterator;
80-
for (int kdx = 0; kdx < iterator; kdx++)
81-
result[idx] += nd2Array[puffer3 + kdx] * nd1Array[dim0 * kdx + puffer1];
82-
}
83-
break;
84-
}
85-
case double[] nd1Array :
86-
{
87-
double[] result = prod.Storage.GetData<double>();
88-
double[] nd2Array = nd2.Storage.GetData<double>();
89-
90-
for (int idx = 0; idx < prod.size; idx++)
91-
{
92-
int puffer1 = idx % dim0;
93-
int puffer2 = idx / dim0;
94-
int puffer3 = puffer2 * iterator;
95-
for (int kdx = 0; kdx < iterator; kdx++)
96-
result[idx] += nd2Array[puffer3 + kdx] * nd1Array[dim0 * kdx + puffer1];
97-
}
98-
break;
99-
}
100-
case float[] nd1Array :
101-
{
102-
float[] result = prod.Storage.GetData<float>();
103-
float[] nd2Array = nd2.Storage.GetData<float>();
104-
105-
for (int idx = 0; idx < prod.size; idx++)
106-
{
107-
int puffer1 = idx % dim0;
108-
int puffer2 = idx / dim0;
109-
int puffer3 = puffer2 * iterator;
110-
for (int kdx = 0; kdx < iterator; kdx++)
111-
result[idx] += nd2Array[puffer3 + kdx] * nd1Array[dim0 * kdx + puffer1];
112-
}
113-
break;
114-
}
115-
case Complex[] nd1Array :
116-
{
117-
Complex[] result = prod.Storage.GetData<Complex>();
118-
Complex[] nd2Array = nd2.Storage.GetData<Complex>();
119-
120-
for (int idx = 0; idx < prod.size; idx++)
121-
{
122-
int puffer1 = idx % dim0;
123-
int puffer2 = idx / dim0;
124-
int puffer3 = puffer2 * iterator;
125-
for (int kdx = 0; kdx < iterator; kdx++)
126-
result[idx] += nd2Array[puffer3 + kdx] * nd1Array[dim0 * kdx + puffer1];
127-
}
128-
break;
129-
}
130-
case Quaternion[] nd1Array :
131-
{
132-
Quaternion[] result = prod.Storage.GetData<Quaternion>();
133-
Quaternion[] nd2Array = nd2.Storage.GetData<Quaternion>();
134-
135-
for (int idx = 0; idx < prod.size; idx++)
136-
{
137-
int puffer1 = idx % dim0;
138-
int puffer2 = idx / dim0;
139-
int puffer3 = puffer2 * iterator;
140-
for (int kdx = 0; kdx < iterator; kdx++)
141-
result[idx] += nd2Array[puffer3 + kdx] * nd1Array[dim0 * kdx + puffer1];
142-
}
143-
break;
144-
}
145-
default :
146-
{
147-
throw new NotImplementedException();
39+
case "Int32":
40+
for (int i = 0; i < shape[1]; i++)
41+
for (int j = 0; j < nd2.shape[0]; j++)
42+
nd[i, j] = nd.Data<int>(i, j) + Data<int>(i, j) * nd2.Data<int>(j);
43+
break;
14844
}
45+
return nd;
14946
}
15047

151-
if ((this.ndim == 1 ) & (nd2.ndim == 1))
152-
{
153-
this.Storage.Reshape(this.Storage.GetData().Length);
154-
nd2.Storage.Reshape(nd2.Storage.GetData().Length);
155-
prod.Storage.Reshape(1);
156-
}
157-
158-
this.Storage = oldStorage1;
159-
nd2.Storage = oldStorage2;
160-
161-
return prod;
48+
throw new NotImplementedException($"dot {ndim} * {nd2.ndim}");
16249
}
16350
}
16451
}

src/NumSharp.Core/NDStorage.cs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class NDStorage : IStorage
2828
protected Type _DType;
2929
protected Shape _Shape;
3030
protected int _TensorLayout;
31-
protected void _ChangeRowToColumnLayout()
31+
/*protected void _ChangeRowToColumnLayout()
3232
{
3333
if ( _Shape.NDim == 1 )
3434
{
@@ -93,7 +93,7 @@ protected void _ChangeColumnToRowLayout()
9393
}
9494
_TensorLayout = 1;
9595
Shape.ChangeTensorLayout(1);
96-
}
96+
}*/
9797
protected Array _ChangeTypeOfArray(Array arrayVar, Type dtype)
9898
{
9999
Array newValues = null;
@@ -203,28 +203,28 @@ public NDStorage()
203203
_DType = np.float64;
204204
_values = new double[1];
205205
_Shape = new Shape(1);
206-
_TensorLayout = 1;
206+
//_TensorLayout = 1;
207207
}
208208
public NDStorage(Type dtype)
209209
{
210210
_DType = dtype;
211211
_values = Array.CreateInstance(dtype,1);
212212
_Shape = new Shape(1);
213-
_TensorLayout = 1;
213+
//_TensorLayout = 1;
214214
}
215215
public NDStorage(double[] values)
216216
{
217217
_DType = typeof(double);
218218
_Shape = new Shape(values.Length);
219219
_values = values;
220-
_TensorLayout = 1;
220+
//_TensorLayout = 1;
221221
}
222222
public NDStorage(object[] values)
223223
{
224224
_DType = values.GetType().GetElementType();
225225
_Shape = new Shape(values.Length);
226226
_values = values;
227-
_TensorLayout = 1;
227+
//_TensorLayout = 1;
228228
}
229229
/// <summary>
230230
/// Allocate memory by dtype, shape, tensororder (default column wise)
@@ -242,7 +242,7 @@ public void Allocate(Type dtype, Shape shape, int tensorOrder = 1)
242242
elementNumber *= shape.Dimensions[idx];
243243

244244
_values = Array.CreateInstance(dtype,elementNumber);
245-
_TensorLayout = tensorOrder;
245+
//_TensorLayout = tensorOrder;
246246
}
247247
/// <summary>
248248
/// Allocate memory by Array and tensororder and deduce shape and dtype (default column wise)
@@ -251,7 +251,7 @@ public void Allocate(Type dtype, Shape shape, int tensorOrder = 1)
251251
/// <param name="tensorOrder">row or column wise</param>
252252
public void Allocate(Array values, int tensorOrder = 1)
253253
{
254-
_TensorLayout = tensorOrder;
254+
//_TensorLayout = tensorOrder;
255255
int[] dim = new int[values.Rank];
256256
for (int idx = 0; idx < dim.Length;idx++)
257257
dim[idx] = values.GetLength(idx);
@@ -270,8 +270,8 @@ public void Allocate(Array values, int tensorOrder = 1)
270270
/// <returns>reference to storage (transformed or not)</returns>
271271
public IStorage GetColumWiseStorage()
272272
{
273-
if ( _TensorLayout != 2 )
274-
this._ChangeRowToColumnLayout();
273+
//if ( _TensorLayout != 2 )
274+
//this._ChangeRowToColumnLayout();
275275

276276
return this;
277277
}
@@ -282,8 +282,8 @@ public IStorage GetColumWiseStorage()
282282
/// <returns>reference to storage (transformed or not)</returns>
283283
public IStorage GetRowWiseStorage()
284284
{
285-
if ( _TensorLayout != 1 )
286-
this._ChangeColumnToRowLayout();
285+
//if ( _TensorLayout != 1 )
286+
//this._ChangeColumnToRowLayout();
287287

288288
return this;
289289
}
@@ -355,6 +355,7 @@ public T[] CloneData<T>()
355355

356356
return puffer as T[];
357357
}
358+
358359
/// <summary>
359360
/// Get single value from internal storage and do not cast dtype
360361
/// </summary>
@@ -404,6 +405,7 @@ public object GetData(params int[] indexes)
404405
throw new Exception("indexes must be equal to number of dimension.");
405406
return element;
406407
}
408+
407409
/// <summary>
408410
/// Get single value from internal storage as type T and cast dtype to T
409411
/// </summary>
@@ -412,11 +414,11 @@ public object GetData(params int[] indexes)
412414
/// <returns>element from internal storage</returns>
413415
public T GetData<T>(params int[] indexes)
414416
{
415-
this.ChangeDataType(this.DType);
416417
T[] values = this.GetData() as T[];
417418

418419
return values[Shape.GetIndexInShape(indexes)];
419420
}
421+
420422
/// <summary>
421423
/// Set an array to internal storage and keep dtype
422424
/// </summary>
@@ -472,17 +474,17 @@ public void SetNewShape(params int[] dimensions)
472474
}
473475
public void Reshape(params int[] dimensions)
474476
{
475-
if (_TensorLayout == 2)
477+
//if (_TensorLayout == 2)
476478
{
477479
_Shape = new Shape(dimensions);
478480
}
479-
else
481+
/*else
480482
{
481483
ChangeTensorLayout(2);
482484
_Shape = new Shape(dimensions);
483485
_Shape.ChangeTensorLayout(2);
484486
ChangeTensorLayout(1);
485-
}
487+
}*/
486488
}
487489
public object Clone()
488490
{
@@ -499,11 +501,11 @@ public object Clone()
499501
/// <returns>success or not</returns>
500502
public void ChangeTensorLayout(int layout)
501503
{
502-
if (layout != _TensorLayout)
504+
/*if (layout != _TensorLayout)
503505
if (_TensorLayout == 1)
504506
_ChangeRowToColumnLayout();
505507
else
506-
_ChangeColumnToRowLayout();
508+
_ChangeColumnToRowLayout();*/
507509
}
508510
}
509511
}

src/NumSharp.Core/NdArray.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ public partial class NDArray : ICloneable, IEnumerable
6868

6969
public T Data<T>(int index) => Storage.GetData<T>()[index];
7070

71+
public T Data<T>(params int[] indexes) => Storage.GetData<T>(indexes);
72+
7173
public Array Data() => Storage.GetData(dtype);
7274

7375
public T Max<T>() => Data<T>().Max();

src/NumSharp.Core/Shape.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ protected void _SetDimOffset()
2525
}
2626
else
2727
{
28-
if (this._TensorLayout == 1)
28+
/*if (this._TensorLayout == 1)
2929
{
3030
_DimOffset[0] = 1;
3131
3232
for(int idx = 1;idx < _DimOffset.Length;idx++)
3333
_DimOffset[idx] = _DimOffset[idx-1] * this._Dimensions[idx-1];
3434
}
35-
else if ( _TensorLayout == 2)
35+
else if ( _TensorLayout == 2)*/
3636
{
3737
_DimOffset[_DimOffset.Length-1] = 1;
3838
for(int idx = _DimOffset.Length-1;idx >= 1;idx--)

0 commit comments

Comments
 (0)