void print_(Node *root) { if (root) { std::cout << root->data << " "; print_(root->left); print_(root->right); } }
void plotly::plot(unsigned long x, int y){ jsonStart(len_(x)+len_(y)); print_(x); jsonMiddle(); print_(y); jsonEnd(); }
void plotly::plot(String x, float y){ jsonStart(len_(x)+len_(y)); print_(x); jsonMiddle(); print_(y); jsonEnd(); }
void plotly::plot(char *x, int y){ jsonStart(len_(x)+len_(y)); print_(x); jsonMiddle(); print_(y); jsonEnd(); }
void plotly::plot(unsigned long x, int y, char *token){ reconnectStream(); jsonStart(len_(x)+len_(y)); print_(x); jsonMiddle(); print_(y); jsonEnd(token); }
void plotly::openStream() { // // Start request to stream servers // if(log_level < 3){} Serial.println(F("... Connecting to plotly's streaming servers...")); char server[] = "arduino.plot.ly"; int port = 80; while ( !client.connect(server, port) ) { if(log_level < 4) Serial.println(F("... Couldn\'t connect to servers... trying again!")); fibonacci_ += fibonacci_; delay(min(fibonacci_, 60000)); } fibonacci_ = 1; if(log_level < 3){} Serial.println(F("... Connected to plotly's streaming servers\n... Initializing stream")); print_(F("POST / HTTP/1.1\r\n")); print_(F("Host: arduino.plot.ly\r\n")); print_(F("User-Agent: Python\r\n")); print_(F("Transfer-Encoding: chunked\r\n")); print_(F("Connection: close\r\n")); if(convertTimestamp){ print_(F("plotly-convertTimestamp: \"")); print_(timezone); print_(F("\"\r\n")); } print_(F("\r\n")); if(log_level < 3){} Serial.println(F("... Done initializing, ready to stream!")); }
void plotly::send_prepad_(){ // print [[ or [ to the client if the start of the matrix or row mi_ += 1; if(mi_ == 1){ ni_ += 1; if(ni_==1){ print_("[[",2); } else{ print_("[",1); } } }
void plotly::plot(unsigned long x, float y, char *token){ reconnectStream(); char s_[15]; my_dtostrf(y,2,3,s_); jsonStart(len_(x)+len_(s_)-1); print_(x); jsonMiddle(); print_(y); jsonEnd(token); }
void plotly::sendString_(float d){ send_prepad_(); char s_[width_]; dtostrf(d,width_,prec_,s_); print_(s_,width_); send_postpad_(); }
void print_base16(void) /* Write a function that prints all the numbers of base 16 from 0 to F */ { char c; for (c = '0'; c <= '9'; c = 'A'; c <= 'F'; ++c) /* Work in progress */ print_(c); }
void plotly::jsonStart(int i){ // Print the length of the message in hex: // 15 char for the json that wraps the data: {"x": , "y": }\n // + 23 char for the token: , "token": "abcdefghij" // = 38 if(log_level<2) Serial.print(i+44, HEX); if(!dry_run) client.print(i+44, HEX); print_("\r\n{\"x\": "); }
void MyArray<T>:: dump(std::ostream& os) { os << "\nno of dimensions: " << ndim << "\nsizes: "; int i; for (i = 0; i < ndim; i++) os << size[i] << " "; os << "length: " << length << " ptr: " << A << std::endl; print_(os); }
void plotly::send_postpad_(){ // print ", ", ", ]", "], ", "]]" depending on the value of the mi_, ni_ counters if(mi_ == 1){ if(ni_==1){ print_(", ", 2); } else{ print_(", ", 2); } } else if(mi_ == M_ && ni_ < N_){ print_("], ", 3); mi_ = 0; } else if(mi_ == M_ && ni_ == N_){ print_("]]", 2); } else{ print_(", ", 2); } if(mi_ == M_ && ni_ == N_){ close_stream(); } }
int main() { int num,i; int a[10]={4,6,8,2,9,1,3,10,5,7}; printf("please input one number from 0ne to five:\n"); scanf("%d",&num); switch(num) { case 1: select_sort(a,10);print_(a,10);break; case 2: insert_sort(a,10);print_(a,10);break; case 3: bubble_sort(a,10);print_(a,10);break; case 4: quick_sort(a,10);print_(a,10);break; case 5: heap_sort(a,10);print_(a,10);break; default: printf("input error");break; } return 0; }
void plotly::openStream() { // // Start request to stream servers // if(log_level < 3){} Serial.println(F("... Connecting to plotly's streaming servers...")); /* #define STREAM_SERVER "arduino.plot.ly" uint32_t stream_ip = 0; // Try looking up the website's IP address while (stream_ip == 0) { if (! cc3000.getHostByName(STREAM_SERVER, &stream_ip)) { if(log_level < 4){} Serial.println(F("Couldn't resolve!")); } } */ uint32_t stream_ip = cc3000.IP2U32(54, 226, 153, 102); client = cc3000.connectTCP(stream_ip, 80); while ( !client.connected() ) { if(log_level < 4){} Serial.println(F("... Couldn\'t connect to servers... trying again!")); fibonacci_ += fibonacci_; delay(min(fibonacci_, 60000)); client = cc3000.connectTCP(stream_ip, 80); } fibonacci_ = 1; if(log_level < 3){} Serial.println(F("... Connected to plotly's streaming servers\n... Initializing stream")); print_(F("POST / HTTP/1.1\r\n")); print_(F("Host: 127.0.0.1\r\n")); print_(F("User-Agent: Python\r\n")); print_(F("Transfer-Encoding: chunked\r\n")); print_(F("Connection: close\r\n")); if(convertTimestamp){ print_(F("plotly-convertTimestamp: \"")); print_(timezone); print_(F("\"")); } print_(F("\r\n\r\n")); if(log_level < 3){} Serial.println(F("... Done initializing, ready to stream!")); }
void plotly::jsonEnd(char *token){ print_(", \"streamtoken\": \""); print_(token); print_("\"}\n\r\n"); }
void plotly::jsonMiddle(){ print_(", \"y\": "); }
void fatal(const std::string & msg) { print_(_fatal, msg); }
bool plotly::init(){ // // Validate a stream with a REST post to plotly // if(dry_run && log_level < 3){ Serial.println(F("... This is a dry run, we are not connecting to plotly's servers...")); } else if(log_level < 3) { Serial.println(F("... Attempting to connect to plotly's REST servers")); } while ( !client.connect("plot.ly", 80) ) { if(log_level < 4){ Serial.println(F("... Couldn\'t connect to plotly's REST servers... trying again!")); } fibonacci_ += fibonacci_; delay(min(fibonacci_, 60000)); } fibonacci_ = 1; if(log_level < 3){} Serial.println(F("... Connected to plotly's REST servers")); if(log_level < 3){} Serial.println(F("... Sending HTTP Post to plotly")); print_(F("POST /clientresp HTTP/1.1\r\n")); print_(F("Host: plot.ly\r\n")); print_(F("User-Agent: SparkCore/0.0.1\r\n")); print_(F("Content-Type: application/x-www-form-urlencoded\r\n")); print_(F("Content-Length: ")); int contentLength = 126 + len_(username_) + len_(fileopt) + nTraces_*(87+len_(maxpoints)) + (nTraces_-1)*2 + len_(filename_); if(world_readable){ contentLength += 4; } else{ contentLength += 5; } print_(contentLength); // contentLength = // 44 // first part of querystring below // + len_(username) // upper bound on username length // + 5 // &key= // + 10 // api_key length // + 7 // &args=[... // + nTraces*(87+len(maxpoints)) // len({\"y\": [], \"x\": [], \"type\": \"scatter\", \"stream\": {\"token\": \") + 10 + len(\", "maxpoints": )+len(maxpoints)+len(}}) // + (nTraces-1)*2 // ", " in between trace objects // + 22 // ]&kwargs={\"fileopt\": \" // + len_(fileopt) // + 16 // \", \"filename\": \" // + len_(filename) // + 21 // ", "world_readable": // + 4 if world_readable, 5 otherwise // + 1 // closing } //------ // 126 + len_(username) + len_(fileopt) + nTraces*(86+len(maxpoints)) + (nTraces-1)*2 + len_(filename) // // Terminate headers with new lines print_(F("\r\n\r\n")); // Start printing querystring body print_(F("version=2.3&origin=plot&platform=arduino&un=")); print_(username_); print_(F("&key=")); print_(api_key_); print_(F("&args=[")); // print a trace for each token supplied for(int i=0; i<nTraces_; i++){ print_(F("{\"y\": [], \"x\": [], \"type\": \"scatter\", \"stream\": {\"token\": \"")); print_(stream_tokens_[i]); print_(F("\", \"maxpoints\": ")); print_(maxpoints); print_(F("}}")); if(nTraces_ > 1 && i != nTraces_-1){ print_(F(", ")); } } print_(F("]&kwargs={\"fileopt\": \"")); print_(fileopt); print_(F("\", \"filename\": \"")); print_(filename_); print_(F("\", \"world_readable\": ")); if(world_readable){ print_("true"); } else{ print_("false"); } print_(F("}")); // final newline to terminate the POST print_(F("\r\n")); // // Wait for a response // Parse the response for the "All Streams Go!" and proceed to streaming // if we find it // char allStreamsGo[] = "All Streams Go!"; int asgCnt = 0; // asg stands for All Streams Go char url[] = "\"url\": \"http://plot.ly/~"; char fid[4]; int fidCnt = 0; int urlCnt = 0; int usernameCnt = 0; bool proceed = false; bool fidMatched = false; if(log_level < 2){ Serial.println(F("... Sent message, waiting for plotly's response...")); } if(!dry_run){ while (client.connected() && (!proceed || client.available())) { if (client.available()) { char c = client.read(); if(log_level < 2) Serial.print(c); // // Attempt to read the "All streams go" msg if it exists // by comparing characters as they roll in // if(asgCnt == len_(allStreamsGo) && !proceed){ proceed = true; } else if(allStreamsGo[asgCnt]==c){ asgCnt += 1; } else if(asgCnt > 0){ // reset counter asgCnt = 0; } // // Extract the last bit of the URL from the response // The url is in the form http://107.21.214.199/~USERNAME/FID // We'll character-count up through char url[] and through username_, then start // filling in characters into fid // if(log_level < 3){ if(url[urlCnt]==c && urlCnt < len_(url)){ urlCnt += 1; } else if(urlCnt > 0 && urlCnt < len_(url)){ // Reset counter urlCnt = 0; } if(urlCnt == len_(url) && fidCnt < 4 && !fidMatched){ // We've counted through the url, start counting through the username if(usernameCnt < len_(username_)+2){ usernameCnt += 1; } else { // the url ends with " if(c != '"'){ fid[fidCnt] = c; fidCnt += 1; } else if(fidCnt>0){ fidMatched = true; } } } } } } client.stop(); } if(!dry_run && !proceed && log_level < 4){ Serial.println(F("... Error initializing stream, aborting. Try again or get in touch with Chris at [email protected]")); } if(!dry_run && proceed && log_level < 3){ Serial.println(F("... A-ok from plotly, All Streams Go!")); if(fidMatched){ Serial.print(F("... View your streaming plot here: https://plot.ly/~")); Serial.print(username_); Serial.print(F("/")); for(int i=0; i<fidCnt; i++){ Serial.print(fid[i]); } Serial.println(F("")); } } return proceed; }
void plotly::closeStream(){ print_(F("0\r\n\r\n")); client.stop(); }
void plotly::open_stream(int N, int M, char *filename_, char *layout){ N_ = N; // total sets of data sent = number of rows in plotly-json data matrix M_ = (M*2); // total number of traces * 2 = number of columns in plotly-json data matrix ni_ = 0; // counter of number of sets of data set (number of rows in the plotly-json data matrix) transmitted mi_ = 0; // counter of points sent in each row of data nChar_ = 0; // counter of number of characters transmitted filename=filename_; delay(1000); if(DRY_RUN){ Serial.println("This is a dry run, we are not connecting to plotly's servers..."); } else{ if(VERBOSE) { Serial.println("Attempting to connect to plotly's servers..."); } char server[] = "plot.ly"; while ( !client.connect(server, 443) ) { if(VERBOSE){ Serial.println("Couldn\'t connect to servers.... trying again!"); } delay(1000); } } if(VERBOSE) Serial.println("Connected to plotly's servers"); if(VERBOSE) Serial.println("\n== Sending HTTP Post to plotly =="); // HTTP Meta println_("POST /clientresp HTTP/1.1", 0); println_("Host: 107.21.214.199", 0); println_("User-Agent: Arduino/2.0", 0); // compute an upper bound on the post body size upper_ = 273+strlen(layout)+((N_*M_-1)*2)+((N_-1)*4)+(max(20,maxStringLength)*N_*M_); /* Computation composition: 44 // First part of querystring below + 30 // Upper limit on username length + 5 // "&key=" + 10 // api key length + 6 // "&args=" + 22 // "&kwargs={\"filename\": \"" + 100 // upper bound on filename + 53 // "\", \"fileopt\": \"extend\", \"transpose\": true, \"layout\": " + layout.length() + 1 // closing } + (N_*M_-1)*2 // + 2-chars for comma and space for all but the last numbers ... + (N_-1)*4 // + 4-chars for square brackets, comma, space for n-1 set of points [], + 2 // + 2-chars for start 'n finish square braces + max(20,maxStringLength)*N_*M_; // + max character buffer in converting floats to strings. NOTE: The largest float is 56 chars... should the buffer be this big? [(48-chars for largest float integer left of the decimal (-3.4028235E+38) )+(1 decimal pt)+(6 digits of precision right of the decimal)=56-chars]*each float */ if(timestamp){ upper_ += 122; /* Computation composition: upper_ += 26 // \"convertTimestamp\": true" + 41 // ", \"convertTimestamp\": true, \"timezone\": \"" + 30 // upper bound on timezones string + 15 // "\", \"sentTime\": " + 10; // max length of unsigned long for sentTime: 4,294,967,295 */ } // send the header to plotly print_("Content-Length: ", 0); println_(upper_, 0); println_("", 0); // start the post string print_("version=0.2&origin=plot&platform=arduino&un=", 44); print_(username); print_("&key=", 5); print_(api_key); print_("&args=", 6); }
void plotly::close_stream(){ print_( "&kwargs={\"filename\": \"", 22 ); print_( filename ); print_( "\", \"fileopt\": \"extend\", \"transpose\": true, \"layout\": ", 53); print_( layout ); if(!world_readable){ print_( ", \"world_readable\": false", 25); } if(timestamp){ print_( ", \"convertTimestamp\": true", 26 ); print_( ", \"timezone\": \"", 15 ); print_(timezone); print_("\"", 1); print_( ", \"sentTime\": ", 14 ); String sT = String(millis()); print_( sT ); print_( "}", 1 ); } else{ print_( "}", 1); } // fill the remainder of the post with white space if(nChar_>=upper_){ Serial.print("Error: Content-Length upper bound is too small. Upper bound was: "); Serial.print(upper_); Serial.print(", and we printed "); Serial.print(nChar_); Serial.println(" characters."); Serial.println("Message will not transmit, report bug to chris at [email protected]."); } for(int i=nChar_; i<upper_; i++){ if(!DRY_RUN) client.print(" "); } // final newline to terminate the post println_("", 0); if(VERBOSE) Serial.println("== Sent message, waiting for plotly's response =="); if(!DRY_RUN && VERBOSE){ while(client.connected()){ if(client.available()){ char c = client.read(); Serial.print(c); } } #ifdef WIFI client.stop(); #endif #ifdef ETHERNET client.stop(); #endif #ifdef CC3000 client.close(); #endif #ifdef GSM client.close(); #endif } return; }
C_FLOAT64 CPraxis::praxis_(C_FLOAT64 *t0, C_FLOAT64 *machep, C_FLOAT64 *h0, C_INT *n, C_INT *prin, C_FLOAT64 *x, FPraxis *f, C_FLOAT64 *fmin) { /* System generated locals */ C_INT i__1, i__2, i__3; C_FLOAT64 ret_val, d__1; /* Local variables */ static C_FLOAT64 scbd; static C_INT idim; static bool illc; static C_INT klmk; static C_FLOAT64 d__[100], h__, ldfac; static C_INT i__, j, k; static C_FLOAT64 s, t, y[100], large, z__[100], small, value, f1; static C_INT k2; static C_FLOAT64 m2, m4, t2, df, dn; static C_INT kl, ii; static C_FLOAT64 sf; static C_INT kt; static C_FLOAT64 sl, vlarge; static C_FLOAT64 vsmall; static C_INT km1, im1; static C_FLOAT64 dni, lds; static C_INT ktm; C_FLOAT64 lastValue = std::numeric_limits<C_FLOAT64>::infinity(); /* LAST MODIFIED 3/1/73 */ /* PRAXIS RETURNS THE MINIMUM OF THE FUNCTION F(X,N) OF N VARIABLES */ /* USING THE PRINCIPAL AXIS METHOD. THE GRADIENT OF THE FUNCTION IS */ /* NOT REQUIRED. */ /* FOR A DESCRIPTION OF THE ALGORITHM, SEE CHAPTER SEVEN OF */ /* "ALGORITHMS FOR FINDING ZEROS AND EXTREMA OF FUNCTIONS WITHOUT */ /* CALCULATING DERIVATIVES" BY RICHARD P BRENT. */ /* THE PARAMETERS ARE: */ /* T0 IS A TOLERANCE. PRAXIS ATTEMPTS TO RETURN PRAXIS=F(X) */ /* SUCH THAT IF X0 IS THE TRUE LOCAL MINIMUM NEAR X, THEN */ /* NORM(X-X0) < T0 + SQUAREROOT(MACHEP)*NORM(X). */ /* MACHEP IS THE MACHINE PRECISION, THE SMALLEST NUMBER SUCH THAT */ /* 1 + MACHEP > 1. MACHEP SHOULD BE 16.**-13 (ABOUT */ /* 2.22D-16) FOR REAL*8 ARITHMETIC ON THE IBM 360. */ /* H0 IS THE MAXIMUM STEP SIZE. H0 SHOULD BE SET TO ABOUT THE */ /* MAXIMUM DISTANCE FROM THE INITIAL GUESS TO THE MINIMUM. */ /* (IF H0 IS SET TOO LARGE OR TOO SMALL, THE INITIAL RATE OF */ /* CONVERGENCE MAY BE SLOW.) */ /* N (AT LEAST TWO) IS THE NUMBER OF VARIABLES UPON WHICH */ /* THE FUNCTION DEPENDS. */ /* PRIN CONTROLS THE PRINTING OF INTERMEDIATE RESULTS. */ /* IF PRIN=0, NOTHING IS PRINTED. */ /* IF PRIN=1, F IS PRINTED AFTER EVERY N+1 OR N+2 LINEAR */ /* MINIMIZATIONS. FINAL X IS PRINTED, BUT INTERMEDIATE X IS */ /* PRINTED ONLY IF N IS AT MOST 4. */ /* IF PRIN=2, THE SCALE FACTORS AND THE PRINCIPAL VALUES OF */ /* THE APPROXIMATING QUADRATIC FORM ARE ALSO PRINTED. */ /* IF PRIN=3, X IS ALSO PRINTED AFTER EVERY FEW LINEAR */ /* MINIMIZATIONS. */ /* IF PRIN=4, THE PRINCIPAL VECTORS OF THE APPROXIMATING */ /* QUADRATIC FORM ARE ALSO PRINTED. */ /* X IS AN ARRAY CONTAINING ON ENTRY A GUESS OF THE POINT OF */ /* MINIMUM, ON RETURN THE ESTIMATED POINT OF MINIMUM. */ /* F(X,N) IS THE FUNCTION TO BE MINIMIZED. F SHOULD BE A REAL*8 */ /* FUNCTION DECLARED EXTERNAL IN THE CALLING PROGRAM. */ /* FMIN IS AN ESTIMATE OF THE MINIMUM, USED ONLY IN PRINTING */ /* INTERMEDIATE RESULTS. */ /* THE APPROXIMATING QUADRATIC FORM IS */ /* Q(X') = F(X,N) + (1/2) * (X'-X)-TRANSPOSE * A * (X'-X) */ /* WHERE X IS THE BEST ESTIMATE OF THE MINIMUM AND A IS */ /* INVERSE(V-TRANSPOSE) * D * INVERSE(V) */ /* (V(*,*) IS THE MATRIX OF SEARCH DIRECTIONS; D(*) IS THE ARRAY */ /* OF SECOND DIFFERENCES). IF F HAS CONTINUOUS SECOND DERIVATIVES */ /* NEAR X0, A WILL TEND TO THE HESSIAN OF F AT X0 AS X APPROACHES X0. */ /* IT IS ASSUMED THAT ON FLOATING-POINT UNDERFLOW THE RESULT IS SET */ /* TO ZERO. */ /* THE USER SHOULD OBSERVE THE COMMENT ON HEURISTIC NUMBERS AFTER */ /* THE INITIALIZATION OF MACHINE DEPENDENT NUMBERS. */ /* .....IF N>20 OR IF N<20 AND YOU NEED MORE SPACE, CHANGE '20' TO THE */ /* LARGEST VALUE OF N IN THE NEXT CARD, IN THE CARD 'IDIM=20', AND */ /* IN THE DIMENSION STATEMENTS IN SUBROUTINES MINFIT,MIN,FLIN,QUAD. */ /* .....INITIALIZATION..... */ /* MACHINE DEPENDENT NUMBERS: */ /* Parameter adjustments */ --x; /* Function Body */ small = *machep * *machep; vsmall = small * small; large = 1. / small; vlarge = 1. / vsmall; m2 = sqrt(*machep); m4 = sqrt(m2); /* HEURISTIC NUMBERS: */ /* IF THE AXES MAY BE BADLY SCALED (WHICH IS TO BE AVOIDED IF */ /* POSSIBLE), THEN SET SCBD=10. OTHERWISE SET SCBD=1. */ /* IF THE PROBLEM IS KNOWN TO BE ILL-CONDITIONED, SET ILLC=TRUE. */ /* OTHERWISE SET ILLC=FALSE. */ /* KTM IS THE NUMBER OF ITERATIONS WITHOUT IMPROVEMENT BEFORE THE */ /* ALGORITHM TERMINATES. KTM=4 IS VERY CAUTIOUS; USUALLY KTM=1 */ /* IS SATISFACTORY. */ scbd = 1.; illc = FALSE_; ktm = 1; ldfac = .01; if (illc) { ldfac = .1; } kt = 0; global_1.nl = 0; global_1.nf = 1; global_1.fx = (*f)(&x[1], n); q_1.qf1 = global_1.fx; t = small + fabs(*t0); t2 = t; global_1.dmin__ = small; h__ = *h0; if (h__ < t * 100) { h__ = t * 100; } global_1.ldt = h__; /* .....THE FIRST SET OF SEARCH DIRECTIONS V IS THE IDENTITY MATRIX..... */ i__1 = *n; for (i__ = 1; i__ <= i__1; ++i__) { i__2 = *n; for (j = 1; j <= i__2; ++j) { /* L10: */ q_1.v[i__ + j * 100 - 101] = 0.; } /* L20: */ q_1.v[i__ + i__ * 100 - 101] = 1.; } d__[0] = 0.; q_1.qd0 = 0.; i__1 = *n; for (i__ = 1; i__ <= i__1; ++i__) { q_1.q0[i__ - 1] = x[i__]; /* L30: */ q_1.q1[i__ - 1] = x[i__]; } if (*prin > 0) { print_(n, &x[1], prin, fmin); } /* .....THE MAIN LOOP STARTS HERE..... */ L40: sf = d__[0]; d__[0] = 0.; s = 0.; /* .....MINIMIZE ALONG THE FIRST DIRECTION V(*,1). */ /* FX MUST BE PASSED TO MIN BY VALUE. */ value = global_1.fx; min_(n, &c__1, &c__2, d__, &s, &value, &c_false, f, &x[1], &t, machep, &h__); if (s > 0.) { goto L50; } i__1 = *n; for (i__ = 1; i__ <= i__1; ++i__) { /* L45: */ q_1.v[i__ - 1] = -q_1.v[i__ - 1]; } L50: if (sf > d__[0] * .9 && sf * .9 < d__[0]) { goto L70; } i__1 = *n; for (i__ = 2; i__ <= i__1; ++i__) { /* L60: */ d__[i__ - 1] = 0.; } /* .....THE INNER LOOP STARTS HERE..... */ L70: i__1 = *n; for (k = 2; k <= i__1; ++k) { i__2 = *n; for (i__ = 1; i__ <= i__2; ++i__) { /* L75: */ y[i__ - 1] = x[i__]; } sf = global_1.fx; if (kt > 0) { illc = TRUE_; } L80: kl = k; df = 0.; /* .....A RANDOM STEP FOLLOWS (TO AVOID RESOLUTION VALLEYS). */ /* PRAXIS ASSUMES THAT RANDOM RETURNS A RANDOM NUMBER UNIFORMLY */ /* DISTRIBUTED IN (0,1). */ if (! illc) { goto L95; } i__2 = *n; for (i__ = 1; i__ <= i__2; ++i__) { s = (global_1.ldt * .1 + t2 * pow(10.0, (C_FLOAT64)kt)) * (mpRandom->getRandomCC() - 0.5); z__[i__ - 1] = s; i__3 = *n; for (j = 1; j <= i__3; ++j) { /* L85: */ x[j] += s * q_1.v[j + i__ * 100 - 101]; } /* L90: */ } global_1.fx = (*f)(&x[1], n); ++global_1.nf; /* .....MINIMIZE ALONG THE "NON-CONJUGATE" DIRECTIONS V(*,K),...,V(*,N ) */ L95: i__2 = *n; for (k2 = k; k2 <= i__2; ++k2) { sl = global_1.fx; s = 0.; value = global_1.fx; min_(n, &k2, &c__2, &d__[k2 - 1], &s, &value, &c_false, f, & x[1], &t, machep, &h__); if (illc) { goto L97; } s = sl - global_1.fx; goto L99; L97: /* Computing 2nd power */ d__1 = s + z__[k2 - 1]; s = d__[k2 - 1] * (d__1 * d__1); L99: if (df > s) { goto L105; } df = s; kl = k2; L105: ; } if (illc || df >= (d__1 = *machep * 100 * global_1.fx, fabs(d__1))) { goto L110; } /* .....IF THERE WAS NOT MUCH IMPROVEMENT ON THE FIRST TRY, SET */ /* ILLC=TRUE AND START THE INNER LOOP AGAIN..... */ illc = TRUE_; goto L80; L110: if (k == 2 && *prin > 1) { vcprnt_(&c__1, d__, n); } /* .....MINIMIZE ALONG THE "CONJUGATE" DIRECTIONS V(*,1),...,V(*,K-1) */ km1 = k - 1; i__2 = km1; for (k2 = 1; k2 <= i__2; ++k2) { s = 0.; value = global_1.fx; min_(n, &k2, &c__2, &d__[k2 - 1], &s, &value, &c_false, f, & x[1], &t, machep, &h__); /* L120: */ } f1 = global_1.fx; global_1.fx = sf; lds = 0.; i__2 = *n; for (i__ = 1; i__ <= i__2; ++i__) { sl = x[i__]; x[i__] = y[i__ - 1]; sl -= y[i__ - 1]; y[i__ - 1] = sl; /* L130: */ lds += sl * sl; } lds = sqrt(lds); if (lds <= small) { goto L160; } /* .....DISCARD DIRECTION V(*,KL). */ /* IF NO RANDOM STEP WAS TAKEN, V(*,KL) IS THE "NON-CONJUGATE" */ /* DIRECTION ALONG WHICH THE GREATEST IMPROVEMENT WAS MADE..... */ klmk = kl - k; if (klmk < 1) { goto L141; } i__2 = klmk; for (ii = 1; ii <= i__2; ++ii) { i__ = kl - ii; i__3 = *n; for (j = 1; j <= i__3; ++j) { /* L135: */ q_1.v[j + (i__ + 1) * 100 - 101] = q_1.v[j + i__ * 100 - 101]; } /* L140: */ d__[i__] = d__[i__ - 1]; } L141: d__[k - 1] = 0.; i__2 = *n; for (i__ = 1; i__ <= i__2; ++i__) { /* L145: */ q_1.v[i__ + k * 100 - 101] = y[i__ - 1] / lds; } /* .....MINIMIZE ALONG THE NEW "CONJUGATE" DIRECTION V(*,K), WHICH IS */ /* THE NORMALIZED VECTOR: (NEW X) - (0LD X)..... */ value = f1; min_(n, &k, &c__4, &d__[k - 1], &lds, &value, &c_true, f, &x[1], &t, machep, &h__); if (lds > 0.) { goto L160; } lds = -lds; i__2 = *n; for (i__ = 1; i__ <= i__2; ++i__) { /* L150: */ q_1.v[i__ + k * 100 - 101] = -q_1.v[i__ + k * 100 - 101]; } L160: global_1.ldt = ldfac * global_1.ldt; if (global_1.ldt < lds) { global_1.ldt = lds; } if (*prin > 0) { print_(n, &x[1], prin, fmin); } t2 = 0.; i__2 = *n; for (i__ = 1; i__ <= i__2; ++i__) { /* L165: */ /* Computing 2nd power */ d__1 = x[i__]; t2 += d__1 * d__1; } t2 = m2 * sqrt(t2) + t; /* .....SEE WHETHER THE LENGTH OF THE STEP TAKEN SINCE STARTING THE */ /* INNER LOOP EXCEEDS HALF THE TOLERANCE..... */ if (global_1.ldt > t2 * .5f) { kt = -1; } ++kt; if (kt > ktm) { goto L400; } /* L170: */ } /* added manually by Pedro Mendes 11/11/1998 */ // if(callback != 0/*NULL*/) callback(global_1.fx); /* .....THE INNER LOOP ENDS HERE. */ /* TRY QUADRATIC EXTRAPOLATION IN CASE WE ARE IN A CURVED VALLEY. */ /* L171: */ quad_(n, f, &x[1], &t, machep, &h__); dn = 0.; i__1 = *n; for (i__ = 1; i__ <= i__1; ++i__) { d__[i__ - 1] = 1. / sqrt(d__[i__ - 1]); if (dn < d__[i__ - 1]) { dn = d__[i__ - 1]; } /* L175: */ } if (*prin > 3) { maprnt_(&c__1, q_1.v, &idim, n); } i__1 = *n; for (j = 1; j <= i__1; ++j) { s = d__[j - 1] / dn; i__2 = *n; for (i__ = 1; i__ <= i__2; ++i__) { /* L180: */ q_1.v[i__ + j * 100 - 101] = s * q_1.v[i__ + j * 100 - 101]; } } /* .....SCALE THE AXES TO TRY TO REDUCE THE CONDITION NUMBER..... */ if (scbd <= 1.) { goto L200; } s = vlarge; i__2 = *n; for (i__ = 1; i__ <= i__2; ++i__) { sl = 0.; i__1 = *n; for (j = 1; j <= i__1; ++j) { /* L182: */ sl += q_1.v[i__ + j * 100 - 101] * q_1.v[i__ + j * 100 - 101]; } z__[i__ - 1] = sqrt(sl); if (z__[i__ - 1] < m4) { z__[i__ - 1] = m4; } if (s > z__[i__ - 1]) { s = z__[i__ - 1]; } /* L185: */ } i__2 = *n; for (i__ = 1; i__ <= i__2; ++i__) { sl = s / z__[i__ - 1]; z__[i__ - 1] = 1. / sl; if (z__[i__ - 1] <= scbd) { goto L189; } sl = 1. / scbd; z__[i__ - 1] = scbd; L189: i__1 = *n; for (j = 1; j <= i__1; ++j) { /* L190: */ q_1.v[i__ + j * 100 - 101] = sl * q_1.v[i__ + j * 100 - 101]; } /* L195: */ } /* .....CALCULATE A NEW SET OF ORTHOGONAL DIRECTIONS BEFORE REPEATING */ /* THE MAIN LOOP. */ /* FIRST TRANSPOSE V FOR MINFIT: */ L200: i__2 = *n; for (i__ = 2; i__ <= i__2; ++i__) { im1 = i__ - 1; i__1 = im1; for (j = 1; j <= i__1; ++j) { s = q_1.v[i__ + j * 100 - 101]; q_1.v[i__ + j * 100 - 101] = q_1.v[j + i__ * 100 - 101]; /* L210: */ q_1.v[j + i__ * 100 - 101] = s; } /* L220: */ } /* .....CALL MINFIT TO FIND THE SINGULAR VALUE DECOMPOSITION OF V. */ /* THIS GIVES THE PRINCIPAL VALUES AND PRINCIPAL DIRECTIONS OF THE */ /* APPROXIMATING QUADRATIC FORM WITHOUT SQUARING THE CONDITION */ /* NUMBER..... */ minfit_(&idim, n, machep, &vsmall, q_1.v, d__); /* .....UNSCALE THE AXES..... */ if (scbd <= 1.) { goto L250; } i__2 = *n; for (i__ = 1; i__ <= i__2; ++i__) { s = z__[i__ - 1]; i__1 = *n; for (j = 1; j <= i__1; ++j) { /* L225: */ q_1.v[i__ + j * 100 - 101] = s * q_1.v[i__ + j * 100 - 101]; } /* L230: */ } i__2 = *n; for (i__ = 1; i__ <= i__2; ++i__) { s = 0.; i__1 = *n; for (j = 1; j <= i__1; ++j) { /* L235: */ /* Computing 2nd power */ d__1 = q_1.v[j + i__ * 100 - 101]; s += d__1 * d__1; } s = sqrt(s); d__[i__ - 1] = s * d__[i__ - 1]; s = 1 / s; i__1 = *n; for (j = 1; j <= i__1; ++j) { /* L240: */ q_1.v[j + i__ * 100 - 101] = s * q_1.v[j + i__ * 100 - 101]; } /* L245: */ } L250: i__2 = *n; for (i__ = 1; i__ <= i__2; ++i__) { dni = dn * d__[i__ - 1]; if (dni > large) { goto L265; } if (dni < small) { goto L260; } d__[i__ - 1] = 1 / (dni * dni); goto L270; L260: d__[i__ - 1] = vlarge; goto L270; L265: d__[i__ - 1] = vsmall; L270: ; } /* .....SORT THE EIGENVALUES AND EIGENVECTORS..... */ sort_(&idim, n, d__, q_1.v); global_1.dmin__ = d__[*n - 1]; if (global_1.dmin__ < small) { global_1.dmin__ = small; } illc = FALSE_; if (m2 * d__[0] > global_1.dmin__) { illc = TRUE_; } if (*prin > 1 && scbd > 1.) { vcprnt_(&c__2, z__, n); } if (*prin > 1) { vcprnt_(&c__3, d__, n); } if (*prin > 3) { maprnt_(&c__2, q_1.v, &idim, n); } /* .....THE MAIN LOOP ENDS HERE..... */ /* added manually by Pedro Mendes 29/1/1998 */ /* if(callback != 0) callback(global_1.fx);*/ // We need a stopping condition for 1 dimensional problems. // We compare the values between the last and current steps. if (*n > 1 || global_1.fx < lastValue) { lastValue = global_1.fx; goto L40; } /* .....RETURN..... */ L400: if (*prin > 0) { vcprnt_(&c__4, &x[1], n); } ret_val = global_1.fx; return ret_val; } /* praxis_ */
plotly::plotly() : cc3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT,SPI_CLOCK_DIV2){ #endif #ifdef GSM plotly::plotly(){ #endif width_ = 10; prec_ = 5; VERBOSE = true; DRY_RUN = true; maxStringLength = 0; layout = "{}"; world_readable = false; timestamp = false; timezone = "America/Montreal"; } void plotly::open_stream(int N, int M, char *filename_, char *layout){ N_ = N; // total sets of data sent = number of rows in plotly-json data matrix M_ = (M*2); // total number of traces * 2 = number of columns in plotly-json data matrix ni_ = 0; // counter of number of sets of data set (number of rows in the plotly-json data matrix) transmitted mi_ = 0; // counter of points sent in each row of data nChar_ = 0; // counter of number of characters transmitted filename=filename_; delay(1000); if(DRY_RUN){ Serial.println("This is a dry run, we are not connecting to plotly's servers..."); } else{ if(VERBOSE) { Serial.println("Attempting to connect to plotly's servers..."); } #ifdef WIFI char server[] = "plot.ly"; while ( !client.connect(server, 80) ) { if(VERBOSE){ Serial.println("Couldn\'t connect to servers.... trying again!"); } delay(1000); } #endif #ifdef ETHERNET char server[] = "plot.ly"; while ( !client.connect(server, 80) ) { if(VERBOSE){ Serial.println("Couldn\'t connect to servers.... trying again!"); } delay(1000); } #endif #ifdef GSM char server[] = "plot.ly"; while ( !client.connect(server, 80) ) { if(VERBOSE){ Serial.println("Couldn\'t connect to servers.... trying again!"); } delay(1000); } #endif #ifdef CC3000 #define WEBSITE "plot.ly" uint32_t ip = 0; // Try looking up the website's IP address Serial.print(WEBSITE); Serial.print(F(" -> ")); while (ip == 0) { if (! cc3000.getHostByName(WEBSITE, &ip)) { Serial.println(F("Couldn't resolve!")); } delay(500); } client = cc3000.connectTCP(ip, 80); while ( !client.connected() ) { if(VERBOSE){ Serial.println("Couldn\'t connect to servers.... trying again!"); } delay(1000); } #endif } if(VERBOSE) Serial.println("Connected to plotly's servers"); if(VERBOSE) Serial.println("\n== Sending HTTP Post to plotly =="); // HTTP Meta println_("POST /clientresp HTTP/1.1", 0); println_("Host: 107.21.214.199", 0); println_("User-Agent: Arduino/2.0", 0); // compute an upper bound on the post body size upper_ = 273+strlen(layout)+((N_*M_-1)*2)+((N_-1)*4)+(max(20,maxStringLength)*N_*M_); /* Computation composition: 44 // First part of querystring below + 30 // Upper limit on username length + 5 // "&key=" + 10 // api key length + 6 // "&args=" + 22 // "&kwargs={\"filename\": \"" + 100 // upper bound on filename + 53 // "\", \"fileopt\": \"extend\", \"transpose\": true, \"layout\": " + layout.length() + 1 // closing } + (N_*M_-1)*2 // + 2-chars for comma and space for all but the last numbers ... + (N_-1)*4 // + 4-chars for square brackets, comma, space for n-1 set of points [], + 2 // + 2-chars for start 'n finish square braces + max(20,maxStringLength)*N_*M_; // + max character buffer in converting floats to strings. NOTE: The largest float is 56 chars... should the buffer be this big? [(48-chars for largest float integer left of the decimal (-3.4028235E+38) )+(1 decimal pt)+(6 digits of precision right of the decimal)=56-chars]*each float */ if(timestamp){ upper_ += 122; /* Computation composition: upper_ += 26 // \"convertTimestamp\": true" + 41 // ", \"convertTimestamp\": true, \"timezone\": \"" + 30 // upper bound on timezones string + 15 // "\", \"sentTime\": " + 10; // max length of unsigned long for sentTime: 4,294,967,295 */ } // send the header to plotly print_("Content-Length: ", 0); println_(upper_, 0); println_("", 0); // start the post string print_("version=0.2&origin=plot&platform=arduino&un=", 44); print_(username); print_("&key=", 5); print_(api_key); print_("&args=", 6); }
std::string FastRational::get_str() const { std::ostringstream os; print_(os); return os.str(); }
void plotly::sendString_(int d){ send_prepad_(); print_(d); send_postpad_(); }
void error(const std::string & msg) { print_(_error, msg); }
void print(Node *root, const std::string &msg) { std::cout << msg << ": "; print_(root); std::cout << "\n"; }
void plotly::sendString_(unsigned long d){ send_prepad_(); print_(String(d)); send_postpad_(); }
void plotly::sendString_(char *d){ send_prepad_(); print_("\"",1); print_(d); print_("\"",1); send_postpad_(); }