/geogebra/edu/jas/application/PolyUtilApp.java

https://github.com/ancsing/ggw · Java · 1244 lines · 772 code · 123 blank · 349 comment · 168 complexity · a0554bb047f3980db54368ba4c082d73 MD5 · raw file

  1. /*
  2. * $Id: PolyUtilApp.java 3176 2010-06-07 20:25:03Z kredel $
  3. */
  4. package edu.jas.application;
  5. import java.io.Serializable;
  6. import java.util.Arrays;
  7. import java.util.Map;
  8. import java.util.List;
  9. import java.util.ArrayList;
  10. import java.util.SortedMap;
  11. import java.util.TreeMap;
  12. import java.util.HashMap;
  13. import java.util.Set;
  14. import java.util.TreeSet;
  15. import org.apache.log4j.Logger;
  16. import edu.jas.structure.GcdRingElem;
  17. import edu.jas.structure.RingElem;
  18. import edu.jas.structure.RingFactory;
  19. import edu.jas.structure.RegularRingElem;
  20. import edu.jas.structure.Product;
  21. import edu.jas.structure.ProductRing;
  22. import edu.jas.structure.Complex;
  23. import edu.jas.structure.ComplexRing;
  24. import edu.jas.structure.UnaryFunctor;
  25. import edu.jas.arith.BigInteger;
  26. import edu.jas.arith.BigRational;
  27. import edu.jas.arith.Rational;
  28. import edu.jas.arith.ModInteger;
  29. import edu.jas.arith.ModIntegerRing;
  30. import edu.jas.arith.BigDecimal;
  31. import edu.jas.gb.GroebnerBase;
  32. import edu.jas.gb.GroebnerBaseSeq;
  33. import edu.jas.poly.ExpVector;
  34. import edu.jas.poly.GenPolynomial;
  35. import edu.jas.poly.GenPolynomialRing;
  36. import edu.jas.poly.AlgebraicNumber;
  37. import edu.jas.poly.AlgebraicNumberRing;
  38. import edu.jas.poly.PolynomialList;
  39. import edu.jas.poly.PolyUtil;
  40. import edu.jas.poly.TermOrder;
  41. import edu.jas.root.ComplexRootsSturm;
  42. import edu.jas.root.ComplexRootsAbstract;
  43. import edu.jas.root.RealRootsSturm;
  44. import edu.jas.root.RealRootAbstract;
  45. import edu.jas.root.RealAlgebraicNumber;
  46. import edu.jas.root.RealAlgebraicRing;
  47. import edu.jas.root.RootFactory;
  48. import edu.jas.util.ListUtil;
  49. /**
  50. * Polynomial utilities for applications,
  51. * for example conversion ExpVector to Product or zero dimensional ideal root computation.
  52. * @param <C> coefficient type
  53. * @author Heinz Kredel
  54. */
  55. public class PolyUtilApp<C extends RingElem<C> > {
  56. private static final Logger logger = Logger.getLogger(PolyUtilApp.class);
  57. private static boolean debug = logger.isDebugEnabled();
  58. /**
  59. * Product representation.
  60. * @param <C> coefficient type.
  61. * @param pfac product polynomial ring factory.
  62. * @param c coefficient to be used.
  63. * @param e exponent vector.
  64. * @return Product represenation of c X^e in the ring pfac.
  65. */
  66. public static <C extends RingElem<C>>
  67. Product<GenPolynomial<C>>
  68. toProduct( ProductRing<GenPolynomial<C>> pfac,
  69. C c, ExpVector e ) {
  70. SortedMap<Integer,GenPolynomial<C>> elem
  71. = new TreeMap<Integer,GenPolynomial<C>>();
  72. for ( int i = 0; i < e.length(); i++ ) {
  73. RingFactory<GenPolynomial<C>> rfac = pfac.getFactory(i);
  74. GenPolynomialRing<C> fac = (GenPolynomialRing<C>) rfac;
  75. //GenPolynomialRing<C> cfac = fac.ring;
  76. long a = e.getVal(i);
  77. GenPolynomial<C> u;
  78. if ( a == 0 ) {
  79. u = fac.getONE();
  80. } else {
  81. u = fac.univariate(0,a);
  82. }
  83. u = u.multiply(c);
  84. elem.put( i, u );
  85. }
  86. return new Product<GenPolynomial<C>>( pfac, elem );
  87. }
  88. /**
  89. * Product representation.
  90. * @param <C> coefficient type.
  91. * @param pfac product polynomial ring factory.
  92. * @param A polynomial.
  93. * @return Product represenation of the terms of A in the ring pfac.
  94. */
  95. public static <C extends RingElem<C>>
  96. Product<GenPolynomial<C>>
  97. toProduct( ProductRing<GenPolynomial<C>> pfac,
  98. GenPolynomial<C> A ) {
  99. Product<GenPolynomial<C>> P = pfac.getZERO();
  100. if ( A == null || A.isZERO() ) {
  101. return P;
  102. }
  103. for ( Map.Entry<ExpVector,C> y: A.getMap().entrySet() ) {
  104. ExpVector e = y.getKey();
  105. C a = y.getValue();
  106. Product<GenPolynomial<C>> p = toProduct(pfac,a,e);
  107. P = P.sum( p );
  108. }
  109. return P;
  110. }
  111. /**
  112. * Product representation.
  113. * @param <C> coefficient type.
  114. * @param pfac product ring factory.
  115. * @param c coefficient to be represented.
  116. * @return Product represenation of c in the ring pfac.
  117. */
  118. public static <C extends GcdRingElem<C>>
  119. Product<C>
  120. toProductGen( ProductRing<C> pfac, C c) {
  121. SortedMap<Integer,C> elem = new TreeMap<Integer,C>();
  122. for ( int i = 0; i < pfac.length(); i++ ) {
  123. RingFactory<C> rfac = pfac.getFactory(i);
  124. C u = rfac.copy( c );
  125. if ( u != null && !u.isZERO() ) {
  126. elem.put( i, u );
  127. }
  128. }
  129. return new Product<C>( pfac, elem );
  130. }
  131. /**
  132. * Product representation.
  133. * @param <C> coefficient type.
  134. * @param pfac polynomial ring factory.
  135. * @param A polynomial to be represented.
  136. * @return Product represenation of A in the polynomial ring pfac.
  137. */
  138. public static <C extends GcdRingElem<C>>
  139. GenPolynomial<Product<C>>
  140. toProductGen( GenPolynomialRing<Product<C>> pfac,
  141. GenPolynomial<C> A) {
  142. GenPolynomial<Product<C>> P = pfac.getZERO().clone();
  143. if ( A == null || A.isZERO() ) {
  144. return P;
  145. }
  146. RingFactory<Product<C>> rpfac = pfac.coFac;
  147. ProductRing<C> rfac = (ProductRing<C>) rpfac;
  148. for ( Map.Entry<ExpVector,C> y: A.getMap().entrySet() ) {
  149. ExpVector e = y.getKey();
  150. C a = y.getValue();
  151. Product<C> p = toProductGen(rfac,a);
  152. if ( p != null && !p.isZERO() ) {
  153. P.doPutToMap( e, p );
  154. }
  155. }
  156. return P;
  157. }
  158. /**
  159. * Product representation.
  160. * @param <C> coefficient type.
  161. * @param pfac polynomial ring factory.
  162. * @param L list of polynomials to be represented.
  163. * @return Product represenation of L in the polynomial ring pfac.
  164. */
  165. public static <C extends GcdRingElem<C>>
  166. List<GenPolynomial<Product<C>>>
  167. toProductGen( GenPolynomialRing<Product<C>> pfac,
  168. List<GenPolynomial<C>> L) {
  169. List<GenPolynomial<Product<C>>>
  170. list = new ArrayList<GenPolynomial<Product<C>>>();
  171. if ( L == null || L.size() == 0 ) {
  172. return list;
  173. }
  174. for ( GenPolynomial<C> a : L ) {
  175. GenPolynomial<Product<C>> b = toProductGen( pfac, a );
  176. list.add( b );
  177. }
  178. return list;
  179. }
  180. /**
  181. * Product representation.
  182. * @param pfac product ring factory.
  183. * @param c coefficient to be represented.
  184. * @return Product represenation of c in the ring pfac.
  185. */
  186. public static
  187. Product<ModInteger> toProduct( ProductRing<ModInteger> pfac,
  188. BigInteger c) {
  189. SortedMap<Integer,ModInteger> elem = new TreeMap<Integer,ModInteger>();
  190. for ( int i = 0; i < pfac.length(); i++ ) {
  191. RingFactory<ModInteger> rfac = pfac.getFactory(i);
  192. ModIntegerRing fac = (ModIntegerRing) rfac;
  193. ModInteger u = fac.fromInteger( c.getVal() );
  194. if ( u != null && !u.isZERO() ) {
  195. elem.put( i, u );
  196. }
  197. }
  198. return new Product<ModInteger>( pfac, elem );
  199. }
  200. /**
  201. * Product representation.
  202. * @param pfac polynomial ring factory.
  203. * @param A polynomial to be represented.
  204. * @return Product represenation of A in the polynomial ring pfac.
  205. */
  206. public static
  207. GenPolynomial<Product<ModInteger>>
  208. toProduct( GenPolynomialRing<Product<ModInteger>> pfac,
  209. GenPolynomial<BigInteger> A) {
  210. GenPolynomial<Product<ModInteger>> P = pfac.getZERO().clone();
  211. if ( A == null || A.isZERO() ) {
  212. return P;
  213. }
  214. RingFactory<Product<ModInteger>> rpfac = pfac.coFac;
  215. ProductRing<ModInteger> fac = (ProductRing<ModInteger>)rpfac;
  216. for ( Map.Entry<ExpVector,BigInteger> y: A.getMap().entrySet() ) {
  217. ExpVector e = y.getKey();
  218. BigInteger a = y.getValue();
  219. Product<ModInteger> p = toProduct(fac,a);
  220. if ( p != null && !p.isZERO() ) {
  221. P.doPutToMap( e, p );
  222. }
  223. }
  224. return P;
  225. }
  226. /**
  227. * Product representation.
  228. * @param pfac polynomial ring factory.
  229. * @param L list of polynomials to be represented.
  230. * @return Product represenation of L in the polynomial ring pfac.
  231. */
  232. public static
  233. List<GenPolynomial<Product<ModInteger>>>
  234. toProduct( GenPolynomialRing<Product<ModInteger>> pfac,
  235. List<GenPolynomial<BigInteger>> L) {
  236. List<GenPolynomial<Product<ModInteger>>>
  237. list = new ArrayList<GenPolynomial<Product<ModInteger>>>();
  238. if ( L == null || L.size() == 0 ) {
  239. return list;
  240. }
  241. for ( GenPolynomial<BigInteger> a : L ) {
  242. GenPolynomial<Product<ModInteger>> b = toProduct( pfac, a );
  243. list.add( b );
  244. }
  245. return list;
  246. }
  247. /**
  248. * Product representation.
  249. * @param <C> coefficient type.
  250. * @param pfac polynomial ring factory.
  251. * @param L list of polynomials to be represented.
  252. * @return Product represenation of L in the polynomial ring pfac.
  253. */
  254. public static <C extends GcdRingElem<C>>
  255. List<GenPolynomial<Product<Residue<C>>>>
  256. toProductRes( GenPolynomialRing<Product<Residue<C>>> pfac,
  257. List<GenPolynomial<GenPolynomial<C>>> L) {
  258. List<GenPolynomial<Product<Residue<C>>>>
  259. list = new ArrayList<GenPolynomial<Product<Residue<C>>>>();
  260. if ( L == null || L.size() == 0 ) {
  261. return list;
  262. }
  263. GenPolynomial<Product<Residue<C>>> b;
  264. for ( GenPolynomial<GenPolynomial<C>> a : L ) {
  265. b = toProductRes( pfac, a );
  266. list.add( b );
  267. }
  268. return list;
  269. }
  270. /**
  271. * Product representation.
  272. * @param <C> coefficient type.
  273. * @param pfac polynomial ring factory.
  274. * @param A polynomial to be represented.
  275. * @return Product represenation of A in the polynomial ring pfac.
  276. */
  277. public static <C extends GcdRingElem<C>>
  278. GenPolynomial<Product<Residue<C>>>
  279. toProductRes( GenPolynomialRing<Product<Residue<C>>> pfac,
  280. GenPolynomial<GenPolynomial<C>> A) {
  281. GenPolynomial<Product<Residue<C>>> P = pfac.getZERO().clone();
  282. if ( A == null || A.isZERO() ) {
  283. return P;
  284. }
  285. RingFactory<Product<Residue<C>>> rpfac = pfac.coFac;
  286. ProductRing<Residue<C>> fac = (ProductRing<Residue<C>>)rpfac;
  287. Product<Residue<C>> p;
  288. for ( Map.Entry<ExpVector,GenPolynomial<C>> y: A.getMap().entrySet() ) {
  289. ExpVector e = y.getKey();
  290. GenPolynomial<C> a = y.getValue();
  291. p = toProductRes(fac,a);
  292. if ( p != null && !p.isZERO() ) {
  293. P.doPutToMap( e, p );
  294. }
  295. }
  296. return P;
  297. }
  298. /**
  299. * Product representation.
  300. * @param <C> coefficient type.
  301. * @param pfac product ring factory.
  302. * @param c coefficient to be represented.
  303. * @return Product represenation of c in the ring pfac.
  304. */
  305. public static <C extends GcdRingElem<C>>
  306. Product<Residue<C>>
  307. toProductRes( ProductRing<Residue<C>> pfac,
  308. GenPolynomial<C> c) {
  309. SortedMap<Integer,Residue<C>> elem = new TreeMap<Integer,Residue<C>>();
  310. for ( int i = 0; i < pfac.length(); i++ ) {
  311. RingFactory<Residue<C>> rfac = pfac.getFactory(i);
  312. ResidueRing<C> fac = (ResidueRing<C>) rfac;
  313. Residue<C> u = new Residue<C>( fac, c );
  314. //fac.fromInteger( c.getVal() );
  315. if ( u != null && !u.isZERO() ) {
  316. elem.put( i, u );
  317. }
  318. }
  319. return new Product<Residue<C>>( pfac, elem );
  320. }
  321. /**
  322. * Product residue representation.
  323. * @param <C> coefficient type.
  324. * @param CS list of ColoredSystems from comprehensive GB system.
  325. * @return Product residue represenation of CS.
  326. */
  327. public static <C extends GcdRingElem<C>>
  328. List<GenPolynomial<Product<Residue<C>>>>
  329. toProductRes(List<ColoredSystem<C>> CS) {
  330. List<GenPolynomial<Product<Residue<C>>>>
  331. list = new ArrayList<GenPolynomial<Product<Residue<C>>>>();
  332. if ( CS == null || CS.size() == 0 ) {
  333. return list;
  334. }
  335. GenPolynomialRing<GenPolynomial<C>> pr = null;
  336. List<RingFactory<Residue<C>>> rrl
  337. = new ArrayList<RingFactory<Residue<C>>>( CS.size() );
  338. for (ColoredSystem<C> cs : CS) {
  339. Ideal<C> id = cs.condition.zero;
  340. ResidueRing<C> r = new ResidueRing<C>(id);
  341. if ( ! rrl.contains(r) ) {
  342. rrl.add(r);
  343. }
  344. if ( pr == null ) {
  345. if ( cs.list.size() > 0 ) {
  346. pr = cs.list.get(0).green.ring;
  347. }
  348. }
  349. }
  350. ProductRing<Residue<C>> pfac;
  351. pfac = new ProductRing<Residue<C>>(rrl);
  352. //System.out.println("pfac = " + pfac);
  353. GenPolynomialRing<Product<Residue<C>>> rf
  354. = new GenPolynomialRing<Product<Residue<C>>>(pfac,pr.nvar,pr.tord,pr.getVars());
  355. GroebnerSystem<C> gs = new GroebnerSystem<C>( CS );
  356. List<GenPolynomial<GenPolynomial<C>>> F = gs.getCGB();
  357. list = PolyUtilApp.<C>toProductRes(rf, F);
  358. return list;
  359. }
  360. /**
  361. * Residue coefficient representation.
  362. * @param pfac polynomial ring factory.
  363. * @param L list of polynomials to be represented.
  364. * @return Represenation of L in the polynomial ring pfac.
  365. */
  366. public static <C extends GcdRingElem<C>>
  367. List<GenPolynomial<Residue<C>>>
  368. toResidue( GenPolynomialRing<Residue<C>> pfac,
  369. List<GenPolynomial<GenPolynomial<C>>> L) {
  370. List<GenPolynomial<Residue<C>>>
  371. list = new ArrayList<GenPolynomial<Residue<C>>>();
  372. if ( L == null || L.size() == 0 ) {
  373. return list;
  374. }
  375. GenPolynomial<Residue<C>> b;
  376. for ( GenPolynomial<GenPolynomial<C>> a : L ) {
  377. b = toResidue( pfac, a );
  378. if ( b != null && !b.isZERO() ) {
  379. list.add( b );
  380. }
  381. }
  382. return list;
  383. }
  384. /**
  385. * Residue coefficient representation.
  386. * @param pfac polynomial ring factory.
  387. * @param A polynomial to be represented.
  388. * @return Represenation of A in the polynomial ring pfac.
  389. */
  390. public static <C extends GcdRingElem<C>>
  391. GenPolynomial<Residue<C>>
  392. toResidue( GenPolynomialRing<Residue<C>> pfac,
  393. GenPolynomial<GenPolynomial<C>> A) {
  394. GenPolynomial<Residue<C>> P = pfac.getZERO().clone();
  395. if ( A == null || A.isZERO() ) {
  396. return P;
  397. }
  398. RingFactory<Residue<C>> rpfac = pfac.coFac;
  399. ResidueRing<C> fac = (ResidueRing<C>)rpfac;
  400. Residue<C> p;
  401. for ( Map.Entry<ExpVector,GenPolynomial<C>> y: A.getMap().entrySet() ) {
  402. ExpVector e = y.getKey();
  403. GenPolynomial<C> a = y.getValue();
  404. p = new Residue<C>(fac,a);
  405. if ( p != null && !p.isZERO() ) {
  406. P.doPutToMap( e, p );
  407. }
  408. }
  409. return P;
  410. }
  411. /**
  412. * Product slice.
  413. * @param <C> coefficient type.
  414. * @param L list of polynomials with product coefficients.
  415. * @return Slices represenation of L.
  416. */
  417. public static <C extends GcdRingElem<C>>
  418. Map<Ideal<C>,PolynomialList<GenPolynomial<C>>>
  419. productSlice( PolynomialList<Product<Residue<C>>> L ) {
  420. Map<Ideal<C>,PolynomialList<GenPolynomial<C>>> map;
  421. RingFactory<Product<Residue<C>>> fpr = L.ring.coFac;
  422. ProductRing<Residue<C>> pr = (ProductRing<Residue<C>>)fpr;
  423. int s = pr.length();
  424. map = new TreeMap<Ideal<C>,PolynomialList<GenPolynomial<C>>>();
  425. List<GenPolynomial<GenPolynomial<C>>> slist;
  426. List<GenPolynomial<Product<Residue<C>>>> plist = L.list;
  427. PolynomialList<GenPolynomial<C>> spl;
  428. for ( int i = 0; i < s; i++ ) {
  429. RingFactory<Residue<C>> r = pr.getFactory( i );
  430. ResidueRing<C> rr = (ResidueRing<C>) r;
  431. Ideal<C> id = rr.ideal;
  432. GenPolynomialRing<C> cof = rr.ring;
  433. GenPolynomialRing<GenPolynomial<C>> pfc;
  434. pfc = new GenPolynomialRing<GenPolynomial<C>>(cof,L.ring);
  435. slist = fromProduct( pfc, plist, i );
  436. spl = new PolynomialList<GenPolynomial<C>>(pfc,slist);
  437. PolynomialList<GenPolynomial<C>> d = map.get( id );
  438. if ( d != null ) {
  439. throw new RuntimeException("ideal exists twice " + id);
  440. }
  441. map.put( id, spl );
  442. }
  443. return map;
  444. }
  445. /**
  446. * Product slice at i.
  447. * @param <C> coefficient type.
  448. * @param L list of polynomials with product coeffients.
  449. * @param i index of slice.
  450. * @return Slice of of L at i.
  451. */
  452. public static <C extends GcdRingElem<C>>
  453. PolynomialList<GenPolynomial<C>>
  454. productSlice( PolynomialList<Product<Residue<C>>> L, int i ) {
  455. RingFactory<Product<Residue<C>>> fpr = L.ring.coFac;
  456. ProductRing<Residue<C>> pr = (ProductRing<Residue<C>>)fpr;
  457. List<GenPolynomial<GenPolynomial<C>>> slist;
  458. List<GenPolynomial<Product<Residue<C>>>> plist = L.list;
  459. PolynomialList<GenPolynomial<C>> spl;
  460. RingFactory<Residue<C>> r = pr.getFactory( i );
  461. ResidueRing<C> rr = (ResidueRing<C>) r;
  462. GenPolynomialRing<C> cof = rr.ring;
  463. GenPolynomialRing<GenPolynomial<C>> pfc;
  464. pfc = new GenPolynomialRing<GenPolynomial<C>>(cof,L.ring);
  465. slist = fromProduct( pfc, plist, i );
  466. spl = new PolynomialList<GenPolynomial<C>>(pfc,slist);
  467. return spl;
  468. }
  469. /**
  470. * From product representation.
  471. * @param <C> coefficient type.
  472. * @param pfac polynomial ring factory.
  473. * @param L list of polynomials to be converted from product representation.
  474. * @param i index of product representation to be taken.
  475. * @return Represenation of i-slice of L in the polynomial ring pfac.
  476. */
  477. public static <C extends GcdRingElem<C>>
  478. List<GenPolynomial<GenPolynomial<C>>>
  479. fromProduct( GenPolynomialRing<GenPolynomial<C>> pfac,
  480. List<GenPolynomial<Product<Residue<C>>>> L,
  481. int i ) {
  482. List<GenPolynomial<GenPolynomial<C>>>
  483. list = new ArrayList<GenPolynomial<GenPolynomial<C>>>();
  484. if ( L == null || L.size() == 0 ) {
  485. return list;
  486. }
  487. GenPolynomial<GenPolynomial<C>> b;
  488. for ( GenPolynomial<Product<Residue<C>>> a : L ) {
  489. b = fromProduct( pfac, a, i );
  490. if ( b != null && !b.isZERO() ) {
  491. b = b.abs();
  492. if ( ! list.contains( b ) ) {
  493. list.add( b );
  494. }
  495. }
  496. }
  497. return list;
  498. }
  499. /**
  500. * From product representation.
  501. * @param <C> coefficient type.
  502. * @param pfac polynomial ring factory.
  503. * @param P polynomial to be converted from product representation.
  504. * @param i index of product representation to be taken.
  505. * @return Represenation of i-slice of P in the polynomial ring pfac.
  506. */
  507. public static <C extends GcdRingElem<C>>
  508. GenPolynomial<GenPolynomial<C>>
  509. fromProduct( GenPolynomialRing<GenPolynomial<C>> pfac,
  510. GenPolynomial<Product<Residue<C>>> P,
  511. int i ) {
  512. GenPolynomial<GenPolynomial<C>> b = pfac.getZERO().clone();
  513. if ( P == null || P.isZERO() ) {
  514. return b;
  515. }
  516. for ( Map.Entry<ExpVector,Product<Residue<C>>> y: P.getMap().entrySet() ) {
  517. ExpVector e = y.getKey();
  518. Product<Residue<C>> a = y.getValue();
  519. Residue<C> r = a.get(i);
  520. if ( r != null && !r.isZERO() ) {
  521. GenPolynomial<C> p = r.val;
  522. if ( p != null && !p.isZERO() ) {
  523. b.doPutToMap( e, p );
  524. }
  525. }
  526. }
  527. return b;
  528. }
  529. /**
  530. * Product slice to String.
  531. * @param <C> coefficient type.
  532. * @param L list of polynomials with to be represented.
  533. * @return Product represenation of L in the polynomial ring pfac.
  534. */
  535. public static <C extends GcdRingElem<C>>
  536. String
  537. productSliceToString( Map<Ideal<C>,PolynomialList<GenPolynomial<C>>> L ) {
  538. Set<GenPolynomial<GenPolynomial<C>>> sl
  539. = new TreeSet<GenPolynomial<GenPolynomial<C>>>();
  540. PolynomialList<GenPolynomial<C>> pl = null;
  541. StringBuffer sb = new StringBuffer(); //"\nproductSlice ----------------- begin");
  542. for ( Ideal<C> id: L.keySet() ) {
  543. sb.append("\n\ncondition == 0:\n");
  544. sb.append( id.list.toScript() );
  545. pl = L.get( id );
  546. sl.addAll( pl.list );
  547. sb.append("\ncorresponding ideal:\n");
  548. sb.append( pl.toScript() );
  549. }
  550. //List<GenPolynomial<GenPolynomial<C>>> sll
  551. // = new ArrayList<GenPolynomial<GenPolynomial<C>>>( sl );
  552. //pl = new PolynomialList<GenPolynomial<C>>(pl.ring,sll);
  553. // sb.append("\nunion = " + pl.toString());
  554. //sb.append("\nproductSlice ------------------------- end\n");
  555. return sb.toString();
  556. }
  557. /**
  558. * Product slice to String.
  559. * @param <C> coefficient type.
  560. * @param L list of polynomials with product coefficients.
  561. * @return string represenation of slices of L.
  562. */
  563. public static <C extends GcdRingElem<C>>
  564. String productToString( PolynomialList<Product<Residue<C>>> L ) {
  565. Map<Ideal<C>,PolynomialList<GenPolynomial<C>>> M;
  566. M = productSlice( L );
  567. String s = productSliceToString( M );
  568. return s;
  569. }
  570. /**
  571. * Construct superset of complex roots for zero dimensional ideal(G).
  572. * @param I zero dimensional ideal.
  573. * @param eps desired precision.
  574. * @return list of coordinates of complex roots for ideal(G)
  575. */
  576. public static <C extends RingElem<C> & Rational, D extends GcdRingElem<D>>
  577. List<List<Complex<BigDecimal>>> complexRootTuples(Ideal<D> I, C eps) {
  578. List<GenPolynomial<D>> univs = I.constructUnivariate();
  579. if ( logger.isInfoEnabled() ) {
  580. logger.info("univs = " + univs);
  581. }
  582. return complexRoots(I,univs,eps);
  583. }
  584. /**
  585. * Construct superset of complex roots for zero dimensional ideal(G).
  586. * @param I zero dimensional ideal.
  587. * @param univs list of univariate polynomials.
  588. * @param eps desired precision.
  589. * @return list of coordinates of complex roots for ideal(G)
  590. */
  591. public static <C extends RingElem<C> & Rational, D extends GcdRingElem<D>>
  592. List<List<Complex<BigDecimal>>> complexRoots(Ideal<D> I, List<GenPolynomial<D>> univs, C eps) {
  593. List<List<Complex<BigDecimal>>> croots = new ArrayList<List<Complex<BigDecimal>>>();
  594. RingFactory<C> cf = (RingFactory<C>) I.list.ring.coFac;
  595. ComplexRing<C> cr = new ComplexRing<C>(cf);
  596. ComplexRootsAbstract<C> cra = new ComplexRootsSturm<C>(cr);
  597. List<GenPolynomial<Complex<C>>> cunivs = new ArrayList<GenPolynomial<Complex<C>>>();
  598. for ( GenPolynomial<D> p : univs ) {
  599. GenPolynomialRing<Complex<C>> pfac = new GenPolynomialRing<Complex<C>>(cr,p.ring);
  600. //System.out.println("pfac = " + pfac.toScript());
  601. GenPolynomial<Complex<C>> cp = PolyUtil.<C> toComplex(pfac,(GenPolynomial<C>) p);
  602. cunivs.add(cp);
  603. //System.out.println("cp = " + cp);
  604. }
  605. for ( int i = 0; i < I.list.ring.nvar; i++ ) {
  606. List<Complex<BigDecimal>> cri = cra.approximateRoots(cunivs.get(i),eps);
  607. //System.out.println("cri = " + cri);
  608. croots.add(cri);
  609. }
  610. croots = ListUtil.<Complex<BigDecimal>> tupleFromList( croots );
  611. return croots;
  612. }
  613. /**
  614. * Construct superset of complex roots for zero dimensional ideal(G).
  615. * @param Il list of zero dimensional ideals with univariate polynomials.
  616. * @param eps desired precision.
  617. * @return list of coordinates of complex roots for ideal(cap_i(G_i))
  618. */
  619. public static <C extends RingElem<C> & Rational, D extends GcdRingElem<D>>
  620. List<List<Complex<BigDecimal>>> complexRootTuples(List<IdealWithUniv<D>> Il, C eps) {
  621. List<List<Complex<BigDecimal>>> croots = new ArrayList<List<Complex<BigDecimal>>>();
  622. for ( IdealWithUniv<D> I : Il ) {
  623. List<List<Complex<BigDecimal>>> cr = complexRoots(I.ideal,I.upolys,eps);
  624. croots.addAll(cr);
  625. }
  626. return croots;
  627. }
  628. /**
  629. * Construct superset of complex roots for zero dimensional ideal(G).
  630. * @param Il list of zero dimensional ideals with univariate polynomials.
  631. * @param eps desired precision.
  632. * @return list of ideals with coordinates of complex roots for ideal(cap_i(G_i))
  633. */
  634. public static <C extends RingElem<C> & Rational, D extends GcdRingElem<D>>
  635. List<IdealWithComplexRoots<D>> complexRoots(List<IdealWithUniv<D>> Il, C eps) {
  636. List<IdealWithComplexRoots<D>> Ic = new ArrayList<IdealWithComplexRoots<D>>(Il.size());
  637. for ( IdealWithUniv<D> I : Il ) {
  638. List<List<Complex<BigDecimal>>> cr = complexRoots(I.ideal,I.upolys,eps);
  639. IdealWithComplexRoots<D> ic = new IdealWithComplexRoots<D>(I,cr);
  640. Ic.add(ic);
  641. }
  642. return Ic;
  643. }
  644. /**
  645. * Construct superset of complex roots for zero dimensional ideal(G).
  646. * @param G list of polynomials of a of zero dimensional ideal.
  647. * @param eps desired precision.
  648. * @return list of ideals with coordinates of complex roots for ideal(G)
  649. */
  650. public static <C extends RingElem<C> & Rational, D extends GcdRingElem<D>>
  651. List<IdealWithComplexRoots<D>> complexRoots(Ideal<D> G, C eps) {
  652. List<IdealWithUniv<D>> Il = G.zeroDimDecomposition();
  653. return complexRoots(Il,eps);
  654. }
  655. /**
  656. * Construct superset of real roots for zero dimensional ideal(G).
  657. * @param I zero dimensional ideal.
  658. * @param eps desired precision.
  659. * @return list of coordinates of real roots for ideal(G)
  660. */
  661. public static <C extends RingElem<C> & Rational, D extends GcdRingElem<D>>
  662. List<List<BigDecimal>> realRootTuples(Ideal<D> I, C eps) {
  663. List<GenPolynomial<D>> univs = I.constructUnivariate();
  664. if ( logger.isInfoEnabled() ) {
  665. logger.info("univs = " + univs);
  666. }
  667. return realRoots(I,univs,eps);
  668. }
  669. /**
  670. * Construct superset of real roots for zero dimensional ideal(G).
  671. * @param I zero dimensional ideal.
  672. * @param univs list of univariate polynomials.
  673. * @param eps desired precision.
  674. * @return list of coordinates of real roots for ideal(G)
  675. */
  676. public static <C extends RingElem<C> & Rational, D extends GcdRingElem<D>>
  677. List<List<BigDecimal>> realRoots(Ideal<D> I, List<GenPolynomial<D>>univs, C eps) {
  678. List<List<BigDecimal>> roots = new ArrayList<List<BigDecimal>>();
  679. RingFactory<C> cf = (RingFactory<C>) I.list.ring.coFac;
  680. RealRootAbstract<C> rra = new RealRootsSturm<C>();
  681. for ( int i = 0; i < I.list.ring.nvar; i++ ) {
  682. List<BigDecimal> rri = rra.approximateRoots((GenPolynomial<C>)univs.get(i),eps);
  683. //System.out.println("rri = " + rri);
  684. roots.add(rri);
  685. }
  686. //System.out.println("roots-1 = " + roots);
  687. roots = ListUtil.<BigDecimal> tupleFromList( roots );
  688. //System.out.println("roots-2 = " + roots);
  689. return roots;
  690. }
  691. /**
  692. * Construct superset of real roots for zero dimensional ideal(G).
  693. * @param Il list of zero dimensional ideals with univariate polynomials.
  694. * @param eps desired precision.
  695. * @return list of coordinates of real roots for ideal(cap_i(G_i))
  696. */
  697. public static <C extends RingElem<C> & Rational, D extends GcdRingElem<D>>
  698. List<List<BigDecimal>> realRootTuples(List<IdealWithUniv<D>> Il, C eps) {
  699. List<List<BigDecimal>> rroots = new ArrayList<List<BigDecimal>>();
  700. for ( IdealWithUniv<D> I : Il ) {
  701. List<List<BigDecimal>> rr = realRoots(I.ideal,I.upolys,eps);
  702. rroots.addAll(rr);
  703. }
  704. return rroots;
  705. }
  706. /**
  707. * Construct superset of real roots for zero dimensional ideal(G).
  708. * @param Il list of zero dimensional ideals with univariate polynomials.
  709. * @param eps desired precision.
  710. * @return list of ideals with coordinates of real roots for ideal(cap_i(G_i))
  711. */
  712. public static <C extends RingElem<C> & Rational, D extends GcdRingElem<D>>
  713. List<IdealWithRealRoots<D>> realRoots(List<IdealWithUniv<D>> Il, C eps) {
  714. List<IdealWithRealRoots<D>> Ir = new ArrayList<IdealWithRealRoots<D>>(Il.size());
  715. for ( IdealWithUniv<D> I : Il ) {
  716. List<List<BigDecimal>> rr = realRoots(I.ideal,I.upolys,eps);
  717. IdealWithRealRoots<D> ir = new IdealWithRealRoots<D>(I,rr);
  718. Ir.add(ir);
  719. }
  720. return Ir;
  721. }
  722. /**
  723. * Construct superset of real roots for zero dimensional ideal(G).
  724. * @param G list of polynomials of a of zero dimensional ideal.
  725. * @param eps desired precision.
  726. * @return list of ideals with coordinates of real roots for ideal(G)
  727. */
  728. public static <C extends RingElem<C> & Rational, D extends GcdRingElem<D>>
  729. List<IdealWithRealRoots<D>> realRoots(Ideal<D> G, C eps) {
  730. List<IdealWithUniv<D>> Il = G.zeroDimDecomposition();
  731. return realRoots(Il,eps);
  732. }
  733. /**
  734. * Test for real roots of zero dimensional ideal(L).
  735. * @param L list of polynomials.
  736. * @param roots list of real roots for ideal(G).
  737. * @param eps desired precision.
  738. * @return true if root is a list of coordinates of real roots for ideal(L)
  739. */
  740. public static
  741. boolean isRealRoots(List<GenPolynomial<BigDecimal>> L, List<List<BigDecimal>> roots, BigDecimal eps) {
  742. if ( L == null || L.size() == 0 ) {
  743. return true;
  744. }
  745. // polynomials with decimal coefficients
  746. BigDecimal dc = BigDecimal.ONE;
  747. GenPolynomialRing<BigDecimal> dfac = L.get(0).ring;
  748. //System.out.println("dfac = " + dfac);
  749. for ( GenPolynomial<BigDecimal> dp : L ) {
  750. //System.out.println("dp = " + dp);
  751. for ( List<BigDecimal> r : roots ) {
  752. //System.out.println("r = " + r);
  753. BigDecimal ev = PolyUtil.<BigDecimal> evaluateAll(dc,dfac,dp,r);
  754. if ( ev.abs().compareTo(eps) > 0 ) {
  755. System.out.println("ev = " + ev);
  756. return false;
  757. }
  758. }
  759. }
  760. return true;
  761. }
  762. /**
  763. * Test for complex roots of zero dimensional ideal(L).
  764. * @param L list of polynomials.
  765. * @param roots list of real roots for ideal(G).
  766. * @param eps desired precision.
  767. * @return true if root is a list of coordinates of complex roots for ideal(L)
  768. */
  769. public static
  770. boolean isComplexRoots(List<GenPolynomial<Complex<BigDecimal>>> L,
  771. List<List<Complex<BigDecimal>>> roots, BigDecimal eps) {
  772. if ( L == null || L.size() == 0 ) {
  773. return true;
  774. }
  775. // polynomials with decimal coefficients
  776. BigDecimal dc = BigDecimal.ONE;
  777. ComplexRing<BigDecimal> dcc = new ComplexRing<BigDecimal>(dc);
  778. GenPolynomialRing<Complex<BigDecimal>> dfac = L.get(0).ring;
  779. //System.out.println("dfac = " + dfac);
  780. for ( GenPolynomial<Complex<BigDecimal>> dp : L ) {
  781. //System.out.println("dp = " + dp);
  782. for ( List<Complex<BigDecimal>> r : roots ) {
  783. //System.out.println("r = " + r);
  784. Complex<BigDecimal> ev = PolyUtil.<Complex<BigDecimal>> evaluateAll(dcc,dfac,dp,r);
  785. if ( ev.norm().getRe().compareTo(eps) > 0 ) {
  786. System.out.println("ev = " + ev);
  787. return false;
  788. }
  789. }
  790. }
  791. return true;
  792. }
  793. /**
  794. * Construct real roots for zero dimensional ideal(G).
  795. * @param I zero dimensional ideal with univariate irreducible polynomials.
  796. * @return list of real algebraic roots for ideal(G)
  797. */
  798. public static <C extends RingElem<C> & Rational, D extends GcdRingElem<D> & Rational>
  799. IdealWithRealAlgebraicRoots<C,D> realAlgebraicRoots(IdealWithUniv<D> I) {
  800. List<List<RealAlgebraicNumber<D>>> ran = new ArrayList<List<RealAlgebraicNumber<D>>>();
  801. if ( I == null || I.ideal == null || I.ideal.isZERO() || I.upolys == null || I.upolys.size() == 0 ) {
  802. return new IdealWithRealAlgebraicRoots<C,D>(I,ran);
  803. }
  804. GenPolynomialRing<D> fac = I.ideal.list.ring;
  805. // case i == 0:
  806. GenPolynomial<D> p0 = I.upolys.get(0);
  807. GenPolynomial<D> p0p = selectWithVariable(I.ideal.list.list, fac.nvar-1 );
  808. if ( p0p == null ) {
  809. throw new RuntimeException("no polynomial found in " + (fac.nvar-1) + " of " + I.ideal);
  810. }
  811. //System.out.println("p0 = " + p0);
  812. if ( logger.isInfoEnabled() ) {
  813. logger.info("p0p = " + p0p);
  814. }
  815. int[] dep0 = p0p.degreeVector().dependencyOnVariables();
  816. //System.out.println("dep0 = " + Arrays.toString(dep0));
  817. if ( dep0.length != 1 ) {
  818. throw new RuntimeException("wrong number of variables " + Arrays.toString(dep0));
  819. }
  820. List<RealAlgebraicNumber<D>> rra = RootFactory.<D> realAlgebraicNumbersIrred(p0);
  821. for ( RealAlgebraicNumber<D> rr : rra ) {
  822. List<RealAlgebraicNumber<D>> rl = new ArrayList<RealAlgebraicNumber<D>>();
  823. rl.add(rr);
  824. ran.add(rl);
  825. }
  826. // case i > 0:
  827. for ( int i = 1; i < I.upolys.size(); i++ ) {
  828. List<List<RealAlgebraicNumber<D>>> rn = new ArrayList<List<RealAlgebraicNumber<D>>>();
  829. GenPolynomial<D> pi = I.upolys.get(i);
  830. GenPolynomial<D> pip = selectWithVariable(I.ideal.list.list, fac.nvar-1-i );
  831. if ( pip == null ) {
  832. throw new RuntimeException("no polynomial found in " + (fac.nvar-1-i) + " of " + I.ideal);
  833. }
  834. //System.out.println("i = " + i);
  835. //System.out.println("pi = " + pi);
  836. if ( logger.isInfoEnabled() ) {
  837. logger.info("pip = " + pip);
  838. }
  839. int[] depi = pip.degreeVector().dependencyOnVariables();
  840. //System.out.println("depi = " + Arrays.toString(depi));
  841. if ( depi.length < 1 || depi.length > 2 ) {
  842. throw new RuntimeException("wrong number of variables " + Arrays.toString(depi));
  843. }
  844. rra = RootFactory.<D> realAlgebraicNumbersIrred(pi);
  845. if ( depi.length == 1 ) {
  846. // all combinations are roots of the ideal I
  847. for ( RealAlgebraicNumber<D> rr : rra ) {
  848. //System.out.println("rr.ring = " + rr.ring);
  849. for ( List<RealAlgebraicNumber<D>> rx : ran ) {
  850. //System.out.println("rx = " + rx);
  851. List<RealAlgebraicNumber<D>> ry = new ArrayList<RealAlgebraicNumber<D>>();
  852. ry.addAll(rx);
  853. ry.add(rr);
  854. rn.add(ry);
  855. }
  856. }
  857. } else { // depi.length == 2
  858. // select roots of the ideal I
  859. GenPolynomial<D> pip2 = removeUnusedUpperVariables(pip);
  860. //System.out.println("pip2 = " + pip2.ring);
  861. GenPolynomialRing<D> ufac = ufac = pip2.ring.contract(1);
  862. GenPolynomialRing<GenPolynomial<D>> rfac = new GenPolynomialRing<GenPolynomial<D>>(ufac,1);
  863. GenPolynomial<GenPolynomial<D>> pip2r = PolyUtil.<D> recursive(rfac,pip2);
  864. int ix = fac.nvar - 1 - depi[ depi.length-1 ];
  865. //System.out.println("ix = " + ix);
  866. for ( RealAlgebraicNumber<D> rr : rra ) {
  867. //System.out.println("rr.ring = " + rr.ring);
  868. GenPolynomial<D> pip2el = PolyUtil.<D> evaluateMain(ufac,pip2r,rr.ring.getRoot().left);
  869. GenPolynomial<D> pip2er = PolyUtil.<D> evaluateMain(ufac,pip2r,rr.ring.getRoot().right);
  870. GenPolynomialRing<D> upfac = I.upolys.get(ix).ring;
  871. GenPolynomial<D> pip2elc = convert(upfac,pip2el);
  872. GenPolynomial<D> pip2erc = convert(upfac,pip2er);
  873. //System.out.println("pip2elc = " + pip2elc);
  874. //System.out.println("pip2erc = " + pip2erc);
  875. for ( List<RealAlgebraicNumber<D>> rx : ran ) {
  876. //System.out.println("rx = " + rx);
  877. RealAlgebraicRing<D> rar = rx.get(ix).ring;
  878. RealAlgebraicNumber<D> rel = new RealAlgebraicNumber<D>(rar,pip2elc);
  879. RealAlgebraicNumber<D> rer = new RealAlgebraicNumber<D>(rar,pip2erc);
  880. int sl = rel.signum();
  881. int sr = rer.signum();
  882. //System.out.println("sl = " + sl + ", sr = " + sr + ", sl*sr = " + (sl*sr));
  883. if ( sl*sr <= 0 ) {
  884. List<RealAlgebraicNumber<D>> ry = new ArrayList<RealAlgebraicNumber<D>>();
  885. ry.addAll(rx);
  886. ry.add(rr);
  887. rn.add(ry);
  888. }
  889. }
  890. }
  891. }
  892. ran = rn;
  893. }
  894. IdealWithRealAlgebraicRoots<C,D> Ir = new IdealWithRealAlgebraicRoots<C,D>(I,ran);
  895. return Ir;
  896. }
  897. /**
  898. * Construct real roots for zero dimensional ideal(G).
  899. * @param I list of zero dimensional ideal with univariate irreducible polynomials.
  900. * @return list of real algebraic roots for all ideal(I_i)
  901. */
  902. public static <C extends RingElem<C> & Rational, D extends GcdRingElem<D> & Rational>
  903. List<IdealWithRealAlgebraicRoots<C,D>> realAlgebraicRoots(List<IdealWithUniv<D>> I) {
  904. List<IdealWithRealAlgebraicRoots<C,D>> lir = new ArrayList<IdealWithRealAlgebraicRoots<C,D>>(I.size());
  905. for ( IdealWithUniv<D> iu : I ) {
  906. IdealWithRealAlgebraicRoots<C,D> iur = PolyUtilApp.<C,D> realAlgebraicRoots(iu);
  907. //System.out.println("iur = " + iur);
  908. lir.add( iur );
  909. }
  910. return lir;
  911. }
  912. /**
  913. * Select polynomial with univariate leading term in variable i.
  914. * @param i variable index.
  915. * @return polynomial with head term in variable i
  916. */
  917. public static <C extends RingElem<C>>
  918. GenPolynomial<C> selectWithVariable(List<GenPolynomial<C>> P, int i) {
  919. for ( GenPolynomial<C> p : P ) {
  920. int[] dep = p.leadingExpVector().dependencyOnVariables();
  921. if ( dep.length == 1 && dep[0] == i ) {
  922. return p;
  923. }
  924. }
  925. return null; // not found
  926. }
  927. /**
  928. * Remove all upper variables, which do not occur in polynomial.
  929. * @param p polynomial.
  930. * @return polynomial with removed variables
  931. */
  932. public static <C extends RingElem<C>>
  933. GenPolynomial<C> removeUnusedUpperVariables(GenPolynomial<C> p) {
  934. int[] dep = p.degreeVector().dependencyOnVariables();
  935. GenPolynomialRing<C> fac = p.ring;
  936. if ( fac.nvar == dep.length ) { // all variables appear
  937. return p;
  938. }
  939. int l = dep[0];
  940. int r = dep[dep.length-1];
  941. int n = l;
  942. GenPolynomialRing<C> facr = fac.contract(n);
  943. Map<ExpVector,GenPolynomial<C>> mpr = p.contract(facr);
  944. if ( mpr.size() != 1 ) {
  945. throw new RuntimeException("this should not happen " + mpr);
  946. }
  947. GenPolynomial<C> pr = mpr.values().iterator().next();
  948. n = fac.nvar-1-r;
  949. if ( n == 0 ) {
  950. return pr;
  951. } // else case not implemented
  952. return pr;
  953. }
  954. /**
  955. * Convert to a polynomial in given ring.
  956. * @param fac result polynomial ring.
  957. * @param p polynomial.
  958. * @return polynomial in ring fac
  959. * <b>Note: </b> if p can not be represented in fac then the results are unpredictable.
  960. */
  961. public static <C extends RingElem<C>>
  962. GenPolynomial<C> convert(GenPolynomialRing<C> fac, GenPolynomial<C> p) {
  963. GenPolynomial<C> q = fac.parse( p.toString() );
  964. return q;
  965. }
  966. /**
  967. * Construct exact set of real roots for zero dimensional ideal(G).
  968. * @param I zero dimensional ideal.
  969. * @return list of coordinates of real roots for ideal(G)
  970. */
  971. public static <C extends RingElem<C> & Rational, D extends GcdRingElem<D> & Rational>
  972. List<IdealWithRealAlgebraicRoots<C,D>> realAlgebraicRoots(Ideal<D> I) {
  973. List<IdealWithUniv<D>> Ir = I.zeroDimRootDecomposition();
  974. //System.out.println("Ir = " + Ir);
  975. List<IdealWithRealAlgebraicRoots<C,D>> roots = PolyUtilApp.<C,D> realAlgebraicRoots(Ir);
  976. return roots;
  977. }
  978. /**
  979. * Construct primitive element for double field extension.
  980. * @param a algebraic number ring with squarefree monic minimal polynomial
  981. * @param b algebraic number ring with squarefree monic minimal polynomial
  982. * @return primitive element container with algebraic number ring c, with Q(c) = Q(a,b)
  983. */
  984. public static <C extends GcdRingElem<C>>
  985. PrimitiveElement<C> primitiveElement(AlgebraicNumberRing<C> a, AlgebraicNumberRing<C> b) {
  986. GenPolynomial<C> ap = a.modul;
  987. GenPolynomial<C> bp = b.modul;
  988. // setup bivariate polynomial ring
  989. String[] cv = new String[2];
  990. cv[0] = ap.ring.getVars()[0];
  991. cv[1] = bp.ring.getVars()[0];
  992. TermOrder to = new TermOrder(TermOrder.INVLEX);
  993. GenPolynomialRing<C> cfac = new GenPolynomialRing<C>(ap.ring.coFac,2,to,cv);
  994. GenPolynomial<C> as = ap.extendUnivariate(cfac,0);
  995. GenPolynomial<C> bs = bp.extendUnivariate(cfac,1);
  996. List<GenPolynomial<C>> L = new ArrayList<GenPolynomial<C>>(2);
  997. L.add(as);
  998. L.add(bs);
  999. List<GenPolynomial<C>> Op = new ArrayList<GenPolynomial<C>>();
  1000. Ideal<C> id = new Ideal<C>(cfac,L);
  1001. //System.out.println("id = " + id);
  1002. IdealWithUniv<C> iu = id.normalPositionFor(0,1,Op);
  1003. //System.out.println("iu = " + iu);
  1004. // extract result polynomials
  1005. List<GenPolynomial<C>> Np = iu.ideal.getList();
  1006. as = Np.get(1); // take care
  1007. bs = Np.get(0); // take care
  1008. GenPolynomial<C> cs = Np.get(2);
  1009. //System.out.println("as = " + as);
  1010. //System.out.println("bs = " + bs);
  1011. //System.out.println("cs = " + cs);
  1012. String[] ev = new String[] { cs.ring.getVars()[0] };
  1013. GenPolynomialRing<C> efac = new GenPolynomialRing<C>(ap.ring.coFac,1,to,ev);
  1014. //System.out.println("efac = " + efac);
  1015. cs = cs.contractCoeff(efac);
  1016. //System.out.println("cs = " + cs);
  1017. as = as.reductum().contractCoeff(efac);
  1018. as = as.negate();
  1019. //System.out.println("as = " + as);
  1020. bs = bs.reductum().contractCoeff(efac);
  1021. bs = bs.negate();
  1022. //System.out.println("bs = " + bs);
  1023. AlgebraicNumberRing<C> c = new AlgebraicNumberRing<C>(cs);
  1024. AlgebraicNumber<C> ab = new AlgebraicNumber<C>(c,as);
  1025. AlgebraicNumber<C> bb = new AlgebraicNumber<C>(c,bs);
  1026. PrimitiveElement<C> pe = new PrimitiveElement<C>(c,ab,bb,a,b);
  1027. if ( logger.isInfoEnabled() ) {
  1028. logger.info("primitive element = " + c);
  1029. }
  1030. return pe;
  1031. }
  1032. /**
  1033. * Convert to primitive element ring.
  1034. * @param cfac primitive element ring.
  1035. * @param A algebraic number representing the generating element of a in the new ring.
  1036. * @param a algebraic number to convert.
  1037. * @return a converted to the primitive element ring
  1038. */
  1039. public static <C extends GcdRingElem<C>>
  1040. AlgebraicNumber<C> convertToPrimitiveElem(AlgebraicNumberRing<C> cfac,
  1041. AlgebraicNumber<C> A, AlgebraicNumber<C> a) {
  1042. GenPolynomialRing<C> aufac = a.ring.ring;
  1043. GenPolynomialRing<AlgebraicNumber<C>> ar = new GenPolynomialRing<AlgebraicNumber<C>>(cfac,aufac);
  1044. GenPolynomial<AlgebraicNumber<C>> aps = PolyUtil.<C> convertToAlgebraicCoefficients(ar,a.val);
  1045. AlgebraicNumber<C> ac = PolyUtil.<AlgebraicNumber<C>> evaluateMain(cfac,aps,A);
  1046. return ac;
  1047. }
  1048. /**
  1049. * Convert coefficients to primitive element ring.
  1050. * @param cfac primitive element ring.
  1051. * @param A algebraic number representing the generating element of a in the new ring.
  1052. * @param a polynomial with coefficients algebraic number to convert.
  1053. * @return a with coefficients converted to the primitive element ring
  1054. */
  1055. public static <C extends GcdRingElem<C>>
  1056. GenPolynomial<AlgebraicNumber<C>> convertToPrimitiveElem(AlgebraicNumberRing<C> cfac,
  1057. AlgebraicNumber<C> A, GenPolynomial<AlgebraicNumber<C>> a) {
  1058. GenPolynomialRing<AlgebraicNumber<C>> cr = new GenPolynomialRing<AlgebraicNumber<C>>(cfac,a.ring);
  1059. return PolyUtil.<AlgebraicNumber<C>,AlgebraicNumber<C>> map(cr,a,new CoeffConvertAlg<C>(cfac,A) );
  1060. }
  1061. /**
  1062. * Convert to primitive element ring.
  1063. * @param cfac primitive element ring.
  1064. * @param A algebraic number representing the generating element of a in the new ring.
  1065. * @param a recursive algebraic number to convert.
  1066. * @return a converted to the primitive element ring
  1067. */
  1068. public static <C extends GcdRingElem<C>>
  1069. AlgebraicNumber<C> convertToPrimitiveElem(AlgebraicNumberRing<C> cfac,
  1070. AlgebraicNumber<C> A, AlgebraicNumber<C> B, AlgebraicNumber<AlgebraicNumber<C>> a) {
  1071. GenPolynomial<AlgebraicNumber<C>> aps = PolyUtilApp.<C> convertToPrimitiveElem(cfac,A,a.val);
  1072. AlgebraicNumber<C> ac = PolyUtil.<AlgebraicNumber<C>> evaluateMain(cfac,aps,B);
  1073. return ac;
  1074. }
  1075. /**
  1076. * Construct primitive element for double field extension.
  1077. * @param b algebraic number ring with squarefree monic minimal polynomial over Q(a)
  1078. * @return primitive element container with algebraic number ring c, with Q(c) = Q(a)(b)
  1079. */
  1080. public static <C extends GcdRingElem<C>>
  1081. PrimitiveElement<C> primitiveElement(AlgebraicNumberRing<AlgebraicNumber<C>> b) {
  1082. GenPolynomial<AlgebraicNumber<C>> bp = b.modul;
  1083. AlgebraicNumberRing<C> a = (AlgebraicNumberRing<C>) b.ring.coFac;
  1084. GenPolynomial<C> ap = a.modul;
  1085. //System.out.println("ap = " + ap);
  1086. //System.out.println("bp = " + bp);
  1087. // setup bivariate polynomial ring
  1088. String[] cv = new String[2];
  1089. cv[0] = ap.ring.getVars()[0];
  1090. cv[1] = bp.ring.getVars()[0];
  1091. TermOrder to = new TermOrder(TermOrder.INVLEX);
  1092. GenPolynomialRing<C> cfac = new GenPolynomialRing<C>(ap.ring.coFac,2,to,cv);
  1093. GenPolynomialRing<GenPolynomial<C>> rfac
  1094. = new GenPolynomialRing<GenPolynomial<C>>(a.ring,1,bp.ring.getVars());
  1095. GenPolynomial<C> as = ap.extendUnivariate(cfac,0);
  1096. GenPolynomial<GenPolynomial<C>> bss = PolyUtil.<C> fromAlgebraicCoefficients(rfac,bp);
  1097. GenPolynomial<C> bs = PolyUtil.<C> distribute(cfac,bss);
  1098. //System.out.println("as = " + as);
  1099. //System.out.println("bs = " + bs);
  1100. List<GenPolynomial<C>> L = new ArrayList<GenPolynomial<C>>(2);
  1101. L.add(as);
  1102. L.add(bs);
  1103. List<GenPolynomial<C>> Op = new ArrayList<GenPolynomial<C>>();
  1104. Ideal<C> id = new Ideal<C>(cfac,L);
  1105. //System.out.println("id = " + id);
  1106. IdealWithUniv<C> iu = id.normalPositionFor(0,1,Op);
  1107. //System.out.println("iu = " + iu);
  1108. // extract result polynomials
  1109. List<GenPolynomial<C>> Np = iu.ideal.getList();
  1110. as = Np.get(1);
  1111. bs = Np.get(0);
  1112. GenPolynomial<C> cs = Np.get(2);
  1113. //System.out.println("as = " + as);
  1114. //System.out.println("bs = " + bs);
  1115. //System.out.println("cs = " + cs);
  1116. String[] ev = new String[] { cs.ring.getVars()[0] };
  1117. GenPolynomialRing<C> efac = new GenPolynomialRing<C>(ap.ring.coFac,1,to,ev);
  1118. // System.out.println("efac = " + efac);
  1119. cs = cs.contractCoeff(efac);
  1120. // System.out.println("cs = " + cs);
  1121. as =