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
65 lines (56 loc) · 1.81 KB
/
Shape.cs
File metadata and controls
65 lines (56 loc) · 1.81 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
using System;
using System.Collections.Generic;
using System.Text;
namespace NumSharp.Core
{
public partial class Shape
{
private readonly IReadOnlyList<int> shape;
private readonly IReadOnlyList<int> dimOffset;
public int Size
{
get
{
int idx = 1;
for (int i = 0; i < shape.Count; i++)
{
idx *= shape[i];
}
return idx;
}
}
public int this[int dim] => shape[dim];
public Shape(params int[] shape)
{
if (shape.Length == 0)
throw new Exception("Shape cannot be empty.");
this.shape = shape;
int[] temp = new int[shape.Length];
temp[shape.Length - 1] = 1;
for (int i = shape.Length - 1; i >= 1; i--)
{
temp[i - 1] = temp[i] * shape[i];
}
dimOffset = temp;
}
public Shape(IReadOnlyList<int> shape)
{
if (shape.Count == 0)
throw new Exception("Shape cannot be empty.");
this.shape = shape;
int[] temp = new int[shape.Count];
temp[shape.Count - 1] = 1;
for (int i = shape.Count - 1; i >= 1; i--)
{
temp[i - 1] = temp[i] * shape[i];
}
dimOffset = temp;
}
public int Length => shape.Count;
public IReadOnlyList<int> DimOffset => dimOffset;
public IReadOnlyList<int> Shapes => shape;
public int UniShape => shape[0];
public (int, int) BiShape => shape.Count == 2 ? (shape[0], shape[1]) : (0, 0);
public (int, int, int) TriShape => shape.Count == 3 ? (shape[0], shape[1], shape[2]) : (0, 0, 0);
}
}