void eD() { char simbolo = palavra[posicaoPalavra]; if (!ehFinalPalavra(simbolo)) { if (ehOperador(simbolo)) { posicaoPalavra++; eA(); } else { if (!pilhaEstaVazia(pilha_estados)) { ptr_estado estado_desempilhado = desempilha(pilha_estados, &posicao_pilha); estado_desempilhado(); } else { if (verificaAceitacao(pilha_estados, simbolo)) { printf("\nACEITO!\n"); } else { printf("\nNÃO ACEITO!\n"); } } } } else { if (verificaAceitacao(pilha_estados, simbolo)) { printf("\nACEITO!\n"); } else { printf("\nNÃO ACEITO!\n"); } } }
//-------------------------------------------------------------- void testApp::setup() { ofSetLogLevel(OF_LOG_VERBOSE); n1 = 10; n2 = 20; n3 = 30; //create dispatcher jppevent::EventDispatcher dispatcher(this); //add listener dispatcher.addEventListener("A", eventHandler1); dispatcher.addEventListener("B", eventHandler1); dispatcher.addEventListener("B", eventHandler2, 100); //high priority dispatcher.addEventListener("B", eventHandler3); //dispatch test ofLog() << "----------------------------------------"; jppevent::Event eA("A"); dispatcher.dispatchEvent(&eA); ofLog() << "----------------------------------------"; dispatcher.dispatchEvent("A"); ofLog() << "----------------------------------------"; jppevent::Event eB("B"); dispatcher.dispatchEvent(&eB); }
void eB() { empilha(eC,pilha_estados, &posicao_pilha); // chamada da própria máquina eA(); }
int main(int argc, char const *argv[]) { printf("Entrada: "); fgets(palavra, 200, stdin); // Limpa a palavra, pois fgets coloca um '\n' na // penúltima posição, antes do '\0'. Assim forçamos // o '\0' ser no lugar do '\n' palavra[tamanhoString(palavra) - 1] = '\0'; printf("Saída: "); // estado inicial eA(); return 0; }
Matrix Matrix::expmts(double tol) const { if (m_nrows != m_ncols) throw Error("source matrix is not square"); double n2 = norm_p(2); unsigned int m = computeNextPowerOfTwo((uint32_t)n2); if (m > 1) { Matrix eA = *this; eA *= (1.0 / m); eA = eA.expmts(); // scaling for (unsigned int i = 1; i < m; i = i << 1) eA = eA * eA; // squaring return eA; } Matrix eA(m_nrows); Matrix A(m_nrows); n2 = 1; double inv_f = 1; int i = 0; while (true) { inv_f = inv_f * (1.0 / ++i); A = A * (*this); eA = eA + inv_f * A; double n2b = eA.norm_p(2); if (std::fabs(n2b - n2) < tol) break; n2 = n2b; } return eA; }
void MagCal::calcMagComp() { /* * Inspired by * http://davidegironi.blogspot.it/2013/01/magnetometer-calibration-helper-01-for.html#.UriTqkMjulM * * Ellipsoid fit from: * http://www.mathworks.com/matlabcentral/fileexchange/24693-ellipsoid-fit * * To use Eigen to convert matlab code, have a look at Eigen/AsciiQuickReference.txt */ if (mMagSamples.size() < 9) { QMessageBox::warning(this, "Magnetometer compensation", "Too few points."); return; } int samples = mMagSamples.size(); Eigen::VectorXd ex(samples); Eigen::VectorXd ey(samples); Eigen::VectorXd ez(samples); for (int i = 0;i < samples;i++) { ex(i) = mMagSamples.at(i).at(0); ey(i) = mMagSamples.at(i).at(1); ez(i) = mMagSamples.at(i).at(2); } Eigen::MatrixXd eD(samples, 9); for (int i = 0;i < samples;i++) { eD(i, 0) = ex(i) * ex(i); eD(i, 1) = ey(i) * ey(i); eD(i, 2) = ez(i) * ez(i); eD(i, 3) = 2.0 * ex(i) * ey(i); eD(i, 4) = 2.0 * ex(i) * ez(i); eD(i, 5) = 2.0 * ey(i) * ez(i); eD(i, 6) = 2.0 * ex(i); eD(i, 7) = 2.0 * ey(i); eD(i, 8) = 2.0 * ez(i); } Eigen::MatrixXd etmp1 = eD.transpose() * eD; Eigen::MatrixXd etmp2 = eD.transpose() * Eigen::MatrixXd::Ones(samples, 1); Eigen::VectorXd eV = etmp1.lu().solve(etmp2); Eigen::MatrixXd eA(4, 4); eA(0,0)=eV(0); eA(0,1)=eV(3); eA(0,2)=eV(4); eA(0,3)=eV(6); eA(1,0)=eV(3); eA(1,1)=eV(1); eA(1,2)=eV(5); eA(1,3)=eV(7); eA(2,0)=eV(4); eA(2,1)=eV(5); eA(2,2)=eV(2); eA(2,3)=eV(8); eA(3,0)=eV(6); eA(3,1)=eV(7); eA(3,2)=eV(8); eA(3,3)=-1.0; Eigen::MatrixXd eCenter = -eA.topLeftCorner(3, 3).lu().solve(eV.segment(6, 3)); Eigen::MatrixXd eT = Eigen::MatrixXd::Identity(4, 4); eT(3, 0) = eCenter(0); eT(3, 1) = eCenter(1); eT(3, 2) = eCenter(2); Eigen::MatrixXd eR = eT * eA * eT.transpose(); Eigen::SelfAdjointEigenSolver<Eigen::Matrix3d> eEv(eR.topLeftCorner(3, 3) * (-1.0 / eR(3, 3))); Eigen::MatrixXd eVecs = eEv.eigenvectors(); Eigen::MatrixXd eVals = eEv.eigenvalues(); Eigen::MatrixXd eRadii(3, 1); eRadii(0) = sqrt(1.0 / eVals(0)); eRadii(1) = sqrt(1.0 / eVals(1)); eRadii(2) = sqrt(1.0 / eVals(2)); Eigen::MatrixXd eScale = eRadii.asDiagonal().inverse() * eRadii.minCoeff(); Eigen::MatrixXd eComp = eVecs * eScale * eVecs.transpose(); mMagComp.resize(9); mMagComp[0] = eComp(0, 0); mMagComp[1] = eComp(0, 1); mMagComp[2] = eComp(0, 2); mMagComp[3] = eComp(1, 0); mMagComp[4] = eComp(1, 1); mMagComp[5] = eComp(1, 2); mMagComp[6] = eComp(2, 0); mMagComp[7] = eComp(2, 1); mMagComp[8] = eComp(2, 2); mMagCompCenter.resize(3); mMagCompCenter[0] = eCenter(0, 0); mMagCompCenter[1] = eCenter(1, 0); mMagCompCenter[2] = eCenter(2, 0); QVector<double> magX, magY, magZ; for (int i = 0;i < mMagSamples.size();i++) { double mx = mMagSamples.at(i).at(0); double my = mMagSamples.at(i).at(1); double mz = mMagSamples.at(i).at(2); mx -= mMagCompCenter.at(0); my -= mMagCompCenter.at(1); mz -= mMagCompCenter.at(2); magX.append(mx * mMagComp.at(0) + my * mMagComp.at(1) + mz * mMagComp.at(2)); magY.append(mx * mMagComp.at(3) + my * mMagComp.at(4) + mz * mMagComp.at(5)); magZ.append(mx * mMagComp.at(6) + my * mMagComp.at(7) + mz * mMagComp.at(8)); } ui->magSampXyPlot->graph(1)->setData(magX, magY); ui->magSampXzPlot->graph(1)->setData(magX, magZ); ui->magSampYzPlot->graph(1)->setData(magY, magZ); updateMagPlots(); }
bool ASMu3Dmx::assembleL2matrices (SparseMatrix& A, StdVector& B, const IntegrandBase& integrand, bool continuous) const { const int p1 = projBasis->order(0); const int p2 = projBasis->order(1); const int p3 = projBasis->order(2); // Get Gaussian quadrature points const int ng1 = continuous ? nGauss : p1 - 1; const int ng2 = continuous ? nGauss : p2 - 1; const int ng3 = continuous ? nGauss : p3 - 1; const double* xg = GaussQuadrature::getCoord(ng1); const double* yg = GaussQuadrature::getCoord(ng2); const double* zg = GaussQuadrature::getCoord(ng3); const double* wg = continuous ? GaussQuadrature::getWeight(nGauss) : nullptr; if (!xg || !yg || !zg) return false; if (continuous && !wg) return false; size_t nnod = this->getNoProjectionNodes(); double dV = 0.0; Vectors phi(2); Matrices dNdu(2); Matrix sField, Xnod, Jac; std::vector<Go::BasisDerivs> spl1(2); std::vector<Go::BasisPts> spl0(2); // === Assembly loop over all elements in the patch ========================== LR::LRSplineVolume* geoVol; if (m_basis[geoBasis-1]->nBasisFunctions() == projBasis->nBasisFunctions()) geoVol = m_basis[geoBasis-1].get(); else geoVol = projBasis.get(); for (const LR::Element* el1 : geoVol->getAllElements()) { double uh = (el1->umin()+el1->umax())/2.0; double vh = (el1->vmin()+el1->vmax())/2.0; double wh = (el1->wmin()+el1->wmax())/2.0; std::vector<size_t> els; els.push_back(projBasis->getElementContaining(uh, vh, wh) + 1); els.push_back(m_basis[geoBasis-1]->getElementContaining(uh, vh, wh) + 1); if (continuous) { // Set up control point (nodal) coordinates for current element if (!this->getElementCoordinates(Xnod,els[1])) return false; else if ((dV = 0.25*this->getParametricVolume(els[1])) < 0.0) return false; // topology error (probably logic error) } // Compute parameter values of the Gauss points over this element RealArray gpar[3], unstrGpar[3]; this->getGaussPointParameters(gpar[0],0,ng1,els[1],xg); this->getGaussPointParameters(gpar[1],1,ng2,els[1],yg); this->getGaussPointParameters(gpar[2],2,ng3,els[1],zg); // convert to unstructred mesh representation expandTensorGrid(gpar, unstrGpar); // Evaluate the secondary solution at all integration points if (!this->evalSolution(sField,integrand,unstrGpar)) return false; // set up basis function size (for extractBasis subroutine) const LR::Element* elm = projBasis->getElement(els[0]-1); phi[0].resize(elm->nBasisFunctions()); phi[1].resize(el1->nBasisFunctions()); IntVec lmnpc; if (projBasis != m_basis[0]) { lmnpc.reserve(phi[0].size()); for (const LR::Basisfunction* f : elm->support()) lmnpc.push_back(f->getId()); } const IntVec& mnpc = projBasis == m_basis[0] ? MNPC[els[1]-1] : lmnpc; // --- Integration loop over all Gauss points in each direction ---------- Matrix eA(phi[0].size(), phi[0].size()); Vectors eB(sField.rows(), Vector(phi[0].size())); int ip = 0; for (int k = 0; k < ng3; k++) for (int j = 0; j < ng2; j++) for (int i = 0; i < ng1; i++, ip++) { if (continuous) { projBasis->computeBasis(gpar[0][i], gpar[1][j], gpar[2][k], spl1[0], els[0]-1); SplineUtils::extractBasis(spl1[0],phi[0],dNdu[0]); m_basis[geoBasis-1]->computeBasis(gpar[0][i], gpar[1][j], gpar[2][k], spl1[1], els[1]-1); SplineUtils::extractBasis(spl1[1], phi[1], dNdu[1]); } else { projBasis->computeBasis(gpar[0][i], gpar[1][j], gpar[2][k], spl0[0], els[0]-1); phi[0] = spl0[0].basisValues; } // Compute the Jacobian inverse and derivatives double dJw = 1.0; if (continuous) { dJw = dV*wg[i]*wg[j]*wg[k]*utl::Jacobian(Jac,dNdu[1],Xnod,dNdu[1],false); if (dJw == 0.0) continue; // skip singular points } // Integrate the mass matrix eA.outer_product(phi[0], phi[0], true, dJw); // Integrate the rhs vector B for (size_t r = 1; r <= sField.rows(); r++) eB[r-1].add(phi[0],sField(r,ip+1)*dJw); } for (size_t i = 0; i < eA.rows(); ++i) { for (size_t j = 0; j < eA.cols(); ++j) A(mnpc[i]+1, mnpc[j]+1) += eA(i+1,j+1); int jp = mnpc[i]+1; for (size_t r = 0; r < sField.rows(); r++, jp += nnod) B(jp) += eB[r](1+i); } } return true; }
bool ASMs3Dmx::assembleL2matrices (SparseMatrix& A, StdVector& B, const IntegrandBase& integrand, bool continuous) const { const size_t nnod = projBasis->numCoefs(0) * projBasis->numCoefs(1) * projBasis->numCoefs(2); const int p1 = svol->order(0); const int p2 = svol->order(1); const int p3 = svol->order(2); const int p11 = projBasis->order(0); const int p21 = projBasis->order(1); const int p31 = projBasis->order(2); const int n1 = svol->numCoefs(0); const int n2 = svol->numCoefs(1); const int n3 = svol->numCoefs(2); const int nel1 = n1 - p1 + 1; const int nel2 = n2 - p2 + 1; const int nel3 = n3 - p3 + 1; // Get Gaussian quadrature point coordinates (and weights if continuous) const int ng1 = continuous ? nGauss : p1 - 1; const int ng2 = continuous ? nGauss : p2 - 1; const int ng3 = continuous ? nGauss : p3 - 1; const double* xg = GaussQuadrature::getCoord(ng1); const double* yg = GaussQuadrature::getCoord(ng2); const double* zg = GaussQuadrature::getCoord(ng3); const double* wg = continuous ? GaussQuadrature::getWeight(nGauss) : 0; if (!xg || !yg || !zg) return false; if (continuous && !wg) return false; // Compute parameter values of the Gauss points over the whole patch Matrix gp; std::array<RealArray,3> gpar; gpar[0] = this->getGaussPointParameters(gp,0,ng1,xg); gpar[1] = this->getGaussPointParameters(gp,1,ng2,yg); gpar[2] = this->getGaussPointParameters(gp,2,ng3,zg); // Evaluate basis functions at all integration points std::vector<Go::BasisPts> spl0; std::array<std::vector<Go::BasisDerivs>,2> spl1; if (continuous) { projBasis->computeBasisGrid(gpar[0],gpar[1],gpar[2],spl1[0]); svol->computeBasisGrid(gpar[0],gpar[1],gpar[2],spl1[1]); } else projBasis->computeBasisGrid(gpar[0],gpar[1],gpar[2],spl0); // Evaluate the secondary solution at all integration points Matrix sField; if (!this->evalSolution(sField,integrand,gpar.data())) { std::cerr <<" *** ASMs3D::assembleL2matrices: Failed for patch "<< idx+1 <<" nPoints="<< gpar[0].size()*gpar[1].size()*gpar[2].size() << std::endl; return false; } double dV = 1.0; std::array<Vector,2> phi; phi[0].resize(p11*p21*p31); phi[1].resize(p1*p2*p3); std::array<Matrix,2> dNdu; Matrix Xnod, J; // === Assembly loop over all elements in the patch ========================== int iel = 0; for (int i3 = 0; i3 < nel3; i3++) for (int i2 = 0; i2 < nel2; i2++) for (int i1 = 0; i1 < nel1; i1++, iel++) { if (MLGE[iel] < 1) continue; // zero-volume element if (continuous) { // Set up control point (nodal) coordinates for current element if (!this->getElementCoordinates(Xnod,1+iel)) return false; else if ((dV = 0.125*this->getParametricVolume(1+iel)) < 0.0) return false; // topology error (probably logic error) } int ip = ((i3*ng2*nel2 + i2)*ng1*nel1 + i1)*ng3; IntVec lmnpc; if (projBasis != m_basis[0]) { lmnpc.reserve(phi[0].size()); int nuv = projBasis->numCoefs(0)*projBasis->numCoefs(1); int widx = (spl1[0][ip].left_idx[2]-p31+1)*nuv; for (int k = 0; k < p31; ++k, widx += nuv) { int vidx = (spl1[0][ip].left_idx[1]-p21+1)*projBasis->numCoefs(0); for (int j = 0; j < p21; ++j, vidx += projBasis->numCoefs(0)) for (int i = 0; i < p11; ++i) if (continuous) lmnpc.push_back(spl1[0][ip].left_idx[0]-p11+1+i+vidx+widx); else lmnpc.push_back(spl0[ip].left_idx[0]-p11+1+i+vidx+widx); } } const IntVec& mnpc = projBasis == m_basis[0] ? MNPC[iel] : lmnpc; // --- Integration loop over all Gauss points in each direction -------- Matrix eA(p11*p21*p31, p11*p21*p31); Vectors eB(sField.rows(), Vector(p11*p21*p31)); for (int k = 0; k < ng3; k++, ip += ng2*(nel2-1)*ng1*nel1) for (int j = 0; j < ng2; j++, ip += ng1*(nel1-1)) for (int i = 0; i < ng1; i++, ip++) { if (continuous) { SplineUtils::extractBasis(spl1[0][ip],phi[0],dNdu[0]); SplineUtils::extractBasis(spl1[1][ip],phi[1],dNdu[1]); } else phi[0] = spl0[ip].basisValues; // Compute the Jacobian inverse and derivatives double dJw = dV; if (continuous) { dJw *= wg[i]*wg[j]*wg[k]*utl::Jacobian(J,dNdu[1],Xnod,dNdu[1],false); if (dJw == 0.0) continue; // skip singular points } // Integrate the mass matrix eA.outer_product(phi[0], phi[0], true, dJw); // Integrate the rhs vector B for (size_t r = 1; r <= sField.rows(); r++) eB[r-1].add(phi[0],sField(r,ip+1)*dJw); } for (int i = 0; i < p11*p21*p31; ++i) { for (int j = 0; j < p11*p21*p31; ++j) A(mnpc[i]+1, mnpc[j]+1) += eA(i+1, j+1); int jp = mnpc[i]+1; for (size_t r = 0; r < sField.rows(); r++, jp += nnod) B(jp) += eB[r](1+i); } } return true; }