Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 138 additions & 0 deletions src/NumSharp.Core/Casting/NdArray.ToString.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
using System;
using NumSharp.Core;
using System.Linq;
using System.Globalization;
using System.Collections.Generic;

namespace NumSharp.Core
{
public partial class NDArray
{
public override string ToString()
{
string output = "";

if (this.ndim == 2)
{
if(dtype == typeof(int))
{
output = this._ToMatrixString<int>();
}
else if(dtype == typeof(double))
{
output = this._ToMatrixString<double>();
}

}
else
{
if (dtype == typeof(int))
{
//output = this._ToVectorString<int>();
}
else if (dtype == typeof(double))
{
//output = this._ToVectorString<double>();
}
}

return output;
}
protected string _ToVectorString<T>()
{
string returnValue = "array([";

int digitBefore = 0;
int digitAfter = 0;

var dataParsed = Storage.GetData<T>().Select(x => _ParseNumber(x,ref digitBefore,ref digitAfter)).ToArray();

string elementFormatStart = "{0:";

string elementFormatEnd = "";
for(int idx = 0; idx < digitAfter;idx++)
elementFormatEnd += "0";

elementFormatEnd += "}";

int missingDigits;
string elementFormat;

for (int idx = 0; idx < (Storage.Shape.Size-1);idx++)
{
missingDigits = digitBefore - dataParsed[idx].Replace(" ","").Split('.')[0].Length;

elementFormat = elementFormatStart + new string(Enumerable.Repeat<char>(' ',missingDigits).ToArray()) + "0." + elementFormatEnd;

returnValue += (String.Format(new CultureInfo("en-us"),elementFormat, Storage.GetData<T>()[idx]) + ", ");
}
missingDigits = digitBefore - dataParsed.Last().Replace(" ","").Split('.')[0].Length;

elementFormat = elementFormatStart + new string(Enumerable.Repeat<char>(' ',missingDigits).ToArray()) + "." + elementFormatEnd;

returnValue += (String.Format(new CultureInfo("en-us"),elementFormat, Storage.GetData<T>().Last()) + "])");

return returnValue;
}
protected string _ToMatrixString<T>()
{
string returnValue = "array([[";

int digitBefore = 0;
int digitAfter = 0;

string[] dataParsed = Storage.GetData<T>().Select(x => _ParseNumber(x, ref digitBefore, ref digitAfter)).ToArray();

string elementFormatStart = "{0:";

string elementFormatEnd = "";
for(int idx = 0; idx < digitAfter;idx++)
elementFormatEnd += "0";

elementFormatEnd += "}";

int missingDigits;
string elementFormat;

for (int idx = 0; idx < shape.NDim - 1; idx++)
{
missingDigits = digitBefore - dataParsed[idx].Replace(" ", "").Split('.')[0].Length;

elementFormat = elementFormatStart + new string(Enumerable.Repeat<char>(' ', missingDigits).ToArray()) + "0." + elementFormatEnd;

if (((idx + 1) % shape.Dimensions[1]) == 0)
{
returnValue += (String.Format(new CultureInfo("en-us"), elementFormat, Storage.GetData<T>()[idx]) + "], \n [");
}
else
{
returnValue += (String.Format(new CultureInfo("en-us"), elementFormat, Storage.GetData<T>()[idx]) + ", ");
}
}

missingDigits = digitBefore - dataParsed.Last().Replace(" ","").Split('.')[0].Length;

elementFormat = elementFormatStart + new string(Enumerable.Repeat<char>(' ',missingDigits).ToArray()) + "." + elementFormatEnd;

returnValue += (String.Format(new CultureInfo("en-us"),elementFormat, Storage.GetData<T>().Last()) + "]])");

return returnValue;
}
protected string _ParseNumber(object number, ref int noBefore,ref int noAfter)
{
string parsed = string.Format(new CultureInfo("en-us"),"{0:0.00000000}",number);

parsed = (parsed.StartsWith("-")) ? parsed : (" " + parsed);

int noBefore_local = parsed.Split('.')[0].Length;
int noAfter_local = parsed.Split('.')[1].ToCharArray().Reverse().SkipWhile(x => x == '0').ToArray().Length;

noBefore = (noBefore_local > noBefore) ? noBefore_local : noBefore;
noAfter = (noAfter_local > noAfter ) ? noAfter_local : noAfter;

return parsed;
}
}

}

24 changes: 12 additions & 12 deletions src/NumSharp.Core/Casting/NdArrayToJaggedArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ public Array ToJaggedArray<T>()
}
case 2 :
{
T[] data = Storage.GetData<T>();
T[][] dotNetArrayPuffer = new T[shape.Shapes[0]][];
T[][] dotNetArrayPuffer = new T[shape.Dimensions[0]][];
for (int idx = 0; idx < dotNetArrayPuffer.Length;idx++)
dotNetArrayPuffer[idx] = new T[shape.Shapes[1]];
dotNetArrayPuffer[idx] = new T[shape.Dimensions[1]];

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

dotNetArray = dotNetArrayPuffer;

Expand All @@ -57,19 +57,19 @@ public Array ToJaggedArray<T>()
case 3 :
{
T[] data = Storage.GetData<T>();
T[][][] dotNetArrayPuffer = new T[shape.Shapes[0]][][];
T[][][] dotNetArrayPuffer = new T[shape.Dimensions[0]][][];
for (int idx = 0; idx < dotNetArrayPuffer.Length;idx++)
{
dotNetArrayPuffer[idx] = new T[shape.Shapes[1]][];
dotNetArrayPuffer[idx] = new T[shape.Dimensions[1]][];
for (int jdx = 0; jdx < dotNetArrayPuffer[idx].Length;jdx++)
dotNetArrayPuffer[idx][jdx] = new T[shape.Shapes[2]];
dotNetArrayPuffer[idx][jdx] = new T[shape.Dimensions[2]];
}

for (int idx = 0; idx < shape.Shapes[0];idx++)
for (int jdx = 0;jdx < shape.Shapes[1];jdx++)
for(int kdx = 0; kdx < shape.Shapes[2];kdx++)
for (int idx = 0; idx < shape.Dimensions[0];idx++)
for (int jdx = 0;jdx < shape.Dimensions[1];jdx++)
for(int kdx = 0; kdx < shape.Dimensions[2];kdx++)
dotNetArrayPuffer[idx][jdx][kdx] = (T) this[idx,jdx,kdx];

dotNetArray = dotNetArrayPuffer;

break;
Expand Down
4 changes: 2 additions & 2 deletions src/NumSharp.Core/Casting/NdArrayToMultiDimArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ public Array ToMuliDimArray<T>()
case 2 :
{
T[] data = Storage.GetData<T>();
T[,] dotNetArrayPuffer = new T[shape.Shapes[0],shape.Shapes[1]];
T[,] dotNetArrayPuffer = new T[shape.Dimensions[0],shape.Dimensions[1]];
for (int idx = 0;idx < data.Length;idx++ )
dotNetArrayPuffer[idx/shape.Shapes[1],idx%shape.Shapes[1]] = data[idx];
dotNetArrayPuffer[idx/shape.Dimensions[1],idx%shape.Dimensions[1]] = data[idx];

dotNetArray = dotNetArrayPuffer;

Expand Down
2 changes: 1 addition & 1 deletion src/NumSharp.Core/Creation/NdArray.ARange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public NDArray arange(int stop, int start = 0, int step = 1)
}
}

this.Storage.Shape = new Shape(list.Length);
this.Storage.Reshape(list.Length);

return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public static matrix AsMatrix(this NDArray nd)
{
var npAsMatrix = new matrix(nd);

npAsMatrix.reshape(nd.shape);
npAsMatrix.reshape((Shape)nd.shape);

return npAsMatrix;
}
Expand Down
18 changes: 7 additions & 11 deletions src/NumSharp.Core/Creation/NdArray.Eye.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,17 @@ public NDArray eye(int dim, int diagonalIndex = 0)
}
else
{
puffer = new NDArray(this.dtype,this.shape.Shapes.ToArray());
puffer = new NDArray(this.dtype,this.shape.Dimensions.ToArray());
}

puffer.Storage.SetData(Array.CreateInstance(dtype,puffer.size));

Array storageArr = puffer.Storage.GetData();

if (diagonalIndex > -1)
puffer.Storage.SetData(Array.CreateInstance(dtype,puffer.size));

if (diagonalIndex >= 0)
for(int idx = 0; idx < noOfDiagElement;idx++ )
storageArr.SetValue(1,diagonalIndex + idx + idx * puffer.shape.Shapes[1]);
puffer.Storage.SetData(1,idx,idx+diagonalIndex);
else
for(int idx = dim - 1; idx > dim - noOfDiagElement - 1;idx-- )
storageArr.SetValue(1,diagonalIndex + idx + idx * puffer.shape.Shapes[1]); // = 1;

this.Storage = puffer.Storage;
for(int idx = puffer.Storage.Shape.Dimensions[0]-1; idx > puffer.Storage.Shape.Dimensions[0]-1 - noOfDiagElement;idx-- )
puffer.Storage.SetData(1,idx,idx+diagonalIndex);

return puffer;
}
Expand Down
16 changes: 9 additions & 7 deletions src/NumSharp.Core/Creation/NdArray.HStack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,14 @@ public NDArray hstack(params NDArray[] nps)
}
else
{
for(int idx = 0; idx < npAll.Length;idx++)
npAll[idx].Storage.ChangeTensorLayout(2);

var list = new List<object>();

int total = npAll[0].ndim == 1 ? 1 : npAll[0].shape.Shapes[0];
int total = npAll[0].ndim == 1 ? 1 : npAll[0].shape.Dimensions[0];

int pageSize = npAll[0].ndim == 1 ? npAll[0].shape.Shapes[0] : npAll[0].shape.DimOffset[0];
int pageSize = npAll[0].ndim == 1 ? npAll[0].shape.Dimensions[0] : npAll[0].shape.DimOffset[0];

for (int i = 0; i < total; i++)
{
Expand All @@ -54,18 +57,17 @@ public NDArray hstack(params NDArray[] nps)
}
}

nd.Storage.SetData( list.ToArray());

int[] shapes = npAll[0].shape.Shapes.ToArray();
int[] shapes = npAll[0].shape.Dimensions.ToArray();

if (shapes.Length == 1)
shapes[0] *= npAll.Length;
else
shapes[1] *= npAll.Length;

nd.Storage.Shape = new Shape(shapes);
nd.Storage.Allocate(nd.Storage.DType,new Shape(shapes),2);
nd.Storage.SetData(list.ToArray());
nd.Storage.ChangeTensorLayout(1);


}

return nd;
Expand Down
66 changes: 2 additions & 64 deletions src/NumSharp.Core/Creation/NdArray.LinSpace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,71 +66,9 @@ public NDArray linspace(double start, double stop,int num, bool entdpoint = true
}
}

this.Storage.Shape = new Shape(doubleArray.Length);
this.Storage.Reshape(doubleArray.Length);

return this;
}
}
public partial class NDArrayGeneric<T>
{
public NDArrayGeneric<T> linspace(double start, double stop,int num, bool entdpoint = true)
{
double steps = (stop - start)/((entdpoint) ? (double) num - 1.0 : (double) num);

double[] doubleArray = new double[num];

for (int idx = 0; idx < doubleArray.Length;idx++)
doubleArray[idx] = start + idx * steps;

Data = new T[doubleArray.Length];

switch (Data)
{
case int[] dataArray :
{
for(int idx = 0; idx < dataArray.Length;idx++)
dataArray[idx] = (int)doubleArray[idx];
break;
}
case long[] dataArray :
{
for(int idx = 0; idx < dataArray.Length;idx++)
dataArray[idx] = (long) doubleArray[idx];
break;
}
case double[] dataArray :
{
for(int idx = 0; idx < dataArray.Length;idx++)
dataArray[idx] = doubleArray[idx];
break;
}
case float[] dataArray :
{
for(int idx = 0; idx < dataArray.Length;idx++)
dataArray[idx] = (float)doubleArray[idx];
break;
}
case Complex[] dataArray :
{
for(int idx = 0; idx < dataArray.Length;idx++)
dataArray[idx] = (Complex)doubleArray[idx];
break;
}
case Quaternion[] dataArray :
{
for(int idx = 0; idx < dataArray.Length;idx++)
dataArray[idx] = new Quaternion(new Vector3(0,0,0),(float)doubleArray[idx]);
break;
}
default :
{
throw new Exception("This method was not yet implemented for this type" + typeof(T).Name);
}
}

this.Shape = new Shape(doubleArray.Length);

return this;
}
}
}
}
4 changes: 3 additions & 1 deletion src/NumSharp.Core/Creation/NdArray.Ones.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ public NDArray ones(Type dtype = null, params int[] shapes)
for (int idx = 0; idx < dataLength;idx++)
dataArray.SetValue(1,idx);

this.Storage = NDStorage.CreateByShapeAndType(dtype,new Shape(shapes));
this.Storage = new NDStorage(dtype);
this.Storage.Allocate(dtype,new Shape(shapes));

this.Storage.SetData(dataArray);

return this;
Expand Down
Loading