/* * Set the rows of a matrix to zero if the corresponding rows of a column vector are negative */ public static void SetRowsToZeroGivenColVector(DenseMatrix Z, DenseColumnVector x) { int nCols = Z.nCols; List<int> IdxNegX = new List<int>(); var xVal = x.VectorValue; int xDim = x.Dim; for (int IdxRow = 0; IdxRow < xDim; ++IdxRow ) { if (xVal[IdxRow]<0.0f) { IdxNegX.Add(IdxRow); } } Parallel.For(0, nCols, new ParallelOptions { MaxDegreeOfParallelism = MaxMultiThreadDegree }, IdxCol => { var ZVal = Z.DenseMatrixValue[IdxCol].VectorValue; int nCnt = IdxNegX.Count; for (int IdxRow = 0; IdxRow < nCnt; ++IdxRow) { ZVal[IdxNegX[IdxRow]] = 0.0f; } }); }
public static DenseColumnVector HorizontalSumMatrix(SparseMatrix X) { DenseColumnVector z = new DenseColumnVector(X.nRows); Array.Clear(z.VectorValue, 0, z.VectorValue.Length); for (int IdxCol = 0; IdxCol < X.nCols; IdxCol++ ) { int nNonzero = X.SparseColumnVectors[IdxCol].nNonzero; var xKey = X.SparseColumnVectors[IdxCol].Key; var xVal = X.SparseColumnVectors[IdxCol].Val; var zVal = z.VectorValue; for (int IdxRow = 0; IdxRow < nNonzero; ++IdxRow) { zVal[xKey[IdxRow]] += xVal[IdxRow]; } } return z; }
/* * z = z + y, where y is a scalar */ public static void ScalarAddVector(DenseColumnVector z, float y) { var zVal = z.VectorValue; for (int IdxCol = 0; IdxCol < z.Dim; IdxCol++) { zVal[IdxCol] += y; } }
public static void VectorSubtractVector(DenseColumnVector z, DenseColumnVector x, DenseColumnVector y) { if (z.Dim != x.Dim || z.Dim != y.Dim) { throw new Exception("Dimension mismatch."); } var zVal = z.VectorValue; var xVal = x.VectorValue; var yVal = y.VectorValue; int Dim = z.Dim; for (int IdxRow = 0; IdxRow < Dim; ++IdxRow) { zVal[IdxRow] = xVal[IdxRow] - yVal[IdxRow]; } }
public static void ScalarMultiplyVector(DenseColumnVector z, float y) { int Dim = z.Dim; var zVal = z.VectorValue; for (int IdxCol = 0; IdxCol < Dim; ++IdxCol) { zVal[IdxCol] *= y; } }
/* * Z = x ./ Z or b = bsxfun(@rdivide, x, Z) * Vector divide by matrix */ public static void bsxfunVectorDivideMatrix(DenseMatrix Z, DenseColumnVector x) { // Dimension check if (x.Dim != Z.nRows) { throw new Exception("Dimension mismatch."); } // Computation int nRows = Z.nRows; var xVal = x.VectorValue; Parallel.For(0, Z.nCols, new ParallelOptions { MaxDegreeOfParallelism = MaxMultiThreadDegree }, IdxCol => { var zVal = Z.DenseMatrixValue[IdxCol].VectorValue; for (int IdxRow = 0; IdxRow < nRows; ++IdxRow) { zVal[IdxRow] = xVal[IdxRow] / (zVal[IdxRow]+1e-12f); } }); }
public void DeepCopyFrom(DenseColumnVector SourceVector) { // Check dimension if (Dim != SourceVector.Dim) { throw new Exception("Dimension mismatch during deep copy of DenseColumnVector."); } // Deep copy of the float array Array.Copy(SourceVector.VectorValue,VectorValue,Dim); }
public DenseMatrix(int NumRows, int NumCols, float Value) { nRows = NumRows; nCols = NumCols; DenseMatrixValue = new DenseColumnVector[nCols]; for (int IdxCol = 0; IdxCol < nCols; IdxCol++) { DenseMatrixValue[IdxCol] = new DenseColumnVector(nRows,Value); } }
public static void AtomicAddVectorMultiplyVectorTranspose(DenseMatrix Z, SparseColumnVector x, DenseColumnVector y, float a) { if (Z.nRows != x.Dim || Z.nCols != y.Dim) { throw new Exception("Dimension mismatch."); } float product = 0.0f; float InitVal = 0.0f; float ComputedVal = 0.0f; int ZnCols = Z.nCols; int xNz = x.nNonzero; var xVal = x.Val; var xKey = x.Key; var yVal = y.VectorValue; for (int IdxCol = 0; IdxCol < ZnCols; ++IdxCol) { var ZVal = Z.DenseMatrixValue[IdxCol].VectorValue; for (int IdxRow = 0; IdxRow < xNz; ++IdxRow) { product = xVal[IdxRow] * yVal[IdxCol] * a; do { InitVal = ZVal[xKey[IdxRow]]; ComputedVal = InitVal + product; } while (InitVal != Interlocked.CompareExchange(ref ZVal[xKey[IdxRow]], ComputedVal, InitVal)); } } }
public static void bsxfunVectorMultiplyMatrix(DenseMatrix X, DenseColumnVector y) { if (X.nRows != y.Dim) { throw new Exception("The Number of rows in the two inputs does not match!"); } int nRows = X.nRows; Parallel.For(0, X.nCols, new ParallelOptions { MaxDegreeOfParallelism = MaxMultiThreadDegree }, IdxCol => { var xVal = X.DenseMatrixValue[IdxCol].VectorValue; var yVal = y.VectorValue; for (int IdxRow = 0; IdxRow < nRows; ++IdxRow) { xVal[IdxRow] *= yVal[IdxRow]; } }); }
/* * Z = exp(Z), or z = exp(z) */ public static void Exp(DenseColumnVector z) { var zVal = z.VectorValue; for (int IdxCol = 0; IdxCol < z.Dim; ++IdxCol) { zVal[IdxCol] = (float)Math.Exp((double)zVal[IdxCol]); } }
public static void bsxfunMatrixRightDivideVector(DenseMatrix X, DenseColumnVector y) { if (X.nRows != y.Dim) { throw new Exception("The Number of rows in the two inputs does not match!"); } for (int IdxRow = 0; IdxRow < X.nRows; IdxRow++) { for (int IdxCol = 0; IdxCol < X.nCols; IdxCol++) { X.DenseMatrixValue[IdxCol].VectorValue[IdxRow] /= (y.VectorValue[IdxRow]+1e-12f); } } }
public static void Log(DenseColumnVector z) { var zVal = z.VectorValue; for (int IdxRow = 0; IdxRow < z.Dim; ++IdxRow) { zVal[IdxRow] = (float) Math.Log((double) zVal[IdxRow]); } }
public static void HorizontalSumMatrix(DenseColumnVector z, SparseMatrix X) { if (z.Dim != X.nRows) { throw new Exception("Dimension mismatch."); } Array.Clear(z.VectorValue, 0, z.VectorValue.Length); for (int IdxCol = 0; IdxCol < X.nCols; IdxCol++) { int nNonzero = X.SparseColumnVectors[IdxCol].nNonzero; var xKey = X.SparseColumnVectors[IdxCol].Key; var xVal = X.SparseColumnVectors[IdxCol].Val; var zVal = z.VectorValue; for (int IdxRow = 0; IdxRow < nNonzero; ++IdxRow) { zVal[xKey[IdxRow]] += xVal[IdxRow]; } } }
public static void ProjCols2SimplexPlane(DenseColumnVector x) { float Mean = x.VectorValue.Average(); MatrixOperation.ScalarAddVector(x, ((-1.0f) * Mean + 1.0f / ((float)x.Dim))); }
/* * z = X * y * Matrix multiply vector */ public static void MatrixMultiplyVector(DenseColumnVector z, DenseMatrix X, DenseColumnVector y) { if (z.Dim != X.nRows || X.nCols != y.Dim) { throw new Exception("Dimension mismatch."); } Array.Clear(z.VectorValue, 0, z.VectorValue.Length); int XnCols = X.nCols; int zDim = z.Dim; for (int IdxCol = 0; IdxCol < XnCols; ++IdxCol) { var zVal = z.VectorValue; var XVal = X.DenseMatrixValue[IdxCol].VectorValue; var yVal = y.VectorValue; for (int IdxRow = 0; IdxRow < zDim; ++IdxRow) { zVal[IdxRow] += XVal[IdxRow] * yVal[IdxCol]; } } }
public DenseMatrix(int NumRows, int NumCols, bool IsPerColumn) { if (IsPerColumn) { nRows = NumRows; nCols = NumCols; isPerColumn = true; DenseMatrixValue = new DenseColumnVector[nCols]; for (int IdxCol = 0; IdxCol < nCols; IdxCol++) { DenseMatrixValue[IdxCol] = new DenseColumnVector(nRows); } } else { nRows = NumRows; nCols = NumCols; isPerColumn = false; DenseMatrixValuePerRow = new DenseRowVector[nRows]; for (int IdxRow = 0; IdxRow < nRows; IdxRow++) { DenseMatrixValuePerRow[IdxRow] = new DenseRowVector(nCols); } } }
public static void MatrixMultiplyVector(SparseColumnVector z, DenseMatrix X, DenseColumnVector y) { // Dimension check if (X.nCols != y.Dim || z.Dim != X.nRows) { throw new Exception("Dimension mismatch."); } // Computation var zVal = z.Val; var zKey = z.Key; Array.Clear(zVal, 0, zVal.Length); var zNNonzero = z.nNonzero; var xnCols = X.nCols; float[] xColumn = null; float yValue; for (int Idx = 0; Idx < xnCols; ++ Idx) { xColumn = X.DenseMatrixValue[Idx].VectorValue; yValue = y.VectorValue[Idx]; for (int IdxRow = 0; IdxRow < zNNonzero; ++ IdxRow) { zVal[IdxRow] += xColumn[zKey[IdxRow]] * yValue; } } }
public DenseMatrix(DenseMatrix SourceMatrix) { nRows = SourceMatrix.nRows; nCols = SourceMatrix.nCols; DenseMatrixValue = new DenseColumnVector[nCols]; for (int IdxCol = 0; IdxCol < nCols; IdxCol++) { DenseMatrixValue[IdxCol] = new DenseColumnVector(nRows); DenseMatrixValue[IdxCol].DeepCopyFrom(SourceMatrix.DenseMatrixValue[IdxCol]); } }
/* * z = X^T * y */ public static void MatrixTransposeMultiplyVector(DenseColumnVector z, DenseMatrix X, DenseColumnVector y) { if (z.Dim != X.nCols || X.nRows != y.Dim) { throw new Exception("Dimension mismatch."); } int zDim = z.Dim; int yDim = y.Dim; var XMat = X.DenseMatrixValue; var zVal = z.VectorValue; var yVal = y.VectorValue; Parallel.For(0, zDim, new ParallelOptions { MaxDegreeOfParallelism = MaxMultiThreadDegree }, IdxRow => { float sum = 0.0f; var XCol = XMat[IdxRow].VectorValue; for (int Idx = 0; Idx < yDim; ++Idx) { sum += XCol[Idx] * yVal[Idx]; } zVal[IdxRow] = sum; }); }
/* * z = x./y */ public static void ElementwiseVectorDivideVector(DenseColumnVector z, DenseColumnVector x, DenseColumnVector y) { if (z.Dim != x.Dim || z.Dim != y.Dim) { throw new Exception("Dimension mismatch."); } var zVal = z.VectorValue; var xVal = x.VectorValue; var yVal = y.VectorValue; int zDim = z.Dim; for (int IdxRow = 0; IdxRow < zDim; ++IdxRow) { zVal[IdxRow] = xVal[IdxRow] / (yVal[IdxRow]+1e-12f); } }
public static void MatrixTransposeMultiplyVector(DenseColumnVector z, DenseMatrix X, SparseColumnVector y) { if (z.Dim != X.nCols || X.nRows != y.Dim) { throw new Exception("Dimension mismatch."); } int zDim = z.Dim; float sum = 0.0f; int yNz = y.nNonzero; var XMat = X.DenseMatrixValue; var zVal = z.VectorValue; for (int IdxRow = 0; IdxRow < zDim; ++IdxRow) { sum = 0.0f; var XCol = XMat[IdxRow].VectorValue; var yKey = y.Key; var yVal = y.Val; for (int Idx = 0; Idx < yNz; ++Idx) { sum += XCol[yKey[Idx]] * yVal[Idx]; } zVal[IdxRow] = sum; } }
/* * Z = x ./ Y or b = bsxfun(@rdivide, x, Y) * Vector divide by matrix */ public static void bsxfunVectorDivideMatrix(DenseMatrix Z, DenseColumnVector x, DenseMatrix Y) { // Dimension check if (x.Dim != Z.nRows || Z.nCols != Y.nCols || Z.nRows != Y.nRows) { throw new Exception("Dimension mismatch."); } // Computation for (int IdxCol = 0; IdxCol < Z.nCols; IdxCol++) { for (int IdxRow = 0; IdxRow < Z.nRows; IdxRow++) { Z.DenseMatrixValue[IdxCol].VectorValue[IdxRow] = x.VectorValue[IdxRow] / (Y.DenseMatrixValue[IdxCol].VectorValue[IdxRow]+1e-12f); } } }
public static int CountValuesLessThanThreshold(DenseColumnVector x, float Threshold) { int NumSpecialElement = 0; var xVal = x.VectorValue; for (int IdxRow = 0; IdxRow < x.Dim; IdxRow++) { if(xVal[IdxRow]<Threshold) { NumSpecialElement++; } } return NumSpecialElement; }
/* * z = x * y, where x is a vector and y is a scalar */ public static void ScalarMultiplyVector(DenseColumnVector z, DenseColumnVector x, float y) { // Dimension check if (z.Dim != x.Dim) { throw new Exception("Dimension mismatch."); } // Computation int Dim = z.Dim; var zVal = z.VectorValue; var xVal = x.VectorValue; for (int IdxCol = 0; IdxCol < Dim; ++IdxCol) { zVal[IdxCol] = xVal[IdxCol] * y; } }
/* * Set elements of z to zero if the corresponding elements of x is zero. */ public static void ResetVectorSparsePattern(DenseColumnVector z, DenseColumnVector x) { if (z.Dim != x.Dim) { throw new Exception("Dimension mismatch."); } var xVal = x.VectorValue; var zVal = z.VectorValue; int zDim = z.Dim; for (int IdxRow = 0; IdxRow < zDim; ++IdxRow) { if (Math.Abs(xVal[IdxRow])<1e-30f) { zVal[IdxRow] = 0.0f; } } }
public static void Log(DenseColumnVector z, DenseColumnVector x) { // Dimension check if (z.Dim != x.Dim) { throw new Exception("Dimension mismatch."); } // Computation for (int IdxCol = 0; IdxCol < z.Dim; IdxCol++) { z.VectorValue[IdxCol] = (float)Math.Log((double)x.VectorValue[IdxCol]); } }
public static float InnerProduct(DenseColumnVector x, DenseColumnVector y) { if (x.Dim != y.Dim) { throw new Exception("Dimension mismatch."); } float z = 0.0f; var xVal = x.VectorValue; var yVal = y.VectorValue; int Dim = x.Dim; for(int Idx = 0; Idx < Dim; ++Idx) { z += xVal[Idx] * yVal[Idx]; } return z; }
/* * z = x + y, where x is a vector and y is a scalar */ public static void ScalarAddVector(DenseColumnVector z, DenseColumnVector x, float y) { if (z.Dim != x.Dim) { throw new Exception("Dimension mismatch."); } var zVal = z.VectorValue; var xVal = x.VectorValue; int Dim = z.Dim; for (int IdxCol = 0; IdxCol < Dim; ++IdxCol) { zVal[IdxCol] = xVal[IdxCol] + y; } }
public static void HorizontalSumMatrix(DenseColumnVector z, DenseMatrix X) { Array.Clear(z.VectorValue, 0, z.VectorValue.Length); Parallel.For(0, X.nRows, new ParallelOptions { MaxDegreeOfParallelism = MaxMultiThreadDegree }, IdxRow => { float sum = 0.0f; var xCol = X.DenseMatrixValue; int nCols = X.nCols; for (int IdxCol = 0; IdxCol < nCols; ++IdxCol) { sum += xCol[IdxCol].VectorValue[IdxRow]; } z.VectorValue[IdxRow] = sum; }); }