forked from SciSharp/NumSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNdArray.MakeGeneric.cs
More file actions
48 lines (43 loc) · 1.88 KB
/
NdArray.MakeGeneric.cs
File metadata and controls
48 lines (43 loc) · 1.88 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
using System;
using NumSharp.Generic;
using NumSharp.Utilities;
namespace NumSharp
{
public partial class NDArray
{
/// <summary>
/// Creates an alias without reallocating data.
/// </summary>
/// <typeparam name="T">The type of the generic</typeparam>
/// <returns>This NDArray as a generic version.</returns>
/// <exception cref="InvalidOperationException">When <typeparamref name="T"/> != <see cref="dtype"/></exception>
public NDArray<T> MakeGeneric<T>() where T : unmanaged
{
return new NDArray<T>(Storage);
}
/// <summary>
/// Tries to cast to <see cref="NDArray{T}"/>, otherwise creates an alias without reallocating data.
/// </summary>
/// <typeparam name="T">The type of the generic</typeparam>
/// <returns>This NDArray as a generic version.</returns>
/// <exception cref="InvalidOperationException">When <typeparamref name="T"/> != <see cref="dtype"/></exception>
public NDArray<T> AsGeneric<T>() where T : unmanaged
{
if (typeof(T) != dtype)
return null;
return this as NDArray<T> ?? new NDArray<T>(Storage);
}
/// <summary>
/// Tries to cast to <see cref="NDArray{T}"/>, otherwise calls <see cref="NDArray{T}.astype"/>.
/// </summary>
/// <typeparam name="T">The type of the generic</typeparam>
/// <returns>This NDArray as a generic version.</returns>
/// <exception cref="InvalidOperationException">When <typeparamref name="T"/> != <see cref="dtype"/></exception>
public NDArray<T> AsOrMakeGeneric<T>() where T : unmanaged
{
if (typeof(T) != dtype)
return new NDArray<T>(this.astype(InfoOf<T>.NPTypeCode, copy: true));
return new NDArray<T>(Storage);
}
}
}