下面列出了怎么用java.lang.Math的API类实例代码及写法,或者点击链接到github查看源代码。
double eval_EC_cubr(char[] cromosoma) {
int i;
double ec, fitness;
/* Se calcula la adecuacion de la base de conocimiento codificada en el
cromosoma actual, se estudia la posible penalizacion del mismo y se
devuelve el valor final */
Decodifica(cromosoma);
Cubrimientos_Base();
if (mincb >= tau) {
ec = ErrorCuadratico();
fitness = (1 + Math.abs(1.0 - medcb)) * ec * P(cromosoma);
}
else {
fitness = maxEC;
}
return (fitness);
}
public Adap_Tun(MiDataset training, BaseR base, double valor_tau, int tipo) {
int i;
tabla = training;
base_reglas = base;
tau = valor_tau;
tipo_fitness = tipo;
long_regla = 3 * tabla.n_variables;
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];
}
public Adap_Sel (MiDataset training, BaseR_Sel base, BaseR_Sel base_t, double porc_radio_nicho, int n_soluciones, double valor_tau, double valor_alfa, int tipo) {
int i;
tabla = training;
base_reglas = base;
base_total = base_t;
tau = valor_tau;
alfa = valor_alfa;
tipo_fitness = tipo;
cont_soluciones = 0;
/* Calculo del radio del nicho */
radio_nicho = (int) (porc_radio_nicho * base_total.n_reglas);
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;
ListaTabu = new char[n_soluciones][base_total.n_reglas];
grado_pertenencia = new double[tabla.n_variables];
}
public void onDrawFrame(GL10 gl)
{
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glClearColor(0.0f,0.0f,0.0f,1.0f);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glTranslatef(0.0f,(float)Math.sin(mTransY), -4.0f);
gl.glRotatef(mAngle, 1, 0, 0);
gl.glRotatef(mAngle, 0, 1, 0);
mPlanet.draw(gl);
mTransY+=.075f;
mAngle+=.4;
}
/**
* Get threads, blocks and shared memory for a reduce all operation
* @param gCtx a valid {@link GPUContext}
* @param n size of input array
* @return integer array containing {blocks, threads, shared memory}
*/
private static int[] getKernelParamsForReduceAll(GPUContext gCtx, int n) {
final int MAX_THREADS = getMaxThreads(gCtx);
final int MAX_BLOCKS = getMaxBlocks(gCtx);
final int WARP_SIZE = getWarpSize(gCtx);
int threads = (n < MAX_THREADS *2) ? nextPow2((n + 1)/ 2) : MAX_THREADS;
int blocks = (n + (threads * 2 - 1)) / (threads * 2);
blocks = Math.min(MAX_BLOCKS, blocks);
int sharedMemSize = threads * sizeOfDataType;
if (threads <= WARP_SIZE) {
sharedMemSize *= 2;
}
return new int[] {blocks, threads, sharedMemSize};
}
public void onDrawFrame(GL10 gl)
{
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glClearColor(0.0f,0.0f,0.0f,1.0f);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glTranslatef(0.0f,(float)Math.sin(mTransY), -4.0f);
gl.glRotatef(mAngle, 1, 0, 0);
gl.glRotatef(mAngle, 0, 1, 0);
mPlanet.draw(gl);
mTransY+=.075f;
mAngle+=.4;
}
void readOptions(StringTokenizer st) {
int flags = new Integer(st.nextToken()).intValue();
dotsCheckItem.setState((flags & 1) != 0);
smallGridCheckItem.setState((flags & 2) != 0);
voltsCheckItem.setState((flags & 4) == 0);
powerCheckItem.setState((flags & 8) == 8);
showValuesCheckItem.setState((flags & 16) == 0);
timeStep = new Double (st.nextToken()).doubleValue();
double sp = new Double(st.nextToken()).doubleValue();
int sp2 = (int) (Math.log(10*sp)*24+61.5);
//int sp2 = (int) (Math.log(sp)*24+1.5);
speedBar.setValue(sp2);
currentBar.setValue(new Integer(st.nextToken()).intValue());
CircuitElm.voltageRange = new Double (st.nextToken()).doubleValue();
try {
powerBar.setValue(new Integer(st.nextToken()).intValue());
} catch (Exception e) {
}
setGrid();
}
private int filterCandidate(String src, String dst, int srcMatchPos, int dstMatchPos, int len,
int[][] distanceBuffer){
int srcRightLen = src.length() - srcMatchPos - len;
int dstRightLen = dst.length() - dstMatchPos - len;
int leftThreshold = mThreshold - Math.abs(srcRightLen - dstRightLen);
int leftDistance = calculateEditDistanceWithThreshold(src, 0, srcMatchPos,
dst, 0, dstMatchPos,
leftThreshold, distanceBuffer);
if (leftDistance > leftThreshold) {
return -1;
}
int rightThreshold = mThreshold - leftDistance;
int rightDistance = calculateEditDistanceWithThreshold(
src, srcMatchPos + len, src.length() - srcMatchPos - len,
dst, dstMatchPos + len, dst.length() - dstMatchPos - len,
rightThreshold, distanceBuffer);
if (rightDistance > rightThreshold) {
return -1;
}
return leftDistance + rightDistance;
}
void Decodifica (double [] cromosoma) {
int i, j;
for (i=0; i<base_reglas.n_reglas; i++) {
for (j=0; j<tabla.n_var_estado; j++) {
base_reglas.BaseReglas[i].Ant[j].x0 = cromosoma[3*(i*tabla.n_var_estado+j)];
base_reglas.BaseReglas[i].Ant[j].x1 = cromosoma[3*(i*tabla.n_var_estado+j)+1];
base_reglas.BaseReglas[i].Ant[j].x2 = cromosoma[3*(i*tabla.n_var_estado+j)+1];
base_reglas.BaseReglas[i].Ant[j].x3 = cromosoma[3*(i*tabla.n_var_estado+j)+2];
base_reglas.BaseReglas[i].Ant[j].y = 1.0;
base_reglas.BaseReglas[i].Ant[j].Nombre = "x" + (j+1);
base_reglas.BaseReglas[i].Ant[j].Etiqueta = "E" + i + j;
base_reglas.BaseReglas[i].Cons[j] = Math.tan (cromosoma[primer_gen_C2+i*(tabla.n_variables)+j]);
}
base_reglas.BaseReglas[i].Cons[j] = Math.tan (cromosoma[primer_gen_C2+i*(tabla.n_variables)+j]);
}
}
double eval_EC_cubr(char[] cromosoma) {
int i;
double ec, fitness;
/* Se calcula la adecuacion de la base de conocimiento codificada en el
cromosoma actual, se estudia la posible penalizacion del mismo y se
devuelve el valor final */
Decodifica(cromosoma);
Cubrimientos_Base();
if (mincb >= tau && min_reglas <= base_reglas.n_reglas) {
ec = ErrorCuadratico();
fitness = (1 + Math.abs(1.0 - medcb)) * ec * P(cromosoma);
}
else {
fitness = maxEC;
}
return (fitness);
}
public static void pfactors(int n){
while (n%2==0)
{
System.out.print(2 + " ");
n /= 2;
}
for (int i=3; i<= Math.sqrt(n); i+=2)
{
while (n%i == 0)
{
System.out.print(i + " ");
n /= i;
}
}
if(n > 2)
System.out.print(n);
}
public Object acos(Object param)
throws ParseException
{
if (param instanceof Complex)
{
return ((Complex)param).acos();
}
else if (param instanceof Number)
{
return new Double(Math.acos(((Number)param).doubleValue()));
}
throw new ParseException("Invalid parameter 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 final static 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;
}
void _doStateChangeV1(boolean force) {
// compile our state value
short s = _buttonStateV1; // buttons
s |= (_dPadStateH > 0 ? 1 : 0) << 5; // sign bit
s |= ((int) (Math.round(Math.min(1.0, Math.abs(_dPadStateH)) * 15.0))) <<
6; // mag
s |= (_dPadStateV > 0 ? 1 : 0) << 10; // sign bit
s |= ((int) (Math.round(Math.min(1.0, Math.abs(_dPadStateV)) * 15.0))) <<
11; // mag
// if our compiled state value hasn't changed, don't send.
// (analog joystick noise can send a bunch of redundant states through here)
// The exception is if forced is true, which is the case with packets that
// double as keepalives.
if ((s == _lastSentState) && (!force)) {
return;
}
_stateBirthTimes[_nextState] = SystemClock.uptimeMillis();
_stateLastSentTimes[_nextState] = 0;
if (debug) {
Log.v(TAG, "STORING NEXT STATE: " + _nextState);
}
_statesV1[_nextState] = s;
_nextState = (_nextState + 1) % 256;
_lastSentState = s;
// if we're pretty up to date as far as state acks, lets go ahead
// and send out this state immediately..
// (keeps us nice and responsive on low latency networks)
int unackedCount = (_nextState - _requestedState) & 0xFF; // upcast to
// get
// unsigned
if (unackedCount < 3) {
_shipUnAckedStatesV1();
}
}
/**
* Generates random values and performs the inserts into the database
*/
public static int insertRow(PreparedStatement ps) {
int rowsAdded = 0;
try {
// Generate random values for the datatypes in the sample table
Random rand = new Random();
int intVal = Math.abs(rand.nextInt()%1000);
String charVal = "Derby";
synchronized(lock) {
charVal += counter;
counter++;
}
// Set parameter values
ps.setInt(1, intVal);
ps.setString(2,charVal);
ps.setFloat(3, rand.nextFloat()*(float)Math.pow(10,Math.abs(rand.nextInt()%30)));
ps.setLong(4,rand.nextLong()%10000);
rowsAdded = ps.executeUpdate();
return rowsAdded;
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
/**
* Constructs a new <CODE>SnmpStringFixed</CODE> from the specified <CODE>Bytes</CODE> array
* with the specified length.
* @param l The length of the fixed-string.
* @param v The <CODE>Bytes</CODE> composing the fixed-string value.
* @exception IllegalArgumentException Either the length or the <CODE>Byte</CODE> array is not valid.
*/
public SnmpStringFixed(int l, Byte[] v) throws IllegalArgumentException {
if ((l <= 0) || (v == null)) {
throw new IllegalArgumentException() ;
}
int length = Math.min(l, v.length);
value = new byte[l] ;
for (int i = 0 ; i < length ; i++) {
value[i] = v[i].byteValue() ;
}
for (int i = length ; i < l ; i++) {
value[i] = 0 ;
}
}
/** Generates a normal value with hope 0 and tipical deviation "desv */
private double ValorNormal (double desv) {
double u1, u2;
/* we generate 2 uniform values [0,1] */
u1=Randomize.Rand ();
u2=Randomize.Rand ();
/* we calcules a normal value with the uniform values */
return (desv * Math.sqrt (-2.0 * Math.log(u1)) * Math.sin (2.0*PI*u2));
}
CpcgMetrics finish() {
final CpcgMetrics m = new CpcgMetrics();
m.LIBRARY = this.library;
m.CONTEXT = this.context;
m.TOTAL_SITES = this.sites;
m.TOTAL_BASES = this.refCcontrolC + this.refCoxidatedC + this.refCcontrolA + this.refCoxidatedA +
this.refGcontrolC + this.refGoxidatedC + this.refGcontrolA + this.refGoxidatedA;
m.REF_OXO_BASES = this.refCoxidatedC + refGoxidatedC;
m.REF_NONOXO_BASES = this.refCcontrolC + this.refGcontrolC;
m.REF_TOTAL_BASES = m.REF_OXO_BASES + m.REF_NONOXO_BASES;
m.ALT_NONOXO_BASES = this.refCcontrolA + this.refGcontrolA;
m.ALT_OXO_BASES = this.refCoxidatedA + this.refGoxidatedA;
/**
* Why do we calculate the oxo error rate using oxidatedA - controlA you ask? We know that all the
* bases counted in oxidatedA are consistent with 8-oxo-G damage during shearing, but not all of them
* will have been caused by this. If we assume that C>A errors caused by other factors will occur randomly
* with respect to read1/read2, then we should see as many in the 8-oxo-G consistent state as not. So we
* assume that controlA is half the story, and remove the other half from oxidatedA.
*/
m.OXIDATION_ERROR_RATE = Math.max(m.ALT_OXO_BASES - m.ALT_NONOXO_BASES, 1) / (double) m.TOTAL_BASES;
m.OXIDATION_Q = -10 * Math.log10(m.OXIDATION_ERROR_RATE);
/** Now look for things that have a reference base bias! */
m.C_REF_REF_BASES = this.refCcontrolC + this.refCoxidatedC;
m.G_REF_REF_BASES = this.refGcontrolC + this.refGoxidatedC;
m.C_REF_ALT_BASES = this.refCcontrolA + this.refCoxidatedA;
m.G_REF_ALT_BASES = this.refGcontrolA + this.refGoxidatedA;
final double cRefErrorRate = m.C_REF_ALT_BASES / (double) (m.C_REF_ALT_BASES + m.C_REF_REF_BASES);
final double gRefErrorRate = m.G_REF_ALT_BASES / (double) (m.G_REF_ALT_BASES + m.G_REF_REF_BASES);
m.C_REF_OXO_ERROR_RATE = Math.max(cRefErrorRate - gRefErrorRate, 1e-10);
m.G_REF_OXO_ERROR_RATE = Math.max(gRefErrorRate - cRefErrorRate, 1e-10);
m.C_REF_OXO_Q = -10 * Math.log10(m.C_REF_OXO_ERROR_RATE);
m.G_REF_OXO_Q = -10 * Math.log10(m.G_REF_OXO_ERROR_RATE);
return m;
}
/**
* Constructs a new <CODE>SnmpStringFixed</CODE> from the specified <CODE>Bytes</CODE> array
* with the specified length.
* @param l The length of the fixed-string.
* @param v The <CODE>Bytes</CODE> composing the fixed-string value.
* @exception IllegalArgumentException Either the length or the <CODE>Byte</CODE> array is not valid.
*/
public SnmpStringFixed(int l, Byte[] v) throws IllegalArgumentException {
if ((l <= 0) || (v == null)) {
throw new IllegalArgumentException() ;
}
int length = Math.min(l, v.length);
value = new byte[l] ;
for (int i = 0 ; i < length ; i++) {
value[i] = v[i].byteValue() ;
}
for (int i = length ; i < l ; i++) {
value[i] = 0 ;
}
}
/**
* Constructs a new <CODE>SnmpStringFixed</CODE> from the specified <CODE>String</CODE>
* with the specified length.
* @param l The length of the fixed-string.
* @param s The <CODE>String</CODE> composing the fixed-string value.
* @exception IllegalArgumentException Either the length or the <CODE>String</CODE> is not valid.
*/
public SnmpStringFixed(int l, String s) throws IllegalArgumentException {
if ((l <= 0) || (s == null)) {
throw new IllegalArgumentException() ;
}
byte[] v = s.getBytes();
int length = Math.min(l, v.length);
value = new byte[l] ;
for (int i = 0 ; i < length ; i++) {
value[i] = v[i] ;
}
for (int i = length ; i < l ; i++) {
value[i] = 0 ;
}
}
private static long randAbs(long l) {
if (l == Long.MIN_VALUE) {
return Long.MAX_VALUE; // 2's complement, so no way to make value
// positive
} else {
return Math.abs(l);
}
}
/**
* Constructs a new <CODE>SnmpStringFixed</CODE> from the specified <CODE>String</CODE>
* with the specified length.
* @param l The length of the fixed-string.
* @param s The <CODE>String</CODE> composing the fixed-string value.
* @exception IllegalArgumentException Either the length or the <CODE>String</CODE> is not valid.
*/
public SnmpStringFixed(int l, String s) throws IllegalArgumentException {
if ((l <= 0) || (s == null)) {
throw new IllegalArgumentException() ;
}
byte[] v = s.getBytes();
int length = Math.min(l, v.length);
value = new byte[l] ;
for (int i = 0 ; i < length ; i++) {
value[i] = v[i] ;
}
for (int i = length ; i < l ; i++) {
value[i] = 0 ;
}
}
private BufferedImage produceForegroundImageIcon(BufferedImage icon) {
int imageWidth = icon.getWidth();
// According to the adaptive icon documentation, both layers are 108x108dp but only the inner
// 72x72dp appears in the masked viewport, so we shrink down the size of the image accordingly.
double iconWidth = imageWidth * 72.0 / 108.0;
// Round iconWidth value to even int for a centered png
int intIconWidth = ((int)Math.round(iconWidth / 2) * 2);
Image tmp = icon.getScaledInstance(intIconWidth, intIconWidth, Image.SCALE_SMOOTH);
int marginWidth = ((imageWidth - intIconWidth) / 2);
BufferedImage foregroundImageIcon = new BufferedImage(imageWidth, imageWidth, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = foregroundImageIcon.createGraphics();
g2.drawImage(tmp, marginWidth, marginWidth, null);
return foregroundImageIcon;
}
/** Generates a normal value with hope 0 and tipical deviation "desv */
private double ValorNormal(double desv) {
double u1, u2;
/* we generate 2 uniform values [0,1] */
u1 = Randomize.Rand();
u2 = Randomize.Rand();
/* we calcules a normal value with the uniform values */
return (desv * Math.sqrt( -2 * Math.log(u1)) * Math.sin(2 * PI * u2));
}
public Object sin(Object param)
throws ParseException
{
if (param instanceof Complex) {
return ((Complex)param).sin();
}
else if (param instanceof Number) {
return new Double(Math.sin(((Number)param).doubleValue()));
}
throw new ParseException("Invalid parameter type");
}
public static double calculateVariance(int [] population, int n){
double mean = calculateMean(population,n);
double sum = 0;
for(int i : population){
double temp = (double) i - mean;
sum += Math.pow(temp,2);
}
return sum/n;
}
/** Generates a normal value with hope 0 and tipical deviation "desv */
public double ValorNormal(double desv) {
double u1, u2;
/* we generate 2 uniform values [0,1] */
u1 = Randomize.Rand();
u2 = Randomize.Rand();
/* we calcules a normal value with the uniform values */
return (desv * Math.sqrt( -2.0 * Math.log(u1)) * Math.sin(2.0 * Math.PI * u2));
}
public Object sinh(Object param)
throws ParseException
{
if (param instanceof Complex)
{
return ((Complex)param).sinh();
}
else if (param instanceof Number)
{
double value = ((Number)param).doubleValue();
return new Double((Math.exp(value)-Math.exp(-value))/2);
}
throw new ParseException("Invalid parameter type");
}
/** Generates a normal value with hope 0 and tipical deviation "desv */
private double ValorNormal (double desv) {
double u1, u2;
/* we generate 2 uniform values [0,1] */
u1 = Randomize.Rand ();
u2 = Randomize.Rand ();
/* we calcules a normal value with the uniform values */
return (desv * Math.sqrt (-2 * Math.log(u1)) * Math.sin (2*Math.PI*u2));
}
public void onSurfaceChanged(GL10 gl, int width, int height) {
gl.glViewport(0, 0, width, height);
/*
* Set our projection matrix. This doesn't have to be done
* each time we draw, but usually a new projection needs to
* be set when the viewport is resized.
*/
float aspectRatio;
float zNear =.1f;
float zFar =1000f;
float fieldOfView = 30.0f/57.3f;
float size;
gl.glEnable(GL10.GL_NORMALIZE);
aspectRatio=(float)width/(float)height; //h/w clamps the fov to the height, flipping it would make it relative to the width
//Set the OpenGL projection matrix
gl.glMatrixMode(GL10.GL_PROJECTION);
size = zNear * (float)(Math.tan((double)(fieldOfView/2.0f)));
gl.glFrustumf(-size, size, -size/aspectRatio, size /aspectRatio, zNear, zFar);
//Make the OpenGL modelview matrix the default
gl.glMatrixMode(GL10.GL_MODELVIEW);
}