Skip to content

Commit fa4bb60

Browse files
committed
add np.frombuffer
add np.prod
1 parent 0fc2dc0 commit fa4bb60

13 files changed

Lines changed: 131 additions & 13 deletions

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using NumSharp.Core.Extensions;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Text;
5+
6+
namespace NumSharp.Core
7+
{
8+
public static partial class np
9+
{
10+
public static NDArray frombuffer(byte[] bytes, Type dtype)
11+
{
12+
if(dtype.Name == "Int32")
13+
{
14+
var size = bytes.Length / sizeof(int);
15+
var ints = new int[size];
16+
for (var index = 0; index < size; index++)
17+
{
18+
ints[index] = BitConverter.ToInt32(bytes, index * sizeof(int));
19+
}
20+
21+
return new NDArray(ints);
22+
}
23+
24+
throw new NotImplementedException("");
25+
}
26+
}
27+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace NumSharp.Core
6+
{
7+
public static partial class np
8+
{
9+
public static NDArray prod(NDArray nd, int axis = -1, Type dtype = null)
10+
{
11+
NDArray result = null;
12+
if(axis == -1)
13+
{
14+
switch (nd.dtype.Name)
15+
{
16+
case "Int32":
17+
{
18+
int prod = 1;
19+
for (int i = 0; i < nd.size; i++)
20+
prod *= nd.Data<int>(i);
21+
result = prod;
22+
}
23+
break;
24+
case "Int64":
25+
{
26+
long prod = 1;
27+
for (int i = 0; i < nd.size; i++)
28+
prod *= nd.Data<long>(i);
29+
result = prod;
30+
}
31+
break;
32+
}
33+
}
34+
else
35+
{
36+
throw new NotImplementedException($"np.prod axis {axis}");
37+
}
38+
39+
return result;
40+
}
41+
}
42+
}

src/NumSharp.Core/NDStorage.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ public T[] GetData<T>()
339339
if (typeof(T) != this._DType)
340340
this.ChangeDataType(typeof(T));
341341

342-
return (_values as T[]);
342+
return _values as T[];
343343
}
344344
/// <summary>
345345
/// Get all elements from cloned storage as T[] and cast dtype
@@ -353,7 +353,7 @@ public T[] CloneData<T>()
353353
if (puffer.GetType().GetElementType() != typeof(T))
354354
puffer = _ChangeTypeOfArray(puffer,typeof(T));
355355

356-
return (puffer as T[]);
356+
return puffer as T[];
357357
}
358358
/// <summary>
359359
/// Get single value from internal storage and do not cast dtype

src/NumSharp.Core/NumSharp.Core.csproj

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@
1111
<PackageProjectUrl>https://github.com/SciSharp</PackageProjectUrl>
1212
<Copyright>Apache 2.0</Copyright>
1313
<RepositoryUrl>https://github.com/SciSharp/NumSharp</RepositoryUrl>
14-
<PackageReleaseNotes>Added np.concatenate</PackageReleaseNotes>
15-
<AssemblyVersion>0.7.4.0</AssemblyVersion>
16-
<FileVersion>0.7.4.0</FileVersion>
14+
<PackageReleaseNotes>Add np.frombuffer
15+
Add np.prod</PackageReleaseNotes>
16+
<AssemblyVersion>0.7.5.0</AssemblyVersion>
17+
<FileVersion>0.7.5.0</FileVersion>
1718
<RepositoryType>git</RepositoryType>
1819
<PackageTags>NumPy, NumSharp, MachineLearning, Math, Scientific, Numeric</PackageTags>
19-
<Version>0.7.4</Version>
20+
<Version>0.7.5</Version>
2021
<PackageLicenseUrl>LICENSE</PackageLicenseUrl>
2122
<LangVersion>latest</LangVersion>
2223
<PackageIconUrl>https://avatars3.githubusercontent.com/u/44989469?s=200&amp;v=4</PackageIconUrl>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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 System.Linq;
7+
using NumSharp.Core;
8+
9+
namespace NumSharp.UnitTest.Creation
10+
{
11+
[TestClass]
12+
public class NpFromBufferTest
13+
{
14+
[TestMethod]
15+
public void ToInt32()
16+
{
17+
int[] ints = { 100, 200, 300, 400, 500};
18+
byte[] bytes = new byte[ints.Length * sizeof(int)];
19+
Buffer.BlockCopy(ints, 0, bytes, 0, bytes.Length);
20+
21+
var nd = np.frombuffer(bytes, np.int32);
22+
Assert.AreEqual((int)nd[0], 100);
23+
Assert.AreEqual((int)nd[4], 500);
24+
}
25+
}
26+
}

test/NumSharp.UnitTest/LinearAlgebra/NdArray.Dot.Test.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
using Microsoft.VisualStudio.TestTools.UnitTesting;
88
using NumSharp.Core;
99

10-
namespace NumSharp.UnitTest.Extensions
10+
namespace NumSharp.UnitTest.LinearAlgebra
1111
{
1212
/// <summary>
1313
/// Test concolve with standard example from

test/NumSharp.UnitTest/LinearAlgebra/NdArray.Inv.Test.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
using System.Linq;
77
using NumSharp.Core;
88

9-
namespace NumSharp.UnitTest.Extensions
9+
namespace NumSharp.UnitTest.LinearAlgebra
1010
{
1111
[TestClass]
1212
public class NdArrayInvTest

test/NumSharp.UnitTest/LinearAlgebra/NdArray.QR.Test.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
using Microsoft.VisualStudio.TestTools.UnitTesting;
88
using NumSharp.Core;
99

10-
namespace NumSharp.UnitTest.Extensions
10+
namespace NumSharp.UnitTest.LinearAlgebra
1111
{
1212
/// <summary>
1313
/// Test concolve with standard example from

test/NumSharp.UnitTest/LinearAlgebra/NdArray.matrix_power.Test.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
using Microsoft.VisualStudio.TestTools.UnitTesting;
88
using NumSharp.Core;
99

10-
namespace NumSharp.UnitTest.Extensions
10+
namespace NumSharp.UnitTest.LinearAlgebra
1111
{
1212
/// <summary>
1313
/// Test concolve with standard example from
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using NumSharp.Core;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Text;
6+
7+
namespace NumSharp.UnitTest.LinearAlgebra
8+
{
9+
[TestClass]
10+
public class NpProdTest
11+
{
12+
[TestMethod]
13+
public void NoAxis()
14+
{
15+
int p = np.prod(new int[] { 1, 2, 3 });
16+
Assert.AreEqual(p, 6);
17+
18+
p = np.prod(new int[][] { new int[] { 1, 2 }, new int[] { 3, 4 } });
19+
Assert.AreEqual(p, 24);
20+
}
21+
}
22+
}

0 commit comments

Comments
 (0)