void primality_test(int n, int a) { int k; int q; int j; long exponent; k = calculate_k(n, 0); while (k >= 0) { q = n / pow(2, k); printf("k = %d\n", k); printf("q = %d\n", q); if (lround(pow(a, q)) % n == 1) { printf("%d may be prime\n", n); return; } else printf("a^q mod n = %ld\n", lround(pow(a, q)) % n); for (j = 1; j <= k; ++j) { exponent = pow(2, (j - 1)) * q; if (lround(pow(a, exponent)) % n == n - 1) { printf("%d may be prime\n", n); return; } else printf("j = %d, a^(2^(j-1)*q) mod n = %ld\n", j, lround(pow(a, exponent)) % n); } k = calculate_k(n, k); } printf("%d is not prime\n", n); }
void sev_valuecache_double::add( void *value, pwr_tTime *t) { double val = *(double *)value; double time; pwr_tDeltaTime dt; time_Adiff_NE( &dt, t, &m_start_time); time = time_DToFloat64( 0, &dt); // Store optimized write index before adding m_last_opt_write = get_optimal_write(); bool update_k = m_length < m_size; if ( !m_length) { m_val[0].val = val; m_val[0].time = time; m_length++; } else { if ( ++m_last >= m_size) m_last -= m_size; m_val[m_last].val = val; m_val[m_last].time = time; m_length++; if ( m_last == m_first) { m_first++; if ( m_first >= m_size) m_first -= m_size; m_length--; } } if ( !m_inited) { write( 0); m_inited = true; return; } if ( update_k) { calculate_k(); // Update epsilon for all data calculate_epsilon(); } else calculate_epsilon(0); }
void sev_valuecache_double::evaluate() { int value_added = 1; while( 1) { if ( !check_deadband()) { // Store optimal value write( m_last_opt_write + value_added); } else break; calculate_k(); calculate_epsilon(); m_last_opt_write = get_optimal_write(); value_added = 0; } }
void state_calibration_play(){ uint8_t c; char t[6]; uint32_t now; value_sensor value_sensor_d; switch(calibration_fsm_state) { case CALFSM_STATE_CHOICE_DELAY: DCALIB_PRINT("\r\nPlease enter the delay between readings (in ms) : \r\n"); calibration_fsm_state=CALFSM_STATE_READING_DELAY; break; case CALFSM_STATE_READING_DELAY: c=usb_serial_get_byte(); if (c>=48 && c<=57) { fprintf(&USBSerialStream,"%c",c); Value_T[fillpos++]=c; } else if (c=='\r') { Value_T[fillpos++]=c; delay_between_readings=char_to_int(Value_T,0,fillpos-1); fillpos=0; calibration_fsm_state=CALFSM_STATE_INITIALIZING; } break; case CALFSM_STATE_INITIALIZING: DCALIB_PRINT("\r\nPlease chose the calibration type (1 for PM, 2 for temperature, 3 for Humidity) \r\n"); calibration_fsm_state=CALFSM_STATE_CHOICE_CALIB; break; case CALFSM_STATE_CHOICE_CALIB: c=usb_serial_get_byte(); if (c>48 && c<=51) { type_of_calib=c-'0'; calibration_fsm_state=CALFSM_STATE_READING_SENSOR; } break; case CALFSM_STATE_READING_SENSOR: sensor_update(); switch(type_of_calib) { case 1: Sensor_t[number_of_reading-1] = (int32_t) (sensors_acc.pm_instant*100); break; case 2: Sensor_t[number_of_reading-1] = (uint32_t) (sensors_acc.tmp_instant*100.0); break; case 3: Sensor_t[number_of_reading-1] = (uint32_t) (sensors_acc.hum_instant*100.0); break; } if (number_of_reading==1 || number_of_reading ==2) { number_of_reading++; now=millis(); waiting_end=set_timeout(now+delay_between_readings); calibration_fsm_state=CALFSM_STATE_WAITING; } else { DCALIB_PRINTF("T_0 : %lu, T_5 : %lu, T_10 : %lu \r\n", Sensor_t[0], Sensor_t[1], Sensor_t[2]); DCALIB_PRINT("Enter the reference values (ref_0;ref_5;ref_10) : \r\n"); number_of_reading=1; calibration_fsm_state=CALFSM_STATE_READING_VALUE_T; } break; case CALFSM_STATE_WAITING: //DCALIB_PRINTF("\r\n %li", (int32_t) (millis()-waiting_end)); if (millis()>waiting_end) { calibration_fsm_state=CALFSM_STATE_READING_SENSOR; } break; case CALFSM_STATE_READING_VALUE_T: c=usb_serial_get_byte(); if (c>=48 && c<=57 || c==';') { fprintf(&USBSerialStream,"%c",c); Value_T[fillpos++]=c; } else if (c=='\r') { Value_T[fillpos++]=c; value_sensor_d=parse_ref_value(Value_T,fillpos); Sensor_Ref_0=char_to_int(Value_T,0,value_sensor_d.length_S_0); Sensor_Ref_5=char_to_int(Value_T,value_sensor_d.length_S_0+1,value_sensor_d.length_S_5); Sensor_Ref_10=char_to_int(Value_T,value_sensor_d.length_S_5+1,value_sensor_d.length_S_10); //DCALIB_PRINTF("T_0 : %lu, T_5 : %lu, T_10 %lu\r\n",Sensor_Ref_0, Sensor_Ref_5 ,Sensor_Ref_10); fillpos=0; calibration_fsm_state=CALFSM_STATE_CALCULATE_K; } break; case CALFSM_STATE_CALCULATE_K: coef=calculate_k(Sensor_Ref_0, Sensor_t[0], Sensor_Ref_10, Sensor_t[2]); coef1=calculate_k(Sensor_Ref_0, Sensor_t[0], Sensor_Ref_5, Sensor_t[1]); coef2=calculate_k(Sensor_Ref_5, Sensor_t[1], Sensor_Ref_10, Sensor_t[2]); fprintf(&USBSerialStream,"\n\r 0-10 a : %u , b : %li , Calibrated_t_5 : %lu ",coef.a, coef.b, (uint32_t) round(((double)(Sensor_t[1]*coef.a+coef.b)/100))); fprintf(&USBSerialStream,"\n\r 0-5 a : %u , b : %li , Calibrated_t_10 : %lu ",coef1.a, coef1.b, (uint32_t) round(((double)(Sensor_t[2]*coef1.a+coef1.b)/100))); fprintf(&USBSerialStream,"\n\r 5-10 a : %u , b : %li , Calibrated_t_0 : %lu \r\n",coef2.a, coef2.b, (uint32_t) round(((double)(Sensor_t[0]*coef2.a+coef2.b)/100)) ); DCALIB_PRINT("Press 1, 2, 3 to save the corresponding coefficient. Press 0 to skip saving.\r\n"); calibration_fsm_state=CALFSM_STATE_CHOICE_SAVE; break; case CALFSM_STATE_CHOICE_SAVE: c=usb_serial_get_byte(); switch (c) { case '0': calibration_fsm_state=CALFSM_STATE_END; break; case '1': sensor_calib_set_k(coef.a,coef.b,SIGNIFICATIVE_DIGITS_K,type_of_calib); calibration_fsm_state=CALFSM_STATE_SAVE; break; case '2': sensor_calib_set_k(coef1.a,coef1.b,SIGNIFICATIVE_DIGITS_K,type_of_calib); calibration_fsm_state=CALFSM_STATE_SAVE; break; case '3': sensor_calib_set_k(coef2.a,coef2.b,SIGNIFICATIVE_DIGITS_K,type_of_calib); calibration_fsm_state=CALFSM_STATE_SAVE; break; case '-': DCALIB_PRINT("Enter the reference values (ref_0;ref_5;ref_10) : \r\n"); calibration_fsm_state=CALFSM_STATE_READING_VALUE_T; break; default : calibration_fsm_state=CALFSM_STATE_CHOICE_SAVE; break; } break; case CALFSM_STATE_SAVE: sensor_calib_save_k(type_of_calib); DCALIB_PRINT("Coefficients saved\r\n"); calibration_fsm_state=CALFSM_STATE_END; break; case CALFSM_STATE_END: state_set_next(&main_fsm, &state_normal); break; default: state_set_next(&main_fsm, &state_normal); break; } }
void fsort_f(void *base, size_t nmemb, size_t size, double factor, compare_fun3 compare, void *argc, metric_fun2 metric, void *argm) { if (nmemb <= 1) { /* nothing to sort */ return; } /* preparation: form classes */ /* use calculate_k for a purpose it has not been made for, but since it is identical with what is needed here it is correct */ size_t lsize = calculate_k(nmemb, factor, nmemb) + 1; if (lsize < 2) { lsize = 2; } size_t *l = (size_t *) malloc(lsize * sizeof(size_t)); handle_ptr_error(l, "malloc for l", PROCESS_EXIT); for (size_t k = 0; k < lsize; k++) { l[k] = 0; } //size_t idx_min = 0; void *amin = POINTER(base, 0, size); size_t idx_max = 0; void *amax = POINTER(base, idx_max, size); for (size_t i = 0; i < nmemb; i++) { if (compare(POINTER(base, i, size), amin, argc) < 0) { // idx_min = i; amin = POINTER(base, i, size); } if (compare(POINTER(base, i, size), amax, argc) > 0) { idx_max = i; amax = POINTER(base, i, size); } } if (compare(amin, amax, argc) == 0) { /* min and max are the same --> already sorted */ free(l); return; } double amin_metric = metric(amin, argm); double amax_metric = metric(amax, argm); double step = (lsize - 1)/(amax_metric - amin_metric); /* size_t k_min = calculate_k(step, 0, lsize); */ /* size_t k_max = calculate_k(step, amax_metric - amin_metric, lsize); */ /* count the elements in each of the lsize classes */ for (size_t i = 0; i < nmemb; i++) { double ai_metric = metric(POINTER(base, i, size), argm); size_t k = calculate_k(step, ai_metric - amin_metric, lsize); l[k]++; } /* find the start positions for each of the classes */ size_t *ll = (size_t *) malloc((lsize + 1) * sizeof(size_t)); handle_ptr_error(ll, "malloc for ll", PROCESS_EXIT); ll[0] = 0; ll[1] = l[0]; for (size_t k = 1; k < lsize; k++) { l[k] += l[k-1]; ll[k+1] = l[k]; } swap_elements(POINTER(base, 0, size), POINTER(base, idx_max, size), size); // printf("prepared\n"); /* do the permutation */ size_t nmove = 0; size_t j = 0; size_t k = lsize - 1; while (nmove < nmemb - 1) { while (j >= l[k]) { j++; k = calculate_k(step, metric(POINTER(base, j, size), argm) - amin_metric, lsize); } /* now: j < l[k] */ void *flash_ptr = alloca(size); handle_ptr_error(flash_ptr, "alloca for flash_ptr", PROCESS_EXIT); /* flash_ptr takes element a[j] such that j > l[k] */ memcpy(flash_ptr, POINTER(base, j, size), size); while (j != l[k]) { k = calculate_k(step, metric(flash_ptr, argm) - amin_metric, lsize); void *alkm_ptr = POINTER(base, l[k]-1, size); swap_elements(flash_ptr, alkm_ptr, size); l[k]--; nmove++; } } /* use qsort or hsort for each class */ for (k = 0; k < lsize; k++) { size_t n = ll[k+1] - ll[k]; if (n > 1 && ll[k+1] < ll[k] || n > nmemb) { char txt[4096]; sprintf(txt, "wrong order: k=%ld lsize=%ld nmemb=%ld n=%ld ll[k]=%ld ll[k+1]=%ld\n", k, lsize, nmemb, n, ll[k], ll[k+1]); handle_error_myerrno(-1, EDOM, txt, PROCESS_EXIT); } if (n > 7) { void *basek = POINTER(base, ll[k], size); hsort_r(basek, n, size, compare, argc); } else if (n > 1) { void *basek = POINTER(base, ll[k], size); isort_r(basek, n, size, compare, argc); } } free(l); l = NULL; free(ll); ll = NULL; return; }