forked from SciSharp/NumSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNdArray.Eye.cs
More file actions
66 lines (57 loc) · 1.77 KB
/
NdArray.Eye.cs
File metadata and controls
66 lines (57 loc) · 1.77 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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
namespace NumSharp.Extensions
{
public static partial class NDArrayExtensions
{
public static NDArray<int> Eye(this NDArray<int> np,int dim, int diagonalIndex = 0)
{
int noOfDiagElement = dim - Math.Abs(diagonalIndex);
if((np.NDim == 1) && (dim != 1))
{
np.Zeros(dim,dim);
}
else
{
np.Zeros(np.Shape.Shapes.ToArray());
}
np.Data = new int[np.Size];
for(int idx = 0; idx < noOfDiagElement;idx++ )
{
np.Data[diagonalIndex + idx + idx * np.Shape.Shapes[1]] = 1;
}
return np;
}
public static NDArray<double> Eye(this NDArray<double> np,int dim, int diagonalIndex = 0)
{
int noOfDiagElement = dim - Math.Abs(diagonalIndex);
if((np.NDim == 1) && (dim != 1))
{
np.Zeros(dim,dim);
}
else
{
np.Zeros(np.Shape.Shapes.ToArray());
}
np.Data = new double[np.Size];
if (diagonalIndex >= 0)
{
for(int idx = 0; idx < noOfDiagElement;idx++ )
{
np.Data[diagonalIndex + idx + idx * np.Shape.Shapes[1]] = 1;
}
}
else
{
for(int idx = 0; idx < noOfDiagElement;idx++ )
{
np.Data[(-1)*diagonalIndex * dim + idx + idx * np.Shape.Shapes[1]] = 1;
}
}
return np;
}
}
}