java 는 Expression 4j 조작 과 수학 공식 을 바탕 으로

8689 단어
import java.util.List;

import com.graphbuilder.math.ExpressionTree;

import fr.expression4j.basic.MathematicalElement;
import fr.expression4j.basic.OperatorManager;
import fr.expression4j.core.Catalog;
import fr.expression4j.core.Expression;
import fr.expression4j.core.ExpressionModel;
import fr.expression4j.core.Parameters;
import fr.expression4j.core.exception.EvalException;
import fr.expression4j.core.exception.ParametersException;
import fr.expression4j.core.exception.ParsingException;
import fr.expression4j.core.predefine.AbstractFunction;
import fr.expression4j.factory.ExpressionFactory;
import fr.expression4j.factory.NumberFactory;
import fr.expression4j.util.ParameterUtil;

//java  Expression4j         
//http://sourceforge.net/projects/expression4j/files/
public class Expression4j {
	
	static class Factoriel extends AbstractFunction {
		 /**
		  * compute the factoriel of a number
		  * @param x number to compute
		  * @return factoriel of the number.
		  */
		 private double fact(double x) {
			 if (x == 0) {
				 return 1;
			 }
			 return x * fact(x-1);
		 }

		 /**
		  * evaluation method called by the Expression4j when needed.
		  * 
		  * @param parameters parameter given to the function for the evaluation.
		  */
		 public MathematicalElement evaluate(Parameters parameters) 
			throws EvalException {
			try {
				MathematicalElement me = parameters.getParameter("x");
				double tmpValue = me.getRealValue(); 
				if (tmpValue < 0) {
					throw new EvalException(
						"Cannot evaluate fact of " + 
						"negativ number.");
				}
				double result = fact(tmpValue);
				return NumberFactory.createReal(result);
			}
			catch (ParametersException pe) {
				throw new EvalException("Cannot evaluate fact(x). " + pe);
			}
		}

		public MathematicalElement evaluate(OperatorManager operatorManager,
					Parameters parameters) throws EvalException {
			return evaluate(parameters);
		}

		public Catalog getCatalog() {
			return ExpressionFactory.getCatalog();
		}

		/**
		 * get the function name
		 */
		public String getName() {
			return "fact";
		}

		/**
		 * get parameter list of the function.
		 */
		public List getParameters() {
			//this util method get one parameter "x".
			return ParameterUtil.generateXParameters();
		}

		public ExpressionModel getExpressionModel() {
			// TODO Auto-generated method stub
			return  null;
		}
	}
	public static void main(String[] args) {
		//test001();
		//test002();
		//test003();
		//test004();
		//test005();
		test006();
	}

	private static void test006() {
		try {
			//y=ax2+bx+c       
			Expression expression = ExpressionFactory.createExpression("y(a,b,c,x)=a*x^2+b*x+c");
			System.out.println("Expression name: " + expression.getName());
			
			System.out.println("Expression parameters: " + expression.getParameters());
			
			MathematicalElement element_a=NumberFactory.createReal(1);
			MathematicalElement element_b=NumberFactory.createReal(4);
			MathematicalElement element_c=NumberFactory.createReal(4);
			MathematicalElement element_x=NumberFactory.createReal(1);
			Parameters parameters=ExpressionFactory.createParameters();
			parameters.addParameter("a", element_a);
			parameters.addParameter("b", element_b);
			parameters.addParameter("c", element_c);
			parameters.addParameter("x", element_x);
			System.out.println("Value of expression:  y for x=1 a=1 b=4 c=4 :" +expression.evaluate(parameters).getRealValue());
			
			//y=x^2+6x+9=(x+3)^2
			element_a=NumberFactory.createReal(1);
			element_b=NumberFactory.createReal(6);
			element_c=NumberFactory.createReal(9);
			element_x=NumberFactory.createReal(1);
			parameters=ExpressionFactory.createParameters();
			parameters.addParameter("a", element_a);
			parameters.addParameter("b", element_b);
			parameters.addParameter("c", element_c);
			parameters.addParameter("x", element_x);
			System.out.println("Value of expression:  y for x=1 a=1 b=6 c=9 :" +expression.evaluate(parameters).getRealValue());
			
			
		} catch (Exception e) {
			e.printStackTrace();
		} 
	}

	private static void test005() {
		try {
			//define a specific catalog (not necessary,
			//we can use the default catalog instead)
			Catalog catalog = ExpressionFactory.createCatalog("catalog");
			//add the user define function to the catalog
			catalog.addExpression(new Factoriel());

			//create the expression who use the user define function
			//dont forget to put the catalog of the user define function
			//if default catalog is not used.
			Expression expression1 = 
				ExpressionFactory.createExpression("f(x)=fact(x+1)",catalog);
			System.out.println("Expression name: " + expression1.getName());
			System.out.println("Expression : " + expression1);
			System.out.println("Expression parameters: " + 
				expression1.getParameters());

			//compute a value
			MathematicalElement me1 = NumberFactory.createReal(5);
			Parameters parameters = ExpressionFactory.createParameters();
			parameters.addParameter("x",me1);
			System.out.println("Value of expression h :" + 
				expression1.evaluate(parameters));
		}
		catch (Exception e) {
			System.out.println("Error: " + e);
		}
		
	}

	private static void test004() {
		try {
			Expression expression = ExpressionFactory
				.createExpression("k()=abs(-3)");
			System.out.println("Expression name: " + expression.getName());
			System.out.println("Value of expression:" + 
				expression.evaluate(null).getRealValue());
		}
		catch (Exception e) {
			System.out.println("Error: " + e);
		}
	}

	private static void test003() {
		try {
			Expression expression1 = ExpressionFactory
				.createExpression("f(x,y)=2.4e-2*x-3.2*y");
			Expression expression2 = ExpressionFactory
				.createExpression("g(x,y)=4*f(x+3,f(y*2,x/2))+3");
			System.out.println("Expression name: " + expression2.getName());
			System.out.println("Expression : " + expression2);
			System.out.println("Expression parameters: " + 
				expression2.getParameters());

			MathematicalElement me1 = NumberFactory.createReal(2);
			MathematicalElement me2 = NumberFactory.createReal(4);
			Parameters parameters = ExpressionFactory.createParameters();
			parameters.addParameter("x",me1);
			parameters.addParameter("y",me2);
			System.out.println("Value of expression g for x=2 and y = 4:" + 
				expression2.evaluate(parameters).getRealValue());

			me1 = NumberFactory.createReal(3);
			me2 = NumberFactory.createReal(2);
			parameters.addParameter("x",me1);
			parameters.addParameter("y",me2);
			System.out.println("Value of expression g for x=3 and y = 2:" + 
				expression2.evaluate(parameters).getRealValue());
		}
		catch (Exception e) {
			System.out.println("Error: " + e);
		}
	}

	private static void test002() {
		try {
			Expression expression = ExpressionFactory.createExpression("f()=2.4e-2");
			
			System.out.println("Expression name: " + expression.getName());
			
			System.out.println("Value of expression:" + expression.evaluate(null).getRealValue());
			}
		catch (Exception e) {
			System.out.println("Error: " + e);
			}
	}

	private static void test001() {
		try {
			
			Expression expression1 = ExpressionFactory.createExpression("f(x,y)=2*x-3*y");
			Expression expression2 = ExpressionFactory.createExpression("g(x,y)=4*f(x,y)+3");

			System.out.println("Expression name: " + expression2.getName());
			System.out.println("Expression : " + expression2);
			System.out.println("Expression parameters: " + expression2.getParameters());

			MathematicalElement me1 = NumberFactory.createReal(2);
			MathematicalElement me2 = NumberFactory.createReal(4);
			Parameters parameters = ExpressionFactory.createParameters();
			parameters.addParameter("x",me1);
			parameters.addParameter("y",me2);

			System.out.println("Value of expression g for x=2 and y = 4:" + 
			expression2.evaluate(parameters).getRealValue());

			me1 = NumberFactory.createReal(3);
			me2 = NumberFactory.createReal(2);
			parameters.addParameter("x",me1);
			parameters.addParameter("y",me2);
			System.out.println("Value of expression g for x=3 and y = 2:" + 
			expression2.evaluate(parameters).getRealValue());
		}
		catch (Exception e) {
			System.out.println("Error: " + e);
		}
	}
	

}

좋은 웹페이지 즐겨찾기