예제 #1
0
		/*
         * 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;
					}
				});
		}
예제 #2
0
		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;
		}
예제 #3
0
		/*
         * 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;
			}
		}
예제 #4
0
		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];
			}
		}
예제 #5
0
		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;
			}
		}
예제 #6
0
		/*
         * 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);
					}
				});
		}
예제 #7
0
		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);
		}
예제 #8
0
		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);
			}
		}
예제 #9
0
		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));
				}
			}
		}
예제 #10
0
		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];
					}
				});
		}
예제 #11
0
		/*
         * 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]);
			}
		}
예제 #12
0
		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);
				}
			}
		}
예제 #13
0
		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]);
			}
		}
예제 #14
0
		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];
				}
			}
		}
예제 #15
0
		public static void ProjCols2SimplexPlane(DenseColumnVector x)
		{
			float Mean = x.VectorValue.Average();
			MatrixOperation.ScalarAddVector(x, ((-1.0f) * Mean + 1.0f / ((float)x.Dim)));
		}
예제 #16
0
		/* 
         * 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];
				}
			}
		}
예제 #17
0
		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);
				}
			}
		}
예제 #18
0
		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;
				}
			}
		}
예제 #19
0
		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]);
			}
		}        
예제 #20
0
		/*
         * 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;
				});
		}
예제 #21
0
		/*
         * 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);
			}
		}
예제 #22
0
		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;
			}

		}
예제 #23
0
		/*
         * 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);
				}
			}
		}
예제 #24
0
		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;
		}
예제 #25
0
		/*
         * 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;
			}
		}
예제 #26
0
		/*
         * 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;
				}
			}
		}
예제 #27
0
		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]);
			}
		}
예제 #28
0
		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;
		}
예제 #29
0
		/*
         * 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;
			}
		}
예제 #30
0
		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;
				});
		}