/** * Gets the value at index from the array. * * @param index the index of the value to get. * @return the value at index in the array as as String. * @throws Exception if index is out of bounds. */ public String value(int index) throws Exception { return actualValue(index); }
/** * Gets the value at index from the array * * @param index the index of the value to get. * @return the value at index in the array as as String. * @throws Exception if index is out of bounds. */ protected String actualValue(int index) throws Exception { checkInRange(index); return m_values.get(index); }
/** * Returns true if the array contains this real value. * * @param value the value to check for * @return true if the array contains this real value */ public boolean contains(float value) { return contains(new Float(value).toString()); }
/** * Computes the dot product between this vector instance and the * supplied array of values * * @param other an array of values to perform the dot product with * @return the dot product of this vector instance with the argument * @throws Exception if something goes wrong */ public double dotProduct(double[] other) throws Exception { // other is a non-sparse array of values double result = 0; // do a fast dot product int n1 = m_values.numValues(); for (int i = 0; i < n1; i++) { int ind1 = m_values.index(i); result += m_values.valueSparseDouble(i) * other[ind1]; } return result; } }
/** * Static factory method for creating non-sparse or sparse array types as * needed. * * @param arrayE the XML element encapsulating the array * @return an appropriate Array type * @throws Exception if there is a problem when constructing the array */ public static Array create(Element arrayE) throws Exception { if (!isArray(arrayE)) { throw new Exception( "[Array] the supplied element does not contain an array!"); } if (isSparseArray(arrayE)) { return new SparseArray(arrayE); } return new Array(arrayE); }
Node child = children.item(i); if (child.getNodeType() == Node.ELEMENT_NODE) { if (Array.isArray((Element) child)) { m_set = Array.create((Element) child); break; if (m_set.getType() == Array.ArrayType.STRING && !m_isNominal) { throw new Exception("[SimpleSetPredicate] referenced field " + totalStructure.attribute(m_fieldIndex).name() + " is numeric but array type is string!"); } else if (m_set.getType() != Array.ArrayType.STRING && m_isNominal) { throw new Exception("[SimpleSetPredicate] referenced field " + totalStructure.attribute(m_fieldIndex).name()
/** * Subtract the values in the supplied array from this vector instance * * @param other an array of values * @return a new VectorInstance containing the result of the subtraction * @throws Exception if something goes wrong */ public VectorInstance subtract(double[] other) throws Exception { // other is a non-sparse array of values ArrayList<Object> diffVals = new ArrayList<Object>(); for (int i = 0; i < other.length; i++) { double x = m_values.valueDouble(i); double y = other[i]; // System.err.println("x: " + x + " y: " + y); double result = x - y; diffVals.add(new Double(result)); } Array newArray = Array.create(diffVals, null); return new VectorInstance(newArray, m_vectorFields); }
@Override Predicate.Eval evaluate(double[] input, int fieldIndex, Array set, Attribute nominalLookup) { if (set.getType() == Array.ArrayType.STRING) { String value = ""; if (!Utils.isMissingValue(input[fieldIndex])) { value = nominalLookup.value((int) input[fieldIndex]); } return Predicate.booleanToEval( Utils.isMissingValue(input[fieldIndex]), set.contains(value)); } else if (set.getType() == Array.ArrayType.NUM || set.getType() == Array.ArrayType.REAL) { return Predicate.booleanToEval( Utils.isMissingValue(input[fieldIndex]), set.contains(input[fieldIndex])); } return Predicate.booleanToEval( Utils.isMissingValue(input[fieldIndex]), set.contains((int) input[fieldIndex])); } },
/** * Construct an array from an XML node * * @param arrayE the Element containing the XML * @throws Exception if something goes wrong */ protected Array(Element arrayE) throws Exception { initialize(arrayE); }
/** * Utility method to check if an XML element is an array. * * @param arrayE the XML element to check * @return returns true if the XML element is an array */ public static boolean isArray(Element arrayE) { String name = arrayE.getTagName(); if (name.equals("Array") || name.equals("NUM-ARRAY") || name.equals("INT-ARRAY") || name.equals("REAL-ARRAY") || name.equals("STRING-ARRAY") || isSparseArray(arrayE)) { return true; } return false; }
m_values = Array.create(theArray);
public static Array create(List<Object> values, List<Integer> indices) throws Exception { ArrayType type = null; Object first = values.get(0); if ((first instanceof Double) || (first instanceof Float)) { type = ArrayType.REAL; } else if ((first instanceof Integer) || (first instanceof Long)) { type = ArrayType.INT; } else if ((first instanceof String)) { type = ArrayType.STRING; } else { throw new Exception("[Array] unsupport type!"); } if (indices != null) { // array is sparse if (indices.size() != values.size()) { throw new Exception("[Array] num values is not equal to num indices!!"); } if (type == ArrayType.REAL) { type = ArrayType.REAL_SPARSE; } else if (type == ArrayType.INT) { type = ArrayType.INT_SPARSE; } else { throw new Exception( "[Array] sparse arrays can only be integer, long, float or double!"); } return new SparseArray(type, values, indices); } return new Array(type, values); }
/** * Computes the dot product between this vector instance and the * supplied array of values * * @param other an array of values to perform the dot product with * @return the dot product of this vector instance with the argument * @throws Exception if something goes wrong */ public double dotProduct(double[] other) throws Exception { // other is a non-sparse array of values double result = 0; // do a fast dot product int n1 = m_values.numValues(); for (int i = 0; i < n1; i++) { int ind1 = m_values.index(i); result += m_values.valueSparseDouble(i) * other[ind1]; } return result; } }
/** * Static factory method for creating non-sparse or sparse array types as * needed. * * @param arrayE the XML element encapsulating the array * @return an appropriate Array type * @throws Exception if there is a problem when constructing the array */ public static Array create(Element arrayE) throws Exception { if (!isArray(arrayE)) { throw new Exception( "[Array] the supplied element does not contain an array!"); } if (isSparseArray(arrayE)) { return new SparseArray(arrayE); } return new Array(arrayE); }
Node child = children.item(i); if (child.getNodeType() == Node.ELEMENT_NODE) { if (Array.isArray((Element) child)) { m_set = Array.create((Element) child); break; if (m_set.getType() == Array.ArrayType.STRING && !m_isNominal) { throw new Exception("[SimpleSetPredicate] referenced field " + totalStructure.attribute(m_fieldIndex).name() + " is numeric but array type is string!"); } else if (m_set.getType() != Array.ArrayType.STRING && m_isNominal) { throw new Exception("[SimpleSetPredicate] referenced field " + totalStructure.attribute(m_fieldIndex).name()
/** * Subtract the values in the supplied array from this vector instance * * @param other an array of values * @return a new VectorInstance containing the result of the subtraction * @throws Exception if something goes wrong */ public VectorInstance subtract(double[] other) throws Exception { // other is a non-sparse array of values ArrayList<Object> diffVals = new ArrayList<Object>(); for (int i = 0; i < other.length; i++) { double x = m_values.valueDouble(i); double y = other[i]; // System.err.println("x: " + x + " y: " + y); double result = x - y; diffVals.add(new Double(result)); } Array newArray = Array.create(diffVals, null); return new VectorInstance(newArray, m_vectorFields); }
@Override Predicate.Eval evaluate(double[] input, int fieldIndex, Array set, Attribute nominalLookup) { if (set.getType() == Array.ArrayType.STRING) { String value = ""; if (!Utils.isMissingValue(input[fieldIndex])) { value = nominalLookup.value((int) input[fieldIndex]); } return Predicate.booleanToEval( Utils.isMissingValue(input[fieldIndex]), set.contains(value)); } else if (set.getType() == Array.ArrayType.NUM || set.getType() == Array.ArrayType.REAL) { return Predicate.booleanToEval( Utils.isMissingValue(input[fieldIndex]), set.contains(input[fieldIndex])); } return Predicate.booleanToEval( Utils.isMissingValue(input[fieldIndex]), set.contains((int) input[fieldIndex])); } },
/** * Construct an array from an XML node * * @param arrayE the Element containing the XML * @throws Exception if something goes wrong */ protected Array(Element arrayE) throws Exception { initialize(arrayE); }
/** * Utility method to check if an XML element is an array. * * @param arrayE the XML element to check * @return returns true if the XML element is an array */ public static boolean isArray(Element arrayE) { String name = arrayE.getTagName(); if (name.equals("Array") || name.equals("NUM-ARRAY") || name.equals("INT-ARRAY") || name.equals("REAL-ARRAY") || name.equals("STRING-ARRAY") || isSparseArray(arrayE)) { return true; } return false; }
m_values = Array.create(theArray);