Class: SyMath::Fraction
- Defined in:
- lib/symath/fraction.rb
Instance Attribute Summary
Attributes inherited from Operator
Class Method Summary collapse
- .compose_with_simplify(a, b) ⇒ Object
-
.simplify_inf(a, b) ⇒ Object
Divide infinite values.
Instance Method Summary collapse
- #dividend ⇒ Object
- #divisor ⇒ Object
- #evaluate ⇒ Object
- #factors ⇒ Object
-
#initialize(dividend, divisor) ⇒ Fraction
constructor
A new instance of Fraction.
- #is_prod_exp? ⇒ Boolean
- #to_latex ⇒ Object
- #to_s ⇒ Object
- #type ⇒ Object
Methods inherited from Operator
#<=>, #==, #args_assoc, #arity, #dump, #hash, #is_associative?, #is_commutative?, #is_constant?, #name, #reduce, #replace, #variables
Methods inherited from Value
#*, #**, #+, #-, #-@, #/, #<, #<=, #<=>, #>, #>=, #^, #add, #base, create, #deep_clone, #div, #dump, #exponent, #inspect, #inv, #is_divisor_factor?, #is_finite?, #is_nan?, #is_negative?, #is_negative_number?, #is_number?, #is_positive?, #is_sum_exp?, #is_unit_quaternion?, #is_zero?, #mul, #neg, #power, #reduce, #reduce_modulo_sign, #sign, #sub, #terms, #to_m, #wedge
Methods included from Operation::Exterior
Methods included from Operation::Integration
#anti_derivative, #get_linear_constants, initialize, #int_constant, #int_failure, #int_function, #int_inv, #int_pattern, #int_power, #int_product, #int_sum, #integral_bounds
Methods included from Operation::Differential
#_d_wedge, #d, #d_failure, #d_fraction, #d_function, #d_function_def, #d_power, #d_product, initialize
Methods included from Operation
Methods included from Operation::DistributiveLaw
#combfrac_add_term, #combfrac_sum, #combine_fractions, #expand, #expand_product, #expand_single_pass, #factorize, #factorize_integer_poly, #factorize_simple, #has_fractional_terms?
Methods included from Operation::Normalization
#combine_factors, #compare_factors_and_swap, #normalize, #normalize_matrix, #normalize_power, #normalize_product, #normalize_single_pass, #normalize_sum, #order_product, #product_on_fraction_form, #reduce_constant_factors, #replace_combined_factors, #swap_factors
Methods included from Operation::Match
#build_assoc_op, #match, #match_assoc, #match_replace
Constructor Details
#initialize(dividend, divisor) ⇒ Fraction
Returns a new instance of Fraction.
90 91 92 |
# File 'lib/symath/fraction.rb', line 90 def initialize(dividend, divisor) super('/', [dividend, divisor]) end |
Class Method Details
.compose_with_simplify(a, b) ⇒ Object
5 6 7 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 |
# File 'lib/symath/fraction.rb', line 5 def self.compose_with_simplify(a, b) a = a.to_m b = b.to_m return a if b == 1 if a.is_finite?() == false or b.is_finite?() == false return self.simplify_inf(a, b) end # Divide by zero if b.is_zero? if SyMath.setting(:complex_arithmetic) if a.is_zero? return :nan.to_m else return :oo.to_m end else return :nan.to_m end end if a.is_a?(SyMath::Fraction) if b.is_a?(SyMath::Fraction) return self.new(a.dividend*b.divisor, a.divisor*b.dividend) else return self.new(a.dividend, a.divisor*b) end elsif b.is_a?(SyMath::Fraction) return self.new(a*b.divisor, b.dividend) end return self.new(a, b) end |
.simplify_inf(a, b) ⇒ Object
Divide infinite values
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 |
# File 'lib/symath/fraction.rb', line 42 def self.simplify_inf(a, b) # Indefinite factors if a.is_finite?.nil? or b.is_finite?.nil? return self.new(a, b) end # NaN/* = */NaN = NaN if a.is_nan? or b.is_nan? return :nan.to_m end # oo/oo = oo/-oo = -oo/oo = NaN if a.is_finite? == false and b.is_finite? == false return :nan.to_m end # */0 = NaN if b.is_zero? if SyMath.setting(:complex_arithmetic) return :oo.to_m else return :nan.to_m end end # n/oo = n/-oo = 0 if a.is_finite? return 0.to_m end # oo/n = -oo/-n = oo, -oo/n = oo/-n = -oo if b.is_finite? if SyMath.setting(:complex_arithmetic) return :oo.to_m else if a.sign == b.sign return :oo.to_m else return -:oo.to_m end end end # :nocov: raise 'Internal error' # :nocov: end |
Instance Method Details
#dividend ⇒ Object
94 95 96 |
# File 'lib/symath/fraction.rb', line 94 def dividend() return @args[0] end |
#divisor ⇒ Object
98 99 100 |
# File 'lib/symath/fraction.rb', line 98 def divisor() return @args[1] end |
#evaluate ⇒ Object
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 |
# File 'lib/symath/fraction.rb', line 117 def evaluate() # Evaluate matrix division by divding elements if dividend.is_a?(SyMath::Matrix) return dividend.matrix_div(divisor) end # Evaluate df/dx expression. if dividend.is_a?(SyMath::Operator) and dividend.definition.is_a?(SyMath::Definition::D) # Evaluate if the divisor is a simple dform. The composed form # d(x) is accepted as well as the simple dx variable. if divisor.is_a?(SyMath::Definition::Variable) and divisor.is_d? v = divisor.undiff elsif divisor.is_a?(SyMath::Definition::D) and divisor.args[0].is_a?(SyMath::Definition::Variable) and divisor.args[0].type.is_scalar? v = divisor.args[0] else return super end diff = dividend.args[0].evaluate.d([v]).normalize # Hack: We must divide all terms by dv since the simplification does # not recognize factors common to each term ret = 0 dv = v.to_d diff.terms.each do |t| ret += (t/dv).normalize end return ret end return super end |
#factors ⇒ Object
106 107 108 109 110 111 112 113 114 115 |
# File 'lib/symath/fraction.rb', line 106 def factors() return Enumerator.new do |f| dividend.factors.each { |d1| f << d1 } divisor.factors.each { |d2| if d2 != 1 f << d2**-1 end } end end |
#is_prod_exp? ⇒ Boolean
102 103 104 |
# File 'lib/symath/fraction.rb', line 102 def is_prod_exp?() return true end |
#to_latex ⇒ Object
173 174 175 |
# File 'lib/symath/fraction.rb', line 173 def to_latex() return '\frac{' + dividend.to_latex + '}{' + divisor.to_latex + '}' end |
#to_s ⇒ Object
161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/symath/fraction.rb', line 161 def to_s() dividend_str = dividend.is_sum_exp? ? '(' + dividend.to_s + ')' : dividend.to_s divisor_str = (divisor.is_sum_exp? or divisor.is_prod_exp?) ? '(' + divisor.to_s + ')' : divisor.to_s if SyMath.setting(:expl_parentheses) return '('.to_s + dividend_str + '/' + divisor_str + ')'.to_s else return dividend_str + '/' + divisor_str end end |
#type ⇒ Object
153 154 155 156 157 158 159 |
# File 'lib/symath/fraction.rb', line 153 def type() if dividend.type.is_subtype?('rational') return 'rational'.to_t else return dividend.type end end |