Class: SyMath::Product

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

Direct Known Subclasses

Wedge

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, #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

#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) ⇒ Product

Returns a new instance of Product.



103
104
105
# File 'lib/symath/product.rb', line 103

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
63
64
65
66
# File 'lib/symath/product.rb', line 5

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

  # Multipling a value with an equation multiplies it with 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

  # First try some simple reductions
  # a*1 => a
  return a if b == 1
  return b if a == 1

  # -a*-b => a*b
  if b.is_a?(SyMath::Minus) and a.is_a?(SyMath::Minus)
    return a.argument*b.argument
  end

  # (-a)*b => -(a*b)
  # a*(-b) => -(a*b)
  return -(a*b.argument) if b.is_a?(SyMath::Minus)
  return -(a.argument*b) if a.is_a?(SyMath::Minus)
  
  if b.is_a?(SyMath::Matrix)
    return self.new(a, b)
  end
  
  if a.base == b.base
    return a.base ** (a.exponent + b.exponent)
  end

  # (1/a)*b => b/a
  if a.is_a?(SyMath::Fraction) and a.dividend == 1.to_m
    return b/a.divisor
  end

  # a*(1/b) => a*/b
  if b.is_a?(SyMath::Fraction) and b.dividend == 1.to_m
    return a/b.divisor
  end

  if a.type.is_subtype?(:tensor) and b.type.is_subtype?(:tensor)
    # Expand expression if any of the parts are sum
    if b.is_sum_exp?
      return b.terms.map { |f| a.*(f) }.inject(:+)
    end

    if a.is_sum_exp?
      return a.terms.map { |f| f.wedge(b) }.inject(:+)
    end
    
    return a.wedge(b)
  end

  return self.new(a, b)
end

.simplify_inf(a, b) ⇒ Object



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

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 multiplies to NaN
  if a.is_nan? or b.is_nan?
    return :nan.to_m
  end

  # oo*0 = 0*oo = NaN
  if a.is_zero? or b.is_zero?
    return :nan.to_m
  end

  if SyMath.setting(:complex_arithmetic)
    return :oo.to_m
  else
    if (a.is_positive? and b.is_positive?) or
      (a.is_negative? and b.is_negative?)
      return :oo.to_m
    end

    if (a.is_negative? and b.is_positive?) or
      (a.is_positive? and b.is_negative?)
      return -:oo.to_m
    end
  end
  
  # :nocov:
  raise 'Internal error'
  # :nocov:
end

Instance Method Details

#evaluateObject



142
143
144
145
146
147
148
149
150
151
# File 'lib/symath/product.rb', line 142

def evaluate()
  if factor1.is_a?(SyMath::Matrix)
    return factor1.matrix_mul(factor2)
  elsif factor2.is_a?(SyMath::Matrix) and
       factor1.type.is_scalar?
    return factor2.matrix_mul(factor1)
  end

  return super
end

#factor1Object



107
108
109
# File 'lib/symath/product.rb', line 107

def factor1()
  return @args[0]
end

#factor1=(f) ⇒ Object



111
112
113
# File 'lib/symath/product.rb', line 111

def factor1=(f)
  @args[0] = f
end

#factor2Object



115
116
117
# File 'lib/symath/product.rb', line 115

def factor2()
  return @args[1]
end

#factor2=(f) ⇒ Object



119
120
121
# File 'lib/symath/product.rb', line 119

def factor2=(f)
  @args[1] = f
end

#factorsObject



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

def factors()
  return Enumerator.new do |f|
    factor1.factors.each { |f1| f << f1 }
    factor2.factors.each { |f2| f << f2 }
  end
end

#is_associative?Boolean

Returns:

  • (Boolean)


127
128
129
# File 'lib/symath/product.rb', line 127

def is_associative?()
  return true
end

#is_commutative?Boolean

Returns:

  • (Boolean)


123
124
125
# File 'lib/symath/product.rb', line 123

def is_commutative?()
  return true
end

#is_prod_exp?Boolean

Returns:

  • (Boolean)


131
132
133
# File 'lib/symath/product.rb', line 131

def is_prod_exp?()
  return true
end

#to_latexObject



171
172
173
174
175
176
177
178
179
180
181
# File 'lib/symath/product.rb', line 171

def to_latex()
  dot = SyMath.setting(:ltx_product_sign) ? ' \ccdot ' : ' ';
  
  return @args.map do |a|
    if a.is_sum_exp?
      '(' + a.to_latex + ')'
    else
      a.to_latex
    end
  end.join(dot)
end

#to_sObject



157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/symath/product.rb', line 157

def to_s()
  if SyMath.setting(:expl_parentheses)
    return '('.to_s + factor1.to_s + '*' + factor2.to_s + ')'.to_s
  else
    return @args.map do |a|
      if a.is_sum_exp?
        '(' + a.to_s + ')'
      else
        a.to_s
      end
    end.join('*')
  end
end

#typeObject



153
154
155
# File 'lib/symath/product.rb', line 153

def type()
  return factor1.type.product(factor2.type)
end