📄 doublenumbertoken.java
字号:
double x = arg2[REAL];
double y = arg2[IMAG];
double zRe, zIm;
double scalar;
double[] temp = new double[2];
if ((x==0.0) && (y==0.0))
{
// something like 1/0 50/0 or 0/0
// real part
if (Double.isNaN(arg1[REAL]))
zRe = Double.NaN;
else if (arg1[REAL]>0)
zRe = Double.POSITIVE_INFINITY;
else if (arg1[REAL]<0)
zRe = Double.NEGATIVE_INFINITY;
else
zRe = Double.NaN;
// something like (1+i)/0 50i/0
// imaginary part
if (Double.isNaN(arg1[IMAG]))
zIm = Double.NaN;
else if (arg1[IMAG]>0)
zIm = Double.POSITIVE_INFINITY;
else if (arg1[IMAG]<0)
zIm = Double.NEGATIVE_INFINITY;
else
zIm = 0.0;
}
else if(Math.abs(x) >= Math.abs(y))
{
scalar = 1.0 / ( x + y*(y/x) );
zRe = scalar * (arg1[REAL] + arg1[IMAG]*(y/x));
zIm = scalar * (arg1[IMAG] - arg1[REAL]*(y/x));
}
else
{
scalar = 1.0 / ( x*(x/y) + y );
zRe = scalar * (arg1[REAL]*(x/y) + arg1[IMAG]);
zIm = scalar * (arg1[IMAG]*(x/y) - arg1[REAL]);
}
temp[REAL] = zRe;
temp[IMAG] = zIm;
return temp;
}
//////////////////////////////////////////SCALAR OPERATORS//////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
/**scalar multiply arg by this object for a number token
@arg = the value to multiply it by
@return the result as an OperandToken*/
public OperandToken scalarMultiply(OperandToken arg)
{
if(!(arg instanceof DoubleNumberToken))
Errors.throwMathLibException("DoubleNumberToken: scalar multiply: no number");
DoubleNumberToken nArg = ((DoubleNumberToken)arg);
if ( checkEqualDimensions(this.sizeA, nArg.sizeA) )
{
// scalar multiplication (n*m) = (n*m) .* (n*m)
ErrorLogger.debugLine("DoubleNumberToken: multiply (n*m) .* (n*m)");
DoubleNumberToken result = new DoubleNumberToken(sizeA, null, null);
for (int n=0; n<noElem; n++)
{
double[] argVal = nArg.getValueComplex(n);
result.setValueComplex(n, multiply(values[n], argVal) );
}
return result;
}
else if ( nArg.isScalar() )
{
// scalar multiplication (n*m) .* (1*1)
ErrorLogger.debugLine("DoubleNumberToken: multiply (n*m) .* (1*1)");
DoubleNumberToken result = new DoubleNumberToken(sizeA, null, null);
for (int n=0; n<noElem; n++)
{
double[] argVal = nArg.getValueComplex(0);
result.setValueComplex(n, multiply(values[n], argVal) );
}
return result;
}
else if ( this.isScalar() )
{
// scalar multiplication (1*1) .* (n*m)
ErrorLogger.debugLine("DoubleNumberToken: multiply (1*1) .* (n*m)");
DoubleNumberToken result = new DoubleNumberToken(nArg.sizeA, null, null);
for (int n=0; n<nArg.noElem; n++)
{
double[] argVal = nArg.getValueComplex(n);
result.setValueComplex(n, multiply(values[0], argVal) );
}
return result;
}
else
{
//dimensions do not match
Errors.throwMathLibException("DoubleNumberToken: scalar multiply: dimensions don't match");
return null;
}
} // end scalarMultiply
/**scalar divide arg by this object for a number token
@arg = the value to divide it by
@return the result as an OperandToken*/
public OperandToken scalarDivide(OperandToken arg)
{
if(!(arg instanceof DoubleNumberToken))
Errors.throwMathLibException("DoubleNumberToken: scalar divide: no number");
DoubleNumberToken nArg = ((DoubleNumberToken)arg);
double[][] argValues = nArg.getValuesRe();
double[][] argValuesImg = nArg.getValuesIm();
int argSizeX = nArg.getSizeX();
int argSizeY = nArg.getSizeY();
ErrorLogger.debugLine("DoubleNumberToken: scalarDivide. " +sizeY+" "+sizeX+" "+argSizeY+" "+argSizeX);
if ((sizeX == argSizeX) && (sizeY == argSizeY))
{
// divide multiplication (n*m) = (n*m) .* (n*m)
ErrorLogger.debugLine("DoubleNumberToken: scalar divide (n*m) .* (n*m)");
double[][][] results = new double[sizeY][sizeX][2];
for (int y=0; y<sizeY; y++)
{
for (int x=0; x<sizeX; x++)
{
int n = yx2n(y,x);
double[] argVal = nArg.getValueComplex(y, x);
results[y][x] = divide(values[n], argVal);
}
}
return new DoubleNumberToken(results);
}
else if ( nArg.isScalar() )
{
// divide multiplication (n*m) ./ (1,1)
ErrorLogger.debugLine("DoubleNumberToken: scalar divide (n*m) ./ (1*1)");
double[][][] results = new double[sizeY][sizeX][2];
for (int y=0; y<sizeY; y++)
{
for (int x=0; x<sizeX; x++)
{
int n = yx2n(y,x);
double[] argVal = nArg.getValueComplex(0, 0);
results[y][x] = divide(values[n], argVal);
}
}
return new DoubleNumberToken(results);
}
else if ( this.isScalar() )
{
// divide multiplication (n*m) ./ (1,1)
ErrorLogger.debugLine("DoubleNumberToken: scalar divide (1*1) ./ (n*m)");
double[][][] results = new double[argSizeY][argSizeX][2];
for (int y=0; y<argSizeY; y++)
{
for (int x=0; x<argSizeX; x++)
{
double[] val = getValueComplex(0, 0);
results[y][x] = divide(val, nArg.getValueComplex(y,x) );
}
}
return new DoubleNumberToken(results);
}
else
{
//dimensions do not match
Errors.throwMathLibException("DoubleNumberToken: scalar multiply: dimensions don't match");
return null;
}
} // end scalarDivide
/**left divide
@arg =
@return the result as an OperandToken*/
public OperandToken leftDivide(OperandToken arg)
{
if(!(arg instanceof DoubleNumberToken))
Errors.throwMathLibException("DoubleNumberToken: left divide: no number");
DoubleNumberToken nArg = ((DoubleNumberToken)arg);
DoubleNumberToken num = new DoubleNumberToken(nArg.getReValues());
//return num.divide(new DoubleNumberToken(values));
return num.divide(new DoubleNumberToken( this.getValuesRe(),this.getValuesIm() ));
} // end leftDivide
/**scalar left divide
@arg =
@return the result as an OperandToken*/
public OperandToken scalarLeftDivide(OperandToken arg)
{
if(!(arg instanceof DoubleNumberToken))
Errors.throwMathLibException("DoubleNumberToken: scalar left divide: no number");
DoubleNumberToken nArg = ((DoubleNumberToken)arg);
DoubleNumberToken num = new DoubleNumberToken(nArg.getReValues());
//return num.scalarDivide(new DoubleNumberToken(values));
return num.scalarDivide(new DoubleNumberToken(this.getValuesRe(),this.getValuesIm()));
} // end scalarLeftDivide
/**calculate the transpose of a matrix
@return the result as an OperandToken*/
public OperandToken transpose()
{
// transposed array
double[][] real = new double[sizeX][sizeY];
double[][] imag = new double[sizeX][sizeY];
// swap rows and columns
for (int y=0; y<sizeY; y++)
{
for (int x=0; x<sizeX; x++)
{
int n = yx2n(y,x);
// copy (y,x) -> (x,y), also change sign of imaginary part
real[x][y] = values[n][REAL];
imag[x][y] = values[n][IMAG] * (-1);
}
}
return new DoubleNumberToken(real, imag);
}
/**calculate the conjugate transpose of a matrix
@return the result as an OperandToken*/
public OperandToken ctranspose()
{
// transposed array
double[][] real = new double[sizeX][sizeY];
double[][] imag = new double[sizeX][sizeY];
// swap rows and columns
for (int y=0; y<sizeY; y++)
{
for (int x=0; x<sizeX; x++)
{
int n = yx2n(y,x);
// copy (y,x) -> (x,y), for conjugate transpose do not
// change sign of imaginary part
real[x][y] = values[n][REAL];
imag[x][y] = values[n][IMAG] ;
}
}
return new DoubleNumberToken(real, imag);
}
///////////////////////////////////////Standard Math Functions///////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////
/**standard function - returns the negative of the number*/
public OperandToken negate()
{
DoubleNumberToken result = new DoubleNumberToken(sizeA, null, null);
for(int n = 0; n < noElem; n++)
{
result.setValue(n, -getValueRe(n), -getValueIm(n) );
}
return result;
}
/**Standard functions - calculates the factorial of the number
@return the result as an OperandToken*/
public OperandToken factorial()
{
DoubleNumberToken result = new DoubleNumberToken(sizeA, null, null);
for(int n = 0; n < noElem; n++)
{
double amount = Math.rint(getValueRe(n));
result.setValue(n,
factorial(amount) ,
0 );
}
return result;
}
/**Calulates the factorial of a real value
@param amount = the number to calc the factorial of
@return the result as a double*/
public double factorial(double amount)
{
double answer = 1;
for(int count = 1; count <= amount; count++)
answer *= count;
return answer;
}
//////////////////////////////////////Complex Functions/////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////
/**Complex function - calculates the complex conjugate of the number*/
public OperandToken conjugate()
{
DoubleNumberToken result = new DoubleNumberToken(sizeA, null, null);
for(int n = 0; n < noElem; n++)
{
result.setValue(n,
getValueRe(n) ,
-getValueIm(n) );
}
return result;
}
//////////////////////////////////////Test Functions///////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////
/**Checks if this operand is zero
@return true if this number == 0 or that all values are
0 for a matrix*/
public boolean isNull()
{
for (int n=0; n<noElem; n++)
{
if( (getValueRe(n)!=0) || (getValueIm(n)!=0) )
return false;
}
return true;
}
/**@return true if this number token is a real matrix, without an imaginary part*/
public boolean isReal()
{
// if at least one element has an imaginary part the matrix is not real
for (int n=0; n<noElem; n++)
{
if (getValueIm(n) != 0)
return false;
}
return true;
}
/**@return true if this number token is an imaginary matrix, without a real part*/
public boolean isImaginary()
{
// if at least one element has a real part the matrix is not imaginary
for (int n=0; n<noElem; n++)
{
if (getValueIm(n) != 0)
return false;
}
return true;
}
/**@return true if this number token is a complex matrix with both real and imaginary parts*/
public boolean isComplex()
{
// if at least one element has a real value and also on element has
// an imaginary part the matrix is complex
boolean isRe = false;
boolean isIm = false;
for (int n=0; n<noElem; n++)
{
if (getValueRe(n) != 0)
isRe = true;
if (getValueIm(n)!= 0)
isIm = true;
}
// check if both real and imaginary tags are set
if (isRe && isIm)
return true;
else
return false;
}
} // end DoubleNumberToken
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -