001/*
002        AUTOMATICALLY GENERATED BY jTemp FROM
003        /Users/jsh2/Work/openimaj/target/checkout/core/core-feature/src/main/jtemp/org/openimaj/feature/Sparse#T#FV.jtemp
004*/
005/**
006 * Copyright (c) 2011, The University of Southampton and the individual contributors.
007 * All rights reserved.
008 *
009 * Redistribution and use in source and binary forms, with or without modification,
010 * are permitted provided that the following conditions are met:
011 *
012 *   *  Redistributions of source code must retain the above copyright notice,
013 *      this list of conditions and the following disclaimer.
014 *
015 *   *  Redistributions in binary form must reproduce the above copyright notice,
016 *      this list of conditions and the following disclaimer in the documentation
017 *      and/or other materials provided with the distribution.
018 *
019 *   *  Neither the name of the University of Southampton nor the names of its
020 *      contributors may be used to endorse or promote products derived from this
021 *      software without specific prior written permission.
022 *
023 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
024 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
025 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
026 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
027 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
028 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
029 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
030 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
031 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
032 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
033 */
034package org.openimaj.feature;
035
036import java.io.DataInput;
037import java.io.DataOutput;
038import java.io.IOException;
039import java.io.PrintWriter;
040import java.util.List;
041import java.util.Scanner;
042
043import org.openimaj.util.array.SparseBinSearchShortArray;
044import org.openimaj.util.array.SparseShortArray;
045import org.openimaj.util.array.SparseShortArray.Entry;
046import org.openimaj.util.concatenate.Concatenatable;
047
048/**
049 * A sparse one-dimensional feature vector of short-valued elements.
050 * 
051 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
052 *
053 */
054public class SparseShortFV implements FeatureVector, Concatenatable<SparseShortFV, SparseShortFV> {
055        private static final long serialVersionUID = 1L;
056
057        /**
058         * The underlying data backing the feature vector
059         */
060        public SparseShortArray values;
061        
062        /**
063         * Construct an empty FV with zero length
064         */
065        protected SparseShortFV() {
066        }
067
068        /**
069         * Construct empty FV with given number of bins
070         * @param nbins the number of bins in each dimension
071         */
072        public SparseShortFV(int nbins) {
073                values = new SparseBinSearchShortArray(nbins);
074        }
075        
076        /**
077         * Construct from sparse array 
078         * @param values the array of values
079         */
080        public SparseShortFV(SparseShortArray values) {
081                this.values = values;
082        }
083
084        /**
085         * Construct from native array
086         * @param values the array of values
087         */
088        public SparseShortFV(short[] values) {
089                this.values = new SparseBinSearchShortArray(values);
090        }
091        
092        /* (non-Javadoc)
093         * @see org.openimaj.io.ReadableASCII#readASCII(java.util.Scanner)
094         */
095        @Override
096        public void readASCII(Scanner in) throws IOException {
097                values = new SparseBinSearchShortArray(0);
098                values.readASCII(in);
099        }
100
101        /* (non-Javadoc)
102         * @see org.openimaj.io.ReadableASCII#asciiHeader()
103         */
104        @Override
105        public String asciiHeader() {
106                return "SShortFV";
107        }
108
109        /* (non-Javadoc)
110         * @see org.openimaj.io.ReadableBinary#readBinary(java.io.DataInput)
111         */
112        @Override
113        public void readBinary(DataInput in) throws IOException {
114                values = new SparseBinSearchShortArray(0);
115                values.readBinary(in);
116        }
117
118        /* (non-Javadoc)
119         * @see org.openimaj.io.ReadableBinary#binaryHeader()
120         */
121        @Override
122        public byte[] binaryHeader() {
123                return "SShortFV".getBytes();
124        }
125
126        /* (non-Javadoc)
127         * @see org.openimaj.io.WriteableASCII#writeASCII(java.io.PrintWriter)
128         */
129        @Override
130        public void writeASCII(PrintWriter out) throws IOException {
131                values.writeASCII(out);
132        }
133
134        /* (non-Javadoc)
135         * @see org.openimaj.io.WriteableBinary#writeBinary(java.io.DataOutput)
136         */
137        @Override
138        public void writeBinary(DataOutput out) throws IOException {
139                values.writeBinary(out);
140        }
141
142        /* (non-Javadoc)
143         * @see org.openimaj.feature.FeatureVector#getVector()
144         */
145        @Override
146        public SparseShortArray getVector() {
147                return values;
148        }
149
150        /* (non-Javadoc)
151         * @see org.openimaj.feature.FeatureVector#length()
152         */
153        @Override
154        public int length() {
155                return values.length;
156        }
157
158        /* (non-Javadoc)
159         * @see org.openimaj.feature.FeatureVector#normaliseFV(double[], double[])
160         */
161        @Override
162        public DoubleFV normaliseFV(double [] min, double [] max) {
163                double [] dvals = asDoubleVector();
164
165                for (int i=0; i<dvals.length; i++) {
166                        dvals[i] -= min[i];
167                        dvals[i] /= (max[i]-min[i]);
168                        
169                        if (dvals[i]<0) dvals[i] = 0;
170                        if (dvals[i]>1) dvals[i] = 1;
171                }
172                
173                return new DoubleFV(dvals);
174        }
175        
176        /* (non-Javadoc)
177         * @see org.openimaj.feature.FeatureVector#normaliseFV(double, double)
178         */
179        @Override
180        public DoubleFV normaliseFV(double min, double max) {
181                double [] dvals = asDoubleVector();
182
183                for (int i=0; i<dvals.length; i++) {
184                        dvals[i] -= min;
185                        dvals[i] /= (max-min);
186                        
187                        if (dvals[i]<0) dvals[i] = 0;
188                        if (dvals[i]>1) dvals[i] = 1;
189                }
190                
191                return new DoubleFV(dvals);
192        }
193        
194        /* (non-Javadoc)
195         * @see org.openimaj.feature.FeatureVector#normaliseFV()
196         */
197        @Override
198        public DoubleFV normaliseFV() {
199                double [] dvals = asDoubleVector();
200                double sum = 0;
201
202                for (int i=0; i<dvals.length; i++)
203                        sum += dvals[i];
204
205                for (int i=0; i<dvals.length; i++)
206                        dvals[i] /= sum;
207                
208                return new DoubleFV(dvals);
209        }
210        
211        /* (non-Javadoc)
212         * @see org.openimaj.feature.FeatureVector#normaliseFV(double)
213         */
214        @Override
215        public DoubleFV normaliseFV(double p) {
216            return asDoubleFV().normaliseFV(p);
217        }
218
219        /* (non-Javadoc)
220         * @see org.openimaj.feature.FeatureVector#asDoubleFV()
221         */
222        @Override
223        public DoubleFV asDoubleFV() {
224                return new DoubleFV(asDoubleVector());
225        }
226
227        /* (non-Javadoc)
228         * @see org.openimaj.feature.FeatureVector#asDoubleVector()
229         */
230        @Override
231        public double[] asDoubleVector() {
232                double [] d = new double[values.length];
233                
234                for (Entry e : values.entries()) {
235                        d[e.index] = e.value;
236                }
237                
238                return d;
239        }
240        
241        @Override
242        public SparseShortFV concatenate(SparseShortFV... ins) {
243                SparseShortArray [] insValues = new SparseShortArray[ins.length];
244                
245                for (int i=0; i<ins.length; i++)
246                        insValues[i] = ins[i].values;
247                
248                SparseShortArray vals = values.concatenate(insValues);
249                
250                return new SparseShortFV(vals);
251        }
252        
253        @Override
254        public SparseShortFV concatenate(List<SparseShortFV> ins) {
255                SparseShortArray [] insValues = new SparseShortArray[ins.size()];
256                
257                for (int i=0; i<ins.size(); i++)
258                        insValues[i] = ins.get(i).values;
259                
260                SparseShortArray vals = values.concatenate(insValues);
261                
262                return new SparseShortFV(vals);
263        }
264
265        @Override
266        public double getAsDouble(int i) {
267                return values.get(i);
268        }
269
270        @Override
271        public void setFromDouble(int i, double v) {
272                values.set(i, (short) v);
273        }
274
275        @Override
276        public SparseShortFV newInstance() {
277                return new SparseShortFV(length());
278        }
279}