static void *init(void) { privdata_t *p; int ix; p = xm(sizeof *p, 1); p->buflen = (int)(MAX_DELAY_LEN / 1000.0 * RATE)+1; p->buf = xm(2*sizeof *(p->buf), p->buflen); p->bufplaypos = p->bufrecordpos = 0; for (ix = 0; ix < (int)p->buflen; ix++) p->buf[2*ix] = p->buf[2*ix+1] = 0.0; return p; }
void Plot_axis_arrow(const string axis,const double length,const double Scale_xy,const unsigned int verbose,const bool goto_CM_units) { if(verbose) cout << __FILE__ << " " << __PRETTY_FUNCTION__ << " line " << __LINE__ << " axis=" << axis << " length=" << length << " Scale_xy=" << Scale_xy << " verbose=" << verbose << " goto_CM_units=" << goto_CM_units << endl; string lunit=to_string((int)length)+"m"; TEveStraightLineSet* eve_axis_arrow = new TEveStraightLineSet( (axis+"_"+lunit).c_str() ); // "xy_1m" TEveVectorT<double> xp; if(axis=="x") xp.Set(length, 0, 0); else if(axis=="y") xp.Set(0, length, 0); else if(axis=="z") xp.Set(0, 0, length); else { cout << " warning axis=" << axis << " no known Plot_axis_arrow does nothing" << '\n'; return; } TEveVectorT<double> xm(0,0,0); // origin // apply Scale_xy to arrows, and optionally convert to CM units if(axis!="z") xp*=Scale_xy; // scale factor not used for z if(goto_CM_units) xp*=100; eve_axis_arrow->AddLine(xm,xp); eve_axis_arrow->SetLineWidth(4); gEve->AddElement(eve_axis_arrow); // text on arrow TEveText* t1 = new TEveText(lunit.c_str()); // https://root.cern.ch/root/html/TEveText.html t1->SetMainColor(kBlack); t1->SetFontSize(18); // t1->SetFontMode(TGLFont::kExtrude); t1->SetLighting(kTRUE); t1->PtrMainTrans()->SetPos(xp); // https://root.cern.ch/root/html/TEveElement.html https://root.cern.ch/root/html/TEveTrans.html gEve->AddElement(t1); }
void PolynomialFitting::Init(const std::string& file) { std::vector<double> x; std::vector<double> y; Load(file, x, y); MatrixData X(x.size(), std::vector<double>(mPower + 1, 0)); for(size_t i = 0;i < x.size(); ++i) { X[i][0] = 1; for(size_t j = 1; j <= mPower; ++j) X[i][j] = X[i][j - 1] * x[i]; } MatrixData Y(1, std::vector<double>(y)); Matrix xm(X), ym(Y); ym = ym.Transpose(); Matrix res = xm.Transpose() * xm; res = res.Inverse() * xm.Transpose() * ym; X = res.Transpose().GetData(); std::cout << "Value: " << std::endl; for(int i = 0;i < X[0].size(); ++i) std::cout << X[0][i] << " "; std::cout << std::endl; mCofficient = Polynomial(X[0]); }
c *xsnd(t c *sr, i n) { c *ds = xm(n+1); memcpy(ds, sr, n); ds[n] = '\0'; r ds; }
/* Fit line y = ax+b (lineType ==1) or y = a log(x) + b (lineType == 2) on interval [qmin,qmax] * method == 1 : Least squares fit * method == 2 : Theil's partial robust fit */ void PowerCepstrum_fitTiltLine (PowerCepstrum me, double qmin, double qmax, double *p_a, double *p_intercept, int lineType, int method) { try { double a, intercept; if (qmax <= qmin) { qmin = my xmin; qmax = my xmax; } long imin, imax; if (! Matrix_getWindowSamplesX (me, qmin, qmax, & imin, & imax)) { return; } imin = (lineType == 2 && imin == 1) ? 2 : imin; // log(0) is undefined! long numberOfPoints = imax - imin + 1; if (numberOfPoints < 2) { Melder_throw (U"Not enough points for fit."); } autoNUMvector<double> y (1, numberOfPoints); autoNUMvector<double> x (1, numberOfPoints); for (long i = 1; i <= numberOfPoints; i++) { long isamp = imin + i - 1; x[i] = my x1 + (isamp - 1) * my dx; if (lineType == 2) { x[i] = log (x[i]); } y[i] = my v_getValueAtSample (isamp, 1, 0); } if (method == 3) { // try local maxima first autoNUMvector<double> ym (1, numberOfPoints / 2 + 1); autoNUMvector<double> xm (1, numberOfPoints / 2 + 1); long numberOfLocalPeaks = 0; // forget y[1] if y[2]<y[1] and y[n] if y[n-1]<y[n] ! for (long i = 2; i <= numberOfPoints; i++) { if (y[i - 1] <= y[i] && y[i] > y[i + 1]) { ym[++numberOfLocalPeaks] = y[i]; xm[numberOfLocalPeaks] = x[i]; } } if (numberOfLocalPeaks > numberOfPoints / 10) { for (long i = 1; i <= numberOfLocalPeaks; i++) { x[i] = xm[i]; y[i] = ym[i]; } numberOfPoints = numberOfLocalPeaks; } method = 2; // robust fit of peaks } // fit a straight line through (x,y)'s NUMlineFit (x.peek(), y.peek(), numberOfPoints, & a, & intercept, method); if (p_intercept) { *p_intercept = intercept; } if (p_a) { *p_a = a; } } catch (MelderError) { Melder_throw (me, U": couldn't fit a line."); } }
// ************************************************************************** void tileHelper::status(){ hLabHelper * hlHelper = hLabHelper::getInstance(); int nx_c = ACTIVECHANNELS<=12? 2 : 4; int ny_c = ACTIVECHANNELS/nx_c+ACTIVECHANNELS%nx_c; if(!(fiberDisplay=(TCanvas*)(gROOT->FindObject("fiberDisplay")))) { TString fD = "Fiber Amplitudes (black - raw, red - fit) Run "; fD += runId.Data(); // TString fD = "Fiber Amplitudes (black - raw, red - fit, blue - integral) Run "; fD += hlHelper->prdfName; fiberDisplay = new TCanvas("fiberDisplay",fD,400*nx_c,200*ny_c); fiberDisplay->Divide(nx_c, ny_c); } Double_t ymx[ACTIVECHANNELS], xmx[ACTIVECHANNELS]; for (int ich=0; ich<ACTIVECHANNELS; ich++){ Double_t rvmax, fvmax, ivmax(0), rvrms, fvrms, ivrms(0), rvmean, fvmean, ivmean(0); rvmax = hlHelper->rpeak[ich]->GetMaximum(); fvmax = hlHelper->fm[ich]->GetMaximum(); // ivmax = hlHelper->fint[ich]->GetMaximum(); rvrms = hlHelper->rpeak[ich]->GetRMS(); fvrms = hlHelper->fm[ich]->GetRMS(); // ivrms = hlHelper->fint[ich]->GetRMS(); rvmean = hlHelper->rpeak[ich]->GetMean(); fvmean = hlHelper->fm[ich]->GetMean(); // ivmean = hlHelper->fint[ich]->GetMean(); Double_t ym = std::max(max(rvmax, fvmax), ivmax); ymx[ich] = (int)(log10(ym)); ymx[ich] = pow(10., ymx[ich]); while(ymx[ich]<ym) ymx[ich] *=2; Double_t xm = std::max(max(rvmean+4*rvrms, fvmean+4*fvrms), ivmean+4*ivrms); xmx[ich] = (int)(log10(xm)); xmx[ich] = pow(10., xmx[ich]); while(xmx[ich]<xm) xmx[ich] *=2; cout<<"chan "<<ich<<" rvmax "<<rvmax<<" fvmax "<<fvmax<<" ivmax "<<ivmax<<" xm "<<xm<<" xmx "<<xmx[ich]<<endl; } Double_t xm(0.); for (int ich=0; ich<ACTIVECHANNELS; ich++){ // just to make x-scales on left and right for the same fiber identical hlHelper->rpeak[ich]->SetLineColor(1); hlHelper->rpeak[ich]->SetMaximum(ymx[ich]); hlHelper->fm[ich] ->SetLineColor(2); hlHelper->fm[ich] ->SetMaximum(ymx[ich]); hlHelper->fint[ich] ->SetLineColor(4); hlHelper->fint[ich] ->SetMaximum(ymx[ich]); if(!(ich%2)) xm = max(xmx[ich], xmx[ich+1]); cout<<"chan "<<ich<<" xrange "<<xm<<endl; fiberDisplay->cd(ich+1); hlHelper->rpeak[ich]->SetAxisRange(0., xm); hlHelper->rpeak[ich]->Draw(); hlHelper->fm[ich]->Draw("same"); // hlHelper->fint[ich]->Draw("same"); } fiberDisplay->Update(); }
void BezierWidget::computeMatrices() { QMatrix4x4 xm(cp[0].x(), cp[1].x(), cp[2].x(), cp[3].x(), cp[4].x(), cp[5].x(), cp[6].x(), cp[7].x(), cp[8].x(), cp[9].x(), cp[10].x(), cp[11].x(), cp[12].x(), cp[13].x(), cp[14].x(), cp[15].x()); QMatrix4x4 ym(cp[0].y(), cp[1].y(), cp[2].y(), cp[3].y(), cp[4].y(), cp[5].y(), cp[6].y(), cp[7].y(), cp[8].y(), cp[9].y(), cp[10].y(), cp[11].y(), cp[12].y(), cp[13].y(), cp[14].y(), cp[15].y()); QMatrix4x4 zm(cp[0].z(), cp[1].z(), cp[2].z(), cp[3].z(), cp[4].z(), cp[5].z(), cp[6].z(), cp[7].z(), cp[8].z(), cp[9].z(), cp[10].z(), cp[11].z(), cp[12].z(), cp[13].z(), cp[14].z(), cp[15].z()); matrixX = matrixM * xm * matrixM; matrixY = matrixM * ym * matrixM; matrixZ = matrixM * zm * matrixM; }
synthdata_t *init_synth(const synthinfo_t *info) { synthdata_t *unit = NULL; int ix; unit = xm(sizeof *unit, 1); unit->state = info->init(); unit->info = info; for (ix = 0; ix < info->numparams; ix++) { if (info->paraminfo[ix].paramtype == PT_BOOL) info->setint(unit, ix, 0); else if (info->paraminfo[ix].paramtype == PT_INT) info->setint(unit, ix, info->paraminfo[ix].defval.n); else if (info->paraminfo[ix].paramtype == PT_FLOAT) info->setfloat(unit, ix, info->paraminfo[ix].defval.f); else COMPLAIN("Bad param type"); } return unit; }
// Recursively add a directory. prefix is the prefix used to store // each name, as opposed to the real filename given by starting with // dirname. static void add_dir(const char *dirname, const char *prefix) { DIR *dh; struct dirent *de; char *newprefix; char *newdirname; char *fname; if ((dh = opendir(dirname)) == NULL) err(1, "cannot open directory %s", dirname); while ((de = readdir(dh)) != NULL) { size_t namlen; int filetype; // TODO: check for wildcard match & skip filetype = de->d_type; namlen = strlen(de->d_name); // Handle symlinks to regular files (but not directories, // since that could get us stuck in a loop), and handle // "unknown" entries. On OpenBSD, I get "unknown" entries // when working with an ext2fs partition. // XXX: if the entry is "unknown" and turns out to be a // symlink, it won't be followed if (filetype == DT_UNKNOWN || filetype == DT_LNK) { struct stat info; fname = xm(1, strlen(dirname) + 1 + namlen + 1); sprintf(fname, "%s%s%s", dirname, strlen(dirname) == 0 ? "" : "/", de->d_name); if (stat(fname, &info) == -1) warnx("\rcan't stat file %s, skipping", fname); else if (filetype == DT_LNK && S_ISDIR(info.st_mode)) { warnx("\rskipping symlinked directory %s", fname); } else if (S_ISDIR(info.st_mode)) filetype = DT_DIR; // XXX else if (S_ISREG(info.st_mode)) filetype = DT_REG; // XXX else warnx("\rskipping non-regular file %s", fname); free(fname); fname = NULL; } if (filetype == DT_REG) { fname = xm(1, strlen(prefix) + 1 + namlen + 1); sprintf(fname, "%s%s%s", prefix, strlen(prefix) == 0 ? "" : "/", de->d_name); XPND(fnams, nfnams, sfnams); fnams[nfnams++] = fname; } else if (filetype == DT_DIR) { if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) { continue; } newprefix = xm(1, strlen(prefix) + 1 + namlen + 1); sprintf(newprefix, "%s%s%s", prefix, strlen(prefix) == 0 ? "" : "/", de->d_name); newdirname = xm(1, strlen(dirname) + 1 + namlen + 1); sprintf(newdirname, "%s/%s", dirname, de->d_name); add_dir(newdirname, newprefix); free(newprefix); free(newdirname); } else { warnx("\rskipping non-regular file %s%s%s (type %d)", dirname, strlen(dirname) == 0 ? "" : "/", de->d_name, filetype); } } if (closedir(dh) == -1) err(1, "cannot close directory %s", dirname); }
void OrganizedData::process(RawData* raw, int nBlock, double pTest, int nSupport) { int nData = raw->nData, nDim = raw->nDim - 1; this->nSupport = nSupport; this->nBlock = nBlock; this->nDim = nDim; train = field<mat>(nBlock,2); test = field<mat>(nBlock,2); support = field<mat>(1,2); mat xm(nSupport,nDim), ym(nSupport,1); vec mark(nData); mark.fill(0); printf("Randomly selecting %d supporting point ...\n", nSupport); for (int i = 0; i < nSupport; i++) { int pos = IRAND(0, nData - 1); while (mark[pos] > 0) pos = IRAND(0, nData - 1); mark[pos] = 1; for (int j = 0; j < nDim; j++) xm(i, j) = raw->X(pos,j); ym(i,0) = raw->X(pos,nDim); } support(0,0) = xm; xm.clear(); support(0,1) = ym; ym.clear(); cout << "Partitioning the remaining data into " << nBlock << " cluster using K-Mean ..." << endl; vvd _remain; for (int i = 0; i < nData; i++) if (!mark(i)) { rowvec R = raw->X.row(i); _remain.push_back(r2v(R)); } mat remaining = v2m(_remain); mark.clear(); RawData* remain = new RawData(remaining); KMean* partitioner = new KMean(remain); Partition* clusters = partitioner->cluster(nBlock); cout << "Packaging training/testing data points into their respective cluster" << endl; for (int i = 0; i < nBlock; i++) { cout << "Processing block " << i + 1 << endl; int bSize = (int) clusters->member[i].size(), tSize = (int) floor(bSize * pTest), pos = 0, counter = 0; mark = vec(bSize); mark.fill(0); if (bSize > tSize) // if we can afford to draw tSize test points from this block without depleting it ... { mat xt(tSize,nDim), yt(tSize,1); for (int j = 0; j < tSize; j++) { pos = IRAND(0, bSize - 1); while (mark[pos] > 0) pos = IRAND(0, bSize - 1); mark[pos] = 1; pos = clusters->member[i][pos]; for (int t = 0; t < nDim; t++) xt(j, t) = remain->X(pos,t); yt(j,0) = remain->X(pos,nDim); } bSize -= tSize; nTest += tSize; test(i,0) = xt; xt.clear(); test(i,1) = yt; yt.clear(); } nTrain += bSize; mat xb(bSize,nDim), yb(bSize,1); //cout << remain->X.n_rows << endl; for (int j = 0; j < (int)mark.n_elem; j++) if (mark[j] < 1) { for (int t = 0; t < nDim; t++) { xb(counter,t) = remain->X(clusters->member[i][j],t); } yb(counter++,0) = remain->X(clusters->member[i][j],nDim); } train(i,0) = xb; xb.clear(); train(i,1) = yb; yb.clear(); mark.clear(); printf("Done ! nData[%d] = %d, nTrain[%d] = %d, nTest[%d] = %d .\n", i, (int) clusters->member[i].size(), i, train(i,0).n_rows, i, (int) test(i,0).n_rows); } }
int main() try { // Several ways to create and initialize band matrices: // Create with uninitialized values tmv::BandMatrix<double> m1(6,6,1,2); for(int i=0;i<m1.nrows();i++) for(int j=0;j<m1.ncols();j++) if (i<=j+m1.nlo() && j<=i+m1.nhi()) m1(i,j) = 3.*i-j*j+7.; std::cout<<"m1 =\n"<<m1; //! m1 = //! 6 6 //! ( 7 6 3 0 0 0 ) //! ( 10 9 6 1 0 0 ) //! ( 0 12 9 4 -3 0 ) //! ( 0 0 12 7 0 -9 ) //! ( 0 0 0 10 3 -6 ) //! ( 0 0 0 0 6 -3 ) // Create with all 2's. tmv::BandMatrix<double> m2(6,6,1,3,2.); std::cout<<"m2 =\n"<<m2; //! m2 = //! 6 6 //! ( 2 2 2 2 0 0 ) //! ( 2 2 2 2 2 0 ) //! ( 0 2 2 2 2 2 ) //! ( 0 0 2 2 2 2 ) //! ( 0 0 0 2 2 2 ) //! ( 0 0 0 0 2 2 ) // A BandMatrix can be non-square: tmv::BandMatrix<double> m3(6,8,1,3,2.); std::cout<<"m3 =\n"<<m3; //! m3 = //! 6 8 //! ( 2 2 2 2 0 0 0 0 ) //! ( 2 2 2 2 2 0 0 0 ) //! ( 0 2 2 2 2 2 0 0 ) //! ( 0 0 2 2 2 2 2 0 ) //! ( 0 0 0 2 2 2 2 2 ) //! ( 0 0 0 0 2 2 2 2 ) // Create from given elements: double mm[20] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}; tmv::BandMatrix<double,tmv::ColMajor> m4(6,6,2,1); std::copy(mm,mm+20,m4.colmajor_begin()); std::cout<<"m4 (ColMajor) =\n"<<m4; //! m4 (ColMajor) = //! 6 6 //! ( 1 4 0 0 0 0 ) //! ( 2 5 8 0 0 0 ) //! ( 3 6 9 12 0 0 ) //! ( 0 7 10 13 16 0 ) //! ( 0 0 11 14 17 19 ) //! ( 0 0 0 15 18 20 ) tmv::BandMatrix<double,tmv::RowMajor> m5(6,6,2,1); std::copy(mm,mm+20,m5.rowmajor_begin()); std::cout<<"m5 (RowMajor) =\n"<<m5; //! m5 (RowMajor) = //! 6 6 //! ( 1 2 0 0 0 0 ) //! ( 3 4 5 0 0 0 ) //! ( 6 7 8 9 0 0 ) //! ( 0 10 11 12 13 0 ) //! ( 0 0 14 15 16 17 ) //! ( 0 0 0 18 19 20 ) tmv::BandMatrix<double,tmv::DiagMajor> m6(6,6,2,1); std::copy(mm,mm+20,m6.diagmajor_begin()); std::cout<<"m6 (DiagMajor) =\n"<<m6; //! m6 (DiagMajor) = //! 6 6 //! ( 10 16 0 0 0 0 ) //! ( 5 11 17 0 0 0 ) //! ( 1 6 12 18 0 0 ) //! ( 0 2 7 13 19 0 ) //! ( 0 0 3 8 14 20 ) //! ( 0 0 0 4 9 15 ) // Can make from the banded portion of a regular Matrix: tmv::Matrix<double> xm(6,6); for(int i=0;i<xm.nrows();i++) for(int j=0;j<xm.ncols();j++) xm(i,j) = 5.*i-j*j+3.; tmv::BandMatrix<double> m7(xm,3,2); std::cout<<"m7 =\n"<<m7; //! m7 = //! 6 6 //! ( 3 2 -1 0 0 0 ) //! ( 8 7 4 -1 0 0 ) //! ( 13 12 9 4 -3 0 ) //! ( 18 17 14 9 2 -7 ) //! ( 0 22 19 14 7 -2 ) //! ( 0 0 24 19 12 3 ) // Or from a wider BandMatrix: tmv::BandMatrix<double> m8(m7,3,0); std::cout<<"m8 =\n"<<m8; //! m8 = //! 6 6 //! ( 3 0 0 0 0 0 ) //! ( 8 7 0 0 0 0 ) //! ( 13 12 9 0 0 0 ) //! ( 18 17 14 9 0 0 ) //! ( 0 22 19 14 7 0 ) //! ( 0 0 24 19 12 3 ) // Shortcuts to Bi- and Tri-diagonal matrices: tmv::Vector<double> v1(5,1.); tmv::Vector<double> v2(6,2.); tmv::Vector<double> v3(5,3.); tmv::BandMatrix<double> m9 = LowerBiDiagMatrix(v1,v2); tmv::BandMatrix<double> m10 = UpperBiDiagMatrix(v2,v3); tmv::BandMatrix<double> m11 = TriDiagMatrix(v1,v2,v3); std::cout<<"LowerBiDiagMatrix(v1,v2) =\n"<<m9; //! LowerBiDiagMatrix(v1,v2) = //! 6 6 //! ( 2 0 0 0 0 0 ) //! ( 1 2 0 0 0 0 ) //! ( 0 1 2 0 0 0 ) //! ( 0 0 1 2 0 0 ) //! ( 0 0 0 1 2 0 ) //! ( 0 0 0 0 1 2 ) std::cout<<"UpperBiDiagMatrix(v2,v3) =\n"<<m10; //! UpperBiDiagMatrix(v2,v3) = //! 6 6 //! ( 2 3 0 0 0 0 ) //! ( 0 2 3 0 0 0 ) //! ( 0 0 2 3 0 0 ) //! ( 0 0 0 2 3 0 ) //! ( 0 0 0 0 2 3 ) //! ( 0 0 0 0 0 2 ) std::cout<<"TriDiagMatrix(v1,v2,v3) =\n"<<m11; //! TriDiagMatrix(v1,v2,v3) = //! 6 6 //! ( 2 3 0 0 0 0 ) //! ( 1 2 3 0 0 0 ) //! ( 0 1 2 3 0 0 ) //! ( 0 0 1 2 3 0 ) //! ( 0 0 0 1 2 3 ) //! ( 0 0 0 0 1 2 ) // Norms, etc. std::cout<<"Norm1(m1) = "<<Norm1(m1)<<std::endl; //! Norm1(m1) = 30 std::cout<<"Norm2(m1) = "<<Norm2(m1)<<std::endl; //! Norm2(m1) = 24.0314 std::cout<<"NormInf(m1) = "<<NormInf(m1)<<std::endl; //! NormInf(m1) = 28 std::cout<<"NormF(m1) = "<<NormF(m1)<<" = "<<Norm(m1)<<std::endl; //! NormF(m1) = 32.0312 = 32.0312 std::cout<<"MaxAbsElement(m1) = "<<MaxAbsElement(m1)<<std::endl; //! MaxAbsElement(m1) = 12 std::cout<<"Trace(m1) = "<<Trace(m1)<<std::endl; //! Trace(m1) = 32 std::cout<<"Det(m1) = "<<Det(m1)<<std::endl; //! Det(m1) = 67635 // Views: std::cout<<"m1 =\n"<<m1; //! m1 = //! 6 6 //! ( 7 6 3 0 0 0 ) //! ( 10 9 6 1 0 0 ) //! ( 0 12 9 4 -3 0 ) //! ( 0 0 12 7 0 -9 ) //! ( 0 0 0 10 3 -6 ) //! ( 0 0 0 0 6 -3 ) std::cout<<"m1.diag() = "<<m1.diag()<<std::endl; //! m1.diag() = 6 ( 7 9 9 7 3 -3 ) std::cout<<"m1.diag(1) = "<<m1.diag(1)<<std::endl; //! m1.diag(1) = 5 ( 6 6 4 0 -6 ) std::cout<<"m1.diag(-1) = "<<m1.diag(-1)<<std::endl; //! m1.diag(-1) = 5 ( 10 12 12 10 6 ) std::cout<<"m1.subBandMatrix(0,3,0,3,1,1) =\n"<< m1.subBandMatrix(0,3,0,3,1,1); //! m1.subBandMatrix(0,3,0,3,1,1) = //! 3 3 //! ( 7 6 0 ) //! ( 10 9 6 ) //! ( 0 12 9 ) std::cout<<"m1.transpose() =\n"<<m1.transpose(); //! m1.transpose() = //! 6 6 //! ( 7 10 0 0 0 0 ) //! ( 6 9 12 0 0 0 ) //! ( 3 6 9 12 0 0 ) //! ( 0 1 4 7 10 0 ) //! ( 0 0 -3 0 3 6 ) //! ( 0 0 0 -9 -6 -3 ) // rowRange, colRange shrink both dimensions of the matrix to include only // the portions that are in those rows or columns: std::cout<<"m1.rowRange(0,4) =\n"<<m1.rowRange(0,4); //! m1.rowRange(0,4) = //! 4 6 //! ( 7 6 3 0 0 0 ) //! ( 10 9 6 1 0 0 ) //! ( 0 12 9 4 -3 0 ) //! ( 0 0 12 7 0 -9 ) std::cout<<"m1.colRange(1,4) =\n"<<m1.colRange(1,4); //! m1.colRange(1,4) = //! 5 3 //! ( 6 3 0 ) //! ( 9 6 1 ) //! ( 12 9 4 ) //! ( 0 12 7 ) //! ( 0 0 10 ) std::cout<<"m1.diagRange(0,2) =\n"<<m1.diagRange(0,2); //! m1.diagRange(0,2) = //! 6 6 //! ( 7 6 0 0 0 0 ) //! ( 0 9 6 0 0 0 ) //! ( 0 0 9 4 0 0 ) //! ( 0 0 0 7 0 0 ) //! ( 0 0 0 0 3 -6 ) //! ( 0 0 0 0 0 -3 ) std::cout<<"m1.diagRange(-1,1) =\n"<<m1.diagRange(-1,1); //! m1.diagRange(-1,1) = //! 6 6 //! ( 7 0 0 0 0 0 ) //! ( 10 9 0 0 0 0 ) //! ( 0 12 9 0 0 0 ) //! ( 0 0 12 7 0 0 ) //! ( 0 0 0 10 3 0 ) //! ( 0 0 0 0 6 -3 ) // Fortran Indexing: tmv::BandMatrix<double,tmv::FortranStyle> fm1 = m1; std::cout<<"fm1 = m1 =\n"<<fm1; //! fm1 = m1 = //! 6 6 //! ( 7 6 3 0 0 0 ) //! ( 10 9 6 1 0 0 ) //! ( 0 12 9 4 -3 0 ) //! ( 0 0 12 7 0 -9 ) //! ( 0 0 0 10 3 -6 ) //! ( 0 0 0 0 6 -3 ) std::cout<<"fm1(1,1) = "<<fm1(1,1)<<std::endl; //! fm1(1,1) = 7 std::cout<<"fm1(4,3) = "<<fm1(4,3)<<std::endl; //! fm1(4,3) = 12 std::cout<<"fm1.subBandMatrix(1,3,1,3,1,1) =\n"<< fm1.subBandMatrix(1,3,1,3,1,1); //! fm1.subBandMatrix(1,3,1,3,1,1) = //! 3 3 //! ( 7 6 0 ) //! ( 10 9 6 ) //! ( 0 12 9 ) std::cout<<"fm1.rowRange(1,4) =\n"<<fm1.rowRange(1,4); //! fm1.rowRange(1,4) = //! 4 6 //! ( 7 6 3 0 0 0 ) //! ( 10 9 6 1 0 0 ) //! ( 0 12 9 4 -3 0 ) //! ( 0 0 12 7 0 -9 ) std::cout<<"fm1.colRange(2,4) =\n"<<fm1.colRange(2,4); //! fm1.colRange(2,4) = //! 5 3 //! ( 6 3 0 ) //! ( 9 6 1 ) //! ( 12 9 4 ) //! ( 0 12 7 ) //! ( 0 0 10 ) std::cout<<"fm1.diagRange(0,1) =\n"<<fm1.diagRange(0,1); //! fm1.diagRange(0,1) = //! 6 6 //! ( 7 6 0 0 0 0 ) //! ( 0 9 6 0 0 0 ) //! ( 0 0 9 4 0 0 ) //! ( 0 0 0 7 0 0 ) //! ( 0 0 0 0 3 -6 ) //! ( 0 0 0 0 0 -3 ) std::cout<<"fm1.diagRange(-1,0) =\n"<<fm1.diagRange(-1,0); //! fm1.diagRange(-1,0) = //! 6 6 //! ( 7 0 0 0 0 0 ) //! ( 10 9 0 0 0 0 ) //! ( 0 12 9 0 0 0 ) //! ( 0 0 12 7 0 0 ) //! ( 0 0 0 10 3 0 ) //! ( 0 0 0 0 6 -3 ) // Matrix arithmetic: tmv::BandMatrix<double> m1pm2 = m1 + m2; std::cout<<"m1 + m2 =\n"<<m1pm2; //! m1 + m2 = //! 6 6 //! ( 9 8 5 2 0 0 ) //! ( 12 11 8 3 2 0 ) //! ( 0 14 11 6 -1 2 ) //! ( 0 0 14 9 2 -7 ) //! ( 0 0 0 12 5 -4 ) //! ( 0 0 0 0 8 -1 ) // Works correctly even if matrices are stored in different order: tmv::BandMatrix<double> m5pm6 = m5 + m6; std::cout<<"m5 + m6 =\n"<<m5pm6; //! m5 + m6 = //! 6 6 //! ( 11 18 0 0 0 0 ) //! ( 8 15 22 0 0 0 ) //! ( 7 13 20 27 0 0 ) //! ( 0 12 18 25 32 0 ) //! ( 0 0 17 23 30 37 ) //! ( 0 0 0 22 28 35 ) // Also expands the number of off-diagonals appropriately as needed: tmv::BandMatrix<double> m2pm4 = m2 + m4; std::cout<<"m2 + m4 =\n"<<m2pm4; //! m2 + m4 = //! 6 6 //! ( 3 6 2 2 0 0 ) //! ( 4 7 10 2 2 0 ) //! ( 3 8 11 14 2 2 ) //! ( 0 7 12 15 18 2 ) //! ( 0 0 11 16 19 21 ) //! ( 0 0 0 15 20 22 ) m1 *= 2.; std::cout<<"m1 *= 2 =\n"<<m1; //! m1 *= 2 = //! 6 6 //! ( 14 12 6 0 0 0 ) //! ( 20 18 12 2 0 0 ) //! ( 0 24 18 8 -6 0 ) //! ( 0 0 24 14 0 -18 ) //! ( 0 0 0 20 6 -12 ) //! ( 0 0 0 0 12 -6 ) m2 += m1; std::cout<<"m2 += m1 =\n"<<m2; //! m2 += m1 = //! 6 6 //! ( 16 14 8 2 0 0 ) //! ( 22 20 14 4 2 0 ) //! ( 0 26 20 10 -4 2 ) //! ( 0 0 26 16 2 -16 ) //! ( 0 0 0 22 8 -10 ) //! ( 0 0 0 0 14 -4 ) tmv::Vector<double> v = xm.col(0); std::cout<<"v = "<<v<<std::endl; //! v = 6 ( 3 8 13 18 23 28 ) std::cout<<"m1 * v = "<<m1*v<<std::endl; //! m1 * v = 6 ( 216 396 432 60 162 108 ) std::cout<<"v * m1 = "<<v*m1<<std::endl; //! v * m1 = 6 ( 202 492 780 832 396 -768 ) // Matrix * matrix product also expands bands appropriately: tmv::BandMatrix<double> m1m2 = m1 * m2; std::cout<<"m1 * m2 =\n"<<m1m2; //! m1 * m2 = //! 6 6 //! ( 488 592 400 136 0 12 ) //! ( 716 952 704 264 -8 -8 ) //! ( 528 948 904 272 -56 -32 ) //! ( 0 624 844 464 -320 -104 ) //! ( 0 0 520 452 -80 -332 ) //! ( 0 0 0 264 12 -96 ) // Can mix BandMatrix with other kinds of matrices: std::cout<<"xm * m1 =\n"<<xm*m1; //! xm * m1 = //! 6 6 //! ( 82 48 -120 -348 -336 396 ) //! ( 252 318 180 -128 -276 216 ) //! ( 422 588 480 92 -216 36 ) //! ( 592 858 780 312 -156 -144 ) //! ( 762 1128 1080 532 -96 -324 ) //! ( 932 1398 1380 752 -36 -504 ) tmv::UpperTriMatrix<double> um(xm); std::cout<<"um + m1 =\n"<<um+m1; //! um + m1 = //! 6 6 //! ( 17 14 5 -6 -13 -22 ) //! ( 20 25 16 1 -8 -17 ) //! ( 0 24 27 12 -9 -12 ) //! ( 0 0 24 23 2 -25 ) //! ( 0 0 0 20 13 -14 ) //! ( 0 0 0 0 12 -3 ) tmv::LowerTriMatrix<double> lm(xm); lm *= m8; std::cout<<"lm *= m8 =\n"<<lm; //! lm *= m8 = //! 6 6 //! ( 9 0 0 0 0 0 ) //! ( 80 49 0 0 0 0 ) //! ( 252 192 81 0 0 0 ) //! ( 534 440 252 81 0 0 ) //! ( 744 774 500 224 49 0 ) //! ( 954 1064 782 396 120 9 ) tmv::DiagMatrix<double> dm(xm); m1 *= dm; std::cout<<"m1 *= dm =\n"<<m1; //! m1 *= dm = //! 6 6 //! ( 42 84 54 0 0 0 ) //! ( 60 126 108 18 0 0 ) //! ( 0 168 162 72 -42 0 ) //! ( 0 0 216 126 0 -54 ) //! ( 0 0 0 180 42 -36 ) //! ( 0 0 0 0 84 -18 ) return 0; } catch (tmv::Error& e) { std::cerr<<e<<std::endl; return 1; }
void MG_poseReader::draw( M3dView & view, const MDagPath & path, M3dView::DisplayStyle dispStyle, M3dView::DisplayStatus status ) { MPlug sizeP (thisMObject(),size); double sizeV; sizeP.getValue(sizeV); MPlug poseMatrixP (thisMObject(),poseMatrix); MObject poseMatrixData; poseMatrixP.getValue(poseMatrixData); MFnMatrixData matrixFn(poseMatrixData); MMatrix poseMatrixV =matrixFn.matrix(); MPlug readerMatrixP (thisMObject(),readerMatrix); MObject readerMatrixData; readerMatrixP.getValue(readerMatrixData); matrixFn.setObject(readerMatrixData); MMatrix readerMatrixV =matrixFn.matrix(); MMatrix poseMatrixFix =poseMatrixV*readerMatrixV.inverse(); MPlug aimAxisP (thisMObject(),aimAxis); int aimAxisV; aimAxisP.getValue(aimAxisV); MVector aimBall; MPlug readerOnOffP(thisMObject(),readerOnOff); MPlug axisOnOffP(thisMObject(),axisOnOff); MPlug poseOnOffP(thisMObject(),poseOnOff); double readerOnOffV; double axisOnOffV; double poseOnOffV; readerOnOffP.getValue(readerOnOffV); axisOnOffP.getValue(axisOnOffV); poseOnOffP.getValue(poseOnOffV); MPlug xPositiveP (thisMObject(),xPositive); MPlug xNegativeP (thisMObject(),xNegative); double xPositiveV; double xNegativeV; xPositiveP.getValue(xPositiveV); xNegativeP.getValue(xNegativeV); double xColor = xPositiveV; if (xPositiveV==0) { xColor=xNegativeV; } MPlug yPositiveP (thisMObject(),yPositive); MPlug yNegativeP (thisMObject(),yNegative); double yPositiveV; double yNegativeV; yPositiveP.getValue(yPositiveV); yNegativeP.getValue(yNegativeV); double yColor = yPositiveV; if (yPositiveV==0) { yColor=yNegativeV; } MPlug zPositiveP (thisMObject(),zPositive); MPlug zNegativeP (thisMObject(),zNegative); double zPositiveV; double zNegativeV; zPositiveP.getValue(zPositiveV); zNegativeP.getValue(zNegativeV); double zColor = zPositiveV; if (zPositiveV==0) { zColor=zNegativeV; } if (aimAxisV==0) { aimBall.x=poseMatrixFix[0][0]; aimBall.y=poseMatrixFix[0][1]; aimBall.z=poseMatrixFix[0][2]; } else if (aimAxisV==1) { aimBall.x=poseMatrixFix[1][0]; aimBall.y=poseMatrixFix[1][1]; aimBall.z=poseMatrixFix[1][2]; }else { aimBall.x=poseMatrixFix[2][0]; aimBall.y=poseMatrixFix[2][1]; aimBall.z=poseMatrixFix[2][2]; } //***************************************************************** // Initialize opengl and draw //***************************************************************** view.beginGL(); glPushAttrib( GL_ALL_ATTRIB_BITS ); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); glLineWidth(2); if(status == M3dView::kLead) glColor4f(0.0,1.0,0.0,0.3f); else glColor4f(1.0,1.0,0.0,0.3f); MVector baseV(0,0,0); MVector xp(1*sizeV,0,0); MVector xm(-1*sizeV,0,0); MVector yp(0,1*sizeV,0); MVector ym(0,-1*sizeV,0); MVector zp(0,0,1*sizeV); MVector zm(0,0,-1*sizeV); double * red; red = new double[4]; red[0]=1; red[1]=0; red[2]=0; red[3]=1; double * green; green = new double[4]; green[0]=0; green[1]=1; green[2]=0; green[3]=1; double * blue; blue = new double[4]; blue[0]=0; blue[1]=0; blue[2]=1; blue[3]=1; double * yellow; yellow = new double[4]; yellow[0]=1; yellow[1]=1; yellow[2]=0.2; yellow[3]=0.3; if (readerOnOffV==1) { drawSphere(sizeV,20,20,baseV,yellow); } if (axisOnOffV==1) { drawSphere(sizeV/7,15,15,xp,red); drawSphere(sizeV/7,15,15,xm,red); drawSphere(sizeV/7,15,15,yp,green); drawSphere(sizeV/7,15,15,ym,green); drawSphere(sizeV/7,15,15,zp,blue); drawSphere(sizeV/7,15,15,zm,blue); } if (poseOnOffV==1) { double* color = blendColor(xColor,yColor,zColor,1); drawSphere(sizeV/7,15,15,aimBall*sizeV,color); } glDisable(GL_BLEND); glPopAttrib(); }
vector<double> CNelderMead::NelderMeadFunction(double (*f)(vector<double>, RMSEinputs rmsein), NMsettings nmset, vector<vector<double> > x) { int NumIters = 0; int i,j; double MaxIters = nmset.MaxIters; double tolerance = nmset.tolerance; int N = nmset.N; RMSEinputs rmsein = nmset.RMSEinp; double S = rmsein.S; double T = rmsein.T; double r = rmsein.r; // Value of the function at the vertices vector<vector<double> > F(N+1, vector<double>(2)); // Step 0. Ordering and Best and Worst points // Order according to the functional values, compute the best and worst points step0: NumIters += 1; for (j=0; j<=N; j++){ vector<double> z(N, 0.0); // Create vector to contain for (i=0; i<=N-1; i++) z[i] = x[i][j]; F[j][0] = f(z,rmsein); // Function values F[j][1] = j; // Original index positions } sort(F.begin(), F.end()); // New vertices order first N best initial vectors and // last (N+1)st vertice is the worst vector // y is the matrix of vertices, ordered so that the worst vertice is last vector<vector<double> > y(N, vector<double>(N+1)); for (j=0; j<=N; j++) for (i=0; i<=N-1; i++) y[i][j] = x[i][F[j][1]]; // First best vector y(1) and function value f1 vector<double> x1(N, 0.0); for (i=0; i<=N-1; i++) x1[i] = y[i][0]; double f1 = f(x1,rmsein); // Last best vector y(N) and function value fn vector<double> xn(N, 0.0); for (i=0; i<=N-1; i++) xn[i] = y[i][N-1]; double fn = f(xn,rmsein); // Worst vector y(N+1) and function value fn1 vector<double> xn1(N, 0.0); for (i=0; i<=N-1; i++) xn1[i] = y[i][N]; double fn1 = f(xn1,rmsein); // z is the first N vectors from y, excludes the worst y(N+1) vector<vector<double> > z(N, vector<double>(N)); for (j=0; j<=N-1; j++) for (i=0; i<=N-1; i++) z[i][j] = y[i][j]; // Mean of best N values and function value fm vector<double> xm(N, 0.0); xm = CNelderMead::VMean(z,N); double fm = f(xm,rmsein); // Reflection point xr and function fr vector<double> xr(N, 0.0); xr = CNelderMead::VSub(VAdd(xm, xm), xn1); double fr = f(xr,rmsein); // Expansion point xe and function fe vector<double> xe(N, 0.0); xe = CNelderMead::VSub(VAdd(xr, xr), xm); double fe = f(xe,rmsein); // Outside contraction point and function foc vector<double> xoc(N, 0.0); xoc = CNelderMead::VAdd(CNelderMead::VMult(xr, 0.5), VMult(xm, 0.5)); double foc = f(xoc,rmsein); // Inside contraction point and function foc vector<double> xic(N, 0.0); xic = CNelderMead::VAdd(CNelderMead::VMult(xm, 0.5), CNelderMead::VMult(xn1, 0.5)); double fic = f(xic,rmsein); while ((NumIters <= MaxIters) && (abs(f1-fn1) >= tolerance)) { // Step 1. Reflection Rule if ((f1<=fr) && (fr<fn)) { for (j=0; j<=N-1; j++) for (i=0; i<=N-1; i++) x[i][j] = y[i][j]; for (i=0; i<=N-1; i++) x[i][N] = xr[i]; goto step0; } // Step 2. Expansion Rule if (fr<f1) { for (j=0; j<=N-1; j++) { for (i=0; i<=N-1; i++) x[i][j] = y[i][j]; } if (fe<fr) for (i=0; i<=N-1; i++) x[i][N] = xe[i]; else for (i=0; i<=N-1; i++) x[i][N] = xr[i]; goto step0; } // Step 3. Outside contraction Rule if ((fn<=fr) && (fr<fn1) && (foc<=fr)) { for (j=0; j<=N-1; j++) { for (i=0; i<=N-1; i++) x[i][j] = y[i][j]; } for (i=0; i<=N-1; i++) x[i][N] = xoc[i]; goto step0; } // Step 4. Inside contraction Rule if ((fr>=fn1) && (fic<fn1)) { for (j=0; j<=N-1; j++) { for (i=0; i<=N-1; i++) x[i][j] = y[i][j]; } for (i=0; i<=N-1; i++) x[i][N] = xic[i]; goto step0; } // Step 5. Shrink Step for (i=0; i<=N-1; i++) x[i][0] = y[i][0]; for (i=0; i<=N-1; i++) { for (j=1; j<=N; j++) x[i][j] = 0.5*(y[i][j] + x[i][0]); } goto step0; } // Output component // Return N parameter values, value of objective function, and number of iterations vector<double> out(N+2); for (i=0; i<=N-1; i++) out[i] = x1[i]; out[N] = f1; out[N+1] = NumIters; return out; }
static void *init(void) { privdata_t *p; p = xm(sizeof *p, 1); return p; }
/* expand array */ v *xpnd(v *p, i nit, i *sit, z sz) { if (nit < *sit) r p; l if (*sit > 0) r xr(p, sz * (*sit *= 2)); l r xm(sz * (*sit = 10)); }