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    
021    import junit.framework.Test;
022    import junit.framework.TestCase;
023    import junit.framework.TestSuite;
024    
025    import org.apache.commons.math.TestUtils;
026    
027    /**
028     * Test cases for the {@link OpenMapRealVector} class.
029     *
030     * @version $Revision: 799857 $ $Date: 2009-08-01 09:07:12 -0400 (Sat, 01 Aug 2009) $
031     */
032    public class SparseRealVectorTest extends TestCase {
033    
034        // 
035        protected double[][] ma1 = {{1d, 2d, 3d}, {4d, 5d, 6d}, {7d, 8d, 9d}};
036        protected double[] vec1 = {1d, 2d, 3d};
037        protected double[] vec2 = {4d, 5d, 6d};
038        protected double[] vec3 = {7d, 8d, 9d};
039        protected double[] vec4 = {1d, 2d, 3d, 4d, 5d, 6d, 7d, 8d, 9d};
040        protected double[] vec_null = {0d, 0d, 0d};
041        protected Double[] dvec1 = {1d, 2d, 3d, 4d, 5d, 6d, 7d, 8d, 9d};
042        protected double[][] mat1 = {{1d, 2d, 3d}, {4d, 5d, 6d},{ 7d, 8d, 9d}};
043    
044        // tolerances
045        protected double entryTolerance = 10E-16;
046        protected double normTolerance = 10E-14;
047    
048        // Testclass to test the RealVector interface 
049        // only with enough content to support the test
050        public static class SparseRealVectorTestImpl implements RealVector, Serializable {
051    
052            /** Serializable version identifier. */
053            private static final long serialVersionUID = 4715341047369582908L;
054    
055            /** Entries of the vector. */
056            protected double data[];
057    
058            public SparseRealVectorTestImpl(double[] d) {
059                data = d.clone();
060            }
061    
062            private UnsupportedOperationException unsupported() {
063                return new UnsupportedOperationException("Not supported, unneeded for test purposes");
064            }
065    
066            public RealVector copy() {
067                throw unsupported();
068            }
069    
070            public RealVector add(RealVector v) throws IllegalArgumentException {
071                throw unsupported();
072            }
073    
074            public RealVector add(double[] v) throws IllegalArgumentException {
075                throw unsupported();
076            }
077    
078            public RealVector subtract(RealVector v) throws IllegalArgumentException {
079                throw unsupported();
080            }
081    
082            public RealVector subtract(double[] v) throws IllegalArgumentException {
083                throw unsupported();
084            }
085    
086            public RealVector mapAdd(double d) {
087                throw unsupported();
088            }
089    
090            public RealVector mapAddToSelf(double d) {
091                throw unsupported();
092            }
093    
094            public RealVector mapSubtract(double d) {
095                throw unsupported();
096            }
097    
098            public RealVector mapSubtractToSelf(double d) {
099                throw unsupported();
100            }
101    
102            public RealVector mapMultiply(double d) {
103                double[] out = new double[data.length];
104                for (int i = 0; i < data.length; i++) {
105                    out[i] = data[i] * d;
106                }
107                return new OpenMapRealVector(out);
108            }
109    
110            public RealVector mapMultiplyToSelf(double d) {
111                throw unsupported();
112            }
113    
114            public RealVector mapDivide(double d) {
115                throw unsupported();
116            }
117    
118            public RealVector mapDivideToSelf(double d) {
119                throw unsupported();
120            }
121    
122            public RealVector mapPow(double d) {
123                throw unsupported();
124            }
125    
126            public RealVector mapPowToSelf(double d) {
127                throw unsupported();
128            }
129    
130            public RealVector mapExp() {
131                throw unsupported();
132            }
133    
134            public RealVector mapExpToSelf() {
135                throw unsupported();
136            }
137    
138            public RealVector mapExpm1() {
139                throw unsupported();
140            }
141    
142            public RealVector mapExpm1ToSelf() {
143                throw unsupported();
144            }
145    
146            public RealVector mapLog() {
147                throw unsupported();
148            }
149    
150            public RealVector mapLogToSelf() {
151                throw unsupported();
152            }
153    
154            public RealVector mapLog10() {
155                throw unsupported();
156            }
157    
158            public RealVector mapLog10ToSelf() {
159                throw unsupported();
160            }
161    
162            public RealVector mapLog1p() {
163                throw unsupported();
164            }
165    
166            public RealVector mapLog1pToSelf() {
167                throw unsupported();
168            }
169    
170            public RealVector mapCosh() {
171                throw unsupported();
172            }
173    
174            public RealVector mapCoshToSelf() {
175                throw unsupported();
176            }
177    
178            public RealVector mapSinh() {
179                throw unsupported();
180            }
181    
182            public RealVector mapSinhToSelf() {
183                throw unsupported();
184            }
185    
186            public RealVector mapTanh() {
187                throw unsupported();
188            }
189    
190            public RealVector mapTanhToSelf() {
191                throw unsupported();
192            }
193    
194            public RealVector mapCos() {
195                throw unsupported();
196            }
197    
198            public RealVector mapCosToSelf() {
199                throw unsupported();
200            }
201    
202            public RealVector mapSin() {
203                throw unsupported();
204            }
205    
206            public RealVector mapSinToSelf() {
207                throw unsupported();
208            }
209    
210            public RealVector mapTan() {
211                throw unsupported();
212            }
213    
214            public RealVector mapTanToSelf() {
215                throw unsupported();
216            }
217    
218            public RealVector mapAcos() {
219                throw unsupported();
220            }
221    
222            public RealVector mapAcosToSelf() {
223                throw unsupported();
224            }
225    
226            public RealVector mapAsin() {
227                throw unsupported();
228            }
229    
230            public RealVector mapAsinToSelf() {
231                throw unsupported();
232            }
233    
234            public RealVector mapAtan() {
235                throw unsupported();
236            }
237    
238            public RealVector mapAtanToSelf() {
239                throw unsupported();
240            }
241    
242            public RealVector mapInv() {
243                throw unsupported();
244            }
245    
246            public RealVector mapInvToSelf() {
247                throw unsupported();
248            }
249    
250            public RealVector mapAbs() {
251                throw unsupported();
252            }
253    
254            public RealVector mapAbsToSelf() {
255                throw unsupported();
256            }
257    
258            public RealVector mapSqrt() {
259                throw unsupported();
260            }
261    
262            public RealVector mapSqrtToSelf() {
263                throw unsupported();
264            }
265    
266            public RealVector mapCbrt() {
267                throw unsupported();
268            }
269    
270            public RealVector mapCbrtToSelf() {
271                throw unsupported();
272            }
273    
274            public RealVector mapCeil() {
275                throw unsupported();
276            }
277    
278            public RealVector mapCeilToSelf() {
279                throw unsupported();
280            }
281    
282            public RealVector mapFloor() {
283                throw unsupported();
284            }
285    
286            public RealVector mapFloorToSelf() {
287                throw unsupported();
288            }
289    
290            public RealVector mapRint() {
291                throw unsupported();
292            }
293    
294            public RealVector mapRintToSelf() {
295                throw unsupported();
296            }
297    
298            public RealVector mapSignum() {
299                throw unsupported();
300            }
301    
302            public RealVector mapSignumToSelf() {
303                throw unsupported();
304            }
305    
306            public RealVector mapUlp() {
307                throw unsupported();
308            }
309    
310            public RealVector mapUlpToSelf() {
311                throw unsupported();
312            }
313    
314            public RealVector ebeMultiply(RealVector v) throws IllegalArgumentException {
315                throw unsupported();
316            }
317    
318            public RealVector ebeMultiply(double[] v) throws IllegalArgumentException {
319                throw unsupported();
320            }
321    
322            public RealVector ebeDivide(RealVector v) throws IllegalArgumentException {
323                throw unsupported();
324            }
325    
326            public RealVector ebeDivide(double[] v) throws IllegalArgumentException {
327                throw unsupported();
328            }
329    
330            public double[] getData() {
331                return data.clone();
332            }
333    
334            public double dotProduct(RealVector v) throws IllegalArgumentException {
335                double dot = 0;
336                for (int i = 0; i < data.length; i++) {
337                    dot += data[i] * v.getEntry(i);
338                }
339                return dot;
340            }
341    
342            public double dotProduct(double[] v) throws IllegalArgumentException {
343                double dot = 0;
344                for (int i = 0; i < data.length; i++) {
345                    dot += data[i] * v[i];
346                }
347                return dot;
348            }
349    
350            public double getNorm() {
351                throw unsupported();
352            }
353    
354            public double getL1Norm() {
355                throw unsupported();
356            }
357    
358            public double getLInfNorm() {
359                throw unsupported();
360            }
361    
362            public double getDistance(RealVector v) throws IllegalArgumentException {
363                throw unsupported();
364            }
365    
366            public double getDistance(double[] v) throws IllegalArgumentException {
367                throw unsupported();
368            }
369    
370            public double getL1Distance(RealVector v) throws IllegalArgumentException {
371                throw unsupported();
372            }
373    
374            public double getL1Distance(double[] v) throws IllegalArgumentException {
375                throw unsupported();
376            }
377    
378            public double getLInfDistance(RealVector v) throws IllegalArgumentException {
379                throw unsupported();
380            }
381    
382            public double getLInfDistance(double[] v) throws IllegalArgumentException {
383                throw unsupported();
384            }
385    
386            public RealVector unitVector() {
387                throw unsupported();
388            }
389    
390            public void unitize() {
391                throw unsupported();
392            }
393    
394            public RealVector projection(RealVector v) throws IllegalArgumentException {
395                throw unsupported();
396            }
397    
398            public RealVector projection(double[] v) throws IllegalArgumentException {
399                throw unsupported();
400            }
401    
402            public RealMatrix outerProduct(RealVector v) throws IllegalArgumentException {
403                throw unsupported();
404            }
405    
406            public RealMatrix outerProduct(double[] v) throws IllegalArgumentException {
407                throw unsupported();
408            }
409    
410            public double getEntry(int index) throws MatrixIndexException {
411                return data[index];
412            }
413    
414            public int getDimension() {
415                return data.length;
416            }
417    
418            public RealVector append(RealVector v) {
419                throw unsupported();
420            }
421    
422            public RealVector append(double d) {
423                throw unsupported();
424            }
425    
426            public RealVector append(double[] a) {
427                throw unsupported();
428            }
429    
430            public RealVector getSubVector(int index, int n) throws MatrixIndexException {
431                throw unsupported();
432            }
433    
434            public void setEntry(int index, double value) throws MatrixIndexException {
435                throw unsupported();
436            }
437    
438            public void setSubVector(int index, RealVector v) throws MatrixIndexException {
439                throw unsupported();
440            }
441    
442            public void setSubVector(int index, double[] v) throws MatrixIndexException {
443                throw unsupported();
444            }
445    
446            public void set(double value) {
447                throw unsupported();
448            }
449    
450            public double[] toArray() {
451                throw unsupported();
452            }
453    
454            public boolean isNaN() {
455                throw unsupported();
456            }
457    
458            public boolean isInfinite() {
459                throw unsupported();
460            }
461    
462        }
463    
464        public static Test suite() {
465            TestSuite suite = new TestSuite(SparseRealVectorTest.class);
466            suite.setName("SparseRealVector Tests");
467            return suite;
468        }
469    
470        public void testConstructors() {
471    
472            OpenMapRealVector v0 = new OpenMapRealVector();
473            assertEquals("testData len", 0, v0.getDimension());
474    
475            OpenMapRealVector v1 = new OpenMapRealVector(7);
476            assertEquals("testData len", 7, v1.getDimension());
477            assertEquals("testData is 0.0 ", 0.0, v1.getEntry(6));
478    
479            OpenMapRealVector v3 = new OpenMapRealVector(vec1);
480            assertEquals("testData len", 3, v3.getDimension());
481            assertEquals("testData is 2.0 ", 2.0, v3.getEntry(1));
482    
483            //SparseRealVector v4 = new SparseRealVector(vec4, 3, 2);
484            //assertEquals("testData len", 2, v4.getDimension());
485            //assertEquals("testData is 4.0 ", 4.0, v4.getEntry(0));
486            //try {
487            //    new SparseRealVector(vec4, 8, 3);
488            //    fail("IllegalArgumentException expected");
489            //} catch (IllegalArgumentException ex) {
490                // expected behavior
491            //} catch (Exception e) {
492            //    fail("wrong exception caught");
493            //}
494    
495            RealVector v5_i = new OpenMapRealVector(dvec1);
496            assertEquals("testData len", 9, v5_i.getDimension());
497            assertEquals("testData is 9.0 ", 9.0, v5_i.getEntry(8));
498    
499            OpenMapRealVector v5 = new OpenMapRealVector(dvec1);
500            assertEquals("testData len", 9, v5.getDimension());
501            assertEquals("testData is 9.0 ", 9.0, v5.getEntry(8));
502    
503            OpenMapRealVector v7 = new OpenMapRealVector(v1);
504            assertEquals("testData len", 7, v7.getDimension());
505            assertEquals("testData is 0.0 ", 0.0, v7.getEntry(6));
506    
507            SparseRealVectorTestImpl v7_i = new SparseRealVectorTestImpl(vec1);
508    
509            OpenMapRealVector v7_2 = new OpenMapRealVector(v7_i);
510            assertEquals("testData len", 3, v7_2.getDimension());
511            assertEquals("testData is 0.0 ", 2.0d, v7_2.getEntry(1));
512    
513            OpenMapRealVector v8 = new OpenMapRealVector(v1);
514            assertEquals("testData len", 7, v8.getDimension());
515            assertEquals("testData is 0.0 ", 0.0, v8.getEntry(6));
516    
517        }
518    
519        public void testDataInOut() {
520    
521            OpenMapRealVector v1 = new OpenMapRealVector(vec1);
522            OpenMapRealVector v2 = new OpenMapRealVector(vec2);
523            OpenMapRealVector v4 = new OpenMapRealVector(vec4);
524            SparseRealVectorTestImpl v2_t = new SparseRealVectorTestImpl(vec2); 
525    
526            RealVector v_append_1 = v1.append(v2);
527            assertEquals("testData len", 6, v_append_1.getDimension());
528            assertEquals("testData is 4.0 ", 4.0, v_append_1.getEntry(3));
529    
530            RealVector v_append_2 = v1.append(2.0);
531            assertEquals("testData len", 4, v_append_2.getDimension());
532            assertEquals("testData is 2.0 ", 2.0, v_append_2.getEntry(3));
533    
534            RealVector v_append_3 = v1.append(vec2);
535            assertEquals("testData len", 6, v_append_3.getDimension());
536            assertEquals("testData is  ", 4.0, v_append_3.getEntry(3));
537    
538                RealVector v_append_4 = v1.append(v2_t);
539            assertEquals("testData len", 6, v_append_4.getDimension());
540            assertEquals("testData is 4.0 ", 4.0, v_append_4.getEntry(3));
541            
542            RealVector vout5 = v4.getSubVector(3, 3);
543            assertEquals("testData len", 3, vout5.getDimension());
544            assertEquals("testData is 4.0 ", 5.0, vout5.getEntry(1));
545            try {
546                v4.getSubVector(3, 7);
547                fail("MatrixIndexException expected");
548            } catch (MatrixIndexException ex) {
549                // expected behavior
550            } catch (Exception e) {
551                fail("wrong exception caught");
552            }
553    
554            OpenMapRealVector v_set1 = (OpenMapRealVector) v1.copy();
555            v_set1.setEntry(1, 11.0);
556            assertEquals("testData is 11.0 ", 11.0, v_set1.getEntry(1));
557            try {
558                v_set1.setEntry(3, 11.0);
559                fail("MatrixIndexException expected");
560            } catch (MatrixIndexException ex) {
561                // expected behavior
562            } catch (Exception e) {
563                fail("wrong exception caught");
564            }
565    
566            OpenMapRealVector v_set2 = (OpenMapRealVector) v4.copy();
567            v_set2.setSubVector(3, v1);
568            assertEquals("testData is 1.0 ", 1.0, v_set2.getEntry(3));
569            assertEquals("testData is 7.0 ", 7.0, v_set2.getEntry(6));
570            try {
571                v_set2.setSubVector(7, v1);
572                fail("MatrixIndexException expected");
573            } catch (MatrixIndexException ex) {
574                // expected behavior
575            } catch (Exception e) {
576                fail("wrong exception caught");
577            }
578    
579            OpenMapRealVector v_set3 = (OpenMapRealVector) v1.copy();
580            v_set3.set(13.0);
581            assertEquals("testData is 13.0 ", 13.0, v_set3.getEntry(2));
582    
583            try {
584                v_set3.getEntry(23);
585                fail("MatrixIndexException expected");
586            } catch (MatrixIndexException ex) {
587                // expected behavior
588            } catch (Exception e) {
589                fail("wrong exception caught");
590            }
591    
592            OpenMapRealVector v_set4 = (OpenMapRealVector) v4.copy();
593            v_set4.setSubVector(3, v2_t);
594            assertEquals("testData is 1.0 ", 4.0, v_set4.getEntry(3));
595            assertEquals("testData is 7.0 ", 7.0, v_set4.getEntry(6));
596            try {
597                v_set4.setSubVector(7, v2_t);
598                fail("MatrixIndexException expected");
599            } catch (MatrixIndexException ex) {
600                // expected behavior
601            } catch (Exception e) {
602                fail("wrong exception caught");
603            }
604    
605    
606        }
607    
608        public void testMapFunctions() { 
609            OpenMapRealVector v1 = new OpenMapRealVector(vec1);
610    
611            //octave =  v1 .+ 2.0
612            RealVector v_mapAdd = v1.mapAdd(2.0d);
613            double[] result_mapAdd = {3d, 4d, 5d};
614            assertClose("compare vectors" ,result_mapAdd,v_mapAdd.getData(),normTolerance);
615    
616            //octave =  v1 .+ 2.0
617            RealVector v_mapAddToSelf = v1.copy();
618            v_mapAddToSelf.mapAddToSelf(2.0d);
619            double[] result_mapAddToSelf = {3d, 4d, 5d};
620            assertClose("compare vectors" ,result_mapAddToSelf,v_mapAddToSelf.getData(),normTolerance);
621    
622            //octave =  v1 .- 2.0
623            RealVector v_mapSubtract = v1.mapSubtract(2.0d);
624            double[] result_mapSubtract = {-1d, 0d, 1d};
625            assertClose("compare vectors" ,result_mapSubtract,v_mapSubtract.getData(),normTolerance);
626    
627            //octave =  v1 .- 2.0
628            RealVector v_mapSubtractToSelf = v1.copy();
629            v_mapSubtractToSelf.mapSubtractToSelf(2.0d);
630            double[] result_mapSubtractToSelf = {-1d, 0d, 1d};
631            assertClose("compare vectors" ,result_mapSubtractToSelf,v_mapSubtractToSelf.getData(),normTolerance);
632    
633            //octave =  v1 .* 2.0
634            RealVector v_mapMultiply = v1.mapMultiply(2.0d);
635            double[] result_mapMultiply = {2d, 4d, 6d};
636            assertClose("compare vectors" ,result_mapMultiply,v_mapMultiply.getData(),normTolerance);
637    
638            //octave =  v1 .* 2.0
639            RealVector v_mapMultiplyToSelf = v1.copy();
640            v_mapMultiplyToSelf.mapMultiplyToSelf(2.0d);
641            double[] result_mapMultiplyToSelf = {2d, 4d, 6d};
642            assertClose("compare vectors" ,result_mapMultiplyToSelf,v_mapMultiplyToSelf.getData(),normTolerance);
643    
644            //octave =  v1 ./ 2.0
645            RealVector v_mapDivide = v1.mapDivide(2.0d);
646            double[] result_mapDivide = {.5d, 1d, 1.5d};
647            assertClose("compare vectors" ,result_mapDivide,v_mapDivide.getData(),normTolerance);
648    
649            //octave =  v1 ./ 2.0
650            RealVector v_mapDivideToSelf = v1.copy();
651            v_mapDivideToSelf.mapDivideToSelf(2.0d);
652            double[] result_mapDivideToSelf = {.5d, 1d, 1.5d};
653            assertClose("compare vectors" ,result_mapDivideToSelf,v_mapDivideToSelf.getData(),normTolerance);
654    
655            //octave =  v1 .^ 2.0
656            RealVector v_mapPow = v1.mapPow(2.0d);
657            double[] result_mapPow = {1d, 4d, 9d};
658            assertClose("compare vectors" ,result_mapPow,v_mapPow.getData(),normTolerance);
659    
660            //octave =  v1 .^ 2.0
661            RealVector v_mapPowToSelf = v1.copy();
662            v_mapPowToSelf.mapPowToSelf(2.0d);
663            double[] result_mapPowToSelf = {1d, 4d, 9d};
664            assertClose("compare vectors" ,result_mapPowToSelf,v_mapPowToSelf.getData(),normTolerance);
665    
666            //octave =  exp(v1)
667            RealVector v_mapExp = v1.mapExp();
668            double[] result_mapExp = {2.718281828459045e+00d,7.389056098930650e+00d, 2.008553692318767e+01d};
669            assertClose("compare vectors" ,result_mapExp,v_mapExp.getData(),normTolerance);
670    
671            //octave =  exp(v1)
672            RealVector v_mapExpToSelf = v1.copy();
673            v_mapExpToSelf.mapExpToSelf();
674            double[] result_mapExpToSelf = {2.718281828459045e+00d,7.389056098930650e+00d, 2.008553692318767e+01d};
675            assertClose("compare vectors" ,result_mapExpToSelf,v_mapExpToSelf.getData(),normTolerance);
676    
677    
678            //octave =  ???
679            RealVector v_mapExpm1 = v1.mapExpm1();
680            double[] result_mapExpm1 = {1.718281828459045d,6.38905609893065d, 19.085536923187668d};
681            assertClose("compare vectors" ,result_mapExpm1,v_mapExpm1.getData(),normTolerance);
682    
683            //octave =  ???
684            RealVector v_mapExpm1ToSelf = v1.copy();
685            v_mapExpm1ToSelf.mapExpm1ToSelf();
686            double[] result_mapExpm1ToSelf = {1.718281828459045d,6.38905609893065d, 19.085536923187668d};
687            assertClose("compare vectors" ,result_mapExpm1ToSelf,v_mapExpm1ToSelf.getData(),normTolerance);
688    
689            //octave =  log(v1)
690            RealVector v_mapLog = v1.mapLog();
691            double[] result_mapLog = {0d,6.931471805599453e-01d, 1.098612288668110e+00d};
692            assertClose("compare vectors" ,result_mapLog,v_mapLog.getData(),normTolerance);
693    
694            //octave =  log(v1)
695            RealVector v_mapLogToSelf = v1.copy();
696            v_mapLogToSelf.mapLogToSelf();
697            double[] result_mapLogToSelf = {0d,6.931471805599453e-01d, 1.098612288668110e+00d};
698            assertClose("compare vectors" ,result_mapLogToSelf,v_mapLogToSelf.getData(),normTolerance);
699    
700            //octave =  log10(v1)
701            RealVector v_mapLog10 = v1.mapLog10();
702            double[] result_mapLog10 = {0d,3.010299956639812e-01d, 4.771212547196624e-01d};
703            assertClose("compare vectors" ,result_mapLog10,v_mapLog10.getData(),normTolerance);
704    
705            //octave =  log(v1)
706            RealVector v_mapLog10ToSelf = v1.copy();
707            v_mapLog10ToSelf.mapLog10ToSelf();
708            double[] result_mapLog10ToSelf = {0d,3.010299956639812e-01d, 4.771212547196624e-01d};
709            assertClose("compare vectors" ,result_mapLog10ToSelf,v_mapLog10ToSelf.getData(),normTolerance);
710    
711            //octave =  ???
712            RealVector v_mapLog1p = v1.mapLog1p();
713            double[] result_mapLog1p = {0.6931471805599453d,1.0986122886681096d,1.3862943611198906d};
714            assertClose("compare vectors" ,result_mapLog1p,v_mapLog1p.getData(),normTolerance);
715    
716            //octave =  ???
717            RealVector v_mapLog1pToSelf = v1.copy();
718            v_mapLog1pToSelf.mapLog1pToSelf();
719            double[] result_mapLog1pToSelf = {0.6931471805599453d,1.0986122886681096d,1.3862943611198906d};
720            assertClose("compare vectors" ,result_mapLog1pToSelf,v_mapLog1pToSelf.getData(),normTolerance);
721    
722            //octave =  cosh(v1)
723            RealVector v_mapCosh = v1.mapCosh();
724            double[] result_mapCosh = {1.543080634815244e+00d,3.762195691083631e+00d, 1.006766199577777e+01d};
725            assertClose("compare vectors" ,result_mapCosh,v_mapCosh.getData(),normTolerance);
726    
727            //octave =  cosh(v1)
728            RealVector v_mapCoshToSelf = v1.copy();
729            v_mapCoshToSelf.mapCoshToSelf();
730            double[] result_mapCoshToSelf = {1.543080634815244e+00d,3.762195691083631e+00d, 1.006766199577777e+01d};
731            assertClose("compare vectors" ,result_mapCoshToSelf,v_mapCoshToSelf.getData(),normTolerance);
732    
733            //octave =  sinh(v1)
734            RealVector v_mapSinh = v1.mapSinh();
735            double[] result_mapSinh = {1.175201193643801e+00d,3.626860407847019e+00d, 1.001787492740990e+01d};
736            assertClose("compare vectors" ,result_mapSinh,v_mapSinh.getData(),normTolerance);
737    
738            //octave =  sinh(v1)
739            RealVector v_mapSinhToSelf = v1.copy();
740            v_mapSinhToSelf.mapSinhToSelf();
741            double[] result_mapSinhToSelf = {1.175201193643801e+00d,3.626860407847019e+00d, 1.001787492740990e+01d};
742            assertClose("compare vectors" ,result_mapSinhToSelf,v_mapSinhToSelf.getData(),normTolerance);
743    
744            //octave =  tanh(v1)
745            RealVector v_mapTanh = v1.mapTanh();
746            double[] result_mapTanh = {7.615941559557649e-01d,9.640275800758169e-01d,9.950547536867305e-01d};
747            assertClose("compare vectors" ,result_mapTanh,v_mapTanh.getData(),normTolerance);
748    
749            //octave =  tanh(v1)
750            RealVector v_mapTanhToSelf = v1.copy();
751            v_mapTanhToSelf.mapTanhToSelf();
752            double[] result_mapTanhToSelf = {7.615941559557649e-01d,9.640275800758169e-01d,9.950547536867305e-01d};
753            assertClose("compare vectors" ,result_mapTanhToSelf,v_mapTanhToSelf.getData(),normTolerance);
754    
755            //octave =  cos(v1)
756            RealVector v_mapCos = v1.mapCos();
757            double[] result_mapCos = {5.403023058681398e-01d,-4.161468365471424e-01d, -9.899924966004454e-01d};
758            assertClose("compare vectors" ,result_mapCos,v_mapCos.getData(),normTolerance);
759    
760            //octave =  cos(v1)
761            RealVector v_mapCosToSelf = v1.copy();
762            v_mapCosToSelf.mapCosToSelf();
763            double[] result_mapCosToSelf = {5.403023058681398e-01d,-4.161468365471424e-01d, -9.899924966004454e-01d};
764            assertClose("compare vectors" ,result_mapCosToSelf,v_mapCosToSelf.getData(),normTolerance);
765    
766            //octave =  sin(v1)
767            RealVector v_mapSin = v1.mapSin();
768            double[] result_mapSin = {8.414709848078965e-01d,9.092974268256817e-01d,1.411200080598672e-01d};
769            assertClose("compare vectors" ,result_mapSin,v_mapSin.getData(),normTolerance);
770    
771            //octave =  sin(v1)
772            RealVector v_mapSinToSelf = v1.copy();
773            v_mapSinToSelf.mapSinToSelf();
774            double[] result_mapSinToSelf = {8.414709848078965e-01d,9.092974268256817e-01d,1.411200080598672e-01d};
775            assertClose("compare vectors" ,result_mapSinToSelf,v_mapSinToSelf.getData(),normTolerance);
776    
777            //octave =  tan(v1)
778            RealVector v_mapTan = v1.mapTan();
779            double[] result_mapTan = {1.557407724654902e+00d,-2.185039863261519e+00d,-1.425465430742778e-01d};
780            assertClose("compare vectors" ,result_mapTan,v_mapTan.getData(),normTolerance);
781    
782            //octave =  tan(v1)
783            RealVector v_mapTanToSelf = v1.copy();
784            v_mapTanToSelf.mapTanToSelf();
785            double[] result_mapTanToSelf = {1.557407724654902e+00d,-2.185039863261519e+00d,-1.425465430742778e-01d};
786            assertClose("compare vectors" ,result_mapTanToSelf,v_mapTanToSelf.getData(),normTolerance);
787    
788            double[] vat_a = {0d, 0.5d, 1.0d};
789            OpenMapRealVector vat = new OpenMapRealVector(vat_a);
790    
791            //octave =  acos(vat)
792            RealVector v_mapAcos = vat.mapAcos();
793            double[] result_mapAcos = {1.570796326794897e+00d,1.047197551196598e+00d, 0.0d};
794            assertClose("compare vectors" ,result_mapAcos,v_mapAcos.getData(),normTolerance);
795    
796            //octave =  acos(vat)
797            RealVector v_mapAcosToSelf = vat.copy();
798            v_mapAcosToSelf.mapAcosToSelf();
799            double[] result_mapAcosToSelf = {1.570796326794897e+00d,1.047197551196598e+00d, 0.0d};
800            assertClose("compare vectors" ,result_mapAcosToSelf,v_mapAcosToSelf.getData(),normTolerance);
801    
802            //octave =  asin(vat)
803            RealVector v_mapAsin = vat.mapAsin();
804            double[] result_mapAsin = {0.0d,5.235987755982989e-01d,1.570796326794897e+00d};
805            assertClose("compare vectors" ,result_mapAsin,v_mapAsin.getData(),normTolerance);
806    
807            //octave =  asin(vat)
808            RealVector v_mapAsinToSelf = vat.copy();
809            v_mapAsinToSelf.mapAsinToSelf();        
810            double[] result_mapAsinToSelf = {0.0d,5.235987755982989e-01d,1.570796326794897e+00d};
811            assertClose("compare vectors" ,result_mapAsinToSelf,v_mapAsinToSelf.getData(),normTolerance);
812    
813            //octave =  atan(vat)
814            RealVector v_mapAtan = vat.mapAtan();
815            double[] result_mapAtan = {0.0d,4.636476090008061e-01d,7.853981633974483e-01d};
816            assertClose("compare vectors" ,result_mapAtan,v_mapAtan.getData(),normTolerance);
817    
818            //octave =  atan(vat)
819            RealVector v_mapAtanToSelf = vat.copy();
820            v_mapAtanToSelf.mapAtanToSelf();
821            double[] result_mapAtanToSelf = {0.0d,4.636476090008061e-01d,7.853981633974483e-01d};
822            assertClose("compare vectors" ,result_mapAtanToSelf,v_mapAtanToSelf.getData(),normTolerance);
823    
824            //octave =  v1 .^-1
825            RealVector v_mapInv = v1.mapInv();
826            double[] result_mapInv = {1d,0.5d,3.333333333333333e-01d};
827            assertClose("compare vectors" ,result_mapInv,v_mapInv.getData(),normTolerance);
828    
829            //octave =  v1 .^-1
830            RealVector v_mapInvToSelf = v1.copy();
831            v_mapInvToSelf.mapInvToSelf();
832            double[] result_mapInvToSelf = {1d,0.5d,3.333333333333333e-01d};
833            assertClose("compare vectors" ,result_mapInvToSelf,v_mapInvToSelf.getData(),normTolerance);
834    
835            double[] abs_a = {-1.0d, 0.0d, 1.0d};
836            OpenMapRealVector abs_v = new OpenMapRealVector(abs_a);
837    
838            //octave =  abs(abs_v)
839            RealVector v_mapAbs = abs_v.mapAbs();
840            double[] result_mapAbs = {1d,0d,1d};
841            assertClose("compare vectors" ,result_mapAbs,v_mapAbs.getData(),normTolerance);
842    
843            //octave = abs(abs_v)
844            RealVector v_mapAbsToSelf = abs_v.copy();
845            v_mapAbsToSelf.mapAbsToSelf();
846            double[] result_mapAbsToSelf = {1d,0d,1d};
847            assertClose("compare vectors" ,result_mapAbsToSelf,v_mapAbsToSelf.getData(),normTolerance);
848    
849            //octave =   sqrt(v1)
850            RealVector v_mapSqrt = v1.mapSqrt();
851            double[] result_mapSqrt = {1d,1.414213562373095e+00d,1.732050807568877e+00d};
852            assertClose("compare vectors" ,result_mapSqrt,v_mapSqrt.getData(),normTolerance);
853    
854            //octave =  sqrt(v1)
855            RealVector v_mapSqrtToSelf = v1.copy();
856            v_mapSqrtToSelf.mapSqrtToSelf();
857            double[] result_mapSqrtToSelf = {1d,1.414213562373095e+00d,1.732050807568877e+00d};
858            assertClose("compare vectors" ,result_mapSqrtToSelf,v_mapSqrtToSelf.getData(),normTolerance);
859    
860            double[] cbrt_a = {-2.0d, 0.0d, 2.0d};
861            OpenMapRealVector cbrt_v = new OpenMapRealVector(cbrt_a);
862    
863            //octave =  ???
864            RealVector v_mapCbrt = cbrt_v.mapCbrt();
865            double[] result_mapCbrt = {-1.2599210498948732d,0d,1.2599210498948732d};
866            assertClose("compare vectors" ,result_mapCbrt,v_mapCbrt.getData(),normTolerance);
867    
868            //octave = ???
869            RealVector v_mapCbrtToSelf = cbrt_v.copy();
870            v_mapCbrtToSelf.mapCbrtToSelf();
871            double[] result_mapCbrtToSelf =  {-1.2599210498948732d,0d,1.2599210498948732d};
872            assertClose("compare vectors" ,result_mapCbrtToSelf,v_mapCbrtToSelf.getData(),normTolerance);
873    
874            double[] ceil_a = {-1.1d, 0.9d, 1.1d};
875            OpenMapRealVector ceil_v = new OpenMapRealVector(ceil_a);
876    
877            //octave =  ceil(ceil_v)
878            RealVector v_mapCeil = ceil_v.mapCeil();
879            double[] result_mapCeil = {-1d,1d,2d};
880            assertClose("compare vectors" ,result_mapCeil,v_mapCeil.getData(),normTolerance);
881    
882            //octave = ceil(ceil_v)
883            RealVector v_mapCeilToSelf = ceil_v.copy();
884            v_mapCeilToSelf.mapCeilToSelf();
885            double[] result_mapCeilToSelf =  {-1d,1d,2d};
886            assertClose("compare vectors" ,result_mapCeilToSelf,v_mapCeilToSelf.getData(),normTolerance);
887    
888            //octave =  floor(ceil_v)
889            RealVector v_mapFloor = ceil_v.mapFloor();
890            double[] result_mapFloor = {-2d,0d,1d};
891            assertClose("compare vectors" ,result_mapFloor,v_mapFloor.getData(),normTolerance);
892    
893            //octave = floor(ceil_v)
894            RealVector v_mapFloorToSelf = ceil_v.copy();
895            v_mapFloorToSelf.mapFloorToSelf();
896            double[] result_mapFloorToSelf =  {-2d,0d,1d};
897            assertClose("compare vectors" ,result_mapFloorToSelf,v_mapFloorToSelf.getData(),normTolerance);
898    
899            //octave =  ???
900            RealVector v_mapRint = ceil_v.mapRint();
901            double[] result_mapRint = {-1d,1d,1d};
902            assertClose("compare vectors" ,result_mapRint,v_mapRint.getData(),normTolerance);
903    
904            //octave = ???
905            RealVector v_mapRintToSelf = ceil_v.copy();
906            v_mapRintToSelf.mapRintToSelf();
907            double[] result_mapRintToSelf =  {-1d,1d,1d};
908            assertClose("compare vectors" ,result_mapRintToSelf,v_mapRintToSelf.getData(),normTolerance);
909    
910            //octave =  ???
911            RealVector v_mapSignum = ceil_v.mapSignum();
912            double[] result_mapSignum = {-1d,1d,1d};
913            assertClose("compare vectors" ,result_mapSignum,v_mapSignum.getData(),normTolerance);
914    
915            //octave = ???
916            RealVector v_mapSignumToSelf = ceil_v.copy();
917            v_mapSignumToSelf.mapSignumToSelf();
918            double[] result_mapSignumToSelf =  {-1d,1d,1d};
919            assertClose("compare vectors" ,result_mapSignumToSelf,v_mapSignumToSelf.getData(),normTolerance);
920    
921    
922            // Is with the used resolutions of limited value as test
923            //octave =  ???
924            RealVector v_mapUlp = ceil_v.mapUlp();
925            double[] result_mapUlp = {2.220446049250313E-16d,1.1102230246251565E-16d,2.220446049250313E-16d};
926            assertClose("compare vectors" ,result_mapUlp,v_mapUlp.getData(),normTolerance);
927    
928            //octave = ???
929            RealVector v_mapUlpToSelf = ceil_v.copy();
930            v_mapUlpToSelf.mapUlpToSelf();
931            double[] result_mapUlpToSelf = {2.220446049250313E-16d,1.1102230246251565E-16d,2.220446049250313E-16d};
932            assertClose("compare vectors" ,result_mapUlpToSelf,v_mapUlpToSelf.getData(),normTolerance);
933    
934        }
935    
936        public void testBasicFunctions() { 
937            OpenMapRealVector v1 = new OpenMapRealVector(vec1);
938            OpenMapRealVector v2 = new OpenMapRealVector(vec2);
939            OpenMapRealVector v_null = new OpenMapRealVector(vec_null);
940    
941            SparseRealVectorTestImpl v2_t = new SparseRealVectorTestImpl(vec2); 
942    
943            //octave =  sqrt(sumsq(v1))
944            double d_getNorm = v1.getNorm();
945            assertEquals("compare values  ", 3.7416573867739413,d_getNorm);
946    
947            double d_getL1Norm = v1.getL1Norm();
948            assertEquals("compare values  ",6.0, d_getL1Norm);
949    
950            double d_getLInfNorm = v1.getLInfNorm();
951            assertEquals("compare values  ",6.0, d_getLInfNorm);
952    
953            //octave =  sqrt(sumsq(v1-v2))
954            double dist = v1.getDistance(v2);
955            assertEquals("compare values  ",v1.subtract(v2).getNorm(), dist );
956    
957            //octave =  sqrt(sumsq(v1-v2))
958            double dist_2 = v1.getDistance(v2_t);
959            assertEquals("compare values  ", v1.subtract(v2).getNorm(),dist_2 );
960    
961            //octave =  ???
962            double d_getL1Distance = v1. getL1Distance(v2);
963            assertEquals("compare values  ",9d, d_getL1Distance );
964    
965            double d_getL1Distance_2 = v1. getL1Distance(v2_t);
966            assertEquals("compare values  ",9d, d_getL1Distance_2 );
967    
968            //octave =  ???
969            double d_getLInfDistance = v1. getLInfDistance(v2);
970            assertEquals("compare values  ",3d, d_getLInfDistance );
971    
972            double d_getLInfDistance_2 = v1. getLInfDistance(v2_t);
973            assertEquals("compare values  ",3d, d_getLInfDistance_2 );
974    
975            //octave =  v1 + v2
976            OpenMapRealVector v_add = v1.add(v2);
977            double[] result_add = {5d, 7d, 9d};
978            assertClose("compare vect" ,v_add.getData(),result_add,normTolerance);
979    
980            SparseRealVectorTestImpl vt2 = new SparseRealVectorTestImpl(vec2);
981            RealVector v_add_i = v1.add(vt2);
982            double[] result_add_i = {5d, 7d, 9d};
983            assertClose("compare vect" ,v_add_i.getData(),result_add_i,normTolerance);
984    
985            //octave =  v1 - v2
986            OpenMapRealVector v_subtract = v1.subtract(v2);
987            double[] result_subtract = {-3d, -3d, -3d};
988            assertClose("compare vect" ,v_subtract.getData(),result_subtract,normTolerance);
989    
990            RealVector v_subtract_i = v1.subtract(vt2);
991            double[] result_subtract_i = {-3d, -3d, -3d};
992            assertClose("compare vect" ,v_subtract_i.getData(),result_subtract_i,normTolerance);
993    
994            // octave v1 .* v2
995            RealVector  v_ebeMultiply = v1.ebeMultiply(v2);
996            double[] result_ebeMultiply = {4d, 10d, 18d};
997            assertClose("compare vect" ,v_ebeMultiply.getData(),result_ebeMultiply,normTolerance);
998    
999            RealVector  v_ebeMultiply_2 = v1.ebeMultiply(v2_t);
1000            double[] result_ebeMultiply_2 = {4d, 10d, 18d};
1001            assertClose("compare vect" ,v_ebeMultiply_2.getData(),result_ebeMultiply_2,normTolerance);
1002    
1003            // octave v1 ./ v2
1004            RealVector  v_ebeDivide = v1.ebeDivide(v2);
1005            double[] result_ebeDivide = {0.25d, 0.4d, 0.5d};
1006            assertClose("compare vect" ,v_ebeDivide.getData(),result_ebeDivide,normTolerance);
1007    
1008            RealVector  v_ebeDivide_2 = v1.ebeDivide(v2_t);
1009            double[] result_ebeDivide_2 = {0.25d, 0.4d, 0.5d};
1010            assertClose("compare vect" ,v_ebeDivide_2.getData(),result_ebeDivide_2,normTolerance);
1011    
1012            // octave  dot(v1,v2)
1013            double dot =  v1.dotProduct(v2);
1014            assertEquals("compare val ",32d, dot);
1015    
1016            // octave  dot(v1,v2_t)
1017            double dot_2 =  v1.dotProduct(v2_t);
1018            assertEquals("compare val ",32d, dot_2);
1019    
1020            RealMatrix m_outerProduct = v1.outerProduct(v2);
1021            assertEquals("compare val ",4d, m_outerProduct.getEntry(0,0));
1022    
1023            RealMatrix m_outerProduct_2 = v1.outerProduct(v2_t);
1024            assertEquals("compare val ",4d, m_outerProduct_2.getEntry(0,0));
1025    
1026            RealVector v_unitVector = v1.unitVector();
1027            RealVector v_unitVector_2 = v1.mapDivide(v1.getNorm()); 
1028            assertClose("compare vect" ,v_unitVector.getData(),v_unitVector_2.getData(),normTolerance);
1029    
1030            try {
1031                v_null.unitVector();
1032                fail("Expecting ArithmeticException");
1033            } catch (ArithmeticException ex) {
1034                // expected behavior
1035            } catch (Exception e) {
1036                fail("wrong exception caught");
1037            }
1038    
1039            OpenMapRealVector v_unitize = (OpenMapRealVector)v1.copy();
1040            v_unitize.unitize();
1041            assertClose("compare vect" ,v_unitVector_2.getData(),v_unitize.getData(),normTolerance);
1042            try {
1043                v_null.unitize();
1044                fail("Expecting ArithmeticException");
1045            } catch (ArithmeticException ex) {
1046                // expected behavior
1047            } catch (Exception e) {
1048                fail("wrong exception caught");
1049            }
1050    
1051            RealVector v_projection = v1.projection(v2);
1052            double[] result_projection = {1.662337662337662, 2.0779220779220777, 2.493506493506493};
1053            assertClose("compare vect", v_projection.getData(), result_projection, normTolerance);
1054    
1055            RealVector v_projection_2 = v1.projection(v2_t);
1056            double[] result_projection_2 = {1.662337662337662, 2.0779220779220777, 2.493506493506493};
1057            assertClose("compare vect", v_projection_2.getData(), result_projection_2, normTolerance);
1058    
1059        }  
1060    
1061        public void testMisc() { 
1062            OpenMapRealVector v1 = new OpenMapRealVector(vec1);
1063    
1064            String out1 = v1.toString();
1065            assertTrue("some output ",  out1.length()!=0);
1066            try {
1067                v1.checkVectorDimensions(2); 
1068                fail("IllegalArgumentException expected");
1069            } catch (IllegalArgumentException ex) {
1070                // expected behavior
1071            } catch (Exception e) {
1072                fail("wrong exception caught");
1073            }     
1074    
1075    
1076        }
1077    
1078        public void testPredicates() {
1079    
1080            OpenMapRealVector v = new OpenMapRealVector(new double[] { 0, 1, 2 });
1081    
1082            assertFalse(v.isNaN());
1083            v.setEntry(1, Double.NaN);
1084            assertTrue(v.isNaN());
1085    
1086            assertFalse(v.isInfinite());
1087            v.setEntry(0, Double.POSITIVE_INFINITY);
1088            assertFalse(v.isInfinite()); // NaN has higher priority than infinity
1089            v.setEntry(1, 1);
1090            assertTrue(v.isInfinite());
1091    
1092            v.setEntry(0, 0);
1093            assertEquals(v, new OpenMapRealVector(new double[] { 0, 1, 2 }));
1094            assertNotSame(v, new OpenMapRealVector(new double[] { 0, 1, 2 + Math.ulp(2)}));
1095            assertNotSame(v, new OpenMapRealVector(new double[] { 0, 1, 2, 3 }));
1096    
1097        }
1098    
1099        public void testSerial()  {
1100            OpenMapRealVector v = new OpenMapRealVector(new double[] { 0, 1, 2 });
1101            assertEquals(v,TestUtils.serializeAndRecover(v));
1102        }
1103        
1104        /** verifies that two vectors are close (sup norm) */
1105        protected void assertClose(String msg, double[] m, double[] n,
1106                double tolerance) {
1107            if (m.length != n.length) {
1108                fail("vectors have different lengths");
1109            }
1110            for (int i = 0; i < m.length; i++) {
1111                assertEquals(msg + " " +  i + " elements differ", m[i],n[i],tolerance);
1112            }
1113        }
1114    
1115    }