Gadgetron::NFFT_internal::NFFT_Matrix<REAL> Gadgetron::NFFT_internal::transpose(const Gadgetron::NFFT_internal::NFFT_Matrix<REAL> &matrix) { GadgetronTimer timer("Transpose"); auto work_index = boost::irange(size_t(0),matrix.n_cols); NFFT_Matrix<REAL> transposed(matrix.n_rows, matrix.n_cols); std::vector<int> counts(matrix.n_rows, 0); for (size_t i : work_index) { auto &rows = matrix.indices[i]; for (auto &row : rows) { counts[row]++; } } for (size_t i = 0; i < counts.size(); i++) { transposed.indices[i].reserve(counts[i]); transposed.weights[i].reserve(counts[i]); } for (size_t i : work_index ) { auto &rows = matrix.indices[i]; auto &weights = matrix.weights[i]; for (size_t n = 0; n < rows.size(); n++) { transposed.indices[rows[n]].push_back(i); transposed.weights[rows[n]].push_back(weights[n]); } } return transposed; }
/** * @brief This function transposes a matrix represented by a 2-D array * @param args[0] The input matrix * return The transposed matrix **/ AnyType lda_transpose::run(AnyType & args) { ArrayHandle<int64_t> matrix = args[0].getAs<ArrayHandle<int64_t> >(); if(matrix.dims() != 2) throw std::domain_error("invalid dimension"); int32_t row_num = static_cast<int32_t>(matrix.sizeOfDim(0)); int32_t col_num = static_cast<int32_t>(matrix.sizeOfDim(1)); int dims[2] = {col_num, row_num}; int lbs[2] = {1, 1}; MutableArrayHandle<int64_t> transposed( madlib_construct_md_array( NULL, NULL, 2, dims, lbs, INT8TI.oid, INT8TI.len, INT8TI.byval, INT8TI.align)); for(int32_t i = 0; i < row_num; i++){ int32_t index = i * col_num; for(int32_t j = 0; j < col_num; j++){ transposed[j * row_num + i] = matrix[index]; index++; } } return transposed; }
Matrix<double> Matrix<double>::Multiply(const Matrix<double>& m) const { if ( GetColumnLength() != m.GetRowLength() ) { Matrix<double> null; return null; } Matrix<double> multiplied(GetRowLength(), m.GetColumnLength()); #if 0 for (size_t row = 0; row < multiplied.GetRowLength(); ++row) { for (size_t col = 0; col < multiplied.GetColumnLength(); ++col) { double v = 0; for (size_t k = 0; k < GetColumnLength(); ++k) { v += (*this)[row][k] * m[k][col]; } multiplied[row][col] = v; } } #else Matrix<double> transposed(m.T()); for (size_t row = 0; row < multiplied.GetRowLength(); ++row) { for (size_t col = 0; col < multiplied.GetColumnLength(); ++col) { multiplied[row][col] = (*this)[row].Dot(transposed[col]); } } #endif return multiplied; }
Matrix<T> Matrix<T>::transpose() const { Matrix<T> transposed(size()); for (unsigned int i = 0; i<size(); i++) for (unsigned int j = 0; j<size(); j++) transposed[j][i] = m_matrix[i][j]; return transposed; }
Matrix Matrix::getTransposed() const { Matrix transposed(m_size); for (size_t row = 0; row < m_size; ++row) { for (size_t col = 0; col < m_size; ++col) { transposed[row][col] = m_data[col][row]; } } return transposed; }
Matrix Matrix::transpose() const { Matrix transposed(this->nCols(), this->nRows()); for (unsigned int i = 0; i < this->nRows(); i++) { for (unsigned int j = 0; j < this->nCols(); j++) { transposed[j][i] = (*(this))[i][j]; } } return transposed; }
Matrix<double> Matrix<double>::Transpose(void) const { Matrix<double> transposed(GetColumnLength(), GetRowLength()); for (size_t i = 0; i < GetRowLength(); ++i) { for (size_t j = 0; j < GetColumnLength(); ++j) { transposed[j][i] = (*this)[i][j]; } } return transposed; }
void RectangularMatrixTest::transposed() { Matrix4x3 original(Vector3( 0.0f, 1.0f, 3.0f), Vector3( 4.0f, 5.0f, 7.0f), Vector3( 8.0f, 9.0f, 11.0f), Vector3(12.0f, 13.0f, 15.0f)); Matrix3x4 transposed(Vector4(0.0f, 4.0f, 8.0f, 12.0f), Vector4(1.0f, 5.0f, 9.0f, 13.0f), Vector4(3.0f, 7.0f, 11.0f, 15.0f)); CORRADE_COMPARE(original.transposed(), transposed); }
Matrix transpose(const Matrix& A){ assert(A.size() >= 1); assert(A[0].size() >= 1); int rows = A.size(); int cols = A[0].size(); Matrix transposed(cols, std::vector<double>(rows)); for(int row = 0; row < rows; row++){ for(int col = 0; col < cols; col++){ transposed[col][row] = A[row][col]; } } return transposed; }
void Basis::get_rotation_axis_angle_local(Vector3 &p_axis, real_t &p_angle) const { // Assumes that the matrix can be decomposed into a proper rotation and scaling matrix as M = R.S, // and returns the Euler angles corresponding to the rotation part, complementing get_scale(). // See the comment in get_scale() for further information. Basis m = transposed(); m.orthonormalize(); real_t det = m.determinant(); if (det < 0) { // Ensure that the determinant is 1, such that result is a proper rotation matrix which can be represented by Euler angles. m.scale(Vector3(-1, -1, -1)); } m.get_axis_angle(p_axis, p_angle); p_angle = -p_angle; }
// Decomposes a Basis into a rotation-reflection matrix (an element of the group O(3)) and a positive scaling matrix as B = O.S. // Returns the rotation-reflection matrix via reference argument, and scaling information is returned as a Vector3. // This (internal) function is too specific and named too ugly to expose to users, and probably there's no need to do so. Vector3 Basis::rotref_posscale_decomposition(Basis &rotref) const { #ifdef MATH_CHECKS ERR_FAIL_COND_V(determinant() == 0, Vector3()); Basis m = transposed() * (*this); ERR_FAIL_COND_V(!m.is_diagonal(), Vector3()); #endif Vector3 scale = get_scale(); Basis inv_scale = Basis().scaled(scale.inverse()); // this will also absorb the sign of scale rotref = (*this) * inv_scale; #ifdef MATH_CHECKS ERR_FAIL_COND_V(!rotref.is_orthogonal(), Vector3()); #endif return scale.abs(); }
/* called by the C++ code to render */ extern "C" void device_render (int* pixels, const unsigned int width, const unsigned int height, const float time, const ISPCCamera& camera) { float t0 = 0.7f*time; float t1 = 1.5f*time; /* rotate instances around themselves */ LinearSpace3fa xfm; xfm.vx = Vec3fa(cos(t1),0,sin(t1)); xfm.vy = Vec3fa(0,1,0); xfm.vz = Vec3fa(-sin(t1),0,cos(t1)); /* calculate transformations to move instances in cirle */ for (int i=0; i<4; i++) { float t = t0+i*2.0f*float(pi)/4.0f; instance_xfm[i] = AffineSpace3fa(xfm,2.2f*Vec3fa(+cos(t),0.0f,+sin(t))); } /* calculate transformations to properly transform normals */ for (int i=0; i<4; i++) normal_xfm[i] = transposed(rcp(instance_xfm[i].l)); /* set instance transformations */ rtcSetTransform2(g_scene,g_instance0,RTC_MATRIX_COLUMN_MAJOR_ALIGNED16,(float*)&instance_xfm[0],0); rtcSetTransform2(g_scene,g_instance1,RTC_MATRIX_COLUMN_MAJOR_ALIGNED16,(float*)&instance_xfm[1],0); rtcSetTransform2(g_scene,g_instance2,RTC_MATRIX_COLUMN_MAJOR_ALIGNED16,(float*)&instance_xfm[2],0); rtcSetTransform2(g_scene,g_instance3,RTC_MATRIX_COLUMN_MAJOR_ALIGNED16,(float*)&instance_xfm[3],0); /* update scene */ rtcUpdate(g_scene,g_instance0); rtcUpdate(g_scene,g_instance1); rtcUpdate(g_scene,g_instance2); rtcUpdate(g_scene,g_instance3); rtcCommit (g_scene); /* render all pixels */ const int numTilesX = (width +TILE_SIZE_X-1)/TILE_SIZE_X; const int numTilesY = (height+TILE_SIZE_Y-1)/TILE_SIZE_Y; parallel_for(size_t(0),size_t(numTilesX*numTilesY),[&](const range<size_t>& range) { const int threadIndex = (int)TaskScheduler::threadIndex(); for (size_t i=range.begin(); i<range.end(); i++) renderTileTask((int)i,threadIndex,pixels,width,height,time,camera,numTilesX,numTilesY); }); }
Matrix<T> Matrix<T>::xxT() const { Q_ASSERT(constData() && size()); int stride1(bytesPerLine()); int stride2(bytesPerElement()); if (type() == CV_64F) { Matrix<T> mulRes(rows(), rows()); int dstStride1(mulRes.bytesPerLine()); int dstStride2(mulRes.bytesPerElement()); const Ipp64f* dataPtr(reinterpret_cast<const Ipp64f*>(constData())); Ipp64f* mulResPtr(reinterpret_cast<Ipp64f*>(mulRes.data())); IppStatus status(ippmMul_mt_64f(dataPtr, stride1, stride2, cols(), rows(), dataPtr, stride1, stride2, cols(), rows(), mulResPtr, dstStride1, dstStride2)); Q_ASSERT(status == ippStsNoErr); return mulRes; } Q_ASSERT(!"CHECK"); return *this * transposed(); }
void MatrixTest:: testTransposedMatrix () { Matrix<3,2,int> matrix; assignList(matrix) = 1, 2, 3, 4, 5, 6; typedef Matrix<3,2,int> Matrix; TransposedMatrix<Matrix>& transposed = transpose(matrix); validateEquals (transposed(0,0), 1); validateEquals (transposed(0,1), 3); validateEquals (transposed(0,2), 5); validateEquals (transposed(1,0), 2); validateEquals (transposed(1,1), 4); validateEquals (transposed(1,2), 6); validate (transpose(transposed) == matrix); }
void check_geometry(Geometry1 const& geometry1, Geometry2 const& geometry2, std::string const& wkt1, std::string const& wkt2, std::string const& expected) { { std::string res_str = bgdr::relate<bgdr::matrix9>(geometry1, geometry2); bool ok = boost::equal(res_str, expected); BOOST_CHECK_MESSAGE(ok, "relate: " << wkt1 << " and " << wkt2 << " -> Expected: " << expected << " detected: " << res_str); } // changed sequence of geometries - transposed result { std::string res_str = bgdr::relate(geometry2, geometry1, bgdr::matrix9()); std::string expected_tr = transposed(expected); bool ok = boost::equal(res_str, expected_tr); BOOST_CHECK_MESSAGE(ok, "relate: " << wkt2 << " and " << wkt1 << " -> Expected: " << expected_tr << " detected: " << res_str); } { bool result = bgdr::relate(geometry1, geometry2, bgdr::mask9(expected)); // TODO: SHOULD BE !interrupted - CHECK THIS! BOOST_CHECK_MESSAGE(result, "relate: " << wkt1 << " and " << wkt2 << " -> Expected: " << expected); } if ( bg::detail::relate::interruption_enabled<Geometry1, Geometry2>::value ) { // brake the expected output std::string expected_interrupt = expected; bool changed = false; BOOST_FOREACH(char & c, expected_interrupt) { if ( c >= '0' && c <= '9' ) { if ( c == '0' ) c = 'F'; else --c; changed = true; } } if ( changed ) { bool result = bgdr::relate(geometry1, geometry2, bgdr::mask9(expected_interrupt)); // TODO: SHOULD BE interrupted - CHECK THIS! BOOST_CHECK_MESSAGE(!result, "relate: " << wkt1 << " and " << wkt2 << " -> Expected interrupt for:" << expected_interrupt); } } }
inline void matrix_reorder_columns(MatrixType& matrix, size_t row_first, size_t row_beyond, size_t column_first, size_t column_beyond, ElementLess element_less) { matrix_transposed <MatrixType> transposed(matrix); matrix_reorder_rows(matrix, column_first, column_beyond, row_first, row_beyond, element_less); }
inline void matrix_apply_column_permutation(MatrixType& matrix, const permutation& perm) { matroid_transposed <MatrixType> transposed(matrix); matrix_apply_row_permutation(transposed, perm); }
bool Basis::is_orthogonal() const { Basis id; Basis m = (*this)*transposed(); return isequal_approx(id,m); }
/** * Set command */ void command_set(const char* line) { char cmd[MAX_BUFFER]; char key[MAX_BUFFER]; char func[MAX_BUFFER]; char arg1[MAX_BUFFER]; char arg2[MAX_BUFFER]; int argc = sscanf(line, "%s %s = %s %s %s", cmd, key, func, arg1, arg2); if (argc < 3) { puts("invalid arguments"); return; } uint32_t* matrix = NULL; switch (argc) { case 3: if (strcasecmp(func, "identity") == 0) { matrix = identity_matrix(); } else { goto invalid; } break; case 4: if (strcasecmp(func, "random") == 0) { uint32_t seed = atoll(arg1); matrix = random_matrix(seed); } else if (strcasecmp(func, "uniform") == 0) { uint32_t value = atoll(arg1); matrix = uniform_matrix(value); } else if (strcasecmp(func, "cloned") == 0) { MATRIX_GUARD(arg1); matrix = cloned(m); } else if (strcasecmp(func, "reversed") == 0) { MATRIX_GUARD(arg1); matrix = reversed(m); } else if (strcasecmp(func, "transposed") == 0) { MATRIX_GUARD(arg1); matrix = transposed(m); } else { goto invalid; } break; case 5: if (strcasecmp(func, "sequence") == 0) { uint32_t start = atoll(arg1); uint32_t step = atoll(arg2); matrix = sequence_matrix(start, step); } else if (strcasecmp(func, "scalar#add") == 0) { MATRIX_GUARD(arg1); uint32_t value = atoll(arg2); matrix = scalar_add(m, value); } else if (strcasecmp(func, "scalar#mul") == 0) { MATRIX_GUARD(arg1); uint32_t value = atoll(arg2); matrix = scalar_mul(m, value); } else if (strcasecmp(func, "matrix#add") == 0) { MATRIX_GUARD_PAIR(arg1, arg2); matrix = matrix_add(m1, m2); } else if (strcasecmp(func, "matrix#mul") == 0) { MATRIX_GUARD_PAIR(arg1, arg2); matrix = matrix_mul(m1, m2); } else if (strcasecmp(func, "matrix#pow") == 0) { MATRIX_GUARD(arg1); uint32_t exponent = atoll(arg2); matrix = matrix_pow(m, exponent); } else { goto invalid; } break; } entry* e = find_entry(key); if (e == NULL) { e = add_entry(key); } else { free(e->matrix); } e->matrix = matrix; puts("ok"); return; invalid: puts("invalid arguments"); }
std::vector<double> TimeSmoother::FitModel(int parameterIndex, const std::vector<double>& dataPoints, const std::vector<int>& framesWithDataPoints) const { // 1. Project data points (from each frame) to model to get corresponding xi // Here the data points are the nodal parameters at each frame and linearly map to xi // 2. Construct P FourierBasis basis; int numRows = dataPoints.size(); gmm::dense_matrix<double> P(numRows, NUMBER_OF_PARAMETERS); // std::cout << "\n\n\nnumRows = " << numRows << '\n'; for (int i = 0; i < numRows; i++) { double xiDouble[1]; xiDouble[0] = MapToXi(static_cast<double>(i) / numRows); //REVISE design // std::cout << "dataPoint(" << i << ") = " << dataPoints[i] << ", xi = "<< xiDouble[0] <<'\n'; double psi[NUMBER_OF_PARAMETERS]; basis.Evaluate(psi, xiDouble); for (int columnIndex = 0; columnIndex < NUMBER_OF_PARAMETERS; columnIndex++) { P(i, columnIndex) = psi[columnIndex]; if (framesWithDataPoints[i]) { P(i, columnIndex) *= CAP_WEIGHT_GP; //TEST } } } // std::cout << "P = " << P << std::endl; // 3. Construct A // Note that G is the identity matrix. StS is read in from file. gmm::dense_matrix<double> A(NUMBER_OF_PARAMETERS, NUMBER_OF_PARAMETERS), temp(NUMBER_OF_PARAMETERS, NUMBER_OF_PARAMETERS); gmm::mult(gmm::transposed(P), P, temp); gmm::add(pImpl->S, temp, A); // std::cout << "A = " << A << std::endl; // 4. Construct rhs std::vector<double> prior(11), p(numRows), rhs(11); for (int i = 0; i < 11; i++) { prior[i] = pImpl->Priors(i, parameterIndex); } // std::cout << "prior: " << prior << std::endl; gmm::mult(P, prior, p); // std::transform(dataPoints.begin(), dataPoints.end(), p.begin(), p.begin(), std::minus<double>()); //TEST // p[0] *= 10; //more weight for frame 0 std::vector<double> dataLambda = dataPoints; for (unsigned int i = 0; i < dataLambda.size(); i++) { if (framesWithDataPoints[i]) { dataLambda[i] *= CAP_WEIGHT_GP; } } std::transform(dataLambda.begin(), dataLambda.end(), p.begin(), p.begin(), std::minus<double>()); gmm::mult(transposed(P), p, rhs); // std::cout << "rhs: " << rhs << std::endl; // 5. Solve normal equation (direct solver) std::vector<double> x(gmm::mat_nrows(A)); gmm::lu_solve(A, x, rhs); #ifndef NDEBUG // std::cout << "delta x (" << parameterIndex << ") " << x << std::endl; #endif std::transform(x.begin(), x.end(), prior.begin(), x.begin(), std::plus<double>()); return x; }