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.math.linear;
18  
19  
20  import org.apache.commons.math.fraction.Fraction;
21  import org.apache.commons.math.fraction.FractionConversionException;
22  import org.apache.commons.math.fraction.FractionField;
23  
24  
25  import junit.framework.TestCase;
26  
27  /**
28   * Test cases for the {@link SparseFieldVector} class.
29   *
30   * @version $Revision: 799857 $ $Date: 2009-08-01 09:07:12 -0400 (Sat, 01 Aug 2009) $
31   */
32  public class SparseFieldVectorTest extends TestCase {
33  
34      // 
35      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)}};
36      protected Fraction[] vec1 = {new Fraction(1), new Fraction(2), new Fraction(3)};
37      protected Fraction[] vec2 = {new Fraction(4), new Fraction(5), new Fraction(6)};
38      protected Fraction[] vec3 = {new Fraction(7), new Fraction(8), new Fraction(9)};
39      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)};
40      protected Fraction[] vec_null = {new Fraction(0), new Fraction(0), new Fraction(0)};
41      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)};
42      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)}};
43  
44      // tolerances
45      protected double entryTolerance = 10E-16;
46      protected double normTolerance = 10E-14;
47  
48      protected FractionField field = FractionField.getInstance();
49  
50      public void testMapFunctions() throws FractionConversionException { 
51          SparseFieldVector<Fraction> v1 = new SparseFieldVector<Fraction>(field,vec1);
52  
53          //octave =  v1 .+ 2.0
54          FieldVector<Fraction> v_mapAdd = v1.mapAdd(new Fraction(2));
55          Fraction[] result_mapAdd = {new Fraction(3), new Fraction(4), new Fraction(5)};
56          assertEquals("compare vectors" ,result_mapAdd,v_mapAdd.getData());
57  
58          //octave =  v1 .+ 2.0
59          FieldVector<Fraction> v_mapAddToSelf = v1.copy();
60          v_mapAddToSelf.mapAddToSelf(new Fraction(2));
61          Fraction[] result_mapAddToSelf = {new Fraction(3), new Fraction(4), new Fraction(5)};
62          assertEquals("compare vectors" ,result_mapAddToSelf,v_mapAddToSelf.getData());
63  
64          //octave =  v1 .- 2.0
65          FieldVector<Fraction> v_mapSubtract = v1.mapSubtract(new Fraction(2));
66          Fraction[] result_mapSubtract = {new Fraction(-1), new Fraction(0), new Fraction(1)};
67          assertEquals("compare vectors" ,result_mapSubtract,v_mapSubtract.getData());
68  
69          //octave =  v1 .- 2.0
70          FieldVector<Fraction> v_mapSubtractToSelf = v1.copy();
71          v_mapSubtractToSelf.mapSubtractToSelf(new Fraction(2));
72          Fraction[] result_mapSubtractToSelf = {new Fraction(-1), new Fraction(0), new Fraction(1)};
73          assertEquals("compare vectors" ,result_mapSubtractToSelf,v_mapSubtractToSelf.getData());
74  
75          //octave =  v1 .* 2.0
76          FieldVector<Fraction> v_mapMultiply = v1.mapMultiply(new Fraction(2));
77          Fraction[] result_mapMultiply = {new Fraction(2), new Fraction(4), new Fraction(6)};
78          assertEquals("compare vectors" ,result_mapMultiply,v_mapMultiply.getData());
79  
80          //octave =  v1 .* 2.0
81          FieldVector<Fraction> v_mapMultiplyToSelf = v1.copy();
82          v_mapMultiplyToSelf.mapMultiplyToSelf(new Fraction(2));
83          Fraction[] result_mapMultiplyToSelf = {new Fraction(2), new Fraction(4), new Fraction(6)};
84          assertEquals("compare vectors" ,result_mapMultiplyToSelf,v_mapMultiplyToSelf.getData());
85  
86          //octave =  v1 ./ 2.0
87          FieldVector<Fraction> v_mapDivide = v1.mapDivide(new Fraction(2));
88          Fraction[] result_mapDivide = {new Fraction(.5d), new Fraction(1), new Fraction(1.5d)};
89          assertEquals("compare vectors" ,result_mapDivide,v_mapDivide.getData());
90  
91          //octave =  v1 ./ 2.0
92          FieldVector<Fraction> v_mapDivideToSelf = v1.copy();
93          v_mapDivideToSelf.mapDivideToSelf(new Fraction(2));
94          Fraction[] result_mapDivideToSelf = {new Fraction(.5d), new Fraction(1), new Fraction(1.5d)};
95          assertEquals("compare vectors" ,result_mapDivideToSelf,v_mapDivideToSelf.getData());
96  
97          //octave =  v1 .^-1
98          FieldVector<Fraction> v_mapInv = v1.mapInv();
99          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 }