/** * <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_F32 innerProdH(CMatrixRMaj x, CMatrixRMaj y, Complex_F32 output) { if (output == null) { output = new Complex_F32(); } else { output.real = output.imaginary = 0; } int m = x.getDataLength(); for (int i = 0; i < m; i += 2) { float realX = x.data[i]; float imagX = x.data[i + 1]; float realY = y.data[i]; float 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(CMatrixRMaj 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 CMatrixRMaj householder(CMatrixRMaj u, float gamma) { int N = u.getDataLength() / 2; // u*u^H CMatrixRMaj uut = new CMatrixRMaj(N, N); VectorVectorMult_CDRM.outerProdH(u, u, uut); // foo = -gamma*u*u^H CommonOps_CDRM.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 float elementMaxAbs(CMatrixRMaj a) { int size = a.getDataLength(); float max = 0; for (int i = 0; i < size; i += 2) { float real = a.data[i]; float imag = a.data[i + 1]; float val = real * real + imag * imag; if (val > max) { max = val; } } return((float)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 float normF(CMatrixRMaj a) { float total = 0; float scale = CommonOps_CDRM.elementMaxAbs(a); if (scale == 0.0f) { return(0.0f); } int size = a.getDataLength(); for (int i = 0; i < size; i += 2) { float real = a.data[i] / scale; float imag = a.data[i + 1] / scale; total += real * real + imag * imag; } return(scale * (float)Math.Sqrt(total)); }