Skip to content

Commit aa570de

Browse files
committed
add NDArray to ValueType
1 parent af8b3a2 commit aa570de

File tree

2 files changed

+43
-12
lines changed

2 files changed

+43
-12
lines changed

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

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,20 @@
2323
using System.Text;
2424
using System.Globalization;
2525
using System.Collections;
26-
using NumSharp.Core;
2726
using System.Numerics;
2827

2928
namespace NumSharp.Core
3029
{
3130
public partial class NDArray
3231
{
32+
public static implicit operator NDArray(float d)
33+
{
34+
var ndArray = new NDArray(typeof(float), new int[0]);
35+
ndArray.Storage.SetData(new float[] { d });
36+
37+
return ndArray;
38+
}
39+
3340
public static implicit operator float(NDArray nd)
3441
{
3542
if (nd.ndim > 0)
@@ -38,20 +45,20 @@ public static implicit operator float(NDArray nd)
3845
return nd.Data<float>(0);
3946
}
4047

41-
public static implicit operator NDArray(float d)
48+
public static implicit operator NDArray(double d)
4249
{
43-
var ndArray = new NDArray(typeof(float), new int[0]);
44-
ndArray.Storage.SetData(new float[] { d });
50+
var ndArray = new NDArray(typeof(double),new int[0]);
51+
ndArray.Storage.SetData(new double[]{d});
4552

4653
return ndArray;
4754
}
4855

49-
public static implicit operator NDArray(double d)
56+
public static implicit operator double(NDArray nd)
5057
{
51-
var ndArray = new NDArray(typeof(double),new int[0]);
52-
ndArray.Storage.SetData(new double[]{d});
58+
if (nd.ndim > 0)
59+
throw new IncorrectShapeException();
5360

54-
return ndArray;
61+
return nd.Data<double>(0);
5562
}
5663

5764
public static implicit operator NDArray(short d)
@@ -62,6 +69,14 @@ public static implicit operator NDArray(short d)
6269
return ndArray;
6370
}
6471

72+
public static implicit operator short(NDArray nd)
73+
{
74+
if (nd.ndim > 0)
75+
throw new IncorrectShapeException();
76+
77+
return nd.Data<short>(0);
78+
}
79+
6580
public static implicit operator NDArray(int d)
6681
{
6782
var ndArray = new NDArray(typeof(int),new int[0]);
@@ -70,6 +85,14 @@ public static implicit operator NDArray(int d)
7085
return ndArray;
7186
}
7287

88+
public static implicit operator int(NDArray nd)
89+
{
90+
if (nd.ndim > 0)
91+
throw new IncorrectShapeException();
92+
93+
return nd.Data<int>(0);
94+
}
95+
7396
public static implicit operator NDArray(long d)
7497
{
7598
var ndArray = new NDArray(typeof(Int64),new int[0]);
@@ -78,6 +101,14 @@ public static implicit operator NDArray(long d)
78101
return ndArray;
79102
}
80103

104+
public static implicit operator long(NDArray nd)
105+
{
106+
if (nd.ndim > 0)
107+
throw new IncorrectShapeException();
108+
109+
return nd.Data<long>(0);
110+
}
111+
81112
public static implicit operator NDArray(Complex d)
82113
{
83114
var ndArray = new NDArray(typeof(Complex),new int[0]);

src/NumSharp.Core/NumSharp.Core.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
<PackageProjectUrl>https://github.com/SciSharp</PackageProjectUrl>
1212
<Copyright>Apache 2.0</Copyright>
1313
<RepositoryUrl>https://github.com/SciSharp/NumSharp</RepositoryUrl>
14-
<PackageReleaseNotes>Improvements</PackageReleaseNotes>
15-
<AssemblyVersion>0.6.5.0</AssemblyVersion>
16-
<FileVersion>0.6.5.0</FileVersion>
14+
<PackageReleaseNotes>add NDArray to ValueType</PackageReleaseNotes>
15+
<AssemblyVersion>0.6.6.0</AssemblyVersion>
16+
<FileVersion>0.6.6.0</FileVersion>
1717
<RepositoryType>git</RepositoryType>
1818
<PackageTags>NumPy, NumSharp, MachineLearning, Math, Scientific, Numeric</PackageTags>
19-
<Version>0.6.5</Version>
19+
<Version>0.6.6</Version>
2020
<PackageLicenseUrl>LICENSE</PackageLicenseUrl>
2121
<LangVersion>latest</LangVersion>
2222
<PackageIconUrl>https://avatars3.githubusercontent.com/u/44989469?s=200&amp;v=4</PackageIconUrl>

0 commit comments

Comments
 (0)