@Test public void testQuadraticForm() { Matrix X = new Matrix(5, 8); X.randomize(); Matrix S = SymmetricMatrix.XXt(X); DataBlock x = new DataBlock(S.getRowsCount()); x.randomize(); assertEquals(SymmetricMatrix.quadraticForm(S, x.getData()), SymmetricMatrix.quadraticForm(S, x), 1e-9); }
public static double quadraticForm(final Matrix S, final DataBlock x) { double[] pm = S.data_; double[] px = x.getData(); int x0 = x.getStartPosition(), x1 = x.getEndPosition(), xinc = x.getIncrement(); int cinc = S.nrows_; double s = 0; for (int ic = 0, id = 0, ix = x0; ix != x1; ix += xinc, id += 1 + cinc, ic += cinc) { double xi = px[ix]; double z = pm[id] * xi; for (int ir = ic, jx = x0; jx < ix; ++ir, jx += xinc) { z += 2 * pm[ir] * px[jx]; } s += z * xi; } return s; }
SymmetricMatrix { public static double quadraticForm(final Matrix S, final DataBlock x) { double[] pm = S.data_; double[] px = x.getData(); int x0 = x.getStartPosition(), x1 = x.getEndPosition(), xinc = x.getIncrement(); int cinc = S.nrows_; double s = 0; for (int ic = 0, id = 0, ix = x0; ix != x1; ix += xinc, id += 1 + cinc, ic += cinc) { double xi = px[ix]; double z = pm[id] * xi; for (int ir = ic, jx = x0; jx < ix; ++ir, jx += xinc) { z += 2 * pm[ir] * px[jx]; } s += z * xi; } return s; } }
SymmetricMatrix { public static double quadraticForm(final Matrix S, final DataBlock x) { double[] pm = S.data_; double[] px = x.getData(); int x0 = x.getStartPosition(), x1 = x.getEndPosition(), xinc = x.getIncrement(); int cinc = S.nrows_; double s = 0; for (int ic = 0, id = 0, ix = x0; ix != x1; ix += xinc, id += 1 + cinc, ic += cinc) { double xi = px[ix]; double z = pm[id] * xi; for (int ir = ic, jx = x0; jx < ix; ++ir, jx += xinc) { z += 2 * pm[ir] * px[jx]; } s += z * xi; } return s; } private  SymmetricMatrix(); }
SymmetricMatrix { public static double quadraticForm(final Matrix S, final DataBlock x) { double[] pm = S.data_; double[] px = x.getData(); int x0 = x.getStartPosition(), x1 = x.getEndPosition(), xinc = x.getIncrement(); int cinc = S.nrows_; double s = 0; for (int ic = 0, id = 0, ix = x0; ix != x1; ix += xinc, id += 1 + cinc, ic += cinc) { double xi = px[ix]; double z = pm[id] * xi; for (int ir = ic, jx = x0; jx < ix; ++ir, jx += xinc) { z += 2 * pm[ir] * px[jx]; } s += z * xi; } return s; } private  SymmetricMatrix(); static void fromLower(final Matrix m); static void fromLower(final SubMatrix m); static void fromUpper(final Matrix m); static void fromUpper(final SubMatrix m); static Matrix inverse(final Matrix s); static boolean isDPos(Matrix m, boolean useM); static boolean isSymmetric(Matrix m, double eps); static void lcholesky(final Matrix m); static void lcholesky(final Matrix m, double Zero); static Matrix LLt(final Matrix lower); static double quadraticForm(final Matrix S, final DataBlock x); static double quadraticForm(final Matrix s, final double[] x); static Matrix quadraticForm(final Matrix s, final Matrix x); static double quadraticForm(final SubMatrix M, final DataBlock x); static Matrix quadraticForm(final SubMatrix s, final SubMatrix x); static void quadraticForm(final SubMatrix s, final SubMatrix x,
            final SubMatrix xtsx); static Matrix quadraticFormT(final Matrix s, final Matrix x); static Matrix quadraticFormT(final SubMatrix s, final SubMatrix x); static void quadraticFormT(final SubMatrix s, final SubMatrix x,
            final SubMatrix xsxt); static void rcumul(Matrix m); static void rcumul(Matrix m, double delta); static void rcumul(Matrix m, double delta, int lag); static void rcumul(Matrix m, int lag); static void reinforceSymmetry(final Matrix m); static void reinforceSymmetry(final SubMatrix m); static void ucholesky(final Matrix s); static Matrix UtU(final Matrix upper); static Matrix XpXt(final Matrix x); static void XpXt(final SubMatrix m, final SubMatrix o); static Matrix XtX(final Matrix x); static void XtX(final SubMatrix x, final SubMatrix m); static Matrix XXt(final Matrix x); static void XXt(final SubMatrix x, final SubMatrix m); static void addXaXt(Matrix S, double a, DataBlock x); static void rsolve(Matrix S, SubMatrix B, boolean clone); static void lsolve(Matrix S, SubMatrix B, boolean clone); static void solve(Matrix S, DataBlock b, boolean clone); static void rsolve(Matrix S, DataBlock b, boolean clone); static Matrix CCt(DataBlock x); }
SymmetricMatrix { public static double quadraticForm(final Matrix S, final DataBlock x) { double[] pm = S.data_; double[] px = x.getData(); int x0 = x.getStartPosition(), x1 = x.getEndPosition(), xinc = x.getIncrement(); int cinc = S.nrows_; double s = 0; for (int ic = 0, id = 0, ix = x0; ix != x1; ix += xinc, id += 1 + cinc, ic += cinc) { double xi = px[ix]; double z = pm[id] * xi; for (int ir = ic, jx = x0; jx < ix; ++ir, jx += xinc) { z += 2 * pm[ir] * px[jx]; } s += z * xi; } return s; } private  SymmetricMatrix(); static void fromLower(final Matrix m); static void fromLower(final SubMatrix m); static void fromUpper(final Matrix m); static void fromUpper(final SubMatrix m); static Matrix inverse(final Matrix s); static boolean isDPos(Matrix m, boolean useM); static boolean isSymmetric(Matrix m, double eps); static void lcholesky(final Matrix m); static void lcholesky(final Matrix m, double Zero); static Matrix LLt(final Matrix lower); static double quadraticForm(final Matrix S, final DataBlock x); static double quadraticForm(final Matrix s, final double[] x); static Matrix quadraticForm(final Matrix s, final Matrix x); static double quadraticForm(final SubMatrix M, final DataBlock x); static Matrix quadraticForm(final SubMatrix s, final SubMatrix x); static void quadraticForm(final SubMatrix s, final SubMatrix x,
            final SubMatrix xtsx); static Matrix quadraticFormT(final Matrix s, final Matrix x); static Matrix quadraticFormT(final SubMatrix s, final SubMatrix x); static void quadraticFormT(final SubMatrix s, final SubMatrix x,
            final SubMatrix xsxt); static void rcumul(Matrix m); static void rcumul(Matrix m, double delta); static void rcumul(Matrix m, double delta, int lag); static void rcumul(Matrix m, int lag); static void reinforceSymmetry(final Matrix m); static void reinforceSymmetry(final SubMatrix m); static void ucholesky(final Matrix s); static Matrix UtU(final Matrix upper); static Matrix XpXt(final Matrix x); static void XpXt(final SubMatrix m, final SubMatrix o); static Matrix XtX(final Matrix x); static void XtX(final SubMatrix x, final SubMatrix m); static Matrix XXt(final Matrix x); static void XXt(final SubMatrix x, final SubMatrix m); static void addXaXt(Matrix S, double a, DataBlock x); static void rsolve(Matrix S, SubMatrix B, boolean clone); static void lsolve(Matrix S, SubMatrix B, boolean clone); static void solve(Matrix S, DataBlock b, boolean clone); static void rsolve(Matrix S, DataBlock b, boolean clone); static Matrix CCt(DataBlock x);  }