Class: SyMath::Sum

Inherits:
Operator show all
Defined in:
lib/symath/sum.rb

Instance Attribute Summary

Attributes inherited from Operator

#args, #definition

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Operator

#<=>, #==, #args_assoc, #arity, #dump, #hash, #is_constant?, #name, #reduce, #replace, #variables

Methods inherited from Value

#*, #**, #+, #-, #-@, #/, #<, #<=, #<=>, #>, #>=, #^, #add, #base, create, #deep_clone, #div, #dump, #exponent, #factors, #inspect, #inv, #is_divisor_factor?, #is_finite?, #is_nan?, #is_negative?, #is_negative_number?, #is_number?, #is_positive?, #is_prod_exp?, #is_unit_quaternion?, #is_zero?, #mul, #neg, #power, #reduce, #reduce_modulo_sign, #sign, #sub, #to_m, #wedge

Methods included from Operation::Exterior

#flat, #hodge, #sharp

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

#iterate, #recurse

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(arg1, arg2) ⇒ Sum

Returns a new instance of Sum.



110
111
112
# File 'lib/symath/sum.rb', line 110

def initialize(arg1, arg2)
  super('+', [arg1, arg2])
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/symath/sum.rb', line 5

def self.compose_with_simplify(a, b)
  a = a.to_m
  b = b.to_m

  # Adding a value to an equation adds it to both sides, preserving
  # the balance of the equation
  if b.is_a?(SyMath::Equation)
    return eq(a + b.args[0], a + b.args[1])
  end

  if a.is_finite?() == false or b.is_finite?() == false
    return self.simplify_inf(a, b)
  end

  return a if b == 0
  return b if a == 0

  sc = 1
  sf = []
  oc = 1
  of = []

  a.factors.each do |f|
    if f == -1
      sc *= -1
    elsif f.is_number?
      sc *= f.value
    else
      sf.push f
    end
  end

  b.factors.each do |f|
    if f == -1
      oc *= -1
    elsif f.is_number?
      oc *= f.value
    else
      of.push f
    end
  end
  
  sc += oc

  if sf == of
    if sc == 0
      return 0.to_m
    end

    if sc != 1
      sf.unshift sc.to_m
    end

    return sf.empty? ? 1.to_m : sf.inject(:*)
  end

  return self.new(a, b)
end

.simplify_inf(a, b) ⇒ Object



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
# File 'lib/symath/sum.rb', line 64

def self.simplify_inf(a, b)
  # Indefinite terms
  if a.is_finite?.nil? or b.is_finite?.nil?
    return a.add(b)
  end
  
  # NaN add to NaN
  if a.is_nan? or b.is_nan?
    return :nan.to_m
  end

  if SyMath.setting(:complex_arithmetic)
    # +- oo +- oo = NaN
    if (a.is_finite? == false and b.is_finite? == false)
      return :nan.to_m
    end

    # oo + n = n + oo = NaN
    if (a.is_finite? == false or b.is_finite? == false)
      return :oo.to_m
    end
  else
    # oo - oo = -oo + oo = NaN
    if (a.is_finite? == false and b.is_finite? == false)
      if (a.is_positive? and b.is_negative?) or
        (a.is_negative? and b.is_positive?)
        return :nan.to_m
      end
    end

    # oo + n = n + oo = oo + oo = oo
    if a.is_finite? == false
      return a
    end

    # n - oo = - oo + n = -oo - oo = -oo
    if b.is_finite? == false
      return b
    end
  end

  # :nocov:
  raise 'Internal error'
  # :nocov:
end

Instance Method Details

#evaluateObject



142
143
144
145
146
147
148
# File 'lib/symath/sum.rb', line 142

def evaluate
  if term1.type.is_matrix?
    return term1.matrix_add(term2)
  end

  return super
end

#is_associative?Boolean

Returns:

  • (Boolean)


126
127
128
# File 'lib/symath/sum.rb', line 126

def is_associative?()
  return true
end

#is_commutative?Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/symath/sum.rb', line 122

def is_commutative?()
  return true
end

#is_sum_exp?Boolean

Returns:

  • (Boolean)


130
131
132
# File 'lib/symath/sum.rb', line 130

def is_sum_exp?()
  return true
end

#term1Object



114
115
116
# File 'lib/symath/sum.rb', line 114

def term1()
  return @args[0]
end

#term2Object



118
119
120
# File 'lib/symath/sum.rb', line 118

def term2()
  return @args[1]
end

#termsObject

Return all terms in the sum



135
136
137
138
139
140
# File 'lib/symath/sum.rb', line 135

def terms()
  return Enumerator.new do |s|
    term1.terms.each { |s1| s << s1 }
    term2.terms.each { |s2| s << s2 }
  end
end

#to_latexObject



166
167
168
169
170
171
172
# File 'lib/symath/sum.rb', line 166

def to_latex()
  if term2.is_a?(SyMath::Minus)
    return term1.to_latex + ' ' + term2.to_latex
  else
    return term1.to_latex + ' + ' + term2.to_latex
  end
end

#to_sObject



154
155
156
157
158
159
160
161
162
163
164
# File 'lib/symath/sum.rb', line 154

def to_s()
  if SyMath.setting(:expl_parentheses)
    return '('.to_s + term1.to_s + ' + ' + term2.to_s + ')'.to_s
  else
    if term2.is_a?(SyMath::Minus)
      return term1.to_s + " " + term2.to_s
    else
      return term1.to_s + " + " + term2.to_s
    end
  end
end

#typeObject



150
151
152
# File 'lib/symath/sum.rb', line 150

def type()
  return term1.type.sum(term2.type)
end