java 는 Expression 4j 조작 과 수학 공식 을 바탕 으로
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);
}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.