forked from SciSharp/NumSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnp.array.cs
More file actions
107 lines (85 loc) · 3.29 KB
/
np.array.cs
File metadata and controls
107 lines (85 loc) · 3.29 KB
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
97
98
99
100
101
102
103
104
105
106
107
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Drawing;
namespace NumSharp.Core
{
public static partial class np
{
public static NDArray array(Array array, Type dtype = null, int ndim = 1)
{
dtype = (dtype == null) ? array.GetType().GetElementType() : dtype;
var nd = new NDArray(dtype);
if ((array.Rank == 1) && ( !array.GetType().GetElementType().IsArray ))
{
nd.Storage = new NDStorage(dtype);
nd.Storage.Allocate(dtype, new Shape(new int[] { array.Length }),1);
nd.Storage.SetData(array);
}
else
{
throw new Exception("Method is not implemeneted for multidimensional arrays or jagged arrays.");
}
return nd;
}
/*public static NDArray array(System.Drawing.Bitmap image)
{
var imageArray = new NDArray(typeof(Byte));
var bmpd = image.LockBits(new System.Drawing.Rectangle(0, 0, image.Width, image.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, image.PixelFormat);
var dataSize = bmpd.Stride * bmpd.Height;
var bytes = new byte[dataSize];
System.Runtime.InteropServices.Marshal.Copy(bmpd.Scan0, bytes, 0, dataSize);
image.UnlockBits(bmpd);
imageArray.Storage.Allocate(typeof(byte),new Shape(bmpd.Height, bmpd.Width, System.Drawing.Image.GetPixelFormatSize(image.PixelFormat) / 8),1);
imageArray.Storage.SetData(bytes);
return imageArray;
}*/
public static NDArray array<T>(T[][] data)
{
var nd = new NDArray(typeof(T), new Shape(data.Length, data[0].Length));
for (int row = 0; row < data.Length; row++)
{
for (int col = 0; col < data[row].Length; col++)
{
nd[row,col] = data[row][col];
}
}
return nd;
}
public static NDArray array<T>(T[,] data)
{
var nd = new NDArray(typeof(T), new Shape(data.GetLength(0), data.GetLength(1)));
for (int dim0 = 0; dim0 < data.GetLength(0); dim0++)
{
for (int dim1 = 0; dim1 < data.GetLength(1); dim1++)
{
nd[dim0, dim1] = data[dim0, dim1];
}
}
return nd;
}
public static NDArray array<T>(T[,,] data)
{
var nd = new NDArray(typeof(T), new Shape(data.GetLength(0), data.GetLength(1), data.GetLength(2)));
for (int dim0 = 0; dim0 < data.GetLength(0); dim0++)
{
for (int dim1 = 0; dim1 < data.GetLength(1); dim1++)
{
for (int dim2 = 0; dim2 < data.GetLength(2); dim2++)
{
nd[dim0,dim1,dim2] = data[dim0, dim1, dim2];
}
}
}
return nd;
}
public static NDArray array<T>(params T[] data)
{
var nd = new NDArray(typeof(T), data.Length);
nd.Storage.SetData<T>(data);
return nd;
}
}
}