Class: SyMath::Definition::Operator

Inherits:
SyMath::Definition show all
Defined in:
lib/symath/definition/operator.rb

Direct Known Subclasses

Bounds, CoDiff, Curl, D, Div, Flat, Function, Grad, Hodge, Int, Laplacian, Sharp, Xd

Instance Attribute Summary collapse

Attributes inherited from SyMath::Definition

#description, #name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from SyMath::Definition

define, defined?, definitions, get, #hash, #inspect, #is_constant?, #is_function?, #reduce_call, undefine, #variables

Methods inherited from Value

#*, #**, #+, #-, #-@, #/, #<, #<=, #>, #>=, #^, #add, #base, compose_with_simplify, create, #deep_clone, #div, #evaluate, #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, #reduce_modulo_sign, #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(name, args: [], exp: nil, define_symbol: true, description: nil) ⇒ Operator



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/symath/definition/operator.rb', line 53

def initialize(name, args: [], exp: nil, define_symbol: true,
               description: nil)
  if exp and !exp.is_a?(SyMath::Value)
    exp = exp.to_m
  end

  @args = args.map { |a| a.to_m }
  @exp = exp

  super(name, define_symbol: define_symbol, description: description)
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



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

def args
  @args
end

#expObject (readonly)

Returns the value of attribute exp.



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

def exp
  @exp
end

Class Method Details

.init_builtinObject



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
# File 'lib/symath/definition/operator.rb', line 8

def self.init_builtin()
  SyMath::Definition::D.new
  SyMath::Definition::Xd.new
  SyMath::Definition::Int.new
  SyMath::Definition::Bounds.new
  SyMath::Definition::Sharp.new
  SyMath::Definition::Flat.new
  SyMath::Definition::Hodge.new
  SyMath::Definition::Grad.new
  SyMath::Definition::Curl.new
  SyMath::Definition::Div.new
  SyMath::Definition::Laplacian.new
  SyMath::Definition::CoDiff.new

  expressions = [
    { :name => 'laplace',
      :exp  => 'lmd(int(f.(t)*e**(-s*t),d(t),0,oo),s)',
      :desc => 'laplace transform',
    },
    { :name => 'fourier',
      :exp  => 'lmd(int(f.(x)*e**(-2*pi*i*x*w),d(x),-oo,oo),w)',
      :desc => 'fourier transform',
    },
    { :name => 'invfourier',
      :exp  => 'lmd(int(f.(w)*e**(2*pi*i*x*w),d(w),-oo,oo),x)',
      :desc => 'inverse fourier transform',
    },
  ]

  expressions.each do |e|
    self.new(e[:name], args: [:f], exp: e[:exp],
             description: "#{e[:name]}(f) - #{e[:desc]}")
  end

  e = op(:d, lmd(:f, :t))/op(:d, :t)
  self.new('dpart', args: [:f, :t], exp: e,
           description: 'dpart - partial derivative')
end

.operatorsObject



47
48
49
50
51
# File 'lib/symath/definition/operator.rb', line 47

def self.operators()
  return self.definitions.select do |d|
    d.is_operator? and !d.is_function?
  end
end

Instance Method Details

#<=>(other) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/symath/definition/operator.rb', line 91

def <=>(other)
  s = super(other)
  return s if s != 0

  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?



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/symath/definition/operator.rb', line 77

def ==(other)
  if !super(other)
    return false
  end

  o = other.to_m
  return false if self.args.length != o.args.length
  return false if self.exp != o.exp
  return false if self.args != o.args
  return true
end

#arityObject



73
74
75
# File 'lib/symath/definition/operator.rb', line 73

def arity()
  return @args.length
end

#call(*args) ⇒ Object

The call method, or the .() operator, returns an operator or function object representing the operator or function being applied to a list of arguments.



112
113
114
# File 'lib/symath/definition/operator.rb', line 112

def call(*args)
  return SyMath::Operator.create(name, args.map { |a| a.nil? ? a : a.to_m })
end

#compose_with_simplify(*args) ⇒ Object



65
66
67
# File 'lib/symath/definition/operator.rb', line 65

def compose_with_simplify(*args)
  return
end

#dump(indent = 0) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
# File 'lib/symath/definition/operator.rb', line 191

def dump(indent = 0)
  res = super(indent)
  i = ' '*indent
  if args
    arglist = args.map { |a| a.to_s }.join(',')
    res = "#{res}\n#{i}  args: #{arglist}"
  end
  if exp
    res = "#{res}\n#{i}  exp: #{exp}"
  end
end

#evaluate_call(c) ⇒ Object

Evaluate the operator in use



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/symath/definition/operator.rb', line 121

def evaluate_call(c)
  if !exp
    # Operator has no expression, return it unchanged.
    return c
  end

  # Operator has expression. Exand it.
  res = exp.deep_clone
  if arity != c.arity
    raise "Cannot evaluate #{name} with #{c.arity} arguments. Expected #{arity}."
  end

  map = {}
  args.each_with_index do |a, i|
    map[a] = c.args[i]
  end
  res.replace(map)

  # Recursively evaluate the expanded formula.
  res = res.evaluate
  return res
end

#is_operator?Boolean



116
117
118
# File 'lib/symath/definition/operator.rb', line 116

def is_operator?()
  return true
end

#latex_formatObject



173
174
175
# File 'lib/symath/definition/operator.rb', line 173

def latex_format()
  return "\\operatorname{#{name}}(%s)"
end

#replace(map) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/symath/definition/operator.rb', line 144

def replace(map)
  # FIXME: We probably need to filter out the local variables before
  # replacing
  if !exp.nil?
    @exp = exp.replace(map)
  end

  # Replace all arguments
  @args = @args.map do |a|
    a.replace(map)
  end

  return self
end

#to_latex(args = nil) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/symath/definition/operator.rb', line 177

def to_latex(args = nil)
  if !args
    args = @args
  end
  
  if args.length > 0
    arglist = args.map { |a| a.to_latex }.join(',')
  else
    arglist = "..."
  end

  return sprintf(latex_format, arglist)
end

#to_s(args = nil) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/symath/definition/operator.rb', line 159

def to_s(args = nil)
  if !args
    args = @args
  end

  if args.length > 0
    arglist = args.map { |a| a.to_s }.join(',')
  else
    arglist = "..."
  end

  return "#{@name}(#{arglist})"
end

#validate_args(e) ⇒ Object



69
70
71
# File 'lib/symath/definition/operator.rb', line 69

def validate_args(e)
  return
end