Coverage Report - net.sourceforge.combean.mathprog.linalg.SparseVectorWithConstantPattern
 
Classes in this File Line Coverage Branch Coverage Complexity
SparseVectorWithConstantPattern
100%
33/33
N/A
0
SparseVectorWithConstantPattern$SparseVectorWithConstantPatternIterator
91%
10/11
100%
2/2
0
 
 1  
 /*
 2  
     This file is part of Combean.
 3  
 
 4  
     Combean is free software; you can redistribute it and/or modify
 5  
     it under the terms of the GNU General Public License as published by
 6  
     the Free Software Foundation; either version 2 of the License, or
 7  
     (at your option) any later version.
 8  
 
 9  
     Combean is distributed in the hope that it will be useful,
 10  
     but WITHOUT ANY WARRANTY; without even the implied warranty of
 11  
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 12  
     GNU General Public License for more details.
 13  
 
 14  
     You should have received a copy of the GNU General Public License
 15  
     along with Combean; if not, write to the Free Software
 16  
     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 17  
 */
 18  
 /*
 19  
  * Created on 04.08.2006
 20  
  *
 21  
  */
 22  
 package net.sourceforge.combean.mathprog.linalg;
 23  
 
 24  
 import net.sourceforge.combean.interfaces.mathprog.linalg.SparseVector;
 25  
 import net.sourceforge.combean.interfaces.mathprog.linalg.VectorIterator;
 26  
 import net.sourceforge.combean.interfaces.mathprog.linalg.VectorValue;
 27  
 import net.sourceforge.combean.interfaces.mathprog.lp.model.NoLabel;
 28  
 import net.sourceforge.combean.util.except.UnsupportedMethodException;
 29  
 
 30  
 /**
 31  
  * A vector which has a constant non-zero value only in a block with
 32  
  * indices that follow a simple arithmetic progression.
 33  
  * 
 34  
  *  A simple example is a vector with a non-zero consecutive block
 35  
  *  like v = [0, ..., 0, 2, 2, ..., 2, 0, ... 0]. A more complex
 36  
  *  example would be w =  [0, ..., 0, 3, 0, 3, 0, ..., 3, 0, ... 0],
 37  
  *  where we would have an arithmetic progression with length 2.
 38  
  * 
 39  
  * @author schickin
 40  
  *
 41  
  */
 42  
 public class SparseVectorWithConstantPattern
 43  
 implements SparseVector {
 44  
     
 45  81
     private int dim = -1;
 46  
     
 47  81
     private double val = 0.0;
 48  
     
 49  81
     private int from = -1;
 50  81
     private int to = -1;
 51  
     
 52  81
     private int incr = 1;
 53  
 
 54  
     /**
 55  
      * Construct a vector with a constant value everywhere.
 56  
      * 
 57  
      * @param dim dimension of the vector
 58  
      * @param val the constant value of all elements of the vector.
 59  
      */
 60  
     public SparseVectorWithConstantPattern(int dim, double val) {
 61  3
         super();
 62  
         
 63  3
         this.dim = dim;
 64  3
         this.val = val;
 65  
         
 66  3
         this.from = 0;
 67  3
         this.to = dim-1;
 68  
         
 69  3
         this.incr = 1;
 70  3
     }
 71  
 
 72  
     /**
 73  
      * Construct a vector with a constant value in a consecutive block.
 74  
      * 
 75  
      * @param dim dimension of the vector
 76  
      * @param val the constant value of all elements of the vector.
 77  
      * @param from the start index of the non-zero block
 78  
      * @param to the end index of the non-zero block
 79  
      */
 80  
     public SparseVectorWithConstantPattern(int dim, double val,
 81  
             int from, int to) {
 82  3
         super();
 83  
         
 84  3
         this.dim = dim;
 85  3
         this.val = val;
 86  
         
 87  3
         this.from = from;
 88  3
         this.to = to;
 89  
         
 90  3
         this.incr = 1;
 91  3
     }
 92  
 
 93  
     /**
 94  
      * Construct a vector with a constant value in a arithmetic progression.
 95  
      * 
 96  
      * @param dim dimension of the vector
 97  
      * @param val the constant value of all non-elements of the vector.
 98  
      * @param from the start index of the non-zero block
 99  
      * @param to the end index of the non-zero block
 100  
      * @param incr the increment of the arithmetic progression
 101  
      */
 102  
     public SparseVectorWithConstantPattern(int dim, double val,
 103  
             int from, int to, int incr) {
 104  75
         super();
 105  
         
 106  75
         this.dim = dim;
 107  75
         this.val = val;
 108  
         
 109  75
         this.from = from;
 110  75
         this.to = to;
 111  
         
 112  75
         this.incr = incr;
 113  75
     }
 114  
 
 115  
     /* (non-Javadoc)
 116  
      * @see net.sourceforge.combean.interfaces.mathprog.linalg.SparseVec#getDimension()
 117  
      */
 118  
     public int getDimension() {
 119  18
          return this.dim;
 120  
     }
 121  
     
 122  
     /* (non-Javadoc)
 123  
      * @see net.sourceforge.combean.interfaces.mathprog.linalg.SparseVec#getIterator()
 124  
      */
 125  
     public VectorIterator<NoLabel> iterator() {
 126  81
          return new SparseVectorWithConstantPatternIterator();
 127  
     }
 128  
 
 129  
     /* (non-Javadoc)
 130  
      * @see net.sourceforge.combean.interfaces.mathprog.linalg.SparseVec#getNumIterations()
 131  
      */
 132  
     public int getNumIterations() {
 133  81
         return (this.to - this.from) / this.incr + 1;
 134  
     }
 135  
 
 136  243
     private class SparseVectorWithConstantPatternIterator
 137  
     implements VectorIterator<NoLabel> {
 138  
 
 139  81
         private int currPos = -1;
 140  
 
 141  
         /**
 142  
          * @param vec
 143  
          */
 144  81
         public SparseVectorWithConstantPatternIterator() {
 145  81
             super();
 146  81
             this.currPos = SparseVectorWithConstantPattern.this.getFrom();
 147  81
         }
 148  
 
 149  
         /* (non-Javadoc)
 150  
          * @see net.sourceforge.combean.interfaces.mathprog.linalg.VectorIterator#hasNext()
 151  
          */
 152  
         public boolean hasNext() {
 153  324
             return this.currPos <= SparseVectorWithConstantPattern.this.getTo();
 154  
         }
 155  
 
 156  
         /* (non-Javadoc)
 157  
          * @see net.sourceforge.combean.interfaces.mathprog.linalg.VectorIterator#next()
 158  
          */
 159  
         public VectorValue<NoLabel> next() {
 160  243
             DoubleVectorValue<NoLabel> result =
 161  
                 new DoubleVectorValue<NoLabel>(this.currPos,
 162  
                         SparseVectorWithConstantPattern.this.getVal());
 163  243
             this.currPos += SparseVectorWithConstantPattern.this.getIncrement();
 164  
             
 165  243
             return result;
 166  
         }
 167  
 
 168  
         /* (non-Javadoc)
 169  
          * @see java.util.Iterator#remove()
 170  
          */
 171  
         public void remove() {
 172  0
             throw new UnsupportedMethodException();
 173  
         }
 174  
     }
 175  
 
 176  
     /**
 177  
      * @return Returns the from.
 178  
      */
 179  
     public final int getFrom() {
 180  81
         return this.from;
 181  
     }
 182  
 
 183  
     /**
 184  
      * @return Returns the to.
 185  
      */
 186  
     public final int getTo() {
 187  324
         return this.to;
 188  
     }
 189  
 
 190  
     /**
 191  
      * @return Returns the val.
 192  
      */
 193  
     public final double getVal() {
 194  243
         return this.val;
 195  
     }
 196  
 
 197  
     /**
 198  
      * @return Returns the increment.
 199  
      */
 200  
     public final int getIncrement() {
 201  243
         return this.incr;
 202  
     }
 203  
 }