Repository navigation
Expand file tree
/
Copy pathNDArray.Shift.cs
More file actions
66 lines (63 loc) · 2.89 KB
/
Copy pathNDArray.Shift.cs
File metadata and controls
66 lines (63 loc) · 2.89 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
namespace NumSharp
{
/// <summary>
/// Bitwise shift operators for NDArray.
///
/// NumPy alignment: mirrors <c>__lshift__</c> / <c>__rshift__</c> on <c>ndarray</c>.
/// Wires straight into <see cref="Backends.TensorEngine.LeftShift"/> and
/// <see cref="Backends.TensorEngine.RightShift"/>, which validate integer dtype.
///
/// C# shift operator constraints:
/// <list type="bullet">
/// <item>First operand must be the declaring type (<see cref="NDArray"/>). So
/// <c>object << NDArray</c> is impossible — use <c>np.left_shift(lhs, rhs)</c>
/// instead, or cast <c>lhs</c> to NDArray explicitly.</item>
/// <item>Second operand can be any type (C# 11+; LangVersion=latest, net8/net10 support this).</item>
/// <item>Compound <c><<=</c> / <c>>>=</c> are synthesized by the compiler from these.</item>
/// </list>
/// </summary>
public partial class NDArray
{
/// <summary>
/// Element-wise left shift. Integer dtypes only.
/// Shifts bits of <paramref name="lhs"/> left by <paramref name="rhs"/>.
/// Broadcast-aware.
/// </summary>
public static NDArray operator <<(NDArray lhs, NDArray rhs)
{
return lhs.TensorEngine.LeftShift(lhs, rhs);
}
/// <summary>
/// Element-wise left shift with any scalar or array-like on RHS.
/// Converts RHS via <see cref="np.asanyarray(object)"/> (matches NumPy's PyArray_FromAny).
/// </summary>
// Scope: np.asanyarray(rhs) mints a temp for a scalar/array-like operand; [NDScoped]
// reclaims it while leaving an NDArray-input passthrough untouched (see NDArray.AND.cs).
[NDScoped]
public static NDArray operator <<(NDArray lhs, object rhs)
{
return lhs << np.asanyarray(rhs);
}
/// <summary>
/// Element-wise right shift. Integer dtypes only.
/// Shifts bits of <paramref name="lhs"/> right by <paramref name="rhs"/>.
/// Logical shift for unsigned types, arithmetic shift for signed types.
/// Broadcast-aware.
/// </summary>
public static NDArray operator >>(NDArray lhs, NDArray rhs)
{
return lhs.TensorEngine.RightShift(lhs, rhs);
}
/// <summary>
/// Element-wise right shift with any scalar or array-like on RHS.
/// Converts RHS via <see cref="np.asanyarray(object)"/> (matches NumPy's PyArray_FromAny).
/// </summary>
// Scope: np.asanyarray(rhs) mints a temp for a scalar/array-like operand; [NDScoped]
// reclaims it while leaving an NDArray-input passthrough untouched (see NDArray.AND.cs).
[NDScoped]
public static NDArray operator >>(NDArray lhs, object rhs)
{
return lhs >> np.asanyarray(rhs);
}
}
}