| | | 1 | | //======================================================================= |
| | | 2 | | // WideWeightedAverage.cs |
| | | 3 | | //======================================================================= |
| | | 4 | | // MIT License, Copyright (c) 2024-present David Oravsky (mrdav30) |
| | | 5 | | // See LICENSE file in the project root for full license information. |
| | | 6 | | //======================================================================= |
| | | 7 | | |
| | | 8 | | using System; |
| | | 9 | | |
| | | 10 | | namespace FixedMathSharp; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Provides methods for computing the weighted average of two-dimensional and |
| | | 14 | | /// three-dimensional vectors using wide arithmetic to avoid overflow and maintain precision. |
| | | 15 | | /// </summary> |
| | | 16 | | internal static class WideWeightedAverage |
| | | 17 | | { |
| | | 18 | | // A maximum-length span needs at most 159 signed bits for weighted |
| | | 19 | | // component products and 95 for the total Q32.32 weight. |
| | | 20 | | |
| | | 21 | | internal static Vector2d GetAverage(ReadOnlySpan<Vector2d> values) |
| | | 22 | | { |
| | 102 | 23 | | Signed192 totalX = default; |
| | 102 | 24 | | Signed192 totalY = default; |
| | 616 | 25 | | for (int i = 0; i < values.Length; i++) |
| | | 26 | | { |
| | 206 | 27 | | totalX = WideArithmetic.AddSigned192( |
| | 206 | 28 | | totalX, |
| | 206 | 29 | | Signed192.Raw(values[i].X)); |
| | 206 | 30 | | totalY = WideArithmetic.AddSigned192( |
| | 206 | 31 | | totalY, |
| | 206 | 32 | | Signed192.Raw(values[i].Y)); |
| | | 33 | | } |
| | | 34 | | |
| | 102 | 35 | | Signed576 denominator = Signed576.ExtendValue( |
| | 102 | 36 | | Signed320.ExtendValue( |
| | 102 | 37 | | Signed192.Signed(values.Length))); |
| | 102 | 38 | | return new Vector2d( |
| | 102 | 39 | | GetComponent(totalX, denominator), |
| | 102 | 40 | | GetComponent(totalY, denominator)); |
| | | 41 | | } |
| | | 42 | | |
| | | 43 | | internal static bool TryGet( |
| | | 44 | | ReadOnlySpan<Vector2d> values, |
| | | 45 | | ReadOnlySpan<Fixed64> weights, |
| | | 46 | | out Vector2d average) |
| | | 47 | | { |
| | 3 | 48 | | Signed192 totalWeight = default; |
| | 3 | 49 | | Signed320 weightedX = default; |
| | 3 | 50 | | Signed320 weightedY = default; |
| | 16 | 51 | | for (int i = 0; i < values.Length; i++) |
| | | 52 | | { |
| | 5 | 53 | | Signed192 weight = Signed192.Raw(weights[i]); |
| | 5 | 54 | | totalWeight = WideArithmetic.AddSigned192( |
| | 5 | 55 | | totalWeight, |
| | 5 | 56 | | weight); |
| | 5 | 57 | | weightedX = WideArithmetic.AddSigned320( |
| | 5 | 58 | | weightedX, |
| | 5 | 59 | | WideArithmetic.MultiplySigned192( |
| | 5 | 60 | | Signed192.Raw(values[i].X), |
| | 5 | 61 | | weight)); |
| | 5 | 62 | | weightedY = WideArithmetic.AddSigned320( |
| | 5 | 63 | | weightedY, |
| | 5 | 64 | | WideArithmetic.MultiplySigned192( |
| | 5 | 65 | | Signed192.Raw(values[i].Y), |
| | 5 | 66 | | weight)); |
| | | 67 | | } |
| | | 68 | | |
| | 3 | 69 | | if (totalWeight.Sign == 0) |
| | | 70 | | { |
| | 1 | 71 | | average = default; |
| | 1 | 72 | | return false; |
| | | 73 | | } |
| | | 74 | | |
| | 2 | 75 | | Signed576 denominator = Signed576.ExtendValue( |
| | 2 | 76 | | Signed320.ExtendValue(totalWeight)); |
| | 2 | 77 | | Fixed64 x = GetComponent( |
| | 2 | 78 | | weightedX, |
| | 2 | 79 | | denominator); |
| | 2 | 80 | | Fixed64 y = GetComponent( |
| | 2 | 81 | | weightedY, |
| | 2 | 82 | | denominator); |
| | 2 | 83 | | average = new Vector2d(x, y); |
| | 2 | 84 | | return true; |
| | | 85 | | } |
| | | 86 | | |
| | | 87 | | internal static bool TryGet( |
| | | 88 | | ReadOnlySpan<Vector3d> values, |
| | | 89 | | ReadOnlySpan<Fixed64> weights, |
| | | 90 | | out Vector3d average) |
| | | 91 | | { |
| | 177 | 92 | | Signed192 totalWeight = default; |
| | 177 | 93 | | Signed320 weightedX = default; |
| | 177 | 94 | | Signed320 weightedY = default; |
| | 177 | 95 | | Signed320 weightedZ = default; |
| | 1058 | 96 | | for (int i = 0; i < values.Length; i++) |
| | | 97 | | { |
| | 352 | 98 | | Signed192 weight = Signed192.Raw(weights[i]); |
| | 352 | 99 | | totalWeight = WideArithmetic.AddSigned192( |
| | 352 | 100 | | totalWeight, |
| | 352 | 101 | | weight); |
| | 352 | 102 | | weightedX = WideArithmetic.AddSigned320( |
| | 352 | 103 | | weightedX, |
| | 352 | 104 | | WideArithmetic.MultiplySigned192( |
| | 352 | 105 | | Signed192.Raw(values[i].X), |
| | 352 | 106 | | weight)); |
| | 352 | 107 | | weightedY = WideArithmetic.AddSigned320( |
| | 352 | 108 | | weightedY, |
| | 352 | 109 | | WideArithmetic.MultiplySigned192( |
| | 352 | 110 | | Signed192.Raw(values[i].Y), |
| | 352 | 111 | | weight)); |
| | 352 | 112 | | weightedZ = WideArithmetic.AddSigned320( |
| | 352 | 113 | | weightedZ, |
| | 352 | 114 | | WideArithmetic.MultiplySigned192( |
| | 352 | 115 | | Signed192.Raw(values[i].Z), |
| | 352 | 116 | | weight)); |
| | | 117 | | } |
| | | 118 | | |
| | 177 | 119 | | if (totalWeight.Sign == 0) |
| | | 120 | | { |
| | 1 | 121 | | average = default; |
| | 1 | 122 | | return false; |
| | | 123 | | } |
| | | 124 | | |
| | 176 | 125 | | Signed576 denominator = Signed576.ExtendValue( |
| | 176 | 126 | | Signed320.ExtendValue(totalWeight)); |
| | 176 | 127 | | Fixed64 x = GetComponent( |
| | 176 | 128 | | weightedX, |
| | 176 | 129 | | denominator); |
| | 176 | 130 | | Fixed64 y = GetComponent( |
| | 176 | 131 | | weightedY, |
| | 176 | 132 | | denominator); |
| | 176 | 133 | | Fixed64 z = GetComponent( |
| | 176 | 134 | | weightedZ, |
| | 176 | 135 | | denominator); |
| | 176 | 136 | | average = new Vector3d(x, y, z); |
| | 176 | 137 | | return true; |
| | | 138 | | } |
| | | 139 | | |
| | | 140 | | private static Fixed64 GetComponent( |
| | | 141 | | Signed320 numerator, |
| | | 142 | | Signed576 denominator) |
| | | 143 | | { |
| | | 144 | | // A non-negative weighted average is bounded by its representable |
| | | 145 | | // inputs, so only the generic ratio helper's representable result is |
| | | 146 | | // reachable here. |
| | 532 | 147 | | Fixed64.TryGetSignedRawRatio( |
| | 532 | 148 | | Signed576.ExtendValue(numerator), |
| | 532 | 149 | | denominator, |
| | 532 | 150 | | out Fixed64 component); |
| | 532 | 151 | | return component; |
| | | 152 | | } |
| | | 153 | | |
| | | 154 | | private static Fixed64 GetComponent( |
| | | 155 | | Signed192 numerator, |
| | | 156 | | Signed576 denominator) |
| | | 157 | | { |
| | | 158 | | // An arithmetic mean is bounded by its representable inputs. |
| | 204 | 159 | | Fixed64.TryGetSignedRawRatio( |
| | 204 | 160 | | Signed576.ExtendValue( |
| | 204 | 161 | | Signed320.ExtendValue(numerator)), |
| | 204 | 162 | | denominator, |
| | 204 | 163 | | out Fixed64 component); |
| | 204 | 164 | | return component; |
| | | 165 | | } |
| | | 166 | | |
| | | 167 | | internal static void ValidateInputs( |
| | | 168 | | int valueCount, |
| | | 169 | | ReadOnlySpan<Fixed64> weights) |
| | | 170 | | { |
| | 184 | 171 | | if (valueCount != weights.Length) |
| | | 172 | | { |
| | 2 | 173 | | throw new ArgumentException( |
| | 2 | 174 | | "Values and weights must have the same length.", |
| | 2 | 175 | | nameof(weights)); |
| | | 176 | | } |
| | | 177 | | |
| | 1078 | 178 | | for (int i = 0; i < weights.Length; i++) |
| | | 179 | | { |
| | 359 | 180 | | if (weights[i] < Fixed64.Zero) |
| | | 181 | | { |
| | 2 | 182 | | throw new ArgumentOutOfRangeException( |
| | 2 | 183 | | nameof(weights), |
| | 2 | 184 | | "Weights cannot be negative."); |
| | | 185 | | } |
| | | 186 | | } |
| | 180 | 187 | | } |
| | | 188 | | } |