下面列出了java.lang.Math#pow ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public Adap (MiDataset training, BaseR base, double valor_tau, int tipo) {
int i;
tabla = training;
base_reglas = base;
tau = valor_tau;
tipo_fitness = tipo;
maxEC = 0.0;
for (i=0; i<tabla.long_tabla; i++)
maxEC += Math.pow (tabla.datos[i].ejemplo[tabla.n_var_estado], 2.0);
maxEC /= 2.0;
grado_pertenencia = new double[tabla.n_variables];
}
private double DistanceEuclidea(Chromosome chromo_a,Chromosome chromo_b)
{
double DistEuclidea,lb_a,ub_a,lb_b,ub_b;
double suma_lb_ub = 0,suma = 0, resta_lb,resta_ub;
for(int i=0; i< chromo_a.getGenes().size();i++){
lb_a = chromo_a.getGenes().get(i).getL();
ub_a = chromo_a.getGenes().get(i).getU();
lb_b = chromo_b.getGenes().get(i).getL();
ub_b = chromo_b.getGenes().get(i).getU();
resta_lb = lb_a - lb_b;
resta_ub = ub_a - ub_b;
suma_lb_ub = resta_lb + resta_ub;
suma = suma + Math.pow(suma_lb_ub,2);//aplicando la sumatoria del cuadrado
}
DistEuclidea = Math.sqrt(suma);
return DistEuclidea;
}
static int[] decompusitionInSumOfTwo(int fibonacciTerm){
int[] result = new int[8];
int k=7;
for(int i=7; i>=0; i--){
if(fibonacciTerm - Math.pow(2,i)>=0){
result[k] = 1;
fibonacciTerm = (int) (fibonacciTerm - Math.pow(2, i));
}
k--;
}
return result;
}
/** Mean Square Error(MSE) and Mean Linear Error(MLE) by test */
public void Error_tst () {
int i, j;
double suma1, suma2, fuerza;
for (j=0,suma1=suma2=0.0; j<tabla_tst.long_tabla; j++) {
fuerza=base_reglas.FLC_TSK (tabla_tst.datos[j].ejemplo);
suma1 += Math.pow (tabla_tst.datos[j].ejemplo[tabla.n_var_estado]-fuerza,2.0);
suma2 += Math.abs (tabla_tst.datos[j].ejemplo[tabla.n_var_estado]-fuerza);
}
EC = suma1 / (double)tabla_tst.long_tabla;
EL = suma2 / (double)tabla_tst.long_tabla;
}
void Error_tst (MiDataset tabla_tst) {
int i, j;
double suma1, suma2, fuerza;
for (j=0,suma1=suma2=0.0; j<tabla_tst.long_tabla; j++) {
fuerza=base_reglas.FLC (tabla_tst.datos[j].ejemplo);
suma1 += 0.5 * Math.pow (tabla_tst.datos[j].ejemplo[tabla.n_var_estado]-fuerza,2.0);
suma2 += Math.abs (tabla_tst.datos[j].ejemplo[tabla.n_var_estado]-fuerza);
}
EC = suma1 / (double)tabla_tst.long_tabla;
EL = suma2 / (double)tabla_tst.long_tabla;
}
double ErrorCuadratico() {
int i;
double suma;
for (i = 0, suma = 0.0; i < tabla.long_tabla; i++) {
suma +=
Math.pow(tabla.datos[i].ejemplo[tabla.n_var_estado] -
base_reglas.FLC(tabla.datos[i].ejemplo), 2.0);
}
return (suma / (double) tabla.long_tabla);
}
private double delta (long t, double y, long n_generaciones) {
double r, potencia, subtotal, sub;
r = Randomize.Rand();
sub = 1.0 - (double)t / (double)n_generaciones;
potencia = Math.pow (sub, (double)b);
subtotal = Math.pow (r, potencia);
return (y * (1.0 - subtotal));
}
private double delta(long t, double y, long n_generaciones) {
double r, potencia, subtotal, sub;
r = Randomize.Rand();
sub = 1.0 - (double) t / (double) n_generaciones;
potencia = Math.pow(sub, (double) b);
subtotal = Math.pow(r, potencia);
return (y * (1.0 - subtotal));
}
private double delta(long t, double y, long n_generaciones) {
double r, potencia, subtotal, sub;
r = Randomize.Rand();
sub = 1.0 - (double) t / (double) n_generaciones;
potencia = Math.pow(sub, (double) b);
subtotal = Math.pow(r, potencia);
return (y * (1.0 - subtotal));
}
void Error_tra () {
int i,j;
double suma1, suma2, fuerza;
for (j=0, suma1=suma2=0.0; j<tabla.long_tabla; j++) {
fuerza=base_reglas.FLC_TSK (tabla.datos[j].ejemplo);
suma1 += 0.5 * Math.pow (tabla.datos[j].ejemplo[tabla.n_var_estado]-fuerza,2.0);
suma2 += Math.abs (tabla.datos[j].ejemplo[tabla.n_var_estado]-fuerza);
}
EC = suma1 / (double)tabla.long_tabla;
EL = suma2 / (double)tabla.long_tabla;
}
void Error_tst() {
int i, j;
double suma1, suma2, fuerza;
for (j = 0, suma1 = suma2 = 0.0; j < tabla_tst.long_tabla; j++) {
fuerza = base_reglas.FLC_TSK(tabla_tst.datos[j].ejemplo);
suma1 +=
Math.pow(tabla_tst.datos[j].ejemplo[tabla.n_var_estado] - fuerza, 2.0);
suma2 += Math.abs(tabla_tst.datos[j].ejemplo[tabla.n_var_estado] - fuerza);
}
EC = suma1 / (double) tabla_tst.long_tabla;
EL = suma2 / (double) tabla_tst.long_tabla;
}
void Error_tra() {
int i, j;
double suma1, suma2, fuerza;
for (j = 0, suma1 = suma2 = 0.0; j < tabla.long_tabla; j++) {
fuerza = base_reglas.FLC_TSK(tabla.datos[j].ejemplo);
suma1 +=
Math.pow(tabla.datos[j].ejemplo[tabla.n_var_estado] - fuerza, 2.0);
suma2 += Math.abs(tabla.datos[j].ejemplo[tabla.n_var_estado] - fuerza);
}
EC = suma1 / (double) tabla.long_tabla;
EL = suma2 / (double) tabla.long_tabla;
}
private double CalcularBADD(int clase, double[] ejemplo) {
double[] A = new double[base_reglas.n_reglas];
double comp, resu, amax, suma1, suma2;
int r, contador, j, posicion, repetido;
int n_inputs = ejemplo.length - 1;
FuzzySet[] D = new FuzzySet[n_inputs];
for (int i = 0; i < n_inputs; i++) {
D[i] = new FuzzySet();
}
r = 0;
contador = 0;
suma1 = 0.0;
suma2 = 0.0;
/* Look for the set of value to be added for the class "clase" */
while (r < base_reglas.n_reglas) {
for (int i = 0; i < tabla.n_inputs; i++) {
D[i] = base_reglas.BaseReglas[r].Ant[i];
}
switch (tipo_reglas) {
case 1:
if (base_reglas.BaseReglas[r].Cons[0].clase == clase) {
comp = MatchingDegree(ejemplo, D, r);
if (comp > 0.0) {
A[contador] = comp;
contador++;
suma1 += Math.pow(comp, frm.p + 1);
suma2 += Math.pow(comp, frm.p);
}
}
break;
case 2:
if (base_reglas.BaseReglas[r].Cons[0].clase == clase) {
comp = MatchingDegree(ejemplo, D, r);
comp = comp * base_reglas.BaseReglas[r].Cons[0].gcerteza;
if (comp > 0.0) {
A[contador] = comp;
contador++;
suma1 += Math.pow(comp, frm.p + 1);
suma2 += Math.pow(comp, frm.p);
}
}
break;
case 3:
if (base_reglas.BaseReglas[r].Cons[clase].gcerteza > 0.0) {
comp = MatchingDegree(ejemplo, D, r);
comp = comp *
base_reglas.BaseReglas[r].Cons[clase].gcerteza;
if (comp > 0.0) {
A[contador] = comp;
contador++;
suma1 += Math.pow(comp, frm.p + 1);
suma2 += Math.pow(comp, frm.p);
}
}
}
r++;
}
if (contador == 0) {
return (0);
} else {
resu = suma1 / suma2;
return (resu);
}
}
/**
* {@inheritDoc}
*/
@Override
public void executeWithUnitOutForAnimation(JLabel unitLabel) {
final GUI gui = getGUI();
final float scale = gui.getAnimationScale();
final int movementRatio = (int)(Math.pow(2, this.speed + 1) * scale);
final Rectangle r1 = gui.getAnimationTileBounds(this.sourceTile);
final Rectangle r2 = gui.getAnimationTileBounds(this.destinationTile);
final Rectangle bounds = r1.union(r2);
final double xratio = ImageLibrary.TILE_SIZE.width
/ (double)ImageLibrary.TILE_SIZE.height;
// Tile positions should be valid at this point.
final Point srcP = gui.getAnimationTilePosition(this.sourceTile);
if (srcP == null) {
logger.warning("Failed move animation for " + this.unit
+ " at source tile: " + this.sourceTile);
return;
}
final Point dstP = gui.getAnimationTilePosition(this.destinationTile);
if (dstP == null) {
logger.warning("Failed move animation for " + this.unit
+ " at destination tile: " + this.destinationTile);
return;
}
final int labelWidth = unitLabel.getWidth();
final int labelHeight = unitLabel.getHeight();
final Point srcPoint = gui.getAnimationPosition(labelWidth, labelHeight, srcP);
final Point dstPoint = gui.getAnimationPosition(labelWidth, labelHeight, dstP);
final int stepX = (int)Math.signum(dstPoint.getX() - srcPoint.getX());
final int stepY = (int)Math.signum(dstPoint.getY() - srcPoint.getY());
Point point = srcPoint;
long time = now(), dropFrames = 0;
while (!point.equals(dstPoint)) {
point.x += stepX * xratio * movementRatio;
point.y += stepY * movementRatio;
if ((stepX < 0 && point.x < dstPoint.x)
|| (stepX > 0 && point.x > dstPoint.x)) {
point.x = dstPoint.x;
}
if ((stepY < 0 && point.y < dstPoint.y)
|| (stepY > 0 && point.y > dstPoint.y)) {
point.y = dstPoint.y;
}
if (dropFrames <= 0) {
unitLabel.setLocation(point);
gui.paintImmediately(bounds);
long newTime = now();
long timeTaken = newTime - time;
time = newTime;
final long waitTime = ANIMATION_DELAY - timeTaken;
if (waitTime > 0) {
delay(waitTime, "Animation interrupted.");
dropFrames = 0;
} else {
dropFrames = timeTaken / ANIMATION_DELAY - 1;
}
} else {
dropFrames--;
}
}
gui.refresh();
}
private double CalcularQUASIARIT(int clase, double[] ejemplo) {
double[] A = new double[base_reglas.n_reglas];
double comp, resu;
int r, contador, j, posicion, repetido, i;
int n_inputs = ejemplo.length - 1;
FuzzySet[] D = new FuzzySet[n_inputs];
for (i = 0; i < n_inputs; i++) {
D[i] = new FuzzySet();
}
r = 0;
contador = 0;
/* Look for the set of value to be added for the class "clase" */
while (r < base_reglas.n_reglas) {
for (i = 0; i < tabla.n_inputs; i++) {
D[i] = base_reglas.BaseReglas[r].Ant[i];
}
switch (tipo_reglas) {
case 1:
if (base_reglas.BaseReglas[r].Cons[0].clase == clase) {
comp = MatchingDegree(ejemplo, D, r);
if (comp > 0.0) {
A[contador] = comp;
contador++;
}
}
break;
case 2:
if (base_reglas.BaseReglas[r].Cons[0].clase == clase) {
comp = MatchingDegree(ejemplo, D, r);
comp = comp * base_reglas.BaseReglas[r].Cons[0].gcerteza;
if (comp > 0.0) {
A[contador] = comp;
contador++;
}
}
break;
case 3:
if (base_reglas.BaseReglas[r].Cons[clase].gcerteza > 0.0) {
comp = MatchingDegree(ejemplo, D, r);
comp = comp *
base_reglas.BaseReglas[r].Cons[clase].gcerteza;
if (comp > 0.0) {
A[contador] = comp;
contador++;
}
}
}
r++;
}
if (contador == 0) {
return (0);
}
/* Obtaining the weights */
double[] w = new double[contador];
for (i = 0; i < contador; i++) {
w[i] = 1 / ((double) contador);
}
/* Calculation of the final value for quasiarithmetic mean */
for (i = 0; i < contador; i++) {
A[i] = Math.pow(A[i], frm.p);
}
resu = SumaPonderada(A, w, contador);
resu = Math.pow(resu, 1 / frm.p);
return (resu);
}
public static void main(String[] args) {
String line;
Vector nums = new Vector();
double num, sum = 0.0;
double mean = 0.0;
double average_deviation = 0.0;
double standard_deviation = 0.0;
double variance = 0.0;
double skew = 0.0;
double kurtosis = 0.0;
double median = 0.0;
double deviation = 0.0;
int i, n, mid = 0;
try {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
while ((line = in.readLine()) != null) {
num = Double.parseDouble(line);
sum += num;
nums.add(new Double(num));
}
} catch (IOException e) {
System.err.println(e);
return;
}
n = nums.size();
mean = sum/n;
for (i=0; i<n; i++) {
deviation = ((Double)nums.get(i)).doubleValue() - mean;
average_deviation += Math.abs(deviation);
variance += Math.pow(deviation,2);
skew += Math.pow(deviation,3);
kurtosis += Math.pow(deviation,4);
}
average_deviation /= n;
variance /= (n - 1);
standard_deviation = Math.sqrt(variance);
if (variance != 0.0) {
skew /= (n * variance * standard_deviation);
kurtosis = kurtosis/(n * variance * variance) - 3.0;
}
Collections.sort(nums);
mid = (n/2);
median = (n % 2 != 0) ?
((Double)nums.get(mid)).doubleValue() :
(((Double)nums.get(mid)).doubleValue() +
((Double)nums.get(mid-1)).doubleValue())/2;
NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(13);
nf.setGroupingUsed(false);
nf.setMaximumFractionDigits(6);
nf.setMinimumFractionDigits(6);
System.out.println("n: " + n);
System.out.println("median: " + nf.format(median));
System.out.println("mean: " + nf.format(mean));
System.out.println("average_deviation: " + nf.format(average_deviation));
System.out.println("standard_deviation: " + nf.format(standard_deviation));
System.out.println("variance: " + nf.format(variance));
System.out.println("skew: " + nf.format(skew));
System.out.println("kurtosis: " + nf.format(kurtosis));
}
private double CompatibilidadMedia(double[] ejemplo, FuzzySet[] D, int regla) {
double media, aux;
double grado_pertenencia = 0.0;
int i, etiqueta;
String cadena, sub;
media = 0.0;
for (i = 0; i < tabla.n_inputs; i++) {
aux = base_reglas.Fuzzification(ejemplo[i], D[i]);
switch (tipo_modificadores) {
case 0:
grado_pertenencia = aux;
break;
case 1:
cadena = D[i].Etiqueta;
sub = cadena.substring(1, cadena.length() - 1);
etiqueta = Integer.parseInt(sub);
switch (base_reglas.modificador[(n_etiquetas * i) + etiqueta]) {
case '0':
grado_pertenencia = aux;
break;
case '1':
grado_pertenencia = Math.pow(aux, 0.5);
break;
case '2':
grado_pertenencia = Math.pow(aux, 2);
}
break;
case 2:
switch (base_reglas.modificador[(tabla.n_inputs * regla) + i]) {
case '0':
grado_pertenencia = aux;
break;
case '1':
grado_pertenencia = Math.pow(aux, 0.5);
break;
case '2':
grado_pertenencia = Math.pow(aux, 2);
}
}
media += grado_pertenencia;
}
media = media / tabla.n_inputs;
return (media);
}
/**
* Rounds a double and converts it into String.
*
* @param value the double value
* @param afterDecimalPoint the (maximum) number of digits permitted
* after the decimal point
* @return the double as a formatted string
*/
public static /*@[email protected]*/ String doubleToString(double value, int afterDecimalPoint) {
StringBuffer stringBuffer;
double temp;
int dotPosition;
long precisionValue;
temp = value * Math.pow(10.0, afterDecimalPoint);
if (Math.abs(temp) < Long.MAX_VALUE) {
precisionValue = (temp > 0) ? (long)(temp + 0.5)
: -(long)(Math.abs(temp) + 0.5);
if (precisionValue == 0) {
stringBuffer = new StringBuffer(String.valueOf(0));
} else {
stringBuffer = new StringBuffer(String.valueOf(precisionValue));
}
if (afterDecimalPoint == 0) {
return stringBuffer.toString();
}
dotPosition = stringBuffer.length() - afterDecimalPoint;
while (((precisionValue < 0) && (dotPosition < 1)) ||
(dotPosition < 0)) {
if (precisionValue < 0) {
stringBuffer.insert(1, '0');
} else {
stringBuffer.insert(0, '0');
}
dotPosition++;
}
stringBuffer.insert(dotPosition, '.');
if ((precisionValue < 0) && (stringBuffer.charAt(1) == '.')) {
stringBuffer.insert(1, '0');
} else if (stringBuffer.charAt(0) == '.') {
stringBuffer.insert(0, '0');
}
int currentPos = stringBuffer.length() - 1;
while ((currentPos > dotPosition) &&
(stringBuffer.charAt(currentPos) == '0')) {
stringBuffer.setCharAt(currentPos--, ' ');
}
if (stringBuffer.charAt(currentPos) == '.') {
stringBuffer.setCharAt(currentPos, ' ');
}
return stringBuffer.toString().trim();
}
return new String("" + value);
}
/**
* Given a String s and an original length, will fill up the remaining
* space in a block with the original length of the string converted
* to standard ASCII numerals modulo the size of the remaining space.
* For example, given an original string 1234567890A (length = 11),
* and a block size of eight, the blocks will start as
* ["12345678", "90A"]. We want to pad the final block "09A" so that
* it is eight chars/bytes long. So we take 11 % 10^5, i.e.,
* 11 % 10000, i.e., 11, and zero-pad that value. So the padded block
* should be "90A00011"
* Assume a new string 1234567890ABCDE, length = 15, same block size
* of 8, so original string blocks = ["12345678", "90ABCDE"]. Now
* we take 15 % 10 ^ 1, i.e. 15 % 10, i.e. 5, and add that to the
* end of the block, so the final padded block is "90ABCDE5".
* This is known as Merkle-Damgard strengthening or padding.
* NOTE: Padding can have other meanings in cryptography.
* @param s string block to pad
* @param len length of original string
* @return String padded version of block
*/
public static String pad(String s, int len) {
int sizeToPad = BLOCK_SIZE - s.length();
int modValue = (int) Math.pow(10, sizeToPad);
int moddedLen = len % modValue;
String padded = String.format("%0" + sizeToPad + "d", moddedLen);
return padded;
}
/**
* Rounds a double to the given number of decimal places.
*
* @param value the double value
* @param afterDecimalPoint the number of digits after the decimal point
* @return the double rounded to the given precision
*/
public static /*@[email protected]*/ double roundDouble(double value,int afterDecimalPoint) {
double mask = Math.pow(10.0, (double)afterDecimalPoint);
return (double)(Math.round(value * mask)) / mask;
}