Skip to content

Commit a18ec94

Browse files
committed
Fixes #3: XYZPolynomial.add() changes values of its parameters
1 parent 8f4df80 commit a18ec94

File tree

2 files changed

+50
-5
lines changed

2 files changed

+50
-5
lines changed

src/main/java/de/mfo/jsurf/algebra/XYZPolynomial.java

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,22 @@ public String longToString()
147147
result.append( "x^" + xExp + "y^" + yExp + "z^" + zExp );
148148
return result.toString();
149149
}
150+
151+
public boolean equals( Term t )
152+
{
153+
return this.coeff == t.coeff &&
154+
this.xExp == t.xExp &&
155+
this.yExp == t.yExp &&
156+
this.zExp == t.zExp;
157+
}
158+
159+
public int hashCode()
160+
{
161+
return ( ( int ) ( Double.doubleToRawLongBits( this.coeff ) % 0x00000000FFFFFFFFL ) ) &
162+
( ( int ) this.xExp ) &
163+
( ( int ) this.xExp ) << 8 &
164+
( ( int ) this.xExp ) << 16;
165+
}
150166
}
151167

152168
private Term[] terms; // ordered list of terms, such that terms[ i ].lexCompare( terms[ i + 1 ] ) < 0
@@ -158,6 +174,8 @@ public String longToString()
158174
private byte degree;
159175
private int numXyTerms; // number of terms in result of evaluateZ
160176
private boolean isCompact = false;
177+
178+
private static final Term[] dummyTermArray = new Term[ 0 ];
161179

162180
public XYZPolynomial()
163181
{
@@ -270,7 +288,7 @@ else if( lexCompare > 0 )
270288
while( i2 < terms2.length )
271289
resultTerms.add( new Term( terms2 [ i2++ ] ) );
272290

273-
return new XYZPolynomial( resultTerms.toArray( this.terms ) );
291+
return new XYZPolynomial( resultTerms.toArray( XYZPolynomial.dummyTermArray ) );
274292
}
275293

276294
public XYZPolynomial mult( XYZPolynomial p )
@@ -300,7 +318,7 @@ private static XYZPolynomial mult( XYZPolynomial p1, XYZPolynomial p2 )
300318
for( int i = 0; i < p2.terms.length; i++ )
301319
resultTerms.add( t1.mult( p2.terms[ i ] ) );
302320
}
303-
return new XYZPolynomial( collect( resultTerms.toArray( new Term[ 0 ] ), true ) );
321+
return new XYZPolynomial( collect( resultTerms.toArray( XYZPolynomial.dummyTermArray ), true ) );
304322
}
305323

306324
// // standard polynomial multiplication (faster, but less numerically stable)
@@ -693,7 +711,7 @@ public XYZPolynomial substitute( XYZPolynomial xPoly, XYZPolynomial yPoly, XYZPo
693711
for( Term e : expandedTerm.terms )
694712
terms.add( e );
695713
}
696-
XYZPolynomial result = new XYZPolynomial( collect( terms.toArray( new Term[ 0 ] ), true ) );
714+
XYZPolynomial result = new XYZPolynomial( collect( terms.toArray( XYZPolynomial.dummyTermArray ), true ) );
697715
return result;
698716
}
699717
*/
@@ -724,7 +742,7 @@ private static Term[] collect( Term[] terms, boolean sort )
724742

725743
if( resultTerms.size() == 0 )
726744
resultTerms.add( new Term( 0.0, (byte) 0, (byte) 0, (byte) 0 ) );
727-
return resultTerms.toArray( new Term[ 0 ] );
745+
return resultTerms.toArray( XYZPolynomial.dummyTermArray );
728746
}
729747
else
730748
{
@@ -776,4 +794,22 @@ public double[][][] recursiveView()
776794
a[ t.zExp ][ t.yExp ][ t.xExp ] = t.coeff;
777795
return a;
778796
}
797+
798+
public boolean equals( XYZPolynomial p )
799+
{
800+
if( this.terms.length != p.terms.length )
801+
return false;
802+
for( int i = 0; i < this.terms.length; ++i )
803+
if( !this.terms[ i ].equals( p.terms[ i ] ) )
804+
return false;
805+
return true;
806+
}
807+
808+
public int hashCode()
809+
{
810+
int c = 0;
811+
for( int i = 0; i < this.terms.length; ++i )
812+
c += this.terms[ i ].hashCode();
813+
return c;
814+
}
779815
}

src/test/java/de/mfo/jsurf/test/TestJSurf.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import de.mfo.jsurf.rendering.cpu.CPUAlgebraicSurfaceRenderer;
66
import de.mfo.jsurf.rendering.RenderingInterruptedException;
7+
import de.mfo.jsurf.algebra.*;
78
import de.mfo.jsurf.util.FileFormat;
89
import java.util.Properties;
910
import javax.swing.SwingWorker;
@@ -54,4 +55,12 @@ public Void doInBackground() {
5455

5556
Assert.assertTrue( "stopDrawing must interrupt and stop the rendering process", t < 200 );
5657
}
57-
}
58+
59+
@Test
60+
public void XYZPolynomialAddShouldNotAffectMethodParameters()
61+
{
62+
XYZPolynomial X = new XYZPolynomial( XYZPolynomial.X );
63+
XYZPolynomial.X.add( XYZPolynomial.X );
64+
Assert.assertTrue( "XYZPolynomial.add changes values of its parameters", X.equals( XYZPolynomial.X ) );
65+
}
66+
}

0 commit comments

Comments
 (0)