Derive: Unterschied zwischen den Versionen
Zur Navigation springen
Zur Suche springen
Keine Bearbeitungszusammenfassung |
Keine Bearbeitungszusammenfassung |
||
Zeile 16: | Zeile 16: | ||
derive('x^2+2x+3','y') --> returns '0' | derive('x^2+2x+3','y') --> returns '0' | ||
====JUnit Tests==== | |||
<syntaxhighlight lang="java" class="mw-collapsible mw-collapsed"> | |||
@Before | |||
public void beforeTest() { | |||
placeFillInVariable(1, OMParser.parse("x")); | |||
placeFillInVariable(2, OMParser.parse("0.5")); | |||
placeExerciseVariable("a", OMParser.parse("x")); | |||
placeExerciseVariable("b", OMParser.parse("0.5")); | |||
} | |||
@Test | |||
public void testDerive() { | |||
assertTrue("'2*x'".equals(getEvaluator().getResultAsEvaluatorString("derive('x^2','x')"))); | |||
assertTrue("'0'".equals(getEvaluator().getResultAsEvaluatorString("derive('x^2','y')"))); | |||
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 | |||
public void testDeriveWithInput() { | |||
assertTrue("'x'".equals(getEvaluator().getResultAsEvaluatorString("derive('0.5*x^2 + 3','x')"))); | |||
assertTrue("'2+2*x'".equals(getEvaluator().getResultAsEvaluatorString("derive('[pos=1]^2 + 2[pos=1] + 4','x')"))); | |||
} | |||
@Test | |||
public void testDeriveWithExpressions(){ | |||
assertTrue("'1'".equals(getEvaluator().getResultAsEvaluatorString("'derive(derive('0.5*x^2', 'x')', 'x'"))); | |||
} | |||
@Test | |||
public void testDeriveWithVariables() { | |||
assertTrue("'x'".equals(getEvaluator().getResultAsEvaluatorString("derive('0.5*x^2 + 3','x')"))); | |||
assertTrue("'2+2*x'".equals(getEvaluator().getResultAsEvaluatorString("derive('[var=a]^2 + 2[var=a] + 4','x')"))); | |||
} | |||
@Test | |||
public void testDeriveWithONECharacter(){ | |||
assertTrue("'1'".equals(getEvaluator().getResultAsEvaluatorString("derive(a, a)"))); | |||
} | |||
@Test(expected=InvalidCASFunctionArgumentException.class) | |||
public void testDeriveWithEmptyStringArguments(){ | |||
getEvaluator().getResultAsEvaluatorString("derive('', '')"); | |||
} | |||
@Test(expected=InvalidCASFunctionArgumentException.class) | |||
public void testDeriveWithEmptyArgument(){ | |||
getEvaluator().getResultAsEvaluatorString("derive()"); | |||
} | |||
@Test(expected=UndefinedExerciseVariableException.class) | |||
public void testDeriveWithoutExerciseVariable() { | |||
getEvaluator().getResultAsEvaluatorString("derive([var=j], [var=j])"); | |||
} | |||
@Test(expected=UndefinedFillInVariableException.class) | |||
public void testDeriveWithoutInput() { | |||
getEvaluator().getResultAsEvaluatorString("derive([pos=42], [pos=42])"); | |||
} | |||
</syntaxhighlight> | |||
===Hinweise=== | ===Hinweise=== |
Version vom 21. Juni 2017, 09:59 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
@Before
public void beforeTest() {
placeFillInVariable(1, OMParser.parse("x"));
placeFillInVariable(2, OMParser.parse("0.5"));
placeExerciseVariable("a", OMParser.parse("x"));
placeExerciseVariable("b", OMParser.parse("0.5"));
}
@Test
public void testDerive() {
assertTrue("'2*x'".equals(getEvaluator().getResultAsEvaluatorString("derive('x^2','x')")));
assertTrue("'0'".equals(getEvaluator().getResultAsEvaluatorString("derive('x^2','y')")));
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
public void testDeriveWithInput() {
assertTrue("'x'".equals(getEvaluator().getResultAsEvaluatorString("derive('0.5*x^2 + 3','x')")));
assertTrue("'2+2*x'".equals(getEvaluator().getResultAsEvaluatorString("derive('[pos=1]^2 + 2[pos=1] + 4','x')")));
}
@Test
public void testDeriveWithExpressions(){
assertTrue("'1'".equals(getEvaluator().getResultAsEvaluatorString("'derive(derive('0.5*x^2', 'x')', 'x'")));
}
@Test
public void testDeriveWithVariables() {
assertTrue("'x'".equals(getEvaluator().getResultAsEvaluatorString("derive('0.5*x^2 + 3','x')")));
assertTrue("'2+2*x'".equals(getEvaluator().getResultAsEvaluatorString("derive('[var=a]^2 + 2[var=a] + 4','x')")));
}
@Test
public void testDeriveWithONECharacter(){
assertTrue("'1'".equals(getEvaluator().getResultAsEvaluatorString("derive(a, a)")));
}
@Test(expected=InvalidCASFunctionArgumentException.class)
public void testDeriveWithEmptyStringArguments(){
getEvaluator().getResultAsEvaluatorString("derive('', '')");
}
@Test(expected=InvalidCASFunctionArgumentException.class)
public void testDeriveWithEmptyArgument(){
getEvaluator().getResultAsEvaluatorString("derive()");
}
@Test(expected=UndefinedExerciseVariableException.class)
public void testDeriveWithoutExerciseVariable() {
getEvaluator().getResultAsEvaluatorString("derive([var=j], [var=j])");
}
@Test(expected=UndefinedFillInVariableException.class)
public void testDeriveWithoutInput() {
getEvaluator().getResultAsEvaluatorString("derive([pos=42], [pos=42])");
}
Hinweise
- Die Funktion derive kann z.B. mit der Funktion equalsExpr abgefragt werden:
equalsExpr(derive('x^2+x+3','x'),'2x+1')