Skip to content

Commit a3926dc

Browse files
committed
Added np.exp2, np.expm1
1 parent 189183d commit a3926dc

File tree

4 files changed

+286
-1
lines changed

4 files changed

+286
-1
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using DecimalMath;
4+
using NumSharp.Utilities;
5+
6+
namespace NumSharp.Backends
7+
{
8+
public partial class DefaultEngine
9+
{
10+
public override NDArray Exp2(in NDArray nd, Type dtype) => Exp2(nd, dtype?.GetTypeCode());
11+
12+
public override NDArray Exp2(in NDArray nd, NPTypeCode? typeCode = null)
13+
{
14+
15+
16+
if (nd.size == 0)
17+
return nd.Clone();
18+
19+
var @out = Cast(nd, typeCode ?? nd.typecode, copy: true);
20+
var len = @out.size;
21+
22+
unsafe
23+
{
24+
switch (@out.GetTypeCode)
25+
{
26+
#if _REGEN
27+
28+
%foreach except(supported_numericals, "Decimal"),except(supported_numericals_lowercase, "decimal")%
29+
case NPTypeCode.#1:
30+
{
31+
var out_addr = (#2*)@out.Address;
32+
Parallel.For(0, len, i => *(out_addr + i) = Converts.To#1(Math.Pow(2, *(out_addr + i))));
33+
return @out;
34+
}
35+
%
36+
case NPTypeCode.Decimal:
37+
{
38+
var out_addr = (decimal*)@out.Address;
39+
Parallel.For(0, len, i => *(out_addr + i) = DecimalEx.Pow(2, *(out_addr + i)));
40+
return @out;
41+
}
42+
default:
43+
throw new NotSupportedException();
44+
#else
45+
case NPTypeCode.Byte:
46+
{
47+
var out_addr = (byte*)@out.Address;
48+
Parallel.For(0, len, i => *(out_addr + i) = Converts.ToByte(Math.Pow(2, *(out_addr + i))));
49+
return @out;
50+
}
51+
case NPTypeCode.Int16:
52+
{
53+
var out_addr = (short*)@out.Address;
54+
Parallel.For(0, len, i => *(out_addr + i) = Converts.ToInt16(Math.Pow(2, *(out_addr + i))));
55+
return @out;
56+
}
57+
case NPTypeCode.UInt16:
58+
{
59+
var out_addr = (ushort*)@out.Address;
60+
Parallel.For(0, len, i => *(out_addr + i) = Converts.ToUInt16(Math.Pow(2, *(out_addr + i))));
61+
return @out;
62+
}
63+
case NPTypeCode.Int32:
64+
{
65+
var out_addr = (int*)@out.Address;
66+
Parallel.For(0, len, i => *(out_addr + i) = Converts.ToInt32(Math.Pow(2, *(out_addr + i))));
67+
return @out;
68+
}
69+
case NPTypeCode.UInt32:
70+
{
71+
var out_addr = (uint*)@out.Address;
72+
Parallel.For(0, len, i => *(out_addr + i) = Converts.ToUInt32(Math.Pow(2, *(out_addr + i))));
73+
return @out;
74+
}
75+
case NPTypeCode.Int64:
76+
{
77+
var out_addr = (long*)@out.Address;
78+
Parallel.For(0, len, i => *(out_addr + i) = Converts.ToInt64(Math.Pow(2, *(out_addr + i))));
79+
return @out;
80+
}
81+
case NPTypeCode.UInt64:
82+
{
83+
var out_addr = (ulong*)@out.Address;
84+
Parallel.For(0, len, i => *(out_addr + i) = Converts.ToUInt64(Math.Pow(2, *(out_addr + i))));
85+
return @out;
86+
}
87+
case NPTypeCode.Char:
88+
{
89+
var out_addr = (char*)@out.Address;
90+
Parallel.For(0, len, i => *(out_addr + i) = Converts.ToChar(Math.Pow(2, *(out_addr + i))));
91+
return @out;
92+
}
93+
case NPTypeCode.Double:
94+
{
95+
var out_addr = (double*)@out.Address;
96+
Parallel.For(0, len, i => *(out_addr + i) = Converts.ToDouble(Math.Pow(2, *(out_addr + i))));
97+
return @out;
98+
}
99+
case NPTypeCode.Single:
100+
{
101+
var out_addr = (float*)@out.Address;
102+
Parallel.For(0, len, i => *(out_addr + i) = Converts.ToSingle(Math.Pow(2, *(out_addr + i))));
103+
return @out;
104+
}
105+
case NPTypeCode.Decimal:
106+
{
107+
var out_addr = (decimal*)@out.Address;
108+
Parallel.For(0, len, i => *(out_addr + i) = DecimalEx.Pow(2, *(out_addr + i)));
109+
return @out;
110+
}
111+
default:
112+
throw new NotSupportedException();
113+
#endif
114+
}
115+
}
116+
}
117+
}
118+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using DecimalMath;
4+
using NumSharp.Utilities;
5+
6+
namespace NumSharp.Backends
7+
{
8+
public partial class DefaultEngine
9+
{
10+
public override NDArray Expm1(in NDArray nd, Type dtype) => Expm1(nd, dtype?.GetTypeCode());
11+
12+
public override NDArray Expm1(in NDArray nd, NPTypeCode? typeCode = null)
13+
{
14+
if (nd.size == 0)
15+
return nd.Clone();
16+
17+
var @out = Cast(nd, ResolveUnaryReturnType(nd, typeCode), copy: true);
18+
var len = @out.size;
19+
20+
unsafe
21+
{
22+
switch (@out.GetTypeCode)
23+
{
24+
#if _REGEN
25+
%foreach except(supported_numericals, "Decimal"),except(supported_numericals_lowercase, "decimal")%
26+
case NPTypeCode.#1:
27+
{
28+
var out_addr = (#2*)@out.Address;
29+
Parallel.For(0, len, i => *(out_addr + i) = Converts.To#1(Math.Exp(*(out_addr + i)) - 1));
30+
return @out;
31+
}
32+
%
33+
case NPTypeCode.Decimal:
34+
{
35+
var out_addr = (decimal*)@out.Address;
36+
Parallel.For(0, len, i => *(out_addr + i) = (decimal)Converts.ToDouble(DecimalEx.Exp(*(out_addr + i)) - 1m));
37+
return @out;
38+
}
39+
default:
40+
throw new NotSupportedException();
41+
#else
42+
case NPTypeCode.Byte:
43+
{
44+
var out_addr = (byte*)@out.Address;
45+
Parallel.For(0, len, i => *(out_addr + i) = Converts.ToByte(Math.Exp(*(out_addr + i)) - 1));
46+
return @out;
47+
}
48+
case NPTypeCode.Int16:
49+
{
50+
var out_addr = (short*)@out.Address;
51+
Parallel.For(0, len, i => *(out_addr + i) = Converts.ToInt16(Math.Exp(*(out_addr + i)) - 1));
52+
return @out;
53+
}
54+
case NPTypeCode.UInt16:
55+
{
56+
var out_addr = (ushort*)@out.Address;
57+
Parallel.For(0, len, i => *(out_addr + i) = Converts.ToUInt16(Math.Exp(*(out_addr + i)) - 1));
58+
return @out;
59+
}
60+
case NPTypeCode.Int32:
61+
{
62+
var out_addr = (int*)@out.Address;
63+
Parallel.For(0, len, i => *(out_addr + i) = Converts.ToInt32(Math.Exp(*(out_addr + i)) - 1));
64+
return @out;
65+
}
66+
case NPTypeCode.UInt32:
67+
{
68+
var out_addr = (uint*)@out.Address;
69+
Parallel.For(0, len, i => *(out_addr + i) = Converts.ToUInt32(Math.Exp(*(out_addr + i)) - 1));
70+
return @out;
71+
}
72+
case NPTypeCode.Int64:
73+
{
74+
var out_addr = (long*)@out.Address;
75+
Parallel.For(0, len, i => *(out_addr + i) = Converts.ToInt64(Math.Exp(*(out_addr + i)) - 1));
76+
return @out;
77+
}
78+
case NPTypeCode.UInt64:
79+
{
80+
var out_addr = (ulong*)@out.Address;
81+
Parallel.For(0, len, i => *(out_addr + i) = Converts.ToUInt64(Math.Exp(*(out_addr + i)) - 1));
82+
return @out;
83+
}
84+
case NPTypeCode.Char:
85+
{
86+
var out_addr = (char*)@out.Address;
87+
Parallel.For(0, len, i => *(out_addr + i) = Converts.ToChar(Math.Exp(*(out_addr + i)) - 1));
88+
return @out;
89+
}
90+
case NPTypeCode.Double:
91+
{
92+
var out_addr = (double*)@out.Address;
93+
Parallel.For(0, len, i => *(out_addr + i) = Converts.ToDouble(Math.Exp(*(out_addr + i)) - 1));
94+
return @out;
95+
}
96+
case NPTypeCode.Single:
97+
{
98+
var out_addr = (float*)@out.Address;
99+
Parallel.For(0, len, i => *(out_addr + i) = Converts.ToSingle(Math.Exp(*(out_addr + i)) - 1));
100+
return @out;
101+
}
102+
case NPTypeCode.Decimal:
103+
{
104+
var out_addr = (decimal*)@out.Address;
105+
Parallel.For(0, len, i => *(out_addr + i) = (decimal)Converts.ToDouble(DecimalEx.Exp(*(out_addr + i)) - 1m));
106+
return @out;
107+
}
108+
default:
109+
throw new NotSupportedException();
110+
#endif
111+
}
112+
}
113+
}
114+
}
115+
}

src/NumSharp.Core/Backends/TensorEngine.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ public abstract class TensorEngine
6262
public abstract NDArray Log1p(in NDArray nd, NPTypeCode? typeCode = null);
6363
public abstract NDArray Exp(in NDArray nd, Type dtype);
6464
public abstract NDArray Exp(in NDArray nd, NPTypeCode? typeCode = null);
65+
public abstract NDArray Exp2(in NDArray nd, Type dtype);
66+
public abstract NDArray Exp2(in NDArray nd, NPTypeCode? typeCode = null);
67+
public abstract NDArray Expm1(in NDArray nd, Type dtype);
68+
public abstract NDArray Expm1(in NDArray nd, NPTypeCode? typeCode = null);
6569
public abstract NDArray Tan(in NDArray nd, Type dtype);
6670
public abstract NDArray Tan(in NDArray nd, NPTypeCode? typeCod = null);
6771
public abstract NDArray Sin(in NDArray nd, Type dtype);

src/NumSharp.Core/Statistics/np.exp.cs

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,54 @@ public partial class np
2929
/// <param name="a">Input value.</param>
3030
/// <returns>The natural logarithm of x, element-wise. This is a scalar NDArray.</returns>
3131
/// <remarks>https://docs.scipy.org/doc/numpy/reference/generated/numpy.exp.html</remarks>
32-
public static NDArray exp(in NDArray a) => a.TensorEngine.Exp(a);
32+
public static NDArray exp(in NDArray a) => a.TensorEngine.Exp(a);
33+
34+
/// <summary>
35+
/// Calculate 2**p for all p in the input array.
36+
/// </summary>
37+
/// <param name="a">Input value.</param>
38+
/// <returns>Element-wise 2 to the power x. This is a scalar if x is a scalar.</returns>
39+
/// <remarks>https://docs.scipy.org/doc/numpy/reference/generated/numpy.exp2.html</remarks>
40+
public static NDArray exp2(in NDArray a, Type dtype) => a.TensorEngine.Exp2(a, dtype);
41+
42+
/// <summary>
43+
/// Calculate 2**p for all p in the input array.
44+
/// </summary>
45+
/// <param name="a">Input value.</param>
46+
/// <returns>Element-wise 2 to the power x. This is a scalar if x is a scalar.</returns>
47+
/// <remarks>https://docs.scipy.org/doc/numpy/reference/generated/numpy.exp2.html</remarks>
48+
public static NDArray exp2(in NDArray a, NPTypeCode typeCode) => a.TensorEngine.Exp2(a, typeCode);
49+
50+
/// <summary>
51+
/// Calculate 2**p for all p in the input array.
52+
/// </summary>
53+
/// <param name="a">Input value.</param>
54+
/// <returns>Element-wise 2 to the power x. This is a scalar if x is a scalar.</returns>
55+
/// <remarks>https://docs.scipy.org/doc/numpy/reference/generated/numpy.exp2.html</remarks>
56+
public static NDArray exp2(in NDArray a) => a.TensorEngine.Exp2(a);
57+
58+
/// <summary>
59+
/// Calculate exp(x) - 1 for all elements in the array.
60+
/// </summary>
61+
/// <param name="a">Input value.</param>
62+
/// <returns>Element-wise exponential minus one: out = exp(x) - 1. This is a scalar if x is a scalar.</returns>
63+
/// <remarks>https://docs.scipy.org/doc/numpy-1.15.0/reference/generated/numpy.expm1.html</remarks>
64+
public static NDArray expm1(in NDArray a, Type dtype) => a.TensorEngine.Expm1(a, dtype);
65+
66+
/// <summary>
67+
/// Calculate exp(x) - 1 for all elements in the array.
68+
/// </summary>
69+
/// <param name="a">Input value.</param>
70+
/// <returns>Element-wise exponential minus one: out = exp(x) - 1. This is a scalar if x is a scalar.</returns>
71+
/// <remarks>https://docs.scipy.org/doc/numpy-1.15.0/reference/generated/numpy.expm1.html</remarks>
72+
public static NDArray expm1(in NDArray a, NPTypeCode typeCode) => a.TensorEngine.Expm1(a, typeCode);
73+
74+
/// <summary>
75+
/// Calculate exp(x) - 1 for all elements in the array.
76+
/// </summary>
77+
/// <param name="a">Input value.</param>
78+
/// <returns>Element-wise exponential minus one: out = exp(x) - 1. This is a scalar if x is a scalar.</returns>
79+
/// <remarks>https://docs.scipy.org/doc/numpy-1.15.0/reference/generated/numpy.expm1.html</remarks>
80+
public static NDArray expm1(in NDArray a) => a.TensorEngine.Expm1(a);
3381
}
3482
}

0 commit comments

Comments
 (0)