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
6 changes: 5 additions & 1 deletion src/NumSharp.Core/Casting/Implicit/NdArray.Implicit.Array.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ public partial class NDArray
public static implicit operator NDArray(Array d)
{
var ndArray = new NDArray();
ndArray.FromMultiDimArray(d);

if (d.GetType().GetElementType().IsArray)
ndArray.FromJaggedArray(d);
else
ndArray.FromMultiDimArray(d);

return ndArray;
}
Expand Down
5 changes: 5 additions & 0 deletions src/NumSharp.Core/Casting/NdArray.ToString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ protected string _ToMatrixString()

string[] dataParsed = new string[Storage.GetData().Length];

int tensorLayout = Storage.TensorLayout;
Storage.ChangeTensorLayout(0);

Array strg = Storage.GetData();

for (int idx = 0; idx < dataParsed.Length;idx++)
Expand Down Expand Up @@ -130,6 +133,8 @@ protected string _ToMatrixString()

returnValue += (String.Format(new CultureInfo("en-us"),elementFormat, strg.GetValue(strg.Length-1)) + "]])");

Storage.ChangeTensorLayout(tensorLayout);

return returnValue;
}
protected string _ParseNumber(object number, ref int noBefore,ref int noAfter)
Expand Down
79 changes: 79 additions & 0 deletions src/NumSharp.Core/Casting/NdArrayFromJaggedArr.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* NumSharp
* Copyright (C) 2018 Haiping Chen
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Apache License 2.0 as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the Apache License 2.0
* along with this program. If not, see <http://www.apache.org/licenses/LICENSE-2.0/>.
*/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Globalization;
using System.Collections;
using NumSharp.Core;

namespace NumSharp.Core
{
public partial class NDArray
{
public void FromJaggedArray(Array dotNetArray)
{
if(!dotNetArray.GetType().GetElementType().IsArray)
throw new Exception("Multi dim arrays are not allowed here!");

List<int> dimList = new List<int>();

dimList.Add(dotNetArray.Length);

object currentArr = dotNetArray;

while (currentArr.GetType().GetElementType().IsArray)
{
Array child = (Array) ((Array) currentArr).GetValue(0);
dimList.Add(child.Length);
currentArr = child;
}

Type elementType = currentArr.GetType().GetElementType();

int[] dims = dimList.ToArray();

Shape shape = new Shape(dims);
shape.ChangeTensorLayout(1);

NDArray nd = new NDArray(elementType,shape);

Array ndStrg = nd.Storage.GetData();

for (int idx = 0; idx < shape.Size;idx++)
{
int[] indexes = shape.GetDimIndexOutShape(idx);

Array puffer = (Array) dotNetArray.GetValue(indexes[0]);

for (int jdx = 1; jdx < indexes.Length-1;jdx++)
{
puffer = (Array) puffer.GetValue(indexes[jdx]);
}

ndStrg.SetValue(puffer.GetValue(indexes[indexes.Length-1]),nd.Storage.Shape.GetIndexInShape(indexes));
}

this.Storage = nd.Storage;
}

}
}
21 changes: 21 additions & 0 deletions test/NumSharp.UnitTest/Casting/NDArray.ImplicitCasts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using NumSharp.Core.Extensions;
using NumSharp.Core;

Expand All @@ -10,6 +11,26 @@ namespace NumSharp.UnitTest
[TestClass]
public class ImplicitCastTester
{
[TestMethod]
public void ConvertFromJagged()
{
double[][] a = new double[3][];
for(int idx = 0; idx < a.Length;idx++)
a[idx] = new double[2];


for (int idx = 0; idx < 3;idx++)
for (int jdx = 0; jdx < 2;jdx++)
a[idx][jdx] = 10 * idx + jdx;

NDArray b = a;

var c = b.MakeGeneric<double>();

for (int idx = 0; idx < 3;idx++)
for (int jdx = 0; jdx < 2;jdx++)
Assert.IsTrue(c[idx,jdx] == a[idx][jdx]);
}
[TestMethod]
public void FromDotNetVector()
{
Expand Down