forked from SciSharp/NumSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNdArray.Onces.cs
More file actions
41 lines (34 loc) · 1.33 KB
/
NdArray.Onces.cs
File metadata and controls
41 lines (34 loc) · 1.33 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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Numerics;
namespace NumSharp.Extensions
{
public static partial class NDArrayExtensions
{
public static NDArray<T> Onces<T>(this NDArray<T> np, params int[] shape)
{
int numOFElements = 1;
for(int idx = 0;idx < shape.Length;idx++)
{
numOFElements *= shape[idx];
}
np.Data= new T[numOFElements];
np.Shape = new Shape(shape);
dynamic onceArray = np.Data;
var elementType = typeof(T);
switch (elementType.Name)
{
case ("Int32"): onceArray = ((int[])onceArray).Select(x => 1).ToArray(); break;
case ("Double"): onceArray = ((double[])onceArray).Select(x => 1.0).ToArray(); break;
case ("Float"): onceArray = ((float[])onceArray).Select(x => 1.0).ToArray(); break;
case ("Complex"): onceArray = ((Complex[])onceArray).Select(x => new Complex(1,0)).ToArray(); break;
case ("Quaternion"): onceArray = ((Quaternion[])onceArray).Select(x => new Quaternion(new Vector3(0,0,0),1)).ToArray(); break;
}
np.Data = (T[])onceArray;
return np;
}
}
}