Class: MathViz::Operation
Overview
n-ary operators
Direct Known Subclasses
Defined Under Namespace
Classes: Unary
Instance Attribute Summary
Attributes inherited from Term
Instance Method Summary collapse
-
#collapse(parentop = nil) ⇒ Object
Combine anonymous nodes with the same operator to simply graph.
-
#color ⇒ Object
Graphviz node color; differentiates basic mathematical operators (+, -, *, /).
- #constant? ⇒ Boolean
- #finite? ⇒ Boolean
-
#initialize(op, *operands) ⇒ Operation
constructor
A new instance of Operation.
-
#link_from(g, other, style, label) ⇒ Object
Draws edges, and ensure the from nodes is drawn.
-
#long ⇒ Object
Debugging method; returns string of names and values.
-
#shape ⇒ Object
Graphviz node shape; differentiates comparison operators.
-
#to_dot(g) ⇒ Object
Extend Graphviz g with a representation of this object, and incoming connections.
-
#to_value ⇒ Object
Apply the operator to create the derived value.
-
#units ⇒ Object
Apply the operator to create the derived units.
Methods inherited from Term
#anonymous?, binop, #data, #generated?, #label, list_terms, name_terms!, #style, #to_i, #to_s, unop
Methods included from Measured
Methods included from Units::Class
Constructor Details
Instance Method Details
#collapse(parentop = nil) ⇒ Object
Combine anonymous nodes with the same operator to simply graph
634 635 636 637 638 639 640 641 |
# File 'lib/mathviz.rb', line 634 def collapse(parentop = nil) @operands = @operands.flat_map {|x| x.collapse(@op)} if anonymous? && parentop == @op @operands else [self] end end |
#color ⇒ Object
Graphviz node color; differentiates basic mathematical operators (+, -, *, /)
587 588 589 590 591 592 593 594 595 |
# File 'lib/mathviz.rb', line 587 def color case @op when :+; :green; when :-; :yellow; when :*; :blue; when :/; :cyan; else :red; end end |
#constant? ⇒ Boolean
625 626 627 |
# File 'lib/mathviz.rb', line 625 def constant? @operands.all?(&:constant?) end |
#finite? ⇒ Boolean
629 630 631 |
# File 'lib/mathviz.rb', line 629 def finite? @operands.all?(&:finite?) end |
#link_from(g, other, style, label) ⇒ Object
Draws edges, and ensure the from nodes is drawn
598 599 600 601 602 603 604 605 |
# File 'lib/mathviz.rb', line 598 def link_from(g, other, style, label) if label (g[other.to_s] >> g[node]) [:arrowhead => style, :headlabel => @op.to_s, :labeldistance => '2'] else (g[other.to_s] >> g[node]) [:arrowhead => style] end other.to_dot(g) unless other.generated? end |
#long ⇒ Object
Debugging method; returns string of names and values
572 573 574 575 |
# File 'lib/mathviz.rb', line 572 def long n = @name && (@name + " = ") "(#{n}#{@op} #{@operands.join(',')} = #{to_value})" end |
#shape ⇒ Object
Graphviz node shape; differentiates comparison operators
578 579 580 581 582 583 584 |
# File 'lib/mathviz.rb', line 578 def shape if ([:>, :<, :>=, :<=, :&, :|, :==].include? @op) :ellipse else :box end end |
#to_dot(g) ⇒ Object
Extend Graphviz g with a representation of this object, and incoming connections
608 609 610 611 612 |
# File 'lib/mathviz.rb', line 608 def to_dot(g) super link_from(g, @operands.first, :normal, false) @operands.slice(1..-1).each {|x| link_from(g, x, :onormal, true)} end |
#to_value ⇒ Object
Apply the operator to create the derived value.
615 616 617 618 |
# File 'lib/mathviz.rb', line 615 def to_value return MathViz::Infinity unless finite? @operands.map(&:to_value).reduce(&@op) end |
#units ⇒ Object
Apply the operator to create the derived units.
621 622 623 |
# File 'lib/mathviz.rb', line 621 def units @operands.map(&:units).reduce(&@op) end |