Class: MathViz::Operation::Binary

Inherits:
MathViz::Operation show all
Defined in:
lib/mathviz.rb

Overview

Display and processing for two-value operators

Instance Attribute Summary

Attributes inherited from Term

#name

Instance Method Summary collapse

Methods inherited from MathViz::Operation

#term

Methods inherited from Term

binop, #data, #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(a, op, b) ⇒ Binary

Returns a new instance of Binary.


537
538
539
540
541
542
# File 'lib/mathviz.rb', line 537

def initialize(a, op, b)
  super()
  @a = term(a)
  @op = op
  @b = term(b)
end

Instance Method Details

#colorObject

Graphviz node color; differentiates basic mathematical operators (+, -, *, /)


560
561
562
563
564
565
566
567
568
# File 'lib/mathviz.rb', line 560

def color
  case @op
  when :+; :green;
  when :-; :yellow;
  when :*; :blue;
  when :/; :cyan;
  else     :red;
  end
end

#constant?Boolean

True only if both operands are #constant?

Returns:

  • (Boolean)

590
591
592
# File 'lib/mathviz.rb', line 590

def constant?
  @a.constant? && @b.constant?
end

#longObject

Debugging method; returns string of names and values


545
546
547
548
# File 'lib/mathviz.rb', line 545

def long
  n = @name && (@name + " = ")
  "(#{n}#{@a} #{@op} #{@b} = #{to_f})"
end

#shapeObject

Graphviz node shape; differentiates comparison operators


551
552
553
554
555
556
557
# File 'lib/mathviz.rb', line 551

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


571
572
573
574
575
576
577
# File 'lib/mathviz.rb', line 571

def to_dot(g)
  super
  (g[@a.node] >> g[node]) [:arrowhead => :normal, :headlabel => @op.to_s, :labeldistance => '2']
  (g[@b.node] >> g[node]) [:arrowhead => :onormal]
  @a.to_dot(g) if (@a.respond_to?(:name) && @a.name.nil?)
  @b.to_dot(g) if (@b.respond_to?(:name) && @b.name.nil?)
end

#to_fObject

Apply the operator to create the derived value.


580
581
582
# File 'lib/mathviz.rb', line 580

def to_f
  @a.to_f.__send__(@op, @b.to_f)
end

#unitsObject

Apply the operator to create the derived units.


585
586
587
# File 'lib/mathviz.rb', line 585

def units
  @a.units.__send__(@op, @b.units)
end