-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathTensorRng.fs
More file actions
98 lines (75 loc) · 3.13 KB
/
Copy pathTensorRng.fs
File metadata and controls
98 lines (75 loc) · 3.13 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
namespace Tensor
open System
open Tensor.Utils
/// Special constants that can be passed or returned instead of indices.
[<AutoOpen>]
module internal SpecialIdx =
/// For slicing: inserts a new axis of size one.
let NewAxis = Int64.MinValue + 1L
/// For slicing: fills all remaining axes with size one.
/// Cannot be used together with NewAxis.
let Fill = Int64.MinValue + 2L
/// For reshape: remainder, so that number of elements stays constant.
let Remainder = Int64.MinValue + 3L
/// For search: value was not found.
let NotFound = Int64.MinValue + 4L
/// Range over a dimension of a tensor.
[<RequireQualifiedAccess; StructuredFormatDisplay("{Pretty}")>]
type Rng =
/// The single element specified.
| Elem of int64
/// Range of elements, including first and last.
| Rng of first:int64 option * last:int64 option
/// Insert broadcastable axis of size 1.
| NewAxis
/// Take all elements of remaining dimensions.
| AllFill
/// All elements.
static member All = Rng (None, None)
/// Pretty string.
member this.Pretty =
match this with
| Elem e -> sprintf "%d" e
| Rng (Some first, Some last) -> sprintf "%d..%d" first last
| Rng (Some first, None) -> sprintf "%d.." first
| Rng (None, Some last) -> sprintf "0..%d" last
| Rng (None, None) -> "*"
| NewAxis -> "NewAxis"
| AllFill -> "Fill"
/// Converts arguments to a .NET Item property or GetSlice, SetSlice method to a TensorRng list.
static member internal ofItemOrSliceArgs (allArgs: obj[]) =
let invalid () =
invalidArg "item" "Specified items/slices are invalid: %A." allArgs
let rec toRng (args: obj list) =
match args with
| [:? (Rng list) as rngs] -> // direct range specification
rngs
| (:? (int64 option) as so) :: (:? (int64 option) as fo) :: rest -> // slice
if so |> Option.contains SpecialIdx.NewAxis || so |> Option.contains SpecialIdx.Fill ||
fo |> Option.contains SpecialIdx.NewAxis || fo |> Option.contains SpecialIdx.Fill then
invalid ()
Rng (so, fo) :: toRng rest
| (:? int64 as i) :: rest when i = SpecialIdx.NewAxis -> // new axis
NewAxis :: toRng rest
| (:? int64 as i) :: rest when i = SpecialIdx.Fill -> // fill
AllFill :: toRng rest
| (:? int64 as i) :: rest -> // single item
Elem i :: toRng rest
| [] -> []
| _ -> invalid ()
allArgs |> Array.toList |> toRng
/// Memory ordering of a tensor.
type TensorOrder =
/// Row-major (C) memory order.
| RowMajor
/// Column-major (Fortran) memory order.
| ColumnMajor
/// The specified custom memory ordering of dimensions.
| CustomOrder of int list
/// Upper or lower trianguler part of a matrix.
[<RequireQualifiedAccess>]
type MatrixPart =
/// Upper triangular part of the matrix.
| Upper
/// Lower triangular part of the matrix.
| Lower