Class: Mathmas::Power

Inherits:
Expression show all
Defined in:
lib/mathmas/core/expression.rb

Instance Method Summary collapse

Methods inherited from Expression

#initialize

Methods included from Basic

#*, #**, #+, #-, #/, #coerce, #to_iruby

Constructor Details

This class inherits a constructor from Mathmas::Expression

Instance Method Details

#exec(args) ⇒ Object



102
103
104
# File 'lib/mathmas/core/expression.rb', line 102

def exec(args)
  super(args).inject(:**)
end

#to_sObject



79
80
81
82
83
84
85
# File 'lib/mathmas/core/expression.rb', line 79

def to_s
  arr = @args.map do |arg|
    str = arg.to_s
    (arg.is_a?(Mathmas::Expression) ? "(" + str + ")" : str)
  end
  arr.join("^")
end

#to_tex(numerator = Number.new(1)) ⇒ Object

TODO: display root when args.is_a? Rational



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/mathmas/core/expression.rb', line 88

def to_tex(numerator = Number.new(1))
  strs = @args.map do |arg|
    str = arg.to_tex
    (arg.is_a?(Mathmas::Expression) ? "(" + str + ")" : str)
  end

  if @args[1].is_a?(Number) && @args[1].num < 0
    denominator = (@args[1].num == -1 ? strs[0] : strs[0] + "^" + (-@args[1].num).to_s)
    "\\frac{#{ numerator.to_tex }}{#{ denominator }}"
  else
    strs.join("^")
  end
end