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
101 lines (86 loc) · 3.04 KB
/
Matrix.cs
File metadata and controls
101 lines (86 loc) · 3.04 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
using System;
using System.Linq;
namespace NumSharp
{
public partial class matrix: NDArray
{
public matrix(NDArray data) : base(data.dtype)
{
Storage.SetData(data.Array);
Storage.Reshape(data.shape);
}
public matrix(string matrixString, Type dtype) : base(dtype)
{
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(shape, dtype);
switch (this.dtype.Name)
{
case "Double":
StringToDoubleMatrix(splitted);
break;
case "Float":
break;
default:
throw new NotImplementedException($"matrix {this.dtype.Name}");
}
}
/// <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];
switch (dtype.Name)
{
case "Double":
{
for (int idx = 0; idx < (dim0 - 1); idx++)
{
for (int jdx = 0; jdx < (dim1 - 1); jdx++)
{
returnValue += Data<double>(idx, jdx) + ", ";
}
returnValue += Data<double>(idx, dim1 - 1) + "], \n [";
}
for (int jdx = 0; jdx < (dim1 - 1); jdx++)
{
returnValue += Data<double>(dim0 - 1, jdx) + ", ";
}
returnValue += Data<double>(dim0 - 1, dim1 - 1) + "]])";
}
break;
}
return returnValue;
}
}
}