forked from SciSharp/NumSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix.cs
More file actions
86 lines (73 loc) · 2.46 KB
/
Matrix.cs
File metadata and controls
86 lines (73 loc) · 2.46 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
using System;
using System.Linq;
namespace NumSharp.Core
{
public partial class matrix: NDArray
{
public matrix(NDArray data, Type dtype = null)
{
this.Storage = data.Storage;
}
public matrix(string matrixString, Type dtype = null)
{
string[][] splitted = null;
dtype = (dtype == null) ? np.float64 : dtype;
if (matrixString.Contains(","))
{
splitted = matrixString.Split(';')
.Select(x => x.Split(',') )
.ToArray();
}
else
{
splitted = matrixString.Split(';')
.Select(x => x.Split(' ') )
.ToArray();
}
int dim0 = splitted.Length;
int dim1 = splitted[0].Length;
var shape = new Shape( new int[] { dim0, dim1 });
this.Storage.Allocate(dtype,shape,1);
switch (this.dtype.Name)
{
case "Double": StringToDoubleMatrix(splitted); break;
case "Float": ; break;
}
}
/// <summary>
/// Convert a string to Double[,] and store
/// in Data field of Matrix object
/// </summary>
/// <param name="matrix"></param>
protected void StringToDoubleMatrix(string[][] matrix)
{
for (int idx = 0; idx< matrix.Length;idx++)
{
for (int jdx = 0; jdx < matrix[0].Length;jdx++)
{
this[idx,jdx] = Double.Parse(matrix[idx][jdx]);
}
}
}
public override string ToString()
{
string returnValue = "matrix([[";
int dim0 = shape[0];
int dim1 = shape[1];
for (int idx = 0; idx < (dim0-1);idx++)
{
for (int jdx = 0;jdx < (dim1-1);jdx++)
{
returnValue += (this[idx,jdx] + ", ");
}
returnValue += (this[idx,dim1-1] + "], \n [");
}
for (int jdx = 0; jdx < (dim1-1);jdx++)
{
returnValue += (this[dim0-1,jdx] + ", ");
}
returnValue += (this[dim0-1,dim1-1] + "]])");
return returnValue;
}
}
}