-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tensor.cs
147 lines (117 loc) · 4.45 KB
/
Tensor.cs
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using System.Collections.Generic;
using System.Linq;
using System;
namespace Tensors
{
public class Tensor<T>
{
private T[] _source;
private int[] _dimensions;
private int[] _dimensionsDegrees;
public int DimensionsCount => _dimensions.Length;
public int GetLength(int dimensionIndexex) => _dimensions[dimensionIndexex];
public IEnumerable<T> GetSource() => _source.Select(v => v);
public IEnumerable<int> Dimensions => _dimensions.Select(d => d);
public Tensor(params int[] dimensions)
{
if (dimensions.Any(d => d < 0))
throw new ArgumentException("Invalid dimesion");
_dimensions = dimensions;
_source = new T[dimensions.Aggregate((d1, d2) => d1 * d2)];
InitializeDegrees();
}
public Tensor(int[] dimensions, T[] source)
{
if (dimensions.Any(d => d < 0))
throw new ArgumentException("Invalid dimesion");
if (source.Length != dimensions.Aggregate((d1, d2) => d1 * d2))
throw new ArgumentException("Invalid source length");
_source = source;
_dimensions = dimensions;
InitializeDegrees();
}
public T GetItem(params int[] indexes)
{
int index = GetIndex(indexes);
return _source[index];
}
public void SetItem(T value, params int[] indexes)
{
int index = GetIndex(indexes);
_source[index] = value;
}
private int GetIndex(params int[] indexes)
{
if (indexes.Length != _dimensions.Length)
throw new InvalidOperationException("Invalid indexes count");
int index = 0;
for (int i = 0; i < _dimensions.Length; i++)
{
index += indexes[i] * _dimensionsDegrees[i];
}
return index;
}
private void InitializeDegrees()
{
_dimensionsDegrees = new int[_dimensions.Length];
_dimensionsDegrees[0] = 1;
for (int i = 1; i < _dimensionsDegrees.Length; i++)
{
_dimensionsDegrees[i] = _dimensionsDegrees[i - 1] * _dimensions[i - 1];
}
}
public T this[params int[] indexes]
{
get { return GetItem(indexes); }
set { SetItem(value, indexes); }
}
public Tensor<T> GetCrop(int[] startPoint, int[] endPoint)
{
if (startPoint.Length != endPoint.Length || startPoint.Length != _dimensions.Length)
throw new ArgumentException("Invalid boundaries");
for (int i = 0; i < startPoint.Length; i++)
{
if (startPoint[i] > endPoint[i])
{
var temp = startPoint;
startPoint = endPoint;
endPoint = temp;
break;
}
if (endPoint[i] > startPoint[i]) break;
}
var dimensions = Enumerable.Range(0, startPoint.Length)
.Select(i => Math.Abs(endPoint[i] - startPoint[i]) + 1)
.Where(i => i != 1)
.ToArray();
var indexes = startPoint.Select(v => v).ToArray();
var source = GetCropIndexesRecursive(startPoint, endPoint, startPoint.Length - 1, indexes)
.Select(i => GetItem(i))
.ToArray();
return new Tensor<T>(dimensions, source);
}
private IEnumerable<int[]> GetCropIndexesRecursive(int[] startPoint, int[] endPoint, int dimensionIndex,
int[] indexes)
{
if (dimensionIndex == -1)
{
yield return indexes;
yield break;
}
else
{
int delta = endPoint[dimensionIndex] - startPoint[dimensionIndex];
int count = Math.Abs(delta);
int dirrection = delta == 0 ? 0 : delta / count;
for (int i = 0; i <= count; i++)
{
int increment = i * dirrection;
indexes[dimensionIndex] += increment;
foreach (var item in GetCropIndexesRecursive(startPoint, endPoint, dimensionIndex - 1, indexes))
yield return item;
indexes[dimensionIndex] -= increment;
}
}
}
}
}