forked from SciSharp/NumSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNDArray.negative.cs
More file actions
106 lines (97 loc) · 3.52 KB
/
NDArray.negative.cs
File metadata and controls
106 lines (97 loc) · 3.52 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
using System;
using NumSharp.Backends;
namespace NumSharp
{
public partial class NDArray
{
/// <summary>
/// Negates all positive values.
/// </summary>
public NDArray negative()
{
if (this.size == 0)
return this.Clone();
var @out = TensorEngine.Cast(this, dtype ?? this.dtype, copy: true);
var len = @out.size;
unsafe
{
switch (@out.GetTypeCode)
{
case NPTypeCode.Boolean:
{
var out_addr = (bool*)@out.Address;
var addr = (bool*)@out.Address;
for (int i = 0; i < len; i++)
*(out_addr + i) = !*(addr + i);
return @out;
}
#if _REGEN1
%foreach supported_numericals_signed,supported_numericals_signed_lowercase%
case NPTypeCode.#1:
{
var out_addr = (#2*)@out.Address;
for (int i = 0; i < len; i++) {
var val = *(out_addr + i);
if (val > 0)
*(out_addr + i) = -val;
}
return @out;
}
%
%foreach supported_numericals_unsigned,supported_numericals_unsigned_lowercase,supported_numericals_unsigned_defaultvals
case NPTypeCode.#1:
default:
throw new NotSupportedException();
#else
case NPTypeCode.Int32:
{
var out_addr = (int*)@out.Address;
for (int i = 0; i < len; i++) {
var val = *(out_addr + i);
if (val > 0)
*(out_addr + i) = -val;
}
return @out;
}
case NPTypeCode.Int64:
{
var out_addr = (long*)@out.Address;
for (int i = 0; i < len; i++) {
var val = *(out_addr + i);
if (val > 0)
*(out_addr + i) = -val;
}
return @out;
}
case NPTypeCode.Single:
{
var out_addr = (float*)@out.Address;
for (int i = 0; i < len; i++) {
var val = *(out_addr + i);
if (val > 0)
*(out_addr + i) = -val;
}
return @out;
}
case NPTypeCode.Double:
{
var out_addr = (double*)@out.Address;
for (int i = 0; i < len; i++) {
var val = *(out_addr + i);
if (val > 0)
*(out_addr + i) = -val;
}
return @out;
}
case NPTypeCode.Byte:
case NPTypeCode.UInt16:
case NPTypeCode.UInt32:
case NPTypeCode.UInt64:
default:
throw new NotSupportedException();
#endif
}
}
}
}
}