void testApp::calcTransforms() { if (camera[0].calib.isReady && camera[1].calib.isReady) { Mat tra, rot; if (!camera[0].calib.getTransformation(camera[1].calib, rot, tra)) return; matPosRotFromXtoOther[0] = makeMatrix(rot, tra); camera[1].calib.getTransformation(camera[0].calib, rot, tra); matPosRotFromXtoOther[1] = makeMatrix(rot, tra); } }
/* invert a matrix using Gaussian Elimination */ Matrix invertRREF(Matrix m) { int prealloc = alloc_matrix; int i,j; Matrix tmp = makeZeroMatrix(m->row_dim, m->col_dim*2); Matrix inverse = makeMatrix(m->row_dim, m->col_dim); DEBUG_CHECK(m->row_dim == m->col_dim,"Matrix can only be inverted if it is square"); /* build the tmp Matrix which will be passed to RREF */ for( i = 0; i < m->row_dim; i++) { for( j = 0; j < m->col_dim; j++) { ME(tmp,i,j) = ME(m,i,j); if(i == j) { ME(tmp,i,j+m->col_dim) = 1; } } } matrixRREF(tmp); for( i = 0; i < m->row_dim; i++) { for( j = 0; j < m->col_dim; j++) { ME(inverse,i,j) = ME(tmp,i,j+m->col_dim); } } freeMatrix(tmp); if(prealloc != alloc_matrix - 1) { printf("Error deallocating matricies <%s>: pre=%d post=%d",__FUNCTION__, prealloc, alloc_matrix); exit(1); } return inverse; }
void Calibration::draw3d(int i) const { ofPushStyle(); ofPushMatrix(); ofNoFill(); applyMatrix(makeMatrix(boardRotations[i], boardTranslations[i])); ofSetColor(ofColor::fromHsb(255 * i / size(), 255, 255)); ofDrawBitmapString(ofToString(i), 0, 0); for(int j = 0; j < (int)objectPoints[i].size(); j++) { ofPushMatrix(); ofTranslate(toOf(objectPoints[i][j])); ofCircle(0, 0, .5); ofPopMatrix(); } ofMesh mesh; mesh.setMode(OF_PRIMITIVE_LINE_STRIP); for(int j = 0; j < (int)objectPoints[i].size(); j++) { ofVec3f cur = toOf(objectPoints[i][j]); mesh.addVertex(cur); } mesh.draw(); ofPopMatrix(); ofPopStyle(); }
/*=========================================================================== * eigenvector * This algorithm determines the eigenvector of a matrix given an eigenvalue. *=========================================================================*/ matrix* eigenvector(matrix* a, double eigenvalue) { matrix* b; // This matrix will store A-eI matrix* zero; // This matrix will store a column vector of zeros matrix* out; double* ptr; int i; assert(a->width == a->height, "Matrix must be square."); // Create our column vector of zeros zero = makeMatrix(1, a->height); // Copy A b = copyMatrix(a); // Subtract eigenvalue from the diagonal elements ptr = b->data; for (i = 0; i < b->height; i++) { *ptr -= eigenvalue; ptr += b->width + 1; } // Find the eigenvector out = solver(b, zero); freeMatrix(b); freeMatrix(zero); return out; }
/*=========================================================================== * powerMethod * This algorithm determines the largest eigenvalue of a matrix using the * power method. * * This was described to me in a Randomized Algoirthms course. *=========================================================================*/ double powerMethod(matrix* a) { matrix* x; matrix* xp1; // x plus 1 const double EPSILON = 0.001; double sum; int i; int k = 0; int converge; assert(a->width == a->height, "Matrix must be square."); srand(time(0)); // Initalize our RNG // Let's initalize x to a random vector x = makeMatrix(1, a->height); for (i = 0; i < a->height; i++) { x->data[i] = (double) rand() / RAND_MAX; } // Iterate until the x vector converges. while (1) { k++; // Multiply A * x xp1 = multiplyMatrix(a, x); // Add up all of the values in xp1 sum = 0; for (i = 0; i < a->height; i++) { sum += xp1->data[i]; } // Divide each value in xp1 by sum. (Normalize) for (i = 0; i < a->height; i++) { xp1->data[i] /= sum; } // Test to see if we need to quit. converge = 1; // Converged for (i = 0; i < a->height; i++) { if (fabs(x->data[i] - xp1->data[i]) >= EPSILON) { converge = 0; // Not converged. break; } } // Set up for the next loop. freeMatrix(x); x = copyMatrix(xp1); freeMatrix(xp1); // Really test for quit. if (converge == 1) { break; } } freeMatrix(x); return sum; }
void ofxMapamok::setData(cv::Mat1d cameraMatrix, cv::Mat rotation, cv::Mat translation, cv::Size2i imageSize, cv::Mat distortionCoefficients) { rvec = rotation; tvec = translation; intrinsics.setup(cameraMatrix, imageSize); modelMatrix = makeMatrix(rvec, tvec); distCoeffs = distortionCoefficients; useDistortionShader = false; for (int i = 0; i < distortionCoefficients.cols; i++) { if (distortionCoefficients.at<double>(i) != 0.0) { useDistortionShader = true; break; } } if (useDistortionShader) { distortionShader.begin(); distortionShader.setUniform3f("k", ofVec3f(distortionCoefficients.at<double>(0), distortionCoefficients.at<double>(1), distortionCoefficients.at<double>(4))); distortionShader.setUniform2f("p", ofVec2f(distortionCoefficients.at<double>(2), distortionCoefficients.at<double>(3))); distortionShader.setUniform2f("focalLength", ofVec2f(cameraMatrix.at<double>(0, 0), cameraMatrix.at<double>(1, 1))); distortionShader.setUniform2f("principalPoint", ofVec2f(cameraMatrix.at<double>(0, 2), cameraMatrix.at<double>(1, 2)) ); distortionShader.end(); } calibrationReady = true; }
int main(int argc, char *argv[]) { if(argc < 2) { printf("Usage: matrix-thread nThreads\n"); return -1; } numThreads = atoi(argv[1]); A = readMatrix(IN_1); B = readMatrix(IN_2); if(A->numCols != B->numRows) { fprintf(stderr, "Wrong matrix dimensions\n"); return -1; } C = makeMatrix(A->numRows, B->numCols); dispatchThreads(); writeMatrix(OUTPUT_FILE, C); freeMatrix(A); freeMatrix(B); freeMatrix(C); return 0; }
/** * Creates a matrix and initializes the elements as per the given order * throws: ALLOCATION_FAILURE */ matrix::matrix(int m, int n, const float *ele_list) { if(isOrderValid(m, n)){ //store the required order row = m; column = n; //Create space for mxn elements makeMatrix(); for(int i=0;i<m*n;i++) p_mat[i] = ele_list[i]; //initialise the elements as in the list for(int i=0;i<m*n;i++) p_mat[i] = ele_list[i]; } else{ resetOrder(); } }
Matrix transposeMultiplyMatrixR(const Matrix A, const Matrix B) { /** creates a new matrix that is the product of the A and (B transpose) */ int i, j, k; /* Variables used as indices */ Matrix P = makeMatrix(A->row_dim, B->row_dim); /* Product A * B */ DEBUG(10, "Multiplying Matrix"); assert(A->row_dim == B->row_dim); /* Verify that the Matrices can be multiplied */ /* Optimized code */ for ( i = 0; i < A->row_dim; i++) { for ( j = 0; j < B->row_dim; j++) { ME( P, i, j) = 0; } } for (k = 0; k < A->col_dim; k++) { for ( i = 0; i < A->row_dim; i++) { for ( j = 0; j < B->row_dim; j++) { ME( P, i, j) += ME(A, i, k) * ME(B, j, k); } } } return P; }
void F4Res::construct(int lev, int degree) { decltype(timer()) timeA, timeB; resetMatrix(lev, degree); timeA = timer(); makeMatrix(); timeB = timer(); mFrame.timeMakeMatrix += seconds(timeB - timeA); if (M2_gbTrace >= 2) mHashTable.dump(); if (M2_gbTrace >= 2) std::cout << " make matrix time: " << seconds(timeB - timeA) << " sec" << std::endl; #if 0 std::cout << "-- rows --" << std::endl; debugOutputReducers(); std::cout << "-- columns --" << std::endl; debugOutputColumns(); std :: cout << "-- reducer matrix --" << std::endl; if (true or lev <= 2) debugOutputMatrix(mReducers); else debugOutputMatrixSparse(mReducers); std :: cout << "-- reducer matrix --" << std::endl; debugOutputMatrix(mReducers); debugOutputMatrixSparse(mReducers); std :: cout << "-- spair matrix --" << std::endl; debugOutputMatrix(mSPairs); debugOutputMatrixSparse(mSPairs); #endif if (M2_gbTrace >= 2) std::cout << " (degree,level)=(" << (mThisDegree - mThisLevel) << "," << mThisLevel << ") #spairs=" << mSPairs.size() << " reducer= " << mReducers.size() << " x " << mReducers.size() << std::endl; if (M2_gbTrace >= 2) std::cout << " gauss reduce matrix" << std::endl; timeA = timer(); gaussReduce(); timeB = timer(); mFrame.timeGaussMatrix += seconds(timeB - timeA); if (M2_gbTrace >= 2) std::cout << " time: " << seconds(timeB - timeA) << " sec" << std::endl; // mFrame.show(-1); timeA = timer(); clearMatrix(); timeB = timer(); mFrame.timeClearMatrix += seconds(timeB - timeA); }
void generatePageRanks(std::vector<WebPage*>& pages) { #if DEBUG mergeSort(pages, FileNameComp()); #endif // prevent divide by zero errors if (pages.size() == 0) { return; } // get the matrix Matrix m = makeMatrix(pages); // get the starting vector std::vector<double> v = makeVector(pages.size()); #if DEBUG // debugging output std::cout << "Matrix:" << std::endl; for (size_t x = 0; x < m.mySize; ++ x) { for (size_t y = 0; y < m.mySize; ++ y) { std::cout << m[x][y] << "\t"; } std::cout << std::endl; } std::cout << "Vector:" << std::endl; for (size_t x = 0; x < v.size(); ++ x) { std::cout << v[x] << "\t"; } std::cout << std::endl; #endif // iterate and calculate for (size_t i = 0; i < NUM_ITERATIONS; ++ i) { multiply(m, v); } #if DEBUG std::cout << "Answer:" << std::endl; for (size_t x = 0; x < v.size(); ++ x) { std::cout << v[x] << "\t"; } std::cout << std::endl; #endif // put the values in the pages std::vector<WebPage*>::iterator i = pages.begin(); for (size_t ind = 0; ind < v.size(); ++ ind) { (*i)->setPageRank(v[ind]); ++ i; } }
/* ** Translation functions */ Transform translateTransform(GLdouble x, GLdouble y, GLdouble z) { Transform result; makeMatrix(& result.transformation, 1, 0, 0, x, 0, 1, 0, y, 0, 0, 1, z, 0, 0, 0, 1); makeMatrix(& result.inverseTransformation, 1, 0, 0, -x, 0, 1, 0, -y, 0, 0, 1, -z, 0, 0, 0, 1); return result; }
/* This function performs a correlation based distance measurement on the geometry of the face graph */ FTYPE GeometrySimCorrelation(FaceGraph f1, FaceGraph f2){ int i; FTYPE sum1 = 0.0, sum2 = 0.0; FTYPE mean1 = 0.0, mean2 = 0.0; FTYPE len1 = 0.0, len2 = 0.0; FTYPE sqsum1 = 0.0, sqsum2 = 0.0; FTYPE corr = 0.0; Matrix g1 = makeMatrix(f1->geosize*2,1); Matrix g2 = makeMatrix(f1->geosize*2,1); NOT_IMPLEMENTED; for (i = 0; i < g1->row_dim; i++) { ME(g1,i,0) = (i%2) ? f1->jets[i/2]->y : f1->jets[i/2]->x ; ME(g2,i,0) = (i%2) ? f2->jets[i/2]->y : f2->jets[i/2]->x ; } for (i = 0; i < g1->row_dim; i++) { sum1 += ME(g1,i,0); sum2 += ME(g2,i,0); } mean1 = sum1 / g1->row_dim; mean2 = sum2 / g2->row_dim; for (i = 0; i < g1->row_dim; i++) { ME(g1,i,0) -= mean1; ME(g2,i,0) -= mean2; sqsum1 += ME(g1,i,0)*ME(g1,i,0); sqsum2 += ME(g2,i,0)*ME(g2,i,0); } len1 = 1.0/sqrt(sqsum1); len2 = 1.0/sqrt(sqsum2); for (i = 0; i < g1->row_dim; i++) { ME(g1,i,0) = ME(g1,i,0)*len1; ME(g2,i,0) = ME(g2,i,0)*len2; corr += ME(g1,i,0)*ME(g2,i,0); } freeMatrix(g1); freeMatrix(g2); return (- corr); }
ofMatrix4x4 & FeaturesTracker::getModelMatrix(cv::Mat & cameraMatrix, cv::Mat & distCoefs){ if(found){ Mat rvec, tvec; tracker.getPose(cameraMatrix, distCoefs, rvec, tvec); modelMatrix = makeMatrix(rvec, tvec); } return modelMatrix; }
int main() { /*#TS*/ makeView("mainView", "wholeGraph","Higraph.PDV", "PlacedNode"); setTitle("mainView", "Matrices PDV"); setDefaultNodeValueShow(true, CENTER); setDefaultNodeValueColor(MARKER_RED); setDefaultNodeShape(RECTANGLE); //ScriptManager::relay("HigraphManager", "setDefaultZoneColor", BLUE); ScriptManager::relay("HigraphManager", "setDefaultNodeFillColor", WHITE); ScriptManager::relay("HigraphManager", "setDefaultNodeValueShow", true, CENTER); ScriptManager::relay("HigraphManager", "setDefaultNodeValueColor", BLACK); ScriptManager::relay("HigraphManager", "setDefaultNodeNameShow", true, WEST); ScriptManager::relay("HigraphManager", "setDefaultNodeNameColor", MAGENTA); ScriptManager::relay("HigraphManager", "setDefaultNodeShape", RECTANGLE); ScriptManager::relay("HigraphManager","setDefaultNodeSize", 40, 40); /*#/TS*/ const int rows = 2; const int cols = 3; int val = 10;/*#TS*/ setupval(val); /*#/TS*/ int matrix[rows*cols]; /*#TS*/makeArray(matrix,(rows*cols),true,"matrix[]");/*#/TS*/ int i,j;/*#TS*//*nodes for i & j*/setup_i_j(i,j);/*#/TS*/ /*#TS*/int **mat = new int*[rows]; for(int k=0;k<rows;k++) mat[k]=new int[cols]; makeMatrix(mat,rows,cols,true,"Theoretical representation");/*#/TS*/ for(i = 0; i < rows; i++) { for(j = 0; j < cols; j++) { /*#TS*/ScriptManager::relay("HigraphManager","setNodeFillColor", mat[i][j], YELLOW); ScriptManager::relay("HigraphManager","setNodeFillColor", matrix[(i*cols)+j], YELLOW);/*#/TS*/ /*#TS*/ mat[i][j]=val;/*#/TS*/ matrix[(i*cols)+j]=val++; /*#TS*/ScriptManager::relay("HigraphManager","setNodeFillColor", mat[i][j], WHITE); ScriptManager::relay("HigraphManager","setNodeFillColor", matrix[(i*cols)+j], WHITE);/*#/TS*/ } } //Call module to add 1 to each element of matrix add1(matrix, rows, cols); //See that the values of the original matrix have changed /*#TS*/ //ScriptManager::relay("HigraphManager","setNodeNameLabel",matrix,"matrix[]"); /*#/TS*/ return 0; }
void create(int nInput, int nHidden, int nOutput){ numInput = nInput; numHidden = nHidden; numOutput = nOutput; inputs = makeArray(numInput); ihWeights = makeMatrix(numInput, numHidden); ihBiases = makeArray(numHidden); ihSums = makeArray(numHidden); ihOutputs = makeArray(numHidden); outputs = makeArray(numOutput); hoWeights = makeMatrix(numHidden, numOutput); hoBiases = makeArray(numOutput); hoSums = makeArray(numOutput); oGrads = makeArray(numOutput); hGrads = makeArray(numHidden); ihPrevWeightsDelta = makeMatrix(numInput, numHidden); ihPrevBiasesDelta = makeArray(numHidden); hoPrevWeightsDelta = makeMatrix(numHidden, numOutput); hoPrevBiasesDelta = makeArray(numOutput); for(int i = 0; i < numHidden; i++){ for(int j = 0; j < numInput; j++){ float num = random(10, 1); ihWeights[j][i] = num/10; } float num = random(10, 1); ihBiases[i] = num/10; } for(int i = 0; i < numOutput; i++){ for(int j = 0; j < numHidden; j++){ float num = random(10, 1); hoWeights[j][i] = num/10; } float num = random(10, 1); hoBiases[i] = num/10; } }
Matrix duplicateMatrix(const Matrix mat) { Matrix dup = makeMatrix(mat->row_dim, mat->col_dim); int i, j; for (i = 0; i < mat->row_dim; i++) { for (j = 0; j < mat->col_dim; j++) { ME(dup, i, j) = ME(mat, i, j); } } return dup; }
//Rotate around the Y-axis (clockwise) Transform rotateYTransform(GLdouble angle) { Transform result; GLdouble c = cos(angle*radiansPerDegree); GLdouble s = sin(angle*radiansPerDegree); makeMatrix(& result.transformation, c, 0, s, 0, 0, 1, 0, 0, -s, 0, c, 0, 0, 0, 0, 1); makeMatrix(& result.inverseTransformation, c, 0, -s, 0, 0, 1, 0, 0, s, 0, c, 0, 0, 0, 0, 1); return result; }
void test_inverse0() { real * A = makeRandMatrix(); real * B = makeMatrix(); real * C = makeMatrix(); real * X = makeMatrix(); identity(X, NN); printf("test inverse0\n"); copyMatrix(B, A); printMat("A=", A); inverse(A,C, NN); printMat("inverse=", C); multiply0(A, B, C, NN, NN, NN); printMat("A*A'=", A); printDiffent("A*A'=I", X, A); freeMatrix(A); freeMatrix(B); freeMatrix(C); freeMatrix(X); }
std::pair<double, std::string> Integration::gauss(unsigned int n) { std::vector<double> points; getGaussPoints(n, points); std::vector< std::vector<double> > gaussMatrix; makeMatrix(points, gaussMatrix); std::vector<double> c; calcCoeffs(gaussMatrix, c); std::pair<double, std::string> g = calcGauss(points, c); return g; }
/* Return a matrix of random elements (from 0.0 to 1.0) */ Matrix makeRandomMatrix (int row_dim, int col_dim) { int i,j; Matrix m = makeMatrix(row_dim,col_dim); for (i = 0; i < m->row_dim; i++) { for (j = 0; j < m->col_dim; j++) { ME(m,i,j) = RANDOM; } } return m; }
/* This method performs an L2 based distance measure after solving for a best fit transformation. The best fit transformation is a combination of a 2D scale, rotation and translation. The algorithm solves for this transformation using a least squares solution to a set of transformation aproximations. */ FTYPE GeometrySimLeastSquares(FaceGraph f1, FaceGraph f2){ int i; FTYPE dist = 0.0; Matrix g1 = makeMatrix(f1->geosize*2,1); Matrix g2 = makeMatrix(f1->geosize*2,1); for (i = 0; i < g1->row_dim; i++) { ME(g1,i,0) = (i%2) ? f1->jets[i/2]->y : f1->jets[i/2]->x ; ME(g2,i,0) = (i%2) ? f2->jets[i/2]->y : f2->jets[i/2]->x ; } TransformLeastSquares(g1,g2); dist = L2Dist(g1, g2); freeMatrix(g1); freeMatrix(g2); return dist; }
/* ** Scaling functions */ Transform scaleTransform(GLdouble x, GLdouble y, GLdouble z) { Transform result; if (x*y*z == 0) return identityTransform(); // Don't want divide by zero // Bascially the inverse can't be found this could be an error // or just ignored as done here! makeMatrix(& result.transformation, x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1); makeMatrix(& result.inverseTransformation, 1/x, 0, 0, 0, 0, 1/y, 0, 0, 0, 0, 1/z, 0, 0, 0, 0, 1); return result; }
Matrix makeZeroMatrix(int row_dim, int col_dim) { /** creates and allocates memory for a matrix */ int i, j; Matrix A = makeMatrix(row_dim, col_dim); for (i = 0; i < row_dim; i++) { for (j = 0; j < col_dim; j++) { ME(A, i, j) = 0; } } return A; }
int filesToMatrix(matrix_data_t *m, char *fileNameA, char *fileNameB) { FILE *fA = fopen(fileNameA, "r"); FILE *fB = fopen(fileNameB, "r"); int i,j; if (!fA || !fB) return 1; fscanf(fA, "%d %d", &m->A.rows, &m->A.colums); fscanf(fB, "%d %d", &m->B.rows, &m->B.colums); makeMatrix(&m->A); makeMatrix(&m->B); m->C.rows = m->A.rows; m->C.colums = m->B.colums; makeMatrix(&m->C); for (i = 0;i < m->A.rows;i++) { for (j = 0; j < m->A.colums;j++) { fscanf(fA, "%lf", &(m->A.M[i][j])); } } for (i = 0;i < m->B.rows;i++) { for (j = 0; j < m->B.colums;j++) { fscanf(fB, "%lf", &(m->B.M[i][j])); } } fclose(fA); fclose(fB); return 0; }
Matrix matrixCols( const Matrix mat, int col1, int col2) { Matrix cols = makeMatrix(mat->row_dim, col2 - col1 + 1); int i, j; DEBUG_CHECK(col1 <= col2 && col2 < mat->col_dim, "Poorly chosen columns for extract columns operation"); for (i = col1; i <= col2; i++) { for (j = 0; j < mat->row_dim; j++) { ME(cols, j, i - col1) = ME(mat, j, i); } } return cols; }
void test_lup() { real * A = makeRandMatrix(); real * B = makeMatrix(); real * L = makeMatrix(); real * X = makeMatrix(); real * P = makeMatrix(); identity(X, NN); printf("test crout_plu\n"); copyMatrix(B, A); printMat("A=", A); crout_plu(A,P,L,NN); multiply0(X, P, L, NN, NN, NN); multiply0(P, X, A, NN, NN, NN); printMat("A*A'=", P); printDiffent("A*A'=I", B, P); freeMatrix(A); freeMatrix(B); freeMatrix(L); freeMatrix(X); freeMatrix(P); }
void Model::bbox() { matrix=makeMatrix(); if(mesh!=NULL) { Vector4f p; Matrix4f m=matrix; m.transpose(); min_x=1000000; max_x=-1000000; min_y=1000000; max_y=-1000000; min_z=1000000; max_z=-1000000; for(GLuint i=0;i < mesh->indexCount();i++) { p.set((mesh->vertexData())[(mesh->indexData())[i]*3] ,mesh->vertexData()[mesh->indexData()[i]*3+1] ,mesh->vertexData()[mesh->indexData()[i]*3+2],1.0); p=m*p; if(p[0]<min_x) min_x=p[0]; if(p[0]>max_x) max_x=p[0]; if(p[1]<min_y) min_y=p[1]; if(p[1]>max_y) max_y=p[1]; if(p[2]<min_z) min_z=p[2]; if(p[2]>max_z) max_z=p[2]; } /* if(min_x<0) { m_position[0]-=min_x; min_x=0; } if(min_y<0) { m_position[1]-=min_y; min_y=0; } if(min_z<0) { m_position[2]-=min_z; min_z=0; } */ box.setExtreme(min_x,min_y,min_z,max_x,max_y,max_z); } }
void FeaturesTracker::update(ofBaseHasPixels & frame){ int prevMillis = ofGetElapsedTimeMillis(); found = tracker.find(toCv(frame)); if(found){ Mat rvec, tvec; Mat cameraMatrix = calibration.getDistortedIntrinsics().getCameraMatrix(); tracker.getPose(cameraMatrix, calibration.getDistCoeffs(), rvec, tvec); modelMatrix = makeMatrix(rvec, tvec); } updateTime = ofGetElapsedTimeMillis()-prevMillis; }
void randomize() { fPathDepth = 0; fPathDepthLimit = fRand.nextRangeU(1, 2); fPathContourCount = fRand.nextRangeU(1, 4); fPathSegmentLimit = fRand.nextRangeU(1, 8); fClip = makePath(); SkASSERT(!fPathDepth); fMatrix = makeMatrix(); fPaint = makePaint(); fPathDepthLimit = fRand.nextRangeU(1, 3); fPathContourCount = fRand.nextRangeU(1, 6); fPathSegmentLimit = fRand.nextRangeU(1, 16); fPath = makePath(); SkASSERT(!fPathDepth); }