001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     * 
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     * 
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.math.linear;
018    
019    import java.io.Serializable;
020    import java.lang.reflect.Array;
021    
022    import junit.framework.Test;
023    import junit.framework.TestCase;
024    import junit.framework.TestSuite;
025    
026    import org.apache.commons.math.Field;
027    import org.apache.commons.math.FieldElement;
028    import org.apache.commons.math.TestUtils;
029    import org.apache.commons.math.fraction.Fraction;
030    import org.apache.commons.math.fraction.FractionField;
031    
032    /**
033     * Test cases for the {@link ArrayFieldVector} class.
034     *
035     * @version $Revision: 783702 $ $Date: 2009-06-11 04:54:02 -0400 (Thu, 11 Jun 2009) $
036     */
037    public class ArrayFieldVectorTest extends TestCase {
038    
039        // 
040        protected Fraction[][] ma1 = {
041                {new Fraction(1), new Fraction(2), new Fraction(3)},
042                {new Fraction(4), new Fraction(5), new Fraction(6)},
043                {new Fraction(7), new Fraction(8), new Fraction(9)}
044        };
045        protected Fraction[] vec1 = {new Fraction(1), new Fraction(2), new Fraction(3)};
046        protected Fraction[] vec2 = {new Fraction(4), new Fraction(5), new Fraction(6)};
047        protected Fraction[] vec3 = {new Fraction(7), new Fraction(8), new Fraction(9)};
048        protected Fraction[] vec4 = { new Fraction(1), new Fraction(2), new Fraction(3),
049                                      new Fraction(4), new Fraction(5), new Fraction(6),
050                                      new Fraction(7), new Fraction(8), new Fraction(9)};
051        protected Fraction[] vec_null = {new Fraction(0), new Fraction(0), new Fraction(0)};
052        protected Fraction[] dvec1 = {new Fraction(1), new Fraction(2), new Fraction(3),
053                                      new Fraction(4), new Fraction(5), new Fraction(6),
054                                      new Fraction(7), new Fraction(8), new Fraction(9)};
055        protected Fraction[][] mat1 = {
056                {new Fraction(1), new Fraction(2), new Fraction(3)},
057                {new Fraction(4), new Fraction(5), new Fraction(6)},
058                {new Fraction(7), new Fraction(8), new Fraction(9)}
059        };
060    
061        // Testclass to test the FieldVector<Fraction> interface 
062        // only with enough content to support the test
063        public static class FieldVectorTestImpl<T extends FieldElement<T>>
064            implements FieldVector<T>, Serializable {
065    
066            private static final long serialVersionUID = 3970959016014158539L;
067    
068            private final Field<T> field;
069    
070            /** Entries of the vector. */
071            protected T[] data;
072    
073            /** Build an array of elements.
074             * @param length size of the array to build
075             * @return a new array
076             */
077            @SuppressWarnings("unchecked")
078            private T[] buildArray(final int length) {
079                return (T[]) Array.newInstance(field.getZero().getClass(), length);
080            }
081    
082            public FieldVectorTestImpl(T[] d) {
083                field = d[0].getField();
084                data = d.clone();
085            }
086    
087            public Field<T> getField() {
088                return field;
089            }
090    
091            private UnsupportedOperationException unsupported() {
092                return new UnsupportedOperationException("Not supported, unneeded for test purposes");
093            }
094    
095            public FieldVector<T> copy() {
096                throw unsupported();
097            }
098    
099            public FieldVector<T> add(FieldVector<T> v) throws IllegalArgumentException {
100                throw unsupported();
101            }
102    
103            public FieldVector<T> add(T[] v) throws IllegalArgumentException {
104                throw unsupported();
105            }
106    
107            public FieldVector<T> subtract(FieldVector<T> v) throws IllegalArgumentException {
108                throw unsupported();
109            }
110    
111            public FieldVector<T> subtract(T[] v) throws IllegalArgumentException {
112                throw unsupported();
113            }
114    
115            public FieldVector<T> mapAdd(T d) {
116                throw unsupported();
117            }
118    
119            public FieldVector<T> mapAddToSelf(T d) {
120                throw unsupported();
121            }
122    
123            public FieldVector<T> mapSubtract(T d) {
124                throw unsupported();
125            }
126    
127            public FieldVector<T> mapSubtractToSelf(T d) {
128                throw unsupported();
129            }
130    
131            public FieldVector<T> mapMultiply(T d) {
132                T[] out = buildArray(data.length);
133                for (int i = 0; i < data.length; i++) {
134                    out[i] = data[i].multiply(d);
135                }
136                return new FieldVectorTestImpl<T>(out);
137            }
138    
139            public FieldVector<T> mapMultiplyToSelf(T d) {
140                throw unsupported();
141            }
142    
143            public FieldVector<T> mapDivide(T d) {
144                throw unsupported();
145            }
146    
147            public FieldVector<T> mapDivideToSelf(T d) {
148                throw unsupported();
149            }
150    
151            public FieldVector<T> mapInv() {
152                throw unsupported();
153            }
154    
155            public FieldVector<T> mapInvToSelf() {
156                throw unsupported();
157            }
158    
159            public FieldVector<T> ebeMultiply(FieldVector<T> v) throws IllegalArgumentException {
160                throw unsupported();
161            }
162    
163            public FieldVector<T> ebeMultiply(T[] v) throws IllegalArgumentException {
164                throw unsupported();
165            }
166    
167            public FieldVector<T> ebeDivide(FieldVector<T> v) throws IllegalArgumentException {
168                throw unsupported();
169            }
170    
171            public FieldVector<T> ebeDivide(T[] v) throws IllegalArgumentException {
172                throw unsupported();
173            }
174    
175            public T[] getData() {
176                return data.clone();
177            }
178    
179            public T dotProduct(FieldVector<T> v) throws IllegalArgumentException {
180                T dot = field.getZero();
181                for (int i = 0; i < data.length; i++) {
182                    dot = dot.add(data[i].multiply(v.getEntry(i)));
183                }
184                return dot;
185            }
186    
187            public T dotProduct(T[] v) throws IllegalArgumentException {
188                T dot = field.getZero();
189                for (int i = 0; i < data.length; i++) {
190                    dot = dot.add(data[i].multiply(v[i]));
191                }
192                return dot;
193            }
194    
195            public FieldVector<T> projection(FieldVector<T> v) throws IllegalArgumentException {
196                throw unsupported();
197            }
198    
199            public FieldVector<T> projection(T[] v) throws IllegalArgumentException {
200                throw unsupported();
201            }
202    
203            public FieldMatrix<T> outerProduct(FieldVector<T> v) throws IllegalArgumentException {
204                throw unsupported();
205            }
206    
207            public FieldMatrix<T> outerProduct(T[] v) throws IllegalArgumentException {
208                throw unsupported();
209            }
210    
211            public T getEntry(int index) throws MatrixIndexException {
212                return data[index];
213            }
214    
215            public int getDimension() {
216                return data.length;
217            }
218    
219            public FieldVector<T> append(FieldVector<T> v) {
220                throw unsupported();
221            }
222    
223            public FieldVector<T> append(T d) {
224                throw unsupported();
225            }
226    
227            public FieldVector<T> append(T[] a) {
228                throw unsupported();
229            }
230    
231            public FieldVector<T> getSubVector(int index, int n) throws MatrixIndexException {
232                throw unsupported();
233            }
234    
235            public void setEntry(int index, T value) throws MatrixIndexException {
236                throw unsupported();
237            }
238    
239            public void setSubVector(int index, FieldVector<T> v) throws MatrixIndexException {
240                throw unsupported();
241            }
242    
243            public void setSubVector(int index, T[] v) throws MatrixIndexException {
244                throw unsupported();
245            }
246    
247            public void set(T value) {
248                throw unsupported();
249            }
250    
251            public T[] toArray() {
252                throw unsupported();
253            }
254    
255        }
256    
257        public static Test suite() {
258            TestSuite suite = new TestSuite(ArrayFieldVectorTest.class);
259            suite.setName("ArrayFieldVector<Fraction> Tests");
260            return suite;
261        }
262    
263        public void testConstructors() {
264    
265            ArrayFieldVector<Fraction> v0 = new ArrayFieldVector<Fraction>(FractionField.getInstance());
266            assertEquals(0, v0.getDimension());
267    
268            ArrayFieldVector<Fraction> v1 = new ArrayFieldVector<Fraction>(FractionField.getInstance(), 7);
269            assertEquals(7, v1.getDimension());
270            assertEquals(new Fraction(0), v1.getEntry(6));
271    
272            ArrayFieldVector<Fraction> v2 = new ArrayFieldVector<Fraction>(5, new Fraction(123, 100));
273            assertEquals(5, v2.getDimension());
274            assertEquals(new Fraction(123, 100), v2.getEntry(4));
275    
276            ArrayFieldVector<Fraction> v3 = new ArrayFieldVector<Fraction>(vec1);
277            assertEquals(3, v3.getDimension());
278            assertEquals(new Fraction(2), v3.getEntry(1));
279    
280            ArrayFieldVector<Fraction> v4 = new ArrayFieldVector<Fraction>(vec4, 3, 2);
281            assertEquals(2, v4.getDimension());
282            assertEquals(new Fraction(4), v4.getEntry(0));
283            try {
284                new ArrayFieldVector<Fraction>(vec4, 8, 3);
285                fail("IllegalArgumentException expected");
286            } catch (IllegalArgumentException ex) {
287                // expected behavior
288            } catch (Exception e) {
289                fail("wrong exception caught");
290            }
291    
292            FieldVector<Fraction> v5_i = new ArrayFieldVector<Fraction>(dvec1);
293            assertEquals(9, v5_i.getDimension());
294            assertEquals(new Fraction(9), v5_i.getEntry(8));
295    
296            ArrayFieldVector<Fraction> v5 = new ArrayFieldVector<Fraction>(dvec1);
297            assertEquals(9, v5.getDimension());
298            assertEquals(new Fraction(9), v5.getEntry(8));
299    
300            ArrayFieldVector<Fraction> v6 = new ArrayFieldVector<Fraction>(dvec1, 3, 2);
301            assertEquals(2, v6.getDimension());
302            assertEquals(new Fraction(4), v6.getEntry(0));
303            try {
304                new ArrayFieldVector<Fraction>(dvec1, 8, 3);
305                fail("IllegalArgumentException expected");
306            } catch (IllegalArgumentException ex) {
307                // expected behavior
308            } catch (Exception e) {
309                fail("wrong exception caught");
310            }
311    
312            ArrayFieldVector<Fraction> v7 = new ArrayFieldVector<Fraction>(v1);
313            assertEquals(7, v7.getDimension());
314            assertEquals(new Fraction(0), v7.getEntry(6));
315    
316            FieldVectorTestImpl<Fraction> v7_i = new FieldVectorTestImpl<Fraction>(vec1);
317    
318            ArrayFieldVector<Fraction> v7_2 = new ArrayFieldVector<Fraction>(v7_i);
319            assertEquals(3, v7_2.getDimension());
320            assertEquals(new Fraction(2), v7_2.getEntry(1));
321    
322            ArrayFieldVector<Fraction> v8 = new ArrayFieldVector<Fraction>(v1, true);
323            assertEquals(7, v8.getDimension());
324            assertEquals(new Fraction(0), v8.getEntry(6));
325            assertNotSame("testData not same object ", v1.data, v8.data);
326    
327            ArrayFieldVector<Fraction> v8_2 = new ArrayFieldVector<Fraction>(v1, false);
328            assertEquals(7, v8_2.getDimension());
329            assertEquals(new Fraction(0), v8_2.getEntry(6));
330            assertEquals(v1.data, v8_2.data);
331    
332            ArrayFieldVector<Fraction> v9 = new ArrayFieldVector<Fraction>(v1, v3);
333            assertEquals(10, v9.getDimension());
334            assertEquals(new Fraction(1), v9.getEntry(7));
335    
336        }
337    
338        public void testDataInOut() {
339    
340            ArrayFieldVector<Fraction> v1 = new ArrayFieldVector<Fraction>(vec1);
341            ArrayFieldVector<Fraction> v2 = new ArrayFieldVector<Fraction>(vec2);
342            ArrayFieldVector<Fraction> v4 = new ArrayFieldVector<Fraction>(vec4);
343            FieldVectorTestImpl<Fraction> v2_t = new FieldVectorTestImpl<Fraction>(vec2); 
344    
345            FieldVector<Fraction> v_append_1 = v1.append(v2);
346            assertEquals(6, v_append_1.getDimension());
347            assertEquals(new Fraction(4), v_append_1.getEntry(3));
348    
349            FieldVector<Fraction> v_append_2 = v1.append(new Fraction(2));
350            assertEquals(4, v_append_2.getDimension());
351            assertEquals(new Fraction(2), v_append_2.getEntry(3));
352    
353            FieldVector<Fraction> v_append_3 = v1.append(vec2);
354            assertEquals(6, v_append_3.getDimension());
355            assertEquals(new Fraction(4), v_append_3.getEntry(3));
356    
357            FieldVector<Fraction> v_append_4 = v1.append(v2_t);
358            assertEquals(6, v_append_4.getDimension());
359            assertEquals(new Fraction(4), v_append_4.getEntry(3));
360    
361            FieldVector<Fraction> v_copy = v1.copy();
362            assertEquals(3, v_copy.getDimension());
363            assertNotSame("testData not same object ", v1.data, v_copy.getData());
364    
365            Fraction[] a_frac = v1.toArray();
366            assertEquals(3, a_frac.length);
367            assertNotSame("testData not same object ", v1.data, a_frac);
368    
369    
370    //      ArrayFieldVector<Fraction> vout4 = (ArrayFieldVector<Fraction>) v1.clone();
371    //      assertEquals(3, vout4.getDimension());
372    //      assertEquals(v1.data, vout4.data);
373    
374    
375            FieldVector<Fraction> vout5 = v4.getSubVector(3, 3);
376            assertEquals(3, vout5.getDimension());
377            assertEquals(new Fraction(5), vout5.getEntry(1));
378            try {
379                v4.getSubVector(3, 7);
380                fail("MatrixIndexException expected");
381            } catch (MatrixIndexException ex) {
382                // expected behavior
383            } catch (Exception e) {
384                fail("wrong exception caught");
385            }
386    
387            ArrayFieldVector<Fraction> v_set1 = (ArrayFieldVector<Fraction>) v1.copy();
388            v_set1.setEntry(1, new Fraction(11));
389            assertEquals(new Fraction(11), v_set1.getEntry(1));
390            try {
391                v_set1.setEntry(3, new Fraction(11));
392                fail("MatrixIndexException expected");
393            } catch (MatrixIndexException ex) {
394                // expected behavior
395            } catch (Exception e) {
396                fail("wrong exception caught");
397            }
398    
399            ArrayFieldVector<Fraction> v_set2 = (ArrayFieldVector<Fraction>) v4.copy();
400            v_set2.set(3, v1);
401            assertEquals(new Fraction(1), v_set2.getEntry(3));
402            assertEquals(new Fraction(7), v_set2.getEntry(6));
403            try {
404                v_set2.set(7, v1);
405                fail("MatrixIndexException expected");
406            } catch (MatrixIndexException ex) {
407                // expected behavior
408            } catch (Exception e) {
409                fail("wrong exception caught");
410            }
411    
412            ArrayFieldVector<Fraction> v_set3 = (ArrayFieldVector<Fraction>) v1.copy();
413            v_set3.set(new Fraction(13));
414            assertEquals(new Fraction(13), v_set3.getEntry(2));
415    
416            try {
417                v_set3.getEntry(23);
418                fail("ArrayIndexOutOfBoundsException expected");
419            } catch (ArrayIndexOutOfBoundsException ex) {
420                // expected behavior
421            } catch (Exception e) {
422                fail("wrong exception caught");
423            }
424    
425            ArrayFieldVector<Fraction> v_set4 = (ArrayFieldVector<Fraction>) v4.copy();
426            v_set4.setSubVector(3, v2_t);
427            assertEquals(new Fraction(4), v_set4.getEntry(3));
428            assertEquals(new Fraction(7), v_set4.getEntry(6));
429            try {
430                v_set4.setSubVector(7, v2_t);
431                fail("MatrixIndexException expected");
432            } catch (MatrixIndexException ex) {
433                // expected behavior
434            } catch (Exception e) {
435                fail("wrong exception caught");
436            }
437    
438    
439            ArrayFieldVector<Fraction> vout10 = (ArrayFieldVector<Fraction>) v1.copy();       
440            ArrayFieldVector<Fraction> vout10_2 = (ArrayFieldVector<Fraction>) v1.copy();
441            assertEquals(vout10, vout10_2);
442            vout10_2.setEntry(0, new Fraction(11, 10));
443            assertNotSame(vout10, vout10_2);
444    
445        }
446    
447        public void testMapFunctions() { 
448            ArrayFieldVector<Fraction> v1 = new ArrayFieldVector<Fraction>(vec1);
449    
450            //octave =  v1 .+ 2.0
451            FieldVector<Fraction> v_mapAdd = v1.mapAdd(new Fraction(2));
452            Fraction[] result_mapAdd = {new Fraction(3), new Fraction(4), new Fraction(5)};
453            checkArray("compare vectors" ,result_mapAdd,v_mapAdd.getData());
454    
455            //octave =  v1 .+ 2.0
456            FieldVector<Fraction> v_mapAddToSelf = v1.copy();
457            v_mapAddToSelf.mapAddToSelf(new Fraction(2));
458            Fraction[] result_mapAddToSelf = {new Fraction(3), new Fraction(4), new Fraction(5)};
459            checkArray("compare vectors" ,result_mapAddToSelf,v_mapAddToSelf.getData());
460    
461            //octave =  v1 .- 2.0
462            FieldVector<Fraction> v_mapSubtract = v1.mapSubtract(new Fraction(2));
463            Fraction[] result_mapSubtract = {new Fraction(-1), new Fraction(0), new Fraction(1)};
464            checkArray("compare vectors" ,result_mapSubtract,v_mapSubtract.getData());
465    
466            //octave =  v1 .- 2.0
467            FieldVector<Fraction> v_mapSubtractToSelf = v1.copy();
468            v_mapSubtractToSelf.mapSubtractToSelf(new Fraction(2));
469            Fraction[] result_mapSubtractToSelf = {new Fraction(-1), new Fraction(0), new Fraction(1)};
470            checkArray("compare vectors" ,result_mapSubtractToSelf,v_mapSubtractToSelf.getData());
471    
472            //octave =  v1 .* 2.0
473            FieldVector<Fraction> v_mapMultiply = v1.mapMultiply(new Fraction(2));
474            Fraction[] result_mapMultiply = {new Fraction(2), new Fraction(4), new Fraction(6)};
475            checkArray("compare vectors" ,result_mapMultiply,v_mapMultiply.getData());
476    
477            //octave =  v1 .* 2.0
478            FieldVector<Fraction> v_mapMultiplyToSelf = v1.copy();
479            v_mapMultiplyToSelf.mapMultiplyToSelf(new Fraction(2));
480            Fraction[] result_mapMultiplyToSelf = {new Fraction(2), new Fraction(4), new Fraction(6)};
481            checkArray("compare vectors" ,result_mapMultiplyToSelf,v_mapMultiplyToSelf.getData());
482    
483            //octave =  v1 ./ 2.0
484            FieldVector<Fraction> v_mapDivide = v1.mapDivide(new Fraction(2));
485            Fraction[] result_mapDivide = {new Fraction(1, 2), new Fraction(1), new Fraction(3, 2)};
486            checkArray("compare vectors" ,result_mapDivide,v_mapDivide.getData());
487    
488            //octave =  v1 ./ 2.0
489            FieldVector<Fraction> v_mapDivideToSelf = v1.copy();
490            v_mapDivideToSelf.mapDivideToSelf(new Fraction(2));
491            Fraction[] result_mapDivideToSelf = {new Fraction(1, 2), new Fraction(1), new Fraction(3, 2)};
492            checkArray("compare vectors" ,result_mapDivideToSelf,v_mapDivideToSelf.getData());
493    
494            //octave =  v1 .^-1
495            FieldVector<Fraction> v_mapInv = v1.mapInv();
496            Fraction[] result_mapInv = {new Fraction(1),new Fraction(1, 2),new Fraction(1, 3)};
497            checkArray("compare vectors" ,result_mapInv,v_mapInv.getData());
498    
499            //octave =  v1 .^-1
500            FieldVector<Fraction> v_mapInvToSelf = v1.copy();
501            v_mapInvToSelf.mapInvToSelf();
502            Fraction[] result_mapInvToSelf = {new Fraction(1),new Fraction(1, 2),new Fraction(1, 3)};
503            checkArray("compare vectors" ,result_mapInvToSelf,v_mapInvToSelf.getData());
504    
505        }
506    
507        public void testBasicFunctions() { 
508            ArrayFieldVector<Fraction> v1 = new ArrayFieldVector<Fraction>(vec1);
509            ArrayFieldVector<Fraction> v2 = new ArrayFieldVector<Fraction>(vec2);
510            new ArrayFieldVector<Fraction>(vec_null);
511    
512            FieldVectorTestImpl<Fraction> v2_t = new FieldVectorTestImpl<Fraction>(vec2); 
513    
514            //octave =  v1 + v2
515            ArrayFieldVector<Fraction> v_add = v1.add(v2);
516            Fraction[] result_add = {new Fraction(5), new Fraction(7), new Fraction(9)};
517            checkArray("compare vect" ,v_add.getData(),result_add);
518    
519            FieldVectorTestImpl<Fraction> vt2 = new FieldVectorTestImpl<Fraction>(vec2);
520            FieldVector<Fraction> v_add_i = v1.add(vt2);
521            Fraction[] result_add_i = {new Fraction(5), new Fraction(7), new Fraction(9)};
522            checkArray("compare vect" ,v_add_i.getData(),result_add_i);
523    
524            //octave =  v1 - v2
525            ArrayFieldVector<Fraction> v_subtract = v1.subtract(v2);
526            Fraction[] result_subtract = {new Fraction(-3), new Fraction(-3), new Fraction(-3)};
527            checkArray("compare vect" ,v_subtract.getData(),result_subtract);
528    
529            FieldVector<Fraction> v_subtract_i = v1.subtract(vt2);
530            Fraction[] result_subtract_i = {new Fraction(-3), new Fraction(-3), new Fraction(-3)};
531            checkArray("compare vect" ,v_subtract_i.getData(),result_subtract_i);
532    
533            // octave v1 .* v2
534            ArrayFieldVector<Fraction>  v_ebeMultiply = v1.ebeMultiply(v2);
535            Fraction[] result_ebeMultiply = {new Fraction(4), new Fraction(10), new Fraction(18)};
536            checkArray("compare vect" ,v_ebeMultiply.getData(),result_ebeMultiply);
537    
538            FieldVector<Fraction>  v_ebeMultiply_2 = v1.ebeMultiply(v2_t);
539            Fraction[] result_ebeMultiply_2 = {new Fraction(4), new Fraction(10), new Fraction(18)};
540            checkArray("compare vect" ,v_ebeMultiply_2.getData(),result_ebeMultiply_2);
541    
542            // octave v1 ./ v2
543            ArrayFieldVector<Fraction>  v_ebeDivide = v1.ebeDivide(v2);
544            Fraction[] result_ebeDivide = {new Fraction(1, 4), new Fraction(2, 5), new Fraction(1, 2)};
545            checkArray("compare vect" ,v_ebeDivide.getData(),result_ebeDivide);
546    
547            FieldVector<Fraction>  v_ebeDivide_2 = v1.ebeDivide(v2_t);
548            Fraction[] result_ebeDivide_2 = {new Fraction(1, 4), new Fraction(2, 5), new Fraction(1, 2)};
549            checkArray("compare vect" ,v_ebeDivide_2.getData(),result_ebeDivide_2);
550    
551            // octave  dot(v1,v2)
552            Fraction dot =  v1.dotProduct(v2);
553            assertEquals("compare val ",new Fraction(32), dot);
554    
555            // octave  dot(v1,v2_t)
556            Fraction dot_2 =  v1.dotProduct(v2_t);
557            assertEquals("compare val ",new Fraction(32), dot_2);
558    
559            FieldMatrix<Fraction> m_outerProduct = v1.outerProduct(v2);
560            assertEquals("compare val ",new Fraction(4), m_outerProduct.getEntry(0,0));
561    
562            FieldMatrix<Fraction> m_outerProduct_2 = v1.outerProduct(v2_t);
563            assertEquals("compare val ",new Fraction(4), m_outerProduct_2.getEntry(0,0));
564    
565            ArrayFieldVector<Fraction> v_projection = v1.projection(v2);
566            Fraction[] result_projection = {new Fraction(128, 77), new Fraction(160, 77), new Fraction(192, 77)};
567            checkArray("compare vect", v_projection.getData(), result_projection);
568    
569            FieldVector<Fraction> v_projection_2 = v1.projection(v2_t);
570            Fraction[] result_projection_2 = {new Fraction(128, 77), new Fraction(160, 77), new Fraction(192, 77)};
571            checkArray("compare vect", v_projection_2.getData(), result_projection_2);
572    
573        }  
574    
575        public void testMisc() { 
576            ArrayFieldVector<Fraction> v1 = new ArrayFieldVector<Fraction>(vec1);
577            ArrayFieldVector<Fraction> v4 = new ArrayFieldVector<Fraction>(vec4);
578            FieldVector<Fraction> v4_2 = new ArrayFieldVector<Fraction>(vec4);
579    
580            String out1 = v1.toString();
581            assertTrue("some output ",  out1.length()!=0);
582            /*    
583             Fraction[] dout1 = v1.copyOut();
584            assertEquals(3, dout1.length);
585            assertNotSame("testData not same object ", v1.data, dout1);   
586             */      
587            try {
588                v1.checkVectorDimensions(2); 
589                fail("IllegalArgumentException expected");
590            } catch (IllegalArgumentException ex) {
591                // expected behavior
592            } catch (Exception e) {
593                fail("wrong exception caught");
594            } 
595    
596           try {
597                v1.checkVectorDimensions(v4); 
598                fail("IllegalArgumentException expected");
599            } catch (IllegalArgumentException ex) {
600                // expected behavior
601            } catch (Exception e) {
602                fail("wrong exception caught");
603            }        
604    
605            try {
606                v1.checkVectorDimensions(v4_2); 
607                fail("IllegalArgumentException expected");
608            } catch (IllegalArgumentException ex) {
609                // expected behavior
610            } catch (Exception e) {
611                fail("wrong exception caught");
612            }        
613    
614        }
615    
616        public void testSerial()  {
617            ArrayFieldVector<Fraction> v = new ArrayFieldVector<Fraction>(vec1);
618            assertEquals(v,TestUtils.serializeAndRecover(v));
619        }
620      
621        /** verifies that two vectors are equals */
622        protected void checkArray(String msg, Fraction[] m, Fraction[] n) {
623            if (m.length != n.length) {
624                fail("vectors have different lengths");
625            }
626            for (int i = 0; i < m.length; i++) {
627                assertEquals(msg + " " +  i + " elements differ", m[i],n[i]);
628            }
629        }
630    
631    }