## matheclipse-devel

 [matheclipse-devel] org.matheclipse.core.test/src/org/matheclipse/core/jas PolynomialTest.java, NONE, 1.1 From: Axel C. Kramer - 2009-02-28 08:38:23 ```Update of /cvsroot/matheclipse/org.matheclipse.core.test/src/org/matheclipse/core/jas In directory 23jxhf1.ch3.sourceforge.com:/tmp/cvs-serv25481/src/org/matheclipse/core/jas Added Files: PolynomialTest.java Log Message: First steps for integrating JAS angine --- NEW FILE: PolynomialTest.java --- package org.matheclipse.core.jas; import java.util.ArrayList; import org.matheclipse.core.convert.JASConvert; import org.matheclipse.core.expression.F; import org.matheclipse.core.expression.IntegerSym; import org.matheclipse.core.expression.Symbol; import org.matheclipse.core.interfaces.IAST; import org.matheclipse.core.interfaces.ISymbol; import org.matheclipse.core.system.AbstractTestCase; import edu.jas.poly.GenPolynomial; public class PolynomialTest extends AbstractTestCase { JASConvert fJAS; public PolynomialTest(String name) { super(name); } public void testPoly001() { ISymbol x = new Symbol("x"); IAST f = F.Plus(IntegerSym.valueOf(10), x); GenPolynomial poly = fJAS.expr2Poly(f); assertEquals("x + 10 ", poly.toString()); // convert back to MathEclipse expression IAST ast = fJAS.poly2Expr(poly); // 1*x^1+10 assertEquals("Plus[Times[1, Power[x, 1]], Times[10]]", ast.toString()); // evaluated expression check(ast, "10+x"); } public void testPoly002() { ISymbol x = new Symbol("x"); ArrayList variables = new ArrayList(); variables.add(x); IAST f = F.Power(x, IntegerSym.valueOf(3)); GenPolynomial poly = fJAS.expr2Poly(f); assertEquals("x^3", poly.toString()); // convert back to MathEclipse expression IAST ast = fJAS.poly2Expr(poly); // 1*x^3 assertEquals("Plus[Times[1, Power[x, 3]]]", ast.toString()); // evaluated expression check(ast, "x^3"); } public void testPoly003() { ISymbol x = new Symbol("x"); IAST f = F.Times(x, IntegerSym.valueOf(3)); GenPolynomial poly = fJAS.expr2Poly(f); assertEquals("3 x", poly.toString()); // convert back to MathEclipse expression IAST ast = fJAS.poly2Expr(poly); // 3*x^1 assertEquals("Plus[Times[3, Power[x, 1]]]", ast.toString()); // evaluated expression check(ast, "3*x"); } public void testPoly004() { ISymbol x = new Symbol("x"); IAST f = F.Plus(IntegerSym.valueOf(10), x); f.add(F.Power(x, IntegerSym.valueOf(3))); f.add(F.Times(x, IntegerSym.valueOf(3))); GenPolynomial poly = fJAS.expr2Poly(f); assertEquals("x^3 + 4 x + 10 ", poly.toString()); // convert back to MathEclipse expression IAST ast = fJAS.poly2Expr(poly); // 1*x^3 + 4*x^1 + 10 assertEquals("Plus[Times[1, Power[x, 3]], Times[4, Power[x, 1]], Times[10]]", ast.toString()); // evaluated expression check(ast, "10+4*x+x^3"); } public void testDivideRemainder001() { ISymbol x = new Symbol("x"); IAST f = F.Plus(IntegerSym.valueOf(10), x); f.add(F.Power(x, IntegerSym.valueOf(3))); f.add(F.Times(x, IntegerSym.valueOf(3))); GenPolynomial poly = fJAS.expr2Poly(f); assertEquals("x^3 + 4 x + 10 ", poly.toString()); GenPolynomial[] result = poly.divideAndRemainder(poly); assertEquals("1 ", result[0].toString()); assertEquals("0", result[1].toString()); } public void testDivideRemainder002() { ISymbol x = new Symbol("x"); // -5-4*x+x^2 IAST f1 = F.Plus(IntegerSym.valueOf(-5), F.Times(x, IntegerSym.valueOf(-4))); f1.add(F.Power(x, IntegerSym.valueOf(2))); GenPolynomial poly1 = fJAS.expr2Poly(f1); // -5+x IAST f2 = F.Plus(IntegerSym.valueOf(-5), x); GenPolynomial poly2 = fJAS.expr2Poly(f2); assertEquals("x^2 - 4 x - 5 ", poly1.toString()); assertEquals("x - 5 ", poly2.toString()); GenPolynomial[] result = poly1.divideAndRemainder(poly2); assertEquals("x + 1 ", result[0].toString()); assertEquals("0", result[1].toString()); } public void testEGCD001() { ISymbol x = new Symbol("x"); IAST f = F.Plus(IntegerSym.valueOf(10), x); f.add(F.Power(x, IntegerSym.valueOf(3))); f.add(F.Times(x, IntegerSym.valueOf(3))); GenPolynomial poly = fJAS.expr2Poly(f); assertEquals("x^3 + 4 x + 10 ", poly.toString()); GenPolynomial[] result = poly.egcd(poly); assertEquals("x^3 + 4 x + 10 ", result[0].toString()); assertEquals("0", result[1].toString()); assertEquals("1 ", result[2].toString()); } public void testEGCD002() { ISymbol x = new Symbol("x"); // -5-4*x+x^2 IAST f1 = F.Plus(IntegerSym.valueOf(-5), F.Times(x, IntegerSym.valueOf(-4))); f1.add(F.Power(x, IntegerSym.valueOf(2))); GenPolynomial poly1 = fJAS.expr2Poly(f1); // -5+x IAST f2 = F.Plus(IntegerSym.valueOf(-5), x); GenPolynomial poly2 = fJAS.expr2Poly(f2); assertEquals("x^2 - 4 x - 5 ", poly1.toString()); assertEquals("x - 5 ", poly2.toString()); GenPolynomial[] result = poly1.egcd(poly2); assertEquals("x - 5 ", result[0].toString()); assertEquals("0", result[1].toString()); assertEquals("1 ", result[2].toString()); } public void testGCD001() { ISymbol x = new Symbol("x"); IAST f = F.Plus(IntegerSym.valueOf(10), x); f.add(F.Power(x, IntegerSym.valueOf(3))); f.add(F.Times(x, IntegerSym.valueOf(3))); GenPolynomial poly = fJAS.expr2Poly(f); assertEquals("x^3 + 4 x + 10 ", poly.toString()); GenPolynomial result = poly.gcd(poly); assertEquals("x^3 + 4 x + 10 ", result.toString()); } public void testGCD002() { ISymbol x = new Symbol("x"); // -5-4*x+x^2 IAST f1 = F.Plus(IntegerSym.valueOf(-5), F.Times(x, IntegerSym.valueOf(-4))); f1.add(F.Power(x, IntegerSym.valueOf(2))); GenPolynomial poly1 = fJAS.expr2Poly(f1); // -5+x IAST f2 = F.Plus(IntegerSym.valueOf(-5), x); GenPolynomial poly2 = fJAS.expr2Poly(f2); assertEquals("x^2 - 4 x - 5 ", poly1.toString()); assertEquals("x - 5 ", poly2.toString()); GenPolynomial result = poly1.gcd(poly2); assertEquals("x - 5 ", result.toString()); } public void testPoly999() { ISymbol x = new Symbol("x"); ISymbol y = new Symbol("y"); ISymbol z = new Symbol("z"); ArrayList variables = new ArrayList(); variables.add(x); variables.add(y); variables.add(z); IAST f = F.Plus(IntegerSym.valueOf(10), x); f.add(F.Power(y, IntegerSym.valueOf(3))); f.add(F.Times(z, IntegerSym.valueOf(3))); f.add(F.Times(IntegerSym.valueOf(4), x, z)); JASConvert jas005 = new JASConvert(variables); GenPolynomial poly = jas005.expr2Poly(f); assertEquals("4 x * z + x + y^3 + 3 z + 10 ", poly.toString()); } @Override protected void setUp() { super.setUp(); ISymbol x = new Symbol("x"); ArrayList variables = new ArrayList(); variables.add(x); fJAS = new JASConvert(variables); } } ```