Skip to content

Commit 224932d

Browse files
author
dotchris90
committed
Feat : Add implicit cast
1 parent d7c59c5 commit 224932d

File tree

5 files changed

+339
-19
lines changed

5 files changed

+339
-19
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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+
33+
// User-defined conversion from double to Digit
34+
public static implicit operator NDArray(Array d)
35+
{
36+
var ndArray = new NDArray();
37+
ndArray.FromMultiDimArray(d);
38+
39+
return ndArray;
40+
}
41+
public static implicit operator Array(NDArray nd)
42+
{
43+
var methods = nd.GetType().GetMethods().Where(x => x.Name.Equals("ToMuliDimArray") && x.IsGenericMethod && x.ReturnType.Name.Equals("Array"));
44+
var genMethods = methods.First().MakeGenericMethod(nd.dtype);
45+
46+
return (Array) genMethods.Invoke(nd,null) ;
47+
}
48+
public static implicit operator NDArray(string str)
49+
{
50+
if (!str.StartsWith("["))
51+
throw new Exception("cannot cast this string to ndarray");
52+
else
53+
str = str.Replace("[","");
54+
55+
if (!str.EndsWith("]"))
56+
throw new Exception("cannot cast this string to ndarray");
57+
else
58+
str = str.Replace("]","");
59+
60+
61+
string[][] splitted = null;
62+
63+
if (str.Contains(","))
64+
{
65+
splitted = str.Split(';')
66+
.Select(x => x.Split(',') )
67+
.ToArray();
68+
69+
}
70+
else
71+
{
72+
splitted = str.Split(';')
73+
.Select(x => x.Split(' ') )
74+
.ToArray();
75+
}
76+
77+
78+
int dim0 = splitted.Length;
79+
int dim1 = splitted[0].Length;
80+
81+
var shape = new Shape( new int[] { dim0, dim1 });
82+
83+
NDArray nd = new NDArray(typeof(double));
84+
85+
nd.Storage.Allocate(nd.dtype,shape,1);
86+
87+
for (int idx = 0; idx< splitted.Length;idx++)
88+
{
89+
for (int jdx = 0; jdx < splitted[0].Length;jdx++)
90+
{
91+
nd[idx,jdx] = Double.Parse(splitted[idx][jdx]);
92+
}
93+
}
94+
95+
return nd;
96+
}
97+
}
98+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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 FromMultiDimArray(Array dotNetArray)
33+
{
34+
if(dotNetArray.GetType().GetElementType().IsArray)
35+
throw new Exception("Jagged arrays are not allowed here!");
36+
37+
int[] dims = new int[dotNetArray.Rank];
38+
39+
for(int idx = 0; idx < dims.Length;idx++)
40+
dims[idx] = dotNetArray.GetLength(idx);
41+
42+
Storage = new NDStorage();
43+
Storage.Allocate(dotNetArray.GetType().GetElementType(),new Shape(dims),1);
44+
45+
Array internalStrg = Storage.GetData();
46+
47+
var pufferShape = new Shape(dims);
48+
pufferShape.ChangeTensorLayout(2);
49+
50+
int[] idxDims = null;
51+
object valueIdx = null;
52+
53+
for(int idx = 0; idx < Storage.Shape.Size;idx++)
54+
{
55+
idxDims = pufferShape.GetDimIndexOutShape(idx);
56+
valueIdx = dotNetArray.GetValue(pufferShape.GetDimIndexOutShape(idx));
57+
internalStrg.SetValue(valueIdx,Storage.Shape.GetIndexInShape(idxDims));
58+
}
59+
}
60+
61+
}
62+
}

src/NumSharp.Core/Casting/NdArrayToMultiDimArray.cs

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,27 +31,21 @@ public partial class NDArray
3131
{
3232
public Array ToMuliDimArray<T>()
3333
{
34-
Array dotNetArray = null;
34+
Array dotNetArray = Array.CreateInstance(typeof(T),this.shape.Dimensions);
3535

36-
switch (ndim)
37-
{
38-
case 1 :
39-
{
40-
dotNetArray = Storage.GetData<T>() ;
41-
break;
42-
}
43-
case 2 :
44-
{
45-
T[] data = Storage.GetData<T>();
46-
T[,] dotNetArrayPuffer = new T[shape.Dimensions[0],shape.Dimensions[1]];
47-
for (int idx = 0;idx < data.Length;idx++ )
48-
dotNetArrayPuffer[idx/shape.Dimensions[1],idx%shape.Dimensions[1]] = data[idx];
49-
50-
dotNetArray = dotNetArrayPuffer;
51-
52-
break;
53-
}
36+
var pufferShape = new Shape(shape.Dimensions);
37+
pufferShape.ChangeTensorLayout(2);
38+
39+
int[] indexes = null;
40+
object idxValue = null;
5441

42+
T[] array = Storage.GetData<T>();
43+
44+
for(int idx = 0; idx < shape.Size;idx++)
45+
{
46+
indexes = pufferShape.GetDimIndexOutShape(idx);
47+
idxValue = array[shape.GetIndexInShape(indexes)];
48+
dotNetArray.SetValue(idxValue,indexes);
5549
}
5650

5751
return dotNetArray;
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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.Generic
29+
{
30+
public partial class NDArray
31+
{
32+
33+
// User-defined conversion from double to Digit
34+
public static implicit operator NDArray<T>(Array d)
35+
{
36+
var ndArray = new NDArray();
37+
ndArray.FromMultiDimArray(d);
38+
39+
return ndArray;
40+
}
41+
public static implicit operator Array(NDArray nd)
42+
{
43+
var methods = nd.GetType().GetMethods().Where(x => x.Name.Equals("ToMuliDimArray") && x.IsGenericMethod && x.ReturnType.Name.Equals("Array"));
44+
var genMethods = methods.First().MakeGenericMethod(nd.dtype);
45+
46+
return (Array) genMethods.Invoke(nd,null) ;
47+
}
48+
public static implicit operator NDArray(string str)
49+
{
50+
if (!str.StartsWith("["))
51+
throw new Exception("cannot cast this string to ndarray");
52+
else
53+
str = str.Replace("[","");
54+
55+
if (!str.EndsWith("]"))
56+
throw new Exception("cannot cast this string to ndarray");
57+
else
58+
str = str.Replace("]","");
59+
60+
61+
string[][] splitted = null;
62+
63+
if (str.Contains(","))
64+
{
65+
splitted = str.Split(';')
66+
.Select(x => x.Split(',') )
67+
.ToArray();
68+
69+
}
70+
else
71+
{
72+
splitted = str.Split(';')
73+
.Select(x => x.Split(' ') )
74+
.ToArray();
75+
}
76+
77+
78+
int dim0 = splitted.Length;
79+
int dim1 = splitted[0].Length;
80+
81+
var shape = new Shape( new int[] { dim0, dim1 });
82+
83+
NDArray nd = new NDArray(typeof(double));
84+
85+
nd.Storage.Allocate(nd.dtype,shape,1);
86+
87+
for (int idx = 0; idx< splitted.Length;idx++)
88+
{
89+
for (int jdx = 0; jdx < splitted[0].Length;jdx++)
90+
{
91+
nd[idx,jdx] = Double.Parse(splitted[idx][jdx]);
92+
}
93+
}
94+
95+
return nd;
96+
}
97+
}
98+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Text;
5+
using NumSharp.Core.Extensions;
6+
using NumSharp.Core;
7+
8+
namespace NumSharp.UnitTest
9+
{
10+
[TestClass]
11+
public class ImplicitCastTester
12+
{
13+
[TestMethod]
14+
public void FromDotNetVector()
15+
{
16+
NDArray nd = new double[]{1,2,3,4};
17+
18+
Assert.IsTrue(((double)nd[0]) == 1);
19+
Assert.IsTrue(((double)nd[1]) == 2);
20+
Assert.IsTrue(((double)nd[2]) == 3);
21+
Assert.IsTrue(((double)nd[3]) == 4);
22+
}
23+
[TestMethod]
24+
public void FromDotNetMatrix()
25+
{
26+
NDArray nd = new double[,]{{1,2,3},{4,5,6}};
27+
28+
var doubleMatr = new double[,]{{1,2,3},{4,5,6}};
29+
30+
for(int idx = 0; idx < doubleMatr.GetLength(0);idx++)
31+
for(int jdx = 0; jdx < doubleMatr.GetLength(1);jdx++)
32+
Assert.IsTrue((double)nd[idx,jdx] == doubleMatr[idx,jdx]);
33+
}
34+
35+
[TestMethod]
36+
public void FromAndToDotNetMatrix()
37+
{
38+
NDArray nd = new double[,]{{1,2,3},{4,5,6}};
39+
40+
double[,] nd_ = new double[,]{{1,2,3},{4,5,6}};
41+
42+
Array arr = nd;
43+
44+
double[,] doubleMatr = (double[,]) arr;
45+
46+
for(int idx = 0; idx < doubleMatr.GetLength(0);idx++)
47+
for(int jdx = 0; jdx < doubleMatr.GetLength(1);jdx++)
48+
{
49+
Assert.IsTrue((double)nd[idx,jdx] == doubleMatr[idx,jdx]);
50+
Assert.IsTrue(nd_[idx,jdx] == doubleMatr[idx,jdx]);
51+
}
52+
53+
}
54+
[TestMethod]
55+
public void StringCast()
56+
{
57+
NDArray nd = "[1,2,3;4,5,6]";
58+
59+
var doubleMatr = new double[,]{{1,2,3},{4,5,6}};
60+
61+
for(int idx = 0; idx < doubleMatr.GetLength(0);idx++)
62+
for(int jdx = 0; jdx < doubleMatr.GetLength(1);jdx++)
63+
Assert.IsTrue((double)nd[idx,jdx] == doubleMatr[idx,jdx]);
64+
}
65+
66+
}
67+
68+
}

0 commit comments

Comments
 (0)