/* Calculate the cofactor of a matrix */ void matrixCofactor(struct Matrix *out, const struct Matrix a) { if(!matrixIsSquare(a)) { printf("Error: The matrix is not square."); return; } double *newValues = malloc(a.numValues * sizeof(double)); for(int i = 0; i < a.numRows; i++) { for(int j = 0; j < a.numColumns; j++) { struct Matrix temp; int tempsize = (a.numRows-1) * (a.numColumns-1); double valuestemp[tempsize]; int o = 0; for (int k = 0; k < a.numRows; k++) { for (int l = 0; l < a.numColumns; l++) { if(l == j) l++; if(k == i) k++; if(k == a.numRows || l == a.numColumns) break; valuestemp[o] = a.values[k*a.numColumns+l]; o++; } } newMatrix(&temp, valuestemp, a.numRows-1, a.numRows-1); newValues[j*a.numRows+i] = matrixDeterminant(temp); } } newMatrix(out, newValues, a.numRows, a.numColumns); }
image *loadImage(const char *filename) { Imlib_Image img; image *result; int i, j; int width, height; if ((result = (image *) calloc(1, sizeof(image))) == NULL) { fprintf(stderr, "Herpaderp, out of memory"); exit(1); } if ((img = imlib_load_image(filename)) == NULL) { return NULL; } imlib_context_set_image(img); width = imlib_image_get_width(); height = imlib_image_get_height(); result->width = width; result->height = height; result->red = newMatrix(width, height); result->green = newMatrix(width, height); result->blue = newMatrix(width, height); for (i = 0; i < width; i++) { for (j = 0; j < height; j++) { Imlib_Color col; imlib_image_query_pixel(i, j, &col); result->red->vals[i][j] = col.red; result->green->vals[i][j] = col.green; result->blue->vals[i][j] = col.blue; } } imlib_free_image(); return result; }
/* Main method to test math */ int main() { // Test matrix math struct Matrix a; //double valuesa[9] = { 5.0, -2.0, 1.0, 0.0, 3.0, -1.0, 2.0, 0.0, 7.0 }; double valuesa[16] = { 1.0, 3.0, -2.0, 1.0, 5.0, 1.0, 0.0, -1.0, 0.0, 1.0, 0.0, -2.0, 2.0, -1.0, 0.0, 3.0 }; newMatrix(&a, valuesa, 4, 4); struct Matrix b; double valuesb[9] = { 1.0, 2.0, 3.0, 0.0, -4.0, 1.0, 0.0, 3.0, -1.0 }; newMatrix(&b, valuesb, 3, 3); struct Matrix cofa; matrixCofactor(&cofa, a); printf("A Cofactor=\n"); matrixToString(cofa); struct Matrix inva; matrixInverse(&inva, a); printf("A Inverse=\n"); matrixToString(inva); printf("A=\n"); matrixToString(a); printf("B=\n"); matrixToString(b); printf("A is %sa square matrix.\n", matrixIsSquare(a) ? "" : "not "); printf("B is %sa square matrix.\n", matrixIsSquare(b) ? "" : "not "); printf("The determinant of A is %f.\n", matrixDeterminant(a)); printf("A is %sunique.\n", matrixIsUnique(a) ? "" : "not "); printf("The determinant of B is %f.\n", matrixDeterminant(b)); printf("B is %sunique.\n", matrixIsUnique(b) ? "" : "not "); struct Matrix aplusb; matrixAdd(&aplusb, a, b); printf("A+B=\n"); matrixToString(aplusb); struct Matrix asubb; matrixSubtract(&asubb, a, b); printf("A-B=\n"); matrixToString(asubb); struct Matrix ascale; matrixScale(&ascale, a, 5); printf("5A=\n"); matrixToString(ascale); struct Matrix bscale; matrixScale(&bscale, b, 5); printf("5B=\n"); matrixToString(bscale); printf("A and B are %sthe same size.\n", matrixIsSameSize(a, b) ? "" : "not "); printf("A=\n"); matrixToString(a); printf("B=\n"); matrixToString(b); // Test 3D vector math }
int main(void){ int i,j; TYPE_MATRIX a; TYPE_MATRIX b; TYPE_MATRIX u; TYPE_MATRIX ab; TYPE_MATRIX ua; a = newMatrix(MatM,MatN); b = newMatrix(MatN,MatP); u = newMatrix(MatM,MatM); for (i=0;i<MatM;i++){ for (j=0;j<MatN;j++){ M(a,i,j)=1; } } for (i=0;i<MatN;i++){ for (j=0;j<MatP;j++){ M(b,i,j)=(j+i*(MatN)); } } for (i=0; i<MatM;i++){ for (j=0;j<MatM;j++){ if (i==j) M(u,i,j)=1; else M(u,i,j)=0; } } printf("A:\n"); showMatrix(a); printf("B:\n"); showMatrix(b); printf("U:\n"); showMatrix(u); ua=newMatrix(MatN,MatN); ua=multMatrix(u,a); ab=newMatrix(MatN,MatP); ab=multMatrix(a,b); printf("AB:\n"); showMatrix(ab); printf("UA:\n"); showMatrix(ua); return 0; }
int main() { char command[128] = { 0 }; clock_t flag = clock(), flagNow, flagDiff; srand((unsigned int)time(NULL)); newMatrix(&map, HEIGHT, WIDTH); newMatrix(&rendered, HEIGHT, WIDTH); sprintf(command, "mode con:cols=%d lines=%d", 2 * WIDTH + 5, 4+HEIGHT); system(command); SetConsoleOutputCP(949); initilize(); eventInitilize(); for (;;) { do { flagNow = clock(); flagDiff = ((flagNow - flag) * 1000 / CLOCKS_PER_SEC) % 10000; if (flagDiff > timeInterval) { eventLoop(); flag = flagNow; } } while (!kbhit()); eventKeyPress(getKeyInput()); switch (state) { case INIT: eventGameInit(); render(&rendered, &map); if(autoState) state = GAME; break; case GAME: break; case DEAD: eventGameOver(); if(autoState) state = INIT; break; default: break; } } }
/*MAIN*/ int main(int argc, char **agv) { Matrix m1 = newMatrix(500, 500, 5); Matrix m2 = newMatrix(500, 500, 3); Matrix m3 = newMatrix(500, 500, 0); double rowMajorExecutionTime = benchRowMajor(m1, m2, m3); double columnMajorExecutionTime = benchColumnsMajor(m1, m2, m3); double difference = rowMajorExecutionTime - columnMajorExecutionTime; printf("row-major execution time : %fs\n", rowMajorExecutionTime); printf("column-major execution time :%fs\n", columnMajorExecutionTime); printf("difference : %fs\n", difference < 0 ? -difference : difference); }
sparse_matrix_ptrtype newMatrix( DomainSpace const& Xh, DualImageSpace const& Yh, size_type matrix_properties = NON_HERMITIAN, bool init = true ) { return newMatrix( Xh->map(), Yh->map(), matrix_properties ); }
Matrix Matrix::operator*(int n){ Matrix newMatrix(*this); for(int i = 0; i != width * height; i ++){ newMatrix.data[i]*=n; } return newMatrix; }
void ApplyHouse(Matrix A,Vector v, int start, int end) { Matrix M,H; M = newMatrix(); H = newMatrix(); HouseMatrix(H,v,0,n-1); /* Apply it A=H*A*H */ matrixMult(M,A,H); matrixMult(A,H,M); freeMatrix(H); freeMatrix(M); }
/* Scale a matrix */ void matrixScale(struct Matrix *out, const struct Matrix a, const double s) { double *newValues = malloc(a.numValues * sizeof(double)); for(int i = 0; i < a.numValues; i++) newValues[i] = a.values[i]*s; newMatrix(out, newValues, a.numRows, a.numColumns); }
LL_MNode newLL_MNode( double data[],int rows ){ int i; if(rows <= 0){ fprintf(stderr,"ERROR trying to initialize matrix_"); fprintf(stderr,"linklist node with no data\n"); return NULL; } LL_MNode node = (LL_MNode) malloc(sizeof(struct _LL_MNode)); if(node==NULL){ return NULL; } //Create Matrix node->data = newMatrix(rows,1); //Initialize the Matrix with the passed data for(i=0;i<rows;i++){ setE(node->data,i+1,1,data[i]); } node->next = NULL; return node; }
matrix *avgGreyscale(image * img) { int i, j; matrix *mat; int r, g, b; int width, height; width = img->width; height = img->height; mat = newMatrix(width, height); for (i = 0; i < width; i++) { for (j = 0; j < height; j++) { r = (int) img->red->vals[i][j]; g = (int) img->green->vals[i][j]; b = (int) img->blue->vals[i][j]; mat->vals[i][j] = (r + g + b) / 3; } } return mat; }
THIS THIS::transpose() { Matrix newMatrix(m_Columns, m_Lines); for (unsigned i = 0; i < m_Lines*m_Columns; i++) { newMatrix.setValue(i/m_Columns, i%m_Lines, m_Values[i/m_Lines * i%m_Columns]); } return newMatrix; }
static void init_matrices(void) { // Init controller state matrices // Adc = newMatrix(4,4); // setElement(Adc,1,1, 619); setElement(Adc, 1,2, 96); setElement(Adc, 1,3, -1); setElement(Adc, 1,4, 8); // setElement(Adc,1,1, 97); setElement(Adc, 1,2, 703); setElement(Adc, 1,3, 10); 0, 0x0setElement(Adc, 1,4, 1); // setElement(Adc,1,1, 1812); setElement(Adc, 1,2, -1800); setElement(Adc, 1,3, 1130); setElement(Adc, 1,4, 235); // setElement(Adc,1,1, -3884); setElement(Adc, 1,2, 872); setElement(Adc, 1,3, -155); setElement(Adc, 1,4, 722); // Bdc = newMatrix(4,2); // setElement(Bdc,1,1, 376); setElement(Bdc, 1,2, -98); // setElement(Bdc,2,1, -94); setElement(Bdc, 2,2, 296); // setElement(Bdc,3,1, -1014); setElement(Bdc, 3,2, 1895); // setElement(Bdc,4,1, 3053); setElement(Bdc, 4,2, -986); // Cdc = newMatrix(1,4); // setElement(Cdc,1,1, -80310); setElement(Cdc, 1,2, -9624); setElement(Cdc, 1,3, -14122); setElement(Cdc, 1,4, -23626); Adc = newMatrix(4,4); setElement(Adc,1,1, 0.61963); setElement(Adc, 1,2, 0.09677); setElement(Adc, 1,3, -0.00077); setElement(Adc, 1,4, 0.00861); setElement(Adc,2,1, 0.09708); setElement(Adc, 2,2, 0.70384); setElement(Adc, 2,3, 0.01065); setElement(Adc, 2,4, 0.00118); setElement(Adc,3,1, 1.81243); setElement(Adc, 3,2, -1.79966); setElement(Adc, 3,3, 1.13056); setElement(Adc, 3,4, 0.23508); setElement(Adc,4,1, -3.88366); setElement(Adc, 4,2, 0.87240); setElement(Adc, 4,3, -0.15461); setElement(Adc, 4,4, 0.72219); // printk("Adc init:\n"); // printMatrix(Adc); // printk("dim Adc: %d x %d\n", Adc->rows, Adc->cols); // float tmp; // getElement(Adc, 1,1, &tmp); // printk("Adc 1_1: %d\n", (int) (tmp*1000)); Bdc = newMatrix(4,2); setElement(Bdc,1,1, 0.37621); setElement(Bdc, 1,2, -0.09734); setElement(Bdc,2,1, -0.09308); setElement(Bdc, 2,2, 0.29664); setElement(Bdc,3,1, -1.01334); setElement(Bdc, 3,2, 1.89542); setElement(Bdc,4,1, 3.05338); setElement(Bdc, 4,2, -0.98579); Cdc = newMatrix(1,4); setElement(Cdc,1,1, -80.30915); setElement(Cdc, 1,2, -9.62374); setElement(Cdc, 1,3, -14.12152); setElement(Cdc, 1,4, -23.62599); Ddc = newMatrix(1,2); setElement(Ddc,1,1, 0.0); setElement(Ddc, 1,2, 0.0); // Init ... x = newMatrix(4,1); y = newMatrix(2,1); u = newMatrix(1,1); // temp matrices for operations tmp4x4_1 = newMatrix(4,1); tmp4x4_2 = newMatrix(4,1); }
XMMATRIX D3DRenderer::ConvertToXMMatrix(EnMatrix4x4 m) { //We have to transpose as it appears that the XMMATRIX is expecting information in a per row basis, however we use per column. XMMATRIX newMatrix( m.c[0].x, m.c[1].x, m.c[2].x, m.c[3].x, m.c[0].y, m.c[1].y, m.c[2].y, m.c[3].y, m.c[0].z, m.c[1].z, m.c[2].z, m.c[3].z, m.c[0].w, m.c[1].w, m.c[2].w, m.c[3].w); return XMMatrixTranspose(newMatrix); }
gcm::gcm(void) { //Initial feature_ori = new double *[maxFrameNum]; for (int i=0; i<maxFrameNum; i++) { feature_ori[i] = new double[featureDim]; } gcm_subspace = new double*[featureDim]; for (int i=0; i<featureDim; i++) { gcm_subspace[i] = new double[subspaceDim]; } myModel = svm_load_model("..\\model\\model_UI4_noHandSeg_noPSVM"); //SVM model myModel_candi = svm_load_model("..\\model\\model_UI4_noHandSeg_PSVM"); //SVM model subFeaAll_model = newMatrix(featureDim, NClass*subSpaceDim*NTrainSample); //Training Matrix fstream infile("..\\model\\subFeaAll_UI4_noHandSeg_noPSVM.dat",ios::in|ios::binary); for (int i=0; i<featureDim; i++) { for(int j=0;j<NClass*subSpaceDim*NTrainSample;j++) { infile.read((char*)&subFeaAll_model[i][j],sizeof(subFeaAll_model[i][j])); } } infile.close( ); x = new svm_node[NClass*NTrainSample+1+1]; //To release votewhj = new int[NClass*NTrainSample]; //To release nFrames = 0; nDimension = featureDim; prob_estimates = new double[NClass]; subFea1 = newMatrix(featureDim, subSpaceDim); imgShow = cvCreateImage(cvSize(640, 480), 8,3); handSegmentVideo.init(); }
Matrix newIdMatrix(void) { Matrix C; C = newMatrix(); MakeID(C); return C; }
matrix getMatrixLLMatchAtRow(matrix_linklist LL, double match, int R, int * numMatches){ #ifdef _ERROR_CHECKING_ON_ if(LL==NULL){ #ifdef _ERROR_ fprintf(stderr,"ERROR LL is NULL in getMatrixLLMatchAtRow\n"); #endif #ifdef _FORCE_HARD_CRASH_ exit(1); #endif return NULL; } #endif *numMatches =getMatrixLLNumberMatchAtRow( LL,(double)match,R); matrix matches; #ifdef _ERROR_CHECKING_ON_ if(*numMatches<0){ matches=NULL; #ifdef _ERROR_ fprintf(stderr,"numMatches in getMatrixLLMatchAtRow is less than 0\n"); #endif #ifdef _FORCE_HARD_CRASH_ exit(1); #endif return NULL; } #endif if(*numMatches==0){ matches=NULL; return matches; } printf("LL print numMatches %d\n",*numMatches); matches = newMatrix(*numMatches,1); int index; int seq; seq = 1; index = 1; LL_MNode node = LL->start; while( node!=NULL){ if(match==getE(node->data,R,1)){ setE(matches,index,1,seq); printf("matchesRec %g\n",getE(matches,index,1)); index++; } node=node->next; seq++; } return matches; }
// matrix object を複製し、新たなmatrix object を生成する int CKLBLuaLibMatrix::luaCopyMatrix(lua_State * L) { CLuaState lua(L); MATRIX * pMat = getMatPointer(lua, 1); MATRIX * pNew = newMatrix(); for(int i = 0; i < 16; i++) pNew->m[i] = pMat->m[i]; lua.retPointer(pNew); return 1; }
Matrix* generateDCTMatrix( int size ) { Matrix* dct_matrix = newMatrix( size, size ); for ( int k = 0; k < size; ++k ) { for ( int n = 0; n < size; ++n ) { dct_matrix->a[ n ][ k ] = generateDCTCoefficient( size, k, n ); } } return dct_matrix; }
/* Subtract two matricies */ void matrixSubtract(struct Matrix *out, const struct Matrix a, const struct Matrix b) { if(!matrixIsSameSize(a, b)) { printf("Error: The matricies are not the same size."); return; } double *newValues = malloc(a.numValues * sizeof(double)); for(int i = 0; i < a.numValues; i++) newValues[i] = a.values[i] - b.values[i]; newMatrix(out, newValues, a.numRows, a.numColumns); }
Matrix<T> Matrix<T>::operator* (const T& rhs) { if (size()==0) throw std::length_error("Can not perform operations on an empty matrix."); Matrix<T> newMatrix(size()); for (unsigned int i=0;i<size();i++) for (unsigned int j=0;j<size();j++) { newMatrix[i][j] = m_matrix[i][j] * rhs; } return newMatrix; }
SparseMatrix& SparseMatrix::operator-(const SparseMatrix& input) const{ if(ncols != input.ncols || nrows != input.nrows){ throw Dimension(); } SparseMatrix& newMatrix = *(new SparseMatrix(nrows, ncols)); for(int i = 0; i < nrows; i++){ for(int j = 0; j < ncols; j++){ newMatrix(i,j) = this->operator()(i,j) - input(i,j); } } return newMatrix; }
// matrix object を作る int CKLBLuaLibMatrix::luaCreateMatrix(lua_State * L) { CLuaState lua(L); int argc = lua.numArgs(); // 引数が無い場合、単位行列を一つ生成して返す。 if(argc == 0) { MATRIX * pMatrix = newMatrix(); // 初期値として単位行列を生成する for(int i = 0; i < 16; i++) { pMatrix->m[i] = ((i % 4) == (i / 4)) ? 1.0f : 0.0f; } lua.retPointer(pMatrix); return 1; } for(int i = 1; i <= argc; i++) { MATRIX * pMatrix = newMatrix(); // matrix object が作れなければ nil を返す if(!pMatrix) { lua.retNil(); } else { if(lua.isNil(i)) { // 積まれたものがnilだったら単位行列を生成 for(int i = 0; i < 16; i++) { pMatrix->m[i] = (!(i % 5)) ? 1.0f : 0.0f; } } else { // Luaテーブルとみなし、初期値を設定する lua.retValue(i); getMatrix(lua, pMatrix); lua.pop(1); } // 生成したmatrix objectのポインタを返す lua.retPointer(pMatrix); } } return argc; }
Matrix MRO_Transpose::operator()() const { Matrix newMatrix(getColumns(), getRows()); unsigned int i, j; for (i = 0; i < getRows(); i++) { for (j = 0; j < getColumns(); j++) { newMatrix.element(j, i) = element(i, j); } } return newMatrix; }
Matrix MRO_Absolute::operator()() const { Matrix newMatrix(getColumns(), getRows()); unsigned int i, j; for (i = 0; i < getRows(); i++) { for (j = 0; j < getColumns(); j++) { newMatrix.element(i, j) = fabs(element(i, j)); } } return newMatrix; }
Matrix MRO_SquaredElements::operator()() const { Matrix newMatrix(m_thisMatrix); unsigned int i, j; for (i = 0; i < getRows(); i++) { for (j = 0; j < getColumns(); j++) { newMatrix.element(i, j) *= element(i, j); } } return newMatrix; }
void initMatrixImplicit( VoronoiDiagram *voronoiDiagram) { int matrixDim = 1; for( int d=0; d<DIMENSIONS; d++) matrixDim *= voronoiDiagram->xN[d]; b = (float*) malloc( matrixDim * sizeof(float)); x = (float*) malloc( matrixDim * sizeof(float)); A = newMatrix( matrixDim, matrixDim); }
Matrix MakeMatrix(int i) { int j,k; Matrix M; M=newMatrix(); for (j=0;j<n;j++) for (k=j;k<n;k++) if (abs(k-j)>i) M[j][k] = M[k][j] = 0.0; else M[j][k] = M[k][j] = 4.0/(5.0*sqrt(2.0*M_PI))* exp(-(8.0/25.0)*(j-k)*(j-k)); return M; }
SparseMatrix& SparseMatrix::operator*(const SparseMatrix& input) const{ if(ncols != input.nrows){ throw Dimension(); } SparseMatrix& newMatrix = *(new SparseMatrix(nrows, input.ncols)); for(int i = 0; i < newMatrix.nrows; i++){ for(int j = 0; j < newMatrix.ncols; j++){ for(int k = 0; k < this->ncols; k++){ newMatrix(i,j) += this->operator()(i,k)*input(k,j); } } } return newMatrix; }