Class: SyMath::Operator

Inherits:
Value
  • Object
show all
Defined in:
lib/symath/operator.rb

Direct Known Subclasses

Equation, Fraction, Minus, Power, Product, Sum

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Value

#*, #**, #+, #-, #-@, #/, #<, #<=, #>, #>=, #^, #add, #base, create, #deep_clone, #div, #exponent, #factors, #inspect, #inv, #is_divisor_factor?, #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_modulo_sign, #sign, #sub, #terms, #to_m, #type, #wedge

Methods included from SyMath::Operation::Exterior

#flat, #hodge, #sharp

Methods included from SyMath::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 SyMath::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 SyMath::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 SyMath::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 SyMath::Operation::Match

#build_assoc_op, #match, #match_assoc, #match_replace

Constructor Details

#initialize(definition, args) ⇒ Operator

Returns a new instance of Operator.



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/symath/operator.rb', line 56

def initialize(definition, args)
  if !definition.is_a?(SyMath::Value)
    definition = SyMath::Definition.get(definition)
  end

  @definition = definition
  @args       = args

  if definition.is_a?(SyMath::Definition::Operator)
    definition.validate_args(self)
  end
end

Instance Attribute Details

#argsObject

Returns the value of attribute args.



7
8
9
# File 'lib/symath/operator.rb', line 7

def args
  @args
end

#definitionObject (readonly)

Returns the value of attribute definition.



6
7
8
# File 'lib/symath/operator.rb', line 6

def definition
  @definition
end

Class Method Details

.compose_with_simplify(*args) ⇒ Object

Compose with simplify. Defaults to composition with no reductions



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/symath/operator.rb', line 10

def self.compose_with_simplify(*args)
  d = args[0]
  if d.is_a?(SyMath::Definition::Operator)
    ret = d.compose_with_simplify(*args)
    if ret
      return ret
    end
  end
  
  return self.new(*args)
end

Instance Method Details

#<=>(other) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/symath/operator.rb', line 111

def <=>(other)
  if self.class.name != other.class.name
    return super(other)
  end

  if name != other.name
    return name.to_s <=> other.name.to_s
  end

  if arity != other.arity
    return arity <=> other.arity
  end

  (0...arity).to_a.each do |i|
    diff = args[i] <=> other.args[i]
    if diff != 0
      return diff
    end
  end

  return 0
end

#==(other) ⇒ Object Also known as: eql?



103
104
105
106
107
108
109
# File 'lib/symath/operator.rb', line 103

def ==(other)
  o = other.to_m
  return false if self.class.name != o.class.name
  return false if name.to_s != o.name.to_s
  return false if arity != o.arity
  return args == o.args
end

#args_assocObject

Return arguments



27
28
29
30
31
32
33
# File 'lib/symath/operator.rb', line 27

def args_assoc()
  if is_associative?
    return args.map { |a| a.class == self.class ? a.args_assoc : [a] }.inject(:+)
  else
    return args
  end
end

#arityObject



52
53
54
# File 'lib/symath/operator.rb', line 52

def arity()
  return @args.length
end

#dump(indent = 0) ⇒ Object



84
85
86
87
88
89
90
91
92
# File 'lib/symath/operator.rb', line 84

def dump(indent = 0)
  i = ' '*indent
  ret = super(indent)
  arglist = args.map do |a|
    a.dump(indent + 2)
  end

  return ret + "\n" + arglist.join("\n")
end

#evaluateObject



43
44
45
46
47
48
49
50
# File 'lib/symath/operator.rb', line 43

def evaluate()
  # Hack: Don't evaluate arguments if the operator is the integral.
  # this is taken care of inside the definition.
  if name != :int
    @args = @args.map { |a| a.evaluate }
  end
  definition.evaluate_call(self)
end

#hashObject



94
95
96
97
98
99
100
101
# File 'lib/symath/operator.rb', line 94

def hash()
  h = @name.hash
  @args.each do |a|
    h ^= a.hash
  end

  return h
end

#is_associative?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/symath/operator.rb', line 39

def is_associative?()
  return false
end

#is_commutative?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/symath/operator.rb', line 35

def is_commutative?()
  return false
end

#is_constant?(vars = nil) ⇒ Boolean

Returns:

  • (Boolean)


140
141
142
143
144
145
146
# File 'lib/symath/operator.rb', line 140

def is_constant?(vars = nil)
  @args.each do |a|
    return false if !a.is_constant?(vars)
  end

  return true
end

#nameObject



22
23
24
# File 'lib/symath/operator.rb', line 22

def name()
  return definition.name
end

#reduceObject



136
137
138
# File 'lib/symath/operator.rb', line 136

def reduce()
  return definition.reduce_call(self)
end

#replace(map) ⇒ Object



153
154
155
156
157
158
159
160
161
# File 'lib/symath/operator.rb', line 153

def replace(map)
  @args = @args.map do |a|
    a.replace(map)
  end

  @definition = definition.replace(map)

  return self
end

#to_latexObject



80
81
82
# File 'lib/symath/operator.rb', line 80

def to_latex()
  return definition.to_latex(@args)
end

#to_sObject



69
70
71
72
73
74
75
76
77
78
# File 'lib/symath/operator.rb', line 69

def to_s()
  if definition.is_a?(SyMath::Definition::Operator)
    return definition.to_s(@args)
  else
    # Expression call
    arglist = @args.map { |a| a.to_s }.join(',')
    
    return "(#{definition}).(#{arglist})"
  end
end

#variablesObject



148
149
150
151
# File 'lib/symath/operator.rb', line 148

def variables()
  vars = @args.map { |a| a.variables }
  return vars.length == 0 ? vars : vars.inject(:|)
end