Skip to content

Commit 037871b

Browse files
committed
Dispose Scalars implicitly created in Tensor operators
1 parent 3701fc5 commit 037871b

File tree

1 file changed

+184
-41
lines changed

1 file changed

+184
-41
lines changed

src/TorchSharp/Tensor/Tensor.Operators.cs

+184-41
Original file line numberDiff line numberDiff line change
@@ -12,66 +12,209 @@ public partial class Tensor
1212
public static Tensor operator +(Tensor left, Scalar right) => left.add(right);
1313
public static Tensor operator +(Scalar left, Tensor right) => right.add(left);
1414

15-
public static Tensor operator +(Tensor left, int right) => left.add(right);
16-
public static Tensor operator +(Tensor left, long right) => left.add(right);
17-
public static Tensor operator +(Tensor left, float right) => left.add(right);
18-
public static Tensor operator +(Tensor left, double right) => left.add(right);
19-
20-
public static Tensor operator +(int left, Tensor right) => right.add(left);
21-
public static Tensor operator +(long left, Tensor right) => right.add(left);
22-
public static Tensor operator +(float left, Tensor right) => right.add(left);
23-
public static Tensor operator +(double left, Tensor right) => right.add(left);
15+
public static Tensor operator +(Tensor left, int right)
16+
{
17+
using Scalar scalar = right;
18+
return left.add(scalar);
19+
}
20+
public static Tensor operator +(Tensor left, long right)
21+
{
22+
using Scalar scalar = right;
23+
return left.add(scalar);
24+
}
25+
public static Tensor operator +(Tensor left, float right)
26+
{
27+
using Scalar scalar = right;
28+
return left.add(scalar);
29+
}
30+
public static Tensor operator +(Tensor left, double right)
31+
{
32+
using Scalar scalar = right;
33+
return left.add(scalar);
34+
}
35+
36+
public static Tensor operator +(int left, Tensor right)
37+
{
38+
using Scalar scalar = left;
39+
return right.add(scalar);
40+
}
41+
public static Tensor operator +(long left, Tensor right)
42+
{
43+
using Scalar scalar = left;
44+
return right.add(scalar);
45+
}
46+
public static Tensor operator +(float left, Tensor right)
47+
{
48+
using Scalar scalar = left;
49+
return right.add(scalar);
50+
}
51+
public static Tensor operator +(double left, Tensor right)
52+
{
53+
using Scalar scalar = left;
54+
return right.add(scalar);
55+
}
2456

2557
public static Tensor operator *(Tensor left, Tensor right) => left.mul(right);
2658
public static Tensor operator *(Tensor left, Scalar right) => left.mul(right);
2759
public static Tensor operator *(Scalar left, Tensor right) => right.mul(left);
2860

29-
public static Tensor operator *(Tensor left, int right) => left.mul(right);
30-
public static Tensor operator *(Tensor left, long right) => left.mul(right);
31-
public static Tensor operator *(Tensor left, float right) => left.mul(right);
32-
public static Tensor operator *(Tensor left, double right) => left.mul(right);
33-
34-
public static Tensor operator *(int left, Tensor right) => right.mul(left);
35-
public static Tensor operator *(long left, Tensor right) => right.mul(left);
36-
public static Tensor operator *(float left, Tensor right) => right.mul(left);
37-
public static Tensor operator *(double left, Tensor right) => right.mul(left);
61+
public static Tensor operator *(Tensor left, int right)
62+
{
63+
using Scalar scalar = right;
64+
return left.mul(scalar);
65+
}
66+
public static Tensor operator *(Tensor left, long right)
67+
{
68+
using Scalar scalar = right;
69+
return left.mul(scalar);
70+
}
71+
public static Tensor operator *(Tensor left, float right)
72+
{
73+
using Scalar scalar = right;
74+
return left.mul(scalar);
75+
}
76+
public static Tensor operator *(Tensor left, double right)
77+
{
78+
using Scalar scalar = right;
79+
return left.mul(scalar);
80+
}
81+
82+
public static Tensor operator *(int left, Tensor right)
83+
{
84+
using Scalar scalar = left;
85+
return right.mul(scalar);
86+
}
87+
public static Tensor operator *(long left, Tensor right)
88+
{
89+
using Scalar scalar = left;
90+
return right.mul(scalar);
91+
}
92+
public static Tensor operator *(float left, Tensor right)
93+
{
94+
using Scalar scalar = left;
95+
return right.mul(scalar);
96+
}
97+
public static Tensor operator *(double left, Tensor right)
98+
{
99+
using Scalar scalar = left;
100+
return right.mul(scalar);
101+
}
38102

39103
public static Tensor operator -(Tensor left, Tensor right) => left.sub(right);
40104
public static Tensor operator -(Tensor left, Scalar right) => left.sub(right);
41105
public static Tensor operator -(Scalar left, Tensor right) => right.negative().add(left);
42106

43-
public static Tensor operator -(Tensor left, int right) => left.sub(right);
44-
public static Tensor operator -(Tensor left, long right) => left.sub(right);
45-
public static Tensor operator -(Tensor left, float right) => left.sub(right);
46-
public static Tensor operator -(Tensor left, double right) => left.sub(right);
47-
48-
public static Tensor operator -(int left, Tensor right) => right.negative().add(left);
49-
public static Tensor operator -(long left, Tensor right) => right.negative().add(left);
50-
public static Tensor operator -(float left, Tensor right) => right.negative().add(left);
51-
public static Tensor operator -(double left, Tensor right) => right.negative().add(left);
107+
public static Tensor operator -(Tensor left, int right)
108+
{
109+
using Scalar scalar = right;
110+
return left.sub(scalar);
111+
}
112+
public static Tensor operator -(Tensor left, long right)
113+
{
114+
using Scalar scalar = right;
115+
return left.sub(scalar);
116+
}
117+
public static Tensor operator -(Tensor left, float right)
118+
{
119+
using Scalar scalar = right;
120+
return left.sub(scalar);
121+
}
122+
public static Tensor operator -(Tensor left, double right)
123+
{
124+
using Scalar scalar = right;
125+
return left.sub(scalar);
126+
}
127+
128+
public static Tensor operator -(int left, Tensor right)
129+
{
130+
using Scalar scalar = left;
131+
return right.negative().add(scalar);
132+
}
133+
public static Tensor operator -(long left, Tensor right)
134+
{
135+
using Scalar scalar = left;
136+
return right.negative().add(scalar);
137+
}
138+
public static Tensor operator -(float left, Tensor right)
139+
{
140+
using Scalar scalar = left;
141+
return right.negative().add(scalar);
142+
}
143+
public static Tensor operator -(double left, Tensor right)
144+
{
145+
using Scalar scalar = left;
146+
return right.negative().add(scalar);
147+
}
52148

53149
public static Tensor operator /(Tensor left, Tensor right) => left.div(right);
54150
public static Tensor operator /(Tensor left, Scalar right) => left.div(right);
55151
public static Tensor operator /(Scalar left, Tensor right) => right.reciprocal().mul(left);
56152

57-
public static Tensor operator /(Tensor left, int right) => left.div(right);
58-
public static Tensor operator /(Tensor left, long right) => left.div(right);
59-
public static Tensor operator /(Tensor left, float right) => left.div(right);
60-
public static Tensor operator /(Tensor left, double right) => left.div(right);
61-
62-
public static Tensor operator /(int left, Tensor right) => right.reciprocal().mul(left);
63-
public static Tensor operator /(long left, Tensor right) => right.reciprocal().mul(left);
64-
public static Tensor operator /(float left, Tensor right) => right.reciprocal().mul(left);
65-
public static Tensor operator /(double left, Tensor right) => right.reciprocal().mul(left);
66-
153+
public static Tensor operator /(Tensor left, int right)
154+
{
155+
using Scalar scalar = right;
156+
return left.div(scalar);
157+
}
158+
public static Tensor operator /(Tensor left, long right)
159+
{
160+
using Scalar scalar = right;
161+
return left.div(scalar);
162+
}
163+
public static Tensor operator /(Tensor left, float right)
164+
{
165+
using Scalar scalar = right;
166+
return left.div(scalar);
167+
}
168+
public static Tensor operator /(Tensor left, double right)
169+
{
170+
using Scalar scalar = right;
171+
return left.div(scalar);
172+
}
173+
174+
public static Tensor operator /(int left, Tensor right)
175+
{
176+
using Scalar scalar = left;
177+
return right.reciprocal().mul(scalar);
178+
}
179+
public static Tensor operator /(long left, Tensor right)
180+
{
181+
using Scalar scalar = left;
182+
return right.reciprocal().mul(scalar);
183+
}
184+
public static Tensor operator /(float left, Tensor right)
185+
{
186+
using Scalar scalar = left;
187+
return right.reciprocal().mul(scalar);
188+
}
189+
public static Tensor operator /(double left, Tensor right)
190+
{
191+
using Scalar scalar = left;
192+
return right.reciprocal().mul(scalar);
193+
}
67194

68195
public static Tensor operator %(Tensor left, Tensor right) => left.remainder(right);
69196
public static Tensor operator %(Tensor left, Scalar right) => left.remainder(right);
70197

71-
public static Tensor operator %(Tensor left, int right) => left.remainder(right);
72-
public static Tensor operator %(Tensor left, long right) => left.remainder(right);
73-
public static Tensor operator %(Tensor left, float right) => left.remainder(right);
74-
public static Tensor operator %(Tensor left, double right) => left.remainder(right);
198+
public static Tensor operator %(Tensor left, int right)
199+
{
200+
using Scalar scalar = right;
201+
return left.remainder(scalar);
202+
}
203+
public static Tensor operator %(Tensor left, long right)
204+
{
205+
using Scalar scalar = right;
206+
return left.remainder(scalar);
207+
}
208+
public static Tensor operator %(Tensor left, float right)
209+
{
210+
using Scalar scalar = right;
211+
return left.remainder(scalar);
212+
}
213+
public static Tensor operator %(Tensor left, double right)
214+
{
215+
using Scalar scalar = right;
216+
return left.remainder(scalar);
217+
}
75218

76219
public static Tensor operator &(Tensor left, Tensor right) => left.bitwise_and(right);
77220

0 commit comments

Comments
 (0)