-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathTensor.fs
More file actions
4608 lines (4243 loc) · 261 KB
/
Copy pathTensor.fs
File metadata and controls
4608 lines (4243 loc) · 261 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
namespace rec Tensor
open System
open System.Collections
open System.Collections.Generic
open System.Diagnostics
open Tensor.Utils
open Tensor.Backend
/// <summary>A singular matrix was encountered during an operation that does not allow singular matrices.</summary>
/// <param name="msg">Detailed error message.</param>
/// <remarks>
/// See the documentation of the method that raised this exception for a detailed description of the error conditions.
/// </remarks>
exception SingularMatrixException of msg:string with
/// <summary>Detailed error message.</summary>
override __.Message = __.msg
/// <summary>Block tensor specification.</summary>
/// <typeparam name="'T">The type of the data stored within the tensor.</typeparam>
/// <remarks>See <see cref="Tensor`1.ofBlocks"/> for usage information.</remarks>
/// <seealso cref="Tensor`1.ofBlocks"/>
type BlockTensor<'T> =
/// A block consisting of multiple sub-blocks.
| SubBlocks of BlockTensor<'T> list
/// A block consisting of a single tensor.
| Block of Tensor<'T>
/// <summary>An N-dimensional array with elements of type 'T.</summary>
/// <typeparam name="'T">The type of the data stored within the tensor.</typeparam>
/// <param name="layout">The memory layout to use.</param>
/// <param name="storage">The storage to use.</param>
/// <returns>A tensor using the specified memory layout and storage.</returns>
/// <remarks>
/// <para>The data of a tensor can be stored on different devices. Currently supported devices are host memory
/// and CUDA GPU memory.</para>
/// <para>Different tensors can share the whole or parts of the underlying data.</para>
/// <para>The recommended way to create a new tensor is to use <see cref="zeros"/>.
/// The implicit constructor creates a view into the specified storage using the specified memory layout.
/// In most cases, it is not necessary to use the implicit constructor.</para>
/// </remarks>
/// <seealso cref="ITensor"/>
type [<StructuredFormatDisplay("{Pretty}"); DebuggerDisplay("{Shape}-Tensor: {Pretty}")>]
Tensor<'T> (layout: TensorLayout, storage: ITensorStorage<'T>) =
do TensorLayout.check layout
let backend = storage.Backend layout
/// <summary>Memory layout of this tensor.</summary>
/// <value>Memory layout.</value>
/// <remarks>Provides information of how the data is stored within this tensor.</remarks>
/// <seealso cref="Storage"/><seealso cref="Shape"/>
member val Layout = layout
/// <summary>Memory layout of the tensor.</summary>
/// <param name="a">The tensor to operate on.</param>
/// <returns>Memory layout.</returns>
/// <seealso cref="Layout"/>
static member inline layout (a: Tensor<'T>) = a.Layout
/// <summary>The storage object that holds the data of this tensor.</summary>
/// <value>Storage object.</value>
/// <remarks>
/// <para>The storage object holds the actual data of the tensor.
/// A storage object can be associated with one or more tensors, i.e. it can be shared between multiple tensors.
/// Sharing occurs, for example, when a view into an existing tensor is created or the tensor is reshapred.</para>
/// <para>The actual type of the storage object depends on the device the data of the tensor is stored on.</para>
/// <para>For tensors stored in host memory the storage object type is <see cref="Tensor.Host.TensorHostStorage`1"/>.</para>
/// <para>For tensors stored on a CUDA GPU the storage object type is <see cref="Tensor.Cuda.TensorCudaStorage`1"/>.</para>
/// </remarks>
/// <seealso cref="Dev"/><seealso cref="Layout"/>
member val Storage = storage
/// <summary>Device the data of tensor is stored on.</summary>
/// <value>Data storage device.</value>
/// <remarks>
/// <para>For tensors stored in host memory the value of this property is <see cref="HostTensor.Dev"/>.</para>
/// <para>For tensors stored on a CUDA GPU the value of this property is <see cref="CudaTensor.Dev"/>.</para>
/// </remarks>
/// <seealso cref="Storage"/>
member inline this.Dev = this.Storage.Dev
/// <summary>Device the data of tensor is stored on.</summary>
/// <param name="a">The tensor to operate on.</param>
/// <returns>Data storage device.</returns>
/// <seealso cref="Dev"/><seealso cref="transfer``1"/>
static member inline dev (a: Tensor<'T>) = a.Dev
/// backend
member internal this.Backend = backend
/// <summary>Shape of this tensor.</summary>
/// <value>Shape.</value>
/// <example><code language="fsharp">
/// let a = HostTensor.ofList [[1.0; 2.0; 5.0]
/// [3.0; 4.0; 6.0]]
/// let c = a.Shape // [2L; 3L]
/// </code></example>
/// <remarks>
/// <para>Provides the shape of this tensor.</para>
/// <para>A tensor is empty of any dimension has size zero.</para>
/// <para>A zero-dimensional tensor has an empty shape and contains one element.</para>
/// </remarks>
/// <seealso cref="reshape"/><seealso cref="NDims"/><seealso cref="NElems"/>
member inline this.Shape = this.Layout.Shape
/// <summary>Shape of the tensor.</summary>
/// <param name="a">The tensor to operate on.</param>
/// <returns>Shape.</returns>
/// <seealso cref="Shape"/>
static member inline shape (a: Tensor<'T>) = a.Shape
/// <summary>Dimensionality of this tensor.</summary>
/// <value>Number of dimensions.</value>
/// <example><code language="fsharp">
/// let a = HostTensor.ofList [[1.0; 2.0; 5.0]
/// [3.0; 4.0; 6.0]]
/// let c = a.NDims // 2
/// </code></example>
/// <remarks>
/// <para>Provides the number of dimensions of this tensor.</para>
/// <para>A zero-dimensional tensor contains one element, i.e. it is a scalar.</para>
/// </remarks>
/// <seealso cref="Shape"/>
member inline this.NDims = this.Layout.NDims
/// <summary>Dimensionality of the tensor.</summary>
/// <param name="a">The tensor to operate on.</param>
/// <returns>Number of dimensions.</returns>
/// <seealso cref="NDims"/>
static member inline nDims (a: Tensor<'T>) = a.NDims
/// <summary>Total number of elements within this tensor.</summary>
/// <value>Number of elements.</value>
/// <example><code language="fsharp">
/// let a = HostTensor.ofList [[1.0; 2.0; 5.0]
/// [3.0; 4.0; 6.0]]
/// let c = a.NElems // 6L
/// </code></example>
/// <remarks>
/// <para>Counts the total number of elements of this tensor.</para>
/// <para>A zero-dimensional tensor contains one element, i.e. it is a scalar.</para>
/// </remarks>
/// <seealso cref="Shape"/>
member inline this.NElems = this.Layout.NElems
/// <summary>Total number of elements within the tensor.</summary>
/// <param name="a">The tensor to operate on.</param>
/// <returns>Number of elements.</returns>
/// <seealso cref="NElems"/>
static member inline nElems (a: Tensor<'T>) = a.NElems
/// <summary>Type of data stored within this tensor.</summary>
/// <value>Data type.</value>
/// <remarks>
/// <para>The data type is <c>typeof<'T></c>.</para>
/// </remarks>
/// <seealso cref="convert``1"/>
member inline this.DataType = typeof<'T>
/// <summary>Type of data stored within the tensor.</summary>
/// <param name="a">The tensor to operate on.</param>
/// <returns>Data type.</returns>
/// <seealso cref="DataType"/>
static member inline dataType (a: Tensor<'T>) = a.DataType
/// a tensor with the same storage but new layout
member internal this.Relayout (newLayout: TensorLayout) =
Tensor<'T> (newLayout, storage)
/// <summary>Creates a tensor with the specified layout sharing its storage with the original tensor.</summary>
/// <param name="newLayout">The new tensor memory layout.</param>
/// <param name="a">The tensor to operate on.</param>
/// <returns>The resulting tensor.</returns>
static member relayout newLayout (a: Tensor<'T>) =
a.Relayout newLayout
/// a view of this tensor over the given range
member internal this.Range (rng: Rng list) =
this.Relayout (this.Layout |> TensorLayout.view rng)
/// <summary>Get a slice (part) of the tensor.</summary>
/// <param name="rng">The range of the tensor to select.</param>
/// <param name="a">The tensor to operate on.</param>
/// <returns>The resulting tensor.</returns>
/// <seealso cref="Item(Microsoft.FSharp.Collections.FSharpList{Tensor.Rng})"/>
static member range (rng: Rng list) (a: Tensor<'T>) = ITensor.range rng a :?> Tensor<'T>
/// <summary>Checks the the specified axis is valid for this tensor.</summary>
/// <param name="ax">The axis number to check.</param>
/// <remarks>If the axis is valid, this function does nothing.</remarks>
/// <exception cref="System.IndexOutOfRangeException">Raised when the axis is invalid.</exception>
member inline this.CheckAxis ax = this.Layout |> TensorLayout.checkAxis ax
/// <summary>Gets a sequence of all indices to enumerate all elements within the tensor.</summary>
/// <param name="a">The tensor to operate on.</param>
/// <returns>Sequence of indicies.</returns>
/// <remarks>The sequence sequentially enumerates the indices of all elements of the tensor.</remarks>
/// <seealso cref="allIdxOfDim"/><seealso cref="allElems"/>
static member allIdx (a: Tensor<'T>) = ITensor.allIdx a
/// <summary>Gets a sequence of all all elements within the tensor.</summary>
/// <param name="a">The tensor to operate on.</param>
/// <returns>Sequence of elements.</returns>
/// <remarks>The sequence sequentially enumerates all elements of the tensor.</remarks>
/// <seealso cref="allIdx"/>
static member allElems (a: Tensor<'T>) = a |> Tensor<_>.allIdx |> Seq.map (fun idx -> a.[idx])
/// <summary>Insert a dimension of size one as the first dimension.</summary>
/// <param name="a">The tensor to operate on.</param>
/// <returns>The resulting tensor.</returns>
/// <example><code language="fsharp">
/// let a = HostTensor.zeros [3L; 4L; 5L]
/// let b = Tensor.padLeft a // b.Shape = [1L; 3L; 4L; 5L]
/// </code></example>
/// <remarks>
/// <para>The operation returns a view of the original tensor and shares its storage. Modifications done to the
/// returned tensor will affect the original tensor. Also, modifying the orignal tensor will affect the view.</para>
/// </remarks>
/// <seealso cref="padRight"/><seealso cref="insertAxis"/>
static member padLeft (a: Tensor<'T>) = ITensor.padLeft a :?> Tensor<'T>
/// <summary>Append a dimension of size one after the last dimension.</summary>
/// <param name="a">The tensor to operate on.</param>
/// <returns>The resulting tensor.</returns>
/// <example><code language="fsharp">
/// let a = HostTensor.zeros [3L; 4L; 5L]
/// let b = Tensor.padRight a // b.Shape = [3L; 4L; 5L; 1L]
/// </code></example>
/// <remarks>
/// <para>The operation returns a view of the original tensor and shares its storage. Modifications done to the
/// returned tensor will affect the original tensor. Also, modifying the orignal tensor will affect the view.</para>
/// </remarks>
/// <seealso cref="padLeft"/><seealso cref="insertAxis"/>
static member padRight (a: Tensor<'T>) = ITensor.padRight a :?> Tensor<'T>
/// <summary>Insert a dimension of size one before the specifed dimension.</summary>
/// <param name="ax">The dimension to insert before.</param>
/// <param name="a">The tensor to operate on.</param>
/// <returns>The resulting tensor.</returns>
/// <example><code language="fsharp">
/// let a = HostTensor.zeros [3L; 4L; 5L]
/// let b = Tensor.insertAxis 1 a // b.Shape = [3L; 1L 4L; 5L]
/// </code></example>
/// <remarks>
/// <para>The operation returns a view of the original tensor and shares its storage. Modifications done to the
/// returned tensor will affect the original tensor. Also, modifying the orignal tensor will affect the view.</para>
/// </remarks>
/// <seealso cref="padLeft"/><seealso cref="padRight"/>
static member insertAxis ax (a: Tensor<'T>) = ITensor.insertAxis ax a :?> Tensor<'T>
/// <summary>Removes the first dimension.</summary>
/// <param name="a">The tensor to operate on.</param>
/// <returns>The resulting tensor.</returns>
/// <example><code language="fsharp">
/// let a = HostTensor.zeros [3L; 4L; 5L]
/// let b = Tensor.cutLeft a // b.Shape = [4L; 5L]
/// </code></example>
/// <remarks>
/// <para>The operation returns a view of the original tensor and shares its storage. Modifications done to the
/// returned tensor will affect the original tensor. Also, modifying the orignal tensor will affect the view.</para>
/// </remarks>
/// <seealso cref="cutRight"/>
static member cutLeft (a: Tensor<'T>) = ITensor.cutLeft a :?> Tensor<'T>
/// <summary>Removes the last dimension.</summary>
/// <param name="a">The tensor to operate on.</param>
/// <returns>The resulting tensor.</returns>
/// <example><code language="fsharp">
/// let a = HostTensor.zeros [3L; 4L; 5L]
/// let b = Tensor.cutRight a // b.Shape = [3L; 4L]
/// </code></example>
/// <remarks>
/// <para>The operation returns a view of the original tensor and shares its storage. Modifications done to the
/// returned tensor will affect the original tensor. Also, modifying the orignal tensor will affect the view.</para>
/// </remarks>
/// <seealso cref="cutLeft"/>
static member cutRight (a: Tensor<'T>) = ITensor.cutRight a :?> Tensor<'T>
/// <summary>Broadcast a dimension to a specified size.</summary>
/// <param name="dim">The size-one dimension to broadcast.</param>
/// <param name="size">The size to broadcast to.</param>
/// <param name="a">The tensor to operate on.</param>
/// <returns>The resulting tensor.</returns>
/// <example><code language="fsharp">
/// let a = HostTensor.zeros [3L; 1L; 5L]
/// let b = Tensor.broadCastDim 1 9L a // b.Shape = [3L; 9L; 5L]
/// </code></example>
/// <remarks>
/// <para>The broadcasted dimension must be of size one. The tensor is repeated <paramref name="size"/> times along
/// the axis <paramref name="dim"/>.</para>
/// <para>Broadcasting is usually performed automatically when the shapes allow for it. See broadcasting rules
/// for details.</para>
/// <para>The operation returns a view of the original tensor and shares its storage. Modifications done to the
/// returned tensor will affect the original tensor. Also, modifying the orignal tensor will affect the view.</para>
/// </remarks>
/// <seealso cref="insertAxis"/>
static member broadcastDim dim size (a: Tensor<'T>) = ITensor.broadcastDim dim size a :?> Tensor<'T>
/// <summary>Creates a new, uninitialized tensor with a new storage.</summary>
/// <param name="shape">The shape of the tensor to create.</param>
/// <param name="dev">The device to store the data of the tensor on.</param>
/// <param name="order">The memory layout to use for the new tensor. (default: row-major)</param>
/// <returns>The new, uninitialized tensor.</returns>
/// <remarks>
/// <para>The contents of the new tensor are undefined. The default memory layout is row-major.</para>
/// <para>The recommended way to create a new tensor is to use <see cref="zeros"/>.</para>
/// </remarks>
/// <seealso cref="NewOfType"/><seealso cref="zeros"/>
new (shape: int64 list, dev: ITensorDevice, ?order: TensorOrder) =
let order = defaultArg order RowMajor
let layout =
match order with
| RowMajor -> TensorLayout.newC shape
| ColumnMajor -> TensorLayout.newF shape
| CustomOrder perm -> TensorLayout.newOrdered shape perm
let storage = dev.Create layout.NElems
Tensor<'T> (layout, storage)
/// Applies the given function to the tensors' layouts.
static member inline internal ApplyLayoutFn (fn, a: Tensor<'TA>, b: Tensor<'TB>) =
let layouts = [Tensor<_>.layout a; Tensor<_>.layout b]
let newLayouts = fn layouts
match newLayouts with
| [al; bl] ->
Tensor<_>.relayout al a, Tensor<_>.relayout bl b
| _ -> failwith "unexpected layout function result"
/// Applies the given function to the tensors' layouts.
static member inline internal ApplyLayoutFn (fn, a: Tensor<'TA>, b: Tensor<'TB>, c: Tensor<'TC>) =
let layouts = [Tensor<_>.layout a; Tensor<_>.layout b; Tensor<_>.layout c]
let newLayouts = fn layouts
match newLayouts with
| [al; bl; cl] ->
Tensor<_>.relayout al a, Tensor<_>.relayout bl b, Tensor<_>.relayout cl c
| _ -> failwith "unexpected layout function result"
/// Applies the given function to the tensors' layouts.
static member inline internal ApplyLayoutFn (fn, xs: Tensor<'T> list) =
let layouts = fn (xs |> List.map Tensor<_>.layout)
(layouts, xs) ||> List.map2 Tensor<_>.relayout
/// <summary>Pads all specified tensors from the left with dimensions of size one until they have the
/// same dimensionality.</summary>
/// <param name="a">The tensor to operate on.</param>
/// <param name="b">The tensor to operate on.</param>
/// <returns>A tuple of the resulting tensors, all having the same dimensionality.</returns>
/// <example><code language="fsharp">
/// let a = HostTensor.zeros [4L; 5L]
/// let b = HostTensor.zeros [3L; 4L; 5L]
/// let pa, pb = Tensor.padToSame (a, b) // pa.Shape = [1L; 4L; 5L]; pb.Shape = [3L; 4L; 5L]
/// </code></example>
/// <remarks>
/// <para>Size one dimensions are added from the left to each tensor until all of them have the same
/// dimensionality.</para>
/// <para>The operation returns a view of the original tensor and shares its storage. Modifications done to the
/// returned tensor will affect the original tensor. Also, modifying the orignal tensor will affect the view.</para>
/// </remarks>
/// <seealso cref="padLeft"/><seealso cref="broadcastToSame"/>
static member padToSame (a: Tensor<'TA>, b: Tensor<'TB>) =
Tensor<_>.ApplyLayoutFn (TensorLayout.padToSameMany, a, b)
/// <summary>Pads all specified tensors from the left with dimensions of size one until they have the
/// same dimensionality.</summary>
/// <param name="a">The tensor to operate on.</param>
/// <param name="b">The tensor to operate on.</param>
/// <param name="c">The tensor to operate on.</param>
/// <returns>A tuple of the resulting tensors, all having the same dimensionality.</returns>
/// <seealso cref="padToSame``2"/>
static member padToSame (a: Tensor<'TA>, b: Tensor<'TB>, c: Tensor<'TC>) =
Tensor<_>.ApplyLayoutFn (TensorLayout.padToSameMany, a, b, c)
/// <summary>Pads all specified tensors from the left with dimensions of size one until they have the
/// same dimensionality.</summary>
/// <param name="xs">A list of tensors to operate on.</param>
/// <returns>A list of the resulting tensors, all having the same dimensionality.</returns>
/// <seealso cref="padToSame``2"/>
static member padToSame (xs: Tensor<'T> list) =
Tensor<_>.ApplyLayoutFn (TensorLayout.padToSameMany, xs)
/// <summary>Broadcasts all specified tensors to have the same shape.</summary>
/// <param name="a">The tensor to operate on.</param>
/// <param name="b">The tensor to operate on.</param>
/// <returns>A tuple of the resulting tensors, all having the same shape.</returns>
/// <example><code language="fsharp">
/// let a = HostTensor.zeros [4L; 5L]
/// let b = HostTensor.zeros [3L; 4L; 5L]
/// let pa, pb = Tensor.broadcastToSame (a, b) // pa.Shape = [3L; 4L; 5L]; pb.Shape = [3L; 4L; 5L]
/// </code></example>
/// <remarks>
/// <para>First, size one dimensions are added from the left to each tensor until all of them have the same
/// dimensionality. Then, size one dimensions are broadcasted to match the size of non-size-one dimensions.</para>
/// <para>The operation returns a view of the original tensor and shares its storage. Modifications done to the
/// returned tensor will affect the original tensor. Also, modifying the orignal tensor will affect the view.</para>
/// </remarks>
/// <exception cref="System.InvalidOperationException">Raised when broadcasting to a common shape is impossible.</exception>
/// <seealso cref="padToSame``2"/><seealso cref="broadcastToSameInDims``2"/><seealso cref="broadcastTo"/>
static member broadcastToSame (a: Tensor<'TA>, b: Tensor<'TB>) =
Tensor<_>.ApplyLayoutFn (TensorLayout.broadcastToSameMany, a, b)
/// <summary>Broadcasts all specified tensors to have the same shape.</summary>
/// <param name="a">The tensor to operate on.</param>
/// <param name="b">The tensor to operate on.</param>
/// <param name="c">The tensor to operate on.</param>
/// <returns>A tuple of the resulting tensors, all having the same shape.</returns>
/// <seealso cref="broadcastToSame``2"/>
static member broadcastToSame (a: Tensor<'TA>, b: Tensor<'TB>, c: Tensor<'TC>) =
Tensor<_>.ApplyLayoutFn (TensorLayout.broadcastToSameMany, a, b, c)
/// <summary>Broadcasts all specified tensors to have the same shape.</summary>
/// <param name="xs">A list of tensors to operate on.</param>
/// <returns>A list of the resulting tensors, all having the same shape.</returns>
/// <seealso cref="broadcastToSame``2"/>
static member broadcastToSame (xs: Tensor<'T> list) =
Tensor<_>.ApplyLayoutFn (TensorLayout.broadcastToSameMany, xs)
/// <summary>Broadcasts all specified tensors to have the same size in the specified dimensions.</summary>
/// <param name="dims">A list of dimensions that should be broadcasted to have the same size.</param>
/// <param name="a">The tensor to operate on.</param>
/// <param name="b">The tensor to operate on.</param>
/// <returns>A tuple of the resulting tensors, all having the same size in the specified dimensions.</returns>
/// <example><code language="fsharp">
/// let a = HostTensor.zeros [1L; 7L; 1L]
/// let b = HostTensor.zeros [3L; 4L; 5L]
/// let pa, pb = Tensor.broadcastToSameInDims ([0; 2], a, b) // pa.Shape = [3L; 7L; 5L]; pb.Shape = [3L; 4L; 5L]
/// </code></example>
/// <remarks>
/// <para>The specified dimensions are broadcasted to match the size of non-size-one dimensions.</para>
/// <para>The operation returns a view of the original tensor and shares its storage. Modifications done to the
/// returned tensor will affect the original tensor. Also, modifying the orignal tensor will affect the view.</para>
/// </remarks>
/// <exception cref="System.InvalidOperationException">Raised when broadcasting to a common shape is impossible.</exception>
/// <seealso cref="broadcastToSame``2"/><seealso cref="broadcastTo"/>
static member broadcastToSameInDims (dims, a: Tensor<'TA>, b: Tensor<'TB>) =
Tensor<_>.ApplyLayoutFn (TensorLayout.broadcastToSameInDimsMany dims, a, b)
/// <summary>Broadcasts all specified tensors to have the same size in the specified dimensions.</summary>
/// <param name="dims">A list of dimensions that should be broadcasted to have the same size.</param>
/// <param name="a">The tensor to operate on.</param>
/// <param name="b">The tensor to operate on.</param>
/// <param name="c">The tensor to operate on.</param>
/// <returns>A tuple of the resulting tensors, all having the same size in the specified dimensions.</returns>
/// <seealso cref="broadcastToSameInDims``2"/>
static member broadcastToSameInDims (dims, a: Tensor<'TA>, b: Tensor<'TB>, c: Tensor<'TC>) =
Tensor<_>.ApplyLayoutFn (TensorLayout.broadcastToSameInDimsMany dims, a, b, c)
/// <summary>Broadcasts all specified tensors to have the same size in the specified dimensions.</summary>
/// <param name="dims">A list of dimensions that should be broadcasted to have the same size.</param>
/// <param name="xs">A list of tensors to operate on.</param>
/// <returns>A list of the resulting tensors, all having the same size in the specified dimensions.</returns>
/// <seealso cref="broadcastToSameInDims``2"/>
static member broadcastToSameInDims (dims, xs: Tensor<'T> list) =
Tensor<_>.ApplyLayoutFn (TensorLayout.broadcastToSameInDimsMany dims, xs)
/// <summary>Broadcasts the specified tensor to the specified shape.</summary>
/// <param name="shp">The target shape.</param>
/// <param name="a">The tensor to operate on.</param>
/// <returns>Tensor of shape <paramref name="shp"/>.</returns>
/// <example><code language="fsharp">
/// let a = HostTensor.zeros [1L; 7L; 1L]
/// let pa = Tensor.broadcastTo [2L; 7L; 3L] a // pa.Shape = [2L; 7L; 3L]
/// </code></example>
/// <remarks>
/// <para>Size one dimensions are broadcasted to match the corresponding dimension of the target shape
/// <paramref name="shp"/>. Non-size-one dimensions must match the target shape.</para>
/// <para>The operation returns a view of the original tensor and shares its storage. Modifications done to the
/// returned tensor will affect the original tensor. Also, modifying the orignal tensor will affect the view.</para>
/// </remarks>
/// <exception cref="System.InvalidOperationException">Raised when broadcasting to the specified shape is impossible.</exception>
/// <seealso cref="broadcastToSame``2"/>
static member broadcastTo shp (a: Tensor<'T>) = ITensor.broadcastTo shp a :?> Tensor<'T>
/// <summary>Checks if the specified tensor is broadcasted in at least one dimension.</summary>
/// <param name="a">The tensor to operate on.</param>
/// <returns>true if at least one dimension is broadcasted, otherwise false.</returns>
/// <remarks>
/// <para>If any stride is zero, it is assumed that the tensor is broadcasted.
/// If this is the case, changing an element of the tensor may change other elements as well.</para>
/// </remarks>
/// <seealso cref="broadcastToSame``2"/><seealso cref="broadcastTo"/>
static member isBroadcasted (a: Tensor<'T>) = ITensor.isBroadcasted a
/// <summary>Tries to create a reshaped view of the tensor (without copying).</summary>
/// <param name="shp">The target shape.</param>
/// <param name="a">The tensor to operate on.</param>
/// <returns>The reshaped tensor, if reshaping without copying is possible. Otherwise <c>None</c>.</returns>
/// <remarks>
/// <para>Changes the shape of the tensor to the specified shape.
/// The total number of elements must not change.
/// One dimension of the <paramref name="shp"/> can be specified as <see cref="Tensor.Remainder"/>,
/// in which case the size of that dimension is inferred automatically.</para>
/// <para>If a reshape is not possible without copying the data of the tensor, <c>None</c> is returned.</para>
/// <para>The operation returns a view of the original tensor and shares its storage. Modifications done to the
/// returned tensor will affect the original tensor. Also, modifying the orignal tensor will affect the view.</para>
/// </remarks>
/// <seealso cref="reshapeView"/><seealso cref="reshape"/>
static member tryReshapeView shp (a: Tensor<'T>) =
ITensor.tryReshapeView shp a |> Option.map (fun r -> r :?> Tensor<'T>)
/// <summary>Creates a reshaped view of the tensor (without copying).</summary>
/// <param name="shp">The target shape.</param>
/// <param name="a">The tensor to operate on.</param>
/// <returns>A reshaped view of the original tensor.</returns>
/// <remarks>
/// <para>Changes the shape of the tensor to the specified shape.
/// The total number of elements must not change.
/// One dimension of the <paramref name="shp"/> can be specified as <see cref="Tensor.Remainder"/>,
/// in which case the size of that dimension is inferred automatically.</para>
/// <para>If a reshape is not possible without copying the data of the tensor, an exception is raised.
/// To avoid this, use <see cref="tryReshapeView"/> instead.</para>
/// <para>The operation returns a view of the original tensor and shares its storage. Modifications done to the
/// returned tensor will affect the original tensor. Also, modifying the orignal tensor will affect the view.</para>
/// </remarks>
/// <seealso cref="tryReshapeView"/><seealso cref="reshape"/>
static member reshapeView shp (a: Tensor<'T>) = ITensor.reshapeView shp a :?> Tensor<'T>
/// <summary>Changes the shape of a tensor.</summary>
/// <param name="shp">The target shape.</param>
/// <param name="a">The tensor to operate on.</param>
/// <returns>A tensor of the specified shape.</returns>
/// <example><code language="fsharp">
/// let a = HostTensor.zeros [2L; 3L; 4L]
/// let b = Tensor.reshape [6L; 4L] a // b.Shape = [6L; 4L]
/// let c = Tensor.reshape [2L; Remainder; 1L] a // c.Shape = [2L; 12L; 1L]
/// </code></example>
/// <remarks>
/// <para>Changes the shape of the tensor to the specified shape.
/// The total number of elements must not change.
/// One dimension of the <paramref name="shp"/> can be specified as <see cref="Tensor.Remainder"/>,
/// in which case the size of that dimension is inferred automatically.</para>
/// <para>If a reshape is possible without copying the data of the tensor, a view of the original tensor is returned
/// and the storage is shared. In this case, modifications done to the returned tensor will affect the original
/// tensor.</para>
/// <para>If a reshape is not possible without copying the data of the tensor, a new tensor of the specified shape
/// and a new storage is allocated and the data is copied into the new tensor.</para>
/// </remarks>
/// <seealso cref="tryReshapeView"/><seealso cref="reshapeView"/><seealso cref="flatten"/><seealso cref="Shape"/>
static member reshape shp (a: Tensor<'T>) = ITensor.reshape shp a :?> Tensor<'T>
/// <summary>Flattens the tensor into a (one-dimensional) vector.</summary>
/// <param name="a">The tensor to operate on.</param>
/// <returns>A vector.</returns>
/// <example><code language="fsharp">
/// let a = HostTensor.zeros [2L; 3L; 4L]
/// let b = Tensor.flatten a // b.Shape = [24L]
/// </code></example>
/// <remarks>
/// <para>If a reshape is possible without copying the data of the tensor, a view of the original tensor is returned
/// and the storage is shared. In this case, modifications done to the returned tensor will affect the original
/// tensor.</para>
/// </remarks>
/// <seealso cref="reshape"/>
static member flatten (a: Tensor<'T>) = ITensor.flatten a :?> Tensor<'T>
/// <summary>Swaps the specified dimensions of the tensor.</summary>
/// <param name="ax1">The dimension to swap.</param>
/// <param name="ax2">The dimension to swap with.</param>
/// <param name="a">The tensor to operate on.</param>
/// <returns>The tensor with the dimensions swapped.</returns>
/// <example><code language="fsharp">
/// let a = HostTensor.zeros [2L; 3L; 4L]
/// let b = Tensor.swapDim 0 2 a // b.Shape = [4L; 3L; 2L]
/// </code></example>
/// <remarks>
/// <para>A view of the original tensor is returned and the storage is shared. Modifications done to the returned
/// tensor will affect the original tensor.</para>
/// </remarks>
/// <seealso cref="permuteAxes"/><seealso cref="T"/>
static member swapDim ax1 ax2 (a: Tensor<'T>) = ITensor.swapDim ax1 ax2 a :?> Tensor<'T>
/// <summary>(Batched) transpose of a matrix.</summary>
/// <param name="a">The tensor to operate on.</param>
/// <returns>The result of this operation.</returns>
/// <seealso cref="T"/>
static member transpose (a: Tensor<'T>) = ITensor.transpose a :?> Tensor<'T>
/// <summary>Permutes the axes as specified.</summary>
/// <param name="permut">The permutation to apply to the dimensions of tensor.</param>
/// <param name="a">The tensor to operate on.</param>
/// <returns>The tensor with the dimensions permuted.</returns>
/// <example><code language="fsharp">
/// let a = HostTensor.zeros [0L; 11L; 22L; 33L; 44L]
/// let b = Tensor.permuteAxes [3; 2; 4; 1; 0] a // b.Shape = [44L; 33L; 11L; 0L; 22L]
/// </code></example>
/// <remarks>
/// <para>Each entry in the specified permutation specifies the new position of the corresponding axis, i.e. to
/// which position the axis moves.</para>
/// <para>A view of the original tensor is returned and the storage is shared. Modifications done to the returned
/// tensor will affect the original tensor.</para>
/// </remarks>
/// <seealso cref="swapDim"/><seealso cref="T"/>
static member permuteAxes (permut: int list) (a: Tensor<'T>) = ITensor.permuteAxes permut a :?> Tensor<'T>
/// <summary>Reverses the elements in the specified dimension.</summary>
/// <param name="ax">The axis to reverse.</param>
/// <param name="a">The tensor to operate on.</param>
/// <returns>The tensor with the dimensions permuted.</returns>
/// <example><code language="fsharp">
/// let a = HostTensor.ofList [0; 1; 2; 3]
/// let b = Tensor.reverseAxis 0 a // b = [3; 2; 1; 0]
/// </code></example>
/// <remarks>
/// <para>The elements along the specified axis are reversed.</para>
/// <para>A view of the original tensor is returned and the storage is shared. Modifications done to the returned
/// tensor will affect the original tensor.</para>
/// </remarks>
static member reverseAxis ax (a: Tensor<'T>) = ITensor.reverseAxis ax a :?> Tensor<'T>
/// <summary>Pads the tensor from the left with size-one dimensions until it has at least the specified number of
/// dimensions.</summary>
/// <param name="minDims">The minimum number of dimensions.</param>
/// <param name="a">The tensor to operate on.</param>
/// <returns>A tensor with at least <paramref name="minDims"/> dimensions.</returns>
/// <example><code language="fsharp">
/// let a = HostTensor.zeros [2L; 3L]
/// let b = Tensor.atLeastND 5 a // b.Shape = [1L; 1L; 1L; 2L; 3L]
/// </code></example>
/// <remarks>
/// <para>Size-one dimensions are inserted at the front until the tensor has at least the specified number of
/// dimensions. If it already has the specified number of dimensions or more, it is returned unchanged.</para>
/// <para>A view of the original tensor is returned and the storage is shared. Modifications done to the returned
/// tensor will affect the original tensor.</para>
/// </remarks>
/// <seealso cref="padLeft"/><seealso cref="reshape"/>
static member atLeastND minDims (a: Tensor<'T>) = ITensor.atLeastND minDims a :?> Tensor<'T>
/// <summary>Pads the tensor from the left with size-one dimensions until it has at least one dimension.</summary>
/// <param name="a">The tensor to operate on.</param>
/// <returns>A tensor with at least one dimensions.</returns>
/// <seealso cref="atLeastND"/>
static member atLeast1D (a: Tensor<'T>) = a |> Tensor<_>.atLeastND 1
/// <summary>Pads the tensor from the left with size-one dimensions until it has at least two dimensions.</summary>
/// <param name="a">The tensor to operate on.</param>
/// <returns>A tensor with at least two dimensions.</returns>
/// <seealso cref="atLeastND"/>
static member atLeast2D (a: Tensor<'T>) = a |> Tensor<_>.atLeastND 2
/// <summary>Pads the tensor from the left with size-one dimensions until it has at least three dimensions.</summary>
/// <param name="a">The tensor to operate on.</param>
/// <returns>A tensor with at least three dimensions.</returns>
/// <seealso cref="atLeastND"/>
static member atLeast3D (a: Tensor<'T>) = a |> Tensor<_>.atLeastND 3
/// <summary>Transpose of a matrix.</summary>
/// <value>The transposed matrx.</value>
/// <example><code language="fsharp">
/// let a = HostTensor.zeros [3L; 5L]
/// let b = a.T // b.Shape = [5L; 3L]
/// </code></example>
/// <remarks>
/// <para>If the given tensor has more then two dimensions, the last two axes are swapped.</para>
/// <para>The operation returns a view of the original tensor and shares its storage. Modifications done to the
/// returned tensor will affect the original tensor. Also, modifying the orignal tensor will affect the view.</para>
/// </remarks>
/// <seealso cref="permuteAxes"/><seealso cref="swapDim"/>
member inline this.T =
Tensor<_>.transpose this
/// Returns a copy of the tensor.
member internal this.Copy (?order) =
let trgt, src = Tensor.PrepareElemwise (this, ?order=order)
trgt.Backend.Copy (trgt=trgt, src=src)
trgt
/// <summary>Returns a copy of the tensor.</summary>
/// <param name="a">The tensor to copy.</param>
/// <param name="order">The memory layout of the copy. (default: row-major)</param>
/// <returns>A copy of the tensor.</returns>
/// <remarks>
/// <para>A new tensor is created with the specified memory layout on the same device as the orignal tensor.</para>
/// <para>The elements of the original tensor are copied into the new tensor.</para>
/// </remarks>
/// <seealso cref="CopyFrom"/><seealso cref="transfer``1"/>
static member copy (a: Tensor<'T>, ?order) : Tensor<'T> =
a.Copy (?order=order)
/// <summary>Fills this tensor with a copy of the specified tensor.</summary>
/// <param name="src">The tensor to copy from.</param>
/// <remarks>
/// <para>The source tensor must have the same shape and be stored on the same device as this tensor.</para>
/// </remarks>
/// <seealso cref="copy"/><seealso cref="FillFrom"/>
member trgt.CopyFrom (src: Tensor<'T>) =
Tensor.CheckSameShape trgt src
Tensor.CheckSameStorage [trgt; src]
trgt.Backend.Copy (trgt=trgt, src=src)
/// <summary>Transfers the specified tensor located on another device into this tensor.</summary>
/// <param name="src">The tensor to transfer from.</param>
/// <remarks>
/// <para>The elements of the original tensor are copied into the new tensor.</para>
/// <para>Both tensors must have same shape and type.</para>
/// <para>If both tensors are located on the same device, a copy is performed.</para>
/// </remarks>
/// <see cref="transfer``1"/>
member trgt.TransferFrom (src: Tensor<'T>) =
Tensor.CheckSameShape trgt src
if trgt.Dev = src.Dev then
trgt.CopyFrom (src)
else
if not (trgt.Backend.Transfer (trgt=trgt, src=src) ||
src.Backend.Transfer (trgt=trgt, src=src)) then
invalidOp "Cannot transfer from storage %s to storage %s." src.Dev.Id trgt.Dev.Id
/// Transfers this tensor to the specifed device.
member internal src.Transfer (dev: ITensorDevice) =
if src.Dev <> dev then
let trgt = Tensor<'T> (src.Shape, dev)
trgt.TransferFrom src
trgt
else src
/// <summary>Transfers a tensor to the specifed device.</summary>
/// <param name="dev">The target device.</param>
/// <param name="a">The tensor to transfer.</param>
/// <returns>A tensor on the target device.</returns>
/// <example><code language="fsharp">
/// let a = HostTensor.zeros [3L; 5L] // a.Dev = HostTensor.Dev
/// let b = Tensor.transfer CudaTensor.Dev a // b.Dev = CudaTensor.Dev
/// </code></example>
/// <remarks>
/// <para>A new tensor is created on the specified device.</para>
/// <para>The elements of the original tensor are copied into the new tensor.</para>
/// <para>If the target device matches the current device of the tensor, the original tensor is returned.</para>
/// </remarks>
/// <seealso cref="TransferFrom"/><seealso cref="Dev"/><seealso cref="copy"/>
static member transfer (dev: ITensorDevice) (src: Tensor<'T>) =
src.Transfer (dev)
/// this tensor as Tensor<bool>
member internal this.AsBool : Tensor<bool> =
if this.DataType = typeof<bool> then
this |> box :?> Tensor<bool>
else
invalidOp "The operation requires a Tensor<bool> but the data type of the specified tensor is %s."
this.DataType.Name
/// this tensor as Tensor<int64>
member internal this.AsInt64 : Tensor<int64> =
if this.DataType = typeof<int64> then
this |> box :?> Tensor<int64>
else
invalidOp "The operation requires a Tensor<int64> but the data type of the specified tensor is %s."
this.DataType.Name
/// <summary>Fills this tensor with a copy of the specified tensor.</summary>
/// <param name="src">The tensor to copy from.</param>
/// <remarks>
/// <para>The source tensor is broadcasted to the size of this tensor.</para>
/// <para>The source tensor must be stored on the same device as this tensor.</para>
/// </remarks>
/// <seealso cref="CopyFrom"/>
member trgt.FillFrom (src: Tensor<'T>) =
let src = Tensor.PrepareElemwiseSources (trgt, src)
trgt.CopyFrom src
/// <summary>Fills this tensor with the specified constant value.</summary>
/// <param name="value">The value to use.</param>
/// <seealso cref="filled"/>
member trgt.FillConst (value: 'T) =
trgt.Backend.FillConst (value=value, trgt=trgt)
/// <summary>Fills this vector with an equispaced sequence of elements.</summary>
/// <param name="start">The starting value.</param>
/// <param name="incr">The increment between successive elements.</param>
/// <remarks>
/// <para>This tensor must be one dimensional.</para>
/// </remarks>
/// <seealso cref="arange``3"/>
member trgt.FillIncrementing (start: 'T, incr: 'T) =
if trgt.NDims <> 1 then invalidOp "FillIncrementing requires a vector."
trgt.Backend.FillIncrementing (start=start, incr=incr, trgt=trgt)
/// <summary>Copies elements from a tensor of different data type into this tensor and converts their type.</summary>
/// <typeparam name="'C">The data type to convert from.</typeparam>
/// <param name="a">The tensor to copy from.</param>
/// <seealso cref="convert``1"/>
member trgt.FillConvert (a: Tensor<'C>) =
let a = Tensor.PrepareElemwiseSources (trgt, a)
trgt.Backend.Convert (trgt=trgt, src=a)
/// <summary>Convert the elements of a tensor to the specifed type.</summary>
/// <typeparam name="'C">The data type to convert from.</typeparam>
/// <param name="a">The tensor to convert.</param>
/// <returns>A tensor of the new data type.</returns>
/// <example><code language="fsharp">
/// let a = HostTensor.ofList [1; 2; 3]
/// let b = Tensor<float>.convert a // b = [1.0; 2.0; 3.0]
/// </code></example>
/// <remarks>
/// <para>The elements of the original tensor are copied into the new tensor and their type is converted
/// during the copy.</para>
/// <para>For tensors that contain data of non-primitive types and are stored on the host,
/// the <c>op_Explicit</c> or <c>op_Implicit</c> methods of the source or destination type are used to perform
/// the conversion.</para>
/// </remarks>
/// <seealso cref="FillConvert``1"/>
static member convert (a: Tensor<'C>) : Tensor<'T> =
let trgt, a = Tensor.PrepareElemwise (a)
trgt.FillConvert (a)
trgt
/// <summary>Fills this tensor with the element-wise prefix plus of the argument.</summary>
/// <param name="a">The tensor to operate on.</param>
/// <seealso cref="op_UnaryPlus"/>
member trgt.FillUnaryPlus (a: Tensor<'T>) =
let a = Tensor.PrepareElemwiseSources (trgt, a)
trgt.Backend.UnaryPlus (trgt=trgt, src1=a)
/// <summary>Element-wise prefix plus.</summary>
/// <param name="a">The tensor to operate on.</param>
/// <returns>A new tensor containing the result of this operation.</returns>
/// <example><code language="fsharp">
/// let a = HostTensor.ofList [5.0; 6.0; 7.0]
/// let c = +a // c = [5.0; 6.0; 7.0]
/// </code></example>
/// <remarks>
/// <para>Applies the unary plus operator to each element of tensor <paramref name="a"/> and returns the result
/// as a new tensor.</para>
/// <para>For most data types, this operation does not change the value.</para>
/// </remarks>
/// <seealso cref="FillUnaryPlus"/>
static member (~+) (a: Tensor<'T>) =
let trgt, a = Tensor.PrepareElemwise (a)
trgt.FillUnaryPlus (a)
trgt
/// <summary>Fills this tensor with the element-wise negation of the argument.</summary>
/// <param name="a">The tensor to operate on.</param>
/// <seealso cref="op_UnaryNegation"/>
member trgt.FillUnaryMinus (a: Tensor<'T>) =
let a = Tensor.PrepareElemwiseSources (trgt, a)
trgt.Backend.UnaryMinus (trgt=trgt, src1=a)
/// <summary>Element-wise negation.</summary>
/// <param name="a">The tensor to operate on.</param>
/// <returns>A new tensor containing the result of this operation.</returns>
/// <example><code language="fsharp">
/// let a = HostTensor.ofList [5.0; 6.0; 7.0]
/// let c = -a // c = [-5.0; -6.0; -7.0]
/// </code></example>
/// <remarks>
/// <para>Negates each element of tensor <paramref name="a"/> and returns the result as a new tensor.</para>
/// </remarks>
/// <seealso cref="FillNegate"/>
static member (~-) (a: Tensor<'T>) =
let trgt, a = Tensor.PrepareElemwise (a)
trgt.FillUnaryMinus (a)
trgt
/// <summary>Fills this tensor with the element-wise absolute value of the argument.</summary>
/// <param name="a">The tensor to apply this operation to.</param>
/// <seealso cref="Abs"/>
member trgt.FillAbs (a: Tensor<'T>) =
let a = Tensor.PrepareElemwiseSources (trgt, a)
trgt.Backend.Abs (trgt=trgt, src1=a)
/// <summary>Element-wise absolute value.</summary>
/// <param name="a">The tensor to apply this operation to.</param>
/// <returns>A new tensor containing the result of this operation.</returns>
/// <example><code language="fsharp">
/// let a = HostTensor.ofList [-2; -1; 1]
/// let b = abs a // b = [2; 1; 1]
/// </code></example>
/// <remarks>Computes the absolute value of each element of the specified tensor and returns them as a new tensor.
/// Do not call this function directly; instead use the F# <c>abs</c> function.</remarks>
/// <seealso cref="FillAbs"/>
static member Abs (a: Tensor<'T>) =
let trgt, a = Tensor.PrepareElemwise (a)
trgt.FillAbs (a)
trgt
/// <summary>Fills this tensor with the element-wise sign of the argument.</summary>
/// <param name="a">The tensor to apply this operation to.</param>
/// <seealso cref="Sgn"/>
member trgt.FillSgn (a: Tensor<'T>) =
let a = Tensor.PrepareElemwiseSources (trgt, a)
trgt.Backend.Sgn (trgt=trgt, src1=a)
/// <summary>Element-wise sign.</summary>
/// <param name="a">The tensor to apply this operation to.</param>
/// <returns>A new tensor containing the result of this operation.</returns>
/// <example><code language="fsharp">
/// let a = HostTensor.ofList [-2; -1; 0; 2]
/// let b = sgn a // b = [-1; -1; 0; 1]
/// </code></example>
/// <remarks>Computes the sign of each element of the specified tensor and returns them as a new tensor.
/// The type of the returned tensor matches the type of the argument tensor.
/// Do not call this function directly; instead use the F# <c>sgn</c> function.</remarks>
/// <seealso cref="FillSgn"/>
static member Sgn (a: Tensor<'T>) =
let trgt, a = Tensor.PrepareElemwise (a)
trgt.FillSgn (a)
trgt
/// <summary>Fills this tensor with the element-wise natural logarithm of the argument.</summary>
/// <param name="a">The tensor to apply this operation to.</param>
/// <seealso cref="Log"/>
member trgt.FillLog (a: Tensor<'T>) =
let a = Tensor.PrepareElemwiseSources (trgt, a)
trgt.Backend.Log (trgt=trgt, src1=a)
/// <summary>Element-wise natural logarithm.</summary>
/// <param name="a">The tensor to apply this operation to.</param>
/// <returns>A new tensor containing the result of this operation.</returns>
/// <example><code language="fsharp">
/// let a = HostTensor.ofList [1.0; 2.71828; 4.0]
/// let b = log a // b = [0.0; 1.0; 1.38529]
/// </code></example>
/// <remarks>Computes the natural logarithm of each element of the specified tensor and returns them as a new tensor.
/// Do not call this function directly; instead use the F# <c>log</c> function.</remarks>
/// <seealso cref="FillLog"/><seealso cref="Log10"/><seealso cref="Exp"/>
static member Log (a: Tensor<'T>) =
let trgt, a = Tensor.PrepareElemwise (a)
trgt.FillLog (a)
trgt
/// <summary>Fills this tensor with the element-wise common logarithm of the argument.</summary>
/// <param name="a">The tensor to apply this operation to.</param>
/// <seealso cref="Log10"/>
member trgt.FillLog10 (a: Tensor<'T>) =
let a = Tensor.PrepareElemwiseSources (trgt, a)
trgt.Backend.Log10 (trgt=trgt, src1=a)
/// <summary>Element-wise common logarithm.</summary>
/// <param name="a">The tensor to apply this operation to.</param>
/// <returns>A new tensor containing the result of this operation.</returns>
/// <example><code language="fsharp">
/// let a = HostTensor.ofList [1.0; 10.0; 100.0]
/// let b = log10 a // b = [0.0; 1.0; 2.0]
/// </code></example>
/// <remarks>Computes the common logarithm (to base 10) of each element of the specified tensor and returns them
/// as a new tensor.
/// Do not call this function directly; instead use the F# <c>log10</c> function.</remarks>
/// <seealso cref="FillLog10"/><seealso cref="Log"/>
static member Log10 (a: Tensor<'T>) =
let trgt, a = Tensor.PrepareElemwise (a)
trgt.FillLog10 (a)
trgt
/// <summary>Fills this tensor with the element-wise exponential function of the argument.</summary>
/// <param name="a">The tensor to apply this operation to.</param>
/// <seealso cref="Exp"/>
member trgt.FillExp (a: Tensor<'T>) =
let a = Tensor.PrepareElemwiseSources (trgt, a)
trgt.Backend.Exp (trgt=trgt, src1=a)
/// <summary>Element-wise exponential function.</summary>
/// <param name="a">The tensor to apply this operation to.</param>
/// <returns>A new tensor containing the result of this operation.</returns>
/// <example><code language="fsharp">
/// let a = HostTensor.ofList [-1.0; 0.0; 1.0; 10.0]
/// let b = exp a // b = [0.36787; 1.0; 2.71828; 22026.4657]
/// </code></example>
/// <remarks>Computes the exponential function of each element of the specified tensor and returns them
/// as a new tensor.
/// Do not call this function directly; instead use the F# <c>exp</c> function.</remarks>
/// <seealso cref="FillExp"/><seealso cref="Log"/>
static member Exp (a: Tensor<'T>) =
let trgt, a = Tensor.PrepareElemwise (a)
trgt.FillExp (a)
trgt
/// <summary>Fills this tensor with the element-wise sine of the argument.</summary>
/// <param name="a">The tensor to apply this operation to.</param>
/// <seealso cref="Sin"/>
member trgt.FillSin (a: Tensor<'T>) =
let a = Tensor.PrepareElemwiseSources (trgt, a)
trgt.Backend.Sin (trgt=trgt, src1=a)
/// <summary>Element-wise sine.</summary>
/// <param name="a">The tensor to apply this operation to.</param>
/// <returns>A new tensor containing the result of this operation.</returns>
/// <example><code language="fsharp">
/// let a = HostTensor.ofList [-1.57079; 0.0; 1.57079]
/// let b = sin a // b = [-1.0; 0.0; 1.0]
/// </code></example>
/// <remarks>Computes the sine of each element of the specified tensor and returns them
/// as a new tensor.
/// Do not call this function directly; instead use the F# <c>sin</c> function.</remarks>
/// <seealso cref="FillSin"/><seealso cref="Asin"/>
static member Sin (a: Tensor<'T>) =
let trgt, a = Tensor.PrepareElemwise (a)
trgt.FillSin (a)
trgt
/// <summary>Fills this tensor with the element-wise cosine of the argument.</summary>
/// <param name="a">The tensor to apply this operation to.</param>
/// <seealso cref="Cos"/>
member trgt.FillCos (a: Tensor<'T>) =
let a = Tensor.PrepareElemwiseSources (trgt, a)
trgt.Backend.Cos (trgt=trgt, src1=a)