PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/matheclipse-core/src/main/java/org/matheclipse/core/eval/interfaces/AbstractArgMultiple.java

http://symja.googlecode.com/
Java | 261 lines | 196 code | 31 blank | 34 comment | 85 complexity | 6bddc3d2d7f84272607ba7c8402b780e MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, GPL-2.0
  1. package org.matheclipse.core.eval.interfaces;
  2. import org.matheclipse.core.expression.F;
  3. import org.matheclipse.core.interfaces.IAST;
  4. import org.matheclipse.core.interfaces.IComplex;
  5. import org.matheclipse.core.interfaces.IComplexNum;
  6. import org.matheclipse.core.interfaces.IExpr;
  7. import org.matheclipse.core.interfaces.IFraction;
  8. import org.matheclipse.core.interfaces.IInteger;
  9. import org.matheclipse.core.interfaces.INum;
  10. import org.matheclipse.core.interfaces.ISymbol;
  11. import org.matheclipse.core.patternmatching.HashedOrderlessMatcher;
  12. import org.matheclipse.parser.client.SyntaxError;
  13. /**
  14. *
  15. */
  16. public abstract class AbstractArgMultiple extends AbstractArg2 {
  17. @Override
  18. public IExpr evaluate(final IAST ast) {
  19. if (ast.size() > 2) {
  20. IAST temp = evaluateHashs(ast);
  21. if (temp != null) {
  22. return temp;
  23. }
  24. }
  25. if (ast.size() == 3) {
  26. return binaryOperator(ast.get(1), ast.get(2));
  27. }
  28. if (ast.size() > 3) {
  29. final ISymbol sym = ast.topHead();
  30. final IAST result = F.function(sym);
  31. IExpr tres;
  32. IExpr temp = ast.get(1);
  33. boolean evaled = false;
  34. int i = 2;
  35. while (i < ast.size()) {
  36. tres = binaryOperator(temp, ast.get(i));
  37. if (tres == null) {
  38. for (int j = i + 1; j < ast.size(); j++) {
  39. tres = binaryOperator(temp, ast.get(j));
  40. if (tres != null) {
  41. evaled = true;
  42. temp = tres;
  43. ast.remove(j);
  44. break;
  45. }
  46. }
  47. if (tres == null) {
  48. result.add(temp);
  49. if (i == ast.size() - 1) {
  50. result.add(ast.get(i));
  51. } else {
  52. temp = ast.get(i);
  53. }
  54. i++;
  55. }
  56. } else {
  57. evaled = true;
  58. temp = tres;
  59. if (i == (ast.size() - 1)) {
  60. result.add(temp);
  61. }
  62. i++;
  63. }
  64. }
  65. if (evaled) {
  66. if ((result.size() == 2) && ((sym.getAttributes() & ISymbol.ONEIDENTITY) == ISymbol.ONEIDENTITY)) {
  67. return result.get(1);
  68. }
  69. return result;
  70. }
  71. }
  72. return null;
  73. }
  74. public HashedOrderlessMatcher getHashRuleMap() {
  75. return null;
  76. }
  77. public IAST evaluateHashs(final IAST ast) {
  78. HashedOrderlessMatcher hashRuleMap = getHashRuleMap();
  79. if (hashRuleMap == null) {
  80. return null;
  81. }
  82. return hashRuleMap.evaluate(ast);
  83. }
  84. /**
  85. * @param lhs1
  86. * @param lhs2
  87. * @param rhs
  88. * @param condition
  89. * @see org.matheclipse.core.patternmatching.HashedOrderlessMatcher#setUpHashRule(org.matheclipse.core.interfaces.IExpr,
  90. * org.matheclipse.core.interfaces.IExpr,
  91. * org.matheclipse.core.interfaces.IExpr,
  92. * org.matheclipse.core.interfaces.IExpr)
  93. */
  94. public void setUpHashRule(IExpr lhs1, IExpr lhs2, IExpr rhs, IExpr condition) {
  95. getHashRuleMap().setUpHashRule(lhs1, lhs2, rhs, condition);
  96. }
  97. /**
  98. * @param lhs1Str
  99. * @param lhs2Str
  100. * @param rhsStr
  101. * @param conditionStr
  102. * @throws SyntaxError
  103. * @see org.matheclipse.core.patternmatching.HashedOrderlessMatcher#setUpHashRule(java.lang.String,
  104. * java.lang.String, java.lang.String, java.lang.String)
  105. */
  106. public void setUpHashRule(String lhs1Str, String lhs2Str, String rhsStr, String conditionStr) throws SyntaxError {
  107. getHashRuleMap().setUpHashRule(lhs1Str, lhs2Str, rhsStr, conditionStr);
  108. }
  109. /**
  110. * @param lhs1Str
  111. * @param lhs2Str
  112. * @param rhsStr
  113. * @throws SyntaxError
  114. * @see org.matheclipse.core.patternmatching.HashedOrderlessMatcher#setUpHashRule(java.lang.String,
  115. * java.lang.String, java.lang.String)
  116. */
  117. public void setUpHashRule(String lhs1Str, String lhs2Str, String rhsStr) throws SyntaxError {
  118. getHashRuleMap().setUpHashRule(lhs1Str, lhs2Str, rhsStr);
  119. }
  120. @Override
  121. public IExpr binaryOperator(final IExpr o0, final IExpr o1) {
  122. IExpr result = null;
  123. if (o0 instanceof INum) {
  124. // use specialized methods for numeric mode
  125. if (o1 instanceof INum) {
  126. result = e2DblArg((INum) o0, (INum) o1);
  127. } else if (o1.isInteger()) {
  128. result = e2DblArg((INum) o0, F.num((IInteger) o1));
  129. } else if (o1.isFraction()) {
  130. result = e2DblArg((INum) o0, F.num((IFraction) o1));
  131. } else if (o1 instanceof IComplexNum) {
  132. result = e2DblComArg(F.complexNum(((INum) o0).getRealPart()), (IComplexNum) o1);
  133. }
  134. if (result != null) {
  135. return result;
  136. }
  137. return e2ObjArg(o0, o1);
  138. } else if (o1 instanceof INum) {
  139. // use specialized methods for numeric mode
  140. if (o0.isInteger()) {
  141. result = e2DblArg(F.num((IInteger) o0), (INum) o1);
  142. } else if (o0.isFraction()) {
  143. result = e2DblArg(F.num((IFraction) o0), (INum) o1);
  144. } else if (o0 instanceof IComplexNum) {
  145. result = e2DblComArg((IComplexNum) o0, F.complexNum(((INum) o1).getRealPart()));
  146. }
  147. if (result != null) {
  148. return result;
  149. }
  150. return e2ObjArg(o0, o1);
  151. }
  152. if (o0 instanceof IComplexNum) {
  153. // use specialized methods for complex numeric mode
  154. if (o1 instanceof INum) {
  155. result = e2DblComArg((IComplexNum) o0, F.complexNum(((INum) o1).getRealPart()));
  156. } else if (o1.isInteger()) {
  157. result = e2DblComArg((IComplexNum) o0, F.complexNum((IInteger) o1));
  158. } else if (o1.isFraction()) {
  159. result = e2DblComArg((IComplexNum) o0, F.complexNum((IFraction) o1));
  160. } else if (o1 instanceof IComplexNum) {
  161. result = e2DblComArg((IComplexNum) o0, (IComplexNum) o1);
  162. }
  163. if (result != null) {
  164. return result;
  165. }
  166. return e2ObjArg(o0, o1);
  167. } else if (o1 instanceof IComplexNum) {
  168. // use specialized methods for complex numeric mode
  169. if (o0 instanceof INum) {
  170. result = e2DblComArg(F.complexNum(((INum) o0).getRealPart()), (IComplexNum) o1);
  171. } else if (o0.isInteger()) {
  172. result = e2DblComArg(F.complexNum((IInteger) o0), (IComplexNum) o1);
  173. } else if (o0.isFraction()) {
  174. result = e2DblComArg(F.complexNum((IFraction) o0), (IComplexNum) o1);
  175. }
  176. if (result != null) {
  177. return result;
  178. }
  179. return e2ObjArg(o0, o1);
  180. }
  181. if (o0 instanceof IInteger) {
  182. if (o1 instanceof IInteger) {
  183. return e2IntArg((IInteger) o0, (IInteger) o1);
  184. }
  185. if (o1 instanceof IFraction) {
  186. return e2FraArg(F.fraction((IInteger) o0, F.C1), (IFraction) o1);
  187. }
  188. if (o1 instanceof IComplex) {
  189. return e2ComArg(F.complex((IInteger) o0, F.C0), (IComplex) o1);
  190. }
  191. } else if (o0 instanceof IFraction) {
  192. if (o1 instanceof IInteger) {
  193. return e2FraArg((IFraction) o0, F.fraction((IInteger) o1, F.C1));
  194. }
  195. if (o1 instanceof IFraction) {
  196. return e2FraArg((IFraction) o0, (IFraction) o1);
  197. }
  198. if (o1 instanceof IComplex) {
  199. return e2ComArg(F.complex((IFraction) o0), (IComplex) o1);
  200. }
  201. } else if (o0 instanceof IComplex) {
  202. if (o1 instanceof IInteger) {
  203. return eComIntArg((IComplex) o0, (IInteger) o1);
  204. }
  205. if (o1 instanceof IComplex) {
  206. return e2ComArg((IComplex) o0, (IComplex) o1);
  207. }
  208. }
  209. result = e2ObjArg(o0, o1);
  210. if (result != null) {
  211. return result;
  212. }
  213. if (o0 instanceof ISymbol) {
  214. if (o1 instanceof ISymbol) {
  215. return e2SymArg((ISymbol) o0, (ISymbol) o1);
  216. }
  217. }
  218. if (o0 instanceof IAST) {
  219. if (o1 instanceof IInteger) {
  220. return eFunIntArg((IAST) o0, (IInteger) o1);
  221. }
  222. if (o1 instanceof IAST) {
  223. return e2FunArg((IAST) o0, (IAST) o1);
  224. }
  225. }
  226. return null;
  227. }
  228. @Override
  229. public abstract IExpr e2IntArg(final IInteger i0, final IInteger i1);
  230. }