Class: MathViz::Term

Inherits:
Object show all
Includes:
Measured
Defined in:
lib/mathviz.rb

Overview

Base class for graphable objects. It also contain the operators, which return MathViz::Operation subclasses.

Direct Known Subclasses

Constant, Operation

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Measured

#per, #unit, #units, #with_units

Methods included from Units::Class

#included, #new_units

Instance Attribute Details

#nameObject

Graphviz node name; see MathViz::Term#name_terms!



284
285
286
# File 'lib/mathviz.rb', line 284

def name
  @name
end

Class Method Details

.binop(op) ⇒ Object

Define op as a binary operator



270
271
272
273
274
# File 'lib/mathviz.rb', line 270

def self.binop(op)
  define_method(op) do |c|
    MathViz::Operation::Binary.new(self, op, c)
  end
end

.list_terms(env) ⇒ Object

Return a list of all MathViz::Terms accessible from a binding



258
259
260
261
262
263
264
265
266
267
# File 'lib/mathviz.rb', line 258

def self.list_terms(env)
  eval("local_variables", env).map { |var|
    value = eval(var.to_s, env)
    if (value.kind_of?(MathViz::Term))
      value
    else
      nil
    end
  }.compact
end

.name_terms!(env) ⇒ Object

Assign names to named MathViz::Terms, so the name can be effiently looked up from the MathViz::Term object.



248
249
250
251
252
253
254
255
# File 'lib/mathviz.rb', line 248

def self.name_terms!(env)
  eval("local_variables", env).each do |var|
    value = eval(var.to_s, env)
    if value.respond_to? :name=
      value.name = var
    end
  end
end

.unop(op) ⇒ Object

Define op as an unary operator



277
278
279
280
281
# File 'lib/mathviz.rb', line 277

def self.unop(op)
  define_method(op) do
    MathViz::Operation::Unary.new(self, op)
  end
end

Instance Method Details

#colorObject

Graphviz node color



327
328
329
# File 'lib/mathviz.rb', line 327

def color
  :black
end

#dataObject

A string representation of the node’s data, typically calculated value with units.



291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/mathviz.rb', line 291

def data
  f = to_f
  if (f.kind_of?(TrueClass) || f.kind_of?(FalseClass))
    f.to_s
  elsif (!f.respond_to? :finite?)
    f.to_s + with_units
  elsif (!f.finite?)
    MathViz::Infinity.to_s
  elsif (f.floor == f)
    f.to_i.to_s + with_units
  else
    f.to_s + with_units
  end
end

#labelObject

Text label for graph nodes



313
314
315
316
317
318
319
# File 'lib/mathviz.rb', line 313

def label
  if (@name)
    [data, node].join("\n")
  else
    data
  end
end

#shapeObject

Graphviz node shape



322
323
324
# File 'lib/mathviz.rb', line 322

def shape
  :ellipse
end

#styleObject

Graphviz node line style



332
333
334
335
336
337
338
339
340
# File 'lib/mathviz.rb', line 332

def style
  if anonymous?
    :dotted
  elsif constant?
    :solid
  else
    :dashed
  end
end

#to_dot(g) ⇒ Object

Extend Graphviz g with a representation of this object



343
344
345
# File 'lib/mathviz.rb', line 343

def to_dot(g)
  g[node] [:label => label, :shape => shape, :color => color, :style => style]
end

#to_iObject



306
307
308
309
310
# File 'lib/mathviz.rb', line 306

def to_i
  f = to_f
  return MathViz::Infinity unless f.finite?
  f.to_i
end

#to_sObject



286
287
288
# File 'lib/mathviz.rb', line 286

def to_s
  @name || anon
end