@Override public boolean onTouchEvent(MotionEvent event) { float x = event.getX() - CENTER_X; float y = event.getY() - CENTER_Y; boolean inCenter = java.lang.Math.sqrt(x * x + y * y) <= CENTER_RADIUS; switch (event.getAction()) { case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_MOVE: if (!inCenter) { float angle = (float) java.lang.Math.atan2(y, x); // need to turn angle [-PI ... PI] into unit [0....1] float unit = angle / (2 * PI); if (unit < 0) { unit += 1; } int color = interpColor(mColors, unit); mCenterPaint.setColor(color); this.setColor(color); invalidate(); } break; } return true; }
public void leafLayer(int cx, int cy, int cz, int width) { for (int x = Math.max(0, cx - width); x < Math.min(m_width, cx + width); x++) { for (int y = Math.max(0, cy - width); y < Math.min(m_height, cy + width); y++) { m_blocks[x][y][cz] = BlockConstants.LEAVES; } } }
public Complex powi(float p) { float magnew = (float) Math.pow((double) abs(), (double) p); float phasenew = arg() * p; re = magnew * (float) Math.cos(phasenew); im = magnew * (float) Math.sin(phasenew); return this; }
/* Operador de Mutacion Uniforme */ void Mutacion_Uniforme() { int posiciones, i, j; double m; posiciones = n_genes * long_poblacion; if (prob_mutacion > 0) { while (Mu_next < posiciones) { /* we determinate the chromosome and the gene */ i = Mu_next / n_genes; j = Mu_next % n_genes; /* we mutate the gene */ if (New[i].Gene[j] == '0') New[i].Gene[j] = '1'; else New[i].Gene[j] = '0'; New[i].n_e = 1; /* we calculate the next position */ if (prob_mutacion < 1) { m = Randomize.Rand(); Mu_next += (int) (Math.log(m) / Math.log(1.0 - prob_mutacion)) + 1; } else Mu_next += 1; } } Mu_next -= posiciones; }
private void averageArea(int sx, int sy, int radius) { for (int x = Math.max(0, sx - radius); x < Math.min(m_width, sx + radius); x++) { for (int y = Math.max(0, sy - radius); y < Math.min(m_height, sy + radius); y++) { m_contour[x][y] = (m_contour[sx][sy] + m_contour[x][y]) / 2; } } }
public boolean seen(int stimVal, double threshold, int numPresentations, int x, int y) { int falseResponse = checkFalseResponse(); if (falseResponse == +1) return true; if (falseResponse == -1) return false; double hensonStdDev = Math.min(Math.exp(m_a * threshold + m_b), 5.0); return GaussianSeen(stimVal, threshold, numPresentations, hensonStdDev); }
@Override public void handleCommand(InsteonPLMBindingConfig conf, Command cmd, InsteonDevice dev) { try { if (cmd == OnOffType.ON) { Msg m = dev.makeStandardMessage((byte) 0x0f, (byte) 0x11, (byte) 0xff); dev.enqueueMessage(m, m_feature); logger.info("{}: sent msg to switch {} on", nm(), dev.getAddress()); } else if (cmd == OnOffType.OFF) { Msg m = dev.makeStandardMessage((byte) 0x0f, (byte) 0x13, (byte) 0x00); dev.enqueueMessage(m, m_feature); logger.info("{}: sent msg to switch {} off", nm(), dev.getAddress()); } // This used to be configurable, but was made static to make // the architecture of the binding cleaner. int delay = 2000; delay = Math.max(1000, delay); delay = Math.min(10000, delay); Timer timer = new Timer(); timer.schedule( new TimerTask() { @Override public void run() { Msg m = m_feature.makePollMsg(); InsteonDevice dev = m_feature.getDevice(); if (m != null) dev.enqueueMessage(m, m_feature); } }, delay); } catch (IOException e) { logger.error("{}: command send i/o error: ", nm(), e); } catch (FieldException e) { logger.error("{}: command send message creation error: ", nm(), e); } }
/** Inicialization of the population */ public void Initialize() { int i, j; last = (int) ((prob_cruce * long_poblacion) - 0.5); Trials = 0; if (prob_mutacion < 1.0) { Mu_next = (int) (Math.log(Randomize.Rand()) / Math.log(1.0 - prob_mutacion)); Mu_next++; } else { Mu_next = 1; } for (j = 0; j < n_genes; j++) { New[0].GeneSel[j] = '1'; } New[0].n_e = 1; for (i = 1; i < long_poblacion; i++) { for (j = 0; j < n_genes; j++) { if (Randomize.RandintClosed(0, 1) == 0) { New[i].GeneSel[j] = '0'; } else { New[i].GeneSel[j] = '1'; } } New[i].n_e = 1; } }
@Override public void evaluate(Solution solution) { double[] theta = new double[numberOfObjectives - 1]; double[] f = new double[numberOfObjectives]; double[] x = EncodingUtils.getReal(solution); int k = numberOfVariables - numberOfObjectives + 1; double g = 0.0; for (int i = numberOfVariables - k; i < numberOfVariables; i++) { g += java.lang.Math.pow(x[i], 0.1); } double t = java.lang.Math.PI / (4.0 * (1.0 + g)); theta[0] = x[0] * java.lang.Math.PI / 2; for (int i = 1; i < (numberOfObjectives - 1); i++) { theta[i] = t * (1.0 + 2.0 * g * x[i]); } for (int i = 0; i < numberOfObjectives; i++) { f[i] = 1.0 + g; for (int j = 0; j < numberOfObjectives - (i + 1); j++) { f[i] *= java.lang.Math.cos(theta[j]); } if (i != 0) { int aux = numberOfObjectives - (i + 1); f[i] *= java.lang.Math.sin(theta[aux]); } solution.setObjective(i, f[i]); } }
@Override public void onClick(View v) { if ((Math.abs(freeX - myX) == 1 && freeY == myY) || (Math.abs(freeY - myY) == 1 && freeX == myX)) { exchange(myX, myY); myActivity .getResult() .setText(String.valueOf(Integer.parseInt((String) myActivity.getResult().getText()) + 1)); if (freeY == DIMENSION - 1 && freeX == DIMENSION - 1) { boolean isSorted = true; for (int i = 0; i < DIMENSION * DIMENSION - 1; ++i) { if (!((Button) myAdapter.getItem(i)).getText().equals(String.valueOf(i + 1))) { isSorted = false; break; } } if (isSorted) { Intent intent = new Intent(myActivity, GiveName.class); myActivity.startActivityForResult(intent, 1); } } } }
/** Расчет результата */ public void calculation() { if (mathOperation.getTypeMathOperation() == TypeMathOperation.ADD) { lastResult = firstNumber + secondNumber; } else if (mathOperation.getTypeMathOperation() == TypeMathOperation.SUB) { lastResult = firstNumber - secondNumber; } else if (mathOperation.getTypeMathOperation() == TypeMathOperation.MUL) { lastResult = firstNumber * secondNumber; } else if (mathOperation.getTypeMathOperation() == TypeMathOperation.DIV) { // try { lastResult = firstNumber / secondNumber; // } catch (ArithmeticException rte){ // lastResult = 0; // textMessage = "div by ZERO"; // changed(); // return; // } } else if (mathOperation.getTypeMathOperation() == TypeMathOperation.MRC) { mrc = lastResult; } else if (mathOperation.getTypeMathOperation() == TypeMathOperation.COS) { lastResult = (float) Math.cos(firstNumber); } else if (mathOperation.getTypeMathOperation() == TypeMathOperation.SIN) { lastResult = (float) Math.sin(firstNumber); } else if (mathOperation.getTypeMathOperation() == TypeMathOperation.TG) { lastResult = (float) Math.tan(firstNumber); } else if (mathOperation.getTypeMathOperation() == TypeMathOperation.CT) { lastResult = (float) 1 / ((float) Math.tan(firstNumber)); } textMessage = "Result: " + String.valueOf(lastResult); changed(); }
/** * Perform the mutation operation * * @param probability Mutation probability * @param solution The solution to mutate * @throws JMException */ public void doMutation(double probability, Solution solution) throws JMException { double rnd, delta1, delta2, mut_pow, deltaq; double y, yl, yu, val, xy; XReal x = new XReal(solution); for (int var = 0; var < solution.numberOfVariables(); var++) { if (PseudoRandom.randDouble() <= probability) { y = x.getValue(var); yl = x.getLowerBound(var); yu = x.getUpperBound(var); delta1 = (y - yl) / (yu - yl); delta2 = (yu - y) / (yu - yl); rnd = PseudoRandom.randDouble(); mut_pow = 1.0 / (eta_m_ + 1.0); if (rnd <= 0.5) { xy = 1.0 - delta1; val = 2.0 * rnd + (1.0 - 2.0 * rnd) * (Math.pow(xy, (eta_m_ + 1.0))); deltaq = java.lang.Math.pow(val, mut_pow) - 1.0; } else { xy = 1.0 - delta2; val = 2.0 * (1.0 - rnd) + 2.0 * (rnd - 0.5) * (java.lang.Math.pow(xy, (eta_m_ + 1.0))); deltaq = 1.0 - (java.lang.Math.pow(val, mut_pow)); } y = y + deltaq * (yu - yl); if (y < yl) y = yl; if (y > yu) y = yu; x.setValue(var, y); } } // for } // doMutation
/** * Convert a year, month, and day of month to a julian date * * @param year The year - it will be satitizedYear() * @param month The month * @param day The day of the month. * @return the julian day */ public static int toJulian(int year, int month, int day) { int julianYear = sanitizeYear(year); if (year < 0) julianYear++; int julianMonth = month; if (month > 2) { julianMonth++; } else { julianYear--; julianMonth += 13; } double julian = (java.lang.Math.floor(365.25 * julianYear) + java.lang.Math.floor(30.6001 * julianMonth) + day + 1720995.0); if (day + 31 * (month + 12 * year) >= JGREG) { // change over to Gregorian calendar int ja = (int) (0.01 * julianYear); julian += 2 - ja + (0.25 * ja); } return (int) (java.lang.Math.floor(julian) + .000001); }
public int getWrongTwo() { int wrongTwo = (int) (Math.random() * (Math.pow(2, bits))); while (wrongTwo == answer || wrongTwo == wrongOne) wrongOne = (int) (Math.random() * (Math.pow(2, bits))); return wrongTwo; }
@Override public void update(GameContainer gc, StateBasedGame sb, int delta) { float rotation = owner.getRotation(); float scale = owner.getScale(); Vector2f position = owner.getPosition(); Input input = gc.getInput(); if (input.isKeyDown(Input.KEY_LEFT)) { rotation += -0.2f * delta; } if (input.isKeyDown(Input.KEY_RIGHT)) { rotation += 0.2f * delta; } if (input.isKeyDown(Input.KEY_UP)) { float hip = 0.45f * delta; position.x += hip * java.lang.Math.sin(java.lang.Math.toRadians(rotation)); position.y -= hip * java.lang.Math.cos(java.lang.Math.toRadians(rotation)); } owner.setPosition(position); owner.setRotation(rotation); owner.setScale(scale); }
// 檢查其子節點並做符號的標示 public static String checkChild(int[][] tmp, int i, int j, int N, int T, int counter) { int testtest = 0; String test = ""; // first & child checking // check if they are greater than T if ((Math.abs(tmp[2 * i][j * 2]) < T) && (Math.abs(tmp[2 * i + 1][j * 2]) < T) && (Math.abs(tmp[2 * i][j * 2 + 1]) < T) && (Math.abs(tmp[2 * i + 1][j * 2 + 1]) < T)) { if (counter == 1) { i = 2 * i; j = 2 * j; for (int r = 0; r < 4; r++) { for (int s = 0; s < 4; s++) { if (Math.abs(tmp[2 * i + r][j * 2 + s]) > T) { testtest++; // IdxTable[2*i+r][j*2+s] = 1; } // else IdxTable[2*i+r][j*2+s] = 0; } } if ((testtest == 0) || (counter == 2)) test = "0"; else test = "1"; } // end if counter ==1 } else test = "1"; return test; }
private static double nextRandom() { int pos = (int) (java.lang.Math.random() * BUFFER_SIZE); if (pos == BUFFER_SIZE) pos = BUFFER_SIZE - 1; double r = buffer[pos]; buffer[pos] = java.lang.Math.random(); return r; }
// These x y values correspond to the x y values of the corners of tiles // get the interpolated height on the map public float getTerrainHeight(float x, float y) { // first find what corner it is int ix = Math.round(x * DENSITY); int iy = Math.round(y * DENSITY); // clip the coordinates if (ix < 0 || ix >= mapWidth) { return 0.0f; } else if (iy < 0 || iy >= mapHeight) { return 0.0f; } // we are in the center /*Vector3f v1 = new Vector3f(), v2 = new Vector3f(); v1.sub(points[iy][ix + 1], points[iy][ix]); v2.sub(points[iy + 1][ix], points[iy][ix]); float dx = x - ix; float dy = y - iy; v1.scale(dx); v2.scale(dy); v1.add(v1, v2); v1.add(points[iy][ix], v1); return v1.y;*/ float terrainHeight = vertices[(iy * (mapWidth) + ix) * 3 + 1]; // 1 for y return terrainHeight; }
/** * Perform a great circle (spherical linear) interpolation between this quaternion and the * quaternion parameter. * * @param q The other quaternion. * @param alpha The interpolation parameter from the interval [0, 1]. * @return The interpolated quaternion. */ public final Quaternion interpolate(Quaternion q, float alpha) { // From "Advanced Animation and Rendering Techniques" // by Watt and Watt pg. 364, function as implemented appeared to be // incorrect. Fails to choose the same quaternion for the float // covering. Resulting in change of direction for rotations. // Fixed function to negate the first quaternion in the case that the // dot product of q and this is negative. Second case was not needed. float dot, s1, s2, om, sinom; dot = x * q.x + y * q.y + z * q.z + w * q.w; if (dot < 0) { q = q.negate(); dot = -dot; } if ((1.0 - dot) > EPS) { om = (float) Math.acos(dot); sinom = (float) Math.sin(om); s1 = (float) Math.sin((1.0 - alpha) * om) / sinom; s2 = (float) Math.sin(alpha * om) / sinom; } else { s1 = 1.0f - alpha; s2 = alpha; } return new Quaternion( s1 * w + s2 * q.w, s1 * x + s2 * q.x, s1 * y + s2 * q.y, s1 * z + s2 * q.z); }
// Right from Monty's code public static String newCrypt(String password, String seed) { byte b; double d; if ((password == null) || (password.length() == 0)) { return password; } long[] pw = newHash(seed); long[] msg = newHash(password); long max = 0x3fffffffL; long seed1 = (pw[0] ^ msg[0]) % max; long seed2 = (pw[1] ^ msg[1]) % max; char[] chars = new char[seed.length()]; for (int i = 0; i < seed.length(); i++) { seed1 = ((seed1 * 3) + seed2) % max; seed2 = (seed1 + seed2 + 33) % max; d = (double) seed1 / (double) max; b = (byte) java.lang.Math.floor((d * 31) + 64); chars[i] = (char) b; } seed1 = ((seed1 * 3) + seed2) % max; seed2 = (seed1 + seed2 + 33) % max; d = (double) seed1 / (double) max; b = (byte) java.lang.Math.floor(d * 31); for (int i = 0; i < seed.length(); i++) { chars[i] ^= (char) b; } return new String(chars); }
public GpsDevice() { m_latitude = new Measurement(java.lang.Math.toRadians(0), Unit.rad); m_longitude = new Measurement(java.lang.Math.toRadians(0), Unit.rad); m_altitude = new Measurement(0, Unit.m); m_speed = new Measurement(0, Unit.m_s); m_track = new Measurement(java.lang.Math.toRadians(0), Unit.rad); }
private static Map<String, Integer> computeColumnWidths( final Iterable<CamelContext> camelContexts) throws Exception { if (camelContexts == null) { throw new IllegalArgumentException( "Unable to determine column widths from null Iterable<CamelContext>"); } else { int maxNameLen = 0; int maxStatusLen = 0; int maxUptimeLen = 0; for (final CamelContext camelContext : camelContexts) { final String name = camelContext.getName(); maxNameLen = java.lang.Math.max(maxNameLen, name == null ? 0 : name.length()); final String status = camelContext.getStatus().toString(); maxStatusLen = java.lang.Math.max(maxStatusLen, status == null ? 0 : status.length()); final String uptime = camelContext.getUptime(); maxUptimeLen = java.lang.Math.max(maxUptimeLen, uptime == null ? 0 : uptime.length()); } final Map<String, Integer> retval = new Hashtable<String, Integer>(3); retval.put(NAME_COLUMN_LABEL, maxNameLen); retval.put(STATUS_COLUMN_LABEL, maxStatusLen); retval.put(UPTIME_COLUMN_LABEL, maxUptimeLen); return retval; } }
public void applyContour() { int maxHeight = 1; for (int x = 0; x < m_width; x++) { for (int y = 0; y < m_height; y++) { if (m_contour[x][y] > maxHeight) maxHeight = m_contour[x][y]; } } m_logger.debug("Applying contour"); for (int x = 0; x < m_width; x++) { for (int y = 0; y < m_height; y++) { int h = Math.max(0, Math.min(m_depth - 1, (m_depth / 2) + m_contour[x][y])); // int d = m_random.nextInt(8) - 4; for (int z = 0; z < m_depth; z++) { int type = BlockConstants.AIR; if (z >= h && z < m_depth / 2 - 1) { type = BlockConstants.WATER; } else if (z >= h) { type = BlockConstants.AIR; } else if (z == (h - 1)) { type = BlockConstants.GRASS; } else if (z < (h - 1) && z > (h - 5)) { type = BlockConstants.DIRT; } else if (z <= (h - 5)) { type = BlockConstants.ROCK; } m_blocks[x][y][z] = (byte) type; } } } }
/** * Returns the correlation coefficient of two double vectors. * * @param y1 double vector 1 * @param y2 double vector 2 * @param n the length of two double vectors * @return the correlation coefficient */ public static final double correlation(double y1[], double y2[], int n) { int i; double av1 = 0.0, av2 = 0.0, y11 = 0.0, y22 = 0.0, y12 = 0.0, c; if (n <= 1) { return 1.0; } for (i = 0; i < n; i++) { av1 += y1[i]; av2 += y2[i]; } av1 /= (double) n; av2 /= (double) n; for (i = 0; i < n; i++) { y11 += (y1[i] - av1) * (y1[i] - av1); y22 += (y2[i] - av2) * (y2[i] - av2); y12 += (y1[i] - av1) * (y2[i] - av2); } if (y11 * y22 == 0.0) { c = 1.0; } else { c = y12 / Math.sqrt(Math.abs(y11 * y22)); } return c; }
public void plantTrees() { ArrayList<Position> treeList = new ArrayList<Position>(); for (int x = 0; x < m_width; x++) { for (int y = 0; y < m_height; y++) { boolean tooClose = false; for (Position p : treeList) { double distance = Math.sqrt(Math.pow(p.getX() - x, 2) + Math.pow(p.getY() - y, 2)); if (distance < 30) tooClose = true; } if (!tooClose) { if (m_random.nextInt(100) <= 5) { for (int z = m_depth - 1; z > 0; z--) { if ((m_blocks[x][y][z] == BlockConstants.DIRT || m_blocks[x][y][z] == BlockConstants.GRASS) && m_blocks[x][y][z + 1] == BlockConstants.AIR) { plantTree(x, y, z); treeList.add(new Position(x, y, z)); break; } else if (z < m_depth - 1 && m_blocks[x][y][z + 1] != BlockConstants.AIR) { break; } } } } } } }
public double distanciaEuclidiana(int x1, int x2, int y1, int y2) { double a, b, c; a = Math.pow(x2 - x1, 2); b = Math.pow(y2 - y1, 2); c = Math.sqrt(a + b); return (c); }
/** * check if the given position collides with the flashlight. * * @param point * @return */ public boolean flashlightCollision(Point2D point) { flY23 = flY2 - flY3; flX32 = flX3 - flX2; flY31 = flY3 - flY1; flX13 = flX1 - flX3; flDet = flY23 * flX13 - flX32 * flY31; flMinD = Math.min(flDet, 0); flMaxD = Math.max(flDet, 0); double x = point.getX(); double y = point.getY(); double dx = x - flX3; double dy = y - flY3; double a = flY23 * dx + flX32 * dy; if (a < flMinD || a > flMaxD) { return false; } double b = flY31 * dx + flX13 * dy; if (b < flMinD || b > flMaxD) { return false; } double c = flDet - a - b; return !(c < flMinD || c > flMaxD); }
// ************************************ public static void initWeights() { for (int j = 0; j < numHidden; j++) { weightsHO[j] = (Math.random() - 0.5) / 2; for (int i = 0; i < numInputs; i++) weightsIH[i][j] = (Math.random() - 0.5) / 5; } }
/** * Evaluates a solution * * @param solution The solution to evaluate * @throws JMException */ public void evaluate(Solution solution) throws JMException { DecisionVariables gen = solution.getDecisionVariables(); double[] x = new double[numberOfVariables_]; double[] f = new double[numberOfObjectives_]; double[] theta = new double[numberOfObjectives_ - 1]; int k = numberOfVariables_ - numberOfObjectives_ + 1; for (int i = 0; i < numberOfVariables_; i++) x[i] = gen.variables_[i].getValue(); double g = 0.0; for (int i = numberOfVariables_ - k; i < numberOfVariables_; i++) g += java.lang.Math.pow(x[i], 0.1); double t = java.lang.Math.PI / (4.0 * (1.0 + g)); theta[0] = x[0] * java.lang.Math.PI / 2; for (int i = 1; i < (numberOfObjectives_ - 1); i++) theta[i] = t * (1.0 + 2.0 * g * x[i]); for (int i = 0; i < numberOfObjectives_; i++) f[i] = 1.0 + g; for (int i = 0; i < numberOfObjectives_; i++) { for (int j = 0; j < numberOfObjectives_ - (i + 1); j++) f[i] *= java.lang.Math.cos(theta[j]); if (i != 0) { int aux = numberOfObjectives_ - (i + 1); f[i] *= java.lang.Math.sin(theta[aux]); } // if } // for for (int i = 0; i < numberOfObjectives_; i++) solution.setObjective(i, f[i]); } // evaluate
@Override public double predict(Example example) throws OperatorException { int i = 0; double distance = intercept; // using kernel for distance calculation double[] values = new double[example.getAttributes().size()]; for (Attribute currentAttribute : example.getAttributes()) { values[i] = example.getValue(currentAttribute); i++; } distance += kernel.calculateDistance(values, coefficients); if (getLabel().isNominal()) { int positiveMapping = getLabel().getMapping().mapString(classPositive); int negativeMapping = getLabel().getMapping().mapString(classNegative); boolean isApplying = example.getAttributes().getPredictedLabel() != null; if (isApplying) { example.setConfidence(classPositive, 1.0d / (1.0d + java.lang.Math.exp(-distance))); example.setConfidence(classNegative, 1.0d / (1.0d + java.lang.Math.exp(distance))); } if (distance < 0) { return negativeMapping; } else { return positiveMapping; } } else { return distance; } }