Class: MathViz::Operation

Inherits:
Term show all
Defined in:
lib/mathviz.rb

Overview

n-ary operators

Direct Known Subclasses

Unary

Defined Under Namespace

Classes: Unary

Instance Attribute Summary

Attributes inherited from Term

#name

Instance Method Summary collapse

Methods inherited from Term

#anonymous?, binop, #data, #generated?, #label, list_terms, name_terms!, #style, #to_i, #to_s, unop

Methods included from Measured

#per, #unit, #with_units

Methods included from Units::Class

#included, #new_units

Constructor Details

#initialize(op, *operands) ⇒ Operation

Returns a new instance of Operation.



565
566
567
568
569
# File 'lib/mathviz.rb', line 565

def initialize(op, *operands)
  super()
  @op = op
  @operands = operands.map{|x| MathViz.term(x)}
end

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

#colorObject

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

Returns:

  • (Boolean)


625
626
627
# File 'lib/mathviz.rb', line 625

def constant?
  @operands.all?(&:constant?)
end

#finite?Boolean

Returns:

  • (Boolean)


629
630
631
# File 'lib/mathviz.rb', line 629

def finite?
  @operands.all?(&:finite?)
end

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

#longObject

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

#shapeObject

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_valueObject

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

#unitsObject

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