forked from SciSharp/NumSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShape.cs
More file actions
128 lines (109 loc) · 4.23 KB
/
Shape.cs
File metadata and controls
128 lines (109 loc) · 4.23 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
using System;
using NumSharp.Core.Interfaces;
using System.Collections.Generic;
using System.Text;
using System.Linq;
namespace NumSharp.Core
{
public partial class Shape : IShape
{
protected int _TensorLayout;
public int TensorLayout {get {return _TensorLayout;}}
protected int[] _Dimensions;
protected int[] _DimOffset;
protected int _size;
public int NDim => _Dimensions.Length;
public int[] Dimensions {get{return _Dimensions;}}
public int[] DimOffset {get{return _DimOffset;}}
public int Size {get{return _size;}}
protected void _SetDimOffset()
{
if (this._Dimensions.Length == 0)
{
}
else
{
if (this._TensorLayout == 1)
{
_DimOffset[0] = 1;
for(int idx = 1;idx < _DimOffset.Length;idx++)
_DimOffset[idx] = _DimOffset[idx-1] * this._Dimensions[idx-1];
}
else if ( _TensorLayout == 2)
{
_DimOffset[_DimOffset.Length-1] = 1;
for(int idx = _DimOffset.Length-1;idx >= 1;idx--)
_DimOffset[idx-1] = _DimOffset[idx] * this._Dimensions[idx];
}
}
}
public Shape(params int[] dims)
{
ReShape(dims);
}
public Shape(IEnumerable<int> shape) : this(shape.ToArray())
{
}
public int GetIndexInShape(params int[] select)
{
int idx = 0;
for (int i = 0; i < select.Length; i++)
{
idx += _DimOffset[i] * select[i];
}
return idx;
}
public int[] GetDimIndexOutShape(int select)
{
int[] dimIndexes = null;
if (this._DimOffset.Length == 1)
dimIndexes = new int[] {select};
else if (this._TensorLayout == 1)
{
int counter = select;
dimIndexes = new int[_DimOffset.Length];
for (int idx = _DimOffset.Length-1; idx > -1;idx--)
{
dimIndexes[idx] = counter / _DimOffset[idx];
counter -= dimIndexes[idx] * _DimOffset[idx];
}
}
else
{
int counter = select;
dimIndexes = new int[_DimOffset.Length];
for (int idx = 0; idx < _DimOffset.Length;idx++)
{
dimIndexes[idx] = counter / _DimOffset[idx];
counter -= dimIndexes[idx] * _DimOffset[idx];
}
}
return dimIndexes;
}
public void ChangeTensorLayout(int layout)
{
_DimOffset = new int[this._Dimensions.Length];
layout = (layout == 0) ? 1 : layout;
_TensorLayout = layout;
_SetDimOffset();
}
public void ReShape(params int[] dims)
{
this._Dimensions = dims;
this._DimOffset = new int[this._Dimensions.Length];
this._TensorLayout = 1;
this._size = 1;
for (int idx = 0; idx < dims.Length; idx++)
_size *= dims[idx];
this._SetDimOffset();
}
public static implicit operator int(Shape shape) => shape.Size;
public static implicit operator (int, int)(Shape shape) => shape._Dimensions.Length == 2 ? (shape._Dimensions[0], shape._Dimensions[1]) : (0, 0);
public static implicit operator (int, int, int) (Shape shape) => shape._Dimensions.Length == 3 ? (shape._Dimensions[0], shape._Dimensions[1], shape._Dimensions[2]) : (0, 0, 0);
public static implicit operator int[](Shape shape) => shape._Dimensions;
public (int, int) BiShape => _Dimensions.Length == 2 ? (_Dimensions[0], _Dimensions[1]) : (0, 0);
public (int, int, int) TriShape => _Dimensions.Length == 3 ? (_Dimensions[0], _Dimensions[1], _Dimensions[2]) : (0, 0, 0);
public static implicit operator Shape(int[] dims) => new Shape(dims);
public static implicit operator Shape(int dim) => new Shape(dim);
}
}