OSDN Git Service

fix multiple operator representation
authorTomohiro Nishimura <tomohiro68@gmail.com>
Thu, 18 Mar 2010 01:04:27 +0000 (10:04 +0900)
committerTomohiro Nishimura <tomohiro68@gmail.com>
Thu, 18 Mar 2010 01:44:15 +0000 (10:44 +0900)
lib/mint/builder/expression_tree.rb
spec/builder_spec.rb
spec/expression_spec.rb

index 9cd604e..e13bddc 100644 (file)
@@ -64,16 +64,11 @@ module Mint
       @operator = :*
     end
     def to_latex_local
-      # FIXME: too dirty
-      if right.instance_of?(LiteralNode) ||
-         right.instance_of?(RootNode)    ||
-         right.instance_of?(FactorialNode)
-
-        unless right.to_s.match(/\A[-\.\d]+\z/)
-          return "#{left.to_latex}#{right.to_latex}"
-        end
+      _right = right.to_latex
+      if _right.match(/\A\(*-?\d/)
+        return "#{left.to_latex} \\times #{_right}"
       end
-      "#{left.to_latex} \\times #{right.to_latex}"
+      "#{left.to_latex}#{_right}"
     end
   end
 
index 838bf68..5f12161 100644 (file)
@@ -12,7 +12,7 @@ module Mint
       before(:all) do
         @original = '(x^-2 + 4)(y^3 --2)'
         @normalized = '(x^-2 + 4) * (y^3 - (-2))'
-        @latex = '(x^-2 + 4) \times (y^3 - (-2))'
+        @latex = '(x^-2 + 4)(y^3 - (-2))'
       end
 
       subject { Builder.build(@original) }
index dda8b6b..27755cd 100644 (file)
@@ -41,9 +41,9 @@ module Mint
         [
           '1 / (x + 4)(x - 2) div 5',
           '1 / ((x + 4) * (x - 2) / 5)',
-          '\frac{1}{(x + 4) \times (x - 2) \div 5}',
           '1 / ((x + 4) * (x - 2) div 5)',
-          '\frac{1}{(x + 4) \times (x - 2) \divide 5}',
+          '\frac{1}{(x + 4)(x - 2) \div 5}',
+          '\frac{1}{(x + 4)(x - 2) \divide 5}',
         ],
         [
           '25.33 - (-0.99)',
@@ -55,49 +55,63 @@ module Mint
         [
           '(-83.68) * 10.23',
           '(-83.68) * 10.23',
-          '(-83.68) \times 10.23',
           '(-83.68) * 10.23',
           '(-83.68) \times 10.23',
+          '(-83.68) \times 10.23',
         ],
         [
           'x^2 + 7x - 8',
           'x^2 + 7 * x - 8',
-          'x^2 + 7x - 8',
           'x^2 + 7 * x - 8',
           'x^2 + 7x - 8',
+          'x^2 + 7x - 8',
         ],
         [
           '3.4 / 54 div 2 / 6',
           '3.4 / 54 / (2 / 6)',
-          '\frac{3.4}{54} \div \frac{2}{6}',
           '3.4 / 54 div (2 / 6)',
+          '\frac{3.4}{54} \div \frac{2}{6}',
           '\frac{3.4}{54} \divide \frac{2}{6}',
         ],
         [
+          '5 * (-2)',
+          '5 * (-2)',
+          '5 * (-2)',
+          '5 \times (-2)',
+          '5 \times (-2)',
+        ],
+        [
+          '5 * (-2x)',
+          '5 * ((-2) * x)',
+          '5 * ((-2) * x)',
+          '5 \times ((-2)x)',
+          '5 \times ((-2)x)',
+        ],
+        [
           '5x * 2y',
           '5 * x * 2 * y',
-          '5x \times 2y',
           '5 * x * 2 * y',
           '5x \times 2y',
+          '5x \times 2y',
         ],
         [
           '5x^2 * 2y^3',
           '5 * x^2 * 2 * y^3',
-          '5x^2 \times 2y^3',
           '5 * x^2 * 2 * y^3',
           '5x^2 \times 2y^3',
+          '5x^2 \times 2y^3',
         ],
         [
           '2root(2) * 35',
           '2 * sqrt(2) * 35',
-          '2\sqrt{2} \times 35',
           '2 * sqrt(2) * 35',
           '2\sqrt{2} \times 35',
+          '2\sqrt{2} \times 35',
         ],
-        [ '2i', '2 * %i', '2\i', '2 * %i', '2\i', ],
-        [ '2e', '2 * %e', '2\e', '2 * %e', '2\e', ],
-        [ '2pi', '2 * %pi', '2\pi', '2 * %pi', '2\pi', ],
-      ].each do |original, maxima, latex, normalize, ascii_math_ml|
+        [ '2i', '2 * %i', '2 * %i', '2\i', '2\i', ],
+        [ '2e', '2 * %e', '2 * %e', '2\e', '2\e', ],
+        [ '2pi', '2 * %pi', '2 * %pi', '2\pi', '2\pi', ],
+      ].each do |original, maxima, normalize, latex, ascii_math_ml|
 
         context original do
 
@@ -123,14 +137,14 @@ module Mint
             subject.to_latex.should == @latex
           end
 
-          it 'normalize' do
-            subject.normalize.should == @normalize
-          end
-
           it 'ascii_math_ml' do
             subject.to_ascii_math_ml.should == @ascii_math_ml
           end
 
+          it 'normalize' do
+            subject.normalize.should == @normalize
+          end
+
           it 'to_s as normalize' do
             subject.to_s.should == @normalize
           end