View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.math4.legacy.linear;
18  
19  import java.io.Serializable;
20  import java.lang.reflect.Array;
21  import java.util.Arrays;
22  
23  import org.apache.commons.math4.legacy.core.Field;
24  import org.apache.commons.math4.legacy.core.FieldElement;
25  import org.apache.commons.math4.legacy.TestUtils;
26  import org.apache.commons.math4.legacy.exception.MathIllegalArgumentException;
27  import org.apache.commons.math4.legacy.exception.NumberIsTooSmallException;
28  import org.apache.commons.math4.legacy.exception.OutOfRangeException;
29  import org.apache.commons.math4.legacy.core.dfp.Dfp;
30  import org.junit.Assert;
31  import org.junit.Test;
32  
33  /**
34   * Test cases for the {@link ArrayFieldVector} class.
35   *
36   */
37  public class ArrayFieldVectorTest {
38  
39      //
40      protected Dfp[][] ma1 = {
41              {Dfp25.of(1), Dfp25.of(2), Dfp25.of(3)},
42              {Dfp25.of(4), Dfp25.of(5), Dfp25.of(6)},
43              {Dfp25.of(7), Dfp25.of(8), Dfp25.of(9)}
44      };
45      protected Dfp[] vec1 = {Dfp25.of(1), Dfp25.of(2), Dfp25.of(3)};
46      protected Dfp[] vec2 = {Dfp25.of(4), Dfp25.of(5), Dfp25.of(6)};
47      protected Dfp[] vec3 = {Dfp25.of(7), Dfp25.of(8), Dfp25.of(9)};
48      protected Dfp[] vec4 = { Dfp25.of(1), Dfp25.of(2), Dfp25.of(3),
49                                    Dfp25.of(4), Dfp25.of(5), Dfp25.of(6),
50                                    Dfp25.of(7), Dfp25.of(8), Dfp25.of(9)};
51      protected Dfp[] vec_null = {Dfp25.ZERO, Dfp25.ZERO, Dfp25.ZERO};
52      protected Dfp[] dvec1 = {Dfp25.of(1), Dfp25.of(2), Dfp25.of(3),
53                                    Dfp25.of(4), Dfp25.of(5), Dfp25.of(6),
54                                    Dfp25.of(7), Dfp25.of(8), Dfp25.of(9)};
55      protected Dfp[][] mat1 = {
56              {Dfp25.of(1), Dfp25.of(2), Dfp25.of(3)},
57              {Dfp25.of(4), Dfp25.of(5), Dfp25.of(6)},
58              {Dfp25.of(7), Dfp25.of(8), Dfp25.of(9)}
59      };
60  
61      // Testclass to test the FieldVector<Dfp> interface
62      // only with enough content to support the test
63      public static class FieldVectorTestImpl<T extends FieldElement<T>>
64          implements FieldVector<T>, Serializable {
65  
66          private static final long serialVersionUID = 3970959016014158539L;
67  
68          private final Field<T> field;
69  
70          /** Entries of the vector. */
71          protected T[] data;
72  
73          /** Build an array of elements.
74           * @param length size of the array to build
75           * @return a new array
76           */
77          @SuppressWarnings("unchecked") // field is of type T
78          private T[] buildArray(final int length) {
79              return (T[]) Array.newInstance(field.getRuntimeClass(), length);
80          }
81  
82          public FieldVectorTestImpl(T[] d) {
83              field = d[0].getField();
84              data = d.clone();
85          }
86  
87          @Override
88          public Field<T> getField() {
89              return field;
90          }
91  
92          private UnsupportedOperationException unsupported() {
93              return new UnsupportedOperationException("Not supported, unneeded for test purposes");
94          }
95  
96          @Override
97          public FieldVector<T> copy() {
98              throw unsupported();
99          }
100 
101         @Override
102         public FieldVector<T> add(FieldVector<T> v) {
103             throw unsupported();
104         }
105 
106         public FieldVector<T> add(T[] v) {
107             throw unsupported();
108         }
109 
110         @Override
111         public FieldVector<T> subtract(FieldVector<T> v) {
112             throw unsupported();
113         }
114 
115         public FieldVector<T> subtract(T[] v) {
116             throw unsupported();
117         }
118 
119         @Override
120         public FieldVector<T> mapAdd(T d) {
121             throw unsupported();
122         }
123 
124         @Override
125         public FieldVector<T> mapAddToSelf(T d) {
126             throw unsupported();
127         }
128 
129         @Override
130         public FieldVector<T> mapSubtract(T d) {
131             throw unsupported();
132         }
133 
134         @Override
135         public FieldVector<T> mapSubtractToSelf(T d) {
136             throw unsupported();
137         }
138 
139         @Override
140         public FieldVector<T> mapMultiply(T d) {
141             T[] out = buildArray(data.length);
142             for (int i = 0; i < data.length; i++) {
143                 out[i] = data[i].multiply(d);
144             }
145             return new FieldVectorTestImpl<>(out);
146         }
147 
148         @Override
149         public FieldVector<T> mapMultiplyToSelf(T d) {
150             throw unsupported();
151         }
152 
153         @Override
154         public FieldVector<T> mapDivide(T d) {
155             throw unsupported();
156         }
157 
158         @Override
159         public FieldVector<T> mapDivideToSelf(T d) {
160             throw unsupported();
161         }
162 
163         @Override
164         public FieldVector<T> mapInv() {
165             throw unsupported();
166         }
167 
168         @Override
169         public FieldVector<T> mapInvToSelf() {
170             throw unsupported();
171         }
172 
173         @Override
174         public FieldVector<T> ebeMultiply(FieldVector<T> v) {
175             throw unsupported();
176         }
177 
178         public FieldVector<T> ebeMultiply(T[] v) {
179             throw unsupported();
180         }
181 
182         @Override
183         public FieldVector<T> ebeDivide(FieldVector<T> v) {
184             throw unsupported();
185         }
186 
187         public FieldVector<T> ebeDivide(T[] v) {
188             throw unsupported();
189         }
190 
191         public T[] getData() {
192             return data.clone();
193         }
194 
195         @Override
196         public T dotProduct(FieldVector<T> v) {
197             T dot = field.getZero();
198             for (int i = 0; i < data.length; i++) {
199                 dot = dot.add(data[i].multiply(v.getEntry(i)));
200             }
201             return dot;
202         }
203 
204         public T dotProduct(T[] v) {
205             T dot = field.getZero();
206             for (int i = 0; i < data.length; i++) {
207                 dot = dot.add(data[i].multiply(v[i]));
208             }
209             return dot;
210         }
211 
212         @Override
213         public FieldVector<T> projection(FieldVector<T> v) {
214             throw unsupported();
215         }
216 
217         public FieldVector<T> projection(T[] v) {
218             throw unsupported();
219         }
220 
221         @Override
222         public FieldMatrix<T> outerProduct(FieldVector<T> v) {
223             throw unsupported();
224         }
225 
226         public FieldMatrix<T> outerProduct(T[] v) {
227             throw unsupported();
228         }
229 
230         @Override
231         public T getEntry(int index) {
232             return data[index];
233         }
234 
235         @Override
236         public int getDimension() {
237             return data.length;
238         }
239 
240         @Override
241         public FieldVector<T> append(FieldVector<T> v) {
242             throw unsupported();
243         }
244 
245         @Override
246         public FieldVector<T> append(T d) {
247             throw unsupported();
248         }
249 
250         public FieldVector<T> append(T[] a) {
251             throw unsupported();
252         }
253 
254         @Override
255         public FieldVector<T> getSubVector(int index, int n) {
256             throw unsupported();
257         }
258 
259         @Override
260         public void setEntry(int index, T value) {
261             throw unsupported();
262         }
263 
264         @Override
265         public void setSubVector(int index, FieldVector<T> v) {
266             throw unsupported();
267         }
268 
269         public void setSubVector(int index, T[] v) {
270             throw unsupported();
271         }
272 
273         @Override
274         public void set(T value) {
275             throw unsupported();
276         }
277 
278         @Override
279         public T[] toArray() {
280             return data.clone();
281         }
282     }
283 
284     @Test
285     public void testConstructors() {
286 
287         ArrayFieldVector<Dfp> v0 = new ArrayFieldVector<>(Dfp25.getField());
288         Assert.assertEquals(0, v0.getDimension());
289 
290         ArrayFieldVector<Dfp> v1 = new ArrayFieldVector<>(Dfp25.getField(), 7);
291         Assert.assertEquals(7, v1.getDimension());
292         Assert.assertEquals(Dfp25.ZERO, v1.getEntry(6));
293 
294         ArrayFieldVector<Dfp> v2 = new ArrayFieldVector<>(5, Dfp25.of(123, 100));
295         Assert.assertEquals(5, v2.getDimension());
296         Assert.assertEquals(Dfp25.of(123, 100), v2.getEntry(4));
297 
298         ArrayFieldVector<Dfp> v3 = new ArrayFieldVector<>(Dfp25.getField(), vec1);
299         Assert.assertEquals(3, v3.getDimension());
300         Assert.assertEquals(Dfp25.of(2), v3.getEntry(1));
301 
302         ArrayFieldVector<Dfp> v4 = new ArrayFieldVector<>(Dfp25.getField(), vec4, 3, 2);
303         Assert.assertEquals(2, v4.getDimension());
304         Assert.assertEquals(Dfp25.of(4), v4.getEntry(0));
305         try {
306             new ArrayFieldVector<>(vec4, 8, 3);
307             Assert.fail("MathIllegalArgumentException expected");
308         } catch (MathIllegalArgumentException ex) {
309             // expected behavior
310         }
311 
312         FieldVector<Dfp> v5_i = new ArrayFieldVector<>(dvec1);
313         Assert.assertEquals(9, v5_i.getDimension());
314         Assert.assertEquals(Dfp25.of(9), v5_i.getEntry(8));
315 
316         ArrayFieldVector<Dfp> v5 = new ArrayFieldVector<>(dvec1);
317         Assert.assertEquals(9, v5.getDimension());
318         Assert.assertEquals(Dfp25.of(9), v5.getEntry(8));
319 
320         ArrayFieldVector<Dfp> v6 = new ArrayFieldVector<>(dvec1, 3, 2);
321         Assert.assertEquals(2, v6.getDimension());
322         Assert.assertEquals(Dfp25.of(4), v6.getEntry(0));
323         try {
324             new ArrayFieldVector<>(dvec1, 8, 3);
325             Assert.fail("MathIllegalArgumentException expected");
326         } catch (MathIllegalArgumentException ex) {
327             // expected behavior
328         }
329 
330         ArrayFieldVector<Dfp> v7 = new ArrayFieldVector<>(v1);
331         Assert.assertEquals(7, v7.getDimension());
332         Assert.assertEquals(Dfp25.ZERO, v7.getEntry(6));
333 
334         FieldVectorTestImpl<Dfp> v7_i = new FieldVectorTestImpl<>(vec1);
335 
336         ArrayFieldVector<Dfp> v7_2 = new ArrayFieldVector<>(v7_i);
337         Assert.assertEquals(3, v7_2.getDimension());
338         Assert.assertEquals(Dfp25.of(2), v7_2.getEntry(1));
339 
340         ArrayFieldVector<Dfp> v8 = new ArrayFieldVector<>(v1, true);
341         Assert.assertEquals(7, v8.getDimension());
342         Assert.assertEquals(Dfp25.ZERO, v8.getEntry(6));
343         Assert.assertNotSame("testData not same object ", v1.getDataRef(), v8.getDataRef());
344 
345         ArrayFieldVector<Dfp> v8_2 = new ArrayFieldVector<>(v1, false);
346         Assert.assertEquals(7, v8_2.getDimension());
347         Assert.assertEquals(Dfp25.ZERO, v8_2.getEntry(6));
348         Assert.assertArrayEquals(v1.getDataRef(), v8_2.getDataRef());
349 
350         ArrayFieldVector<Dfp> v9 = new ArrayFieldVector<>((FieldVector<Dfp>) v1, (FieldVector<Dfp>) v3);
351         Assert.assertEquals(10, v9.getDimension());
352         Assert.assertEquals(Dfp25.of(1), v9.getEntry(7));
353     }
354 
355     @Test
356     public void testDataInOut() {
357 
358         ArrayFieldVector<Dfp> v1 = new ArrayFieldVector<>(vec1);
359         ArrayFieldVector<Dfp> v2 = new ArrayFieldVector<>(vec2);
360         ArrayFieldVector<Dfp> v4 = new ArrayFieldVector<>(vec4);
361         FieldVectorTestImpl<Dfp> v2_t = new FieldVectorTestImpl<>(vec2);
362 
363         FieldVector<Dfp> v_append_1 = v1.append(v2);
364         Assert.assertEquals(6, v_append_1.getDimension());
365         Assert.assertEquals(Dfp25.of(4), v_append_1.getEntry(3));
366 
367         FieldVector<Dfp> v_append_2 = v1.append(Dfp25.of(2));
368         Assert.assertEquals(4, v_append_2.getDimension());
369         Assert.assertEquals(Dfp25.of(2), v_append_2.getEntry(3));
370 
371         FieldVector<Dfp> v_append_4 = v1.append(v2_t);
372         Assert.assertEquals(6, v_append_4.getDimension());
373         Assert.assertEquals(Dfp25.of(4), v_append_4.getEntry(3));
374 
375         FieldVector<Dfp> v_copy = v1.copy();
376         Assert.assertEquals(3, v_copy.getDimension());
377         Assert.assertNotSame("testData not same object ", v1.getDataRef(), v_copy.toArray());
378 
379         Dfp[] a_frac = v1.toArray();
380         Assert.assertEquals(3, a_frac.length);
381         Assert.assertNotSame("testData not same object ", v1.getDataRef(), a_frac);
382 
383 
384 //      ArrayFieldVector<Dfp> vout4 = (ArrayFieldVector<Dfp>) v1.clone();
385 //      Assert.assertEquals(3, vout4.getDimension());
386 //      Assert.assertEquals(v1.getDataRef(), vout4.getDataRef());
387 
388 
389         FieldVector<Dfp> vout5 = v4.getSubVector(3, 3);
390         Assert.assertEquals(3, vout5.getDimension());
391         Assert.assertEquals(Dfp25.of(5), vout5.getEntry(1));
392         try {
393             v4.getSubVector(3, 7);
394             Assert.fail("OutOfRangeException expected");
395         } catch (OutOfRangeException ex) {
396             // expected behavior
397         }
398 
399         ArrayFieldVector<Dfp> v_set1 = (ArrayFieldVector<Dfp>) v1.copy();
400         v_set1.setEntry(1, Dfp25.of(11));
401         Assert.assertEquals(Dfp25.of(11), v_set1.getEntry(1));
402         try {
403             v_set1.setEntry(3, Dfp25.of(11));
404             Assert.fail("OutOfRangeException expected");
405         } catch (OutOfRangeException ex) {
406             // expected behavior
407         }
408 
409         ArrayFieldVector<Dfp> v_set2 = (ArrayFieldVector<Dfp>) v4.copy();
410         v_set2.set(3, v1);
411         Assert.assertEquals(Dfp25.of(1), v_set2.getEntry(3));
412         Assert.assertEquals(Dfp25.of(7), v_set2.getEntry(6));
413         try {
414             v_set2.set(7, v1);
415             Assert.fail("OutOfRangeException expected");
416         } catch (OutOfRangeException ex) {
417             // expected behavior
418         }
419 
420         ArrayFieldVector<Dfp> v_set3 = (ArrayFieldVector<Dfp>) v1.copy();
421         v_set3.set(Dfp25.of(13));
422         Assert.assertEquals(Dfp25.of(13), v_set3.getEntry(2));
423 
424         try {
425             v_set3.getEntry(23);
426             Assert.fail("ArrayIndexOutOfBoundsException expected");
427         } catch (ArrayIndexOutOfBoundsException ex) {
428             // expected behavior
429         }
430 
431         ArrayFieldVector<Dfp> v_set4 = (ArrayFieldVector<Dfp>) v4.copy();
432         v_set4.setSubVector(3, v2_t);
433         Assert.assertEquals(Dfp25.of(4), v_set4.getEntry(3));
434         Assert.assertEquals(Dfp25.of(7), v_set4.getEntry(6));
435         try {
436             v_set4.setSubVector(7, v2_t);
437             Assert.fail("OutOfRangeException expected");
438         } catch (OutOfRangeException ex) {
439             // expected behavior
440         }
441 
442 
443         ArrayFieldVector<Dfp> vout10 = (ArrayFieldVector<Dfp>) v1.copy();
444         ArrayFieldVector<Dfp> vout10_2 = (ArrayFieldVector<Dfp>) v1.copy();
445         Assert.assertEquals(vout10, vout10_2);
446         vout10_2.setEntry(0, Dfp25.of(11, 10));
447         Assert.assertNotSame(vout10, vout10_2);
448     }
449 
450     @Test
451     public void testMapFunctions() {
452         ArrayFieldVector<Dfp> v1 = new ArrayFieldVector<>(vec1);
453 
454         //octave =  v1 .+ 2.0
455         FieldVector<Dfp> v_mapAdd = v1.mapAdd(Dfp25.of(2));
456         Dfp[] result_mapAdd = {Dfp25.of(3), Dfp25.of(4), Dfp25.of(5)};
457         checkArray("compare vectors" ,result_mapAdd,v_mapAdd.toArray());
458 
459         //octave =  v1 .+ 2.0
460         FieldVector<Dfp> v_mapAddToSelf = v1.copy();
461         v_mapAddToSelf.mapAddToSelf(Dfp25.of(2));
462         Dfp[] result_mapAddToSelf = {Dfp25.of(3), Dfp25.of(4), Dfp25.of(5)};
463         checkArray("compare vectors" ,result_mapAddToSelf,v_mapAddToSelf.toArray());
464 
465         //octave =  v1 .- 2.0
466         FieldVector<Dfp> v_mapSubtract = v1.mapSubtract(Dfp25.of(2));
467         Dfp[] result_mapSubtract = {Dfp25.of(-1), Dfp25.ZERO, Dfp25.of(1)};
468         checkArray("compare vectors" ,result_mapSubtract,v_mapSubtract.toArray());
469 
470         //octave =  v1 .- 2.0
471         FieldVector<Dfp> v_mapSubtractToSelf = v1.copy();
472         v_mapSubtractToSelf.mapSubtractToSelf(Dfp25.of(2));
473         Dfp[] result_mapSubtractToSelf = {Dfp25.of(-1), Dfp25.ZERO, Dfp25.of(1)};
474         checkArray("compare vectors" ,result_mapSubtractToSelf,v_mapSubtractToSelf.toArray());
475 
476         //octave =  v1 .* 2.0
477         FieldVector<Dfp> v_mapMultiply = v1.mapMultiply(Dfp25.of(2));
478         Dfp[] result_mapMultiply = {Dfp25.of(2), Dfp25.of(4), Dfp25.of(6)};
479         checkArray("compare vectors" ,result_mapMultiply,v_mapMultiply.toArray());
480 
481         //octave =  v1 .* 2.0
482         FieldVector<Dfp> v_mapMultiplyToSelf = v1.copy();
483         v_mapMultiplyToSelf.mapMultiplyToSelf(Dfp25.of(2));
484         Dfp[] result_mapMultiplyToSelf = {Dfp25.of(2), Dfp25.of(4), Dfp25.of(6)};
485         checkArray("compare vectors" ,result_mapMultiplyToSelf,v_mapMultiplyToSelf.toArray());
486 
487         //octave =  v1 ./ 2.0
488         FieldVector<Dfp> v_mapDivide = v1.mapDivide(Dfp25.of(2));
489         Dfp[] result_mapDivide = {Dfp25.of(1, 2), Dfp25.of(1), Dfp25.of(3, 2)};
490         checkArray("compare vectors" ,result_mapDivide,v_mapDivide.toArray());
491 
492         //octave =  v1 ./ 2.0
493         FieldVector<Dfp> v_mapDivideToSelf = v1.copy();
494         v_mapDivideToSelf.mapDivideToSelf(Dfp25.of(2));
495         Dfp[] result_mapDivideToSelf = {Dfp25.of(1, 2), Dfp25.of(1), Dfp25.of(3, 2)};
496         checkArray("compare vectors" ,result_mapDivideToSelf,v_mapDivideToSelf.toArray());
497 
498         //octave =  v1 .^-1
499         FieldVector<Dfp> v_mapInv = v1.mapInv();
500         Dfp[] result_mapInv = {Dfp25.of(1),Dfp25.of(1, 2),Dfp25.of(1, 3)};
501         checkArray("compare vectors" ,result_mapInv,v_mapInv.toArray());
502 
503         //octave =  v1 .^-1
504         FieldVector<Dfp> v_mapInvToSelf = v1.copy();
505         v_mapInvToSelf.mapInvToSelf();
506         Dfp[] result_mapInvToSelf = {Dfp25.of(1),Dfp25.of(1, 2),Dfp25.of(1, 3)};
507         checkArray("compare vectors" ,result_mapInvToSelf,v_mapInvToSelf.toArray());
508     }
509 
510     @Test
511     public void testBasicFunctions() {
512         ArrayFieldVector<Dfp> v1 = new ArrayFieldVector<>(vec1);
513         ArrayFieldVector<Dfp> v2 = new ArrayFieldVector<>(vec2);
514         new ArrayFieldVector<>(vec_null);
515 
516         FieldVectorTestImpl<Dfp> v2_t = new FieldVectorTestImpl<>(vec2);
517 
518         //octave =  v1 + v2
519         ArrayFieldVector<Dfp> v_add = v1.add(v2);
520         Dfp[] result_add = {Dfp25.of(5), Dfp25.of(7), Dfp25.of(9)};
521         checkArray("compare vect" ,v_add.toArray(),result_add);
522 
523         FieldVectorTestImpl<Dfp> vt2 = new FieldVectorTestImpl<>(vec2);
524         FieldVector<Dfp> v_add_i = v1.add(vt2);
525         Dfp[] result_add_i = {Dfp25.of(5), Dfp25.of(7), Dfp25.of(9)};
526         checkArray("compare vect" ,v_add_i.toArray(),result_add_i);
527 
528         //octave =  v1 - v2
529         ArrayFieldVector<Dfp> v_subtract = v1.subtract(v2);
530         Dfp[] result_subtract = {Dfp25.of(-3), Dfp25.of(-3), Dfp25.of(-3)};
531         checkArray("compare vect" ,v_subtract.toArray(),result_subtract);
532 
533         FieldVector<Dfp> v_subtract_i = v1.subtract(vt2);
534         Dfp[] result_subtract_i = {Dfp25.of(-3), Dfp25.of(-3), Dfp25.of(-3)};
535         checkArray("compare vect" ,v_subtract_i.toArray(),result_subtract_i);
536 
537         // octave v1 .* v2
538         ArrayFieldVector<Dfp>  v_ebeMultiply = v1.ebeMultiply(v2);
539         Dfp[] result_ebeMultiply = {Dfp25.of(4), Dfp25.of(10), Dfp25.of(18)};
540         checkArray("compare vect" ,v_ebeMultiply.toArray(),result_ebeMultiply);
541 
542         FieldVector<Dfp>  v_ebeMultiply_2 = v1.ebeMultiply(v2_t);
543         Dfp[] result_ebeMultiply_2 = {Dfp25.of(4), Dfp25.of(10), Dfp25.of(18)};
544         checkArray("compare vect" ,v_ebeMultiply_2.toArray(),result_ebeMultiply_2);
545 
546         // octave v1 ./ v2
547         ArrayFieldVector<Dfp>  v_ebeDivide = v1.ebeDivide(v2);
548         Dfp[] result_ebeDivide = {Dfp25.of(1, 4), Dfp25.of(2, 5), Dfp25.of(1, 2)};
549         checkArray("compare vect" ,v_ebeDivide.toArray(),result_ebeDivide);
550 
551         FieldVector<Dfp>  v_ebeDivide_2 = v1.ebeDivide(v2_t);
552         Dfp[] result_ebeDivide_2 = {Dfp25.of(1, 4), Dfp25.of(2, 5), Dfp25.of(1, 2)};
553         checkArray("compare vect" ,v_ebeDivide_2.toArray(),result_ebeDivide_2);
554 
555         // octave  dot(v1,v2)
556         Dfp dot =  v1.dotProduct(v2);
557         Assert.assertEquals("compare val ",Dfp25.of(32), dot);
558 
559         // octave  dot(v1,v2_t)
560         Dfp dot_2 =  v1.dotProduct(v2_t);
561         Assert.assertEquals("compare val ",Dfp25.of(32), dot_2);
562 
563         FieldMatrix<Dfp> m_outerProduct = v1.outerProduct(v2);
564         Assert.assertEquals("compare val ",Dfp25.of(4), m_outerProduct.getEntry(0,0));
565 
566         FieldMatrix<Dfp> m_outerProduct_2 = v1.outerProduct(v2_t);
567         Assert.assertEquals("compare val ",Dfp25.of(4), m_outerProduct_2.getEntry(0,0));
568 
569         ArrayFieldVector<Dfp> v_projection = v1.projection(v2);
570         Dfp[] result_projection = {Dfp25.of(128, 77), Dfp25.of(160, 77), Dfp25.of(192, 77)};
571         checkArray("compare vect", v_projection.toArray(), result_projection);
572 
573         FieldVector<Dfp> v_projection_2 = v1.projection(v2_t);
574         Dfp[] result_projection_2 = {Dfp25.of(128, 77), Dfp25.of(160, 77), Dfp25.of(192, 77)};
575         checkArray("compare vect", v_projection_2.toArray(), result_projection_2);
576     }
577 
578     @Test
579     public void testMisc() {
580         ArrayFieldVector<Dfp> v1 = new ArrayFieldVector<>(vec1);
581         ArrayFieldVector<Dfp> v4 = new ArrayFieldVector<>(vec4);
582         FieldVector<Dfp> v4_2 = new ArrayFieldVector<>(vec4);
583 
584         String out1 = v1.toString();
585         Assert.assertTrue("some output ",  out1.length()!=0);
586         /*
587          Dfp[] dout1 = v1.copyOut();
588         Assert.assertEquals(3, dout1.length);
589         assertNotSame("testData not same object ", v1.getDataRef(), dout1);
590          */
591         try {
592             v1.checkVectorDimensions(2);
593             Assert.fail("MathIllegalArgumentException expected");
594         } catch (MathIllegalArgumentException ex) {
595             // expected behavior
596         }
597 
598        try {
599             v1.checkVectorDimensions(v4);
600             Assert.fail("MathIllegalArgumentException expected");
601         } catch (MathIllegalArgumentException ex) {
602             // expected behavior
603         }
604 
605         try {
606             v1.checkVectorDimensions(v4_2);
607             Assert.fail("MathIllegalArgumentException expected");
608         } catch (MathIllegalArgumentException ex) {
609             // expected behavior
610         }
611     }
612 
613     @Test
614     public void testSerial()  {
615         final int n = 2;
616         ArrayFieldVector<BigReal> v = new ArrayFieldVector<>(BigRealField.getInstance());
617         for (int i = 0; i < n; i++) {
618             v.append(new BigReal(Math.random()));
619         }
620         Assert.assertEquals(v, TestUtils.serializeAndRecover(v));
621     }
622 
623     @Test
624     public void testZeroVectors() {
625 
626         // when the field is not specified, array cannot be empty
627         try {
628             new ArrayFieldVector<>(new Dfp[0]);
629             Assert.fail("MathIllegalArgumentException expected");
630         } catch (MathIllegalArgumentException ex) {
631             // expected behavior
632         }
633         try {
634             new ArrayFieldVector<>(new Dfp[0], true);
635             Assert.fail("MathIllegalArgumentException expected");
636         } catch (MathIllegalArgumentException ex) {
637             // expected behavior
638         }
639         try {
640             new ArrayFieldVector<>(new Dfp[0], false);
641             Assert.fail("MathIllegalArgumentException expected");
642         } catch (MathIllegalArgumentException ex) {
643             // expected behavior
644         }
645 
646         // when the field is specified, array can be empty
647         Assert.assertEquals(0, new ArrayFieldVector<>(Dfp25.getField(), new Dfp[0]).getDimension());
648         Assert.assertEquals(0, new ArrayFieldVector<>(Dfp25.getField(), new Dfp[0], true).getDimension());
649         Assert.assertEquals(0, new ArrayFieldVector<>(Dfp25.getField(), new Dfp[0], false).getDimension());
650     }
651 
652     @Test
653     public void testOuterProduct() {
654         final ArrayFieldVector<Dfp> u
655             = new ArrayFieldVector<>(Dfp25.getField(),
656                                              new Dfp[] {Dfp25.of(1),
657                                                              Dfp25.of(2),
658                                                              Dfp25.of(-3)});
659         final ArrayFieldVector<Dfp> v
660             = new ArrayFieldVector<>(Dfp25.getField(),
661                                              new Dfp[] {Dfp25.of(4),
662                                                              Dfp25.of(-2)});
663 
664         final FieldMatrix<Dfp> uv = u.outerProduct(v);
665 
666         final double tol = Math.ulp(1d);
667         Assert.assertEquals(Dfp25.of(4).toDouble(), uv.getEntry(0, 0).toDouble(), tol);
668         Assert.assertEquals(Dfp25.of(-2).toDouble(), uv.getEntry(0, 1).toDouble(), tol);
669         Assert.assertEquals(Dfp25.of(8).toDouble(), uv.getEntry(1, 0).toDouble(), tol);
670         Assert.assertEquals(Dfp25.of(-4).toDouble(), uv.getEntry(1, 1).toDouble(), tol);
671         Assert.assertEquals(Dfp25.of(-12).toDouble(), uv.getEntry(2, 0).toDouble(), tol);
672         Assert.assertEquals(Dfp25.of(6).toDouble(), uv.getEntry(2, 1).toDouble(), tol);
673     }
674 
675     /** verifies that two vectors are equals */
676     protected void checkArray(String msg, Dfp[] m, Dfp[] n) {
677         if (m.length != n.length) {
678             Assert.fail("vectors have different lengths");
679         }
680         for (int i = 0; i < m.length; i++) {
681             Assert.assertEquals(msg + " " +  i + " elements differ", m[i],n[i]);
682         }
683     }
684 
685     /*
686      * TESTS OF THE VISITOR PATTERN
687      */
688 
689     /** The whole vector is visited. */
690     @Test
691     public void testWalkInDefaultOrderPreservingVisitor1() {
692         final Dfp[] data = new Dfp[] {
693             Dfp25.ZERO, Dfp25.ONE, Dfp25.ZERO,
694             Dfp25.ZERO, Dfp25.TWO, Dfp25.ZERO,
695             Dfp25.ZERO, Dfp25.ZERO, Dfp25.of(3)
696         };
697         final ArrayFieldVector<Dfp> v = new ArrayFieldVector<>(data);
698         final FieldVectorPreservingVisitor<Dfp> visitor;
699         visitor = new FieldVectorPreservingVisitor<Dfp>() {
700 
701             private int expectedIndex;
702 
703             @Override
704             public void visit(final int actualIndex, final Dfp actualValue) {
705                 Assert.assertEquals(expectedIndex, actualIndex);
706                 Assert.assertEquals(Integer.toString(actualIndex),
707                                     data[actualIndex], actualValue);
708                 ++expectedIndex;
709             }
710 
711             @Override
712             public void start(final int actualSize, final int actualStart,
713                               final int actualEnd) {
714                 Assert.assertEquals(data.length, actualSize);
715                 Assert.assertEquals(0, actualStart);
716                 Assert.assertEquals(data.length - 1, actualEnd);
717                 expectedIndex = 0;
718             }
719 
720             @Override
721             public Dfp end() {
722                 return Dfp25.ZERO;
723             }
724         };
725         v.walkInDefaultOrder(visitor);
726     }
727 
728     /** Visiting an invalid subvector. */
729     @Test
730     public void testWalkInDefaultOrderPreservingVisitor2() {
731         final ArrayFieldVector<Dfp> v = create(5);
732         final FieldVectorPreservingVisitor<Dfp> visitor;
733         visitor = new FieldVectorPreservingVisitor<Dfp>() {
734 
735             @Override
736             public void visit(int index, Dfp value) {
737                 // Do nothing
738             }
739 
740             @Override
741             public void start(int dimension, int start, int end) {
742                 // Do nothing
743             }
744 
745             @Override
746             public Dfp end() {
747                 return Dfp25.ZERO;
748             }
749         };
750         try {
751             v.walkInDefaultOrder(visitor, -1, 4);
752             Assert.fail();
753         } catch (OutOfRangeException e) {
754             // Expected behavior
755         }
756         try {
757             v.walkInDefaultOrder(visitor, 5, 4);
758             Assert.fail();
759         } catch (OutOfRangeException e) {
760             // Expected behavior
761         }
762         try {
763             v.walkInDefaultOrder(visitor, 0, -1);
764             Assert.fail();
765         } catch (OutOfRangeException e) {
766             // Expected behavior
767         }
768         try {
769             v.walkInDefaultOrder(visitor, 0, 5);
770             Assert.fail();
771         } catch (OutOfRangeException e) {
772             // Expected behavior
773         }
774         try {
775             v.walkInDefaultOrder(visitor, 4, 0);
776             Assert.fail();
777         } catch (NumberIsTooSmallException e) {
778             // Expected behavior
779         }
780     }
781 
782     /** Visiting a valid subvector. */
783     @Test
784     public void testWalkInDefaultOrderPreservingVisitor3() {
785         final Dfp[] data = new Dfp[] {
786             Dfp25.ZERO, Dfp25.ONE, Dfp25.ZERO,
787             Dfp25.ZERO, Dfp25.TWO, Dfp25.ZERO,
788             Dfp25.ZERO, Dfp25.ZERO, Dfp25.of(3)
789         };
790         final ArrayFieldVector<Dfp> v = new ArrayFieldVector<>(data);
791         final int expectedStart = 2;
792         final int expectedEnd = 7;
793         final FieldVectorPreservingVisitor<Dfp> visitor;
794         visitor = new FieldVectorPreservingVisitor<Dfp>() {
795 
796             private int expectedIndex;
797 
798             @Override
799             public void visit(final int actualIndex, final Dfp actualValue) {
800                 Assert.assertEquals(expectedIndex, actualIndex);
801                 Assert.assertEquals(Integer.toString(actualIndex),
802                                     data[actualIndex], actualValue);
803                 ++expectedIndex;
804             }
805 
806             @Override
807             public void start(final int actualSize, final int actualStart,
808                               final int actualEnd) {
809                 Assert.assertEquals(data.length, actualSize);
810                 Assert.assertEquals(expectedStart, actualStart);
811                 Assert.assertEquals(expectedEnd, actualEnd);
812                 expectedIndex = expectedStart;
813             }
814 
815             @Override
816             public Dfp end() {
817                 return Dfp25.ZERO;
818             }
819         };
820         v.walkInDefaultOrder(visitor, expectedStart, expectedEnd);
821     }
822 
823     /** The whole vector is visited. */
824     @Test
825     public void testWalkInOptimizedOrderPreservingVisitor1() {
826         final Dfp[] data = new Dfp[] {
827             Dfp25.ZERO, Dfp25.ONE, Dfp25.ZERO,
828             Dfp25.ZERO, Dfp25.TWO, Dfp25.ZERO,
829             Dfp25.ZERO, Dfp25.ZERO, Dfp25.of(3)
830         };
831         final ArrayFieldVector<Dfp> v = new ArrayFieldVector<>(data);
832         final FieldVectorPreservingVisitor<Dfp> visitor;
833         visitor = new FieldVectorPreservingVisitor<Dfp>() {
834             private final boolean[] visited = new boolean[data.length];
835 
836             @Override
837             public void visit(final int actualIndex, final Dfp actualValue) {
838                 visited[actualIndex] = true;
839                 Assert.assertEquals(Integer.toString(actualIndex),
840                                     data[actualIndex], actualValue);
841             }
842 
843             @Override
844             public void start(final int actualSize, final int actualStart,
845                               final int actualEnd) {
846                 Assert.assertEquals(data.length, actualSize);
847                 Assert.assertEquals(0, actualStart);
848                 Assert.assertEquals(data.length - 1, actualEnd);
849                 Arrays.fill(visited, false);
850             }
851 
852             @Override
853             public Dfp end() {
854                 for (int i = 0; i < data.length; i++) {
855                     Assert.assertTrue("entry " + i + "has not been visited",
856                                       visited[i]);
857                 }
858                 return Dfp25.ZERO;
859             }
860         };
861         v.walkInOptimizedOrder(visitor);
862     }
863 
864     /** Visiting an invalid subvector. */
865     @Test
866     public void testWalkInOptimizedOrderPreservingVisitor2() {
867         final ArrayFieldVector<Dfp> v = create(5);
868         final FieldVectorPreservingVisitor<Dfp> visitor;
869         visitor = new FieldVectorPreservingVisitor<Dfp>() {
870 
871             @Override
872             public void visit(int index, Dfp value) {
873                 // Do nothing
874             }
875 
876             @Override
877             public void start(int dimension, int start, int end) {
878                 // Do nothing
879             }
880 
881             @Override
882             public Dfp end() {
883                 return Dfp25.ZERO;
884             }
885         };
886         try {
887             v.walkInOptimizedOrder(visitor, -1, 4);
888             Assert.fail();
889         } catch (OutOfRangeException e) {
890             // Expected behavior
891         }
892         try {
893             v.walkInOptimizedOrder(visitor, 5, 4);
894             Assert.fail();
895         } catch (OutOfRangeException e) {
896             // Expected behavior
897         }
898         try {
899             v.walkInOptimizedOrder(visitor, 0, -1);
900             Assert.fail();
901         } catch (OutOfRangeException e) {
902             // Expected behavior
903         }
904         try {
905             v.walkInOptimizedOrder(visitor, 0, 5);
906             Assert.fail();
907         } catch (OutOfRangeException e) {
908             // Expected behavior
909         }
910         try {
911             v.walkInOptimizedOrder(visitor, 4, 0);
912             Assert.fail();
913         } catch (NumberIsTooSmallException e) {
914             // Expected behavior
915         }
916     }
917 
918     /** Visiting a valid subvector. */
919     @Test
920     public void testWalkInOptimizedOrderPreservingVisitor3() {
921         final Dfp[] data = new Dfp[] {
922             Dfp25.ZERO, Dfp25.ONE, Dfp25.ZERO,
923             Dfp25.ZERO, Dfp25.TWO, Dfp25.ZERO,
924             Dfp25.ZERO, Dfp25.ZERO, Dfp25.of(3)
925         };
926         final ArrayFieldVector<Dfp> v = new ArrayFieldVector<>(data);
927         final int expectedStart = 2;
928         final int expectedEnd = 7;
929         final FieldVectorPreservingVisitor<Dfp> visitor;
930         visitor = new FieldVectorPreservingVisitor<Dfp>() {
931             private final boolean[] visited = new boolean[data.length];
932 
933             @Override
934             public void visit(final int actualIndex, final Dfp actualValue) {
935                 Assert.assertEquals(Integer.toString(actualIndex),
936                                     data[actualIndex], actualValue);
937                 visited[actualIndex] = true;
938             }
939 
940             @Override
941             public void start(final int actualSize, final int actualStart,
942                               final int actualEnd) {
943                 Assert.assertEquals(data.length, actualSize);
944                 Assert.assertEquals(expectedStart, actualStart);
945                 Assert.assertEquals(expectedEnd, actualEnd);
946                 Arrays.fill(visited, true);
947             }
948 
949             @Override
950             public Dfp end() {
951                 for (int i = expectedStart; i <= expectedEnd; i++) {
952                     Assert.assertTrue("entry " + i + "has not been visited",
953                                       visited[i]);
954                 }
955                 return Dfp25.ZERO;
956             }
957         };
958         v.walkInOptimizedOrder(visitor, expectedStart, expectedEnd);
959     }
960 
961     /** The whole vector is visited. */
962     @Test
963     public void testWalkInDefaultOrderChangingVisitor1() {
964         final Dfp[] data = new Dfp[] {
965             Dfp25.ZERO, Dfp25.ONE, Dfp25.ZERO,
966             Dfp25.ZERO, Dfp25.TWO, Dfp25.ZERO,
967             Dfp25.ZERO, Dfp25.ZERO, Dfp25.of(3)
968         };
969         final ArrayFieldVector<Dfp> v = new ArrayFieldVector<>(data);
970         final FieldVectorChangingVisitor<Dfp> visitor;
971         visitor = new FieldVectorChangingVisitor<Dfp>() {
972 
973             private int expectedIndex;
974 
975             @Override
976             public Dfp visit(final int actualIndex, final Dfp actualValue) {
977                 Assert.assertEquals(expectedIndex, actualIndex);
978                 Assert.assertEquals(Integer.toString(actualIndex),
979                                     data[actualIndex], actualValue);
980                 ++expectedIndex;
981                 return actualValue.add(actualIndex);
982             }
983 
984             @Override
985             public void start(final int actualSize, final int actualStart,
986                               final int actualEnd) {
987                 Assert.assertEquals(data.length, actualSize);
988                 Assert.assertEquals(0, actualStart);
989                 Assert.assertEquals(data.length - 1, actualEnd);
990                 expectedIndex = 0;
991             }
992 
993             @Override
994             public Dfp end() {
995                 return Dfp25.ZERO;
996             }
997         };
998         v.walkInDefaultOrder(visitor);
999         for (int i = 0; i < data.length; i++) {
1000             Assert.assertEquals("entry " + i, data[i].add(i), v.getEntry(i));
1001         }
1002     }
1003 
1004     /** Visiting an invalid subvector. */
1005     @Test
1006     public void testWalkInDefaultOrderChangingVisitor2() {
1007         final ArrayFieldVector<Dfp> v = create(5);
1008         final FieldVectorChangingVisitor<Dfp> visitor;
1009         visitor = new FieldVectorChangingVisitor<Dfp>() {
1010 
1011             @Override
1012             public Dfp visit(int index, Dfp value) {
1013                 return Dfp25.ZERO;
1014             }
1015 
1016             @Override
1017             public void start(int dimension, int start, int end) {
1018                 // Do nothing
1019             }
1020 
1021             @Override
1022             public Dfp end() {
1023                 return Dfp25.ZERO;
1024             }
1025         };
1026         try {
1027             v.walkInDefaultOrder(visitor, -1, 4);
1028             Assert.fail();
1029         } catch (OutOfRangeException e) {
1030             // Expected behavior
1031         }
1032         try {
1033             v.walkInDefaultOrder(visitor, 5, 4);
1034             Assert.fail();
1035         } catch (OutOfRangeException e) {
1036             // Expected behavior
1037         }
1038         try {
1039             v.walkInDefaultOrder(visitor, 0, -1);
1040             Assert.fail();
1041         } catch (OutOfRangeException e) {
1042             // Expected behavior
1043         }
1044         try {
1045             v.walkInDefaultOrder(visitor, 0, 5);
1046             Assert.fail();
1047         } catch (OutOfRangeException e) {
1048             // Expected behavior
1049         }
1050         try {
1051             v.walkInDefaultOrder(visitor, 4, 0);
1052             Assert.fail();
1053         } catch (NumberIsTooSmallException e) {
1054             // Expected behavior
1055         }
1056     }
1057 
1058     /** Visiting a valid subvector. */
1059     @Test
1060     public void testWalkInDefaultOrderChangingVisitor3() {
1061         final Dfp[] data = new Dfp[] {
1062             Dfp25.ZERO, Dfp25.ONE, Dfp25.ZERO,
1063             Dfp25.ZERO, Dfp25.TWO, Dfp25.ZERO,
1064             Dfp25.ZERO, Dfp25.ZERO, Dfp25.of(3)
1065         };
1066         final ArrayFieldVector<Dfp> v = new ArrayFieldVector<>(data);
1067         final int expectedStart = 2;
1068         final int expectedEnd = 7;
1069         final FieldVectorChangingVisitor<Dfp> visitor;
1070         visitor = new FieldVectorChangingVisitor<Dfp>() {
1071 
1072             private int expectedIndex;
1073 
1074             @Override
1075             public Dfp visit(final int actualIndex, final Dfp actualValue) {
1076                 Assert.assertEquals(expectedIndex, actualIndex);
1077                 Assert.assertEquals(Integer.toString(actualIndex),
1078                                     data[actualIndex], actualValue);
1079                 ++expectedIndex;
1080                 return actualValue.add(actualIndex);
1081             }
1082 
1083             @Override
1084             public void start(final int actualSize, final int actualStart,
1085                               final int actualEnd) {
1086                 Assert.assertEquals(data.length, actualSize);
1087                 Assert.assertEquals(expectedStart, actualStart);
1088                 Assert.assertEquals(expectedEnd, actualEnd);
1089                 expectedIndex = expectedStart;
1090             }
1091 
1092             @Override
1093             public Dfp end() {
1094                 return Dfp25.ZERO;
1095             }
1096         };
1097         v.walkInDefaultOrder(visitor, expectedStart, expectedEnd);
1098         for (int i = expectedStart; i <= expectedEnd; i++) {
1099             Assert.assertEquals("entry " + i, data[i].add(i), v.getEntry(i));
1100         }
1101     }
1102 
1103     /** The whole vector is visited. */
1104     @Test
1105     public void testWalkInOptimizedOrderChangingVisitor1() {
1106         final Dfp[] data = new Dfp[] {
1107             Dfp25.ZERO, Dfp25.ONE, Dfp25.ZERO,
1108             Dfp25.ZERO, Dfp25.TWO, Dfp25.ZERO,
1109             Dfp25.ZERO, Dfp25.ZERO, Dfp25.of(3)
1110         };
1111         final ArrayFieldVector<Dfp> v = new ArrayFieldVector<>(data);
1112         final FieldVectorChangingVisitor<Dfp> visitor;
1113         visitor = new FieldVectorChangingVisitor<Dfp>() {
1114             private final boolean[] visited = new boolean[data.length];
1115 
1116             @Override
1117             public Dfp visit(final int actualIndex, final Dfp actualValue) {
1118                 visited[actualIndex] = true;
1119                 Assert.assertEquals(Integer.toString(actualIndex),
1120                                     data[actualIndex], actualValue);
1121                 return actualValue.add(actualIndex);
1122             }
1123 
1124             @Override
1125             public void start(final int actualSize, final int actualStart,
1126                               final int actualEnd) {
1127                 Assert.assertEquals(data.length, actualSize);
1128                 Assert.assertEquals(0, actualStart);
1129                 Assert.assertEquals(data.length - 1, actualEnd);
1130                 Arrays.fill(visited, false);
1131             }
1132 
1133             @Override
1134             public Dfp end() {
1135                 for (int i = 0; i < data.length; i++) {
1136                     Assert.assertTrue("entry " + i + "has not been visited",
1137                                       visited[i]);
1138                 }
1139                 return Dfp25.ZERO;
1140             }
1141         };
1142         v.walkInOptimizedOrder(visitor);
1143         for (int i = 0; i < data.length; i++) {
1144             Assert.assertEquals("entry " + i, data[i].add(i), v.getEntry(i));
1145         }
1146     }
1147 
1148     /** Visiting an invalid subvector. */
1149     @Test
1150     public void testWalkInOptimizedOrderChangingVisitor2() {
1151         final ArrayFieldVector<Dfp> v = create(5);
1152         final FieldVectorChangingVisitor<Dfp> visitor;
1153         visitor = new FieldVectorChangingVisitor<Dfp>() {
1154 
1155             @Override
1156             public Dfp visit(int index, Dfp value) {
1157                 return Dfp25.ZERO;
1158             }
1159 
1160             @Override
1161             public void start(int dimension, int start, int end) {
1162                 // Do nothing
1163             }
1164 
1165             @Override
1166             public Dfp end() {
1167                 return Dfp25.ZERO;
1168             }
1169         };
1170         try {
1171             v.walkInOptimizedOrder(visitor, -1, 4);
1172             Assert.fail();
1173         } catch (OutOfRangeException e) {
1174             // Expected behavior
1175         }
1176         try {
1177             v.walkInOptimizedOrder(visitor, 5, 4);
1178             Assert.fail();
1179         } catch (OutOfRangeException e) {
1180             // Expected behavior
1181         }
1182         try {
1183             v.walkInOptimizedOrder(visitor, 0, -1);
1184             Assert.fail();
1185         } catch (OutOfRangeException e) {
1186             // Expected behavior
1187         }
1188         try {
1189             v.walkInOptimizedOrder(visitor, 0, 5);
1190             Assert.fail();
1191         } catch (OutOfRangeException e) {
1192             // Expected behavior
1193         }
1194         try {
1195             v.walkInOptimizedOrder(visitor, 4, 0);
1196             Assert.fail();
1197         } catch (NumberIsTooSmallException e) {
1198             // Expected behavior
1199         }
1200     }
1201 
1202     /** Visiting a valid subvector. */
1203     @Test
1204     public void testWalkInOptimizedOrderChangingVisitor3() {
1205         final Dfp[] data = new Dfp[] {
1206             Dfp25.ZERO, Dfp25.ONE, Dfp25.ZERO,
1207             Dfp25.ZERO, Dfp25.TWO, Dfp25.ZERO,
1208             Dfp25.ZERO, Dfp25.ZERO, Dfp25.of(3)
1209         };
1210         final ArrayFieldVector<Dfp> v = new ArrayFieldVector<>(data);
1211         final int expectedStart = 2;
1212         final int expectedEnd = 7;
1213         final FieldVectorChangingVisitor<Dfp> visitor;
1214         visitor = new FieldVectorChangingVisitor<Dfp>() {
1215             private final boolean[] visited = new boolean[data.length];
1216 
1217             @Override
1218             public Dfp visit(final int actualIndex, final Dfp actualValue) {
1219                 Assert.assertEquals(Integer.toString(actualIndex),
1220                                     data[actualIndex], actualValue);
1221                 visited[actualIndex] = true;
1222                 return actualValue.add(actualIndex);
1223             }
1224 
1225             @Override
1226             public void start(final int actualSize, final int actualStart,
1227                               final int actualEnd) {
1228                 Assert.assertEquals(data.length, actualSize);
1229                 Assert.assertEquals(expectedStart, actualStart);
1230                 Assert.assertEquals(expectedEnd, actualEnd);
1231                 Arrays.fill(visited, true);
1232             }
1233 
1234             @Override
1235             public Dfp end() {
1236                 for (int i = expectedStart; i <= expectedEnd; i++) {
1237                     Assert.assertTrue("entry " + i + "has not been visited",
1238                                       visited[i]);
1239                 }
1240                 return Dfp25.ZERO;
1241             }
1242         };
1243         v.walkInOptimizedOrder(visitor, expectedStart, expectedEnd);
1244         for (int i = expectedStart; i <= expectedEnd; i++) {
1245             Assert.assertEquals("entry " + i, data[i].add(i), v.getEntry(i));
1246         }
1247     }
1248 
1249     private ArrayFieldVector<Dfp> create(int n) {
1250         Dfp[] t = new Dfp[n];
1251         for (int i = 0; i < n; ++i) {
1252             t[i] = Dfp25.ZERO;
1253         }
1254         return new ArrayFieldVector<>(t);
1255     }
1256 }