GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — pythonnet/src/embed_tests/NumPyTests.cs at master · pythonnet/pythonnet

Expand file tree

/

NumPyTests.cs

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
96 lines (80 loc) · 3.24 KB
/

NumPyTests.cs

Copy path

File metadata and controls

96 lines (80 loc) · 3.24 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using System;
using System.Collections.Generic;
using NUnit.Framework;
using Python.Runtime;
using Python.Runtime.Codecs;
namespace Python.EmbeddingTest
{
public class NumPyTests
{
[OneTimeSetUp]
public void SetUp()
{
TupleCodec<ValueTuple>.Register();
}
[OneTimeTearDown]
public void Dispose()
{
PyObjectConversions.Reset();
}
[Test]
public void TestReadme()
{
Assert.That(np.cos(np.pi * 2).ToString(), Is.EqualTo("1.0"));
dynamic sin = np.sin;
StringAssert.StartsWith("-0.95892", sin(5).ToString());
double c = (double)(np.cos(5) + sin(5));
Assert.That(c, Is.EqualTo(-0.675262).Within(0.01));
dynamic a = np.array(new List<float> { 1, 2, 3 });
Assert.That(a.dtype.ToString(), Is.EqualTo("float64"));
dynamic b = np.array(new List<float> { 6, 5, 4 }, Py.kw("dtype", np.int32));
Assert.That(b.dtype.ToString(), Is.EqualTo("int32"));
Assert.That((a * b).ToString().Replace(" ", " "), Is.EqualTo("[ 6. 10. 12.]"));
}
[Test]
public void MultidimensionalNumPyArray()
{
var array = new[,] { { 1, 2 }, { 3, 4 } };
var ndarray = np.InvokeMethod("asarray", array.ToPython());
Assert.That(ndarray.GetAttr("shape").As<(int, int)>(), Is.EqualTo((2, 2)));
Assert.That(ndarray[(0, 0).ToPython()].InvokeMethod("__int__").As<int>(), Is.EqualTo(1));
Assert.That(ndarray[(1, 0).ToPython()].InvokeMethod("__int__").As<int>(), Is.EqualTo(array[1, 0]));
}
[Test]
public void Int64Array()
{
var array = new long[,] { { 1, 2 }, { 3, 4 } };
var ndarray = np.InvokeMethod("asarray", array.ToPython());
Assert.That(ndarray.GetAttr("shape").As<(int, int)>(), Is.EqualTo((2, 2)));
Assert.That(ndarray[(0, 0).ToPython()].InvokeMethod("__int__").As<long>(), Is.EqualTo(1));
Assert.That(ndarray[(1, 0).ToPython()].InvokeMethod("__int__").As<long>(), Is.EqualTo(array[1, 0]));
}
[Test]
public void VarArg()
{
dynamic zX = np.array(new[,] { { 1, 2, 3 }, { 4, 5, 6 }, { 8, 9, 0 } });
dynamic grad = np.gradient(zX, 4.0, 5.0);
dynamic grad2 = np.InvokeMethod("gradient", new PyObject[] { zX, new PyFloat(4.0), new PyFloat(5.0) });
Assert.That(grad[0].sum().__float__().As<double>(), Is.EqualTo(4.125).Within(0.001));
Assert.That(grad[1].sum().__float__().As<double>(), Is.EqualTo(-1.2).Within(0.001));
Assert.That(grad2[0].sum().__float__().As<double>(), Is.EqualTo(4.125).Within(0.001));
Assert.That(grad2[1].sum().__float__().As<double>(), Is.EqualTo(-1.2).Within(0.001));
}
#pragma warning disable IDE1006
dynamic np
{
get
{
try
{
return Py.Import("numpy");
}
catch (PythonException)
{
Assert.Inconclusive("Numpy or dependency not installed");
return null;
}
}
}
}
}
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.