void calculate_CH() { // Using algorithm from Baulik and Bandyopadhyay. See there for // notation. // First calculate centroid of entire dataset. ProbabilitySequence* total_centroid = calc_median(fasta->array); //display_prob(total_centroid, 10); // Calculate TrB - trace of between cluster scatter matrix. // k is index over clusters. double TrB = 0.0; for (guint k = 0; k < clusters->len; k++){ GArray* cluster = g_array_index(clusters, GArray*, k); // Calculate centroid of cluster. ProbabilitySequence* cluster_centroid = calc_median(cluster); //display_prob(cluster_centroid, 10); double dist = dist_pseq_pseq(cluster_centroid, total_centroid); TrB += dist * dist * ((double) cluster->len); } g_print("TrB = %f\n", TrB); // Calculate TrW - trace of within cluster scatter matrix. double TrW = 0.0; for (guint k = 0; k < clusters->len; k++){ GArray* cluster = g_array_index(clusters, GArray*, k); // Calculate centroid of cluster. ProbabilitySequence* cluster_centroid = calc_median(cluster); for (guint i = 0; i < cluster->len; i++){ Sequence* seq = g_array_index(cluster, Sequence*, i); double dist = dist_seq_pseq(seq, cluster_centroid); TrW += dist*dist; } } g_print("TrW = %f\n", TrW); // K is number of clusters double K = (double) clusters->len; // n is total num of elements double n = (double) fasta->array->len; double CH = TrB * (n - K) / (K - 1) / TrW; g_print("K = %f\n", K); g_print("n = %f\n", n); g_print("CH = %f\n", CH); }
int main () { int num_grades; int* grades = get_grades (&num_grades); float ave = calc_ave (num_grades, grades); display_ave (num_grades, ave); float median = calc_median(num_grades, grades); display_median(num_grades, median); float std_dev = calc_std_dev(num_grades, grades); display_std_dev(num_grades, std_dev); return 0; }
//-------------------------------------------------------------- void testApp::draw(){ int n_value = ofMap(analog_value, 0, 1023, 0, 255); ard.sendPwm(9, n_value);//0~255!!(analogWrite) ofSetColor(0); ofLine(ofGetWidth()/4, ofGetHeight()*2/3, ofGetWidth()*3/4, ofGetHeight()*2/3); ofLine(ofGetWidth()/4, ofGetHeight()*2/3, ofGetWidth()/4, ofGetHeight()/3); int x, y; for (int i = 0; i < LENGTH; i++) { cout << buffer[i] << endl; x = ofGetWidth()/4 + (i * 10); y = ofGetHeight()*2/3 - buffer[i]; data[i].x = x; data[i].y = y; } calc_avarage(data); calc_median(data); for (int i = 1; i < LENGTH; i++) { ofSetColor(0, 0, 255); ofLine(data[i-1].x, data[i-1].y, data[i].x, data[i].y); ofSetColor(255, 0, 0); ofLine(data[i-1].x, avarage[i-1], data[i].x, avarage[i]); ofSetColor(0, 255, 0); ofLine(data[i-1].x, median[i-1], data[i].x, median[i]); cout << median[i] << endl; } }
void calc_median_global(void *data_local, void *result, size_t n_data_local, SID_Datatype type, int mode, SID_Comm *comm) { int i_mid; double median; size_t *index; double value_1, value_2; size_t n_data; size_t index_1, index_2; size_t *idx; size_t i_lo, i_hi; size_t index_lo, index_hi, index_mid; size_t i_1, i_2; int flag_1, flag_2; int rank_1_local, rank_1, rank_2_local, rank_2; #if USE_MPI SID_exit_error("calc_median_global has not been implemented yet!", SID_ERROR_LOGIC); /* MPI_Allreduce(&n_data_local,&n_data,1,MPI_SIZE_T,MPI_SUM,MPI_COMM_WORLD); if(n_data>0){ if(n_data>1){ sort(data_local, n_data_local, &index, type, GBP_FALSE, GBP_TRUE, GBP_FALSE); index_1=n_data/2-1; index_2=n_data/2; sort(index, n_data_local, &idx, SID_SIZE_T, GBP_TRUE, GBP_TRUE, GBP_FALSE); i_lo =0; i_hi =n_data_local-1; index_lo=index[idx[i_lo]]; index_hi=index[idx[i_hi]]; do{ i_mid =(i_lo+i_hi)/2; index_mid=index[idx[i_mid]]; if(index_1<index_mid) i_hi=i_mid; else if(index_1>index_mid) i_lo=i_mid; else{ i_lo=i_mid; i_hi=i_mid; } } while(i_hi-i_lo>2); if(index_1==index[i_lo]){ i_1 =i_lo; flag_1=GBP_TRUE; } else flag_1=GBP_FALSE; if(index_2==index[i_lo]){ i_2 =i_lo; flag_2=GBP_TRUE; } else if(index_2==index[i_hi]){ i_2 =i_hi; flag_2=GBP_TRUE; } else flag_2=GBP_FALSE; if(!(n_data%2)){ if(flag_1){ rank_1_local=SID.My_rank; if(type==SID_DOUBLE) value_1=((double *)data_local)[i_1]; else if(type==SID_FLOAT) value_1=((float *)data_local)[i_1]; else if(type==SID_INT) value_1=((int *)data_local)[i_1]; else if(type==SID_SIZE_T) value_1=((size_t *)data_local)[i_1]; else SID_trap_error("type not supported by calc_median_global.",SID_ERROR_LOGIC); } } else rank_1_local=-1; MPI_Allreduce(&rank_1_local,&rank_1,1,MPI_INT,MPI_MAX,MPI_COMM_WORLD); MPI_Bcast(&value_1,1,MPI_DOUBLE,rank_1,MPI_COMM_WORLD); } if(flag_2){ rank_2_local=SID.My_rank; if(type==SID_DOUBLE) value_2=((double *)data_local)[i_2]; else if(type==SID_FLOAT) value_2=((float *)data_local)[i_2]; else if(type==SID_INT) value_2=((int *)data_local)[i_2]; else if(type==SID_SIZE_T) value_2=((size_t *)data_local)[i_2]; else SID_trap_error("type not supported by calc_median_global.",SID_ERROR_LOGIC); } else rank_2_local=-1; MPI_Allreduce(&rank_2_local,&rank_2,1,MPI_INT,MPI_MAX,MPI_COMM_WORLD); MPI_Bcast(&value_2,1,MPI_DOUBLE,rank_2,MPI_COMM_WORLD); free(idx); free(index); if(n_data%2) median=value_2; else median=ONE_HALF*(value_1+value_2); } else if(type==SID_DOUBLE) median=((double *)data_local)[0]; else if(type==SID_FLOAT) median=((float *)data_local)[0]; else if(type==SID_INT) median=((int *)data_local)[0]; else if(type==SID_SIZE_T) median=((size_t *)data_local)[0]; else SID_exit_error("type not supported by calc_median_global.",SID_ERROR_LOGIC); } else median=0.; */ #else calc_median(data_local, result, n_data_local, type, mode); #endif }
static void client (int fd, const char* prio, unsigned int text_size, struct test_st *test) { int ret; char buffer[MAX_BUF + 1]; char text[text_size]; gnutls_certificate_credentials_t x509_cred; gnutls_session_t session; struct timespec start, stop; static unsigned long taken = 0; static unsigned long measurement; const char* err; global_init (); setpriority(PRIO_PROCESS, getpid(), -15); memset(text, 0, text_size); #ifdef DEBUG gnutls_global_set_log_function (client_log_func); gnutls_global_set_log_level (6); #endif gnutls_certificate_allocate_credentials (&x509_cred); #ifdef REHANDSHAKE restart: #endif /* Initialize TLS session */ gnutls_init (&session, GNUTLS_CLIENT); gnutls_session_set_ptr(session, test); cli_session = session; /* Use default priorities */ if ((ret=gnutls_priority_set_direct (session, prio, &err)) < 0) { fprintf(stderr, "Error in priority string %s: %s\n", gnutls_strerror(ret), err); exit(1); } /* put the anonymous credentials to the current session */ gnutls_credentials_set (session, GNUTLS_CRD_CERTIFICATE, x509_cred); gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) fd); /* Perform the TLS handshake */ do { ret = gnutls_handshake (session); } while (ret < 0 && gnutls_error_is_fatal(ret) == 0); if (ret < 0) { fprintf (stderr, "client: Handshake failed\n"); gnutls_perror (ret); exit(1); } ret = gnutls_protocol_get_version(session); if (ret < GNUTLS_TLS1_1) { fprintf (stderr, "client: Handshake didn't negotiate TLS 1.1 (or later)\n"); exit(1); } gnutls_transport_set_push_function (session, push_crippled); #ifndef REHANDSHAKE restart: #endif do { ret = gnutls_record_send (session, text, sizeof(text)); } while (ret == GNUTLS_E_AGAIN || ret == GNUTLS_E_INTERRUPTED); /* measure peer's processing time */ clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start); #define TLS_RECV #ifdef TLS_RECV do { ret = gnutls_record_recv(session, buffer, sizeof(buffer)); } while(ret < 0 && (ret == GNUTLS_E_AGAIN || ret == GNUTLS_E_INTERRUPTED)); #else do { ret = recv(fd, buffer, sizeof(buffer), 0); } while(ret == -1 && errno == EAGAIN); #endif if (taken < MAX_MEASUREMENTS(test->npoints) && ret > 0) { clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &stop); taken++; measurement = timespec_sub_ns(&stop, &start); prev_point_ptr->measurements[prev_point_ptr->midx] = measurement; /*fprintf(stderr, "(%u,%u): %lu\n", (unsigned) prev_point_ptr->byte1, (unsigned) prev_point_ptr->byte2, measurements[taken]);*/ memcpy(&measurement, buffer, sizeof(measurement)); prev_point_ptr->smeasurements[prev_point_ptr->midx] = measurement; prev_point_ptr->midx++; /* read server's measurement */ #ifdef REHANDSHAKE gnutls_deinit(session); #endif goto restart; } #ifndef TLS_RECV else if (ret < 0) { fprintf(stderr, "Error in recv()\n"); exit(1); } #endif gnutls_transport_set_push_function (session, push); gnutls_bye (session, GNUTLS_SHUT_WR); { double avg2, med, savg, smed; unsigned i; FILE* fp = NULL; if (test->file) fp = fopen(test->file, "w"); if (fp) /* point, avg, median */ fprintf(fp, "Delta,TimeAvg,TimeMedian,ServerAvg,ServerMedian\n"); for (i=0;i<test->npoints;i++) { qsort( test->points[i].measurements, test->points[i].midx, sizeof(test->points[i].measurements[0]), compar); qsort( test->points[i].smeasurements, test->points[i].midx, sizeof(test->points[i].smeasurements[0]), compar); avg2 = calc_avg( test->points[i].measurements, test->points[i].midx); /*var = calc_var( test->points[i].measurements, test->points[i].midx, avg2);*/ med = calc_median( test->points[i].measurements, test->points[i].midx); savg = calc_avg( test->points[i].smeasurements, test->points[i].midx); /*var = calc_var( test->points[i].measurements, test->points[i].midx, avg2);*/ smed = calc_median( test->points[i].smeasurements, test->points[i].midx); /*min = calc_min( test->points[i].measurements, test->points[i].midx);*/ if (fp) /* point, avg, median */ fprintf(fp, "%u,%.2lf,%.2lf,%.2lf,%.2lf\n", (unsigned)test->points[i].byte1, avg2,med,savg, smed); /*printf("(%u) Avg: %.3f nanosec, Median: %.3f, Variance: %.3f\n", (unsigned)test->points[i].byte1, avg2, med, var);*/ } if (fp) fclose(fp); } if (test->desc) fprintf(stderr, "Description: %s\n", test->desc); close (fd); gnutls_deinit (session); gnutls_certificate_free_credentials (x509_cred); gnutls_global_deinit (); }