All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.openimaj.feature.SparseIntFV Maven / Gradle / Ivy

/*
	AUTOMATICALLY GENERATED BY jTemp FROM
	/Users/jsh2/Work/openimaj/target/checkout/core/core-feature/src/main/jtemp/org/openimaj/feature/Sparse#T#FV.jtemp
*/
/**
 * Copyright (c) 2011, The University of Southampton and the individual contributors.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 *   * 	Redistributions of source code must retain the above copyright notice,
 * 	this list of conditions and the following disclaimer.
 *
 *   *	Redistributions in binary form must reproduce the above copyright notice,
 * 	this list of conditions and the following disclaimer in the documentation
 * 	and/or other materials provided with the distribution.
 *
 *   *	Neither the name of the University of Southampton nor the names of its
 * 	contributors may be used to endorse or promote products derived from this
 * 	software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package org.openimaj.feature;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Scanner;

import org.openimaj.util.array.SparseBinSearchIntArray;
import org.openimaj.util.array.SparseIntArray;
import org.openimaj.util.array.SparseIntArray.Entry;
import org.openimaj.util.concatenate.Concatenatable;

/**
 * A sparse one-dimensional feature vector of int-valued elements.
 * 
 * @author Jonathon Hare ([email protected])
 *
 */
public class SparseIntFV implements FeatureVector, Concatenatable {
	private static final long serialVersionUID = 1L;

	/**
	 * The underlying data backing the feature vector
	 */
	public SparseIntArray values;
	
	/**
	 * Construct an empty FV with zero length
	 */
	protected SparseIntFV() {
	}

	/**
	 * Construct empty FV with given number of bins
	 * @param nbins the number of bins in each dimension
	 */
	public SparseIntFV(int nbins) {
		values = new SparseBinSearchIntArray(nbins);
	}
	
	/**
	 * Construct from sparse array 
	 * @param values the array of values
	 */
	public SparseIntFV(SparseIntArray values) {
		this.values = values;
	}

	/**
	 * Construct from native array
	 * @param values the array of values
	 */
	public SparseIntFV(int[] values) {
		this.values = new SparseBinSearchIntArray(values);
	}
	
	/* (non-Javadoc)
	 * @see org.openimaj.io.ReadableASCII#readASCII(java.util.Scanner)
	 */
	@Override
	public void readASCII(Scanner in) throws IOException {
		values = new SparseBinSearchIntArray(0);
		values.readASCII(in);
	}

	/* (non-Javadoc)
	 * @see org.openimaj.io.ReadableASCII#asciiHeader()
	 */
	@Override
	public String asciiHeader() {
		return "SIntFV";
	}

	/* (non-Javadoc)
	 * @see org.openimaj.io.ReadableBinary#readBinary(java.io.DataInput)
	 */
	@Override
	public void readBinary(DataInput in) throws IOException {
		values = new SparseBinSearchIntArray(0);
		values.readBinary(in);
	}

	/* (non-Javadoc)
	 * @see org.openimaj.io.ReadableBinary#binaryHeader()
	 */
	@Override
	public byte[] binaryHeader() {
		return "SIntFV".getBytes();
	}

	/* (non-Javadoc)
	 * @see org.openimaj.io.WriteableASCII#writeASCII(java.io.PrintWriter)
	 */
	@Override
	public void writeASCII(PrintWriter out) throws IOException {
		values.writeASCII(out);
	}

	/* (non-Javadoc)
	 * @see org.openimaj.io.WriteableBinary#writeBinary(java.io.DataOutput)
	 */
	@Override
	public void writeBinary(DataOutput out) throws IOException {
		values.writeBinary(out);
	}

	/* (non-Javadoc)
	 * @see org.openimaj.feature.FeatureVector#getVector()
	 */
	@Override
	public SparseIntArray getVector() {
		return values;
	}

	/* (non-Javadoc)
	 * @see org.openimaj.feature.FeatureVector#length()
	 */
	@Override
	public int length() {
		return values.length;
	}

	/* (non-Javadoc)
	 * @see org.openimaj.feature.FeatureVector#normaliseFV(double[], double[])
	 */
	@Override
	public DoubleFV normaliseFV(double [] min, double [] max) {
		double [] dvals = asDoubleVector();

		for (int i=0; i1) dvals[i] = 1;
		}
		
		return new DoubleFV(dvals);
	}
	
	/* (non-Javadoc)
	 * @see org.openimaj.feature.FeatureVector#normaliseFV(double, double)
	 */
	@Override
	public DoubleFV normaliseFV(double min, double max) {
		double [] dvals = asDoubleVector();

		for (int i=0; i1) dvals[i] = 1;
		}
		
		return new DoubleFV(dvals);
	}
	
	/* (non-Javadoc)
	 * @see org.openimaj.feature.FeatureVector#normaliseFV()
	 */
	@Override
	public DoubleFV normaliseFV() {
		double [] dvals = asDoubleVector();
		double sum = 0;

		for (int i=0; i ins) {
		SparseIntArray [] insValues = new SparseIntArray[ins.size()];
		
		for (int i=0; i




© 2015 - 2025 Weber Informatics LLC | Privacy Policy