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
-
#color ⇒ Object
Graphviz node color.
-
#data ⇒ Object
A string representation of the node’s data, typically calculated value with units.
-
#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, #unit, #units, #with_units
Methods included from Units::Class
Instance Attribute Details
#name ⇒ Object
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 |
Instance Method Details
#color ⇒ Object
Graphviz node color
327 328 329 |
# File 'lib/mathviz.rb', line 327 def color :black end |
#data ⇒ Object
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 |
#label ⇒ Object
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 |
#shape ⇒ Object
Graphviz node shape
322 323 324 |
# File 'lib/mathviz.rb', line 322 def shape :ellipse end |
#style ⇒ Object
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_i ⇒ Object
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_s ⇒ Object
286 287 288 |
# File 'lib/mathviz.rb', line 286 def to_s @name || anon end |