Module: DecisionAgent::Dsl::Operators::MathematicalOperators
- Defined in:
- lib/decision_agent/dsl/operators/mathematical_operators.rb
Overview
Handles mathematical operators: trigonometric, exponential, logarithmic, rounding, etc.
Class Method Summary collapse
- .handle(op, actual_value, expected_value, param_cache: nil, param_cache_mutex: nil) ⇒ Object
-
.parse_atan2_params(value, param_cache: nil, param_cache_mutex: nil) ⇒ Object
Parse atan2 parameters.
- .parse_atan2_params_impl(value) ⇒ Object
-
.parse_gcd_lcm_params(value, param_cache: nil, param_cache_mutex: nil) ⇒ Object
Parse gcd/lcm parameters.
- .parse_gcd_lcm_params_impl(value) ⇒ Object
-
.parse_power_params(value, param_cache: nil, param_cache_mutex: nil) ⇒ Object
Parse power parameters.
- .parse_power_params_impl(value) ⇒ Object
Class Method Details
.handle(op, actual_value, expected_value, param_cache: nil, param_cache_mutex: nil) ⇒ Object
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 |
# File 'lib/decision_agent/dsl/operators/mathematical_operators.rb', line 8 def self.handle(op, actual_value, expected_value, param_cache: nil, param_cache_mutex: nil) case op # Trigonometric functions when "sin" return false unless actual_value.is_a?(Numeric) && expected_value.is_a?(Numeric) Base.epsilon_equal?(Math.sin(actual_value), expected_value) when "cos" return false unless actual_value.is_a?(Numeric) && expected_value.is_a?(Numeric) Base.epsilon_equal?(Math.cos(actual_value), expected_value) when "tan" return false unless actual_value.is_a?(Numeric) && expected_value.is_a?(Numeric) Base.epsilon_equal?(Math.tan(actual_value), expected_value) when "asin" return false unless actual_value.is_a?(Numeric) && expected_value.is_a?(Numeric) return false if actual_value < -1 || actual_value > 1 Base.epsilon_equal?(Math.asin(actual_value), expected_value) when "acos" return false unless actual_value.is_a?(Numeric) && expected_value.is_a?(Numeric) return false if actual_value < -1 || actual_value > 1 Base.epsilon_equal?(Math.acos(actual_value), expected_value) when "atan" return false unless actual_value.is_a?(Numeric) && expected_value.is_a?(Numeric) Base.epsilon_equal?(Math.atan(actual_value), expected_value) when "atan2" return false unless actual_value.is_a?(Numeric) params = parse_atan2_params(expected_value, param_cache: param_cache, param_cache_mutex: param_cache_mutex) return false unless params Base.epsilon_equal?(Math.atan2(actual_value, params[:y]), params[:result]) when "sinh" return false unless actual_value.is_a?(Numeric) && expected_value.is_a?(Numeric) Base.epsilon_equal?(Math.sinh(actual_value), expected_value) when "cosh" return false unless actual_value.is_a?(Numeric) && expected_value.is_a?(Numeric) Base.epsilon_equal?(Math.cosh(actual_value), expected_value) when "tanh" return false unless actual_value.is_a?(Numeric) && expected_value.is_a?(Numeric) Base.epsilon_equal?(Math.tanh(actual_value), expected_value) # Exponential and logarithmic functions when "sqrt" return false unless actual_value.is_a?(Numeric) && expected_value.is_a?(Numeric) return false if actual_value.negative? Base.epsilon_equal?(Math.sqrt(actual_value), expected_value) when "cbrt" return false unless actual_value.is_a?(Numeric) && expected_value.is_a?(Numeric) result = if actual_value.negative? -((-actual_value)**(1.0 / 3)) else actual_value**(1.0 / 3) end Base.epsilon_equal?(result, expected_value) when "power" return false unless actual_value.is_a?(Numeric) params = parse_power_params(expected_value, param_cache: param_cache, param_cache_mutex: param_cache_mutex) return false unless params Base.epsilon_equal?(actual_value**params[:exponent], params[:result]) when "exp" return false unless actual_value.is_a?(Numeric) && expected_value.is_a?(Numeric) Base.epsilon_equal?(Math.exp(actual_value), expected_value) when "log" return false unless actual_value.is_a?(Numeric) && expected_value.is_a?(Numeric) return false if actual_value <= 0 Base.epsilon_equal?(Math.log(actual_value), expected_value) when "log10" return false unless actual_value.is_a?(Numeric) && expected_value.is_a?(Numeric) return false if actual_value <= 0 Base.epsilon_equal?(Math.log10(actual_value), expected_value) when "log2" return false unless actual_value.is_a?(Numeric) && expected_value.is_a?(Numeric) return false if actual_value <= 0 Base.epsilon_equal?(Math.log(actual_value) / Math.log(2), expected_value) # Rounding and absolute value functions when "round" return false unless actual_value.is_a?(Numeric) && expected_value.is_a?(Numeric) actual_value.round == expected_value when "floor" return false unless actual_value.is_a?(Numeric) && expected_value.is_a?(Numeric) actual_value.floor == expected_value when "ceil" return false unless actual_value.is_a?(Numeric) && expected_value.is_a?(Numeric) actual_value.ceil == expected_value when "abs" return false unless actual_value.is_a?(Numeric) && expected_value.is_a?(Numeric) actual_value.abs == expected_value when "truncate" return false unless actual_value.is_a?(Numeric) && expected_value.is_a?(Numeric) actual_value.truncate == expected_value # Advanced mathematical functions when "factorial" return false unless actual_value.is_a?(Numeric) && expected_value.is_a?(Numeric) return false if actual_value.negative? || !actual_value.integer? (1..actual_value.to_i).reduce(1, :*) == expected_value when "gcd" return false unless actual_value.is_a?(Numeric) && actual_value.integer? params = parse_gcd_lcm_params(expected_value, param_cache: param_cache, param_cache_mutex: param_cache_mutex) return false unless params && params[:other].integer? actual_value.to_i.gcd(params[:other].to_i) == params[:result] when "lcm" return false unless actual_value.is_a?(Numeric) && actual_value.integer? params = parse_gcd_lcm_params(expected_value, param_cache: param_cache, param_cache_mutex: param_cache_mutex) return false unless params && params[:other].integer? actual_value.to_i.lcm(params[:other].to_i) == params[:result] end # Returns nil if not handled by this module end |
.parse_atan2_params(value, param_cache: nil, param_cache_mutex: nil) ⇒ Object
Parse atan2 parameters
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/decision_agent/dsl/operators/mathematical_operators.rb', line 146 def self.parse_atan2_params(value, param_cache: nil, param_cache_mutex: nil) normalized = Base.normalize_params_to_hash(value, %i[y result]) return nil unless normalized.is_a?(Hash) cache = param_cache mutex = param_cache_mutex if cache.nil? || mutex.nil? cache = ConditionEvaluator.instance_variable_get(:@param_cache) mutex = ConditionEvaluator.instance_variable_get(:@param_cache_mutex) end cache_key = Base.normalize_param_cache_key(normalized, "atan2") cached = cache[cache_key] return cached if cached mutex.synchronize do cache[cache_key] ||= parse_atan2_params_impl(normalized) end end |
.parse_atan2_params_impl(value) ⇒ Object
166 167 168 169 170 171 172 |
# File 'lib/decision_agent/dsl/operators/mathematical_operators.rb', line 166 def self.parse_atan2_params_impl(value) y = value[:y] || value["y"] result = value[:result] || value["result"] return nil unless y && !result.nil? { y: y, result: result } end |
.parse_gcd_lcm_params(value, param_cache: nil, param_cache_mutex: nil) ⇒ Object
Parse gcd/lcm parameters
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/decision_agent/dsl/operators/mathematical_operators.rb', line 204 def self.parse_gcd_lcm_params(value, param_cache: nil, param_cache_mutex: nil) normalized = Base.normalize_params_to_hash(value, %i[other result]) return nil unless normalized.is_a?(Hash) cache = param_cache mutex = param_cache_mutex if cache.nil? || mutex.nil? cache = ConditionEvaluator.instance_variable_get(:@param_cache) mutex = ConditionEvaluator.instance_variable_get(:@param_cache_mutex) end cache_key = Base.normalize_param_cache_key(normalized, "gcd_lcm") cached = cache[cache_key] return cached if cached mutex.synchronize do cache[cache_key] ||= parse_gcd_lcm_params_impl(normalized) end end |
.parse_gcd_lcm_params_impl(value) ⇒ Object
224 225 226 227 228 229 230 |
# File 'lib/decision_agent/dsl/operators/mathematical_operators.rb', line 224 def self.parse_gcd_lcm_params_impl(value) other = value[:other] || value["other"] result = value[:result] || value["result"] return nil unless other && !result.nil? { other: other, result: result } end |
.parse_power_params(value, param_cache: nil, param_cache_mutex: nil) ⇒ Object
Parse power parameters
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/decision_agent/dsl/operators/mathematical_operators.rb', line 175 def self.parse_power_params(value, param_cache: nil, param_cache_mutex: nil) normalized = Base.normalize_params_to_hash(value, %i[exponent result]) return nil unless normalized.is_a?(Hash) cache = param_cache mutex = param_cache_mutex if cache.nil? || mutex.nil? cache = ConditionEvaluator.instance_variable_get(:@param_cache) mutex = ConditionEvaluator.instance_variable_get(:@param_cache_mutex) end cache_key = Base.normalize_param_cache_key(normalized, "power") cached = cache[cache_key] return cached if cached mutex.synchronize do cache[cache_key] ||= parse_power_params_impl(normalized) end end |
.parse_power_params_impl(value) ⇒ Object
195 196 197 198 199 200 201 |
# File 'lib/decision_agent/dsl/operators/mathematical_operators.rb', line 195 def self.parse_power_params_impl(value) exponent = value[:exponent] || value["exponent"] result = value[:result] || value["result"] return nil unless exponent && !result.nil? { exponent: exponent, result: result } end |