Class: SyMath::Power

Inherits:
Operator show all
Defined in:
lib/symath/power.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, #evaluate, #hash, #is_associative?, #is_commutative?, #is_constant?, #name, #reduce, #replace, #variables

Methods inherited from Value

#*, #**, #+, #-, #-@, #/, #<, #<=, #<=>, #>, #>=, #^, #add, create, #deep_clone, #div, #dump, #evaluate, #factors, #inspect, #inv, #is_finite?, #is_nan?, #is_negative?, #is_negative_number?, #is_number?, #is_positive?, #is_prod_exp?, #is_sum_exp?, #is_unit_quaternion?, #is_zero?, #mul, #neg, #power, #reduce, #sign, #sub, #terms, #to_m, #type, #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(base, exponent) ⇒ Power

Returns a new instance of Power.



83
84
85
# File 'lib/symath/power.rb', line 83

def initialize(base, exponent)
  super('**', [base, exponent])
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
# File 'lib/symath/power.rb', line 5

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

  if a.is_finite?() == false or b.is_finite?() == false
    return self.simplify_inf(a, b)
  end
        
  # 0**0 = NaN
  if a.is_zero? and b.is_zero?
    return :nan.to_m
  end

  # n**1 = n
  if b == 1
    return a
  end
  
  if a.is_a?(SyMath::Power)
    return a.base**(a.exponent*b)
  end

  return self.new(a, b)
end

.simplify_inf(a, b) ⇒ Object



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

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

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

  if SyMath.setting(:complex_arithmetic)
    if b.is_finite? == false
      return :nan.to_m
    else
      return :oo.to_m
    end
  else
    if a.is_zero? and b.is_finite? == false
      return :nan.to_m
    end

    # n**-oo = oo**-oo = -oo**-oo = 0
    if b.is_finite? == false and b.is_negative?
      return 0.to_m
    end
    
    if a.is_finite? == false and a.is_negative?
      if b.is_finite? == true
        # -oo*n = oo*(-1**n)
        return :oo.to_m.mul(a.sign**b)
      else
        # -oo**oo = NaN
        return :nan.to_m
      end
    end

    # -n**oo => NaN
    if a.is_finite? and a.is_negative?
      return :nan.to_m
    end
    
    # The only remaining possibilities:
    # oo**n = n*oo = oo*oo = oo
    return :oo.to_m
  end
end

Instance Method Details

#baseObject



87
88
89
# File 'lib/symath/power.rb', line 87

def base()
  return @args[0]
end

#exponentObject



91
92
93
# File 'lib/symath/power.rb', line 91

def exponent()
  return @args[1]
end

#is_divisor_factor?Boolean

Expression is on the form a**-n, n is a positive number

Returns:

  • (Boolean)


96
97
98
# File 'lib/symath/power.rb', line 96

def is_divisor_factor?()
  return exponent.is_negative_number?
end

#reduce_modulo_signObject

Simple reduction rules, allows sign to change. Returns (reduced exp, sign, changed).



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

def reduce_modulo_sign
  # a to the power of 1 reduces to a
  if exponent == 1
    return base, 1, true
  end
  
  # Powers of 1 reduces to 1
  if base == 1 and exponent.is_finite?
    return base, 1, true
  end

  # Power of 0 reduces to 0
  if base == 0 and exponent.is_finite? and exponent != 0
    return 0.to_m, 1, true
  end
  
  if base != 0 and exponent == 0
    return 1.to_m, 1, true
  end

  if base == :e
    fn = fn(:exp, exponent)
    # FIXME: Merge functions reduce and reduce_modulo_sign
    red = fn.reduce
    if red != fn
      return red, 1, true
    end
  end

  # Reduce negative number
  if base.is_a?(SyMath::Minus)
    if exponent.is_number?
      exp = exponent
    elsif exponent.is_negative_number?
      exp = exponent.argument
    else
      exp = nil
    end

    if !exp.nil?
      e, sign, changed = (base.argument**exp).reduce_modulo_sign
      if exp.value.odd?
        sign *= -1
      end
      return e, sign, true
    end
  end

  # Number power of number reduces to number
  if base.is_number?
    if exponent.is_number?
      return (base.value ** exponent.value).to_m, 1, true
    end

    if exponent.is_negative_number? and exponent.argument.value > 1
      return (base.value ** exponent.argument.value).to_m.power(-1), 1, true
    end
  end

  # p**q**r reduces to p**(q*r)
  if base.is_a?(SyMath::Power)
    return base.base.power(base.exponent.mul(exponent)), 1, true
  end

  # Reduce positive integer power of vectors and dforms to zero
  if (base.type.is_dform? or base.type.is_vector?) and
    exponent.is_number?
    return 0.to_m, 1, true
  end
  
  # Remaining code reduces only quaternions
  if !base.is_unit_quaternion?
    return self, 1, false
  end
  
  # q**n for some unit quaternion
  # Exponent is 1 or not a number
  if !exponent.is_number? or exponent == 1
    return self, 1, false
  end

  # e is on the form q**n for some integer n >= 2
  x = exponent.value
  
  if x.odd?
    ret = base
    x -= 1
  else
    ret = 1.to_m
  end

  if (x/2).odd?
    return ret, -1, true
  else
    return ret, 1, true
  end
end

#to_latexObject



214
215
216
217
218
219
220
221
222
# File 'lib/symath/power.rb', line 214

def to_latex()
  if base.is_sum_exp? or base.is_prod_exp? or base.is_a?(SyMath::Power)
    base_str = '\left(' + base.to_latex + '\right)'
  else
    base_str = base.to_latex
  end

  return base_str + '^' + '{' + exponent.to_latex + '}'
end

#to_sObject



200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/symath/power.rb', line 200

def to_s()
  if base.is_sum_exp? or base.is_prod_exp? or base.is_a?(SyMath::Power)
    base_str = '(' + base.to_s + ')'
  else
    base_str = base.to_s
  end

  expo_str = (exponent.is_sum_exp? or exponent.is_prod_exp?) ?
             '(' + exponent.to_s + ')' :
             exponent.to_s
  
  return base_str + '**' + expo_str
end