Repository navigation
Expand file tree
/
Copy pathnp.broadcast_to.cs
More file actions
152 lines (139 loc) · 9.55 KB
/
Copy pathnp.broadcast_to.cs
File metadata and controls
152 lines (139 loc) · 9.55 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using System;
using NumSharp.Backends;
namespace NumSharp
{
public static partial class np
{
/// <summary>
/// Validates that <paramref name="from"/> can be unilaterally broadcast to <paramref name="target"/>.
/// NumPy's broadcast_to only stretches dimensions of the source that are size 1 to match the target.
/// If any source dimension is larger than the target, or if the source has more dimensions than the target,
/// it raises ValueError.
/// </summary>
private static void ValidateBroadcastTo(Shape from, Shape target)
{
if (from.NDim > target.NDim)
throw new IncorrectShapeException(
$"operands could not be broadcast together with remapped shapes " +
$"[original->remapped]: ({string.Join(",", from.dimensions)}) and requested shape ({string.Join(",", target.dimensions)})");
// Right-align: iterate from the rightmost dimension
for (int i = 0; i < from.NDim; i++)
{
long fromDim = from.dimensions[from.NDim - 1 - i];
long targetDim = target.dimensions[target.NDim - 1 - i];
if (fromDim != 1 && fromDim != targetDim)
throw new IncorrectShapeException(
$"operands could not be broadcast together with remapped shapes " +
$"[original->remapped]: ({string.Join(",", from.dimensions)}) and requested shape ({string.Join(",", target.dimensions)})");
}
}
/// <summary>
/// Broadcast an shape against an other new shape.
/// </summary>
/// <param name="from">The shape that is to be broadcasted</param>
/// <param name="against">The shape that'll be used to broadcast <paramref name="from"/> shape</param>
/// <returns>A readonly view on the original array with the given shape. It is typically not contiguous. Furthermore, more than one element of a broadcasted array may refer to a single memory location.</returns>
/// <remarks>https://numpy.org/doc/stable/reference/generated/numpy.broadcast_to.html</remarks>
public static Shape broadcast_to(Shape from, Shape against)
{
ValidateBroadcastTo(from, against);
return Shape.Broadcast(from, against).LeftShape;
}
/// <summary>
/// Broadcast an array to a new shape.
/// </summary>
/// <param name="from">The NDArray to broadcast.</param>
/// <param name="against">The shape to broadcast against.</param>
/// <returns>These arrays are views on the original arrays. They are typically not contiguous. Furthermore, more than one element of a broadcasted array may refer to a single memory location. If you need to write to the arrays, make copies first.</returns>
/// <remarks>https://numpy.org/doc/stable/reference/generated/numpy.broadcast_to.html</remarks>
public static NDArray broadcast_to(UnmanagedStorage from, Shape against)
{
ValidateBroadcastTo(from.Shape, against);
return new NDArray(UnmanagedStorage.CreateBroadcastedUnsafe(from, Shape.Broadcast(from.Shape, against).LeftShape.WithFlags(flagsToClear: ArrayFlags.WRITEABLE)));
}
/// <summary>
/// Broadcast an array to a new shape.
/// </summary>
/// <param name="from">The NDArray to broadcast.</param>
/// <param name="against">The shape to broadcast against.</param>
/// <returns>These arrays are views on the original arrays. They are typically not contiguous. Furthermore, more than one element of a broadcasted array may refer to a single memory location. If you need to write to the arrays, make copies first.</returns>
/// <remarks>https://numpy.org/doc/stable/reference/generated/numpy.broadcast_to.html</remarks>
public static NDArray broadcast_to(NDArray from, Shape against)
{
ValidateBroadcastTo(from.Shape, against);
return new NDArray(UnmanagedStorage.CreateBroadcastedUnsafe(from.Storage, Shape.Broadcast(from.Shape, against).LeftShape.WithFlags(flagsToClear: ArrayFlags.WRITEABLE)));
}
/// <summary>
/// Broadcast an shape against an other new shape.
/// </summary>
/// <param name="from">The shape that is to be broadcasted</param>
/// <param name="against">The shape that'll be used to broadcast <paramref name="from"/> shape</param>
/// <returns>A readonly view on the original array with the given shape. It is typically not contiguous. Furthermore, more than one element of a broadcasted array may refer to a single memory location.</returns>
/// <remarks>https://numpy.org/doc/stable/reference/generated/numpy.broadcast_to.html</remarks>
public static Shape broadcast_to(Shape from, NDArray against)
{
ValidateBroadcastTo(from, against.Shape);
return Shape.Broadcast(from, against.Shape).LeftShape;
}
/// <summary>
/// Broadcast an array to a new shape.
/// </summary>
/// <param name="from">The UnmanagedStorage to broadcast.</param>
/// <param name="against">The shape to broadcast against.</param>
/// <returns>These arrays are views on the original arrays. They are typically not contiguous. Furthermore, more than one element of a broadcasted array may refer to a single memory location. If you need to write to the arrays, make copies first.</returns>
/// <remarks>https://numpy.org/doc/stable/reference/generated/numpy.broadcast_to.html</remarks>
public static NDArray broadcast_to(UnmanagedStorage from, NDArray against)
{
ValidateBroadcastTo(from.Shape, against.Shape);
return new NDArray(UnmanagedStorage.CreateBroadcastedUnsafe(from, Shape.Broadcast(from.Shape, against.Shape).LeftShape.WithFlags(flagsToClear: ArrayFlags.WRITEABLE)));
}
/// <summary>
/// Broadcast an array to a new shape.
/// </summary>
/// <param name="from">The NDArray to broadcast.</param>
/// <param name="against">The shape to broadcast against.</param>
/// <returns>These arrays are views on the original arrays. They are typically not contiguous. Furthermore, more than one element of a broadcasted array may refer to a single memory location. If you need to write to the arrays, make copies first.</returns>
/// <remarks>https://numpy.org/doc/stable/reference/generated/numpy.broadcast_to.html</remarks>
public static NDArray broadcast_to(NDArray from, NDArray against)
{
ValidateBroadcastTo(from.Shape, against.Shape);
return new NDArray(UnmanagedStorage.CreateBroadcastedUnsafe(from.Storage, Shape.Broadcast(from.Shape, against.Shape).LeftShape.WithFlags(flagsToClear: ArrayFlags.WRITEABLE)));
}
/// <summary>
/// Broadcast an shape against an other new shape.
/// </summary>
/// <param name="from">The shape that is to be broadcasted</param>
/// <param name="against">The shape that'll be used to broadcast <paramref name="from"/> shape</param>
/// <returns>A readonly view on the original array with the given shape. It is typically not contiguous. Furthermore, more than one element of a broadcasted array may refer to a single memory location.</returns>
/// <remarks>https://numpy.org/doc/stable/reference/generated/numpy.broadcast_to.html</remarks>
public static Shape broadcast_to(Shape from, UnmanagedStorage against)
{
ValidateBroadcastTo(from, against.Shape);
return Shape.Broadcast(from, against.Shape).LeftShape;
}
/// <summary>
/// Broadcast an array to a new shape.
/// </summary>
/// <param name="from">The UnmanagedStorage to broadcast.</param>
/// <param name="against">The shape to broadcast against.</param>
/// <returns>These arrays are views on the original arrays. They are typically not contiguous. Furthermore, more than one element of a broadcasted array may refer to a single memory location. If you need to write to the arrays, make copies first.</returns>
/// <remarks>https://numpy.org/doc/stable/reference/generated/numpy.broadcast_to.html</remarks>
public static NDArray broadcast_to(UnmanagedStorage from, UnmanagedStorage against)
{
ValidateBroadcastTo(from.Shape, against.Shape);
return new NDArray(UnmanagedStorage.CreateBroadcastedUnsafe(from, Shape.Broadcast(from.Shape, against.Shape).LeftShape.WithFlags(flagsToClear: ArrayFlags.WRITEABLE)));
}
/// <summary>
/// Broadcast an array to a new shape.
/// </summary>
/// <param name="from">The NDArray to broadcast.</param>
/// <param name="against">The shape to broadcast against.</param>
/// <returns>These arrays are views on the original arrays. They are typically not contiguous. Furthermore, more than one element of a broadcasted array may refer to a single memory location. If you need to write to the arrays, make copies first.</returns>
/// <remarks>https://numpy.org/doc/stable/reference/generated/numpy.broadcast_to.html</remarks>
public static NDArray broadcast_to(NDArray from, UnmanagedStorage against)
{
ValidateBroadcastTo(from.Shape, against.Shape);
return new NDArray(UnmanagedStorage.CreateBroadcastedUnsafe(from.Storage, Shape.Broadcast(from.Shape, against.Shape).LeftShape.WithFlags(flagsToClear: ArrayFlags.WRITEABLE)));
}
}
}