//double operator ^(double num1, double num2) //{ // return pow(num1,num2); //} Matrix mean(Matrix M1){ Matrix ret; ret.zeros(1,M1.getCols()); for(int i = 1; i <= M1.cols; i++){ for( int j = 1; j <= M1.getRows(); j++){ ret.add(1,i,M1.getMat(j,i)+ret.getMat(1,i));} ret.add(1,i,ret.getMat(1,i)/M1.getRows()); } return ret; }
std::vector<double> Neural_network::generate_output(const std::vector<double>& vec) { Matrix inputs = Matrix::from_vector(vec); Matrix hidden = Matrix::multiply(input_to_hidden_weights, inputs); if(is_bias_on) hidden.add(bias_hidden); hidden.apply_function(activation_function_hid); Matrix output = Matrix::multiply(hidden_to_output_weights, hidden); if(is_bias_on) output.add(bias_output); output.apply_function(activation_function_out); return output.to_vector(); }
void BlockMatrix::multiply(BlockMatrix & m1, Matrix * m2, double factor) { if( m1.nGlobCols != m2->nGlobRows ) { misc.error("Error: An internal error was happened. Block matrices with a discrepant number of rows and columns cannot be multiplied.", 0); } if( m1.nBlockRows == 0 || m1.nBlockCols == 0 ) { misc.error("Error: An internal error was happened. Empty block matrices cannot be multiplied.", 0); } if( m1.nGlobRows == 0 || m1.nGlobCols == 0 || m2->nGlobRows == 0 || m2->nGlobCols == 0) { misc.error("Error: An internal error was happened. Empty block matrices cannot be multiplied.", 0); } clear(); std::vector<Matrix*> m2Blocks; int rowShift = 0; for(int m1c = 0; m1c < m1.nBlockCols; m1c++) { Matrix * m2Block = new Matrix(MATRIX_DEFAULT_DISTRIBUTION, m1.m[0][m1c]->nGlobCols, m2->nGlobCols); m2Block->fillWithConstant(0.); subMatrix smDest(m2Block); subMatrix smSrc(rowShift, 0, m1.m[0][m1c]->nGlobCols, m2->nGlobCols); m2Block->add(m2, 1., 1., smDest, smSrc); m2Blocks.push_back(m2Block); rowShift += m1.m[0][m1c]->nGlobCols; } for(int m1r = 0; m1r < m1.nBlockRows; m1r++) { std::vector<Matrix*> blockRow; Matrix * resultBlock = NULL; for(int m1c = 0; m1c < m1.nBlockCols; m1c++) { Matrix * temp = new Matrix(); temp->multiply(m1.m[m1r][m1c], 'N', m2Blocks[m1c], 'N', factor); if(resultBlock == NULL) { resultBlock = temp; } else { resultBlock->add(temp); delete temp; } } blockRow.push_back(resultBlock); addBlockRow(blockRow); } for(int m1c = 0; m1c < m1.nBlockCols; m1c++) { delete m2Blocks[m1c]; } m2Blocks.clear(); }
Matrix sin(Matrix M1) { Matrix ret = M1; for( int i = 0; i < M1.getRows(); i++) for( int j = 0; j < M1.getCols(); j++) ret.add(i+1, j+1, sin(M1.getMat(i+1,j+1))); return ret; }
Matrix diff(Matrix M, float h)//Encontra a derivada de uma Matriz { Matrix Ret; for(int i = 0; i < M.rows-1; i++) for(int j = 0; j < M.cols; j++) Ret.add(i+1,j+1,(M.Mat[i+1][j] - M.Mat[i][j])/h); return Ret; }
void Neural_network::feed_forward_with_MSE(const std::vector<double>& inputs_vec, const std::vector<double>& targets_vec) { Matrix inputs = Matrix::from_vector(inputs_vec); Matrix hidden = Matrix::multiply(input_to_hidden_weights, inputs); if(is_bias_on) hidden.add(bias_hidden); hidden.apply_function(activation_function_hid); Matrix output = Matrix::multiply(hidden_to_output_weights, hidden); if(is_bias_on) output.add(bias_output); output.apply_function(activation_function_out); Matrix targets = Matrix::from_vector(targets_vec); auto errors = Matrix::subtract(targets, output); current_MSE += Matrix::compute_MSE(errors); }
void Matrix::getEigenvector(double val, double * iVec) { Matrix copMat = copy(); for (int i = 0; i < rows; i++) { copMat.add(i, i, -val); } copMat.rowReduce(); //copMat.print(); findNullVector(copMat.mat, rows, cols, iVec); }
void BlockMatrix::multiply(BlockMatrix & m1, BlockMatrix & m2, double factor) { if( m1.nBlockCols != m2.nBlockRows ) { misc.error("Error: An internal error was happened. Block matrices with a discrepant number of block rows and block columns cannot be multiplied.", 0); } if( m1.nGlobCols != m2.nGlobRows ) { misc.error("Error: An internal error was happened. Block matrices with a discrepant number of rows and columns cannot be multiplied.", 0); } if( m1.nBlockRows == 0 || m1.nBlockCols == 0 || m2.nBlockRows == 0 || m2.nBlockCols == 0) { misc.error("Error: An internal error was happened. Empty block matrices cannot be multiplied.", 0); } if( m1.nGlobRows == 0 || m1.nGlobCols == 0 || m2.nGlobRows == 0 || m2.nGlobCols == 0) { misc.error("Error: An internal error was happened. Empty block matrices cannot be multiplied.", 0); } clear(); for(int m1r = 0; m1r < m1.nBlockRows; m1r++) { std::vector<Matrix*> blockRow; for(int m2c = 0; m2c < m2.nBlockCols; m2c++) { Matrix * resultBlock = NULL; for(int m1c = 0; m1c < m1.nBlockCols; m1c++) { Matrix * temp = new Matrix(); temp->multiply(m1.m[m1r][m1c], 'N', m2.m[m1c][m2c], 'N', factor); if(resultBlock == NULL) { resultBlock = temp; } else { resultBlock->add(temp); delete temp; } } blockRow.push_back(resultBlock); } addBlockRow(blockRow); } }
Matrix Matrix::operator() (Matrix M1,Matrix M2) { float maxM1 = max(M1), maxM2 = max(M2), minM1 = min(M1), minM2 = min(M2); Matrix Ret; try { if (minM1 < 1 || minM2 < 1 || maxM1 > this->rows || maxM2 > this->cols) throw "A matrix não é quadrada"; else { for(int i = 1; i <= M1.getCols(); i++) for(int j = 1; j <= M2.getCols(); j++) Ret.add(i, j, this->Mat[(int)M1(1,i) - 1][(int)M2(1,j) - 1]); Ret.initMatOriginal(this->rows, this->cols); Ret.initVet1(M1.rows, M1.cols); Ret.initVet2(M2.rows, M2.cols); // this->print(); for(int i = 0; i < Ret.MatOriginalRows; i++) for(int j = 0; j < Ret.MatOriginalCols; j++) Ret.MatOriginal[i][j] = this->Mat[i][j]; // Ret.printMatOr(); for(int i = 0; i < Ret.vet1Rows; i++) for(int j = 0; j < Ret.vet1Cols; j++) Ret.vet1[i][j] = M1.Mat[i][j]; for(int i = 0; i < Ret.vet2Rows; i++) for(int j = 0; j < Ret.vet2Cols; j++) Ret.vet2[i][j] = M2.Mat[i][j]; } } catch (const char* msg) { cerr<<msg<<endl; } Ret.Address = this; return Ret; }
Matrix* BlockMatrix::block2distributed() { if(this->nBlockRows < 1 || this->nBlockCols < 1) { misc.error("Error: An internal error was happened. A cyclic matrix cannot be generated from an empty block matrix.", 0); } Matrix* result = new Matrix(MATRIX_DEFAULT_DISTRIBUTION, this->nGlobRows, this->nGlobCols); result->fillWithConstant(0.); int rowShift = 0; for(int r = 0; r < this->nBlockRows; r++) { int colShift = 0; for(int c = 0; c < this->nBlockCols; c++) { result->add( this->m[r][c], 1., 1., subMatrix(rowShift, colShift ,this->m[r][c]->nGlobRows, this->m[r][c]->nGlobCols), subMatrix(this->m[r][c]) ); colShift += this->m[r][c]->nGlobCols; } rowShift += this->m[r][0]->nGlobRows; } return result; }
void rec::simu(Matrix y,Matrix u,int n){ for(int i=n; i < y.length() ;i++) Fia.add(i-n+1,1,-y(i,1)); for(int i=n; i < u.length() ;i++) Fib.add(i-n+1,1,u(i,1)); Fi=Fia|Fib; for(int i=n+1; i <= y.length() ;i++) this->teta1.add(i-n,1,y(i,1)); this->teta1=((~Fi*Fi)^-1)*~Fi*(this->teta1); C.eye(n); this->yest=y; for(int k=n+1; k <= y.length() ;k++){ Matrix fi; fi.add(1,1,-this->yest(k-1,1)); fi.add(1,2,u(k-1,1)); p=fi*this->teta1; this->yest.add(k,1,p(1,1)); } erro=y-this->yest; this->somaErro=mean(abs(erro)); for(int i=1; i <= n-1 ;i++){ X1=Fi; for(int j=n-i; j <= y.length()-1-i ;j++) X2a.add(1+j-n+i,1,-y.getMat(j,1)); for(int j=n-i; j <= u.length()-1-i ;j++) X2b.add(1+j-n+i,1,u(j,1)); X2=X2a|X2b; C=(~X1*X1)^(-1); B=(~X2*X2-(~X2)*X1*C*(~X1)*X2)^(-1); A=C*(~X1)*X2*B; for(int j=n+1; j <= y.length() ;j++) ya.add(j-n,1,y(j,1)); this->teta2=B*(~X2)*(ya-X1*this->teta1); this->teta1=this->teta1-A*(~X2)*(ya-X1*this->teta1); Fi=X1|X2; this->teta=((this->teta1)||(this->teta2)); for(int k=n+1; k <= y.length() ;k++){ Matrix fi; fi.add(1,3,0); fi.add(1,4,0); for(int j=1; j <= i+1 ;j++){ fi.add(1,1,fi(1,3)); fi.add(1,2,fi(1,4)); fi.add(1,3,-this->yest(k-j,1)); fi.add(1,4,u(k-j,1)); } p=fi*this->teta; this->yest.add(k,1,p(1,1)); } for(int j=n+1; j <= this->yest.length() ;j++) erro.add(j-n,1,this->yest(j,1)); erro=Fi*this->teta-erro; p=mean(abs(erro)); this->somaErro.add(this->somaErro.getRows(),i+1,p(1,1)); this->teta1=this->teta; } cout<<"teta=\n "; this->teta.print(); cout<<"\n\nsoma do Erro=\n "; this->somaErro.print(); cout<<"\n\nY estimado=\n "; this->yest.print(); cout<<"\n\nerro=\n "; erro.print(); }
int main() { //Get Learning Dictionary GetLearningDictionary(RGBDic,LabDic); //LabDic[0].print("OK?"); //Do Saliency Detection for(int FileNode=1; FileNode<=MAXPIC; FileNode++) { //GetPic Mat SourceImage; Mat RGBImage; Mat LABImage; sprintf(FileName,"%s%d.jpg",IMGSAIM,FileNode); //get filename SourceImage=imread(FileName); //load picture from file resize(SourceImage,RGBImage,Size(PICSIZE,PICSIZE),0,0,INTER_LINEAR); cvtColor(RGBImage,LABImage,COLOR_BGR2Lab); //translate RGB into Lab imshow("Source",RGBImage); Matrix<double> RGBCannels[3]; Matrix<double> LabCanenls[3]; SpMatrix<double> SPRGB[3]; SpMatrix<double> SPLab[3]; Matrix<double> RGBSaliency; Matrix<double> LabSaliency; Matrix<double> FinalSaliency; Sparam sparam; for(int ColorSpace=1; ColorSpace<=2; ColorSpace++) { Matrix<double> *Cannels; Matrix<double> *Dic; SpMatrix<double> *Sparsecode; Matrix<double> * Saliency; //Choose Image Color Space if(ColorSpace==1) { Cannels=RGBCannels; Dic=RGBDic; Sparsecode=SPRGB; Saliency=&RGBSaliency; } else { Cannels=LabCanenls; Dic=LabDic; Sparsecode=SPLab; Saliency=&LabSaliency; } //Split picture into different cannels and transform Mat into Matrix<T> SplitCannel(RGBImage,Cannels[0],Cannels[1],Cannels[2]); //Split Picture into R,G,B cannel for(int Can=0; Can<3; Can++) { //Image into Row ImageToCol(Cannels[Can],PATCHSIZE,PATCHSIZE); //Represent picture by sparse coding Matrix<double> Result; lasso(Cannels[Can],Dic[Can],Sparsecode[Can],sparam); Dic->mult(Sparsecode[Can],Result); //Result into Image ColToImage(Result,PATCHSIZE,PATCHSIZE,PICSIZE,PICSIZE); } //Get cannnel's local saliency for(int Can=0; Can<3; Can++) { LocalSailency(Sparsecode[Can],Cannels[Can]); Normalization(Cannels[Can]); } //Composite cannels' saliency into space's saliency Saliency->resize(PATCHLEN,PATCHLEN); Saliency->setZeros(); for(int Can=0; Can<3; Can++) Saliency->add(Cannels[Can]); Normalization(*Saliency); } //Composite Lab's and RGB's saliency into finnal sailency FinalSaliency.resize(PATCHLEN,PATCHLEN); FinalSaliency.setZeros(); FinalSaliency.add(LabSaliency); FinalSaliency.add(RGBSaliency); Normalization(FinalSaliency); //transform saliency into image Mat SaliencyImage(PATCHLEN,PATCHLEN,CV_8UC1); MatrixtoMat(FinalSaliency,SaliencyImage); imshow("Final Saliency",SaliencyImage); waitKey(0); } return 0; }
int main() { srand((unsigned)time(NULL)); const float alpha = 0.15; const int I = 5; const int J = 4; const int K = 3; int rnd; int goal; string line; float input[I]; float t[K] = {0.0, 0.0, 0.0}; float maxval[4]; float minval[4]; char *inp; int *number; number = (int *)calloc(2, sizeof(int)); char **nums; nums = (char **)calloc(2, sizeof(char *)); inp = readData(); int i = 0; nums[i] = strtok(inp, " "); while (i < 2) { //printf("token[%d]: %s\n", i, nums[i]); i++; nums[i] = strtok(NULL, " "); } // converts raw data to a string of characters negating the spaces. for(int n = 0; n < 2; n++) number[n] = atoi(nums[n]); //printf("*\n"); //for(int n = 0; n < 2; n++) printf("token[%d]: %d\n", n, number[n]); int items = number[0]; int elems = number[1]; float data[items][elems]; float rawdata[items][elems]; if(SUBMIT) { //read in data set for(int l = 0; l < items; l++) { inp = readData(); i = 0; nums[i] = strtok(inp, " "); while (i < elems) { //printf("%s ", nums[i]); i++; nums[i] = strtok(NULL, " "); } printf("\n"); for(int n = 0; n < elems; n++) rawdata[l][n] = atof(nums[n]); } /*cout << "rawdata: "; for(int r = 0; r < items; r++){ for(int c = 0; c < elems - 1; c++){ cout << rawdata[r][c] << " "; } cout << endl; }*/ } else { ifstream in("irisClean.dat"); for(int i = 0; i < items; i++) { in >> rawdata[i][0] >> rawdata[i][1] >> rawdata[i][2] >> rawdata[i][3] >> rawdata[i][4]; //cout << "reading" << i << ": " << rawdata[i][0] << " " << rawdata[i][1] << " " << rawdata[i][2] << " " << rawdata[i][3] << " | " << rawdata[i][4] << endl; } in.close(); } //normalizeData for(int c = 0; c < elems - 1; c++) { maxval[c] = -10000.0; minval[c] = 10000.0; for(int r = 0; r < items; r++) { if(rawdata[r][c] > maxval[c]) maxval[c] = rawdata[r][c]; if(rawdata[r][c] < minval[c]) minval[c] = rawdata[r][c]; } //cout << minval[c] << " : " << maxval[c] << endl; } for(int c = 0; c < elems -1; c++) { //cout << "data "; for(int r = 0; r < items; r++) { data[r][c] = (rawdata[r][c] - minval[c]) / (maxval[c] - minval[c]); //cout << data[r][c] << " "; } //cout << endl; } for(int r = 0; r < items; r++) data[r][4] = rawdata[r][4]; //set up weights Matrix *v = new Matrix(J, I); for(int j = 0; j < J; j++) v->m[0][j] = 0.0; Matrix *w = new Matrix(K, J); Matrix *tempMatrix; Matrix *tempMatrix2; if(SHOW_WEIGHT) { cout << "w" << endl; w->print(); cout << "v" << endl; v->print(); } for(int ev = 0; ev < ITERATIONS; ev++) { rnd = rand() % items; input[0] = BIAS; for(int i = 0; i < elems - 1; i++) { input[i + 1] = data[rnd][i]; } goal = (int)data[rnd][elems - 1]; for(int k = 0; k < K; k++) t[k] = 0.0; t[goal] = 1.0; Matrix *x = new Matrix(input, I); if(SHOW_PROG)cout << "x" << endl; if(SHOW_PROG)x->print(); Matrix *g = new Matrix(x->dotProduct(v)); if(SHOW_PROG)cout << "g" << endl; if(SHOW_PROG)g->print(); Matrix *a = g->s(); a->m[0][0] = BIAS; if(SHOW_PROG)cout << "a" << endl; if(SHOW_PROG)a->print(); Matrix *h = new Matrix(a->dotProduct(w)); if(SHOW_PROG)cout << "h" << endl; if(SHOW_PROG)h->print(); Matrix *y = h->s(); if(SHOW_PROG)cout << "y" << endl; if(SHOW_PROG)y->print(); if(SHOW_PROG)cout << "t" << endl; if(SHOW_PROG)for(int k = 0; k < K; k++)cout << t[k] << endl; if(SHOW_PROG)cout << "\nBACK" << endl; //back //calculate deltaY Matrix *deltaY = new Matrix(y); for(int k = 0; k < K; k++) { deltaY->m[0][k] = (t[k] - y->m[0][k]) * y->m[0][k] * (1.0 - y->m[0][k]); //deltaY->m[0][k] = (t[k] - y->m[0][k]); } if(SHOW_PROG)cout << "y" << endl; if(SHOW_PROG)y->print(); if(SHOW_PROG)cout << "deltaY" << endl; if(SHOW_PROG)deltaY->print(); //calculate deltaA Matrix *deltaA = new Matrix(a); //delete tempMatrix; tempMatrix = new Matrix(a); tempMatrix = deltaY->dotProduct(w->flip()); if(SHOW_PROG)cout << "tempMatrix - deltaA" << endl; if(SHOW_PROG)tempMatrix->print(); for(int j = 0; j < J; j++) { deltaA->m[0][j] = (a->m[0][j] * (1 - a->m[0][j])) * tempMatrix->m[0][j]; //deltaA->m[0][j] = tempMatrix->m[0][j]; } //deltaA->m[0][0] = 0.0; if(SHOW_PROG)cout << "deltaA" << endl; if(SHOW_PROG)deltaA->print(); //adjust w delete tempMatrix2; tempMatrix2 = new Matrix(w); tempMatrix2 = deltaY->flip()->dotProduct(a->flip()); delete tempMatrix; tempMatrix = new Matrix(w); tempMatrix = tempMatrix2->mult(alpha); if(SHOW_PROG)cout << "tempMatrix - w" << endl; if(SHOW_PROG)tempMatrix->print(); delete tempMatrix2; tempMatrix2 = new Matrix(w); tempMatrix2 = w->add(tempMatrix); //w = w->add(tempMatrix); delete w; w = new Matrix(K, J); for(int r = 0 ; r < tempMatrix2->height; r++) for(int c = 0; c < tempMatrix2->width; c++) w->m[c][r] = tempMatrix2->m[c][r]; //adjust v deltaA->m[0][0] = 0.0; delete tempMatrix2; tempMatrix2 = new Matrix(v); tempMatrix2 = deltaA->flip()->dotProduct(x->flip()); delete tempMatrix; tempMatrix = new Matrix(v); tempMatrix = tempMatrix2->mult(alpha); if(SHOW_PROG)cout << "tempMatrix - v" << endl; if(SHOW_PROG)tempMatrix->print(); delete tempMatrix2; tempMatrix2 = new Matrix(v); tempMatrix2 = v->add(tempMatrix); //v = v->add(tempMatrix); delete v; v = new Matrix(J, I); for(int r = 0 ; r < tempMatrix2->height; r++) for(int c = 0; c < tempMatrix2->width; c++) v->m[c][r] = tempMatrix2->m[c][r]; delete x; delete g; delete a; delete h; delete y; delete deltaA; delete deltaY; delete tempMatrix; } //testing int max; int success = 0; if(SUBMIT) { inp = readData(); // converts raw data to a string of characters negating the spaces. number[0] = atoi(inp); //printf("*\n"); printf("token[0]: %d\n", number[0]); items = number[0]; elems = elems - 1; //number[1]; cout << items << " " << elems << endl; } float tdata[items][elems]; float testdata[items][elems]; if(SUBMIT) { //read in data set for(int l = 0; l < items; l++) { inp = readData(); i = 0; nums[i] = strtok(inp, " "); while (i < elems) { //printf("%s ",nums[i]); i++; nums[i] = strtok(NULL, " "); } //printf("\n"); for(int n = 0; n < elems; n++) testdata[l][n] = atof(nums[n]); } for(int r = 0; r < items; r++) { cout << r << "testdata{ "; for(int c = 0; c < elems; c++) { cout << testdata[r][c] << " " ; } cout << "}" << endl; } } else { for(int r = 0; r < items; r++) { //cout << r << "testdata{ "; for(int c = 0; c < elems; c++) { testdata[r][c] = rawdata[r][c]; //cout << testdata[r][c] << " " ; } //cout << "}" << endl; } } //normalizeTestData /*for(int c = 0; c < elems; c++){ cout << minval[c] << " : " << maxval[c] << endl; }*/ for(int r = 0; r < items; r++) { for(int c = 0; c < elems; c++) { tdata[r][c] = (testdata[r][c] - minval[c]) / (maxval[c] - minval[c]); } } success = 0; int count = 0; for(int ds = 0; ds < items; ds++) { if(!SUBMIT)goal = rawdata[ds][elems - 1]; input[0] = BIAS; for(int i = 0; i < elems; i++) { input[i + 1] = tdata[ds][i]; } cout << "input " << ds << ": " << input[0] << " " << input[1] << " " << input[2] << " " << input[3] << " " << input[4] << endl; Matrix *x = new Matrix(input, I); if(SHOW_PROG)cout << "x" << endl; if(SHOW_PROG)x->print(); Matrix *g = new Matrix(x->dotProduct(v)); if(SHOW_PROG)cout << "g" << endl; if(SHOW_PROG)g->print(); Matrix *a = g->s(); a->m[0][0] = BIAS; if(SHOW_PROG)cout << "a" << endl; if(SHOW_PROG)a->print(); Matrix *h = new Matrix(a->dotProduct(w)); if(SHOW_PROG)cout << "h" << endl; if(SHOW_PROG)h->print(); Matrix *y = h->s(); if(SHOW_PROG)cout << "y" << endl; if(SHOW_PROG)y->print(); if(SHOW_PROG)cout << "t" << endl; if(SHOW_PROG)for(int k = 0; k < K; k++)cout << t[k] << endl; if(SHOW_PROG)cout << "\nBACK" << endl; max = 0; float maxScore = 0; if(SHOW_TEST)cout << y->m[0][0] << " : " << y->m[0][1] << " : " << y->m[0][2] << " || "; cout << "output: " << y->m[0][0] << " : " << y->m[0][1] << " : " << y->m[0][2] << endl; for(int k = 0; k < K; k++) { if(y->m[0][k] > maxScore) { maxScore = y->m[0][k]; max = k; } } delete x; delete g; delete a; delete y; if(!SUBMIT) { if(goal == max) success++; } if(SHOW_TEST)cout << "TEST(" << max << ") GOAL:" << goal << endl; if(SUBMIT)cout << "TEST(" << count << "): " << max << endl; count++; } if(SHOW_TEST)cout << "success " << success << " out of " << count << " or " << 100 * (float)success / (float)count << "%" << endl; if(SHOW_WEIGHT) { cout << "\nw" << endl; w->print(); cout << "v" << endl; v->print(); } }
void L1Graph::L1Minimization(SpMatrix<float>& alpha,std::vector<int>& neighborhood) { //neighborhood contains cidx int p_num=index_->size(); int f_dim=(*features_)[0].size(); Matrix<float> X(f_dim, p_num - 1); //construct matrix X std::map<int,int> map_gl; //map between global and local point index for(int j = 0, k = 0; j < p_num; j++) { //cols int id=(*index_)[j]; if(id==cidx_) { continue; } Util::StdVector2MatrixCol((*features_)[id], X, k); map_gl.insert(make_pair<int,int>(k,id)); ++k; } //TODO:dont use I //construct I // Matrix<float> I(f_dim,f_dim); // I.eye(); // Util::Matrix2File(I,"I.txt"); // Matrix<float> B(f_dim, f_dim + p_num - 1); // X.merge(I, B); // Util::Matrix2File(B,"B.txt"); //clean // X.clear(); // I.clear(); //using lasso Matrix<float> x; //x=Ba Util::StdVector2Matrix((*features_)[cidx_], x); float lambda = 0.01; //lambda int L = (*features_)[0].size(); //max non-zero number // int L = 20; //max non-zero number SpMatrix<float> all; //TODO:debug in matlab ofstream fout("f.txt"); for (int i = 0; i < x.m(); i++) { for (int j = 0; j < x.n(); j++) { fout<<x(i,j)<<" "; } fout<<std::endl; } fout.close(); ofstream dout("d.txt"); for (int i = 0; i < X.m(); i++) { for (int j = 0; j < X.n(); j++) { dout<<X(i,j)<<" "; } dout<<std::endl; } dout.close(); lasso2<float>(x, X, all, L, lambda , 0 , PENALTY , true); //TODO:X->B Util::SubSpMatrix(all,alpha,p_num-1); // all.print("all"); // alpha.print("alpha"); // getchar(); Matrix<float> tmp; X.mult(alpha,tmp); x.add(tmp,-1); std::cout<<(0.5*x.normFsq())<<" "<<(lambda*alpha.asum())<<" "<<(0.5*x.normFsq())/(lambda*alpha.asum())<<std::endl; //save for vis std::vector<int> mk; std::vector<float> mv; for(int ii = 0; ii < alpha.n(); ++ii) { //TODO:calls for pB and pE are not consist for(int j = alpha.pB(ii); j < alpha.pE()[ii]; ++j) { //<i,j>->all.v(j) mk.push_back(map_gl[j]); mv.push_back(alpha.v(j)); neighborhood.push_back(map_gl[j]); } } std::string namev = "debug/" + boost::lexical_cast<std::string>(cidx_) + ".xyznq"; ofstream ov(namev.c_str()); for(int ii = 0; ii < cloud_->size(); ++ii) { if(ii == cidx_) { ov << cloud_->points[ii].x << " " << cloud_->points[ii].y << " " << cloud_->points[ii].z << " 0 0 0 1" << std::endl; } else { std::vector<int>::iterator it = find(mk.begin(), mk.end(), ii); if(it != mk.end()) { int dis = std::distance(mk.begin(), it); ov << cloud_->points[ii].x << " " << cloud_->points[ii].y << " " << cloud_->points[ii].z << " 0 0 0 " << mv[dis] << std::endl; // ov << cloud_->points[ii].x <<" " << cloud_->points[ii].y << " " << cloud_->points[ii].z << " 0 0 0 0" << std::endl; } else { ov << cloud_->points[ii].x << " " << cloud_->points[ii].y << " " << cloud_->points[ii].z << " 0 0 0 -1" << std::endl; } } } ov.close(); return ; } /* ----- end of method L1Graph::L1Minimization ----- */
Matrix operator+(const Matrix &c1, double c2) { return c1.add(c2); }
Matrix operator+(double c1, const Matrix &c2) { return c2.add(c1); }