001/*
002 * $Id: JLAdapterUtil.java 5244 2015-05-01 13:29:44Z kredel $
003 */
004
005package edu.jas.jlinalg;
006
007
008// import static edu.jas.jlinalg.JLAdapterUtil.toArray;
009
010import java.util.ArrayList;
011import java.util.List;
012
013import org.jlinalg.Matrix;
014import org.jlinalg.Vector;
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 utilities.
025 * @author Heinz Kredel
026 */
027
028public class JLAdapterUtil {
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(JLAdapterUtil.<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 toArrayFromList(a.matrix);
082    }
083
084
085    @SuppressWarnings("unchecked")
086    public static <C extends RingElem<C>> C[][] toArrayFromList(List<ArrayList<C>> a) { // Array only once
087        if (a == null) {
088            return null;
089        }
090        C[][] av = (C[][]) new RingElem[a.size()][];
091        int i = 0;
092        for (List<C> e : a) {
093            av[i++] = toArray(e);
094        }
095        return av;
096    }
097
098
099    /**
100     * Convert JAS <code>RingElem</code> to JLinAlg <code>IRingElement</code>.
101     * @param <C> ring element type
102     * @param v array of ring elements
103     * @return array of JLAdapter objects
104     */
105    @SuppressWarnings({ "unchecked", "cast" })
106    public static <C extends RingElem<C>> JLAdapter<C>[] toJLAdapter(C[] v) {
107        if (v == null) {
108            return null;
109        }
110        JLAdapter<C>[] va = (JLAdapter<C>[]) new JLAdapter[v.length];
111        for (int i = 0; i < v.length; i++) {
112            va[i] = new JLAdapter<C>(v[i]);
113        }
114        return va;
115    }
116
117
118    /**
119     * Convert JAS <code>RingElem</code> to JLinAlg <code>IRingElement</code>.
120     * @param <C> ring element type
121     * @param v array of ring elements
122     * @return array of JLAdapter objects
123     */
124    @SuppressWarnings({ "unchecked", "cast" })
125    public static <C extends RingElem<C>> JLAdapter<C>[] toJLAdapterRE(RingElem<C>[] v) {
126        if (v == null) {
127            return null;
128        }
129        JLAdapter<C>[] va = (JLAdapter<C>[]) new JLAdapter[v.length];
130        for (int i = 0; i < v.length; i++) {
131            va[i] = new JLAdapter<C>((C) v[i]);
132        }
133        return va;
134    }
135
136
137    /**
138     * Convert JAS <code>RingElem</code> to JLinAlg <code>IRingElement</code>.
139     * @param <C> ring element type
140     * @param v JAS vector of ring elements
141     * @return array of JLAdapter objects
142     */
143    public static <C extends RingElem<C>> JLAdapter<C>[] toJLAdapter(GenVector<C> v) {
144        if (v == null) {
145            return null;
146        }
147        JLAdapter<C>[] va = JLAdapterUtil.<C> toJLAdapter(v.val);
148        return va;
149    }
150
151
152    /**
153     * Convert JAS <code>RingElem</code> to JLinAlg <code>IRingElement</code>.
154     * @param <C> ring element type
155     * @param v list of ring elements
156     * @return array of JLAdapter objects
157     */
158    @SuppressWarnings({ "unchecked", "cast" })
159    public static <C extends RingElem<C>> JLAdapter<C>[] toJLAdapter(List<C> v) {
160        if (v == null) {
161            return null;
162        }
163        JLAdapter<C>[] va = (JLAdapter<C>[]) new JLAdapter[v.size()];
164        for (int i = 0; i < v.size(); i++) {
165            va[i] = new JLAdapter<C>(v.get(i));
166        }
167        return va;
168    }
169
170
171    /**
172     * Convert JAS <code>RingElem</code> to JLinAlg <code>IRingElement</code>.
173     * @param <C> ring element type
174     * @param v JAS vector of ring elements
175     * @return JLinAlg vector of JLAdapter objects
176     */
177    public static <C extends RingElem<C>> Vector<JLAdapter<C>> toJLAdapterVector(GenVector<C> v) {
178        if (v == null) {
179            return null;
180        }
181        Vector<JLAdapter<C>> va = new Vector<JLAdapter<C>>(JLAdapterUtil.<C> toJLAdapter(v.val));
182        return va;
183    }
184
185
186    /**
187     * Convert JAS <code>RingElem</code> to JLinAlg <code>IRingElement</code>.
188     * @param <C> ring element type
189     * @param v matrix of ring elements
190     * @return matrix of JLAdapter objects
191     */
192    @SuppressWarnings({ "unchecked", "cast" })
193    public static <C extends RingElem<C>> JLAdapter<C>[][] toJLAdapter(C[][] v) {
194        if (v == null) {
195            return null;
196        }
197        JLAdapter<C>[][] va = (JLAdapter<C>[][]) new JLAdapter[v.length][];
198        for (int i = 0; i < v.length; i++) {
199            va[i] = JLAdapterUtil.<C> toJLAdapter(v[i]);
200        }
201        return va;
202    }
203
204
205    /**
206     * Convert JAS <code>RingElem</code> to JLinAlg <code>IRingElement</code>.
207     * @param <C> ring element type
208     * @param v matrix of ring elements
209     * @return matrix of JLAdapter objects
210     */
211    @SuppressWarnings({ "unchecked", "cast" })
212    public static <C extends RingElem<C>> JLAdapter<C>[][] toJLAdapterRE(RingElem<C>[][] v) {
213        if (v == null) {
214            return null;
215        }
216        JLAdapter<C>[][] va = (JLAdapter<C>[][]) new JLAdapter[v.length][];
217        for (int i = 0; i < v.length; i++) {
218            va[i] = JLAdapterUtil.<C> toJLAdapterRE(v[i]);
219        }
220        return va;
221    }
222
223
224    /**
225     * Convert JAS <code>RingElem</code> to JLinAlg <code>IRingElement</code>.
226     * @param <C> ring element type
227     * @param v JAS matrix of ring elements
228     * @return array of JLAdapter objects
229     */
230    public static <C extends RingElem<C>> JLAdapter<C>[][] toJLAdapter(GenMatrix<C> v) {
231        if (v == null) {
232            return null;
233        }
234        JLAdapter<C>[][] va = JLAdapterUtil.<C> toJLAdapterFromList(v.matrix);
235        return va;
236    }
237
238
239    /**
240     * Convert JAS <code>RingElem</code> to JLinAlg <code>IRingElement</code>.
241     * @param <C> ring element type
242     * @param v list of lists of ring elements
243     * @return array of JLAdapter objects
244     */
245    @SuppressWarnings({ "unchecked", "cast" })
246    public static <C extends RingElem<C>> JLAdapter<C>[][] toJLAdapterFromList(List<ArrayList<C>> v) {
247        if (v == null) {
248            return null;
249        }
250        JLAdapter<C>[][] va = (JLAdapter<C>[][]) new JLAdapter[v.size()][];
251        for (int i = 0; i < v.size(); i++) {
252            va[i] = JLAdapterUtil.<C> toJLAdapter(v.get(i));
253        }
254        return va;
255    }
256
257
258    /**
259     * Convert JAS <code>RingElem</code> to JLinAlg <code>IRingElement</code>.
260     * @param <C> ring element type
261     * @param v JAS vector of ring elements
262     * @return JLinAlg vector of JLAdapter objects
263     */
264    public static <C extends RingElem<C>> Matrix<JLAdapter<C>> toJLAdapterMatrix(GenMatrix<C> v) {
265        if (v == null) {
266            return null;
267        }
268        Matrix<JLAdapter<C>> va = new Matrix<JLAdapter<C>>(JLAdapterUtil.<C> toJLAdapterFromList(v.matrix));
269        return va;
270    }
271
272
273    /**
274     * Convert JLinAlg <code>IRingElement</code> to JAS <code>RingElem</code>
275     * to.
276     * @param <C> ring element type
277     * @param v array of JLAdapter objects
278     * @return array of ring elements
279     */
280    @SuppressWarnings("unchecked")
281    public static <C extends RingElem<C>> C[] fromJLAdapter(JLAdapter<C>[] v) {
282        if (v == null) {
283            return null;
284        }
285        C[] va = (C[]) new RingElem[v.length];
286        for (int i = 0; i < v.length; i++) {
287            if (v[i] != null) {
288                va[i] = v[i].val;
289            } else {
290                va[i] = null;
291            }
292        }
293        return va;
294    }
295
296
297    /**
298     * Convert JLinAlg <code>IRingElement</code> to JAS <code>RingElem</code>
299     * to.
300     * @param <C> ring element type
301     * @param v matrix of JLAdapter objects
302     * @return matrix of ring elements
303     */
304    @SuppressWarnings("unchecked")
305    public static <C extends RingElem<C>> C[][] fromJLAdapter(JLAdapter<C>[][] v) {
306        if (v == null) {
307            return null;
308        }
309        C[][] va = (C[][]) new RingElem[v.length][];
310        for (int i = 0; i < v.length; i++) {
311            va[i] = JLAdapterUtil.<C> fromJLAdapter(v[i]);
312        }
313        return va;
314    }
315
316
317    /**
318     * Convert JLinAlg <code>IRingElement</code> to JAS <code>RingElem</code>
319     * to.
320     * @param <C> ring element type
321     * @param v JLinAlg vector of JLAdapter objects
322     * @return array of ring elements
323     */
324    @SuppressWarnings("unchecked")
325    public static <C extends RingElem<C>> C[] fromJLAdapter(Vector<JLAdapter<C>> v) {
326        if (v == null) {
327            return null;
328        }
329        C[] va = (C[]) new RingElem[v.length()];
330        for (int i = 0; i < va.length; i++) {
331            JLAdapter<C> e = v.getEntry(i + 1);
332            if (e != null) {
333                va[i] = e.val;
334            } else {
335                va[i] = null;
336            }
337        }
338        return va;
339    }
340
341
342    /**
343     * Convert JLinAlg <code>IRingElement</code> to JAS <code>RingElem</code>
344     * to.
345     * @param <C> ring element type
346     * @param v JLinAlg vector of JLAdapter objects
347     * @return Java list of ring elements
348     */
349    public static <C extends RingElem<C>> ArrayList<C> listFromJLAdapter(Vector<JLAdapter<C>> v) {
350        if (v == null) {
351            return null;
352        }
353        ArrayList<C> vv = new ArrayList<C>(v.length());
354        for (int i = 0; i < v.length(); i++) {
355            JLAdapter<C> e = v.getEntry(i + 1);
356            if (e != null) {
357                vv.add(e.val);
358            } else {
359                vv.add(null);
360            }
361        }
362        return vv;
363    }
364
365
366    /**
367     * Convert JLinAlg <code>IRingElement</code> to JAS <code>RingElem</code>
368     * to.
369     * @param <C> ring element type
370     * @param v JLinAlg vector of JLAdapter objects
371     * @return JAS vector of ring elements
372     */
373    public static <C extends RingElem<C>> GenVector<C> vectorFromJLAdapter(GenVectorModul<C> fac,
374                    Vector<JLAdapter<C>> v) {
375        if (v == null) {
376            return null;
377        }
378        List<C> list = listFromJLAdapter(v);
379        GenVector<C> vv = new GenVector<C>(fac, list);
380        return vv;
381    }
382
383
384    /**
385     * Convert JLinAlg <code>IRingElement</code> to JAS <code>RingElem</code>
386     * to.
387     * @param <C> ring element type
388     * @param v JLinAlg vector of JLAdapter objects
389     * @return Java list of ring elements
390     */
391    public static <C extends RingElem<C>> List<List<C>> listFromJLAdapter(Matrix<JLAdapter<C>> v) {
392        if (v == null) {
393            return null;
394        }
395        ArrayList<List<C>> vv = new ArrayList<List<C>>(v.getRows());
396        for (int i = 0; i < v.getRows(); i++) {
397            Vector<JLAdapter<C>> e = v.getRow(i + 1);
398            List<C> l = listFromJLAdapter(e);
399            vv.add(l);
400        }
401        return vv;
402    }
403
404
405    /**
406     * Convert JLinAlg <code>IRingElement</code> to JAS <code>RingElem</code>
407     * to.
408     * @param <C> ring element type
409     * @param v JLinAlg vector of JLAdapter objects
410     * @return JAS matrix of ring elements
411     */
412    public static <C extends RingElem<C>> GenMatrix<C> matrixFromJLAdapter(GenMatrixRing<C> fac,
413                    Matrix<JLAdapter<C>> v) {
414        if (v == null) {
415            return null;
416        }
417        List<List<C>> list = listFromJLAdapter(v);
418        GenMatrix<C> vv = new GenMatrix<C>(fac, list);
419        return vv;
420    }
421
422}