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
85 lines (74 loc) · 2.48 KB
/
Matrix.cs
File metadata and controls
85 lines (74 loc) · 2.48 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
using System;
using System.Linq;
namespace NumSharp.Core
{
public partial class Matrix<TData> : NDArray<TData>
{
public Matrix()
{
}
public Matrix(string matrixString)
{
string[][] splitted = null;
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;
this.Data = new TData[dim0 * dim1];
this.Shape = new Shape(new int[] { dim0, dim1 });
var dataType = typeof(TData);
switch (dataType.Name)
{
case ("Double"): this.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)
{
dynamic matrixData = this;
for (int idx = 0; idx< matrix.Length;idx++)
{
for (int jdx = 0; jdx < matrix[0].Length;jdx++)
{
matrixData[idx,jdx] = Double.Parse(matrix[idx][jdx]);
}
}
this.Data = (TData[])matrixData.Data;
}
public override string ToString()
{
string returnValue = "matrix([[";
int dim0 = Shape.Shapes[0];
int dim1 = Shape.Shapes[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;
}
}
}