/** {@inheritDoc} */ public RealVector solve(RealVector b) throws IllegalArgumentException, InvalidMatrixException { try { return solve((RealVectorImpl) b); } catch (ClassCastException cce) { final int m = lTData.length; if (b.getDimension() != m) { throw MathRuntimeException.createIllegalArgumentException( "vector length mismatch: got {0} but expected {1}", b.getDimension(), m); } final double[] x = b.getData(); // Solve LY = b for (int j = 0; j < m; j++) { final double[] lJ = lTData[j]; x[j] /= lJ[j]; final double xJ = x[j]; for (int i = j + 1; i < m; i++) { x[i] -= xJ * lJ[i]; } } // Solve LTX = Y for (int j = m - 1; j >= 0; j--) { x[j] /= lTData[j][j]; final double xJ = x[j]; for (int i = 0; i < j; i++) { x[i] -= xJ * lTData[i][j]; } } return new RealVectorImpl(x, false); } }
/** {@inheritDoc} */ public RealVector solve(RealVector b) { final int m = pivot.length; if (b.getDimension() != m) { throw new DimensionMismatchException(b.getDimension(), m); } if (singular) { throw new SingularMatrixException(); } final double[] bp = new double[m]; // Apply permutations to b for (int row = 0; row < m; row++) { bp[row] = b.getEntry(pivot[row]); } // Solve LY = b for (int col = 0; col < m; col++) { final double bpCol = bp[col]; for (int i = col + 1; i < m; i++) { bp[i] -= bpCol * lu[i][col]; } } // Solve UX = Y for (int col = m - 1; col >= 0; col--) { bp[col] /= lu[col][col]; final double bpCol = bp[col]; for (int i = 0; i < col; i++) { bp[i] -= bpCol * lu[i][col]; } } return new ArrayRealVector(bp, false); }
/** {@inheritDoc} */ public void setColumnVector(final int column, final RealVector vector) { MatrixUtils.checkColumnIndex(this, column); final int nRows = getRowDimension(); if (vector.getDimension() != nRows) { throw new MatrixDimensionMismatchException(vector.getDimension(), 1, nRows, 1); } for (int i = 0; i < nRows; ++i) { setEntry(i, column, vector.getEntry(i)); } }
/** {@inheritDoc} */ public void setRowVector(final int row, final RealVector vector) { MatrixUtils.checkRowIndex(this, row); final int nCols = getColumnDimension(); if (vector.getDimension() != nCols) { throw new MatrixDimensionMismatchException(1, vector.getDimension(), 1, nCols); } for (int i = 0; i < nCols; ++i) { setEntry(row, i, vector.getEntry(i)); } }
/** {@inheritDoc} */ public OpenMapRealVector add(RealVector v) throws IllegalArgumentException { checkVectorDimensions(v.getDimension()); if (v instanceof OpenMapRealVector) { return add((OpenMapRealVector) v); } return add(v.getData()); }
/** {@inheritDoc} */ public double getLInfDistance(RealVector v) throws IllegalArgumentException { checkVectorDimensions(v.getDimension()); if (v instanceof OpenMapRealVector) { return getLInfDistance((OpenMapRealVector) v); } return getLInfDistance(v.getData()); }
@Override public Label predict(Instance instance) { Label l = null; if (instance.getLabel() instanceof ClassificationLabel || instance.getLabel() == null) { // ----------------- declare variables ------------------ double lambda = 0.0; RealVector x_instance = new ArrayRealVector(matrixX.getColumnDimension(), 0); double result = 0.0; // -------------------------- initialize xi ------------------------- for (int idx = 0; idx < matrixX.getColumnDimension(); idx++) { x_instance.setEntry(idx, instance.getFeatureVector().get(idx + 1)); } // ------------------ get lambda ----------------------- for (int j = 0; j < alpha.getDimension(); j++) { lambda += alpha.getEntry(j) * kernelFunction(matrixX.getRowVector(j), x_instance); } // ----------------- make prediction ----------------- Sigmoid g = new Sigmoid(); // helper function result = g.value(lambda); l = new ClassificationLabel(result < 0.5 ? 0 : 1); } else { System.out.println("label type error!"); } return l; }
/** * Performs all dimension checks on the parameters of {@link #solve(RealLinearOperator, * RealVector, RealVector) solve} and {@link #solveInPlace(RealLinearOperator, RealVector, * RealVector) solveInPlace}, and throws an exception if one of the checks fails. * * @param a the linear operator A of the system * @param b the right-hand side vector * @param x0 the initial guess of the solution * @throws NullArgumentException if one of the parameters is {@code null} * @throws NonSquareOperatorException if {@code a} is not square * @throws DimensionMismatchException if {@code b} or {@code x0} have dimensions inconsistent with * {@code a} */ protected static void checkParameters( final RealLinearOperator a, final RealVector b, final RealVector x0) throws NullArgumentException, NonSquareOperatorException, DimensionMismatchException { MathUtils.checkNotNull(a); MathUtils.checkNotNull(b); MathUtils.checkNotNull(x0); if (a.getRowDimension() != a.getColumnDimension()) { throw new NonSquareOperatorException(a.getRowDimension(), a.getColumnDimension()); } if (b.getDimension() != a.getRowDimension()) { throw new DimensionMismatchException(b.getDimension(), a.getRowDimension()); } if (x0.getDimension() != a.getColumnDimension()) { throw new DimensionMismatchException(x0.getDimension(), a.getColumnDimension()); } }
/** * Serialize a {@link RealVector}. * * <p>This method is intended to be called from within a private <code>writeObject</code> method * (after a call to <code>oos.defaultWriteObject()</code>) in a class that has a {@link * RealVector} field, which should be declared <code>transient</code>. This way, the default * handling does not serialize the vector (the {@link RealVector} interface is not serializable by * default) but this method does serialize it specifically. * * <p>The following example shows how a simple class with a name and a real vector should be * written: * * <pre><code> * public class NamedVector implements Serializable { * * private final String name; * private final transient RealVector coefficients; * * // omitted constructors, getters ... * * private void writeObject(ObjectOutputStream oos) throws IOException { * oos.defaultWriteObject(); // takes care of name field * MatrixUtils.serializeRealVector(coefficients, oos); * } * * private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException { * ois.defaultReadObject(); // takes care of name field * MatrixUtils.deserializeRealVector(this, "coefficients", ois); * } * * } * </code></pre> * * @param vector real vector to serialize * @param oos stream where the real vector should be written * @exception IOException if object cannot be written to stream * @see #deserializeRealVector(Object, String, ObjectInputStream) */ public static void serializeRealVector(final RealVector vector, final ObjectOutputStream oos) throws IOException { final int n = vector.getDimension(); oos.writeInt(n); for (int i = 0; i < n; ++i) { oos.writeDouble(vector.getEntry(i)); } }
/** * Solve the linear equation A × X = B in least square sense. * * <p>The m×n matrix A may not be square, the solution X is such that ||A × X - B|| * is minimal. * * @param b right-hand side of the equation A × X = B * @return a vector X that minimizes the two norm of A × X - B * @exception IllegalArgumentException if matrices dimensions don't match * @exception InvalidMatrixException if decomposed matrix is singular */ public RealVector solve(final RealVector b) throws IllegalArgumentException, InvalidMatrixException { if (b.getDimension() != uT.getColumnDimension()) { throw MathRuntimeException.createIllegalArgumentException( "vector length mismatch: got {0} but expected {1}", b.getDimension(), uT.getColumnDimension()); } final RealVector w = uT.operate(b); for (int i = 0; i < singularValues.length; ++i) { final double si = singularValues[i]; if (si == 0) { throw new SingularMatrixException(); } w.setEntry(i, w.getEntry(i) / si); } return v.operate(w); }
/** {@inheritDoc} */ public OpenMapRealVector ebeMultiply(RealVector v) throws IllegalArgumentException { checkVectorDimensions(v.getDimension()); OpenMapRealVector res = new OpenMapRealVector(this); Iterator iter = res.entries.iterator(); while (iter.hasNext()) { iter.advance(); res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key())); } return res; }
/** {@inheritDoc} */ public double dotProduct(RealVector v) throws IllegalArgumentException { checkVectorDimensions(v.getDimension()); double res = 0; Iterator iter = entries.iterator(); while (iter.hasNext()) { iter.advance(); res += v.getEntry(iter.key()) * iter.value(); } return res; }
/** * Generic copy constructor. * * @param v The instance to copy from */ public OpenMapRealVector(RealVector v) { virtualSize = v.getDimension(); entries = new OpenIntToDoubleHashMap(0.0); epsilon = DEFAULT_ZERO_TOLERANCE; for (int key = 0; key < virtualSize; key++) { double value = v.getEntry(key); if (!isZero(value)) { entries.put(key, value); } } }
@Test public void testGenreVector() { double[] testVec1 = {0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; double[] testVec2 = {0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; RealVector testRealVector1 = MatrixUtils.createRealVector(testVec1); RealVector testRealVector2 = MatrixUtils.createRealVector(testVec2); assertThat(gdao.getItemGenre(0), equalTo(testRealVector1)); assertThat(gdao.getItemGenre(5), equalTo(testRealVector2)); assertThat(testRealVector1.getDimension(), equalTo(gdao.getGenreSize())); assertThat(gdao.getItemGenre(0).getDimension(), equalTo(gdao.getGenreSize())); assertThat(testVec1.length, equalTo(gdao.getGenreSize())); }
/** {@inheritDoc} */ public RealVector preMultiply(final RealVector v) throws DimensionMismatchException { try { return new ArrayRealVector(preMultiply(((ArrayRealVector) v).getDataRef()), false); } catch (ClassCastException cce) { final int nRows = getRowDimension(); final int nCols = getColumnDimension(); if (v.getDimension() != nRows) { throw new DimensionMismatchException(v.getDimension(), nRows); } final double[] out = new double[nCols]; for (int col = 0; col < nCols; ++col) { double sum = 0; for (int i = 0; i < nRows; ++i) { sum += getEntry(i, col) * v.getEntry(i); } out[col] = sum; } return new ArrayRealVector(out, false); } }
/** {@inheritDoc} */ @Override public RealVector operate(final RealVector v) throws DimensionMismatchException { try { return new ArrayRealVector(operate(((ArrayRealVector) v).getDataRef()), false); } catch (ClassCastException cce) { final int nRows = getRowDimension(); final int nCols = getColumnDimension(); if (v.getDimension() != nCols) { throw new DimensionMismatchException(v.getDimension(), nCols); } final double[] out = new double[nRows]; for (int row = 0; row < nRows; ++row) { double sum = 0; for (int i = 0; i < nCols; ++i) { sum += getEntry(row, i) * v.getEntry(i); } out[row] = sum; } return new ArrayRealVector(out, false); } }
/** {@inheritDoc} */ public RealMatrix outerProduct(RealVector v) throws IllegalArgumentException { checkVectorDimensions(v.getDimension()); if (v instanceof OpenMapRealVector) { return outerproduct((OpenMapRealVector) v); } RealMatrix res = new OpenMapRealMatrix(virtualSize, virtualSize); Iterator iter = entries.iterator(); while (iter.hasNext()) { iter.advance(); int row = iter.key(); for (int col = 0; col < virtualSize; col++) { res.setEntry(row, col, iter.value() * v.getEntry(col)); } } return res; }
@Test public void testConstructors() { OpenMapRealVector v0 = new OpenMapRealVector(); Assert.assertEquals("testData len", 0, v0.getDimension()); OpenMapRealVector v1 = new OpenMapRealVector(7); Assert.assertEquals("testData len", 7, v1.getDimension()); Assert.assertEquals("testData is 0.0 ", 0.0, v1.getEntry(6), 0); OpenMapRealVector v3 = new OpenMapRealVector(vec1); Assert.assertEquals("testData len", 3, v3.getDimension()); Assert.assertEquals("testData is 2.0 ", 2.0, v3.getEntry(1), 0); // SparseRealVector v4 = new SparseRealVector(vec4, 3, 2); // Assert.assertEquals("testData len", 2, v4.getDimension()); // Assert.assertEquals("testData is 4.0 ", 4.0, v4.getEntry(0)); // try { // new SparseRealVector(vec4, 8, 3); // Assert.fail("MathIllegalArgumentException expected"); // } catch (MathIllegalArgumentException ex) { // expected behavior // } RealVector v5_i = new OpenMapRealVector(dvec1); Assert.assertEquals("testData len", 9, v5_i.getDimension()); Assert.assertEquals("testData is 9.0 ", 9.0, v5_i.getEntry(8), 0); OpenMapRealVector v5 = new OpenMapRealVector(dvec1); Assert.assertEquals("testData len", 9, v5.getDimension()); Assert.assertEquals("testData is 9.0 ", 9.0, v5.getEntry(8), 0); OpenMapRealVector v7 = new OpenMapRealVector(v1); Assert.assertEquals("testData len", 7, v7.getDimension()); Assert.assertEquals("testData is 0.0 ", 0.0, v7.getEntry(6), 0); SparseRealVectorTestImpl v7_i = new SparseRealVectorTestImpl(vec1); OpenMapRealVector v7_2 = new OpenMapRealVector(v7_i); Assert.assertEquals("testData len", 3, v7_2.getDimension()); Assert.assertEquals("testData is 0.0 ", 2.0d, v7_2.getEntry(1), 0); OpenMapRealVector v8 = new OpenMapRealVector(v1); Assert.assertEquals("testData len", 7, v8.getDimension()); Assert.assertEquals("testData is 0.0 ", 0.0, v8.getEntry(6), 0); }
/** {@inheritDoc} */ public RealVector projection(RealVector v) throws IllegalArgumentException { checkVectorDimensions(v.getDimension()); return v.mapMultiply(dotProduct(v) / v.dotProduct(v)); }
/** {@inheritDoc} */ public void setSubVector(int index, RealVector v) throws MatrixIndexException { checkIndex(index); checkIndex(index + v.getDimension() - 1); setSubVector(index, v.getData()); }