Ejemplo n.º 1
0
Matrix<Point3D>::Matrix(const Matrix<double>& m1, const Matrix<double>& m2, const Matrix<double>& m3)
{
	Matrix<Point3D> m(m1.GetNumRows(),m1.GetNumCols());	
	for (int i=0; i<m1.GetNumRows(); i++) 
		for (int j=0; j<m1.GetNumCols(); j++) m[i][j] = Point3D(m1[i][j],m2[i][j],m3[i][j]);

	*this=m;
}
Ejemplo n.º 2
0
void MatrixQuantizerCPU<ElemType>::QuantizeAsync(const Matrix<ElemType>& inMatrix, const Matrix<ElemType>& inResidual, QuantizedMatrix<ElemType>& outQMatrix, Matrix<ElemType>& outResidual, bool zeroThresholdFor1Bit)
{
    // The outQMatrix should be on the CPU
    // TODO: Support transferring the quantization output to a quantized matrix on the GPU
    assert(outQMatrix.GetDeviceId() == CPUDEVICE);

    size_t nBits = outQMatrix.GetNumBits();

    size_t nRow = inMatrix.GetNumRows();
    size_t nCol = inMatrix.GetNumCols();

    // Verify that the different matrix parameters have matching dimensions
    assert((outQMatrix.GetNumRows() == nRow) && (outQMatrix.GetNumCols() == nCol));
    assert((inResidual.GetNumRows() == nRow) && (inResidual.GetNumCols() == nCol));
    assert((outResidual.GetNumRows() == nRow) && (outResidual.GetNumCols() == nCol));

    const size_t ldNbits = ValueQuantizer<ElemType>::ld(nBits);
#ifdef QUANTUSEPPL
    Concurrency::parallel_for((size_t) 0, us.cols(), [&](size_t j)
#else
    for (size_t j = 0; j < nCol; j++)
#endif
    {
        auto& qcol = *(outQMatrix.GetQuantizedColumn(j));
        if (zeroThresholdFor1Bit)
        {
            // Explicit use of 'template' keyword is needed to compile with GCC
            ColumnQuantizer<ElemType>::template ComputeRangeStatColj<true>(inMatrix.Data(), inResidual.Data(), (long) nRow, j, nBits, qcol.lower, qcol.upper);
        }
        else
        {
            // Explicit use of 'template' keyword is needed to compile with GCC
            ColumnQuantizer<ElemType>::template ComputeRangeStatColj<false>(inMatrix.Data(), inResidual.Data(), (long) nRow, j, nBits, qcol.lower, qcol.upper);
        }

        ColumnQuantizer<ElemType> q(ldNbits, qcol.lower, qcol.upper);
        if (zeroThresholdFor1Bit)
        {
            // Explicit use of 'template' keyword is needed to compile with GCC
            q.template Quantize<true>(inMatrix.Data(), inResidual.Data(), (long) nRow, j, qcol.bits, outResidual.Data());
        }
        else
        {
            // Explicit use of 'template' keyword is needed to compile with GCC
            q.template Quantize<false>(inMatrix.Data(), inResidual.Data(), (long) nRow, j, qcol.bits, outResidual.Data());
        }
    }
#ifdef QUANTUSEPPL
    );
Ejemplo n.º 3
0
Vector LeastSquare(const Matrix& matrix, const Vector& vector)
{
	// 1. construct Eigen matrix and Eigen vector
	// 2. solve the least square problem 
	// 3. transform back to Vector object
	int row = matrix.GetNumRows();
	int col = matrix.GetNumCols();
	int len = vector.GetLength();

	Eigen::MatrixXd mat(col, row);
	for (int i = 0; i < row; ++i)
	{
		for (int j = 0; j < col; ++j)
			mat(j, i) = matrix.GetValue(i, j);
	}

	Eigen::VectorXd vec(len);
	for (int i = 0; i < len; ++i)
		vec(i) = vector.GetValue(i);

	Eigen::VectorXd res = mat.jacobiSvd(Eigen::ComputeThinU | Eigen::ComputeThinV).solve(vec);

	Vector result(row);
	for (int i = 0; i < row; ++i)
		result.SetValue(i, res(i));
	return result;
}
Ejemplo n.º 4
0
void Avx2Mat4x4TestF64(const Matrix<double>& m1, const Matrix<double>& m2)
{
    if (!Matrix<double>::IsConforming(m1, m2))
        throw runtime_error("Non-conforming operands - Avx2Mat4x4TestF64");

    const size_t nrows = m1.GetNumRows();
    const size_t ncols = m1.GetNumCols();

    if (nrows != 4 || ncols != 4)
        throw runtime_error("Invalid size - Avx2Mat4x4TestF64");

    Matrix<double> m3_a(nrows, ncols);
    Matrix<double> m3_b(nrows, ncols);

    Matrix<double>::Mul(m3_a, m1, m2);
    Avx2Mat4x4MulF64_(m3_b.Data(), m1.Data(), m2.Data());

    cout << "\nResults for Avx2Mat4x4TestF64\n";

    cout << "\nMatrix m3_a\n";
    cout << m3_a << endl;

    cout << "\nMatrix m3_b\n";
    cout << m3_b << endl;

    double tr_a = m1.Trace();
    double tr_b = Avx2Mat4x4TraceF64_(m1.Data());
    cout << "tr_a = " << tr_a << '\n';
    cout << "tr_b = " << tr_b << '\n';
}
Ejemplo n.º 5
0
Matrix VStack(const Matrix& m1, const Matrix& m2)
{
	int row1 = m1.GetNumRows();
	int row2 = m2.GetNumRows();
	int col1 = m1.GetNumCols();
	int col2 = m2.GetNumCols();
	if (col1 != col2)
		throw std::runtime_error("Col size not equal");
	Matrix result(row1+row2, col1);
	for (int i = 0; i < row1; ++i)
		for (int j = 0; j < col1; ++j)
			result.SetValue(i, j, m1.GetValue(i, j));
	for (int i = 0; i < row2; ++i)
		for (int j = 0; j < col2; ++j)
			result.SetValue(i+row1, j, m2.GetValue(i, j));
	return result;
}
Ejemplo n.º 6
0
void GaussJ::swapRows(Matrix& matrix, int idx_1, int idx_2)
{
    int n = matrix.GetNumCols();
    
    double temp;
    for(int j = 0; j < n; ++j)
    {
        temp = matrix(idx_1, j);
        matrix(idx_1, j) = matrix(idx_2, j);
        matrix(idx_2, j) = temp;
    }
}
Ejemplo n.º 7
0
void DisplayMatrix(const Matrix& m)
{
	int rows = m.GetNumRows();
	int cols = m.GetNumCols();
	for (int i = 0; i < rows; ++i)
	{
		for (int j = 0; j < cols; ++j)
		{
			std::cout << m.GetValue(i, j) << "   ";
		}
		std::cout << std::endl;
	}
}
Ejemplo n.º 8
0
void MxM(const Matrix& m1, const Matrix& m2, Matrix* result)
{
	int row1 = m1.GetNumRows();
	int col1 = m1.GetNumCols();
	int row2 = m2.GetNumRows();
	int col2 = m2.GetNumCols();

	if (col1 != row2)
		return;

	result->Resize(row1, col2);
	for (int i = 0; i < row1; ++i)
	{
		for (int j = 0; j < col2; ++j)
		{
			double sum = 0.0;
			for (int k = 0; k < col1; ++k)
				sum += m1.GetValue(i, k) * m2.GetValue(k, j);
			result->SetValue(i, j, sum);
		}
	}
}
Ejemplo n.º 9
0
Vector VxM(const Vector& v, const Matrix& m)
{
	int row = m.GetNumRows();
	int col = m.GetNumCols();
	Vector result(col);
	for (int i = 0; i < col; ++i)
	{
		double sum = 0.0;
		for (int k = 0; k < row; ++k)
			sum += v.GetValue(k) * m.GetValue(k, i);
		result.SetValue(i, sum);
	}
	return result;
}
Ejemplo n.º 10
0
Vector MxV(const Matrix& m, const Vector& v)
{
	int row = m.GetNumRows();
	int col = m.GetNumCols();

	Vector result(row);
	for (int i = 0; i < row; ++i)
	{
		double sum = 0.0;
		for (int k = 0; k < col; ++k)
			sum += m.GetValue(i, k) * v.GetValue(k);
		result.SetValue(i, sum);
	}
	return result;
}
Ejemplo n.º 11
0
void LibSVMBinaryReader<ElemType>::DoDSSMMatrix(Matrix<ElemType>& mat, size_t actualMBSize)
{
    size_t numRows = mat.GetNumRows();
    if (DSSMCols < actualMBSize)
    {
        if (DSSMLabels != nullptr)
        {
            // free(DSSMLabels);
            CUDAPageLockedMemAllocator::Free(DSSMLabels, mat.GetDeviceId());
        }
        DSSMCols = actualMBSize;
        // DSSMLabels = (ElemType*)malloc(sizeof(ElemType)*numRows*actualMBSize);
        DSSMLabels = (ElemType*) CUDAPageLockedMemAllocator::Malloc(sizeof(ElemType) * numRows * actualMBSize, mat.GetDeviceId());
        memset(DSSMLabels, 0, sizeof(ElemType) * numRows * actualMBSize);
        for (size_t c = 0; c < numRows * actualMBSize; c += numRows)
        {
            DSSMLabels[c] = 1;
        }
    }
    if (mat.GetNumCols() != actualMBSize)
    {
        mat.SetValue(numRows, actualMBSize, mat.GetDeviceId(), DSSMLabels, matrixFlagNormal);
    }
}