1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
|
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>JAS - Designs, Problems and Solutions</title>
<style type="text/css">
body { background-color: #FFFFF5; }
pre { background-color: silver;
margin-left: 1em;
margin-right: 1em;
padding: 1em;
}
dt { font-weight: bolder;
margin-top: 1em;
}
.note { color: maroon; }
</style>
</head>
<body>
<h1>JAS - Designs, Problems and Solutions</h1>
<p>In ths document we discus some design alternatives,
some problems and present our implemented solutions.
</p>
<h2>1. Designs</h2>
<p><strong>Note:</strong>
In this section 'base ring'
and 'extension ring' mean object oriented concepts not
the mathematical concepts. I.e. 'base ring' is the super class
of all considered ring classes and 'extension ring' is some
subclass of the 'base ring' class.
</p>
<p>The first question is which classes or which objects
implement the arithmetic of polynomials. Are polynomials
only passive containers which are transformed by ring methods?
Or are polynomials active objects with methods.
</p>
<h3>1.1. Ring is object with methods</h3>
<p>
One design was proposed e.g. by M. Conrad in 2002
(see <a href="http://ring.perisic.com/">ring.perisic.com</a>).
It starts with an abstract <code>Ring</code> with abstract
methods for the ring operations and some real implementations,
e.g. for powers. The method parameters are <code>RingElt</code>s,
which serve mostly as containers for the different ring
implementations. The concrete rings, e.g. rational numbers
or polynomials, extend <code>Ring</code> and implement the
algortihms for the respective (extended) <code>RingElt</code>
data structures. <code>RingElt</code> structures are moreover
mostly private classes within their corresponding <code>Ring</code>
extension.
</p>
<p>
Type resolution of the parameters of the methods is completely dynamic
during runtime. There is no compile time type checking.
The type resolution, by means of a <code>RingX.map(RingElt)</code>
method, is moreover able to coerce elements from one ring to
some other ring, e.g. form rational to polynomial over rationals,
similar to Scratchpad.
The base <code>Ring</code> knows about all extension rings, like in
a closed world.
</p>
<p>
Creation of extension rings is mainly at initialization time of the
base ring (since it knows all extensions) into ring properties.
Creation of ring elements is mostly dynamic using direct constructors
in the various <code>map()</code> methods.
</p>
<h3>1.2. Polynomial is object with methods</h3>
<p>
An other design, e.g. used in our approach, takes polynomials as
the primary players. A <code>Polynomial</code> is implemented as
a class with the usual algebraic operations as methods.
Each polynomial has a reference to a corresponding <code>Ring</code>
object, which is a container for the ring characteristics. E.g.
for polynomial rings these are the number of variables, the
type information of the coefficent ring, the term order,
the names of the variables and eventualy the commutator relations.
There is one proposal by V. Niculescu, from 2003,
[ref:
<a href="http://www.cs.ubbcluj.ro/~vniculescu/Niculescu-03i1.zip"
>A design proposal for an object oriented algebraic library</a>]
to view and implement the <code>Ring</code> as a factory class
for polynomials and to make the polynomial constructors unavailable.
</p>
<p>
Creation of ring elements was in our first design by employing the
prototype creational pattern (via <code>clone()</code>)
and directly using element constructors. In the new design it
will use the factory pattern (via <code>getZERO()</code>,
<code>getONE()</code> etc.) of the <code>RingFactory</code>
</p>
<p>
Type resolution of the coefficient or polynomial method parameters are
to the respective interface during compile time with a dynamic upcast
to the actual polynomial or coefficient during runtime.
There is currently no mapping of elements from one ring to
another. However there are conversions / constructor / parser methods
from <code>long</code>, <code>java.math.BigInteger</code>,
<code>String</code> and <code>java.io.Reader</code> in the new design.
</p>
<h3>1.3. Template, generics and type parameter approaches</h3>
<p>
These approaches may not be completely covered by Java, C++ or C#.
For polynomials they mean the usage of a type parameter (eventually
restricted to some unterface) for the coefficient ring.
</p>
<p>
The creation problem is difficult to solve in Java, since
type parameters can not be used in <code>new</code>
or <code>class.newInstance()</code>. I.e. new objects can not be
generated only from a type parameter but only from an object or class.
</p>
<h2>2. Problems</h2>
<p>During the development and refactorings
<!--no to use type parmeters (generics, templates) -->
some problems have been detected.
Consider the following interface and class definitions.
</p>
<pre>
interface ModulElem {
ModulElem sum(ModulElem other);
...
}
interface RingElem extends ModulElem {
// RingElem sum(RingElem other); no override
RingElem multiply(RingElem other);
...
}
class Rational implements RingElem { // jdk 1.5
/*ModulElem*/ Rational sum(ModulElem other) {
...
}
/*RingElem*/ Rational multiply(RingElem other) {
if ( other instanceof Rational ) {
return multiply( (Rational)other );
} else {
return // coerce to suitable ring extension
}
}
Rational multiply(Rational other) {
...
}
}
class Complex implements RingElem {
...
}
void usageOK() {
Rational a = new Rational();
Rational b = new Rational();
Rational c;
c = a.sum(b); // jdk 1.5
c = a.multiply(b); // jdk 1.5
}
void usageProblem1() {
RingElem a = new Rational();
RingElem b = new Rational();
RingElem c;
c = (RingElem) a.sum(b); // must cast
c = a.multiply(b); // no cast
}
void usageProblem2() {
RingElem a = new Rational();
RingElem b = new Complex();
RingElem c;
c = a.multiply(b); // runtime failiure
}
</pre>
<p>
One problem is the cast in
<code>c = (RingElem)a.sum(b)</code>
which is not expected since a and b are <code>RingElem</code>s.
One sulution would be to redefine <code>sum()</code> for
<code>RingElem</code>, but then <code>sum()</code> in
<code>ModulElem</code> is not overriden.
Then <code>RingElem</code> is no longer an extension of
<code>ModulElem</code> and the relation between the
interfaces is broken.
</p>
<p>
The other problem is 'up cast' in
<code>return multiply( (Rational)other )</code>
which defeates compile time type safety.
<code>multiply( )</code> is at first not meaningful defined
between <code>Rational</code> and <code>Complex</code>.
One could as in Scratchpad coerce <code>Rational</code>
to <code>Complex</code> (here extend) and multiply to
<code>Complex</code> objects, but this may not be expected
by the application.
</p>
<p>
This problems exist also if abstract classes are used
instead of interfaces.
</p>
<h2>3. Solutions</h2>
<p>Reflecting on the mentioned designs and problems our
design proposal is as follows.
</p>
<ol>
<li><p>We do not distinguish between interfaces for
modules, rings or fields. There is only one interface
for rings, wich also defines <code>inverse()</code>
and <code>quotient()</code> together with
a method <code>isUnit()</code> to see if a certain element is
invertible or can be used as divisor.
</p>
</li>
<li><p>To separate the creation process of ring elements
from the implementation of the ring element
abstract data type we distinguish two interfaces:
<code>RingElem<C extends RingElem></code>
and
<code>RingFactory<C extends RingElem></code>.
</p>
</li>
<li><p><code>RingElem</code> uses a type parameter
<code>C</code> which is itself recursively reqired
to extend <code>RingElem</code>: <code>C extends RingElem</code>.
Also the interface <code>RingFactory</code> depends on the
same type parameter.
</p>
</li>
<li><p>Basic data types, such as rational numbers, can directly
implement both interfaces but more complex data types, such
as polynomials will implement the interfaces in two different
classes. e.g.
</p>
<pre>
BigRational implements RingElem<BigRational>, RingFactory<BigRational>
</pre>
<p>or for generic polynomials
</p>
<pre>
GenPolynomial<C extends RingElem<C> > implements RingElem< GenPolynomial<C> >
GenPolynomialRing<C extends RingElem<C> > implements RingFactory< GenPolynomial<C> >
</pre>
</li>
<li><p>Constructors for basic data types can be implemented in any
appropriate way.
Constructors for more complex data types should always require one
parameter to be of the respective factory type. This is to
avoid the creation of elements with no knowledge of is corresponding
ring factory.
Constructors which require more preconditions, which are only
provided by type (internal) methods should not be declared public.
It seems best to declare them as protected.
</p>
</li>
<li><p>Basic arithmetic is implemented using the
<code>java.math.BigInteger</code> class, which
is itself implemented like GnuMP.
At the moment the following classes are implemented
<code>BigInteger</code>,
<code>BigRational</code>,
<code>ModInteger</code>,
<code>BigComplex</code>,
<code>BigQuaternion</code>
and <code>AlgebraicNumber</code>.
</p>
</li>
<li><p>Generic polynomials are implemented as sorted maps from
exponent vectors to coefficients.
For sorted map the Java class <code>java.util.TreeMap</code>
is used.
The older alternative implementation using <code>Map</code>,
implemented with <code>java.util.LinkedHashMap</code>, has
been abandoned.
There is only one implementation of exponent vectors
<code>ExpVector</code> as dense Java array of <code>long</code>s.
Other implementations, e.g. sparse representation or
bigger numbers or <code>int</code>s are not considered
at the moment.
The comparators for <code>SortedMap<ExpVector,C></code>
are created from a <code>TermOrder</code> class which
implements most used term orders in practice.
</p>
</li>
<li><p>Non commutative polynomials with respect to certain
commutator relations, so called solvable polynomials,
are extended from
<code>GenPolynomial</code> respectively
<code>GenPolynomialRing</code>.
The relations are stored in <code>RelationTable</code> objects,
which are inteded to be internal to the
<code>GenSolvablePolynomialRing</code>.
The class
<code>GenSolvablePolynomial</code> implements the
non commutative multiplication and uses the commutative
methods from its super class <code>GenPolynomial</code>.
As mentioned before, some casts are eventualy required, e.g.
<code>GenSolvablePolynomial<C> p.sum(q)</code>.
The respective objects are however correctly buildt
using the methods from the solvable ring factory.
<br />
The class design allows solvable polynomial objects to be
used in all algorithms where <code>GenPolynomial</code>s
can be used as parameters as long as no distinction between
left and right multiplication is required.
</p>
</li>
<!--
<li><p>
</p>
</li>
-->
</ol>
<h3>3.1. Interfaces</h3>
<p>The interface definition for ring elements with the usual
arithmetic operations and some status, comparison methods
and a clone method is as follows.
</p>
<pre>
public interface RingElem<C extends RingElem>
extends Cloneable,
Comparable<C>,
Serializable {
public C clone();
public boolean isZERO();
public boolean isONE();
public boolean isUnit();
public boolean equals(Object b);
public int hashCode();
public int compareTo(C b);
public int signum();
public C sum(C S);
public C subtract(C S);
public C negate();
public C abs();
public C multiply(C S);
public C divide(C S);
public C remainder(C S);
public C inverse();
}
</pre>
<p>The interface definition for a ring factory for the creation
respectively the reference to the ring constants 0 and 1 is given
in the following code. Moreover there are often used casts /
conversions from the basic Java types long and BigInteger,
as well as a method to create a random element of the ring,
a counter part to clone and some parsing methods to obtain
a ring element from some external String or Reader.
</p>
<pre>
public interface RingFactory<C extends RingElem>
extends Serializable {
public C getZERO();
public C getONE();
public C fromInteger(long a);
public C fromInteger(BigInteger a);
public C random(int n);
public C copy(C c);
public C parse(String s);
public C parse(Reader r);
}
</pre>
<h3>3.2. Some constructors</h3>
<p>Constructors for BigRational:
</p>
<pre>
protected BigRational(BigInteger n, BigInteger d)
// assert gcd(n,d) == 1
public BigRational(BigInteger n)
public BigRational(long n, long d)
public BigRational(long n)
public BigRational()
public BigRational(String s) throws NumberFormatException
</pre>
<p>Constructors for GenPolynomial
</p>
<pre>
public GenPolynomial(GenPolynomialRing< C > r)
public GenPolynomial(GenPolynomialRing< C > r, SortedMap<ExpVector,C> v)
public GenPolynomial(GenPolynomialRing< C > r, C c, ExpVector e)
</pre>
<p>Constructors for GenPolynomialRing
</p>
<pre>
public GenPolynomialRing(RingFactory< C > cf, int n)
public GenPolynomialRing(RingFactory< C > cf, int n, TermOrder t) {
public GenPolynomialRing(RingFactory< C > cf, int n, TermOrder t, String[] v)
</pre>
<h3>3.3. Polynomial examples</h3>
<p>Example of a
random polynomial in 7 variables over the rational numbers
with default term order and with 10 non zero coefficients:
</p>
<pre>
BigRational cfac = new BigRational();
GenPolynomialRing<BigRational> fac;
fac = new GenPolynomialRing<BigRational>(cfac,7);
GenPolynomial<BigRational> a = fac.random(10);
a = GenPolynomial[ 31/5 (0,0,0,1,2,1,2), 19/15 (2,0,0,0,0,0,2),
13/5 (0,2,1,1,0,0,0), 2/3 (0,0,2,0,0,0,0), 217/18 (0,0,0,2,0,0,0),
18/5 (0,0,0,0,2,0,0), 11/32 (1,0,0,0,0,0,0), 63/4 (0,0,0,0,0,0,0) ]
:: GenPolynomialRing[ BigRational, 7, IGRLEX(4), ]
</pre>
<p>Example of a
random polynomial in 3 variables over a polynomial ring in 7 variables
over the rational numbers, both with default term order and
with 10 non zero coefficients:
</p>
<pre>
BigRational cfac = new BigRational();
GenPolynomialRing<BigRational> fac;
fac = new GenPolynomialRing<BigRational>(cfac,7);
GenPolynomialRing<GenPolynomial<BigRational>> gfac;
gfac = new GenPolynomialRing<GenPolynomial<BigRational>>(fac,3);
GenPolynomial<GenPolynomial<BigRational>> a = gfac.random(10);
a = GenPolynomial[
GenPolynomial[ 10/3 (2,0,1,1,0,0,2), 8/7 (1,0,2,0,0,0,0),
9/5 (0,1,0,0,0,0,0), 1/4 (0,0,1,0,0,0,0), 3/14 (0,0,0,0,0,0,0) ]
:: GenPolynomialRing[ BigRational, 7, IGRLEX(4), ] (2,1,0),
GenPolynomial[ 26/23 (0,2,2,0,1,0,2), 9/4 (1,0,0,0,0,1,1),
29/17 (0,0,2,0,1,0,0), 24/19 (2,0,0,0,0,0,0), 28/13 (1,0,0,1,0,0,0),
11/32 (0,0,1,0,1,0,0), 18/11 (1,0,0,0,0,0,0), 5/11 (0,0,0,0,1,0,0),
475/32 (0,0,0,0,0,0,0) ]
:: GenPolynomialRing[ BigRational, 7, IGRLEX(4), ] (2,0,0),
GenPolynomial[ 14/15 (2,0,0,1,1,0,2), 19/5 (1,1,0,0,0,0,0),
4/29 (0,0,2,0,0,0,0), 23/27 (0,0,0,2,0,0,0), 20/13 (0,0,0,0,0,0,0) ]
:: GenPolynomialRing[ BigRational, 7, IGRLEX(4), ] (0,0,2),
GenPolynomial[ 13/8 (2,0,0,1,1,0,0), 8/7 (2,0,0,0,0,0,2),
21/2 (0,0,1,0,0,0,2), 23/22 (0,1,0,0,0,1,0), 9/11 (0,0,0,2,0,0,0),
21/2 (0,0,0,0,2,0,0), 23/13 (0,0,0,0,0,2,0), 5/2 (0,1,0,0,0,0,0),
367/62 (0,0,0,0,0,0,0) ]
:: GenPolynomialRing[ BigRational, 7, IGRLEX(4), ] (1,0,0),
GenPolynomial[ 4/3 (0,1,0,1,2,1,1), 17/2 (2,1,0,0,2,0,0),
10/29 (1,0,0,0,0,2,2), 3/2 (0,0,2,2,0,0,1), 11/8 (2,0,0,0,0,0,2),
26/31 (0,2,1,0,0,0,0), 10/9 (0,0,1,2,0,0,0), 4/5 (0,0,1,0,0,2,0),
1/8 (2,0,0,0,0,0,0), 1161/406 (0,2,0,0,0,0,0), 31/6 (0,0,0,2,0,0,0),
19 (0,0,1,0,0,0,0), 2 (0,0,0,0,0,1,0), 7/19 (0,0,0,0,0,0,1),
20227/2520 (0,0,0,0,0,0,0) ]
:: GenPolynomialRing[ BigRational, 7, IGRLEX(4), ] (0,0,0) ]
:: GenPolynomialRing[ GenPolynomialRing, 3, IGRLEX(4), ]
</pre>
<h3>3.4. Algebraic number examples</h3>
<p>Example of algebraic numbers
</p>
<pre>
AlgebraicNumber<C extends RingElem<C> >
implements RingElem< AlgebraicNumber<C> >,
RingFactory< AlgebraicNumber<C> >
</pre>
<p>over rational numbers (so defining an algebraic extension Q(alpha))
</p>
<pre>
BigRational cfac = new BigRational();
GenPolynomialRing<BigRational> mfac;
mfac = new GenPolynomialRing<BigRational>( cfac, 1 );
GenPolynomial<BigRational> modul = mfac.random(8).monic();
// assume !modul.isUnit()
AlgebraicNumber<BigRational> fac;
fac = new AlgebraicNumber<BigRational>( modul );
AlgebraicNumber< BigRational > a = fac.random(15);
modul = GenPolynomial[ 1 (2), 13/12 (1), 55/21 (0) ]
:: GenPolynomialRing[ BigRational, 1, IGRLEX(4), ]
a = AlgebraicNumber[
GenPolynomial[ 1 (1), -175698982/14106209 (0) ]
:: GenPolynomialRing[ BigRational, 1, IGRLEX(4), ]
mod
GenPolynomial[ 1 (2), 13/12 (1), 55/21 (0) ]
:: GenPolynomialRing[ BigRational, 1, IGRLEX(4), ] ]
</pre>
<p>or modular integers (so defining a Galois field GF(p,n)).
</p>
<pre>
long prime = getPrime(); // 2^60-93
ModInteger cfac = new ModInteger(prime,1);
GenPolynomialRing<ModInteger> mfac;
mfac = new GenPolynomialRing<ModInteger>( cfac, 1 );
GenPolynomial<ModInteger> modul = mfac.random(8).monic();
// assume !modul.isUnit()
AlgebraicNumber<ModInteger> fac;
fac = new AlgebraicNumber<ModInteger>( modul );
AlgebraicNumber< ModInteger > a = fac.random(12);
modul = GenPolynomial[ 1 mod(1152921504606846883) (2),
123527304065019309 mod(1152921504606846883) (1),
452933448238404135 mod(1152921504606846883) (0) ]
:: GenPolynomialRing[ ModInteger, 1, IGRLEX(4), ]
a = AlgebraicNumber[
GenPolynomial[ 1 mod(1152921504606846883) (1),
384307168202282226 mod(1152921504606846883) (0) ]
:: GenPolynomialRing[ ModInteger, 1, IGRLEX(4), ]
mod
GenPolynomial[ 1 mod(1152921504606846883) (2),
123527304065019309 mod(1152921504606846883) (1),
452933448238404135 mod(1152921504606846883) (0) ]
:: GenPolynomialRing[ ModInteger, 1, IGRLEX(4), ] ]
</pre>
<h3>3.5. Solvable polynomial examples</h3>
<p>Example for the creation of a solvable polynomial ring factory.
The relation table is created internally.
</p>
<pre>
BigRational fac = new BigRational(0);
TermOrder tord = new TermOrder(TermOrder.INVLEX);
String[] vars = new String[]{ "x", "y", "z" };
int nvar = vars.length;
spfac = new GenSolvablePolynomialRing<BigRational>(fac,nvar,tord,vars);
spfac = GenSolvablePolynomialRing[ BigRational, 3,
INVLEX(2), x y z ,
#rel = 0 ]
spfac.table = RelationTable[]
</pre>
<p>A non empty relation table looks as follows.
</p>
<pre>
f = GenSolvablePolynomialRing[ BigRational, 3, INVLEX(2), x y z , #rel = 1 ]
f.ring.table = RelationTable[
[0, 1]=[ExpVectorPair[(1,0,0),(0,1,0)],
GenSolvablePolynomial[ 1 (1,1,0), -1 (0,0,0) ]
:: GenSolvablePolynomialRing[ BigRational, 3,
INVLEX(2), x y z ,
#rel = 1 ]
]
]
</pre>
<p>Example of a solvable polynomial over Z_19.
</p>
<pre>
d = GenSolvablePolynomial[ 3 mod(19) (1,1,0), 1 mod(19) (0,0,1) ]
:: GenSolvablePolynomialRing[ ModInteger, 3, INVLEX(2), x y z , #rel = 1 ]
</pre>
<!--
<p>
</p>
<pre>
</pre>
-->
<p><!--a href="README" target="readme" >README</a-->
</p>
<hr />
<address><a href="mailto:kredel at rz.uni-mannheim.de">Heinz Kredel</a></address>
<p>
<!-- Created: Thu May 26 10:38:37 CEST 2005 -->
<!-- hhmts start -->
Last modified: Sun Mar 12 14:29:09 CET 2006
<!-- hhmts end -->
</p>
<p align="right" >
$Id$
</p>
</body>
</html>
|