Module: SyMath::Operation::DistributiveLaw
- Included in:
- Value
- Defined in:
- lib/symath/operation/distributivelaw.rb
Instance Method Summary collapse
- #combfrac_add_term(sum, t) ⇒ Object
- #combfrac_sum ⇒ Object
-
#combine_fractions ⇒ Object
The combine_fractions() method combines fractions by first determining their least common denominator, then applying the distributive law.
-
#expand ⇒ Object
The expand() method expands a product using the distributive law over products of sums: a*(b + c) -> a*b + a*c a*(b - c) -> a*b - a*c The transformation iterates until no changes occur.
- #expand_product(exp1, exp2) ⇒ Object
- #expand_single_pass ⇒ Object
-
#factorize ⇒ Object
The factorize() method factorizes a univariate polynomial expression with integer coefficients.
- #factorize_integer_poly ⇒ Object
-
#factorize_simple ⇒ Object
Collect factors which occur in each term.
- #has_fractional_terms? ⇒ Boolean
Instance Method Details
#combfrac_add_term(sum, t) ⇒ Object
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 |
# File 'lib/symath/operation/distributivelaw.rb', line 272 def combfrac_add_term(sum, t) c = 1 dc = 1 fact = 1.to_m divf = 1.to_m t.factors.each do |f| if f.is_number? c *= f.value next end if f == -1 fact *= -1 next end if f.is_divisor_factor? if f.base.is_number? dc *= (f.base.value**f.exponent.argument.value) else divf *= f.base end next end fact *= f end if !sum.key?(divf) sum[divf] = {} sum[divf][:fact] = fact sum[divf][:c] = c sum[divf][:dc] = dc return end s = sum[divf] lcm = dc.lcm(s[:dc]) if lcm > dc c *= lcm/dc dc = lcm end if lcm > s[:dc] s[:c] *= lcm/s[:dc] s[:dc] = lcm end if fact.nil? fact = c.to_m elsif c > 1 fact = fact.mul(c.to_m) end if s[:fact].nil? fact = fact.add(s[:c].to_m) if s[:c] > 1 else fact = fact.add(s[:c] > 1 ? s[:c].to_m*s[:fact] : s[:fact]) end s[:fact] = fact s[:c] = 1 end |
#combfrac_sum ⇒ Object
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 |
# File 'lib/symath/operation/distributivelaw.rb', line 338 def combfrac_sum sum = {} terms.each do |t| combfrac_add_term(sum, t) end ret = 0.to_m sum.keys.each do |divf| s = sum[divf] if s[:c] > 1 r = s[:c].to_m.mul(s[:fact]) else r = s[:fact] end if divf.nil? r = r.div(s[:dc]) if s[:dc] > 1 elsif s[:dc] > 1 r = r.div(s[:dc].to_m*divf) else r = r.div(divf) end ret += r return ret end end |
#combine_fractions ⇒ Object
The combine_fractions() method combines fractions by first determining their least common denominator, then applying the distributive law. Examples:
a/c + b/c -> (a + b)/c
2/3 + 3/4 -> 17/12
a/2 + 2*a/3 -> 7*a/6
2*a/b + 2*c/(3*b) -> (6*a + 2*c)/(3*b)
264 265 266 267 268 269 270 |
# File 'lib/symath/operation/distributivelaw.rb', line 264 def combine_fractions() if is_sum_exp? return combfrac_sum end return recurse('combine_fractions', nil) end |
#expand ⇒ Object
The expand() method expands a product using the distributive law over products of sums:
a*(b + c) -> a*b + a*c
a*(b - c) -> a*b - a*c
The transformation iterates until no changes occur. Thus, the expression
(a + b)*(c + d) transforms to a*c + a*d + b*c + b*d
11 12 13 |
# File 'lib/symath/operation/distributivelaw.rb', line 11 def () return iterate('expand_single_pass') end |
#expand_product(exp1, exp2) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/symath/operation/distributivelaw.rb', line 51 def (exp1, exp2) sign = 1.to_m if exp1.is_a?(SyMath::Minus) exp1 = exp1.argument sign = -sign end if exp2.is_a?(SyMath::Minus) exp2 = exp2.argument sign = -sign end ret = 0.to_m exp1.terms.each do |t1| exp2.terms.each do |t2| ret += sign*t1*t2 end end return ret end |
#expand_single_pass ⇒ Object
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 |
# File 'lib/symath/operation/distributivelaw.rb', line 15 def if is_a?(SyMath::Minus) return -argument. end if is_a?(SyMath::Power) or is_a?(SyMath::Product) ret = 1.to_m factors.each do |f| if f.is_a?(SyMath::Power) if f.exponent.is_number? f.exponent.value.times { ret = (ret, f.base) } else ret = (ret, f) end else ret = (ret, f) end end return ret end if is_sum_exp? ret = 0.to_m terms.each do |t| ret += t. end return ret end return self end |
#factorize ⇒ Object
The factorize() method factorizes a univariate polynomial expression with integer coefficients.
226 227 228 229 230 231 232 233 234 235 |
# File 'lib/symath/operation/distributivelaw.rb', line 226 def factorize() if (has_fractional_terms?) e = combine_fractions if e.is_a?(SyMath::Fraction) return e.dividend.factorize_integer_poly.div(e.divisor) end else return factorize_integer_poly end end |
#factorize_integer_poly ⇒ Object
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'lib/symath/operation/distributivelaw.rb', line 237 def factorize_integer_poly() dup = SyMath::Poly::DUP.new(self) factors = dup.factor ret = factors[1].map do |f| if f[1] != 1 f[0].to_m.power(f[1]) else f[0].to_m end end if factors[0] != 1 ret.unshift(factors[0].to_m) end return ret.inject(:mul) end |
#factorize_simple ⇒ Object
Collect factors which occur in each term.
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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/symath/operation/distributivelaw.rb', line 88 def factorize_simple() return self if !self.is_sum_exp? sfactors = {} vfactors = {} coeffs = [] dcoeffs = [] vectors = [] terms.each_with_index do |t, i| c = 1 dc = 1 vf = 1.to_m t.factors.each do |f| # Sign if f == -1 c *= -1 next end # Constant if f.is_number? c *= f.value next end if f.is_divisor_factor? # Divisor constant if f.base.is_number? dc *= f.base.value**f.exponent.argument.value next end # Divisor factor ex = f.base if !sfactors.key?(ex) sfactors[ex] = [] end if sfactors[ex][i].nil? sfactors[ex][i] = - f.exponent.argument.value else sfactors[ex][i] -= f.exponent.argument.value end next end # Vector factor if f.type.is_subtype?(:tensor) vf *= f next end # Scalar factor if f.exponent.is_number? ex = f.base n = f.exponent.value else ex = f n = 1 end if !sfactors.key?(ex) sfactors[ex] = [] end sfactors[ex][i] = n.value end coeffs.push(c) dcoeffs.push(dc) vectors.push(vf) end # If there is only one term, there is nothing to factorize if coeffs.length == 1 return self end # Try to factorize the scalar part spart = 1.to_m dpart = 1.to_m sfactors.each do |ex, pow| # Replace nil with 0 and extend array to full length pow.map! { |i| i || 0 } (coeffs.length - pow.length).times { pow << 0 } if pow.max > 0 and pow.min > 0 f = pow.min pow.map! { |i| i - f } spart = spart*ex**f end if pow.max < 0 and pow.min < 0 f = pow.max pow.map! { |i| i - f } dpart = dpart*ex**(-f) end end # Return self if there were no common factors. if spart == 1 and dpart == 1 return self end # Extract gcd from coeffs and dcoeffs gcd_coeffs = coeffs.inject(:gcd) gcd_dcoeffs = dcoeffs.inject(:gcd) coeffs.map! { |i| i/gcd_coeffs } dcoeffs.map! { |i| i/gcd_dcoeffs } newsum = 0.to_m (0..coeffs.length-1).each do |i| t = coeffs[i].to_m sfactors.each do |ex, pow| next if pow[i].nil? if pow[i] > 0 t *= ex**pow[i] elsif pow[i] < 0 t /= ex**(-pow[i]) end end t /= dcoeffs[i] t *= vectors[i] newsum += t end return gcd_coeffs*spart*newsum/(gcd_dcoeffs*dpart) end |
#has_fractional_terms? ⇒ Boolean
75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/symath/operation/distributivelaw.rb', line 75 def has_fractional_terms?() terms.each do |t| t.factors.each do |f| if f.is_divisor_factor? return true end end end return false end |