Class: MathViz::Term
Overview
Base class for graphable objects. It also contain the operators, which return MathViz::Operation subclasses.
Instance Attribute Summary collapse
-
#name ⇒ Object
Graphviz node name; see MathViz::Term#name_terms!.
Class Method Summary collapse
-
.binop(op) ⇒ Object
Define op as a binary operator.
-
.list_terms(env) ⇒ Object
Return a list of all MathViz::Terms accessible from a binding.
-
.name_terms!(env) ⇒ Object
Assign names to named MathViz::Terms, so the name can be effiently looked up from the MathViz::Term object.
-
.unop(op) ⇒ Object
Define op as an unary operator.
Instance Method Summary collapse
-
#anonymous? ⇒ Boolean
Only valid after names have been assigned, which means not during graph construction.
-
#collapse(parentop = nil) ⇒ Object
Stub.
-
#color ⇒ Object
Graphviz node color.
-
#data ⇒ Object
A string representation of the node’s data, typically calculated value with units.
-
#generated? ⇒ Boolean
Helper to avoid duplicate nodes.
-
#label ⇒ Object
Text label for graph nodes.
-
#shape ⇒ Object
Graphviz node shape.
-
#style ⇒ Object
Graphviz node line style.
-
#to_dot(g) ⇒ Object
Extend Graphviz g with a representation of this object.
- #to_i ⇒ Object
- #to_s ⇒ Object
Methods included from Measured
#per, #to_value, #unit, #units, #with_units
Methods included from Units::Class
Instance Attribute Details
#name ⇒ Object
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 |
Instance Method Details
#anonymous? ⇒ Boolean
Only valid after names have been assigned, which means not during graph construction
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 |
#color ⇒ Object
Graphviz node color
404 405 406 |
# File 'lib/mathviz.rb', line 404 def color :black end |
#data ⇒ Object
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
425 426 427 |
# File 'lib/mathviz.rb', line 425 def generated? !name.nil? end |
#label ⇒ Object
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 |
#shape ⇒ Object
Graphviz node shape
399 400 401 |
# File 'lib/mathviz.rb', line 399 def shape :ellipse end |
#style ⇒ Object
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_i ⇒ Object
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_s ⇒ Object
363 364 365 |
# File 'lib/mathviz.rb', line 363 def to_s @name || anon end |