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    
020    import org.apache.commons.math.fraction.Fraction;
021    import org.apache.commons.math.fraction.FractionConversionException;
022    import org.apache.commons.math.fraction.FractionField;
023    
024    
025    import junit.framework.TestCase;
026    
027    /**
028     * Test cases for the {@link SparseFieldVector} class.
029     *
030     * @version $Revision: 799857 $ $Date: 2009-08-01 09:07:12 -0400 (Sat, 01 Aug 2009) $
031     */
032    public class SparseFieldVectorTest extends TestCase {
033    
034        // 
035        protected Fraction[][] ma1 = {{new Fraction(1), new Fraction(2), new Fraction(3)}, {new Fraction(4), new Fraction(5), new Fraction(6)}, {new Fraction(7), new Fraction(8), new Fraction(9)}};
036        protected Fraction[] vec1 = {new Fraction(1), new Fraction(2), new Fraction(3)};
037        protected Fraction[] vec2 = {new Fraction(4), new Fraction(5), new Fraction(6)};
038        protected Fraction[] vec3 = {new Fraction(7), new Fraction(8), new Fraction(9)};
039        protected Fraction[] vec4 = {new Fraction(1), new Fraction(2), new Fraction(3), new Fraction(4), new Fraction(5), new Fraction(6), new Fraction(7), new Fraction(8), new Fraction(9)};
040        protected Fraction[] vec_null = {new Fraction(0), new Fraction(0), new Fraction(0)};
041        protected Fraction[] dvec1 = {new Fraction(1), new Fraction(2), new Fraction(3), new Fraction(4), new Fraction(5), new Fraction(6), new Fraction(7), new Fraction(8),new Fraction(9)};
042        protected Fraction[][] mat1 = {{new Fraction(1), new Fraction(2), new Fraction(3)}, {new Fraction(4), new Fraction(5), new Fraction(6)},{ new Fraction(7), new Fraction(8), new Fraction(9)}};
043    
044        // tolerances
045        protected double entryTolerance = 10E-16;
046        protected double normTolerance = 10E-14;
047    
048        protected FractionField field = FractionField.getInstance();
049    
050        public void testMapFunctions() throws FractionConversionException { 
051            SparseFieldVector<Fraction> v1 = new SparseFieldVector<Fraction>(field,vec1);
052    
053            //octave =  v1 .+ 2.0
054            FieldVector<Fraction> v_mapAdd = v1.mapAdd(new Fraction(2));
055            Fraction[] result_mapAdd = {new Fraction(3), new Fraction(4), new Fraction(5)};
056            assertEquals("compare vectors" ,result_mapAdd,v_mapAdd.getData());
057    
058            //octave =  v1 .+ 2.0
059            FieldVector<Fraction> v_mapAddToSelf = v1.copy();
060            v_mapAddToSelf.mapAddToSelf(new Fraction(2));
061            Fraction[] result_mapAddToSelf = {new Fraction(3), new Fraction(4), new Fraction(5)};
062            assertEquals("compare vectors" ,result_mapAddToSelf,v_mapAddToSelf.getData());
063    
064            //octave =  v1 .- 2.0
065            FieldVector<Fraction> v_mapSubtract = v1.mapSubtract(new Fraction(2));
066            Fraction[] result_mapSubtract = {new Fraction(-1), new Fraction(0), new Fraction(1)};
067            assertEquals("compare vectors" ,result_mapSubtract,v_mapSubtract.getData());
068    
069            //octave =  v1 .- 2.0
070            FieldVector<Fraction> v_mapSubtractToSelf = v1.copy();
071            v_mapSubtractToSelf.mapSubtractToSelf(new Fraction(2));
072            Fraction[] result_mapSubtractToSelf = {new Fraction(-1), new Fraction(0), new Fraction(1)};
073            assertEquals("compare vectors" ,result_mapSubtractToSelf,v_mapSubtractToSelf.getData());
074    
075            //octave =  v1 .* 2.0
076            FieldVector<Fraction> v_mapMultiply = v1.mapMultiply(new Fraction(2));
077            Fraction[] result_mapMultiply = {new Fraction(2), new Fraction(4), new Fraction(6)};
078            assertEquals("compare vectors" ,result_mapMultiply,v_mapMultiply.getData());
079    
080            //octave =  v1 .* 2.0
081            FieldVector<Fraction> v_mapMultiplyToSelf = v1.copy();
082            v_mapMultiplyToSelf.mapMultiplyToSelf(new Fraction(2));
083            Fraction[] result_mapMultiplyToSelf = {new Fraction(2), new Fraction(4), new Fraction(6)};
084            assertEquals("compare vectors" ,result_mapMultiplyToSelf,v_mapMultiplyToSelf.getData());
085    
086            //octave =  v1 ./ 2.0
087            FieldVector<Fraction> v_mapDivide = v1.mapDivide(new Fraction(2));
088            Fraction[] result_mapDivide = {new Fraction(.5d), new Fraction(1), new Fraction(1.5d)};
089            assertEquals("compare vectors" ,result_mapDivide,v_mapDivide.getData());
090    
091            //octave =  v1 ./ 2.0
092            FieldVector<Fraction> v_mapDivideToSelf = v1.copy();
093            v_mapDivideToSelf.mapDivideToSelf(new Fraction(2));
094            Fraction[] result_mapDivideToSelf = {new Fraction(.5d), new Fraction(1), new Fraction(1.5d)};
095            assertEquals("compare vectors" ,result_mapDivideToSelf,v_mapDivideToSelf.getData());
096    
097            //octave =  v1 .^-1
098            FieldVector<Fraction> v_mapInv = v1.mapInv();
099            Fraction[] result_mapInv = {new Fraction(1),new Fraction(0.5d),new Fraction(3.333333333333333e-01d)};
100            assertEquals("compare vectors" ,result_mapInv,v_mapInv.getData());
101    
102            //octave =  v1 .^-1
103            FieldVector<Fraction> v_mapInvToSelf = v1.copy();
104            v_mapInvToSelf.mapInvToSelf();
105            Fraction[] result_mapInvToSelf = {new Fraction(1),new Fraction(0.5d),new Fraction(3.333333333333333e-01d)};
106            assertEquals("compare vectors" ,result_mapInvToSelf,v_mapInvToSelf.getData());
107    
108    
109        }
110    
111        public void testBasicFunctions() throws FractionConversionException { 
112            SparseFieldVector<Fraction> v1 = new SparseFieldVector<Fraction>(field,vec1);
113            SparseFieldVector<Fraction> v2 = new SparseFieldVector<Fraction>(field,vec2);
114    
115            SparseFieldVector<Fraction> v2_t = new SparseFieldVector<Fraction>(field,vec2); 
116    
117            //octave =  v1 + v2
118            FieldVector<Fraction> v_add = v1.add(v2);
119            Fraction[] result_add = {new Fraction(5), new Fraction(7), new Fraction(9)};
120            assertEquals("compare vect" ,v_add.getData(),result_add);
121    
122            SparseFieldVector<Fraction> vt2 = new SparseFieldVector<Fraction>(field,vec2);
123            FieldVector<Fraction> v_add_i = v1.add(vt2);
124            Fraction[] result_add_i = {new Fraction(5), new Fraction(7), new Fraction(9)};
125            assertEquals("compare vect" ,v_add_i.getData(),result_add_i);
126    
127            //octave =  v1 - v2
128            SparseFieldVector<Fraction> v_subtract = v1.subtract(v2);
129            Fraction[] result_subtract = {new Fraction(-3), new Fraction(-3), new Fraction(-3)};
130            assertClose("compare vect" ,v_subtract.getData(),result_subtract,normTolerance);
131    
132            FieldVector<Fraction> v_subtract_i = v1.subtract(vt2);
133            Fraction[] result_subtract_i = {new Fraction(-3), new Fraction(-3), new Fraction(-3)};
134            assertClose("compare vect" ,v_subtract_i.getData(),result_subtract_i,normTolerance);
135    
136            // octave v1 .* v2
137            FieldVector<Fraction>  v_ebeMultiply = v1.ebeMultiply(v2);
138            Fraction[] result_ebeMultiply = {new Fraction(4), new Fraction(10), new Fraction(18)};
139            assertClose("compare vect" ,v_ebeMultiply.getData(),result_ebeMultiply,normTolerance);
140    
141            FieldVector<Fraction>  v_ebeMultiply_2 = v1.ebeMultiply(v2_t);
142            Fraction[] result_ebeMultiply_2 = {new Fraction(4), new Fraction(10), new Fraction(18)};
143            assertClose("compare vect" ,v_ebeMultiply_2.getData(),result_ebeMultiply_2,normTolerance);
144    
145            // octave v1 ./ v2
146            FieldVector<Fraction>  v_ebeDivide = v1.ebeDivide(v2);
147            Fraction[] result_ebeDivide = {new Fraction(0.25d), new Fraction(0.4d), new Fraction(0.5d)};
148            assertClose("compare vect" ,v_ebeDivide.getData(),result_ebeDivide,normTolerance);
149    
150            FieldVector<Fraction>  v_ebeDivide_2 = v1.ebeDivide(v2_t);
151            Fraction[] result_ebeDivide_2 = {new Fraction(0.25d), new Fraction(0.4d), new Fraction(0.5d)};
152            assertClose("compare vect" ,v_ebeDivide_2.getData(),result_ebeDivide_2,normTolerance);
153    
154            // octave  dot(v1,v2)
155            Fraction dot =  v1.dotProduct(v2);
156            assertEquals("compare val ",new Fraction(32), dot);
157    
158            // octave  dot(v1,v2_t)
159            Fraction dot_2 =  v1.dotProduct(v2_t);
160            assertEquals("compare val ",new Fraction(32), dot_2);
161    
162            FieldMatrix<Fraction> m_outerProduct = v1.outerProduct(v2);
163            assertEquals("compare val ",new Fraction(4), m_outerProduct.getEntry(0,0));
164    
165            FieldMatrix<Fraction> m_outerProduct_2 = v1.outerProduct(v2_t);
166            assertEquals("compare val ",new Fraction(4), m_outerProduct_2.getEntry(0,0));
167    
168        }
169    
170    
171        public void testMisc() { 
172            SparseFieldVector<Fraction> v1 = new SparseFieldVector<Fraction>(field,vec1);
173    
174            String out1 = v1.toString();
175            assertTrue("some output ",  out1.length()!=0);
176            try {
177                v1.checkVectorDimensions(2); 
178                fail("IllegalArgumentException expected");
179            } catch (IllegalArgumentException ex) {
180                // expected behavior
181            } catch (Exception e) {
182                fail("wrong exception caught");
183            }     
184    
185    
186        }
187    
188        public void testPredicates() {
189    
190            SparseFieldVector<Fraction> v = new SparseFieldVector<Fraction>(field, new Fraction[] { new Fraction(0), new Fraction(1), new Fraction(2) });
191    
192            v.setEntry(0, field.getZero());
193            assertEquals(v, new SparseFieldVector<Fraction>(field, new Fraction[] { new Fraction(0), new Fraction(1), new Fraction(2) }));
194            assertNotSame(v, new SparseFieldVector<Fraction>(field, new Fraction[] { new Fraction(0), new Fraction(1), new Fraction(2), new Fraction(3) }));
195    
196        }
197    
198        /** verifies that two vectors are close (sup norm) */
199        protected void assertEquals(String msg, Fraction[] m, Fraction[] n) {
200            if (m.length != n.length) {
201                fail("vectors have different lengths");
202            }
203            for (int i = 0; i < m.length; i++) {
204                assertEquals(msg + " " +  i + " elements differ", m[i],n[i]);
205            }
206        }
207    
208        /** verifies that two vectors are close (sup norm) */
209        protected void assertClose(String msg, Fraction[] m, Fraction[] n, double tolerance) {
210            if (m.length != n.length) {
211                fail("vectors have different lengths");
212            }
213            for (int i = 0; i < m.length; i++) {
214                assertEquals(msg + " " +  i + " elements differ", m[i].doubleValue(),n[i].doubleValue(), tolerance);
215            }
216        }
217    
218    }