001/*
002 * $Id$
003 */
004
005package edu.jas.commons.math;
006
007
008import java.util.ArrayList;
009import java.util.List;
010
011import org.apache.commons.math3.linear.ArrayFieldVector;
012import org.apache.commons.math3.linear.BlockFieldMatrix;
013import org.apache.commons.math3.linear.FieldMatrix;
014import org.apache.commons.math3.linear.FieldVector;
015
016import edu.jas.structure.RingElem;
017import edu.jas.vector.GenMatrix;
018import edu.jas.vector.GenMatrixRing;
019import edu.jas.vector.GenVector;
020import edu.jas.vector.GenVectorModul;
021
022
023/**
024 * Conversion methods from JAS to commons-math and vice versa.
025 * @author Heinz Kredel
026 */
027
028public class CMFieldElementUtil {
029
030
031    public static <C extends RingElem<C>> C[] toArray(GenVector<C> a) {
032        if (a == null) {
033            return null;
034        }
035        return toArray(a.val);
036    }
037
038
039    @SuppressWarnings("unchecked")
040    public static <C extends RingElem<C>> C[] toArray(List<C> a) {
041        if (a == null) {
042            return null;
043        }
044        C[] av = (C[]) new RingElem[a.size()];
045        int i = 0;
046        for (C e : a) {
047            av[i++] = e;
048        }
049        return av;
050    }
051
052
053    public static <C extends RingElem<C>> ArrayList<C> toList(C[] a) {
054        if (a == null) {
055            return null;
056        }
057        ArrayList<C> av = new ArrayList<C>(a.length);
058        for (int i = 0; i < a.length; i++) {
059            av.add(a[i]);
060        }
061        return av;
062    }
063
064
065    public static <C extends RingElem<C>> ArrayList<ArrayList<C>> toList(C[][] a) {
066        if (a == null) {
067            return null;
068        }
069        ArrayList<ArrayList<C>> av = new ArrayList<ArrayList<C>>(a.length);
070        for (int i = 0; i < a.length; i++) {
071            av.add(CMFieldElementUtil.<C> toList(a[i]));
072        }
073        return av;
074    }
075
076
077    public static <C extends RingElem<C>> C[][] toArray(GenMatrix<C> a) {
078        if (a == null) {
079            return null;
080        }
081        return toArrayFromMatrix(a.matrix);
082    }
083
084
085    @SuppressWarnings("unchecked")
086    public static <C extends RingElem<C>> C[][] toArrayFromMatrix(List<ArrayList<C>> a) { // Array
087        // only
088        // once
089        if (a == null) {
090            return null;
091        }
092        C[][] av = (C[][]) new RingElem[a.size()][];
093        int i = 0;
094        for (List<C> e : a) {
095            av[i++] = toArray(e);
096        }
097        return av;
098    }
099
100
101    /**
102     * Convert JAS <code>RingElem</code> to commons-math
103     * <code>FieldElement</code>.
104     * @param <C> ring element type
105     * @param v array of ring elements
106     * @return array of CMFieldElement objects
107     */
108    @SuppressWarnings({ "unchecked", "cast" })
109    public static <C extends RingElem<C>> CMFieldElement<C>[] toCMFieldElement(C[] v) {
110        if (v == null) {
111            return null;
112        }
113        CMFieldElement<C>[] va = (CMFieldElement<C>[]) new CMFieldElement[v.length];
114        for (int i = 0; i < v.length; i++) {
115            va[i] = new CMFieldElement<C>(v[i]);
116        }
117        return va;
118    }
119
120
121    /**
122     * Convert JAS <code>RingElem</code> to commons-math
123     * <code>FieldElement</code>.
124     * @param <C> ring element type
125     * @param v array of ring elements
126     * @return array of CMFieldElement objects
127     */
128    @SuppressWarnings({ "unchecked", "cast" })
129    public static <C extends RingElem<C>> CMFieldElement<C>[] toCMFieldElementRE(RingElem<C>[] v) {
130        if (v == null) {
131            return null;
132        }
133        CMFieldElement<C>[] va = (CMFieldElement<C>[]) new CMFieldElement[v.length];
134        for (int i = 0; i < v.length; i++) {
135            va[i] = new CMFieldElement<C>((C) v[i]);
136        }
137        return va;
138    }
139
140
141    /**
142     * Convert JAS <code>RingElem</code> to commons-math
143     * <code>FieldElement</code>.
144     * @param <C> ring element type
145     * @param v JAS vector of ring elements
146     * @return array of CMFieldElement objects
147     */
148    public static <C extends RingElem<C>> CMFieldElement<C>[] toCMFieldElement(GenVector<C> v) {
149        if (v == null) {
150            return null;
151        }
152        CMFieldElement<C>[] va = CMFieldElementUtil.<C> toCMFieldElement(v.val);
153        return va;
154    }
155
156
157    /**
158     * Convert JAS <code>RingElem</code> to commons-math
159     * <code>FieldElement</code>.
160     * @param <C> ring element type
161     * @param v list of ring elements
162     * @return array of CMFieldElement objects
163     */
164    @SuppressWarnings({ "unchecked", "cast" })
165    public static <C extends RingElem<C>> CMFieldElement<C>[] toCMFieldElement(List<C> v) {
166        if (v == null) {
167            return null;
168        }
169        CMFieldElement<C>[] va = (CMFieldElement<C>[]) new CMFieldElement[v.size()];
170        for (int i = 0; i < v.size(); i++) {
171            va[i] = new CMFieldElement<C>(v.get(i));
172        }
173        return va;
174    }
175
176
177    /**
178     * Convert JAS <code>RingElem</code> to commons-math
179     * <code>FieldElement</code>.
180     * @param <C> ring element type
181     * @param v JAS vector of ring elements
182     * @return commons-math vector of CMFieldElementr objects
183     */
184    public static <C extends RingElem<C>> FieldVector<CMFieldElement<C>> toCMFieldElementVector(GenVector<C> v) {
185        if (v == null) {
186            return null;
187        }
188        return new ArrayFieldVector<CMFieldElement<C>>(CMFieldElementUtil.<C> toCMFieldElement(v.val));
189    }
190
191
192    /**
193     * Convert JAS <code>RingElem</code> to commons-math
194     * <code>FieldElement</code>.
195     * @param <C> ring element type
196     * @param v matrix of ring elements
197     * @return matrix of CMFieldElement objects
198     */
199    @SuppressWarnings({ "unchecked", "cast" })
200    public static <C extends RingElem<C>> CMFieldElement<C>[][] toCMFieldElement(C[][] v) {
201        if (v == null) {
202            return null;
203        }
204        CMFieldElement<C>[][] va = (CMFieldElement<C>[][]) new CMFieldElement[v.length][];
205        for (int i = 0; i < v.length; i++) {
206            va[i] = CMFieldElementUtil.<C> toCMFieldElement(v[i]);
207        }
208        return va;
209    }
210
211
212    /**
213     * Convert JAS <code>RingElem</code> to commons-math
214     * <code>FieldElement</code>.
215     * @param <C> ring element type
216     * @param v matrix of ring elements
217     * @return matrix of CMFieldElement objects
218     */
219    @SuppressWarnings({ "unchecked", "cast" })
220    public static <C extends RingElem<C>> CMFieldElement<C>[][] toCMFieldElementRE(RingElem<C>[][] v) {
221        if (v == null) {
222            return null;
223        }
224        CMFieldElement<C>[][] va = (CMFieldElement<C>[][]) new CMFieldElement[v.length][];
225        for (int i = 0; i < v.length; i++) {
226            va[i] = CMFieldElementUtil.<C> toCMFieldElementRE(v[i]);
227        }
228        return va;
229    }
230
231
232    /**
233     * Convert JAS <code>RingElem</code> to commons-math
234     * <code>FieldElement</code>.
235     * @param <C> ring element type
236     * @param v JAS matrix of ring elements
237     * @return matrix of CMFieldElement objects
238     */
239    public static <C extends RingElem<C>> CMFieldElement<C>[][] toCMFieldElement(GenMatrix<C> v) {
240        if (v == null) {
241            return null;
242        }
243        CMFieldElement<C>[][] va = CMFieldElementUtil.<C> toCMFieldElementFromMatrix(v.matrix);
244        return va;
245    }
246
247
248    /**
249     * Convert JAS <code>RingElem</code> to commons-math
250     * <code>FieldElement</code>.
251     * @param <C> ring element type
252     * @param v list of lists of ring elements
253     * @return array of CMFieldElement objects
254     */
255    @SuppressWarnings({ "unchecked", "cast" })
256    public static <C extends RingElem<C>> CMFieldElement<C>[][] toCMFieldElementFromMatrix(
257                    List<ArrayList<C>> v) {
258        if (v == null) {
259            return null;
260        }
261        CMFieldElement<C>[][] va = (CMFieldElement<C>[][]) new CMFieldElement[v.size()][];
262        for (int i = 0; i < v.size(); i++) {
263            va[i] = CMFieldElementUtil.<C> toCMFieldElement(v.get(i));
264        }
265        return va;
266    }
267
268
269    /**
270     * Convert JAS <code>RingElem</code> to commons-math
271     * <code>FieldElement</code>.
272     * @param <C> ring element type
273     * @param v JAS vector of ring elements
274     * @return commons-math FieldMatrix of CMFieldElement objects
275     */
276    public static <C extends RingElem<C>> FieldMatrix<CMFieldElement<C>> toCMFieldMatrix(GenMatrix<C> v) {
277        if (v == null) {
278            return null;
279        }
280        FieldMatrix<CMFieldElement<C>> va = new BlockFieldMatrix<CMFieldElement<C>>(
281                        CMFieldElementUtil.<C> toCMFieldElementFromMatrix(v.matrix));
282        return va;
283    }
284
285
286    /**
287     * Convert commons-math <code>FieldElement</code> to JAS
288     * <code>RingElem</code> to.
289     * @param <C> ring element type
290     * @param v array of CMFieldElement objects
291     * @return array of ring elements
292     */
293    @SuppressWarnings("unchecked")
294    public static <C extends RingElem<C>> C[] fromCMFieldElement(CMFieldElement<C>[] v) {
295        if (v == null) {
296            return null;
297        }
298        C[] va = (C[]) new RingElem[v.length];
299        for (int i = 0; i < v.length; i++) {
300            if (v[i] != null) {
301                va[i] = v[i].val;
302            } else {
303                va[i] = null;
304            }
305        }
306        return va;
307    }
308
309
310    /**
311     * Convert commons-math <code>FieldElement</code> to JAS
312     * <code>RingElem</code> to.
313     * @param <C> ring element type
314     * @param v matrix of CMFieldElement objects
315     * @return matrix of ring elements
316     */
317    @SuppressWarnings("unchecked")
318    public static <C extends RingElem<C>> C[][] fromCMFieldElement(CMFieldElement<C>[][] v) {
319        if (v == null) {
320            return null;
321        }
322        C[][] va = (C[][]) new RingElem[v.length][];
323        for (int i = 0; i < v.length; i++) {
324            va[i] = CMFieldElementUtil.<C> fromCMFieldElement(v[i]);
325        }
326        return va;
327    }
328
329
330    /**
331     * Convert commons-math <code>FieldElement</code> to JAS
332     * <code>RingElem</code> to.
333     * @param <C> ring element type
334     * @param v Commons-math vector of CMFieldElement objects
335     * @return array of ring elements
336     */
337    @SuppressWarnings("unchecked")
338    public static <C extends RingElem<C>> C[] fromCMFieldVector(FieldVector<CMFieldElement<C>> v) {
339        if (v == null) {
340            return null;
341        }
342        C[] va = (C[]) new RingElem[v.getDimension()];
343        for (int i = 0; i < va.length; i++) {
344            CMFieldElement<C> e = v.getEntry(i);
345            if (e != null) {
346                va[i] = e.val;
347            } else {
348                va[i] = null;
349            }
350        }
351        return va;
352    }
353
354
355    /**
356     * Convert commons-math <code>FieldElement</code> to JAS
357     * <code>RingElem</code> to.
358     * 
359     * @param <C> ring element type
360     * @param v commons-math vector of CMFieldElement objects
361     * @return Java list of ring elements
362     */
363    public static <C extends RingElem<C>> ArrayList<C> listFromCMFieldVector(FieldVector<CMFieldElement<C>> v) {
364        if (v == null) {
365            return null;
366        }
367        ArrayList<C> vv = new ArrayList<C>(v.getDimension());
368        for (int i = 0; i < v.getDimension(); i++) {
369            CMFieldElement<C> e = v.getEntry(i);
370            if (e != null) {
371                vv.add(e.val);
372            } else {
373                vv.add(null);
374            }
375        }
376        return vv;
377    }
378
379
380    /**
381     * Convert commons-math <code>FieldElement</code> to JAS
382     * <code>RingElem</code> to.
383     * 
384     * @param <C> ring element type
385     * @param v commons-math FieldVector of CMFieldElement objects
386     * @return JAS vector of ring elements
387     */
388    public static <C extends RingElem<C>> GenVector<C> vectorFromCMFieldVector(GenVectorModul<C> fac,
389                    FieldVector<CMFieldElement<C>> v) {
390        if (v == null) {
391            return null;
392        }
393        List<C> list = listFromCMFieldVector(v);
394        GenVector<C> vv = new GenVector<C>(fac, list);
395        return vv;
396    }
397
398
399    /**
400     * Convert commons-math <code>FieldElement</code> to JAS
401     * <code>RingElem</code> to.
402     * @param <C> ring element type
403     * @param v commons-math FieldMatrix of CMFieldElement objects
404     * @return java.util.List of ring elements
405     */
406    public static <C extends RingElem<C>> List<List<C>> listFromCMFieldMatrix(FieldMatrix<CMFieldElement<C>> v) {
407        if (v == null) {
408            return null;
409        }
410        ArrayList<List<C>> vv = new ArrayList<List<C>>(v.getRowDimension());
411        for (int i = 0; i < v.getRowDimension(); i++) {
412            FieldVector<CMFieldElement<C>> e = v.getRowVector(i + 1);
413            List<C> l = listFromCMFieldVector(e);
414            vv.add(l);
415        }
416        return vv;
417    }
418
419
420    /**
421     * Convert commons-math <code>FieldMatrix</code> to JAS
422     * <code>RingElem</code> to.
423     * @param <C> ring element type
424     * @param v commons-math <code>FieldMatrix</code> of CMFieldElement objects
425     * @return JAS matrix of ring elements
426     */
427    public static <C extends RingElem<C>> GenMatrix<C> matrixFromCMFieldMatrix(GenMatrixRing<C> fac,
428                    FieldMatrix<CMFieldElement<C>> v) {
429        if (v == null) {
430            return null;
431        }
432        List<List<C>> list = listFromCMFieldMatrix(v);
433        GenMatrix<C> vv = new GenMatrix<C>(fac, list);
434        return vv;
435    }
436
437}