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, #to_value, #unit, #units, #with_units

Methods included from Units::Class

#included, #new_units

Instance Attribute Details

#nameObject

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



361
362
363
# File 'lib/mathviz.rb', line 361

def name
  @name
end

Class Method Details

.binop(op) ⇒ Object

Define op as a binary operator



347
348
349
350
351
# File 'lib/mathviz.rb', line 347

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

.list_terms(env) ⇒ Object

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



335
336
337
338
339
340
341
342
343
344
# File 'lib/mathviz.rb', line 335

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.



325
326
327
328
329
330
331
332
# File 'lib/mathviz.rb', line 325

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.to_s
    end
  end
end

.unop(op) ⇒ Object

Define op as an unary operator



354
355
356
357
358
# File 'lib/mathviz.rb', line 354

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

Instance Method Details

#anonymous?Boolean

Only valid after names have been assigned, which means not during graph construction

Returns:

  • (Boolean)


430
431
432
# File 'lib/mathviz.rb', line 430

def anonymous?
  !@name
end

#collapse(parentop = nil) ⇒ Object

Stub



435
436
437
# File 'lib/mathviz.rb', line 435

def collapse(parentop = nil)
  [self]
end

#colorObject

Graphviz node color



404
405
406
# File 'lib/mathviz.rb', line 404

def color
  :black
end

#dataObject

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



368
369
370
371
372
373
374
375
376
377
378
379
380
381
# File 'lib/mathviz.rb', line 368

def data
  f = to_value
  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

#generated?Boolean

Helper to avoid duplicate nodes

Returns:

  • (Boolean)


425
426
427
# File 'lib/mathviz.rb', line 425

def generated?
  !name.nil?
end

#labelObject

Text label for graph nodes



390
391
392
393
394
395
396
# File 'lib/mathviz.rb', line 390

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

#shapeObject

Graphviz node shape



399
400
401
# File 'lib/mathviz.rb', line 399

def shape
  :ellipse
end

#styleObject

Graphviz node line style



409
410
411
412
413
414
415
416
417
# File 'lib/mathviz.rb', line 409

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



420
421
422
# File 'lib/mathviz.rb', line 420

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

#to_iObject



383
384
385
386
387
# File 'lib/mathviz.rb', line 383

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

#to_sObject



363
364
365
# File 'lib/mathviz.rb', line 363

def to_s
  @name || anon
end