void ColumnMajorMatrixTest::exp() { ColumnMajorMatrix matrix1(3,3), matrix_exp1(3,3); ColumnMajorMatrix matrix2(3,3), matrix_exp2(3,3); Real err = 1.0e-10; Real e = 2.71828182846; Real e1 = (e*e*e*e - e)/3, e2 = (2*e + e*e*e*e)/3; matrix1(0,0) = 2.0; matrix1(0,1) = 1.0; matrix1(0,2) = 1.0; matrix1(1,0) = 1.0; matrix1(1,1) = 2.0; matrix1(1,2) = 1.0; matrix1(2,0) = 1.0; matrix1(2,1) = 1.0; matrix1(2,2) = 2.0; matrix1.exp(matrix_exp1); CPPUNIT_ASSERT( std::abs(matrix_exp1(0,0) - e2) < err ); CPPUNIT_ASSERT( std::abs(matrix_exp1(0,1) - e1) < err ); CPPUNIT_ASSERT( std::abs(matrix_exp1(0,2) - e1) < err ); CPPUNIT_ASSERT( std::abs(matrix_exp1(1,0) - e1) < err ); CPPUNIT_ASSERT( std::abs(matrix_exp1(1,1) - e2) < err ); CPPUNIT_ASSERT( std::abs(matrix_exp1(1,2) - e1) < err ); CPPUNIT_ASSERT( std::abs(matrix_exp1(2,0) - e1) < err ); CPPUNIT_ASSERT( std::abs(matrix_exp1(2,1) - e1) < err ); CPPUNIT_ASSERT( std::abs(matrix_exp1(2,2) - e2) < err ); matrix2(0,0) = 1.0; matrix2(0,1) = 1.0; matrix2(0,2) = 0.0; matrix2(1,0) = 0.0; matrix2(1,1) = 0.0; matrix2(1,2) = 2.0; matrix2(2,0) = 0.0; matrix2(2,1) = 0.0; matrix2(2,2) = -1.0; matrix2.exp(matrix_exp2); CPPUNIT_ASSERT( std::abs(matrix_exp2(0,0) - e) < err ); CPPUNIT_ASSERT( std::abs(matrix_exp2(0,1) - (e-1)) < err ); CPPUNIT_ASSERT( std::abs(matrix_exp2(0,2) - (-(-(e*e)+(2*e)-1)/e)) < err ); CPPUNIT_ASSERT( std::abs(matrix_exp2(1,0) - 0.0) < err ); CPPUNIT_ASSERT( std::abs(matrix_exp2(1,1) - 1) < err ); CPPUNIT_ASSERT( std::abs(matrix_exp2(1,2) - (2*(e-1)/e)) < err ); CPPUNIT_ASSERT( std::abs(matrix_exp2(2,0) - 0.0) < err ); CPPUNIT_ASSERT( std::abs(matrix_exp2(2,1) - 0.0) < err ); CPPUNIT_ASSERT( std::abs(matrix_exp2(2,2) - (1/e)) < err ); }
void MatrixTest::testMatrixMatrixOperations () { Matrix<2,3,int> lMatrix; Matrix<3,2,int> rMatrix; Matrix<2,2,int> result(0); assignList(lMatrix) = 1, 2, 3, 4, 5, 6; assignList(rMatrix) = 6, 5, 4, 3, 2, 1; // Matrix matrix multiplication multiply (lMatrix, rMatrix, result); validateEquals (result(0,0), 20); validateEquals (result(0,1), 14); validateEquals (result(1,0), 56); validateEquals (result(1,1), 41); // Bitwise comparison Matrix<2,3,int> matrixA(1); Matrix<2,3,int> matrixB(2); validate (matrixA == matrixA); validate (! (matrixA == matrixB)); // Test equalsReturnIndex Matrix<2,3,double> matrix1(1); Matrix<2,3,double> matrix2(2); int i=equalsReturnIndex(matrix1,matrix2); validateEquals(i,0); }
/******************************************************************** * Calculates the modularity for the clustering obtained. Obviously, * The clustering must have already been made. * IMPORTANT! We have a "virtual" cluster 0, that represents all hubs * and outliers. It will exist here only so that we don't lose coherence * of proportions, but e_{0,0} SHOULD NOT be considered for the trace Tr e. * ... (I think) ********************************************************************/ float ClusterEvaluator::getModularity() { // 1 - Generate Matrix e, where: // e_{i,j} = (edges linking ci to cj)/(all edges in G) float** e; //std::cout << "num_clusters = " << num_clusters << std::endl; SquareMatrix<float> matrix(num_clusters+1); e = matrix; buildAssortativityMatrix(e); //std::cout << "Matrix e:" << std::endl; //printSquareMatrix(e, num_clusters+1); //std::cout << "---------------------------------------" << std::endl; // 2 - Calculate Trace(e) = SUM_{i} e_{ii} float tr = 0.0; for (int i = 0; i <= num_clusters; ++i){ tr += e[i][i]; } //std::cout << "Trace is: " << tr << std::endl; //std::cout << "Calculate trace (e): DONE!" << std::endl; // 3 - Calculate e^2 SquareMatrix<float> matrix2(num_clusters+1); float** e_squared; e_squared = matrix2; squareMatrixMultiplication(e, e, e_squared, num_clusters+1); //std::cout << "Matrix e squared:" << std::endl; //printSquareMatrix(e_squared, num_clusters+1); //std::cout << "---------------------------------------" << std::endl; //std::cout << "Calculate e^2: DONE!" << std::endl; // 4 - Sum all elements of e^2 (||e^2||) float sum_e = sumElementsSquareMatrix(e_squared, num_clusters+1); //std::cout << "Sum is: " << sum_e << std::endl; //std::cout << "Sum all elements e^2 (||e^2||): DONE!" << std::endl; // 5 - Q = tr - ||e^2|| //std::cout << "Modularity is: " << tr - sum_e << std::endl; return tr - sum_e; }
static void printDistanceMatrix(MknnKmeansAlgorithm *kmeans) { MknnDataset *dataset = mknn_kmeans_getDataset(kmeans); MknnDistance *distance = mknn_kmeans_getDistance(kmeans); int64_t n = mknn_dataset_getNumObjects(dataset); std::vector<double> matrix(n * n); mknn_fillDistanceMatrix(dataset, distance, matrix.data(), 8); std::vector<int> limits_clusters; showMatrixToScreen("distances", matrix, n, limits_clusters); int64_t *assign = mknn_kmeans_getAssignations(kmeans, false); int64_t nc = mknn_kmeans_getNumCentroids(kmeans); std::vector<int> new_pos(n * n); int cont = 0; for (int j = 0; j < nc; j++) { limits_clusters.push_back(cont); for (int i = 0; i < n; i++) { if (assign[i] == j) new_pos[cont++] = i; } } my::assert::equalInt("n", cont, n); std::vector<double> matrix2(n * n); for (int row = 0; row < n; row++) { for (int col = 0; col < n; col++) { matrix2[row * n + col] = matrix[new_pos[row] * n + new_pos[col]]; } } showMatrixToScreen("distances2", matrix2, n, limits_clusters); }
void MatrixTest::testConstruction () { Matrix<1,2,int> matrix(1); validateEquals (matrix.size(), 2); validateEquals (matrix.rows(), 1); validateEquals (matrix.cols(), 2); validateEquals (matrix(0,0), 1); validateEquals (matrix(0,1), 1); Matrix<1,2,int> matrix2(matrix); validateEquals (matrix(0,0), 1); validateEquals (matrix(0,1), 1); DynamicMatrix<int> dynmatrix(1, 2, 1); validateEquals (dynmatrix.size(), 2); validateEquals (dynmatrix.rows(), 1); validateEquals (dynmatrix.cols(), 2); validateEquals (dynmatrix(0,0), 1); validateEquals (dynmatrix(0,1), 1); DynamicMatrix<int> dynmatrix2(dynmatrix); validateEquals (dynmatrix(0,0), 1); validateEquals (dynmatrix(0,1), 1); DynamicColumnMatrix<int> dyncolmatrix(1, 2, 1); validateEquals (dyncolmatrix.size(), 2); validateEquals (dyncolmatrix.rows(), 1); validateEquals (dyncolmatrix.cols(), 2); validateEquals (dyncolmatrix(0,0), 1); validateEquals (dyncolmatrix(0,1), 1); }
Transform * SceneParser::parseTransform() { char token[MAX_PARSER_TOKEN_LENGTH]; Matrix4f matrix = Matrix4f::identity(); Object3D *object = NULL; getToken(token); assert(!strcmp(token, "{")); // read in transformations: // apply to the LEFT side of the current matrix (so the first // transform in the list is the last applied to the object) getToken(token); while (true) { if (!strcmp(token,"Scale")) { Vector3f s = readVector3f(); matrix = matrix * Matrix4f::scaling( s[0], s[1], s[2] ); } else if (!strcmp(token,"UniformScale")) { float s = readFloat(); matrix = matrix * Matrix4f::uniformScaling( s ); } else if (!strcmp(token,"Translate")) { matrix = matrix * Matrix4f::translation( readVector3f() ); } else if (!strcmp(token,"XRotate")) { matrix = matrix * Matrix4f::rotateX((float) DegreesToRadians(readFloat())); } else if (!strcmp(token,"YRotate")) { matrix = matrix * Matrix4f::rotateY((float) DegreesToRadians(readFloat())); } else if (!strcmp(token,"ZRotate")) { matrix = matrix * Matrix4f::rotateZ((float) DegreesToRadians(readFloat())); } else if (!strcmp(token,"Rotate")) { getToken(token); assert(!strcmp(token, "{")); Vector3f axis = readVector3f(); float degrees = readFloat(); float radians = (float) DegreesToRadians(degrees); matrix = matrix * Matrix4f::rotation(axis,radians); getToken(token); assert(!strcmp(token, "}")); } else if (!strcmp(token,"Matrix4f")) { Matrix4f matrix2 = Matrix4f::identity(); getToken(token); assert(!strcmp(token, "{")); for (int j = 0; j < 4; j++) { for (int i = 0; i < 4; i++) { float v = readFloat(); matrix2( i, j ) = v; } } getToken(token); assert(!strcmp(token, "}")); matrix = matrix2 * matrix; } else { // otherwise this must be an object, // and there are no more transformations object = parseObject(token); break; } getToken(token); } assert(object != NULL); getToken(token); assert(!strcmp(token, "}")); return new Transform(matrix, object); }
void DynamicColumnMatrixTest::testBasics () { DynamicColumnMatrix<int> matrix(2, 3, 5); validateEquals (matrix.size(), 6); validateEquals (matrix.rows(), 2); validateEquals (matrix.cols(), 3); validateEquals (matrix(0,0), 5); validateEquals (matrix(0,1), 5); validateEquals (matrix(1,2), 5); assignList(matrix) = 1, 2, 3, 4, 5, 6; validateEquals (matrix(0,0), 1); validateEquals (matrix(0,1), 2); validateEquals (matrix(0,2), 3); validateEquals (matrix(1,0), 4); validateEquals (matrix(1,1), 5); validateEquals (matrix(1,2), 6); validateEquals (matrix.column(0).size(), 2); validateEquals (matrix.column(1).size(), 2); validateEquals (matrix.column(2).size(), 2); validateEquals (matrix.column(0)[0], 1); validateEquals (matrix.column(1)[0], 2); validateEquals (matrix.column(2)[1], 6); DynamicColumnMatrix<int> matrix2(matrix); validateEquals (matrix2.size(), 6); validateEquals (matrix2.rows(), 2); validateEquals (matrix2.cols(), 3); validateEquals (matrix2(0,0), 1); validateEquals (matrix2(0,1), 2); validateEquals (matrix2(0,2), 3); validateEquals (matrix2(1,0), 4); validateEquals (matrix2(1,1), 5); validateEquals (matrix2(1,2), 6); }
void rotate(vector<vector<int>>& matrix) { vector<vector<int>> matrix2(matrix); int T = matrix2[0].size(); for (int i = 0; i < T; i++){ for (int j = 0; j < T; j++){ matrix2[j][T - i - 1] = matrix[i][j]; } } matrix = matrix2; }
/** * <h2>Part 1: Helper functions</h2> * * We start by defining a couple of helper functions for sparse matrices represented by C++ STL containers, i.e. std::vector<std::map<T,U> > * * <h3>Reorder Matrix</h3> * * * Reorders a matrix according to a previously generated node number permutation vector r **/ inline std::vector< std::map<int, double> > reorder_matrix(std::vector< std::map<int, double> > const & matrix, std::vector<int> const & r) { std::vector< std::map<int, double> > matrix2(r.size()); for (std::size_t i = 0; i < r.size(); i++) for (std::map<int, double>::const_iterator it = matrix[i].begin(); it != matrix[i].end(); it++) matrix2[static_cast<std::size_t>(r[i])][r[static_cast<std::size_t>(it->first)]] = it->second; return matrix2; }
void MatrixTest::testMatrixOperations () { // Test computing determinant Matrix<3,3,int> matrix; assignList(matrix) = 1, 2, 3, 4, 5, 6, 7, 8, 9; validateEquals (0, det3x3(matrix)); assignList(matrix) = -1, 2, 3, 4, -5, 2, -2, 3, 1; validateEquals (1, det3x3(matrix)); // computed by octave // Test streaming Matrix<2,2,int> matrix2; assignList(matrix2) = 1, 2, 3, 4; std::ostringstream stream; stream << matrix2; validateEquals (stream.str(), std::string("1, 2; 3, 4")); // Test matrix multiply scalar matrix2 =matrix2*2; validateEquals(matrix2(0,0),2); validateEquals(matrix2(0,1),4); validateEquals(matrix2(1,0),6); validateEquals(matrix2(1,1),8); // Test matrix add matrix // Matrix<2,2,int> matrix3; // assignList(matrix3) = 1, 2, 3, 4; // matrix3=matrix3+matrix2; // validateEquals(matrix3(0,0),3); // validateEquals(matrix3(0,1),6); // validateEquals(matrix3(1,0),9); // validateEquals(matrix3(1,1),12); // Test matrix square Matrix<2,2,double> matrix4; assignList(matrix4) = 4.0, 9.0, 16.0, 25.0; matrix4=sqrt(matrix4); validateEquals(matrix4(0,0),2.0); validateEquals(matrix4(0,1),3.0); validateEquals(matrix4(1,0),4.0); validateEquals(matrix4(1,1),5.0); }
void MatrixTest:: testAssignment () { Matrix<2,2,int> matrix(1); assignList(matrix) = 1, 2, 3, 4; validateEquals (matrix(0,0), 1); validateEquals (matrix(0,1), 2); validateEquals (matrix(1,0), 3); validateEquals (matrix(1,1), 4); DynamicMatrix<int> dynmatrix(2, 2, 2); validateEquals (dynmatrix(0,0), 2); validateEquals (dynmatrix(0,1), 2); validateEquals (dynmatrix(1,0), 2); validateEquals (dynmatrix(1,1), 2); assign(dynmatrix) = matrix; validateEquals (dynmatrix(0,0), 1); validateEquals (dynmatrix(0,1), 2); validateEquals (dynmatrix(1,0), 3); validateEquals (dynmatrix(1,1), 4); assign(dynmatrix) = 5; validateEquals (dynmatrix(0,0), 5); validateEquals (dynmatrix(0,1), 5); validateEquals (dynmatrix(1,0), 5); validateEquals (dynmatrix(1,1), 5); assign(matrix) = dynmatrix; validateEquals (matrix(0,0), 5); validateEquals (matrix(0,1), 5); validateEquals (matrix(1,0), 5); validateEquals (matrix(1,1), 5); Matrix<1,2,int> matrix2; assignList(matrix2) = 1, 2; validateEquals (matrix2(0,0), 1); validateEquals (matrix2(0,1), 2); }
// Reorders a matrix according to a previously generated node // number permutation vector r std::vector< std::map<int, double> > reorder_matrix(std::vector< std::map<int, double> > const & matrix, std::vector<int> const & r) { std::vector< std::map<int, double> > matrix2(r.size()); std::vector<std::size_t> r2(r.size()); for (std::size_t i = 0; i < r.size(); i++) r2[r[i]] = i; for (std::size_t i = 0; i < r.size(); i++) for (std::map<int, double>::const_iterator it = matrix[r[i]].begin(); it != matrix[r[i]].end(); it++) matrix2[i][r2[it->first]] = it->second; return matrix2; }
void MapsMerge::GaleShapleyMatcherStrategy::testGaleShapleyAlgorithm() { // Initialization const int dimension = 4; int prefer[2 * dimension][dimension] = { {7, 5, 6, 4}, {5, 4, 6, 7}, {4, 5, 6, 7}, {4, 5, 6, 7}, {0, 1, 2, 3}, {0, 1, 2, 3}, {0, 1, 2, 3}, {0, 1, 2, 3}, }; vector<vector<int>> matrix1(dimension, vector<int>(dimension)); vector<vector<int>> matrix2(dimension, vector<int>(dimension)); cout << "Before initialization" << endl; //for (int i = 0; i < dimension; i++) { // matrix1[i].assign(&prefer[i][0], &prefer[i][dimension]); // matrix1[i + dimension].assign(&prefer[i + dimension][0], &prefer[i + dimension][dimension]); //} matrix1[0][0] = 3; matrix1[0][1] = 1; matrix1[0][2] = 2; matrix1[0][3] = 0; matrix1[1][0] = 1; matrix1[1][1] = 0; matrix1[1][2] = 2; matrix1[1][3] = 3; matrix1[2][0] = 0; matrix1[2][1] = 1; matrix1[2][2] = 2; matrix1[2][3] = 3; matrix1[3][0] = 0; matrix1[3][1] = 1; matrix1[3][2] = 2; matrix1[3][3] = 3; matrix2[0][0] = 0; matrix2[0][1] = 1; matrix2[0][2] = 2; matrix2[0][3] = 3; matrix2[1][0] = 0; matrix2[1][1] = 1; matrix2[1][2] = 2; matrix2[1][3] = 3; matrix2[2][0] = 0; matrix2[2][1] = 1; matrix2[2][2] = 2; matrix2[2][3] = 3; matrix2[3][0] = 0; matrix2[3][1] = 1; matrix2[3][2] = 2; matrix2[3][3] = 3; Utils::printMatrix("Matrix 1", matrix1); Utils::printMatrix("Matrix 2", matrix2); cout << "After initialization" << endl; // Algorithm vector<int> result = algGaleShapley(matrix1, matrix2); cout << "After algorithm" << endl; // Print results cout << "Results: " << endl; for (int i = 0; i < result.size(); i++) { cout << result[i] << ", "; } }
/** * \brief 给出3参数的相机坐标系 * * \param eye_position 视点的位置 * \param center_position 参考点的位置 * \param up_vector 视点向上方向的向量,通常为0.0, 1.0, 0.0 * * \return 世界坐标系转相机坐标系的矩阵 */ Mat3 LookAt2(const Vec3 &eye_position, const Vec3 ¢er_position, const Vec3 &up_vector) { Vec3 forward, side, up; Mat3 matrix2, resultMatrix; //------------------ forward = center_position - eye_position; forward.normalize(); //------------------ //Side = forward x up //ComputeNormalOfPlane(side, forward, up_vector); side[0] = (forward[1] * up_vector[2]) - (forward[2] * up_vector[1]); side[1] = (forward[2] * up_vector[0]) - (forward[0] * up_vector[2]); side[2] = (forward[0] * up_vector[1]) - (forward[1] * up_vector[0]); side.normalize(); //------------------ //Recompute up as: up = side x forward //ComputeNormalOfPlane(up, side, forward); up[0] = (side[1] * forward[2]) - (side[2] * forward[1]); up[1] = (side[2] * forward[0]) - (side[0] * forward[2]); up[2] = (side[0] * forward[1]) - (side[1] * forward[0]); //------------------ matrix2(0) = side[0]; matrix2(1) = side[1]; matrix2(2) = side[2]; //------------------ matrix2(3) = up[0]; matrix2(4) = up[1]; matrix2(5) = up[2]; //------------------ matrix2(6) = -forward[0]; matrix2(7) = -forward[1]; matrix2(8) = -forward[2]; return matrix2; }
void AffineTransformTestCase::InvertMatrix() { wxAffineMatrix2D matrix1; matrix1.Set(wxMatrix2D(2, 1, 1, 1), wxPoint2DDouble(1, 1)); wxAffineMatrix2D matrix2(matrix1); matrix2.Invert(); wxMatrix2D m; wxPoint2DDouble p; matrix2.Get(&m, &p); CPPUNIT_ASSERT_EQUAL( 1, (int)m.m_11 ); CPPUNIT_ASSERT_EQUAL( -1, (int)m.m_12 ); CPPUNIT_ASSERT_EQUAL( -1, (int)m.m_21 ); CPPUNIT_ASSERT_EQUAL( 2, (int)m.m_22 ); CPPUNIT_ASSERT_EQUAL( 0, (int)p.m_x ); CPPUNIT_ASSERT_EQUAL( -1, (int)p.m_y ); matrix2.Concat(matrix1); CPPUNIT_ASSERT( matrix2.IsIdentity() ); }
void matrix3x3Test() { printf("33¾ØÕó²âÊÔ\n"); S3D::Matrix3x3 matrix1; S3D::Matrix3x3 matrix2(1,2,3,4,5,6,7,8,9); S3D::Matrix3x3 matrix3(9,8,7,6,5,4,3,2,1); S3D::Matrix3x3 matrix4; S3D::print(&matrix1); S3D::print(&matrix2); PRINT_LINE; S3D::print(&matrix2); S3D::print(&matrix3); PRINT_LINE; matrix4=matrix2*matrix3; S3D::print(&matrix4); PRINT_LINE; S3D::print(&matrix2); S3D::print(&matrix3); PRINT_LINE; matrix2*=matrix3; matrix2=S3D::Matrix3x3(1,2,3,4,5,6,6,80,9); S3D::print(&matrix2); { printf("---ÇóÄæ¾ØÕó\n"); S3D::Matrix3x3 matrix5; S3D::Matrix3x3Inverse(&matrix5,&matrix2); matrix5=matrix5*matrix2; S3D::print(&matrix5); } }
TYPE main() { static TYPE A[X*Y] ; static TYPE B[Y*Z] ; static TYPE C[X*Z] ; STORAGE_CLASS TYPE *p_a = &A[0] ; STORAGE_CLASS TYPE *p_b = &B[0] ; STORAGE_CLASS TYPE *p_c = &C[0] ; int j; // TYPE f,i,k ; pin_down(&A[0], &B[0], &C[0]) ; matrix2(*&p_a,*&p_b,*&p_c); for(j=0;j<100;j++) printf("C[%d]: %d\n",j,C[j]); pin_down(&A[0], &B[0], &C[0]) ; return(0) ; }
GAIndividual<CodeVInt> GSTM::crossover(const GAIndividual<CodeVInt> &indivl1, const GAIndividual<CodeVInt> &indivl2) { if (indivl1 == indivl2) return indivl1; GAIndividual<CodeVInt> indivl; int i, j, n; vector<int> arr(m_numDim), flag(m_numDim+1); vector<vector<int>> matrix1(m_numDim), matrix2(m_numDim); for (i = 0; i < m_numDim; i++) { matrix1[i].resize(m_numDim); matrix2[i].resize(m_numDim); } map<int, vector<int> > frag; for (i = 0; i<m_numDim; i++) arr[i] = indivl1.data().m_x[i]; for (i = 0; i<m_numDim + 1; i++) flag[i] = 0; for (i = 0; i<m_numDim; i++) { for (j = 0; j<m_numDim; j++) { matrix1[i][j] = 0; matrix2[i][j] = 0; } } for (i = 0; i<m_numDim; i++) { matrix1[arr[i]][arr[(i + 1) % m_numDim]] = 1; matrix1[arr[(i + 1) % m_numDim]][arr[i]] = 1; matrix2[indivl2.data().m_x[i]][indivl2.data().m_x[(i + 1) % m_numDim]] = 1; matrix2[indivl2.data().m_x[(i + 1) % m_numDim]][indivl2.data().m_x[i]] = 1; } for (i = 0; i<m_numDim; i++) { if (matrix2[arr[i]][arr[(i + 1) % m_numDim]] == 0) flag[i + 1] = 1; } flag[0] = flag[m_numDim]; int m = 0; n = 0; j = 0; frag[n].push_back(arr[j++]); m++; while (flag[j] == 0 && m<m_numDim) { frag[n].push_back(arr[j++]); m++; } i = m_numDim; while (flag[i] == 0 && m<m_numDim) { frag[n].insert(frag[n].begin(), arr[i - 1]); i--; m++; } while (m<m_numDim) { if (flag[j] == 1) n++; frag[n].push_back(arr[j++]); m++; while (flag[j] == 0) { frag[n].push_back(arr[j++]); m++; } } vector<int> visited(m_numDim); map<int, int> mp; for (i = 0; i<m_numDim; i++) visited[i] = 0; m = 0; for (i = 0; i <= n; i++) { visited[m++] = frag[i].front(); mp[visited[m - 1]] = i; if (frag[i].size()>1) { visited[m++] = frag[i].back(); mp[visited[m - 1]] = i; } } int starts; i = Global::msp_global->getRandInt(0, m - 1); j = 0; arr[j++] = visited[i]; starts = visited[i]; if (frag[mp[visited[i]]].front() == starts) { for (size_t z = 1; z<frag[mp[visited[i]]].size(); z++) arr[j++] = frag[mp[visited[i]]][z]; starts = frag[mp[visited[i]]].back(); if (frag[mp[visited[i]]].size()>1) { int fg = 0; if (visited[m - 1] == visited[i]) fg = 1; for (int z = 0; z<m; z++) { if (visited[z] == frag[mp[visited[i]]].back()) { visited[z] = visited[m - 1]; m--; if (fg == 1) i = z; break; } } } visited[i] = visited[m - 1]; m--; } else if (frag[mp[visited[i]]].back() == starts) { for (int z = frag[mp[visited[i]]].size() - 2; z >= 0; z--) arr[j++] = frag[mp[visited[i]]][z]; starts = frag[mp[visited[i]]].front(); if (frag[mp[visited[i]]].size()>1) { int fg = 0; if (visited[m - 1] == visited[i]) fg = 1; for (int z = 0; z<m; z++) { if (visited[z] == frag[mp[visited[i]]].front()) { visited[z] = visited[m - 1]; m--; if (fg == 1) i = z; break; } } } visited[i] = visited[m - 1]; m--; } n--; for (i = 0; i <= n; i++) { double min = DBL_MAX; int temp; int pos = -1; TravellingSalesman * ptr = dynamic_cast<TravellingSalesman*>(Global::msp_global->mp_problem.get()); for (int z = 0; z<m; z++) { if (min>ptr->getCost()[starts][visited[z]] && matrix1[starts][visited[z]] == 0 && matrix2[starts][visited[z]] == 0) { min = ptr->getCost()[starts][visited[z]]; temp = visited[z]; pos = z; } } if (pos == -1) { temp = visited[0]; pos = 0; } arr[j++] = temp; if (frag[mp[temp]].front() == temp) { for (size_t z = 1; z<frag[mp[temp]].size(); z++) arr[j++] = frag[mp[temp]][z]; starts = frag[mp[temp]].back(); if (frag[mp[temp]].size()>1) { int fg = 0; if (visited[m - 1] == visited[pos]) fg = 1; for (int z = 0; z<m; z++) { if (visited[z] == frag[mp[temp]].back()) { visited[z] = visited[m - 1]; m--; if (fg == 1) pos = z; break; } } } visited[pos] = visited[m - 1]; m--; } else if (frag[mp[temp]].back() == temp) { for (int z = frag[mp[temp]].size() - 2; z >= 0; z--) arr[j++] = frag[mp[temp]][z]; starts = frag[mp[temp]].front(); if (frag[mp[temp]].size()>1) { int fg = 0; if (visited[m - 1] == visited[pos]) fg = 1; for (int z = 0; z<m; z++) { if (visited[z] == frag[mp[temp]].front()) { visited[z] = visited[m - 1]; m--; if (fg == 1) pos = z; break; } } } visited[pos] = visited[m - 1]; m--; } } for (i = 0; i<m_numDim; i++) indivl.data().m_x[i] = arr[i]; return indivl; }
void DynamicColumnMatrixTest::testColumnManipulations () { DynamicColumnMatrix<int> matrix(2, 3); validateEquals (matrix.size(), 6); validateEquals (matrix.rows(), 2); validateEquals (matrix.cols(), 3); assignList(matrix) = 1, 2, 3, 4, 5, 6; // Remove columns matrix.remove(2); validateEquals (matrix.size(), 4); validateEquals (matrix.rows(), 2); validateEquals (matrix.cols(), 2); matrix.remove(1); validateEquals (matrix.size(), 2); validateEquals (matrix.rows(), 2); validateEquals (matrix.cols(), 1); matrix.remove(0); validateEquals (matrix.size(), 0); validateEquals (matrix.rows(), 0); validateEquals (matrix.cols(), 0); // Add columns matrix.append (DynamicVector<int>(3,1)); validateEquals (matrix.size(), 3); validateEquals (matrix.rows(), 3); validateEquals (matrix.cols(), 1); validateEquals (matrix(0,0), 1); validateEquals (matrix(1,0), 1); validateEquals (matrix(2,0), 1); matrix.append (DynamicVector<int>(3,2)); validateEquals (matrix.size(), 6); validateEquals (matrix.rows(), 3); validateEquals (matrix.cols(), 2); validateEquals (matrix(0,0), 1); validateEquals (matrix(1,0), 1); validateEquals (matrix(2,0), 1); validateEquals (matrix(0,1), 2); validateEquals (matrix(1,1), 2); validateEquals (matrix(2,1), 2); matrix.appendFront (DynamicVector<int>(3,3)); validateEquals (matrix.size(), 9); validateEquals (matrix.rows(), 3); validateEquals (matrix.cols(), 3); validateEquals (matrix(0,0), 3); validateEquals (matrix(1,0), 3); validateEquals (matrix(2,0), 3); validateEquals (matrix(0,1), 1); validateEquals (matrix(1,1), 1); validateEquals (matrix(2,1), 1); validateEquals (matrix(0,2), 2); validateEquals (matrix(1,2), 2); validateEquals (matrix(2,2), 2); // Shift columns matrix.shiftSetFirst (DynamicVector<int>(3,4)); validateEquals (matrix.size(), 9); validateEquals (matrix.rows(), 3); validateEquals (matrix.cols(), 3); validateEquals (matrix(0,0), 4); validateEquals (matrix(1,0), 4); validateEquals (matrix(2,0), 4); validateEquals (matrix(0,1), 3); validateEquals (matrix(1,1), 3); validateEquals (matrix(2,1), 3); validateEquals (matrix(0,2), 1); validateEquals (matrix(1,2), 1); validateEquals (matrix(2,2), 1); DynamicColumnMatrix<int> matrix2; matrix2.append (matrix); validateEquals (matrix2.size(), 9); validateEquals (matrix2.rows(), 3); validateEquals (matrix2.cols(), 3); validateEquals (matrix2(0,0), 4); validateEquals (matrix2(1,0), 4); validateEquals (matrix2(2,0), 4); validateEquals (matrix2(0,1), 3); validateEquals (matrix2(1,1), 3); validateEquals (matrix2(2,1), 3); validateEquals (matrix2(0,2), 1); validateEquals (matrix2(1,2), 1); validateEquals (matrix2(2,2), 1); // Remove columns from intermediate positions matrix2.remove (1); validateEquals (matrix2.size(), 6); validateEquals (matrix2.rows(), 3); validateEquals (matrix2.cols(), 2); validateEquals (matrix2(0,0), 4); validateEquals (matrix2(1,0), 4); validateEquals (matrix2(2,0), 4); validateEquals (matrix2(0,1), 1); validateEquals (matrix2(1,1), 1); validateEquals (matrix2(2,1), 1); matrix2.remove (0); validateEquals (matrix2.size(), 3); validateEquals (matrix2.rows(), 3); validateEquals (matrix2.cols(), 1); validateEquals (matrix2(0,0), 1); validateEquals (matrix2(1,0), 1); validateEquals (matrix2(2,0), 1); matrix2.clear (); validateEquals (matrix2.size(), 0); validateEquals (matrix2.rows(), 0); validateEquals (matrix2.cols(), 0); }
int main() { std::cout << "[moeoDominanceMatrix]\n\n"; // objective vectors ObjectiveVector obj0, obj1, obj2, obj3, obj4, obj5, obj6; obj0[0] = 2; obj0[1] = 5; obj1[0] = 3; obj1[1] = 3; obj2[0] = 4; obj2[1] = 1; obj3[0] = 5; obj3[1] = 5; obj4[0] = 5; obj4[1] = 1; obj5[0] = 3; obj5[1] = 3; obj6[0] = 4; obj6[1] = 4; // population eoPop < Solution > pop; pop.resize(4); pop[0].objectiveVector(obj0); // class 1 pop[1].objectiveVector(obj1); // class 1 pop[2].objectiveVector(obj2); // class 1 pop[3].objectiveVector(obj3); // class 3 moeoUnboundedArchive < Solution > archive; archive.resize(3); archive[0].objectiveVector(obj4); archive[1].objectiveVector(obj5); archive[2].objectiveVector(obj6); moeoParetoObjectiveVectorComparator < ObjectiveVector > paretoComparator; // fitness assignment moeoDominanceMatrix< Solution > matrix(true); moeoDominanceMatrix< Solution > matrix2(paretoComparator, false); //test operator() with 2 parameters matrix(pop,archive); //test result of matrix for (unsigned int i=0; i<7; i++) for (unsigned int j=0; j<3; j++) assert(!matrix[i][j]); assert(matrix[0][3]); assert(matrix[0][5]); assert(matrix[1][3]); assert(matrix[1][5]); assert(matrix[1][6]); assert(matrix[2][3]); assert(matrix[2][4]); assert(matrix[2][5]); assert(matrix[2][6]); assert(matrix[3][5]); assert(matrix[4][3]); assert(matrix[4][5]); assert(matrix[6][3]); assert(matrix[6][5]); assert(!matrix[0][4]); assert(!matrix[0][6]); assert(!matrix[1][4]); assert(!matrix[3][3]); assert(!matrix[3][4]); assert(!matrix[3][6]); assert(!matrix[4][4]); assert(!matrix[4][6]); assert(!matrix[5][3]); assert(!matrix[5][4]); assert(!matrix[5][5]); assert(!matrix[5][6]); assert(!matrix[6][4]); assert(!matrix[6][6]); //test methode count assert(matrix.count(0)==2); assert(matrix.count(1)==3); assert(matrix.count(2)==4); assert(matrix.count(3)==1); assert(matrix.count(4)==2); assert(matrix.count(5)==0); assert(matrix.count(6)==2); //test methode rank assert(matrix.rank(0)==0); assert(matrix.rank(1)==0); assert(matrix.rank(2)==0); assert(matrix.rank(3)==5); assert(matrix.rank(4)==1); assert(matrix.rank(5)==6); assert(matrix.rank(6)==2); //test operator() with one parameter matrix2(archive); assert(!matrix2[0][0]); assert(!matrix2[0][1]); assert(!matrix2[0][2]); assert(!matrix2[1][0]); assert(!matrix2[1][1]); assert(matrix2[1][2]); assert(!matrix2[2][0]); assert(!matrix2[2][1]); assert(!matrix2[2][2]); assert(matrix2.count(0)==0); assert(matrix2.count(1)==1); assert(matrix2.count(2)==0); assert(matrix2.rank(0)==0); assert(matrix2.rank(1)==0); assert(matrix2.rank(2)==1); std::set<int> hop; hop.insert(2); hop.insert(2); hop.insert(10); hop.insert(3); hop.insert(45); hop.insert(45); hop.insert(45); std::set<int>::iterator it=hop.begin(); while (it!=hop.end()) { std::cout << *it << "\n"; it++; } std::cout << "OK"; return EXIT_SUCCESS; }
TEST_F(CublasWrapperTest, matrixTransMatrixMultiply) { const int numberOfRows = 5; const int numberOfColumns = 3; const int numberOfRows2 = 5; const int numberOfColumns2 = 4; PinnedHostMatrix matrixT(numberOfRows, numberOfColumns); PinnedHostMatrix matrix2(numberOfRows2, numberOfColumns2); matrixT(0, 0) = 1; matrixT(1, 0) = 2; matrixT(2, 0) = 3; matrixT(3, 0) = 4; matrixT(4, 0) = 5; matrixT(0, 1) = 10; matrixT(1, 1) = 20; matrixT(2, 1) = 30; matrixT(3, 1) = 40; matrixT(4, 1) = 50; matrixT(0, 2) = 1.1; matrixT(1, 2) = 2.2; matrixT(2, 2) = 3.3; matrixT(3, 2) = 4.4; matrixT(4, 2) = 5.5; for(int i = 0; i < numberOfRows2; ++i){ matrix2(i, 0) = 6; } for(int i = 0; i < numberOfRows2; ++i){ matrix2(i, 1) = 7; } for(int i = 0; i < numberOfRows2; ++i){ matrix2(i, 2) = 8; } for(int i = 0; i < numberOfRows2; ++i){ matrix2(i, 3) = 9; } DeviceMatrix* matrixTDevice = hostToDeviceStream1.transferMatrix(matrixT); DeviceMatrix* matrix2Device = hostToDeviceStream1.transferMatrix(matrix2); DeviceMatrix* resultDevice = new DeviceMatrix(numberOfColumns, numberOfColumns2); cublasWrapper.matrixTransMatrixMultiply(*matrixTDevice, *matrix2Device, *resultDevice); cublasWrapper.syncStream(); handleCudaStatus(cudaGetLastError(), "Error with matrixTransMatrixMultiply in matrixTransMatrixMultiply: "); HostMatrix* resultHost = deviceToHostStream1.transferMatrix(*resultDevice); cublasWrapper.syncStream(); handleCudaStatus(cudaGetLastError(), "Error with transfer in matrixTransMatrixMultiply: "); EXPECT_EQ(90, (*resultHost)(0, 0)); EXPECT_EQ(105, (*resultHost)(0, 1)); EXPECT_EQ(120, (*resultHost)(0, 2)); EXPECT_EQ(135, (*resultHost)(0, 3)); EXPECT_EQ(900, (*resultHost)(1, 0)); EXPECT_EQ(1050, (*resultHost)(1, 1)); EXPECT_EQ(1200, (*resultHost)(1, 2)); EXPECT_EQ(1350, (*resultHost)(1, 3)); EXPECT_EQ(99, (*resultHost)(2, 0)); EXPECT_EQ(115.5, (*resultHost)(2, 1)); EXPECT_EQ(132, (*resultHost)(2, 2)); EXPECT_EQ(148.5, (*resultHost)(2, 3)); delete matrixTDevice; delete matrix2Device; delete resultDevice; delete resultHost; }
int tests() { { //assignment Matrix<double> matrix(5, 5, 1); Matrix<double> copyMatrix = matrix; if (matrix != copyMatrix) { return 101; } } { //test simple matrix accessor Matrix<double> matrix(5, 5, 1); if (matrix[1][1] != 1) { return 102; } if (matrix(1, 1) != 1) { return 103; } } { //test matrix addition Matrix<double> matrix1(3, 3, 1); Matrix<double> matrix2(3, 3, 3); Matrix<double> result(3, 3, 4); Matrix<double> matrix3 = matrix1 + matrix2; if (matrix3 != result) { return 104; } } { //test cumulative matrix addition Matrix<double> matrix1(3, 3, 1); Matrix<double> matrix2(3, 3, 3); Matrix<double> result(3, 3, 4); matrix1 += matrix2; if (matrix1 != result) { return 105; } } { //test matrix subtraction Matrix<double> matrix1(5, 5, 20); Matrix<double> matrix2(5, 5, 50); Matrix<double> result(5, 5, -30); Matrix<double> matrix3 = matrix1 - matrix2; if (matrix3 != result) { return 106; } } { //test cumulative matrix subtraction Matrix<double> matrix1(5, 5, 20); Matrix<double> matrix2(5, 5, 50); Matrix<double> result(5, 5, -30); matrix1 -= matrix2; if (matrix1 != result) { return 107; } } { //test matrix multiplication Matrix<double> matrix1(5, 3, 1); Matrix<double> matrix2(5, 5, 2); Matrix<double> matrix3 = matrix1 * matrix2; Matrix<double> result(5, 3, 15); if (result != matrix3) { return 108; } } { //test cumulative multiplication Matrix<double> matrix1(5, 3, 1); Matrix<double> matrix2(5, 5, 2); matrix1 *= matrix2; Matrix<double> result(5, 3, 15); if (result != matrix1) { return 109; } } { Matrix<string> matrix("sorin"); Matrix<string> result(1, 1, "sorin"); if (result != matrix) { return 110; } } { Matrix<double> matrix1(5, 5, 6); Matrix<double> matrix2(matrix1); if (matrix1 != matrix2) { return 111; } } return 0; }
int main(int argn, char** args){ if (argn !=2 ) { printf("When running the simulation, please give a valid geometry file name!\n"); return 1; } //set the geometry file char *filename = NULL; filename = args[1]; //initialize variables double t = 0; /*time start*/ int it, n = 0; /*iteration and time step counter*/ double res; /*residual for SOR*/ /*arrays*/ double ***U, ***V, ***W, ***P; double ***RS, ***F, ***G, ***H; int ***Flag; //additional data structure for arbitrary geometry int ***S; //additional data structure for arbitrary geometry int matrix_output = 0; /*those to be read in from the input file*/ double Re, UI, VI, WI, PI, GX, GY, GZ, t_end, xlength, ylength, zlength, dt, dx, dy, dz, alpha, omg, tau, eps, dt_value; int imax, jmax, kmax, itermax; //double presLeft, presRight, presDelta; //for pressure stuff ...TODO: not allowed for now int wl, wr, wt, wb, wf, wh; char problemGeometry[200]; //in case of a given inflow or wall velocity TODO: will we have this? needs to be a vector? double velIN; double velMW[3]; // the moving wall velocity is a vector struct particleline *Partlines = (struct particleline *)malloc((unsigned)(1 * sizeof(struct particleline))); //char szFileName[80]; //read the parameters, using problem.dat, including wl, wr, wt, wb read_parameters(filename, &Re, &UI, &VI, &WI, &PI, &GX, &GY, &GZ, &t_end, &xlength, &ylength, &zlength, &dt, &dx, &dy, &dz, &imax, &jmax, &kmax, &alpha, &omg, &tau, &itermax, &eps, &dt_value, &wl, &wr, &wf, &wh, &wt, &wb, problemGeometry, &velIN, &velMW[0]); //&presLeft, &presRight, &presDelta, &vel); //printf("d: %f, %f, %f\n", dx,dy,dz); //int pics = dt_value/dt; //just a helping variable for outputing vtk double last_output_t = -dt_value; //double every = t_end/10; //helping variable. Can be used for displaying info every tenth of the progress during simulation //allocate memory, including Flag U = matrix2(0, imax+1, 0, jmax+1, 0, kmax+1); V = matrix2(0, imax+1, 0, jmax+1, 0, kmax+1); W = matrix2(0, imax+1, 0, jmax+1, 0, kmax+1); P = matrix2(0, imax+1, 0, jmax+1, 0, kmax+1); RS = matrix2(1, imax, 1, jmax, 1, kmax); F = matrix2(0, imax, 1, jmax, 1, kmax); G = matrix2(1, imax, 0, jmax, 1, kmax); H = matrix2(1, imax, 1, jmax, 0, kmax); Flag = imatrix2(0, imax+1, 0, jmax+1, 0, kmax+1); // or Flag = imatrix(1, imax, 1, jmax); S = imatrix2(0, imax+1, 0, jmax+1, 0, kmax+1); //S = Flag -> adjust C_x Flags in helper.h //initialisation, including **Flag init_flag(problemGeometry, imax, jmax, kmax, Flag, wl, wr, wf, wh, wt, wb); //presDelta, Flag); init_particles(Flag,dx,dy,dz,imax,jmax,kmax,3,Partlines); printf("!!!!!!!!%d\n",binMatch(B_NO,B_N)); init_uvwp(UI, VI, WI, PI, Flag,imax, jmax, kmax, U, V, W, P, problemGeometry); write_particles("particles_init",0, 1, Partlines); write_imatrix2("Flag_start.txt",t,Flag, 0, imax, 1, jmax, 1, kmax); //write_flag_imatrix("Flag.txt",t,Flag, 0, imax+1, 0, jmax+1, 0, kmax+1); write_vtkFile(filename, -1, xlength, ylength, zlength, imax, jmax, kmax, dx, dy, dz, U, V, W, P,Flag); //write_vtkFile("init", n, xlength, ylength, zlength, imax, jmax, kmax, dx, dy, dz, U, V, W, P); //going through all time steps /* write_matrix2("P_start.txt",t,P, 0, imax+1, 0, jmax+1, 0, kmax+1); write_matrix2("U_start.txt",t,U, 0, imax+1, 0, jmax+1, 0, kmax+1); write_matrix2("V_start.txt",t,V, 0, imax+1, 0, jmax+1, 0, kmax+1); write_matrix2("W_start.txt",t,W, 0, imax+1, 0, jmax+1, 0, kmax+1); */ //setting bound.values boundaryvalues(imax, jmax, kmax, U, V, W, P, F, G, H, problemGeometry, Flag, velIN, velMW); //including P, wl, wr, wt, wb, F, G, problem // printf("calc bc \n"); while(t < t_end){ /*if(t - every >= 0){ printf("Calculating time %f ... \n", t); every += t; }*/ //adaptive time stepping calculate_dt(Re, tau, &dt, dx, dy, dz, imax, jmax, kmax, U, V, W); // printf("calc dt \n"); /* mark_cells(Flag,dx,dy,dz,imax,jmax,kmax,1,Partlines); set_uvwp_surface(U,V,W,P,Flag,dx,dy,dz,imax,jmax,kmax,GX,GY,GZ,dt,Re); */ //computing F, G, H and right hand side of pressue eq. calculate_fgh(Re, GX, GY, GZ, alpha, dt, dx, dy, dz, imax, jmax, kmax, U, V, W, F, G, H, Flag); // printf("calc fgh \n"); calculate_rs(dt, dx, dy, dz, imax, jmax, kmax, F, G, H, RS,Flag); // printf("calc rs \n"); //iteration counter it = 0; //write_matrix2("RS.txt",t,RS, 1, imax, 1, jmax, 1, kmax); //write_matrix2("F.txt",t,F, 0, imax, 1, jmax, 1, kmax); //write_matrix2("G.txt",t,G, 1, imax, 0, jmax, 1, kmax); //write_matrix2("H.txt",t,H, 1, imax, 1, jmax, 0, kmax); do{ // sprintf( szFileName, "P_%d.txt",it ); //write_matrix2(szFileName,1000+t,P, 0, imax+1, 0, jmax+1, 0, kmax+1); //perform SOR iteration, at same time set bound.values for P and new residual value sor(omg, dx, dy, dz, imax, jmax, kmax, P, RS, &res, Flag); //, presLeft, presRight); it++; }while(it<itermax && res>eps); /*if (it == itermax) { printf("Warning: sor while loop exits because it reaches the itermax. res = %f, time =%f\n", res, t); } */ //write_matrix2("P.txt",t,P, 0, imax+1, 0, jmax+1, 0, kmax+1); //calculate U, V and W of this time step calculate_uvw(dt, dx, dy, dz, imax, jmax, kmax, U, V, W, F, G, H, P, Flag); //write_matrix2("U.txt",t,U, 0, imax+1, 0, jmax+1, 0, kmax+1); //write_matrix2("V.txt",t,V, 0, imax+1, 0, jmax+1, 0, kmax+1); //write_matrix2("W.txt",t,W, 0, imax+1, 0, jmax+1, 0, kmax+1); // printf("calc uvw \n"); //sprintf( szFileName, "simulation/%s.%i_debug.vtk", szProblem, timeStepNumber ); //setting bound.values boundaryvalues(imax, jmax, kmax, U, V, W, P, F, G, H, problemGeometry, Flag, velIN, velMW); //including P, wl, wr, wt, wb, F, G, problem /* set_uvwp_surface(U,V,W,P,Flag,dx,dy,dz,imax,jmax,kmax,GX,GY,GZ,dt,Re); printf("advance_particles!\n"); advance_particles(dx,dy, dz,imax,jmax,kmax, dt,U,V,W,1,Partlines); */ //indent time and number of time steps printf("timer\n"); n++; t += dt; //output of pics for animation if ( t-last_output_t >= dt_value ){ //n%pics==0 ){ printf("output\n!"); write_particles(filename,n, 1, Partlines); write_vtkFile(filename, n, xlength, ylength, zlength, imax, jmax, kmax, dx, dy, dz, U, V, W, P,Flag); printf("output vtk (%d)\n",n); last_output_t = t; matrix_output++; } printf("timestep: %f - next output: %f (dt: %f) \n",t,dt_value- (t-last_output_t),dt); } //output of U, V, P at the end for visualization //write_vtkFile("DrivenCavity", n, xlength, ylength, imax, jmax, dx, dy, U, V, P); //free memory free_matrix2(U, 0, imax+1, 0, jmax+1, 0, kmax+1); free_matrix2(V, 0, imax+1, 0, jmax+1, 0, kmax+1); free_matrix2(W, 0, imax+1, 0, jmax+1, 0, kmax+1); free_matrix2(P, 0, imax+1, 0, jmax+1, 0, kmax+1); free_matrix2(RS, 1, imax, 1, jmax, 1, kmax); free_matrix2(F, 0, imax, 1, jmax, 1, kmax); free_matrix2(G, 1, imax, 0, jmax, 1, kmax); free_matrix2(H, 1, imax, 1, jmax, 0, kmax); free_imatrix2(Flag, 0, imax+1, 0, jmax+1, 0, kmax+1); free_imatrix2(S, 0, imax+1, 0, jmax+1, 0, kmax+1); //printf("\n-\n"); return -1; }
void testMatrix3() { reportTesting("Matrix3 class"); Matrix3 matrix3d = Matrix3( 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f); assertEquals(__LINE__, 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f, matrix3d); //Constructors Matrix3 matrix = Matrix3(matrix3d); assertEquals(__LINE__, matrix, matrix3d); matrix3d = Matrix3(); assertEquals(__LINE__, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, matrix3d); assertEquals(__LINE__, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, Matrix3::newIdentity()); //Getters and setters matrix3d.set( 9.0f, 8.0f, 7.0f, 6.0f, 5.0f, 4.0f, 3.0f, 2.0f, 1.0f); assertEquals(__LINE__, 9.0f, 8.0f, 7.0f, 6.0f, 5.0f, 4.0f, 3.0f, 2.0f, 1.0f, matrix3d); matrix3d(0,0) = 10.0f; matrix3d(0,1) = 11.0f; matrix3d(0,2) = 12.0f; matrix3d(1,0) = 13.0f; matrix3d(1,1) = 14.0f; matrix3d(1,2) = 15.0f; matrix3d(2,0) = 16.0f; matrix3d(2,1) = 17.0f; matrix3d(2,2) = 18.0f; assertEquals(__LINE__, 10.0f, 11.0f, 12.0f, 13.0f, 14.0f, 15.0f, 16.0f, 17.0f, 18.0f, matrix3d); matrix3d = matrix1(); const Matrix3 m = matrix1(); assertEquals(__LINE__, 1.0f, m(0,0)); assertEquals(__LINE__, 2.0f, m(0,1)); assertEquals(__LINE__, 3.0f, m(0,2)); assertEquals(__LINE__, 4.0f, m(0,3)); assertEquals(__LINE__, 5.0f, m(0,4)); assertEquals(__LINE__, 6.0f, m(0,5)); assertEquals(__LINE__, 7.0f, m(0,6)); assertEquals(__LINE__, 8.0f, m(0,7)); assertEquals(__LINE__, 9.0f, m(0,8)); //Operators matrix3d = matrix1(); matrix3d += matrix2(); assertEquals(__LINE__, 2.0f, 11.0f, 5.0f, 12.0f, 8.0f, 13.0f, 11.0f, 14.0f, 14.0f, matrix3d); matrix3d = matrix1(); Matrix3 other = matrix3d + matrix2(); assertEquals(__LINE__, matrix1(), matrix3d); assertEquals(__LINE__, 2.0f, 11.0f, 5.0f, 12.0f, 8.0f, 13.0f, 11.0f, 14.0f, 14.0f, other); matrix3d = matrix1(); matrix3d -= matrix2(); assertEquals(__LINE__, 0.0f, -7.0f, 1.0f, -4.0f, 2.0f, -1.0f, 3.0f, 2.0f, 4.0f, matrix3d); matrix3d = matrix1(); other = matrix3d - matrix2(); assertEquals(__LINE__, matrix1(), matrix3d); assertEquals(__LINE__, 0.0f, -7.0f, 1.0f, -4.0f, 2.0f, -1.0f, 3.0f, 2.0f, 4.0f, other); matrix3d = matrix1(); matrix3d *= Matrix3( 9.0f, 8.0f, 7.0f, 6.0f, 10.0f, 4.0f, 3.0f, 2.0f, 1.0f); assertEquals(__LINE__, 30.0f, 34.0f, 18.0f, 84.0f, 94.0f, 54.0f, 138.0f, 154.0f, 90.0f, matrix3d); matrix3d = matrix1(); other = matrix3d * Matrix3( 9.0f, 8.0f, 7.0f, 6.0f, 10.0f, 4.0f, 3.0f, 2.0f, 1.0f); assertEquals(__LINE__, matrix1(), matrix3d); assertEquals(__LINE__, 30.0f, 34.0f, 18.0f, 84.0f, 94.0f, 54.0f, 138.0f, 154.0f, 90.0f, other); matrix3d = matrix1(); matrix3d *= 3; assertEquals(__LINE__, 3.0f, 6.0f, 9.0f, 12.0f, 15.0f, 18.0f, 21.0f, 24.0f, 27.0f, matrix3d); matrix3d = matrix1(); other = matrix1() * 3; assertEquals(__LINE__, 3.0f, 6.0f, 9.0f, 12.0f, 15.0f, 18.0f, 21.0f, 24.0f, 27.0f, other); matrix3d = matrix1(); other = 2 * matrix1(); assertEquals(__LINE__, 2.0f, 4.0f, 6.0f, 8.0f, 10.0f, 12.0f, 14.0f, 16.0f, 18.0f, other); //Methods matrix3d.set( 5, 0, 1, -2, 3, 4, 0, 2, -1); assertEquals(__LINE__, -59.0f, matrix3d.determinant()); assertTrue(__LINE__, matrix3d.isInvertible()); matrix3d.set( 1, 3, 10, -1, 1, 10, 0, 2, 10); assertEquals(__LINE__, 0.0f, matrix3d.determinant()); assertTrue(__LINE__, !matrix3d.isInvertible()); matrix3d.set( 3, 0, 2, 9, 1, 7, 1, 0, 1); matrix3d.inverse(); assertEquals(__LINE__, 1.0f, 0.0f, -2.0f, -2.0f, 1.0f, -3.0f, -1.0f, 0.0f, 3.0f, matrix3d); matrix3d = matrix1(); matrix3d.transpose(); assertEquals(__LINE__, 1.0f, 4.0f, 7.0f, 2.0f, 5.0f, 8.0f, 3.0f, 6.0f, 9.0f, matrix3d); //Auxiliary functions matrix3d = matrix1(); other = transpose(matrix3d); assertEquals(__LINE__, matrix1(), matrix3d); assertEquals(__LINE__, 1.0f, 4.0f, 7.0f, 2.0f, 5.0f, 8.0f, 3.0f, 6.0f, 9.0f, other); matrix3d.set( 3, 0, 2, 9, 1, 7, 1, 0, 1); other = inverse(matrix3d); assertEquals(__LINE__, 3, 0, 2, 9, 1, 7, 1, 0, 1, matrix3d); assertEquals(__LINE__, 1.0f, 0.0f, -2.0f, -2.0f, 1.0f, -3.0f, -1.0f, 0.0f, 3.0f, other); }
int main(int argc, char *argv[]) { if (argc != 2) { puts("\a\n usage: math_tst matrix_file_name\n"); exit( 1 ); } ::printf("\n\n------------- MACHINE (CPU) DEPENDENT THINGS --------------\n\n"); // defining machine epsilon for different built in data types supported by C++ float float_eps = f_macheps(); // or use float.h values FLT_EPSILON double double_eps = d_macheps(); // or use float.h values DBL_EPSILON //long double long_double_eps = ld_macheps(); // or use float.h values LDBL_EPSILON ::printf("\n float macheps = %.20e \n",float_eps); ::printf(" double macheps = %.20e \n",double_eps); //::printf(" long double macheps = %.20Le \n\n\n",long_double_eps); // Usual tolerance is defined as square root of machine epsilon float sqrt_f_eps = sqrt(f_macheps()); double sqrt_d_eps = sqrt(d_macheps()); //long double sqrt_ld_eps = sqrt(ld_macheps()); ::printf("\n sqrt float macheps = %.20e \n",sqrt_f_eps); ::printf(" sqrt double macheps = %.20e \n",sqrt_d_eps); //::printf(" sqrt long double macheps = %.20Le \n",sqrt_ld_eps); ::printf("\n\n---------------- MATRIX -----------------\n\n"); // New data type MATRIX is defined in such a way that it can be regarded // as concrete data type. // default constructor: BJmatrix matrix1; matrix1.print("m1","matrix1"); static double initvalue[] = {1.0, 2.0, 3.0, 4.0}; // constructor with matrix initialization BJmatrix matrix2(2,2,initvalue); matrix2.print("m2","matrix2"); // unit matrix initialization ( third order ) BJmatrix matrix3("I",3); matrix3.print("m3","matrix3"); // matrix initialization from the file that is in predefined format! BJmatrix m(argv[1]); m.print("m","m is"); //==== // matrix assignment BJmatrix first_matrix = m; first_matrix.print("fm","first_matrix is"); // --------------------------------- ::printf("\n\n--------EIGENVECTORS and EIGENVALUES-----------\n"); static double symm_initvalue[] = {1.0, 0.0, 0.0, 0.0}; // constructor with matrix initialization BJmatrix sym_matrix(2,2,symm_initvalue); sym_matrix.print("m","Sym_matrix"); // Eigenvalues BJvector eval = sym_matrix.eigenvalues(); eval.print("ev","Eigenvalues of sym_matrix"); // Eigenvectors BJmatrix evect = sym_matrix.eigenvectors(); evect.print("ev","Eigenvectors of sym_matrix"); // matrix to file "test.$$$" m.write_standard("test.$$$", "this is a test"); // equivalence of two matrices ( within tolerance sqrt(macheps) ) if ( first_matrix == m ) ::printf("\n first_matrix == m TRUE \n"); else ::printf("\a\n first_matrix == m NOTTRUE \n"); // matrix addition BJmatrix m2 =m+m; m2.print("m2","\nm+m = \n"); // equivalence of two matrices ( within tolerance sqrt(macheps) ) if ( m2 == m ) ::printf("\a\n m2 == m TRUE \n"); else ::printf("\n m2 == m NOTTRUE \n"); // matrix substraction BJmatrix m3 =m-m; m3.print("m3","\nm-m = \n"); // matrix multiplication BJmatrix m4 = m*m; m4.print("m4","\n m*m = \n"); // matrix transposition BJmatrix mtran = m.transpose(); mtran.print("mt","transpose of m is"); // determinant of matrix printf("determinant = %6.6f \n",m.determinant()); // matrix inversion BJmatrix minv = m.inverse(); minv.print("mi","inverse of m is"); // check matrix inversion ( m * minv ).print("1"," m * m.inverse() "); // minima, maxima, mean and variance values for matrix: printf("min = %6.6f \n", m.mmin() ); printf("max = %6.6f \n", m.mmax() ); printf("mean = %6.6f \n", m.mean() ); printf("variance = %6.6f \n", m.variance() ); //====//===printf("\n\n----------------- SKY MATRIX ----------------------\n\n"); //====//=== //====//===// skymatrix as defined by K.J. Bathe in "FE procedures in Engineering Analysis" //====//===// The example is from that book too! //====//=== //====//===int maxa[] = { 1, 2, 4, 6, 8, 13}; //====//===double a[] = {2.0, 3.0, -2.0, 5.0, -2.0, 10.0, -3.0, 10.0, 4.0, 0.0, 0.0, -1.0}; //====//===double rhs[] = { 0.0, 1.0, 0.0, 0.0, 0.0 }; //====//=== //====//===// skymatrix constructor //====//=== skymatrix first( 5, maxa, a); //====//=== //====//===// simple print functions: //====//=== first.full_print("\nfirst Sparse Skyline Matrix as FULL :\n "); //====//=== first.lower_print("\nfirst Sparse Skyline Matrix :\n "); //====//=== first.upper_print("\nfirst Sparse Skyline Matrix :\n "); //====//=== //====//===// skymatrix assignment //====//=== skymatrix second = first; //====//=== second.full_print("\nsecond Sparse Skyline Matrix as FULL :\n "); //====//=== //====//===// skymatrix third; // not defined ! //====//===// assignments: //====//=== skymatrix third = second = first; //====//=== //====//===// LDL factorization ( see K.J. Bathe's book! ) //====//=== first = first.v_ldl_factorize(); //====//=== //====//===// simple print functions: //====//=== first.lower_print("\n lower first but factorized :\n "); //====//=== first.full_print("\nfirst but factorized Sparse Skyline Matrix as FULL :\n "); //====//=== first.upper_print("\n upper first but factorized :\n "); //====//=== //====//===// right hand side reduction ( see K.J. Bathe's book! ) ( vetor of unknowns is rhs ) //====//=== first.d_reduce_r_h_s_l_v( rhs ); //====//=== int i=0; //====//=== for( i=0 ; i<5 ; i++ ) //====//=== { //====//=== printf("intermediate_rhs[%d] = %8.4f\n",i, rhs[i]); //====//=== } //====//=== //====//===// backsubstitution //====//=== first.d_back_substitute ( rhs ); //====//=== for( i=0 ; i<5 ; i++ ) //====//=== { //====//=== printf("solution_rhs[%d] = %8.4f\n",i, rhs[i]); //====//=== } //====//=== //====//===// minima, maxima and mean values for skymatrix: //====//=== printf("min = %6.6f \n", first.mmin() ); //====//=== printf("max = %6.6f \n", first.mmax() ); //====//=== printf("mean = %6.6f \n", first.mean() ); //====//===// //====//===// //====//=== //====//===printf("\n\n----------------- PROFILE MATRIX ----------------------\n"); //====//===printf("----------------- EXAMPLE 1 ----------------------\n\n"); //====//=== //====//===// profilematrix as defined by O.C. Zienkiewicz and R.L. Taylor //====//===// in "The FEM - fourth edition" //====//===// The example is from: K.J. Bathe: "FE procedures in Engineering Analysis" //====//=== //====//===int jp[] = { 0, 1, 2, 3, 7}; //====//===double au[] = { -2.0, -2.0, -3.0, -1.0, 0.0, 0.0, 4.0}; //====//===double ad[] = { 2.0, 3.0, 5.0, 10.0, 10.0 }; //====//===double* al = au; //====//===double b[] = { 0.0, 1.0, 0.0, 0.0, 0.0 }; //====//=== //====//===// profile matrix constructor //====//=== profilematrix first_profile( 5, jp, 'S', au, al, ad); //====//=== //====//===// simple print function //====//=== first_profile.full_print("\nfirst_profile Sparse Profile Matrix as FULL :\n "); //====//=== //====//===// asignment operetor //====//=== profilematrix second_profile = first_profile; //====//=== second_profile.full_print("\nsecond_profile Sparse Profile Matrix as FULL :\n "); //====//=== //====//===// triangularization ( Crout's method ); see: O.C. Zienkiewicz and R.L. Taylor: //====//===// "The FEM - fourth edition" //====//=== first_profile = first_profile.datri(); //====//=== first_profile.full_print("\nfirst but factorized Sparse Profile Matrix as FULL :\n "); //====//=== ::printf("\n\n"); //====//=== //====//===// right hand side reduction and backsubstitution ( vector of unknowns is b ) //====//=== first_profile.dasol(b); //====//=== for( i=0 ; i<5 ; i++ ) //====//=== { //====//=== printf("solution_b[%d] = %8.4f\n",i, b[i]); //====//=== } //====//=== //====//=== printf("\n\nmin = %6.6f \n", first_profile.mmin() ); //====//=== printf("max = %6.6f \n", first_profile.mmax() ); //====//=== printf("mean = %6.6f \n", first_profile.mean() ); //====//=== //====//=== //====//===printf("\n----------------- EXAMPLE 2 ----------------------\n\n"); //====//=== //====//===// The example is from: O.C. Zienkiewicz and R.L. Taylor: //====//===// "The FEM - fourth edition" //====//===int jp2[] = { 0, 1, 3}; //====//===double au2[] = { 2.0, 1.0, 2.0}; //====//===double ad2[] = { 4.0, 4.0, 4.0}; //====//===double* al2 = au2; //====//===double b2[] = { 0.0, 0.0, 1.0 }; //====//=== //====//===// profile matrix constructor //====//=== profilematrix first_profile2( 3, jp2, 'S', au2, al2, ad2); //====//=== //====//===// simple print function //====//=== first_profile2.full_print("\nfirst_profile Sparse Profile Matrix as FULL :\n "); //====//=== //====//===// asignment operetor //====//=== profilematrix second_profile2 = first_profile2; //====//=== second_profile2.full_print("\nsecond_profile Sparse Profile Matrix as FULL :\n "); //====//=== //====//===// triangularization ( Crout's method ); see: O.C. Zienkiewicz and R.L. Taylor: //====//===// "The FEM - fourth edition" //====//=== profilematrix first_profile2TRI = first_profile2.datri(); //====//=== first_profile2TRI.full_print("\nfirst but factorized Sparse Profile Matrix as FULL :\n "); //====//=== ::printf("\n\n"); //====//=== //====//===// right hand side reduction and backsubstitution ( vector of unknowns is b2 ) //====//=== first_profile2TRI.dasol( b2 ); //====//=== for( i=0 ; i<3 ; i++ ) //====//=== { //====//=== printf("solution_b[%d] = %8.4f\n",i, b2[i]); //====//=== } //====//=== //====//=== printf("\n\nmin = %6.6f \n", first_profile2.mmin() ); //====//=== printf("max = %6.6f \n", first_profile2.mmax() ); //====//=== printf("mean = %6.6f \n", first_profile2.mean() ); //====//=== //====//=== //====//===printf("\n----------------- EXAMPLE 3 ----------------------\n\n"); //====//=== //====//===// The main advantage of profile solver as described in //====//===// O.C. Zienkiewicz and R.L. Taylor: "The FEM - fourth edition" //====//===// is that it supports nonsymetric matrices stored in profile format. //====//=== //====//===int jp3[] = { 0, 1, 3}; //====//===double au3[] = { 2.0, 3.0, 2.0}; //====//===double ad3[] = { 4.0, 4.0, 4.0}; //====//===double al3[] = { 2.0, 1.0, 2.0}; //====//===double b3[] = { 1.0, 1.0, 1.0 }; //====//=== //====//===// nonsymetric profile matrix constructor //====//=== profilematrix first_profile3( 3, jp3, 'N', au3, al3, ad3); //====//=== //====//===// print out //====//=== first_profile3.full_print("\nfirst_profile Sparse Profile Matrix as FULL :\n "); //====//=== //====//===// triangularization ( Crout's method ); see: O.C. Zienkiewicz and R.L. Taylor: //====//===// "The FEM - fourth edition" //====//=== profilematrix first_profile3TRI = first_profile3.datri(); //====//=== first_profile3TRI.full_print("\nfirst but factorized \ //====//=== Sparse Profile Matrix as FULL :\n "); //====//=== ::printf("\n\n"); //====//=== //====//===// right hand side reduction and backsubstitution ( vector of unknowns is b3 ) //====//=== first_profile3TRI.dasol( b3 ); //====//=== for( i=0 ; i<3 ; i++ ) //====//=== { //====//=== printf("solution_b[%d] = %8.4f\n",i, b3 //====//=== [i]); //====//=== } //====//=== //====//=== printf("\n\nmin = %6.6f \n", first_profile3.mmin() ); //====//=== printf("max = %6.6f \n", first_profile3.mmax() ); //====//=== printf("mean = %6.6f \n", first_profile3.mean() ); //====//=== //====//=== //====printf("\n\n------------ VECTOR -------------------\n\n"); static double vect1[] = {2.0, 3.0, -2.0, 5.0, -2.0}; static double vect2[] = {1.0, 1.0, 1.0, 1.0, 1.0}; // vector constructor BJvector first_vector( 5, vect1); first_vector.print("f","\n\nfirst_vector vector\n"); // vector constructor BJvector second_vector ( 5, vect2); second_vector.print("s","\n\nsecond_vector vector\n"); // default constructor for vector BJvector third_vector; // vector assignment third_vector = first_vector + second_vector; third_vector.print("t","\nfirst_vector + second_vector = \n"); BJvector dot_product = first_vector.transpose() * second_vector; // vector dot product; // dot_product = first_vector.transpose() * second_vector; //==== dot_product.print("d","\nfirst_vector * second_vector \n"); //==== printf("\n\n------------VECTOR AND MATRIX -----------------\n\n"); static double vect3[] = {1.0, 2.0, 3.0, 4.0, 5.0}; //default construcotrs matrix res1; matrix res2; // matrix constructor matrix vect_as_matrix(5, 1, vect3); BJvector vect(5, vect3); first_matrix.print("f","\n first matrix"); // vector and matrix are quite similar ! vect_as_matrix.print("vm","\n vector as matrix"); vect.print("v","\n vector"); // this should give the same answer! ( vector multiplication with matrix res1 = vect_as_matrix.transpose() * first_matrix; res2 = vect.transpose() * first_matrix; res1.print("r1","\n result"); res2.print("r2","\n result"); ::printf("\n\n----------------- TENSOR --------------------\n\n"); // tensor default construcotr tensor t1; t1.print("t1","\n tensor t1"); static double t2values[] = { 1,2,3, 4,5,6, 7,8,9 }; // tensor constructor with values assignments ( order 2 ; like matrix ) tensor t2( 2, def_dim_2, t2values); t2.print("t2","\ntensor t2 (2nd-order with values assignement)"); tensor tst( 2, def_dim_2, t2values); // t2 = t2*2.0; // t2.print("t2x2","\ntensor t2 x 2.0"); t2.print("t2","\noriginal tensor t2 "); tst.print("tst","\ntst "); tensor t2xt2 = t2("ij")*t2("jk"); t2xt2.print("t2xt2","\ntensor t2 x t2"); tensor t2xtst = t2("ij")*tst("jk"); t2xtst.print("t2xtst","\ntensor t2 x tst"); t2.print("t2","\noriginal tensor t2 "); static double Tvalues[] = { 1,2,3, 4,5,6, 7,8,9 }; tensor multTest( 2, def_dim_2, t2values); double a = 5.0; double b = a * multTest("ab"): // tensor constructor with 0.0 assignment ( order 4 ; 4D array ) tensor ZERO(4,def_dim_4,0.0); ZERO.print("z","\ntensor ZERO (4-th order with value assignment)"); static double t4val[] = { 11.100, 12.300, 13.100, 15.230, 16.450, 14.450, 17.450, 18.657, 19.340, 110.020, 111.000, 112.030, 114.034, 115.043, 113.450, 116.670, 117.009, 118.060, 128.400, 129.500, 130.060, 132.560, 133.000, 131.680, 134.100, 135.030, 136.700, 119.003, 120.400, 121.004, 123.045, 124.023, 122.546, 125.023, 126.043, 127.000, 137.500, 138.600, 139.000, 141.067, 142.230, 140.120, 143.250, 144.030, 145.900, 146.060, 147.700, 148.990, 150.870, 151.000, 149.780, 152.310, 153.200, 154.700, 164.050, 165.900, 166.003, 168.450, 169.023, 167.067, 170.500, 171.567, 172.500, 155.070, 156.800, 157.067, 159.000, 160.230, 158.234, 161.470, 162.234, 163.800, 173.090, 174.030, 175.340, 177.060, 178.500, 176.000, 179.070, 180.088, 181.200}; // tensor constructor with values assignment ( order 4 ; 4D array ) tensor t4(4, def_dim_4, t4val); t4.print("t4","\ntensor t4 (4th-order with values assignment)"); // Some relevant notes from the Dec. 96 Draft (as pointed out by KAI folks): // // "A binary operator shall be implemented either by a non-static member // function (_class.mfct_) with one parameter or by a non-member function // with two parameters. Thus, for any binary operator @, x@y can be // interpreted as either x.operator@(y) or operator@(x,y)." [13.5.2] // // "The order of evaluation of arguments [of a function call] is // unspecified." [5.2.2 paragraph 9] // // // SO THAT IS WHY I NEED THIS temp // It is not evident which operator will be called up first: operator() or operator* . // //% //////////////////////////////////////////////////////////////////////// // testing the multi products using indicial notation: tensor tst1; tensor temp = t2("ij") * t4("ijkl"); tst1 = temp("kl") * t4("klpq") * t2("pq"); tst1.print("tst1","\ntensor tst1 = t2(\"ij\")*t4(\"ijkl\")*t4(\"klpq\")*t2(\"pq\");"); // testing inverse function for 4. order tensor. Inverse for 4. order tensor // is done by converting that tensor to matrix, inverting that matrix // and finally converting matrix back to tensor tensor t4inv_2 = t4.inverse_2(); t4inv_2.print("t4i","\ntensor t4 inverted_2"); tensor unity_2 = t4("ijkl")*t4inv_2("klpq"); unity_2.print("uno2","\ntensor unity_2"); //.................................................................... // There are several built in tensor types. One of them is: // Levi-Civita permutation tensor tensor e("e",3,def_dim_3); e.print("e","\nLevi-Civita permutation tensor e"); // The other one is: // Kronecker delta tensor tensor I2("I", 2, def_dim_2); I2.print("I2","\ntensor I2 (2nd order unit tensor: Kronecker Delta -> d_ij)"); // tensor multiplications tensor I2I2 = I2("ij")*I2("ij"); I2I2.print("I2I2","\ntensor I2 * I2"); // tensor multiplications ( from right) tensor I2PI = I2*PI; I2PI.print("I2PI","\ntensor I2 * PI"); // tensor multiplications ( from left) tensor PII2 = PI*I2; PII2.print("PII2","\ntensor PI * I2"); // Unary minus tensor mPII2 = -PII2; mPII2.print("mPII2","\ntensor -PI * I2"); // trace function double deltatrace = I2.trace(); ::printf("\ntrace of Kronecker delta is %f \n",deltatrace); // determinant of 2. order tensor only! double deltadet = I2.determinant(); ::printf("\ndeterminant of Kronecker delta is %f \n",deltadet); // comparison of tensor ( again within sqrt(macheps) tolerance ) tensor I2again = I2; if ( I2again == I2 ) ::printf("\n I2again == I2 TRUE (OK)\n"); else ::printf("\a\n\n\n I2again == I2 NOTTRUE \n\n\n"); //.................................................................... // some of the 4. order tensor used in conituum mechanics: // the most general representation of the fourth order isotropic tensor // includes the following fourth orther unit isotropic tensors: tensor I_ijkl( 4, def_dim_4, 0.0 ); I_ijkl = I2("ij")*I2("kl"); // I_ijkl.print("ijkl","\ntensor I_ijkl = d_ij d_kl)"); tensor I_ikjl( 4, def_dim_4, 0.0 ); // I_ikjl = I2("ij")*I2("kl"); I_ikjl = I_ijkl.transpose0110(); // I_ikjl.print("ikjl","\ntensor I_ikjl) "); tensor I_ikjl_inv_2 = I_ikjl.inverse_2(); // I_ikjl_inv_2.print("I_ikjl","\ntensor I_ikjl_inverse_2)"); if ( I_ikjl == I_ikjl_inv_2 ) ::printf("\n\n I_ikjl == I_ikjl_inv_2 !\n"); else ::printf("\a\n\n I_ikjl != I_ikjl_inv_2 !\n"); tensor I_iljk( 4, def_dim_4, 0.0 ); // I_ikjl = I2("ij")*I2("kl"); //.. I_iljk = I_ikjl.transpose0101(); I_iljk = I_ijkl.transpose0111(); // I_iljk.print("iljk","\ntensor I_iljk) "); // To check out this three fourth-order isotropic tensor please see // W.Michael Lai, David Rubin, Erhard Krempl // " Introduction to Continuum Mechanics" // QA808.2 // ISBN 0-08-022699-X //.................................................................... // Multiplications between 4. order tensors ::printf("\n\n intermultiplications \n"); // tensor I_ijkl_I_ikjl = I_ijkl("ijkl")*I_ikjl("ikjl"); tensor I_ijkl_I_ikjl = I_ijkl("ijkl")*I_ikjl("ijkl"); I_ijkl_I_ikjl.print("i","\n\n I_ijkl*I_ikjl \n"); // tensor I_ijkl_I_iljk = I_ijkl("ijkl")*I_iljk("iljk"); tensor I_ijkl_I_iljk = I_ijkl("ijkl")*I_iljk("ijkl"); I_ijkl_I_iljk.print("i","\n\n I_ijkl*I_iljk \n"); // tensor I_ikjl_I_iljk = I_ikjl("ikjl")*I_iljk("iljk"); tensor I_ikjl_I_iljk = I_ikjl("ijkl")*I_iljk("ijkl"); I_ikjl_I_iljk.print("i","\n\n I_ikjl*I_iljk \n"); //.................................................................... // More multiplications between 4. order tensors ::printf("\n\n intermultiplications same with same \n"); tensor I_ijkl_I_ijkl = I_ijkl("ijkl")*I_ijkl("ijkl"); I_ijkl_I_ijkl.print("i","\n\n I_ijkl*I_ijkl \n"); tensor I_ikjl_I_ikjl = I_ikjl("ikjl")*I_ikjl("ikjl"); I_ikjl_I_ikjl.print("i","\n\n I_ikjl*I_ikjl \n"); tensor I_iljk_I_iljk = I_iljk("iljk")*I_iljk("iljk"); I_iljk_I_iljk.print("i","\n\n I_iljk*I_iljk \n"); // tensor additions ans scalar multiplications // symmetric part of fourth oder unit isotropic tensor tensor I4s = (I_ikjl+I_iljk)*0.5; // tensor I4s = 0.5*(I_ikjl+I_iljk); I4s.print("I4s","\n symmetric tensor I4s = (I_ikjl+I_iljk)*0.5 "); // skew-symmetric part of fourth oder unit isotropic tensor tensor I4sk = (I_ikjl-I_iljk)*0.5; I4sk.print("I4sk","\n skew-symmetric tensor I4sk = (I_ikjl-I_iljk)*0.5 "); // equvalence check ( they should be different ) if ( I4s == I4sk ) ::printf("\n\n I4s == I4sk !\n"); else ::printf("\a\n\n I4s != I4sk !\n"); //.................................................................... // e-delta identity ( see Lubliner ( QA931 .L939 1990) page 2. // and Lai ( QA808.2 L3 1978) page 12 ) tensor ee = e("ijm")*e("klm"); ee.print("ee","\ntensor e_ijm*e_klm"); tensor id = ee - (I_ikjl - I_iljk); if ( id == ZERO ) ::printf("\n\n e-delta identity HOLDS !! \n\n"); tensor ee1 = e("ilm")*e("jlm"); ee1.print("ee1","\n\n e(\"ilm\")*e(\"jlm\") \n"); tensor ee2 = e("ijk")*e("ijk"); ee2.print("ee2","\n\n e(\"ijk\")*e(\"ijk\") \n"); //.................................................................... // Linear Isotropic Elasticity Tensor // building elasticity tensor double Ey = 20000; double nu = 0.2; // building stiffness tensor in one command line tensor E1 = (I_ijkl*((Ey*nu*2.0)/(2.0*(1.0+nu)*(1-2.0*nu)))) + ( (I_ikjl+I_iljk)*(Ey/(2.0*(1.0+nu)))); // building compliance tensor in one command line tensor D1= (I_ijkl * (-nu/Ey)) + ( (I_ikjl+I_iljk) * ((1.0+nu)/(2.0*Ey))); // multiplication between them tensor test = E1("ijkl")*D1("klpq"); test.print("t","\n\n\n test = E1(\"ijkl\")*D1(\"klpq\") \n"); // and this should be equal to the symmetric part of 4. order unit tensor if ( test == I4s ) ::printf("\n test == I4s TRUE ( up to sqrt(macheps())) \n"); else ::printf("\a\n\n\n test == I4s NOTTRUE ( up to sqrt(macheps())) \n\n\n"); //............Different Way........................................... // Linear Isotropic Elasticity Tensor double lambda = nu * Ey / (1. + nu) / (1. - 2. * nu); double mu = Ey / (2. * (1. + nu)); ::printf("\n lambda = %.12e\n",lambda); ::printf(" mu = %.12e\n",mu); ::printf("\nYoung Modulus = %.4e",Ey); ::printf("\nPoisson ratio = %.4e\n",nu); ::printf( " lambda + 2 mu = %.4e\n", (lambda + 2 * mu)); // building elasticity tensor tensor E2 = (I_ijkl * lambda) + (I4s * (2. * mu)); E2.print("E2","tensor E2: elastic moduli Tensor = lambda*I2*I2+2*mu*I4s"); tensor E2inv = E2.inverse(); // building compliance tensor tensor D2= (I_ijkl * (-nu/Ey)) + (I4s * (1./(2.*mu))); D2.print("D2","tensor D2: compliance Tensor (I_ijkl * (-nu/Ey)) + (I4s * (1./2./mu))"); if ( E2inv == D2 ) ::printf("\n E2inv == D2 TRUE \n"); else ::printf("\a\n\n\n E2inv == D2 NOTTRUE \n\n\n"); if ( E1 == E2 ) ::printf("\n E1 == E2 TRUE \n"); else ::printf("\a\n\n\n E1 == E2 NOTTRUE \n\n\n"); if ( D1 == D2 ) ::printf("\n D1 == D2 TRUE \n"); else ::printf("\a\n\n\n D1 == D2 NOTTRUE \n\n\n"); // -------------- ::printf("\n\n\n --------------------------TENSOR SCALAR MULTIPLICATION ----\n\n"); tensor EEEE1 = E2; tensor EEEE2 = EEEE1*2.0; EEEE1.print("E1","tensor EEEE1"); EEEE2.print("E2","tensor EEEE2"); //###printf("\n\n------------VECTOR AND SKYMATRIX -----------------\n\n"); //###// 1) use of member function full_val is of crucial importance here //###// and it enable us to use skymatrix as if it is full matrix. //###// There is however small time overhead but that can be optimized . . . //###double vect4[] = {1.0, 2.0, 3.0, 4.0, 5.0}; //### //###matrix res2; //###vector vector2(5, vect4); //###second.full_print(); //###vector2.print(); //###res2 = vector2.transpose() * second; //###res2.print(); //### //### //###printf("\n\n------------ MATRIX AND SKYMATRIX -----------------\n\n"); //###// 1) use of member function full_val is of crucial importance here //###// and it enable us to use skymatrix as if it is full matrix. //###// There is however small time overhead but that can be optimized . . . //### //###matrix big_result; //###big_result = first_matrix * second; //###big_result.print(); exit(1); // return 1; }