From 4ddfb2f2de83e5b1d0c9dd08dac3b691e0c106c4 Mon Sep 17 00:00:00 2001 From: dotchris90 Date: Mon, 7 Jan 2019 22:49:35 +0100 Subject: [PATCH] Fix : allow jagged arrays for implicit cast --- .../Implicit/NdArray.Implicit.Array.cs | 6 +- src/NumSharp.Core/Casting/NdArray.ToString.cs | 5 ++ .../Casting/NdArrayFromJaggedArr.cs | 79 +++++++++++++++++++ .../Casting/NDArray.ImplicitCasts.cs | 21 +++++ 4 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 src/NumSharp.Core/Casting/NdArrayFromJaggedArr.cs diff --git a/src/NumSharp.Core/Casting/Implicit/NdArray.Implicit.Array.cs b/src/NumSharp.Core/Casting/Implicit/NdArray.Implicit.Array.cs index faa658ac..224132d0 100644 --- a/src/NumSharp.Core/Casting/Implicit/NdArray.Implicit.Array.cs +++ b/src/NumSharp.Core/Casting/Implicit/NdArray.Implicit.Array.cs @@ -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; } diff --git a/src/NumSharp.Core/Casting/NdArray.ToString.cs b/src/NumSharp.Core/Casting/NdArray.ToString.cs index e57e706d..e63450db 100644 --- a/src/NumSharp.Core/Casting/NdArray.ToString.cs +++ b/src/NumSharp.Core/Casting/NdArray.ToString.cs @@ -73,6 +73,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++) @@ -111,6 +114,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) diff --git a/src/NumSharp.Core/Casting/NdArrayFromJaggedArr.cs b/src/NumSharp.Core/Casting/NdArrayFromJaggedArr.cs new file mode 100644 index 00000000..1608cc06 --- /dev/null +++ b/src/NumSharp.Core/Casting/NdArrayFromJaggedArr.cs @@ -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 . + */ + +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 dimList = new List(); + + 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; + } + + } +} diff --git a/test/NumSharp.UnitTest/Casting/NDArray.ImplicitCasts.cs b/test/NumSharp.UnitTest/Casting/NDArray.ImplicitCasts.cs index c139b4d0..95ee09ed 100644 --- a/test/NumSharp.UnitTest/Casting/NDArray.ImplicitCasts.cs +++ b/test/NumSharp.UnitTest/Casting/NDArray.ImplicitCasts.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Text; +using System.Linq; using NumSharp.Core.Extensions; using NumSharp.Core; @@ -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(); + + 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() {