/** * <p> * Computes the inner product between a vector and the conjugate of another one. * <br> * <br> * ∑<sub>k=1:n</sub> x<sub>k</sub> * conj(y<sub>k</sub>)<br> * where x and y are vectors with n elements. * </p> * * <p> * These functions are often used inside of highly optimized code and therefor sanity checks are * kept to a minimum. It is not recommended that any of these functions be used directly. * </p> * * @param x A vector with n elements. Not modified. * @param y A vector with n elements. Not modified. * @return The inner product of the two vectors. */ public static Complex_F64 innerProdH(ZMatrixRMaj x, ZMatrixRMaj y, Complex_F64 output) { if (output == null) { output = new Complex_F64(); } else { output.real = output.imaginary = 0; } int m = x.getDataLength(); for (int i = 0; i < m; i += 2) { double realX = x.data[i]; double imagX = x.data[i + 1]; double realY = y.data[i]; double imagY = -y.data[i + 1]; output.real += realX * realY - imagX * imagY; output.imaginary += realX * imagY + imagX * realY; } return(output); }
/** * Sets all the diagonal elements equal to one and everything else equal to zero. * If this is a square matrix then it will be an identity matrix. * * @param mat A square matrix. */ public static void setIdentity(ZMatrixRMaj mat) { int width = mat.numRows < mat.numCols ? mat.numRows : mat.numCols; Array.Clear(mat.data, 0, mat.getDataLength()); int index = 0; int stride = mat.getRowStride(); for (int i = 0; i < width; i++, index += stride + 2) { mat.data[index] = 1; } }
/** * Q = I - gamma*u*u<sup>H</sup> */ public static ZMatrixRMaj householder(ZMatrixRMaj u, double gamma) { int N = u.getDataLength() / 2; // u*u^H ZMatrixRMaj uut = new ZMatrixRMaj(N, N); VectorVectorMult_ZDRM.outerProdH(u, u, uut); // foo = -gamma*u*u^H CommonOps_ZDRM.elementMultiply(uut, -gamma, 0, uut); // I + foo for (int i = 0; i < N; i++) { int index = (i * uut.numCols + i) * 2; uut.data[index] = 1 + uut.data[index]; } return(uut); }
/** * <p> * Returns the absolute value of the element in the matrix that has the largest absolute value.<br> * <br> * Max{ |a<sub>ij</sub>| } for all i and j<br> * </p> * * @param a A matrix. Not modified. * @return The max abs element value of the matrix. */ public static double elementMaxAbs(ZMatrixRMaj a) { int size = a.getDataLength(); double max = 0; for (int i = 0; i < size; i += 2) { double real = a.data[i]; double imag = a.data[i + 1]; double val = real * real + imag * imag; if (val > max) { max = val; } } return(Math.Sqrt(max)); }
/** * <p> * Computes the Frobenius matrix norm:<br> * <br> * normF = Sqrt{ ∑<sub>i=1:m</sub> ∑<sub>j=1:n</sub> { a<sub>ij</sub><sup>2</sup>} } * </p> * <p> * This is equivalent to the element wise p=2 norm. * </p> * * @param a The matrix whose norm is computed. Not modified. * @return The norm's value. */ public static double normF(ZMatrixRMaj a) { double total = 0; double scale = CommonOps_ZDRM.elementMaxAbs(a); if (scale == 0.0) { return(0.0); } int size = a.getDataLength(); for (int i = 0; i < size; i += 2) { double real = a.data[i] / scale; double imag = a.data[i + 1] / scale; total += real * real + imag * imag; } return(scale * Math.Sqrt(total)); }