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; }
vector<double> GaussJ::gaussElimination() { Matrix aug = augmentMatrix(); int N = aug.GetNumRows(); for(int col_itr = 0; col_itr < N; ++col_itr) { // Search for maximum element in the column double max_entry = aug(col_itr, col_itr); int max_row = col_itr; for(int row_itr = (col_itr+1); row_itr < N; ++row_itr) { if(fabs(aug(row_itr, col_itr)) >= max_entry + numeric_limits<double>::epsilon()) { max_entry = fabs(aug(row_itr, col_itr)); max_row = row_itr; } } // Swap current row with the maximum-entry row if(max_row != col_itr) swapRows(aug, col_itr, max_row); // Make all rows below the current row 0 in the current column for(int row_itr = (col_itr+1); row_itr < N; ++row_itr) { double multiplier = -1.0 * (aug(row_itr, col_itr)/aug(col_itr, col_itr)); for(int col_itr2 = col_itr; col_itr2 < (N+1); ++col_itr2) { if(col_itr == col_itr2) aug(row_itr, col_itr2) = 0; else aug(row_itr, col_itr2) += multiplier * aug(col_itr, col_itr2); } } } // Solve the equations using the upper triangular matrix vector<double> solutions; solutions.resize(aug.GetNumRows(), 0.0); for(int i = (N-1); i >= 0; --i) { solutions[i] = (aug(i, N) / aug(i, i)); for(int j = (i-1); j >= 0; --j) aug(j, N) -= (solutions[i] * aug(j, i)); } return solutions; }
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 );
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; }
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'; }
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; }
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; } }
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); } } }
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; }
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; }
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); } }