Module: Polymath

Defined in:
lib/polymath.rb,
lib/polymath/math/math.rb,
lib/polymath/nomial/parser.rb,
lib/polymath/nomial/monomial.rb,
lib/polymath/nomial/polynomial.rb

Defined Under Namespace

Modules: Nomial Classes: Math

Class Method Summary collapse

Class Method Details

.command_line(options) ⇒ Object

Returns nil.

Parameters:

  • options

    The parsed options

Returns:

  • nil



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/polymath.rb', line 76

def self.command_line(options)
  raise "No polynomial given" unless options[:polynomial]

  polynomial = Polymath.polynomial(options[:polynomial])
  math       = Polymath::Math.new

  polynomial.cleanup!

  if options[:factor]
    zeroes = math.factor_rational_zeroes(polynomial: polynomial)
  end

  #output
  puts options.map { |opt, value|
    next unless value
    case opt
    when :verbose
      polynomial.to_s
    when :factor
      "zeroes: #{zeroes}" if options[:factor]
    when :analyze
      {
        deg:   polynomial.deg,
        class: polynomial.classification.map { |t, c| c }.compact.join(" ")
      }.map { |a,b| "#{a}: #{b}" }.join("\n")
    end
  }.compact.join("\n")

end

.parse_options(args) ⇒ Object

Returns a hash of parsed options.

Parameters:

  • args

    The arguments

Returns:

  • a hash of parsed options



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/polymath.rb', line 17

def self.parse_options(args)

  options = {
    :polynomial => nil,
    :verbose    => true,
    :analyze    => false,
    :factor     => false
  }

  parser = OptionParser.new { |opts|
    opts.banner = "Usage: polymath [options] <polynomial>"

    opts.on("-f", "--factor", "factor the polynomial expression") { |f|
      options[:factor] = f
    }

    opts.on("-a", "--analyze", "analyze the polynomial expression") { |a|
      options[:analyze] = a
    }

    opts.on("-q", "--quiet", "only output what is specified") { |q|
      options[:verbose] = ! q
    }

    opts.on("-r", "--random [N]", "generate a random polynomial") { |n|
      options[:polynomial] =  if n
                                Nomial::random_polynomial(Integer(n))
                              else
                                Nomial::random_polynomial
                              end
    }

    opts.on_tail("-h", "--help", "Show this message") {
      puts opts
      exit
    }

    opts.on_tail("--version", "Show version") {
      puts self::Version.join('.')
      exit
    }
  }

  parser.parse!(args)
  options[:polynomial] = args.pop unless options[:polynomial]
  options
end

.polynomial(exp) ⇒ Object



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

def self.polynomial(exp)
  Polymath::Nomial::Polynomial.new(exp)
end