Hi,
I tried to use JScience for multiplication of polynomials. During
debugging of program I discovered that such code (this is reproduction
example, not actual code) produce wrong result:
x = Variable.Local("x")
curr = Polynomial.valueOf(Float64.valueOf(-0.69098300562505266),
x).plus(Float64.valueOf(0.30901699437494745))
prev = Polynomial.valueOf(Float64.valueOf(-1.8090169943749472),
x).plus(Float64.valueOf(-0.8090169943749473))
next = curr.times(prev)
next #==>
[1.25]x² + [-0.5590169943749474]x + [-0.25]
To compare results from:
numpy: 1.25 x² - 0.25
maxima: 1.25 x² - 0.25
wolfram alpha: 1.25 x² - 0.25
I'm not sure what causes this problem but it looks that either I use
JScience incorrectly or there is some serious bug in polynomial
multiplication.
Best regards
EDIT:
It looks like the term is not removed after it is zeroed. Patch is
below:
Index:
mathematics/src/main/java/org/jscience/mathematics/function/Polynomial.java
===================================================================
---
mathematics/src/main/java/org/jscience/mathematics/function/Polynomial.java(revision 694)
+++
mathematics/src/main/java/org/jscience/mathematics/function/Polynomial.java(working copy)
@@ -229,6 +229,7 @@
R coef = (prev != null) ? prev.plus(c) : c;
if (isZero(coef)) {
zero = coef;
+ result._termToCoef.remove(t);
} else {
result._termToCoef.put(t, coef);
}
[JScience-Users] [Bug in JScience]: problem with multiplication of polynomials |
Maciej Piechotka | 12/15/2012 |
[JScience-Users] Re: [Bug in JScience]: problem with multiplication of polynomials |
Jean-Marie Dautelle | 12/15/2012 |