using System;
using System.Collections.Generic;
using NumSharp;
//using NumpyDotNet;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
int[,] arr = new int[2, 3] { { 5, 6, 7 }, { 8, 7, 6 } };
NDArray ndarr = np.array(arr);
Console.WriteLine(ndarr.ToString());
Console.WriteLine("------------------------");
//int[,] arr1 = np.arange(0, 9).reshape(3, 3);
NDArray arr1 = np.array(new int[,] { { 1, 2, 3 }, { 4, 5, 6 } });
var arr2 = np.matmul(arr1.T, arr1);
Console.WriteLine(arr2.ToString());
Console.WriteLine("------------------------");
var arr3 = arr2[":,1:3"];
Console.WriteLine(arr3.ToString());
Console.WriteLine("***************");
foreach (var item in arr3)
{
Console.WriteLine(item);
}
Console.WriteLine("***************");
IEnumerable<int> iter = arr3.AsIterator<int>();
foreach (var item in iter)
{
Console.WriteLine(item);
}
Console.ReadKey();
}
}
}
结果:
[[5, 6, 7],
[8, 7, 6]]
------------------------
[[17, 22, 27],
[22, 29, 36],
[27, 36, 45]]
------------------------
[[22, 27],
[29, 36],
[36, 45]]
***************
22
27
29
36
36
45
***************
22
27
29
36
36
45
官方例程:
using NumSharp;
var nd = np.full(5, 12); //[5, 5, 5 .. 5]
nd = np.zeros(12); //[0, 0, 0 .. 0]
nd = np.arange(12); //[0, 1, 2 .. 11]
// create a matrix
nd = np.zeros((3, 4)); //[0, 0, 0 .. 0]
nd = np.arange(12).reshape(3, 4);
// access data by index
var data = nd[1, 1];
// create a tensor
nd = np.arange(12);
// reshaping
data = nd.reshape(2, -1); //returning ndarray shaped (2, 6)
Shape shape = (2, 3, 2);
data = nd.reshape(shape); //Tuple implicitly casted to Shape
//or:
nd = nd.reshape(2, 3, 2);
// slicing tensor
data = nd[":, 0, :"]; //returning ndarray shaped (2, 1, 2)
data = nd[Slice.All, 0, Slice.All]; //equivalent to the line above.
// nd is currently shaped (2, 3, 2)
// get the 2nd vector in the 1st dimension
data = nd[1]; //returning ndarray shaped (3, 2)
// get the 3rd vector in the (axis 1, axis 2) dimension
data = nd[1, 2]; //returning ndarray shaped (2, )
// get flat representation of nd
data = nd.flat; //or nd.flatten() for a copy
// interate ndarray
foreach (object val in nd)
{
// val can be either boxed value-type or a NDArray.
}
var iter = nd.AsIterator<int>(); //a different T can be used to automatically perform cast behind the scenes.
while (iter.HasNext())
{
//read
int val = iter.MoveNext();
//write
iter.MoveNextReference() = 123; //set value to the next val
//note that setting is not supported when calling AsIterator<T>() where T is not the dtype of the ndarray.
}
与二维数组的互相转化
// See https://aka.ms/new-console-template for more information
using NumSharp;
//测试numsharp
int[,] ints = new int[2, 3] { { 1, 2, 3 }, { 4, 5, 6 } };
NDArray dArray1 = np.array(ints);
foreach (var item in dArray1)
{
Console.WriteLine($"{item}");
}
//dArray1.ToArray();
int a = dArray1.size;
int[] b = dArray1.shape;
Console.WriteLine($"{dArray1[0][1]},{a},{b[0]},{b[1]}");
int[,] ints1 = new int[dArray1.shape[0],dArray1.shape[1]];
for (int i = 0; i < ints1.GetLength(0); i++)
{
for (int j = 0; j < ints1.GetLength(1); j++)
{
ints1[i,j] = dArray1[i][j];
}
}
for (int i = 0; i < ints1.GetLength(0); i++)
{
for (int j = 0; j < ints1.GetLength(1); j++)
{
Console.WriteLine($"写入的值为:{ints1[i, j]}");
}
}
//结果:
/*
1
2
3
4
5
6
2,6,2,3
写入的值为:1
写入的值为:2
写入的值为:3
写入的值为:4
写入的值为:5
写入的值为:6
*/
矩阵求逆 MathNet.Numerics:
<ItemGroup>
<PackageReference Include="MathNet.Numerics" Version="5.0.0" />
<PackageReference Include="NumSharp" Version="0.30.0" />
</ItemGroup>
using MathNet.Numerics.LinearAlgebra;
using MathNet.Numerics.LinearAlgebra.Double;
using NumSharp;
using NumSharp.Generic;
int[][] mat1 =
[
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
double[][] m1 =
[
[1, 2, 3],
[0, 5, 6],
[0, 0, 9]
];
double[][] mat5 =
[
[1.1d, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
int[][] mat2 =
[
[1, 2, 3, 4],
[4, 5, 6, 8],
[7, 8, 9, 10]
];
NDArray? arr1 = np.array(mat1,np.float32);
var arr2 = np.matmul(mat1, mat2);
int a = 1;
Console.WriteLine(arr2.ToString());
Console.WriteLine("=================");
Console.WriteLine(arr2[$":,{a}"].ToString());
Console.WriteLine("=================");
//取所有行, 第0列到第2列, 不包括第二列
Console.WriteLine(arr2[$":,{a - 1}:{a + 1}"].ToString());
Console.WriteLine("-----------");
NDArray mat3 = arr2[$":,{a - 1}:{a + 1}"];
Console.WriteLine(mat3.ToString());
var mat1_1 = NDArrayTo2DDouble(m1);
Matrix<double> mat1_2 = DenseMatrix.OfArray(mat1_1);
var mat1_3 = mat1_2.Inverse();
Console.WriteLine(mat1_3);
Console.WriteLine(mat1_3* mat1_2);
var mat1_4 = mat1_3.ToArray();
Console.WriteLine(np.matmul(np.array(mat1_4), m1).ToString());
static double[,] NDArrayTo2DDouble(NDArray ndArray)
{
if (ndArray.Shape.NDim != 2)
{
return default;
}
// 创建一个与 ndArray 形状相同的二维 int 数组
double[,] ints = new double[ndArray.shape[0], ndArray.shape[1]];
// 复制 ndArray 的内容到 ints
for (int i = 0; i < ints.GetLength(0); i++)
{
for (int j = 0; j < ints.GetLength(1); j++)
{
// 注意:这里直接使用 ndArray[i, j] 访问元素,因为 NumSharp 支持这种索引方式
ints[i, j] = ndArray[i, j];
}
}
return ints;
}
static int[,] NDArrayTo2DInt(NDArray ndArray)
{
// 创建一个与 ndArray 形状相同的二维 int 数组
int[,] ints = new int[ndArray.shape[0], ndArray.shape[1]];
// 复制 ndArray 的内容到 ints
for (int i = 0; i < ints.GetLength(0); i++)
{
for (int j = 0; j < ints.GetLength(1); j++)
{
// 注意:这里直接使用 ndArray[i, j] 访问元素,因为 NumSharp 支持这种索引方式
ints[i, j] = ndArray[i, j];
}
}
return ints;
}