Class: MathViz

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

Overview

Top level object.

Defined Under Namespace

Modules: Graphable, Measurable, Measured, NumericOperations, Units Classes: Constant, Input, Operation, Term, Unit

Constant Summary collapse

VERSION =

RubyGem version

'1.1.0'
Infinity =

Something to return instead of dividing by zero, etc.

1.0/0

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = nil, bind = nil, &proc) ⇒ MathViz

  • base name of the output file. If omitted(falsy) it will use the top level program name.

  • Binding object, as if from ‘binding’

  • A proc which returns a binding.

If bind is passed, the proc will not be executed. If bind is falsy, the proc will be executed and it’s return value stored.

The proc is evaluated in the context of the new MathViz instance, making #const and #input directly available



18
19
20
21
# File 'lib/mathviz.rb', line 18

def initialize(name = nil, bind = nil, &proc)
  @name = name || File.basename($PROGRAM_NAME, '.rb')
  @env = bind || instance_eval(&proc)
end

Class Method Details

.default_termObject

internal method



84
85
86
# File 'lib/mathviz.rb', line 84

def self.default_term
  @@default_term ||= Constant
end

.term(x) ⇒ Object

Turn the object into a MathViz::Term (MathViz::Constant) if isn’t already a MathViz::Term. This allows for operator parameters to be primitive values without needing MathViz#const, MathViz#input, or units.



89
90
91
92
93
94
95
# File 'lib/mathviz.rb', line 89

def self.term(x)
  if (x.kind_of?(MathViz::Term))
    x
  else
    MathViz::Constant.new(x)
  end
end

Instance Method Details

#And(desc) ⇒ Object

Comment which reads a little better than repetition.



80
81
# File 'lib/mathviz.rb', line 80

def And(desc)
end

#binop(op) ⇒ Object

Define op as a binary operator



40
41
42
43
# File 'lib/mathviz.rb', line 40

def binop(op)
  MathViz::Term.binop op
  MathViz::Unit.binop op
end

#const(x) ⇒ Object

Convert a basic value (typically Numeric) into a MathViz::Term (MathViz::Constant)



24
25
26
# File 'lib/mathviz.rb', line 24

def const(x)
  MathViz::Constant.new(x)
end

#dotObject

Save a Graphviz .dot file in the current directory, with name specified in the constructor. Triggers most of the actual processsing.



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/mathviz.rb', line 51

def dot
  MathViz::Term.name_terms!(@env)
  #puts MathViz::Term.list_terms(@env).map {|t| t.long}
  graph = GraphvizR.new @name
  MathViz::Term.list_terms(@env).flat_map(&:collapse).each {|t|
    t.to_dot(graph)
  }

  filename = @name + '.dot'
  graph.output(filename, 'dot')
  puts "Wrote #{filename}"
end

#Given(desc) ⇒ Object

Comment that identifies a set of constants. Subsequent measured values will be marked as #const



70
71
72
# File 'lib/mathviz.rb', line 70

def Given(desc)
  @@default_term = Constant
end

#input(x) ⇒ Object

Convert a basic value (typically Numeric) into a MathViz::Term (MathViz::Input)



29
30
31
# File 'lib/mathviz.rb', line 29

def input(x)
  MathViz::Input.new(x)
end

#new_units(*units) ⇒ Object

Define new units (instance methods) on module MathViz::Units (where they will be picked up by everything including the module) Defined methods are essentialy aliases for #unit(name); see MathViz::Measurable / MathViz::Measured



35
36
37
# File 'lib/mathviz.rb', line 35

def new_units(*units)
  MathViz::Units.new_units(*units)
end

#Then(desc) ⇒ Object

Comment that identifies a section of calculations on the inputs and constants.



75
76
77
# File 'lib/mathviz.rb', line 75

def Then(desc)
  @@default_term = Constant
end

#unop(op) ⇒ Object

Define op as unary operator



46
47
48
# File 'lib/mathviz.rb', line 46

def unop(op)
  MathViz::Term.unop op
end

#When(desc) ⇒ Object

Comment that identifies a set of inputs. Subsequent measured values will be marked as #input



65
66
67
# File 'lib/mathviz.rb', line 65

def When(desc)
  @@default_term = Input
end