//create ,pass and recieve a simple dictionary. [Note Dictionary will be // homogenous. This demo gives an idea on how to deal withthe heterogenous // case.] int eg6() { int i=0,l=2; K keys=ktn(KS,l); K vals=ktn(KI,l); K dict; K result,resultk,resultv; //A dictionary is a mapping of two conforming lists. kI(vals)[0]=1; kI(vals)[1]=2; kS(keys)[0]=ss("key1"); kS(keys)[1]=ss("key2"); // Turn these into a dictionary dict=xD(keys,vals); result=k(c,"{[x]x[`key3]:`int$.z.t;show x;x}",dict,(K)0); printf( " This return has [type=%i] and contains %i enclosed elements\n\n\n",result->t,result->i); //Extract the keys and value vectors from the k bject resultk=kK(result)[0]; resultv=kK(result)[1]; //extract the lenth of the vectors l=resultk->n; printf( " The returned dictionary has %i elements.\n",l); for(i=0;i<l;i++) { printf( " The %i key is %s \n",i,kS(resultk)[i]); printf( " The associated value is %i \n",kI(resultv)[i]); } return 1; }
//create ,pass and recieve a simple table. int eg7() { K cc,d,e,v,tab; K flip,result,columnNames,columnData; int row,col,nCols,nRows; cc=ktn(KS,2);kS(cc)[0]=ss("pid");kS(cc)[1]=ss("uid"); d=ktn(KS,3);kS(d)[0]=ss("ibm");kS(d)[1]=ss("gte");kS(d)[2]=ss("kvm"); e=ktn(KI,3);kI(e)[0]=1;kI(e)[1]=2;kI(e)[2]=3; v=knk(2,d,e); tab=xT(xD(cc,v)); flip=k(c,"{[x]a:update t:.z.t,y:.z.d from x;.tst.t:a;a}",tab,(K)0); //Turn into a dictionary. flip->k [transpose?] //Display table. [Borrowed from code.kx.com: // https://code.kx.com/trac/attachment/wiki/Cookbook/InterfacingWithC/csv.c ] columnNames=kK(flip->k)[0]; columnData=kK(flip->k)[1]; nCols=columnNames->n; nRows=kK(columnData)[0]->n; for(row=0;row<nRows;row++) { if(0==row) { for(col=0;col<nCols;col++) { if(col>0)printf(","); printf("%s",kS(columnNames)[col]); } printf("\n"); } for(col=0;col<nCols;col++) { K obj=kK(columnData)[col]; if(col>0)printf(","); switch(obj->t) { case(1):{printf("%d",kG(obj)[row]);}break; case(4):{printf("%d",kG(obj)[row]);}break; case(5):{printf("%d",kH(obj)[row]);}break; case(6):{printf("%d",kI(obj)[row]);}break; case(7):{printf("%lld",kJ(obj)[row]);}break; case(8):{printf("%f",kE(obj)[row]);}break; case(9):{printf("%f",kF(obj)[row]);}break; case(11):{printf("%s",kS(obj)[row]);}break; case(19):{printf("%i",kI(obj)[row]);}break; case(14):{printf("%i",kI(obj)[row]);}break; default:{printf("unknown type");}break; } } printf("\n"); } return 1; }
// ----------------------------------------------------------------------------- // // ----------------------------------------------------------------------------- void StatsGenPlotWidget::createPowerCurve(int tableRow, float& xMax, float& yMax) { QwtPlotCurve* curve = m_PlotCurves[tableRow]; int err = 0; float alpha = m_TableModel->getDataValue(SGPowerLawTableModel::Alpha, tableRow); float k = m_TableModel->getDataValue(SGPowerLawTableModel::K, tableRow); float beta = m_TableModel->getDataValue(SGPowerLawTableModel::Beta, tableRow); int size = 256; QwtArray<float> x; QwtArray<float> y; err = StatsGen::GenPowerLawPlotData<QwtArray<float > > (alpha, k, beta, x, y, size); if (err == 1) { //TODO: Present Error Message return; } QwtArray<double> xD(size); QwtArray<double> yD(size); for (int i = 0; i < size; ++i) { // qDebug() << x[i] << " " << y[i] << "\n"; if (x[i] > xMax) { xMax = x[i]; } if (y[i] > yMax) { yMax = y[i]; } xD[i] = static_cast<double>(x[i]); yD[i] = static_cast<double>(y[i]); } #if QWT_VERSION >= 0x060000 curve->setSamples(xD, yD); #else curve->setData(xD, yD); #endif m_PlotView->setAxisScale(QwtPlot::yLeft, 0.0, yMax); m_PlotView->setAxisScale(QwtPlot::xBottom, 0.0, xMax); }
// ----------------------------------------------------------------------------- // // ----------------------------------------------------------------------------- void StatsGenRDFWidget::updateRDFPlot(QVector<float>& freqs) { // These are the output vectors QwtArray<double> xD(static_cast<int>(freqs.size())); QwtArray<double> yD(static_cast<int>(freqs.size())); QLocale loc = QLocale::system(); bool ok = false; float minDist = loc.toFloat(minDistLE->text(), &ok); float maxDist = loc.toFloat(maxDistLE->text(), &ok); const int numValues = freqs.size(); float increment = (maxDist - minDist) / numValues; double pos = minDist; for (qint32 i = 0; i < numValues; ++i) { xD[i] = pos; yD[i] = static_cast<double>(freqs.at(i)); pos = pos + increment; } // This will actually plot the XY data in the Qwt plot widget QwtPlotCurve* curve = m_PlotCurve; #if QWT_VERSION >= 0x060000 curve->setSamples(xD, yD); #else curve->setData(xD, yD); #endif curve->setStyle(QwtPlotCurve::Lines); //Use Antialiasing to improve plot render quality curve->setRenderHint( QwtPlotItem::RenderAntialiased, true ); QPen pen; pen.setColor(Qt::white); pen.setWidth(2); curve->setPen(pen);//Set colour and thickness for drawing the curve curve->attach(m_RDFPlot); m_RDFPlot->replot(); }
void Dialog::mkSensor() { QCPGraph * gr; QPen pn; int steps = (20) + 1; double stepTemp = 0.47; QVector<double> xLabels; QVector<double> yLabels; QVector<double> xD(steps), yD(steps),yDlin(steps); for (int x=0; x<steps; x++) { //xD[x] = 30.0 + (stepTemp * (double)x) + 0.5 * ((double)(100 - qrand()%200) / 100.0); xD[x] = 30.0 + x; yD[x] = 158 + floor( ((double)x / 0.47) + 1.5 * ((double)(100 - qrand()%200) / 100.0) ); yDlin[x] = 158.0 + ((double)x / 0.47); //if (((int)yD[x])%50 == 0) yLabels.push_back(yD[x]); //if (((int)xD[x])%10 == 0) xLabels.push_back(xD[x]); } for (int x=150; x<=210; x+=10) { yLabels.push_back(x); } for (int x=30; x<=50; x+=1) { xLabels.push_back(x); } qDebug() << yD; qDebug() << xD; gr = ui->plot->addGraph(); gr->setData(xD, yD); pn.setColor(Qt::red); pn.setWidth(3); gr->setPen(pn); gr = ui->plot->addGraph(); gr->setData(xD, yDlin); pn.setColor(0x008080); pn.setWidth(1); pn.setStyle(Qt::DashLine); gr->setPen(pn); ui->plot->xAxis->setLabel("Температура, °C"); ui->plot->xAxis->setRange(30, 50); ui->plot->xAxis->setAutoTicks(false); ui->plot->xAxis->setTickVector(xLabels); ui->plot->yAxis->setLabel("Сигнал АЦП"); ui->plot->yAxis->setRange(145, 215); ui->plot->yAxis->setAutoTicks(false); ui->plot->yAxis->setTickVector(yLabels); ui->plot->replot(); ui->plot->savePdf("graph-sensor.pdf", false, 800, 400); }
// ----------------------------------------------------------------------------- // // ----------------------------------------------------------------------------- void StatsGenMDFWidget::updateMDFPlot(QVector<float>& odf) { int err = 0; int size = 100000; // These are the input vectors QVector<float> angles; QVector<float> axes; QVector<float> weights; angles = m_MDFTableModel->getData(SGMDFTableModel::Angle); weights = m_MDFTableModel->getData(SGMDFTableModel::Weight); axes = m_MDFTableModel->getData(SGMDFTableModel::Axis); // These are the output vectors QVector<float> x; QVector<float> y; if ( Ebsd::CrystalStructure::Cubic_High == m_CrystalStructure ) { // Allocate a new vector to hold the mdf data QVector<float> mdf(CubicOps::k_MdfSize); // Calculate the MDF Data using the ODF data and the rows from the MDF Table model Texture::CalculateMDFData<float, CubicOps>(angles.data(), axes.data(), weights.data(), odf.data(), mdf.data(), static_cast<size_t>(angles.size())); // Now generate the actual XY point data that gets plotted. int npoints = 13; x.resize(npoints); y.resize(npoints); err = StatsGen::GenCubicMDFPlotData(mdf.data(), x.data(), y.data(), npoints, size); if (err < 0) { return; } } else if ( Ebsd::CrystalStructure::Hexagonal_High == m_CrystalStructure ) { // Allocate a new vector to hold the mdf data QVector<float> mdf(HexagonalOps::k_MdfSize); // Calculate the MDF Data using the ODF data and the rows from the MDF Table model Texture::CalculateMDFData<float, HexagonalOps>(angles.data(), axes.data(), weights.data(), odf.data(), mdf.data(), static_cast<size_t>(angles.size())); // Now generate the actual XY point data that gets plotted. int npoints = 20; x.resize(npoints); y.resize(npoints); err = StatsGen::GenHexMDFPlotData(mdf.data(), x.data(), y.data(), npoints, size); if (err < 0) { return; } } QwtArray<double> xD(static_cast<int>(x.size())); QwtArray<double> yD(static_cast<int>(x.size())); for (qint32 i = 0; i < x.size(); ++i) { xD[i] = static_cast<double>(x.at(i)); yD[i] = static_cast<double>(y.at(i)); } // This will actually plot the XY data in the Qwt plot widget QwtPlotCurve* curve = m_PlotCurve; #if QWT_VERSION >= 0x060000 curve->setSamples(xD, yD); #else curve->setData(xD, yD); #endif QColor color = QColor("DodgerBlue"); curve->setPen(color, 2); curve->setRenderHint( QwtPlotItem::RenderAntialiased, true ); curve->setStyle(QwtPlotCurve::Lines); QwtSymbol *symbol = new QwtSymbol( QwtSymbol::Ellipse, QBrush( Qt::white ), QPen( color, 2 ), QSize( 8, 8 ) ); curve->setSymbol( symbol ); curve->attach(m_MDFPlot); m_MDFPlot->replot(); }
static int enc(K*k,lua_State *L) { switch (lua_type(L, -1)) { case LUA_TSTRING: { size_t len;const char *str = lua_tolstring(L,-1,&len);(*k)=kpn(str,len);R 1;} break; case LUA_TNUMBER: { F num = lua_tonumber(L,-1);(*k) = (num==floor(num))?kj((J)num):kf(num);R 1;} break; case LUA_TBOOLEAN: { (*k)=kb( lua_toboolean(L,-1) );R 1;} break; case LUA_TNIL: { (*k)=ktn(0,0);R 1;} break; case LUA_TTABLE: { double p; int max = 0; int items = 0; int t_integer = 0, t_number = 0, t_boolean = 0, t_other= 0; lua_pushnil(L); /* table, startkey */ while (lua_next(L, -2) != 0) { items++; /* table, key, value */ switch (lua_type(L, -1)) { case LUA_TNUMBER: t_number++; p = lua_tonumber(L,-1); t_integer += (floor(p) == p); break; case LUA_TBOOLEAN: t_boolean++; break; default: t_other++; break; /* or anything else */ }; if (lua_type(L, -2) == LUA_TNUMBER && (p = lua_tonumber(L, -2))) { /* Integer >= 1 ? */ if (floor(p) == p && p >= 1) { if (p > max) max = p; lua_pop(L, 1); continue; } } /* Must not be an array (non integer key) */ for (lua_pop(L,1); lua_next(L, -2) != 0; lua_pop(L,1)) ++items; max = 0; break; } lua_pushnil(L); if (max != items) { /* build K dictionary */ K keys = ktn(KS,items); K values = ktn(0,items); int n = 0; /* table, startkey */ while (lua_next(L, -2) != 0) { kS(keys)[n] = ss(lua_tostring(L, -2)); if(!enc(kK(values)+n,L))R 0; lua_pop(L,1); ++n; } *k = xD(keys,values); R 1; } /* build K list */ if(t_other || ((!!t_boolean)+(!!t_number)) > 1) { K a = ktn(0,items); while (lua_next(L, -2) != 0) { p = lua_tonumber(L, -2); if(!enc(kK(a)+LI(p),L))R 0; lua_pop(L, 1); } *k = a; R 1; } if(t_boolean) { K a = ktn(KB,items); while (lua_next(L, -2) != 0) { p = lua_tonumber(L, -2); kG(a)[LI(p)] = lua_toboolean(L,-1); lua_pop(L, 1); } *k = a; R 1; } if(t_number == t_integer) { K a = ktn(KJ,items); while (lua_next(L, -2) != 0) { p = lua_tonumber(L, -2); kJ(a)[LI(p)] = (int)floor(lua_tonumber(L,-1)); lua_pop(L, 1); } *k = a; R 1; } if(t_number) { K a = ktn(KF,items); while (lua_next(L, -2) != 0) { p = lua_tonumber(L, -2); kF(a)[LI(p)] = lua_tonumber(L,-1); lua_pop(L, 1); } *k = a; R 1; } *k = ktn(0,0); R 1; }; break; default: luaL_error(L, "Cannot serialise %s: %s", lua_typename(L, lua_type(L, -1)), "can't serialize type"); R 0; }; }
K eval(K x,K y,K z){K*k;S*b,s;SQLULEN w;SQLLEN*nb;SQLINTEGER*wb;H*tb,u,t,j=0,p,m;F f;C c[128];I n=xj<0;D d=d1(n?-xj:xj);U(d)x=y;Q(z->t!=-KJ||xt!=-KS&&xt!=KC,"type") if(z->j)SQLSetStmtAttr(d,SQL_ATTR_QUERY_TIMEOUT,(SQLPOINTER)(SQLULEN)z->j,0); if(xt==-KS)Q1(SQLColumns(d,(S)0,0,(S)0,0,xs,S0,(S)0,0))else{I e;K q=kpn(xG,xn);ja(&q,"\0");e=SQLExecDirect(d,q->G0,xn);r0(q);Q1(e)} SQLNumResultCols(d,&j);P(!j,(d0(d),knk(0))) b=malloc(j*SZ),tb=malloc(j*2),wb=malloc(j*SZ),nb=malloc(j*SZ),x=ktn(KS,j),y=ktn(0,j);// sqlserver: no bind past nonbind DO(j,Q1(SQLDescribeCol(d,(H)(i+1),c,128,&u,&t,&w,&p,&m))xS[i]=sn(c,u); if(t>90)t-=82; Q(t<-11||t>12,xS[i])wb[i]=ut[tb[i]=t=t>0?t:12-t]==KS&&w?w+1:wt[t];if(ut[t]==KS&&(n||!wb[i]||wb[i]>9))tb[i]=13) DO(j,kK(y)[i]=ktn(ut[t=tb[i]],0);if(w=wb[i])Q1(SQLBindCol(d,(H)(i+1),ct[t],b[i]=malloc(w),w,nb+i))) for(;SQL_SUCCEEDED(SQLFetch(d));)DO(j,k=kK(y)+i;u=ut[t=tb[i]];s=b[i];n=SQL_NULL_DATA==(int)nb[i]; if(!u)jk(k,n?ktn(ct[t]?KC:KG,0):wb[i]?kp(s):gb(d,(H)(i+1),t)); else ja(k,n?nu(u):u==KH&&wb[i]==1?(t=(H)*s,(S)&t):u==KS?(s=dtb(s,nb[i]),(S)&s):u<KD?s:u==KZ?(f=ds(s)+(vs(s+6)+*(I*)(s+12)/1e9)/8.64e4,(S)&f):(w=u==KD?ds(s):vs(s),(S)&w))) if(!SQLMoreResults(d))O("more\n");DO(j,if(wb[i])free(b[i]))R free(b),free(tb),free(wb),free(nb),d0(d),xT(xD(x,y));}