Derive: Unterschied zwischen den Versionen

Aus JACK Wiki
Zur Navigation springen Zur Suche springen
Keine Bearbeitungszusammenfassung
Zeile 20: Zeile 20:
<span class="mw-customtoggle-myDivision">[Anzeigen]</span>
<span class="mw-customtoggle-myDivision">[Anzeigen]</span>
<syntaxhighlight lang="java" class="mw-collapsible mw-collapsed" id="mw-customcollapsible-myDivision">
<syntaxhighlight lang="java" class="mw-collapsible mw-collapsed" id="mw-customcollapsible-myDivision">
@Before
@BeforeClass
public void beforeTest() {
public static void beforeTest() throws FunctionNotImplementedException, UndefinedFillInVariableException,
  placeFillInVariable(1, OMParser.parse("x"));
UndefinedExerciseVariableException, ParserException, ErroneousFillInVariableException,
  placeFillInVariable(2, OMParser.parse("0.5"));
ErroneousExerciseVariableException {
deriveFillInVariableMap.put(1, ExpressionParser.parse("x", null, null));
  placeExerciseVariable("a", OMParser.parse("x"));
deriveFillInVariableMap.put(2, ExpressionParser.parse("0.5", null, null));
  placeExerciseVariable("b", OMParser.parse("0.5"));
 
deriveExerciseVariableMap.put("a", ExpressionParser.parse("x", null, null));
deriveExerciseVariableMap.put("b", ExpressionParser.parse("0.5", null, null));
}
 
@Test
public void testDerive1() {
assertEquals(ExpressionParser.parse("2*x", null, null),
Evaluator.evaluate("derive('x^2','x')", deriveExerciseVariableMap, deriveFillInVariableMap));
}
 
@Test
public void testDerive2() {
assertEquals(ExpressionParser.parse("0", null, null),
Evaluator.evaluate("derive('x^2','y')", deriveExerciseVariableMap, deriveFillInVariableMap));
}
 
@Test
public void testDerive3() {
assertEquals(ExpressionParser.parse("2*x+2", null, null),
Evaluator.evaluate("derive('x^2 + 2x','x')", deriveExerciseVariableMap, deriveFillInVariableMap));
}
 
@Test
public void testDerive4() {
assertEquals(ExpressionParser.parse("x", null, null),
Evaluator.evaluate("derive('0.5*x^2','x')", deriveExerciseVariableMap, deriveFillInVariableMap));
}
 
@Test
public void testDerive5() {
assertEquals(ExpressionParser.parse("x", null, null),
Evaluator.evaluate("derive('.5*x^2','x')", deriveExerciseVariableMap, deriveFillInVariableMap));
}
 
@Test
public void testDerive6() {
assertEquals(ExpressionParser.parse("2*x+1", null, null),
Evaluator.evaluate("derive('x^2+x+3','x')", deriveExerciseVariableMap, deriveFillInVariableMap));
}
}
 
@Test
@Test
public void testDerive() {
public void testDerive7() {
  assertTrue("'2*x'".equals(getEvaluator().getResultAsEvaluatorString("derive('x^2','x')")));
OMOBJ expected = ExpressionParser.parse("2*(y^2*x)", null, null);
  assertTrue("'0'".equals(getEvaluator().getResultAsEvaluatorString("derive('x^2','y')")));
assertEquals(expected.getOMA(), Evaluator
.evaluate("derive('(x*y)^2','x')", deriveExerciseVariableMap, deriveFillInVariableMap).getOMA());
  assertTrue("'2+2*x'".equals(getEvaluator().getResultAsEvaluatorString("derive('x^2 + 2x','x')")));
  assertTrue("'x'".equals(getEvaluator().getResultAsEvaluatorString("derive('0.5*x^2','x')")));
  assertTrue("'x'".equals(getEvaluator().getResultAsEvaluatorString("derive('.5*x^2','x')")));
  assertTrue("'1+2*x'".equals(getEvaluator().getResultAsEvaluatorString("derive('x^2+x+3','x')")));
  assertTrue("'2*x*y^2'".equals(getEvaluator().getResultAsEvaluatorString("derive('(x*y)^2','x')")));
}
}
 
@Test
@Test
public void testDeriveWithInput() {
public void testDeriveWithInput1() {
  assertTrue("'x'".equals(getEvaluator().getResultAsEvaluatorString("derive('0.5*x^2 + 3','x')")));
assertEquals(ExpressionParser.parse("x", null, null),
  assertTrue("'2+2*x'".equals(getEvaluator().getResultAsEvaluatorString("derive('[pos=1]^2 + 2[pos=1] + 4','x')")));
Evaluator.evaluate("derive('0.5*x^2 + 3','x')", deriveExerciseVariableMap, deriveFillInVariableMap));
}
}
 
@Test  
@Test
public void testDeriveWithExpressions(){
public void testDeriveWithInput2() {
  assertTrue("'1'".equals(getEvaluator().getResultAsEvaluatorString("'derive(derive('0.5*x^2', 'x')', 'x'")));
assertEquals(ExpressionParser.parse("2*x+2", null, null), Evaluator.evaluate(
"derive('[pos=1]^2 + 2*[pos=1] + 4','x')", deriveExerciseVariableMap, deriveFillInVariableMap));
}
}


@Test
@Test
public void testDeriveWithVariables() {
public void testDeriveWithExpressions() {
  assertTrue("'x'".equals(getEvaluator().getResultAsEvaluatorString("derive('0.5*x^2 + 3','x')")));
assertEquals(ExpressionParser.parse("x", null, null), Evaluator.evaluate("derive(derive('(1/6)*x^3', 'x'),'x')",
  assertTrue("'2+2*x'".equals(getEvaluator().getResultAsEvaluatorString("derive('[var=a]^2 + 2[var=a] + 4','x')")));
deriveExerciseVariableMap, deriveFillInVariableMap));
}
}
 
@Test
@Test
public void testDeriveWithONECharacter(){
public void testDeriveWithVariables1() {
  assertTrue("'1'".equals(getEvaluator().getResultAsEvaluatorString("derive(a, a)")));
assertEquals(ExpressionParser.parse("x", null, null),
Evaluator.evaluate("derive('0.5*x^2 + 3','x')", deriveExerciseVariableMap, deriveFillInVariableMap));
}
}
 
@Test(expected=InvalidCASFunctionArgumentException.class)
@Test
public void testDeriveWithEmptyStringArguments(){
public void testDeriveWithVariables2() {
  getEvaluator().getResultAsEvaluatorString("derive('', '')");
assertEquals(ExpressionParser.parse("2*x+2", null, null), Evaluator.evaluate(
"derive('[var=a]^2 + 2*[var=a] + 4','x')", deriveExerciseVariableMap, deriveFillInVariableMap));
}
}
 
@Test(expected=InvalidCASFunctionArgumentException.class)
@Test
public void testDeriveWithEmptyArgument(){
public void testDeriveWithONECharacter() {
  getEvaluator().getResultAsEvaluatorString("derive()");
assertEquals(ExpressionParser.parse("1", null, null),
Evaluator.evaluate("derive(a, a)", deriveExerciseVariableMap, deriveFillInVariableMap));
}
 
@Test(expected = FunctionInvalidArgumentTypeException.class)
public void testDeriveWithEmptyStringArguments() {
Evaluator.evaluate("derive('', '')", deriveExerciseVariableMap, deriveFillInVariableMap);
fail();
}
 
@Test(expected = FunctionInvalidNumberOfArgumentsException.class)
public void testDeriveWithEmptyArgument() {
Evaluator.evaluate("derive()", deriveExerciseVariableMap, deriveFillInVariableMap);
fail();
}
}
 
@Test(expected=UndefinedExerciseVariableException.class)
@Test(expected = UndefinedExerciseVariableException.class)
public void testDeriveWithoutExerciseVariable() {
public void testDeriveWithoutExerciseVariable() {
  getEvaluator().getResultAsEvaluatorString("derive([var=j], [var=j])");
Evaluator.evaluate("derive([var=j], [var=j])", deriveExerciseVariableMap, deriveFillInVariableMap);
fail();
}
}
 
@Test(expected=UndefinedFillInVariableException.class)
@Test(expected = UndefinedFillInVariableException.class)
public void testDeriveWithoutInput() {
public void testDeriveWithoutInput() {
  getEvaluator().getResultAsEvaluatorString("derive([pos=42], [pos=42])");
Evaluator.evaluate("derive([pos=42], [pos=42])", deriveExerciseVariableMap, deriveFillInVariableMap);
fail();
}
}
</syntaxhighlight>
</syntaxhighlight>

Version vom 18. September 2017, 10:56 Uhr

Beschreibung

Die Funktion derive leitet eine Funktion ab. Sie benötigt zwei Strings, den Ausdruck der abgeleitet werden soll und die Variable nach der abgeleitet werden soll. Die Funktion gibt die Ableitung zurück.

Syntax

derive(String term, String variable)

Parameter

  • term - die Funktion die abgeleitet werden soll
  • variable - die Variable nach der abgeleitet werden soll

Return Value

  • Gibt den Wert zurück, der die abgeleitete Funktion darstellt.

Beispiele

derive('x^2+2x+3','x')    --> returns '2x+2'
derive('x^2+2x+3','y')    --> returns '0'

JUnit Tests

[Anzeigen]

@BeforeClass
public static void beforeTest() throws FunctionNotImplementedException, UndefinedFillInVariableException,
		UndefinedExerciseVariableException, ParserException, ErroneousFillInVariableException,
		ErroneousExerciseVariableException {
	deriveFillInVariableMap.put(1, ExpressionParser.parse("x", null, null));
	deriveFillInVariableMap.put(2, ExpressionParser.parse("0.5", null, null));

	deriveExerciseVariableMap.put("a", ExpressionParser.parse("x", null, null));
	deriveExerciseVariableMap.put("b", ExpressionParser.parse("0.5", null, null));
}

@Test
public void testDerive1() {
	assertEquals(ExpressionParser.parse("2*x", null, null),
			Evaluator.evaluate("derive('x^2','x')", deriveExerciseVariableMap, deriveFillInVariableMap));
}

@Test
public void testDerive2() {
	assertEquals(ExpressionParser.parse("0", null, null),
			Evaluator.evaluate("derive('x^2','y')", deriveExerciseVariableMap, deriveFillInVariableMap));
}

@Test
public void testDerive3() {
	assertEquals(ExpressionParser.parse("2*x+2", null, null),
			Evaluator.evaluate("derive('x^2 + 2x','x')", deriveExerciseVariableMap, deriveFillInVariableMap));
}

@Test
public void testDerive4() {
	assertEquals(ExpressionParser.parse("x", null, null),
			Evaluator.evaluate("derive('0.5*x^2','x')", deriveExerciseVariableMap, deriveFillInVariableMap));
}

@Test
public void testDerive5() {
	assertEquals(ExpressionParser.parse("x", null, null),
			Evaluator.evaluate("derive('.5*x^2','x')", deriveExerciseVariableMap, deriveFillInVariableMap));
}

@Test
public void testDerive6() {
	assertEquals(ExpressionParser.parse("2*x+1", null, null),
			Evaluator.evaluate("derive('x^2+x+3','x')", deriveExerciseVariableMap, deriveFillInVariableMap));
}

@Test
public void testDerive7() {
	OMOBJ expected = ExpressionParser.parse("2*(y^2*x)", null, null);
	assertEquals(expected.getOMA(), Evaluator
			.evaluate("derive('(x*y)^2','x')", deriveExerciseVariableMap, deriveFillInVariableMap).getOMA());
}

@Test
public void testDeriveWithInput1() {
	assertEquals(ExpressionParser.parse("x", null, null),
			Evaluator.evaluate("derive('0.5*x^2 + 3','x')", deriveExerciseVariableMap, deriveFillInVariableMap));
}

@Test
public void testDeriveWithInput2() {
	assertEquals(ExpressionParser.parse("2*x+2", null, null), Evaluator.evaluate(
			"derive('[pos=1]^2 + 2*[pos=1] + 4','x')", deriveExerciseVariableMap, deriveFillInVariableMap));
}

@Test
public void testDeriveWithExpressions() {
	assertEquals(ExpressionParser.parse("x", null, null), Evaluator.evaluate("derive(derive('(1/6)*x^3', 'x'),'x')",
			deriveExerciseVariableMap, deriveFillInVariableMap));
}

@Test
public void testDeriveWithVariables1() {
	assertEquals(ExpressionParser.parse("x", null, null),
			Evaluator.evaluate("derive('0.5*x^2 + 3','x')", deriveExerciseVariableMap, deriveFillInVariableMap));
}

@Test
public void testDeriveWithVariables2() {
	assertEquals(ExpressionParser.parse("2*x+2", null, null), Evaluator.evaluate(
			"derive('[var=a]^2 + 2*[var=a] + 4','x')", deriveExerciseVariableMap, deriveFillInVariableMap));
}

@Test
public void testDeriveWithONECharacter() {
	assertEquals(ExpressionParser.parse("1", null, null),
			Evaluator.evaluate("derive(a, a)", deriveExerciseVariableMap, deriveFillInVariableMap));
}

@Test(expected = FunctionInvalidArgumentTypeException.class)
public void testDeriveWithEmptyStringArguments() {
	Evaluator.evaluate("derive('', '')", deriveExerciseVariableMap, deriveFillInVariableMap);
	fail();
}

@Test(expected = FunctionInvalidNumberOfArgumentsException.class)
public void testDeriveWithEmptyArgument() {
	Evaluator.evaluate("derive()", deriveExerciseVariableMap, deriveFillInVariableMap);
	fail();
}

@Test(expected = UndefinedExerciseVariableException.class)
public void testDeriveWithoutExerciseVariable() {
	Evaluator.evaluate("derive([var=j], [var=j])", deriveExerciseVariableMap, deriveFillInVariableMap);
	fail();
}

@Test(expected = UndefinedFillInVariableException.class)
public void testDeriveWithoutInput() {
	Evaluator.evaluate("derive([pos=42], [pos=42])", deriveExerciseVariableMap, deriveFillInVariableMap);
	fail();
}

Hinweise

  • Die Funktion derive kann z.B. mit der Funktion equalsExpr abgefragt werden:
equalsExpr(derive('x^2+x+3','x'),'2x+1')