/** {@inheritDoc} */ @Override public double getLInfDistance(RealVector v) throws IllegalArgumentException { checkVectorDimensions(v.getDimension()); if (v instanceof OpenMapRealVector) { return getLInfDistance((OpenMapRealVector) v); } return getLInfDistance(v.getData()); }
/** {@inheritDoc} */ public RealVector projection(RealVector v) throws IllegalArgumentException { checkVectorDimensions(v.getDimension()); return v.mapMultiply(dotProduct(v) / v.dotProduct(v)); }
/** * Construct a vector from another vector, using a deep copy. * @param v vector to copy */ public ArrayRealVector(RealVector v) { data = new double[v.getDimension()]; for (int i = 0; i < data.length; ++i) { data[i] = v.getEntry(i); } }
/** * Compute the cosine of the angle between 2 vectors. * * @param v Vector. * @param w Vector. * @return cosine of the angle */ private double cosAngle(final RealVector v, final RealVector w) { return v.dotProduct(w) / (v.getNorm() * w.getNorm()); }
/** {@inheritDoc} */ public RealVector projection(RealVector v) { return v.mapMultiply(dotProduct(v) / v.dotProduct(v)); }
/** {@inheritDoc} */ public RealVector add(RealVector v) throws IllegalArgumentException { if (v instanceof ArrayRealVector) { double[] values = ((ArrayRealVector)v).getDataRef(); return add(values); } RealVector result = v.copy(); Iterator<Entry> it = sparseIterator(); Entry e; while (it.hasNext() && (e = it.next()) != null) { final int index = e.getIndex(); result.setEntry(index, e.getValue() + result.getEntry(index)); } return result; }
/** * Get the -1 times the sum of all coefficients in the given array. * @param coefficients coefficients to sum * @return the -1 times the sum of all coefficients in the given array. */ protected static double getInvertedCoeffiecientSum(final RealVector coefficients) { double sum = 0; for (double coefficient : coefficients.getData()) { sum -= coefficient; } return sum; }
matrix.setEntry(zIndex, zIndex, maximize ? 1 : -1); RealVector objectiveCoefficients = maximize ? f.getCoefficients().mapMultiply(-1) : f.getCoefficients(); copyArray(objectiveCoefficients.getData(), matrix.getDataRef()[zIndex]); matrix.setEntry(zIndex, width - 1, maximize ? f.getConstantTerm() : -1 * f.getConstantTerm()); copyArray(constraint.getCoefficients().getData(), matrix.getDataRef()[row]); matrix.setEntry(0, getArtificialVariableOffset() + artificialVar, 1); matrix.setEntry(row, getArtificialVariableOffset() + artificialVar++, 1); matrix.setRowVector(0, matrix.getRowVector(0).subtract(matrix.getRowVector(row)));
if (data.length != rhs.getDimension()) { return false; if (rhs.isNaN()) { return this.isNaN(); if (data[i] != rhs.getEntry(i)) { return false;
/** {@inheritDoc} */ @Override public double getLInfDistance(RealVector v) throws IllegalArgumentException { if (v instanceof ArrayRealVector) { return getLInfDistance((ArrayRealVector) v); } else { checkVectorDimensions(v); double max = 0; for (int i = 0; i < data.length; ++i) { final double delta = data[i] - v.getEntry(i); max = Math.max(max, Math.abs(delta)); } return max; } }
/** * Compute the value of the linear equation at the current point * @param point point at which linear equation must be evaluated * @return value of the linear equation at the current point */ public double getValue(final double[] point) { return coefficients.dotProduct(point) + constantTerm; }
/** * Get the original number of decision variables. * @return original number of decision variables * @see #getNumDecisionVariables() */ protected final int getOriginalNumDecisionVariables() { return f.getCoefficients().getDimension(); }
/** * Subtracts a multiple of one row from another. * <p> * After application of this operation, the following will hold: * minuendRow = minuendRow - multiple * subtrahendRow * </p> * @param minuendRow row index * @param subtrahendRow row index * @param multiple multiplication factor */ protected void subtractRow(final int minuendRow, final int subtrahendRow, final double multiple) { tableau.setRowVector(minuendRow, tableau.getRowVector(minuendRow) .subtract(tableau.getRowVector(subtrahendRow).mapMultiply(multiple))); }
/** * Get a new equation equivalent to this one with a positive right hand side. * @param constraint reference constraint * @return new equation */ private LinearConstraint normalize(final LinearConstraint constraint) { if (constraint.getValue() < 0) { return new LinearConstraint(constraint.getCoefficients().mapMultiply(-1), constraint.getRelationship().oppositeRelationship(), -1 * constraint.getValue()); } return new LinearConstraint(constraint.getCoefficients(), constraint.getRelationship(), constraint.getValue()); }
/** {@inheritDoc} */ public RealVector subtract(RealVector v) throws IllegalArgumentException { if (v instanceof ArrayRealVector) { double[] values = ((ArrayRealVector)v).getDataRef(); return add(values); } RealVector result = v.copy(); Iterator<Entry> it = sparseIterator(); Entry e; while (it.hasNext() && (e = it.next()) != null) { final int index = e.getIndex(); v.setEntry(index, e.getValue() - result.getEntry(index)); } return result; }
/** * Get the -1 times the sum of all coefficients in the given array. * @param coefficients coefficients to sum * @return the -1 times the sum of all coefficients in the given array. */ protected static double getInvertedCoeffiecientSum(final RealVector coefficients) { double sum = 0; for (double coefficient : coefficients.getData()) { sum -= coefficient; } return sum; }
matrix.setEntry(zIndex, zIndex, maximize ? 1 : -1); RealVector objectiveCoefficients = maximize ? f.getCoefficients().mapMultiply(-1) : f.getCoefficients(); copyArray(objectiveCoefficients.getData(), matrix.getDataRef()[zIndex]); matrix.setEntry(zIndex, width - 1, maximize ? f.getConstantTerm() : -1 * f.getConstantTerm()); copyArray(constraint.getCoefficients().getData(), matrix.getDataRef()[row]); matrix.setEntry(0, getArtificialVariableOffset() + artificialVar, 1); matrix.setEntry(row, getArtificialVariableOffset() + artificialVar++, 1); matrix.setRowVector(0, matrix.getRowVector(0).subtract(matrix.getRowVector(row)));