@Override public void process() { DenseMatrix64F a = ((VariableMatrix)A).matrix; DenseMatrix64F b = ((VariableMatrix)B).matrix; if( !MatrixFeatures.isVector(a) || !MatrixFeatures.isVector(b)) throw new RuntimeException("Both inputs to dot() must be vectors"); output.value = VectorVectorMult.innerProd(a,b); } };
/** * <p></p> * Computes the dot product or inner product between two vectors. If the two vectors are columns vectors * then it is defined as:<br> * {@code dot(a,b) = a<sup>T</sup> * b}<br> * If the vectors are column or row or both is ignored by this function. * </p> * @param a Vector * @param b Vector * @return Dot product of the two vectors */ public static double dot( D1Matrix64F a , D1Matrix64F b ) { if( !MatrixFeatures.isVector(a) || !MatrixFeatures.isVector(b)) throw new RuntimeException("Both inputs must be vectors"); return VectorVectorMult.innerProd(a,b); }
/** * Extracts the elements from the source matrix by their 1D index. * * @param src Source matrix. Not modified. * @param indexes array of row indexes * @param length maximum element in row array * @param dst output matrix. Must be a vector of the correct length. */ public static void extract( DenseMatrix64F src, int indexes[] , int length , DenseMatrix64F dst ) { if( !MatrixFeatures.isVector(dst)) throw new IllegalArgumentException("Dst must be a vector"); if( length != dst.getNumElements()) throw new IllegalArgumentException("Unexpected number of elements in dst vector"); for (int i = 0; i < length; i++) { dst.data[i] = src.data[indexes[i]]; } }
/** * Computes the p=∞ norm. If A is a matrix then the induced norm is computed. * * @param A Matrix or vector. * @return The norm. */ public static double normPInf( DenseMatrix64F A ) { if( MatrixFeatures.isVector(A)) { return CommonOps.elementMaxAbs(A); } else { return inducedPInf(A); } }
/** * Computes the p=2 norm. If A is a matrix then the induced norm is computed. This * implementation is faster, but more prone to buffer overflow or underflow problems. * * @param A Matrix or vector. * @return The norm. */ public static double fastNormP2( DenseMatrix64F A ) { if( MatrixFeatures.isVector(A)) { return fastNormF(A); } else { return inducedP2(A); } }
/** * Computes the p=2 norm. If A is a matrix then the induced norm is computed. * * @param A Matrix or vector. * @return The norm. */ public static double normP2( DenseMatrix64F A ) { if( MatrixFeatures.isVector(A)) { return normF(A); } else { return inducedP2(A); } }
/** * Computes the p=∞ norm. If A is a matrix then the induced norm is computed. * * @param A Matrix or vector. * @return The norm. */ public static double normPInf( DenseMatrix64F A ) { if( MatrixFeatures.isVector(A)) { return CommonOps.elementMaxAbs(A); } else { return inducedPInf(A); } }
/** * Computes the p=2 norm. If A is a matrix then the induced norm is computed. * * @param A Matrix or vector. * @return The norm. */ public static double normP2( DenseMatrix64F A ) { if( MatrixFeatures.isVector(A)) { return normF(A); } else { return inducedP2(A); } }
/** * Computes the p=∞ norm. If A is a matrix then the induced norm is computed. * * @param A Matrix or vector. * @return The norm. */ public static double normPInf( DenseMatrix64F A ) { if( MatrixFeatures.isVector(A)) { return CommonOps.elementMaxAbs(A); } else { return inducedPInf(A); } }
/** * Computes the p=1 norm. If A is a matrix then the induced norm is computed. * * @param A Matrix or vector. * @return The norm. */ public static double normP1( DenseMatrix64F A ) { if( MatrixFeatures.isVector(A)) { return CommonOps.elementSumAbs(A); } else { return inducedP1(A); } }
/** * Computes the p=2 norm. If A is a matrix then the induced norm is computed. This * implementation is faster, but more prone to buffer overflow or underflow problems. * * @param A Matrix or vector. * @return The norm. */ public static double fastNormP2( DenseMatrix64F A ) { if( MatrixFeatures.isVector(A)) { return fastNormF(A); } else { return inducedP2(A); } }
/** * Extracts the column from a matrix. * @param a Input matrix * @param column Which column is to be extracted * @param out output. Storage for the extracted column. If null then a new vector will be returned. * @return The extracted column. */ public static DenseMatrix64F extractColumn( DenseMatrix64F a , int column , DenseMatrix64F out ) { if( out == null) out = new DenseMatrix64F(a.numRows,1); else if( !MatrixFeatures.isVector(out) || out.getNumElements() != a.numRows ) throw new IllegalArgumentException("Output must be a vector of length "+a.numRows); int index = column; for (int i = 0; i < a.numRows; i++, index += a.numCols ) { out.data[i] = a.data[index]; } return out; }
/** * Computes the p=1 norm. If A is a matrix then the induced norm is computed. * * @param A Matrix or vector. * @return The norm. */ public static double normP1( DenseMatrix64F A ) { if( MatrixFeatures.isVector(A)) { return CommonOps.elementSumAbs(A); } else { return inducedP1(A); } }
/** * Computes the p=1 norm. If A is a matrix then the induced norm is computed. * * @param A Matrix or vector. * @return The norm. */ public static double normP1( DenseMatrix64F A ) { if( MatrixFeatures.isVector(A)) { return CommonOps.elementSumAbs(A); } else { return inducedP1(A); } }
/** * Computes the p=2 norm. If A is a matrix then the induced norm is computed. * * @param A Matrix or vector. * @return The norm. */ public static double normP2( DenseMatrix64F A ) { if( MatrixFeatures.isVector(A)) { return normF(A); } else { return inducedP2(A); } }
/** * Extracts the row from a matrix. * @param a Input matrix * @param row Which row is to be extracted * @param out output. Storage for the extracted row. If null then a new vector will be returned. * @return The extracted row. */ public static DenseMatrix64F extractRow( DenseMatrix64F a , int row , DenseMatrix64F out ) { if( out == null) out = new DenseMatrix64F(1,a.numCols); else if( !MatrixFeatures.isVector(out) || out.getNumElements() != a.numCols ) throw new IllegalArgumentException("Output must be a vector of length "+a.numCols); System.arraycopy(a.data,a.getIndex(row,0),out.data,0,a.numCols); return out; }
/** * Computes the p=2 norm. If A is a matrix then the induced norm is computed. This * implementation is faster, but more prone to buffer overflow or underflow problems. * * @param A Matrix or vector. * @return The norm. */ public static double fastNormP2( DenseMatrix64F A ) { if( MatrixFeatures.isVector(A)) { return fastNormF(A); } else { return inducedP2(A); } }
/** * <p> * Extracts the diagonal elements 'src' write it to the 'dst' vector. 'dst' * can either be a row or column vector. * <p> * * @param src Matrix whose diagonal elements are being extracted. Not modified. * @param dst A vector the results will be written into. Modified. */ public static void extractDiag( DenseMatrix64F src, DenseMatrix64F dst ) { int N = Math.min(src.numRows, src.numCols); if( !MatrixFeatures.isVector(dst) ) { throw new IllegalArgumentException("Expected a vector for dst."); } else if( dst.getNumElements() != N ) { throw new IllegalArgumentException("Expected "+N+" elements in dst."); } for( int i = 0; i < N; i++ ) { dst.set( i , src.unsafe_get(i,i) ); } }
/** * Computes either the vector p-norm or the induced matrix p-norm depending on A * being a vector or a matrix respectively. * * @param A Vector or matrix whose norm is to be computed. * @param p The p value of the p-norm. * @return The computed norm. */ public static double normP( DenseMatrix64F A , double p ) { if( p == 1 ) { return normP1(A); } else if( p == 2 ) { return normP2(A); } else if( Double.isInfinite(p)) { return normPInf(A); } if( MatrixFeatures.isVector(A) ) { return elementP(A,p); } else { throw new IllegalArgumentException("Doesn't support induced norms yet."); } }
@Override public void process() { DenseMatrix64F mA = ((VariableMatrix)A).matrix; if(MatrixFeatures.isVector(mA)) { int N = mA.getNumElements(); output.matrix.reshape(N,N); CommonOps.diag(output.matrix,N,mA.data); } else { int N = Math.min(mA.numCols,mA.numRows); output.matrix.reshape(N,1); for (int i = 0; i < N; i++) { output.matrix.data[i] = mA.unsafe_get(i,i); } } } };