@@ -65,6 +65,93 @@ public void Insert(int index, Tensor tensor)
65
65
IEnumerator IEnumerable . GetEnumerator ( )
66
66
=> GetEnumerator ( ) ;
67
67
68
+ public NDArray numpy ( )
69
+ {
70
+ EnsureSingleTensor ( this , "nnumpy" ) ;
71
+ return this [ 0 ] . numpy ( ) ;
72
+ }
73
+
74
+ public T [ ] ToArray < T > ( ) where T : unmanaged
75
+ {
76
+ EnsureSingleTensor ( this , $ "ToArray<{ typeof ( T ) } >") ;
77
+ return this [ 0 ] . ToArray < T > ( ) ;
78
+ }
79
+
80
+ #region Explicit Conversions
81
+ public unsafe static explicit operator bool ( Tensors tensor )
82
+ {
83
+ EnsureSingleTensor ( tensor , "explicit conversion to bool" ) ;
84
+ return ( bool ) tensor [ 0 ] ;
85
+ }
86
+
87
+ public unsafe static explicit operator sbyte ( Tensors tensor )
88
+ {
89
+ EnsureSingleTensor ( tensor , "explicit conversion to sbyte" ) ;
90
+ return ( sbyte ) tensor [ 0 ] ;
91
+ }
92
+
93
+ public unsafe static explicit operator byte ( Tensors tensor )
94
+ {
95
+ EnsureSingleTensor ( tensor , "explicit conversion to byte" ) ;
96
+ return ( byte ) tensor [ 0 ] ;
97
+ }
98
+
99
+ public unsafe static explicit operator ushort ( Tensors tensor )
100
+ {
101
+ EnsureSingleTensor ( tensor , "explicit conversion to ushort" ) ;
102
+ return ( ushort ) tensor [ 0 ] ;
103
+ }
104
+
105
+ public unsafe static explicit operator short ( Tensors tensor )
106
+ {
107
+ EnsureSingleTensor ( tensor , "explicit conversion to short" ) ;
108
+ return ( short ) tensor [ 0 ] ;
109
+ }
110
+
111
+ public unsafe static explicit operator int ( Tensors tensor )
112
+ {
113
+ EnsureSingleTensor ( tensor , "explicit conversion to int" ) ;
114
+ return ( int ) tensor [ 0 ] ;
115
+ }
116
+
117
+ public unsafe static explicit operator uint ( Tensors tensor )
118
+ {
119
+ EnsureSingleTensor ( tensor , "explicit conversion to uint" ) ;
120
+ return ( uint ) tensor [ 0 ] ;
121
+ }
122
+
123
+ public unsafe static explicit operator long ( Tensors tensor )
124
+ {
125
+ EnsureSingleTensor ( tensor , "explicit conversion to long" ) ;
126
+ return ( long ) tensor [ 0 ] ;
127
+ }
128
+
129
+ public unsafe static explicit operator ulong ( Tensors tensor )
130
+ {
131
+ EnsureSingleTensor ( tensor , "explicit conversion to ulong" ) ;
132
+ return ( ulong ) tensor [ 0 ] ;
133
+ }
134
+
135
+ public unsafe static explicit operator float ( Tensors tensor )
136
+ {
137
+ EnsureSingleTensor ( tensor , "explicit conversion to byte" ) ;
138
+ return ( byte ) tensor [ 0 ] ;
139
+ }
140
+
141
+ public unsafe static explicit operator double ( Tensors tensor )
142
+ {
143
+ EnsureSingleTensor ( tensor , "explicit conversion to double" ) ;
144
+ return ( double ) tensor [ 0 ] ;
145
+ }
146
+
147
+ public unsafe static explicit operator string ( Tensors tensor )
148
+ {
149
+ EnsureSingleTensor ( tensor , "explicit conversion to string" ) ;
150
+ return ( string ) tensor [ 0 ] ;
151
+ }
152
+ #endregion
153
+
154
+ #region Implicit Conversions
68
155
public static implicit operator Tensors ( Tensor tensor )
69
156
=> new Tensors ( tensor ) ;
70
157
@@ -87,12 +174,26 @@ public static implicit operator Tensor(Tensors tensors)
87
174
public static implicit operator Tensor [ ] ( Tensors tensors )
88
175
=> tensors . items . ToArray ( ) ;
89
176
177
+ #endregion
178
+
90
179
public void Deconstruct ( out Tensor a , out Tensor b )
91
180
{
92
181
a = items [ 0 ] ;
93
182
b = items [ 1 ] ;
94
183
}
95
184
185
+ private static void EnsureSingleTensor ( Tensors tensors , string methodnName )
186
+ {
187
+ if ( tensors . Length == 0 )
188
+ {
189
+ throw new ValueError ( $ "Method `{ methodnName } ` of `Tensors` cannot be used when `Tensors` contains no Tensor.") ;
190
+ }
191
+ else if ( tensors . Length > 1 )
192
+ {
193
+ throw new ValueError ( $ "Method `{ methodnName } ` of `Tensors` cannot be used when `Tensors` contains more than one Tensor.") ;
194
+ }
195
+ }
196
+
96
197
public override string ToString ( )
97
198
=> items . Count ( ) == 1
98
199
? items . First ( ) . ToString ( )
0 commit comments