Skip to content

Commit 5a634f5

Browse files
authored
Merge pull request #189 from dotChris90/master
allow jagged arrays in implicit cast
2 parents 137b368 + 314069e commit 5a634f5

File tree

4 files changed

+110
-1
lines changed

4 files changed

+110
-1
lines changed

src/NumSharp.Core/Casting/Implicit/NdArray.Implicit.Array.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ public partial class NDArray
3535
public static implicit operator NDArray(Array d)
3636
{
3737
var ndArray = new NDArray();
38-
ndArray.FromMultiDimArray(d);
38+
39+
if (d.GetType().GetElementType().IsArray)
40+
ndArray.FromJaggedArray(d);
41+
else
42+
ndArray.FromMultiDimArray(d);
3943

4044
return ndArray;
4145
}

src/NumSharp.Core/Casting/NdArray.ToString.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ protected string _ToMatrixString()
9292

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

95+
int tensorLayout = Storage.TensorLayout;
96+
Storage.ChangeTensorLayout(0);
97+
9598
Array strg = Storage.GetData();
9699

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

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

136+
Storage.ChangeTensorLayout(tensorLayout);
137+
133138
return returnValue;
134139
}
135140
protected string _ParseNumber(object number, ref int noBefore,ref int noAfter)
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
using NumSharp.Core;
27+
28+
namespace NumSharp.Core
29+
{
30+
public partial class NDArray
31+
{
32+
public void FromJaggedArray(Array dotNetArray)
33+
{
34+
if(!dotNetArray.GetType().GetElementType().IsArray)
35+
throw new Exception("Multi dim arrays are not allowed here!");
36+
37+
List<int> dimList = new List<int>();
38+
39+
dimList.Add(dotNetArray.Length);
40+
41+
object currentArr = dotNetArray;
42+
43+
while (currentArr.GetType().GetElementType().IsArray)
44+
{
45+
Array child = (Array) ((Array) currentArr).GetValue(0);
46+
dimList.Add(child.Length);
47+
currentArr = child;
48+
}
49+
50+
Type elementType = currentArr.GetType().GetElementType();
51+
52+
int[] dims = dimList.ToArray();
53+
54+
Shape shape = new Shape(dims);
55+
shape.ChangeTensorLayout(1);
56+
57+
NDArray nd = new NDArray(elementType,shape);
58+
59+
Array ndStrg = nd.Storage.GetData();
60+
61+
for (int idx = 0; idx < shape.Size;idx++)
62+
{
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++)
68+
{
69+
puffer = (Array) puffer.GetValue(indexes[jdx]);
70+
}
71+
72+
ndStrg.SetValue(puffer.GetValue(indexes[indexes.Length-1]),nd.Storage.Shape.GetIndexInShape(indexes));
73+
}
74+
75+
this.Storage = nd.Storage;
76+
}
77+
78+
}
79+
}

test/NumSharp.UnitTest/Casting/NDArray.ImplicitCasts.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System;
33
using System.Collections.Generic;
44
using System.Text;
5+
using System.Linq;
56
using NumSharp.Core.Extensions;
67
using NumSharp.Core;
78

@@ -10,6 +11,26 @@ namespace NumSharp.UnitTest
1011
[TestClass]
1112
public class ImplicitCastTester
1213
{
14+
[TestMethod]
15+
public void ConvertFromJagged()
16+
{
17+
double[][] a = new double[3][];
18+
for(int idx = 0; idx < a.Length;idx++)
19+
a[idx] = new double[2];
20+
21+
22+
for (int idx = 0; idx < 3;idx++)
23+
for (int jdx = 0; jdx < 2;jdx++)
24+
a[idx][jdx] = 10 * idx + jdx;
25+
26+
NDArray b = a;
27+
28+
var c = b.MakeGeneric<double>();
29+
30+
for (int idx = 0; idx < 3;idx++)
31+
for (int jdx = 0; jdx < 2;jdx++)
32+
Assert.IsTrue(c[idx,jdx] == a[idx][jdx]);
33+
}
1334
[TestMethod]
1435
public void FromDotNetVector()
1536
{

0 commit comments

Comments
 (0)