typename ScalarTraits<BasisFunctionType>::RealType L2NormOfDifference( const GridFunction<BasisFunctionType, ResultType> &gridFunction, const Fiber::Function<ResultType> &refFunction, const Fiber::QuadratureStrategy<BasisFunctionType, ResultType, GeometryFactory> &quadStrategy, const EvaluationOptions &options) { // First, construct the evaluator. typedef Fiber::EvaluatorForIntegralOperators<ResultType> Evaluator; std::unique_ptr<Evaluator> evaluator = makeEvaluator(gridFunction, refFunction, quadStrategy, options); typedef typename Fiber::ScalarTraits<BasisFunctionType>::RealType MagnitudeType; typedef MagnitudeType CoordinateType; arma::Mat<CoordinateType> evaluationPoints(1, 1); evaluationPoints(0, 0) = 0.; arma::Mat<ResultType> resultMatrix; evaluator->evaluate(Evaluator::FAR_FIELD, evaluationPoints, resultMatrix); ResultType result = resultMatrix(0, 0); if (fabs(imagPart(result)) > 1000. * std::numeric_limits<MagnitudeType>::epsilon()) std::cout << "Warning: squared L2 norm has non-negligible imaginary part: " << imagPart(result) << std::endl; return sqrt(realPart(result)); }
CellMatrix // returns delta, gamma, vega, rho, theta BSGreeks(const MyMatrix& parametersMatrix) { if (parametersMatrix.columns() != 6 && parametersMatrix.rows() != 1 ) { throw("Input matrix should be 1 x 5");} double Spot = parametersMatrix(0,0); double Strike = parametersMatrix(0,1); double r = parametersMatrix(0,2); double d = parametersMatrix(0,3); double vol = parametersMatrix(0,4); double expiry = parametersMatrix(0,5); CellMatrix resultMatrix(1,5); resultMatrix(0,0) = BlackScholesDelta(Spot,Strike,r,d,vol,expiry); resultMatrix(0,1) = BlackScholesGamma(Spot,Strike,r,d,vol,expiry); resultMatrix(0,2) = BlackScholesVega(Spot,Strike,r,d,vol,expiry); resultMatrix(0,3) = BlackScholesRho(Spot,Strike,r,d,vol,expiry); resultMatrix(0,4) = BlackScholesTheta(Spot,Strike,r,d,vol,expiry); return resultMatrix; }
CellMatrix // BSGreeksFD(const MyMatrix& parametersMatrix,double epsilon) { if (parametersMatrix.columns() != 6 && parametersMatrix.rows() != 1 ) { throw("Input matrix should be 1 x 5");} double Spot = parametersMatrix(0,0); double Strike = parametersMatrix(0,1); double r = parametersMatrix(0,2); double d = parametersMatrix(0,3); double vol = parametersMatrix(0,4); double expiry = parametersMatrix(0,5); CellMatrix resultMatrix(1,5); resultMatrix(0,0) = BlackScholesDeltaFD(Spot,Strike,r,d,vol,expiry,epsilon); resultMatrix(0,1) = BlackScholesGammaFD(Spot,Strike,r,d,vol,expiry,epsilon); resultMatrix(0,2) = BlackScholesVegaFD(Spot,Strike,r,d,vol,expiry,epsilon); resultMatrix(0,3) = BlackScholesRhoFD(Spot,Strike,r,d,vol,expiry,epsilon); resultMatrix(0,4) = BlackScholesThetaFD(Spot,Strike,r,d,vol,expiry,epsilon); return resultMatrix; }
RcppExport SEXP RXMLAExecute(SEXP handle, SEXP query, SEXP rPropertiesString) { XmlaWebServiceSoapProxy service = XmlaWebServiceSoapProxy(SOAP_XML_DEFAULTNS, SOAP_XML_DEFAULTNS); Rcpp::XPtr<XMLAHandle> ptr(handle); const char *connectionString = ptr->connectionString; std::string propertiesString = CHAR(STRING_ELT(rPropertiesString,0)); ns1__Session session; std::string sessionId = ptr->sessionID; session.SessionId = &sessionId; service.soap_header(NULL, NULL, &session, NULL); _ns1__Execute execute; ns1__CommandStatement command; ns1__Properties properties; ns1__PropertyList propertyList; _ns1__ExecuteResponse response; std::string statement = CHAR(STRING_ELT(query,0)); command.Statement = &statement; execute.Command = &command; execute.Properties = &properties; properties.PropertyList = &propertyList; if (!propertiesString.empty()) { parseKeyValuePairs(&propertiesString, propertyList.__any); } service.userid = ptr->userName; service.passwd = ptr->password; if (service.Execute(connectionString, NULL, &execute, &response) == SOAP_OK) { // Parse MDDataSet if (response.return_->ns4__root != NULL && response.return_->ns4__root->__union_ResultXmlRoot != NULL && response.return_->ns4__root->__union_ResultXmlRoot->Axes != NULL) { if (response.return_->ns4__root->__union_ResultXmlRoot->Axes->Axis.size() < 3) { std::cerr << "Error: No data on Axis1" << std::endl; return Rcpp::wrap(false); } if (response.return_->ns4__root->__union_ResultXmlRoot->Axes->Axis.size() > 3) { std::cerr << "Error: More than 2 axes not supported" << std::endl; return Rcpp::wrap(false); } ns4__Axes *axes = response.return_->ns4__root->__union_ResultXmlRoot->Axes; std::vector<ns4__Cell *> cellDataVector = response.return_->ns4__root->__union_ResultXmlRoot->CellData->Cell; int numCols = response.return_->ns4__root->__union_ResultXmlRoot->Axes->Axis[0]->__union_Axis->Tuples->Tuple.size(); int numRows = response.return_->ns4__root->__union_ResultXmlRoot->Axes->Axis[1]->__union_Axis->Tuples->Tuple.size(); int cellDataVectorMember = 0; Rcpp::CharacterVector colNames; Rcpp::CharacterVector rowNames; Rcpp::NumericMatrix resultMatrix(numRows, numCols); for (int row = 0; row < numRows; row++) { for (int col = 0; col < numCols; col++) { if (cellDataVector[cellDataVectorMember]->CellOrdinal == ((row * numCols) + col)) { resultMatrix(row, col) = *cellDataVector[cellDataVectorMember]->Value; if (cellDataVectorMember < cellDataVector.size() - 1) { cellDataVectorMember += 1; } } else { resultMatrix(row, col) = NA_REAL; } } mdDataSetGetNames(rowNames, axes, row, true); } for (int col = 0; col < numCols; col++) { mdDataSetGetNames(colNames, axes, col, false); } colNames.push_front("Row Names"); Rcpp::DataFrame resultDataFrame(resultMatrix); resultDataFrame.push_front(rowNames); resultDataFrame.attr("names") = colNames; service.destroy(); return resultDataFrame; } // Parse RowSet else if (response.return_->ns2__root != NULL && response.return_->ns2__root->xsd__schema != NULL && response.return_->ns2__root->__union_ResultXmlRoot != NULL && !response.return_->ns2__root->__union_ResultXmlRoot->row.empty()) { std::string rawXML = "<root xmlns=\"urn:schemas-microsoft-com:xml-analysis:rowset\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><xsd:schema targetNamespace=\"urn:schemas-microsoft-com:xml-analysis:rowset\" xmlns:sql=\"urn:schemas-microsoft-com:xml-sql\" elementFormDefault=\"qualified\">"; rawXML = rawXML + response.return_->ns2__root->xsd__schema + "</xsd:schema></root>"; char *schema = strdup(rawXML.c_str()); rapidxml::xml_document<> doc; doc.parse<0>(schema); // Find XML section containing column names rapidxml::xml_node<char> *rowNode = doc.first_node()->first_node()->first_node("xsd:complexType"); while(rowNode != NULL && strcmp(rowNode->first_attribute("name")->value(), "row") != 0) { rowNode = rowNode->next_sibling("xsd:complexType"); } rapidxml::xml_node<char> *schemaElementNode = rowNode->first_node()->first_node(); std::vector<char *> rows = response.return_->ns2__root->__union_ResultXmlRoot->row; Rcpp::DataFrame resultDataFrame; Rcpp::CharacterVector colNames; char *colName; while(schemaElementNode != NULL) { colName = schemaElementNode->first_attribute("name")->value(); colNames.push_back(colName); if (schemaElementNode->first_attribute("type") != 0) { rowSetParseData(rows, &resultDataFrame, colName, true); } else { rowSetParseData(rows, &resultDataFrame, colName, false); } schemaElementNode = schemaElementNode->next_sibling(); } resultDataFrame.attr("names") = colNames; service.destroy(); return resultDataFrame; } service.destroy(); return Rcpp::wrap(true); } else { char * errorMessage = service.fault->detail->__any; std::cerr << errorMessage << std::endl; } service.destroy(); return Rcpp::wrap(false); }