Skip to content

Commit 9edeb81

Browse files
edfink234ferdymercury
edfink234
authored andcommitted
Adding changed suggested by ferdymercury
1 parent 1a2bbfd commit 9edeb81

File tree

2 files changed

+16
-33
lines changed

2 files changed

+16
-33
lines changed

math/vecops/inc/ROOT/RVec.hxx

+11-10
Original file line numberDiff line numberDiff line change
@@ -3263,14 +3263,14 @@ RVec<typename RVec<T>::size_type> Enumerate(const RVec<T> &v)
32633263
*
32643264
* \param start The first value in the sequence.
32653265
* \param end The last value in the sequence if \p endpoint is true; otherwise, \p end is excluded.
3266-
* \param n The number of evenly spaced entries to produce.
3266+
* \param n The number of evenly spaced entries to produce. The default value is 128, which is different than numpy's default value of 50.
32673267
* \param endpoint If true (default), \p end is included as the final element; if false, \p end is excluded.
32683268
*
32693269
* \return A vector (RVec<Common_t>) containing \p n evenly spaced values.
32703270
*
32713271
* \note If \p n is 1, the resulting vector will contain only the value \p start.
3272-
* \note The check `if (!n || (n > std::numeric_limits<long long>::max()))` is used to ensure that:
3273-
* - n is nonzero, and
3272+
* \note The check `if (!n || (endpoint && n == 1) || (n > std::numeric_limits<long long>::max()))` is used to ensure that:
3273+
* - division by zero is avoided when calculating `step`
32743274
* - n does not exceed std::numeric_limits<long long>::max(), which would indicate that a negative range (or other arithmetic issue)
32753275
* has resulted in an extremely large unsigned value, thereby preventing an attempt to reserve an absurd
32763276
* amount of memory.
@@ -3303,12 +3303,13 @@ inline RVec<Common_t> Linspace(T1 start, T2 end, unsigned long long n = 128, con
33033303
{
33043304
RVec<Common_t> temp;
33053305

3306-
if (!n || (n > std::numeric_limits<long long>::max())) // Check for invalid or absurd n.
3306+
if (!n || (endpoint && n == 1) || (n > std::numeric_limits<long long>::max())) // Check for invalid or absurd n.
33073307
{
33083308
return temp;
33093309
}
33103310

33113311
Common_t step = (static_cast<Common_t>(end) - static_cast<Common_t>(start)) / static_cast<Common_t>(n - endpoint);
3312+
33123313
temp.reserve(n);
33133314
temp.push_back(static_cast<Common_t>(start));
33143315
for (unsigned long long i = 1; i < n; i++)
@@ -3337,15 +3338,15 @@ inline RVec<Common_t> Linspace(T1 start, T2 end, unsigned long long n = 128, con
33373338
*
33383339
* \param start The exponent corresponding to the first element (i.e., the first element is \f$base^{start}\f$).
33393340
* \param end The exponent corresponding to the final element if \p endpoint is true; otherwise, \p end is excluded.
3340-
* \param n The number of log-spaced entries to produce.
3341-
* \param base The base to be used in the exponentiation (default is 10.0).
3341+
* \param n The number of log-spaced entries to produce. The default value is 128, which is different than numpy's default value of 50.
33423342
* \param endpoint If true (default), \f$base^{end}\f$ is included as the final element; if false, \f$base^{end}\f$ is excluded.
3343+
* \param base The base to be used in the exponentiation (default is 10.0).
33433344
*
33443345
* \return A vector (RVec<Common_t>) containing n log-spaced values.
33453346
*
33463347
* \note If \p n is 1, the resulting vector will contain only the value \f$base^{start}\f$.
3347-
* \note The check `if (!n || (n > std::numeric_limits<long long>::max()))` is used to ensure that:
3348-
* - n is nonzero, and
3348+
* \note The check `if (!n || (endpoint && n == 1) || (n > std::numeric_limits<long long>::max()))` is used to ensure that:
3349+
* - division by zero is avoided when calculating `step`
33493350
* - n does not exceed std::numeric_limits<long long>::max(), which would indicate that a negative range (or other arithmetic issue)
33503351
* has resulted in an extremely large unsigned value, thereby preventing an attempt to reserve an absurd
33513352
* amount of memory.
@@ -3376,11 +3377,11 @@ inline RVec<Common_t> Linspace(T1 start, T2 end, unsigned long long n = 128, con
33763377
* ~~~
33773378
*/
33783379
template <typename T1 = double, typename T2 = double, typename T3 = double, typename Common_t = std::conditional_t<std::is_floating_point_v<std::common_type_t<T1, T2, T3>>, std::common_type_t<T1, T2, T3>, double>>
3379-
inline RVec<Common_t> Logspace(T1 start, T2 end, unsigned long long n = 128, T3 base = 10.0, const bool endpoint = true)
3380+
inline RVec<Common_t> Logspace(T1 start, T2 end, unsigned long long n = 128, const bool endpoint = true, T3 base = 10.0)
33803381
{
33813382
RVec<Common_t> temp;
33823383

3383-
if (!n || (n > std::numeric_limits<long long>::max())) // Check for invalid or absurd n.
3384+
if (!n || (endpoint && n == 1) || (n > std::numeric_limits<long long>::max())) // Check for invalid or absurd n.
33843385
{
33853386
return temp;
33863387
}

math/vecops/test/vecops_rvec.cxx

+5-23
Original file line numberDiff line numberDiff line change
@@ -37,24 +37,6 @@ void CheckEqual(const ROOT::RVecD &a, const ROOT::RVecD &b, std::string_view msg
3737
}
3838
}
3939

40-
// Checks if all the elements in `v1` are at most `percentage` percent different
41-
// from `v2`
42-
void CheckNear(const RVecD &v1, const RVecD &v2, double percentage = 0.01)
43-
{
44-
ASSERT_EQ(v1.size(), v2.size());
45-
for (size_t i = 0; i < v1.size(); i++) {
46-
double expected = v2[i];
47-
double actual = v1[i];
48-
// Compute tolerance as percentage of expected value.
49-
double tol = std::abs(expected) * percentage;
50-
// Fallback: if expected is zero, use the percentage as an absolute tolerance.
51-
if (expected == 0.0) {
52-
tol = percentage;
53-
}
54-
ASSERT_NEAR(actual, expected, tol);
55-
}
56-
}
57-
5840
void CheckEqual(const ROOT::Math::PtEtaPhiMVector &a, const ROOT::Math::PtEtaPhiMVector &b) {
5941
EXPECT_DOUBLE_EQ(a.Pt(), b.Pt());
6042
EXPECT_DOUBLE_EQ(a.Eta(), b.Eta());
@@ -931,11 +913,11 @@ TEST(VecOps, Logspace)
931913
CheckNear(Logspace(-1, 10, 22), RVecD{ 0.1, 0.33404850, 1.1158840, 3.7275937, 12.451971, 41.595622, 138.94955, 464.15888, 1550.5158, 5179.4747, 17301.957, 57796.929, 193069.77, 644946.68, 2154434.7, 7196856.7, 24040992., 80308572., 2.6826958e+08, 8.9615050e+08, 2.9935773e+09, 1e+10 });
932914
CheckNear(Logspace(5, -3, 3), RVecD{ 100000, 10, 0.001 });
933915
CheckNear(Logspace(1.4, 3.66, 5), RVecD{ 25.118864, 92.257143, 338.84416, 1244.5146, 4570.8819 });
934-
CheckNear(Logspace(1.4, 13.66, 5, 2), RVecD{ 2.6390158, 22.085078, 184.82294, 1546.7239, 12944.037 });
935-
CheckNear(Logspace(5, 9, 200, 2), RVecD{ 32, 32.448964, 32.904227, 33.365877, 33.834004, 34.308699, 34.790054, 35.278163, 35.773119, 36.275020, 36.783963, 37.300047, 37.823371, 38.354037, 38.892149, 39.437810, 39.991127, 40.552207, 41.121160, 41.698094, 42.283124, 42.876361, 43.477921, 44.087921, 44.706480, 45.333717, 45.969755, 46.614716, 47.268726, 47.931912, 48.604402, 49.286327, 49.977820, 50.679015, 51.390048, 52.111056, 52.842180, 53.583562, 54.335346, 55.097677, 55.870704, 56.654577, 57.449447, 58.255470, 59.072801, 59.901599, 60.742026, 61.594243, 62.458418, 63.334717, 64.223310, 65.124371, 66.038074, 66.964596, 67.904117, 68.856819, 69.822889, 70.802512, 71.795880, 72.803184, 73.824622, 74.860390, 75.910690, 76.975726, 78.055704, 79.150835, 80.261330, 81.387406, 82.529281, 83.687177, 84.861318, 86.051932, 87.259251, 88.483508, 89.724942, 90.983794, 92.260307, 93.554730, 94.867314, 96.198314, 97.547987, 98.916597, 100.30441, 101.71169, 103.13872, 104.58577, 106.05312, 107.54105, 109.04987, 110.57985, 112.13130, 113.70451, 115.29980, 116.91747, 118.55784, 120.22122, 121.90794, 123.61832, 125.35270, 127.11141, 128.89480, 130.70321, 132.53699, 134.39650, 136.28210, 138.19415, 140.13303, 142.09912, 144.09278, 146.11442, 148.16442, 150.24319, 152.35112, 154.48862, 156.65612, 158.85402, 161.08276, 163.34277, 165.63449, 167.95836, 170.31484, 172.70437, 175.12744, 177.58449, 180.07603, 182.60251, 185.16445, 187.76233, 190.39665, 193.06794, 195.77671, 198.52348, 201.30879, 204.13317, 206.99718, 209.90138, 212.84632, 215.83258, 218.86074, 221.93138, 225.04510, 228.20251, 231.40422, 234.65085, 237.94303, 241.28139, 244.66660, 248.09930, 251.58016, 255.10986, 258.68909, 262.31852, 265.99888, 269.73088, 273.51524, 277.35269, 281.24398, 285.18986, 289.19111, 293.24850, 297.36281, 301.53484, 305.76541, 310.05534, 314.40545, 318.81659, 323.28963, 327.82542, 332.42485, 337.08881, 341.81821, 346.61395, 351.47699, 356.40825, 361.40870, 366.47931, 371.62106, 376.83494, 382.12198, 387.48320, 392.91963, 398.43234, 404.02240, 409.69088, 415.43889, 421.26755, 427.17798, 433.17134, 439.24878, 445.41149, 451.66067, 457.99752, 464.42328, 470.93919, 477.54653, 484.24656, 491.04060, 497.92995, 504.91597, 512 });
936-
CheckNear(Logspace(4, 10, 12, 10.0, false), RVecD{ 10000, 31622.8, 100000, 316228, 1e+06, 3.16228e+06, 1e+07, 3.16228e+07, 1e+08, 3.16228e+08, 1e+09, 3.16228e+09 });
937-
CheckNear(Logspace(5, 9, 200, 2, false), RVecD{ 32, 32.4467, 32.8996, 33.3589, 33.8246, 34.2968, 34.7755, 35.261, 35.7532, 36.2523, 36.7583, 37.2715, 37.7918, 38.3193, 38.8542, 39.3966, 39.9466, 40.5042, 41.0696, 41.6429, 42.2243, 42.8137, 43.4113, 44.0173, 44.6318, 45.2548, 45.8866, 46.5271, 47.1766, 47.8352, 48.5029, 49.18, 49.8665, 50.5626, 51.2685, 51.9842, 52.7098, 53.4456, 54.1917, 54.9482, 55.7152, 56.493, 57.2816, 58.0812, 58.892, 59.7141, 60.5477, 61.3929, 62.2499, 63.1189, 64, 64.8934, 65.7993, 66.7178, 67.6492, 68.5935, 69.551, 70.5219, 71.5064, 72.5046, 73.5167, 74.5429, 75.5835, 76.6386, 77.7085, 78.7932, 79.8932, 81.0084, 82.1393, 83.2859, 84.4485, 85.6274, 86.8227, 88.0347, 89.2636, 90.5097, 91.7731, 93.0542, 94.3532, 95.6704, 97.0059, 98.36, 99.7331, 101.125, 102.537, 103.968, 105.42, 106.891, 108.383, 109.896, 111.43, 112.986, 114.563, 116.162, 117.784, 119.428, 121.095, 122.786, 124.5, 126.238, 128, 129.787, 131.599, 133.436, 135.298, 137.187, 139.102, 141.044, 143.013, 145.009, 147.033, 149.086, 151.167, 153.277, 155.417, 157.586, 159.786, 162.017, 164.279, 166.572, 168.897, 171.255, 173.645, 176.069, 178.527, 181.019, 183.546, 186.108, 188.706, 191.341, 194.012, 196.72, 199.466, 202.251, 205.074, 207.937, 210.839, 213.783, 216.767, 219.793, 222.861, 225.972, 229.126, 232.325, 235.568, 238.856, 242.191, 245.572, 249, 252.476, 256, 259.574, 263.197, 266.871, 270.597, 274.374, 278.204, 282.088, 286.026, 290.018, 294.067, 298.172, 302.334, 306.555, 310.834, 315.173, 319.573, 324.034, 328.557, 333.144, 337.794, 342.509, 347.291, 352.139, 357.054, 362.039, 367.093, 372.217, 377.413, 382.681, 388.023, 393.44, 398.932, 404.501, 410.148, 415.873, 421.679, 427.565, 433.534, 439.586, 445.722, 451.944, 458.253, 464.65, 471.136, 477.713, 484.382, 491.143, 497.999, 504.951 });
938-
CheckNear(Logspace(1.4, 13.66, 1, 2, false), RVecD{ 2.63902 });
916+
CheckNear(Logspace(1.4, 13.66, 5, true, 2), RVecD{ 2.6390158, 22.085078, 184.82294, 1546.7239, 12944.037 });
917+
CheckNear(Logspace(5, 9, 200, true, 2), RVecD{ 32, 32.448964, 32.904227, 33.365877, 33.834004, 34.308699, 34.790054, 35.278163, 35.773119, 36.275020, 36.783963, 37.300047, 37.823371, 38.354037, 38.892149, 39.437810, 39.991127, 40.552207, 41.121160, 41.698094, 42.283124, 42.876361, 43.477921, 44.087921, 44.706480, 45.333717, 45.969755, 46.614716, 47.268726, 47.931912, 48.604402, 49.286327, 49.977820, 50.679015, 51.390048, 52.111056, 52.842180, 53.583562, 54.335346, 55.097677, 55.870704, 56.654577, 57.449447, 58.255470, 59.072801, 59.901599, 60.742026, 61.594243, 62.458418, 63.334717, 64.223310, 65.124371, 66.038074, 66.964596, 67.904117, 68.856819, 69.822889, 70.802512, 71.795880, 72.803184, 73.824622, 74.860390, 75.910690, 76.975726, 78.055704, 79.150835, 80.261330, 81.387406, 82.529281, 83.687177, 84.861318, 86.051932, 87.259251, 88.483508, 89.724942, 90.983794, 92.260307, 93.554730, 94.867314, 96.198314, 97.547987, 98.916597, 100.30441, 101.71169, 103.13872, 104.58577, 106.05312, 107.54105, 109.04987, 110.57985, 112.13130, 113.70451, 115.29980, 116.91747, 118.55784, 120.22122, 121.90794, 123.61832, 125.35270, 127.11141, 128.89480, 130.70321, 132.53699, 134.39650, 136.28210, 138.19415, 140.13303, 142.09912, 144.09278, 146.11442, 148.16442, 150.24319, 152.35112, 154.48862, 156.65612, 158.85402, 161.08276, 163.34277, 165.63449, 167.95836, 170.31484, 172.70437, 175.12744, 177.58449, 180.07603, 182.60251, 185.16445, 187.76233, 190.39665, 193.06794, 195.77671, 198.52348, 201.30879, 204.13317, 206.99718, 209.90138, 212.84632, 215.83258, 218.86074, 221.93138, 225.04510, 228.20251, 231.40422, 234.65085, 237.94303, 241.28139, 244.66660, 248.09930, 251.58016, 255.10986, 258.68909, 262.31852, 265.99888, 269.73088, 273.51524, 277.35269, 281.24398, 285.18986, 289.19111, 293.24850, 297.36281, 301.53484, 305.76541, 310.05534, 314.40545, 318.81659, 323.28963, 327.82542, 332.42485, 337.08881, 341.81821, 346.61395, 351.47699, 356.40825, 361.40870, 366.47931, 371.62106, 376.83494, 382.12198, 387.48320, 392.91963, 398.43234, 404.02240, 409.69088, 415.43889, 421.26755, 427.17798, 433.17134, 439.24878, 445.41149, 451.66067, 457.99752, 464.42328, 470.93919, 477.54653, 484.24656, 491.04060, 497.92995, 504.91597, 512 });
918+
CheckNear(Logspace(4, 10, 12, false, 10.0), RVecD{ 10000, 31622.8, 100000, 316228, 1e+06, 3.16228e+06, 1e+07, 3.16228e+07, 1e+08, 3.16228e+08, 1e+09, 3.16228e+09 });
919+
CheckNear(Logspace(5, 9, 200, false, 2), RVecD{ 32, 32.4467, 32.8996, 33.3589, 33.8246, 34.2968, 34.7755, 35.261, 35.7532, 36.2523, 36.7583, 37.2715, 37.7918, 38.3193, 38.8542, 39.3966, 39.9466, 40.5042, 41.0696, 41.6429, 42.2243, 42.8137, 43.4113, 44.0173, 44.6318, 45.2548, 45.8866, 46.5271, 47.1766, 47.8352, 48.5029, 49.18, 49.8665, 50.5626, 51.2685, 51.9842, 52.7098, 53.4456, 54.1917, 54.9482, 55.7152, 56.493, 57.2816, 58.0812, 58.892, 59.7141, 60.5477, 61.3929, 62.2499, 63.1189, 64, 64.8934, 65.7993, 66.7178, 67.6492, 68.5935, 69.551, 70.5219, 71.5064, 72.5046, 73.5167, 74.5429, 75.5835, 76.6386, 77.7085, 78.7932, 79.8932, 81.0084, 82.1393, 83.2859, 84.4485, 85.6274, 86.8227, 88.0347, 89.2636, 90.5097, 91.7731, 93.0542, 94.3532, 95.6704, 97.0059, 98.36, 99.7331, 101.125, 102.537, 103.968, 105.42, 106.891, 108.383, 109.896, 111.43, 112.986, 114.563, 116.162, 117.784, 119.428, 121.095, 122.786, 124.5, 126.238, 128, 129.787, 131.599, 133.436, 135.298, 137.187, 139.102, 141.044, 143.013, 145.009, 147.033, 149.086, 151.167, 153.277, 155.417, 157.586, 159.786, 162.017, 164.279, 166.572, 168.897, 171.255, 173.645, 176.069, 178.527, 181.019, 183.546, 186.108, 188.706, 191.341, 194.012, 196.72, 199.466, 202.251, 205.074, 207.937, 210.839, 213.783, 216.767, 219.793, 222.861, 225.972, 229.126, 232.325, 235.568, 238.856, 242.191, 245.572, 249, 252.476, 256, 259.574, 263.197, 266.871, 270.597, 274.374, 278.204, 282.088, 286.026, 290.018, 294.067, 298.172, 302.334, 306.555, 310.834, 315.173, 319.573, 324.034, 328.557, 333.144, 337.794, 342.509, 347.291, 352.139, 357.054, 362.039, 367.093, 372.217, 377.413, 382.681, 388.023, 393.44, 398.932, 404.501, 410.148, 415.873, 421.679, 427.565, 433.534, 439.586, 445.722, 451.944, 458.253, 464.65, 471.136, 477.713, 484.382, 491.143, 497.999, 504.951 });
920+
CheckNear(Logspace(1.4, 13.66, 1, false, 2), RVecD{ 2.63902 });
939921
CheckEqual(Logspace<int, int, int, int>(1, 5, 3), RVecI{ 10, 1000, 100000 });
940922
}
941923

0 commit comments

Comments
 (0)