📄 sparsenumbertoken.java
字号:
package jmathlib.core.tokens;
import jmathlib.core.interpreter.*;
import jmathlib.core.tokens.numbertokens.DoubleNumberToken;
import java.text.NumberFormat;
/**Class representing numbers used in expression
holds a 2D array of complex numers in a 3d array
values[y][x][REAL/IMAGINARY]
All operations on a DoubleNumberToken create a new DoubleNumberToken*/
public class SparseNumberToken extends DataToken
{
/**Complex values of the token*/
private double values[][][];
/**Constant value set to 1*/
public static final DoubleNumberToken one = new DoubleNumberToken(1);
/**Constant value set to 0*/
public static final DoubleNumberToken zero = new DoubleNumberToken(0);
/**Constant value set to 2*/
public static final DoubleNumberToken two = new DoubleNumberToken(2);
/**Constant value set to j*/
public static final DoubleNumberToken j = new DoubleNumberToken(0,1);
/**stores the number format for displaying the number*/
private static NumberFormat numFormat = NumberFormat.getInstance();
/**Index for real values within array*/
private static final int REAL = 0;
/**Index for Imaginary values within array*/
private static final int IMAGINARY = 1;
/** Constructor creating empty number token
*/
public SparseNumberToken()
{
super(5, "sparse");
sizeY = 0;
sizeX = 0;
values = null;
}
/** Constructor creating a scalar taking the numbers value as a string
* @param _value = the numbers value as a string
*/
public SparseNumberToken(String _value)
{
this(_value, "");
}
/** Constructor creating a scalar taking the numbers value as a double
* @param _value = the numbers value as a double
*/
public SparseNumberToken(double _value)
{
this(_value, 0);
}
/** Constructor taking the numbers value as a double[][]
* @param _value = the numbers value as a 2D array of double
*/
public SparseNumberToken(double[][] _values)
{
this(_values, null);
}
/**Constructor taking the numbers value as a string
* @param _real = the numbers real value as a string
* @param _imaginary = the numbers imaginary value as a string
*/
public SparseNumberToken(String _real, String _imaginary)
{
super(5, "sparse");
sizeX = 1;
sizeY = 1;
values = new double[1][1][2];
if (!_real.equals(""))
values[0][0][REAL] = new Double(_real).doubleValue();
if (!_imaginary.equals(""))
values[0][0][IMAGINARY] = new Double(_imaginary).doubleValue();
}
/**Constructor taking the numbers value as a pair of double
* values representing real and imaginary part
* @param _real = the numbers real value as a double
* @param _imaginary = the numbers imaginary value as a double
*/
public SparseNumberToken(double _real, double _imaginary)
{
super(5, "sparse");
sizeX = 1;
sizeY = 1;
values = new double[1][1][2];
values[0][0][REAL] = _real;
values[0][0][IMAGINARY] = _imaginary;
}
/**Constructor taking the numbers value as a pair of double
values representing real and imaginary part
@param _values = the values as a array containing the real and
imaginary values*/
public SparseNumberToken(double[] _values)
{
super(5, "sparse");
sizeX = 1;
sizeY = 1;
values = new double[1][1][2];
values[0][0][REAL] = _values[REAL];
values[0][0][IMAGINARY] = _values[IMAGINARY];
}
/**Constructor taking the numbers value as two double[][]
@param _real = the numbers value as a 2D array of double
@param _imaginary = the numbers value as a 2D array of double*/
public SparseNumberToken(double[][] _real, double[][] _imaginary)
{
super(5, "sparse");
sizeY = _real.length;
sizeX = _real[0].length;
values = new double[sizeY][sizeX][2];
for(int yy = 0; yy < sizeY; yy++)
{
for(int xx = 0; xx < sizeX; xx++)
{
values[yy][xx][REAL] = _real[yy][xx];
// imaginary number may be null
if (_imaginary != null)
values[yy][xx][IMAGINARY] = _imaginary[yy][xx];
else
values[yy][xx][IMAGINARY] = 0;
}
}
}
/**Constructor taking the numbers value as a double[][][]
@param _values = the numbers value as a 3D array of double*/
public SparseNumberToken(double[][][] _values)
{
super(5, "sparse");
sizeY = _values.length;
sizeX = _values[0].length;
values = new double[sizeY][sizeX][2];
for(int yy = 0; yy < sizeY; yy++)
{
for(int xx = 0; xx < sizeX; xx++)
{
values[yy][xx][REAL] = _values[yy][xx][REAL];
values[yy][xx][IMAGINARY] = _values[yy][xx][IMAGINARY];
}
}
}
/** return a new Number Token of size y*x
*
*/
public DataToken getElementSized(int y, int x)
{
return new DoubleNumberToken(new double[y][x][2]);
}
/** increase/decrease the size of the current DoubleNumberToken to size y*x
* @param dy number of rows
* @param dx number of columns
*/
public void setSize(int dy, int dx)
{
double[][][] newValues = new double[dy][dx][2];
ErrorLogger.debugLine("number "+dy+" "+dx);
ErrorLogger.debugLine("number "+sizeY+" "+sizeX);
for(int yy = 0; yy < sizeY; yy++)
{
for(int xx = 0; xx < sizeX; xx++)
{
ErrorLogger.debugLine("number "+yy+" "+xx);
newValues[yy][xx][REAL] = values[yy][xx][REAL];
newValues[yy][xx][IMAGINARY] = values[yy][xx][IMAGINARY];
}
}
values = newValues;
sizeY = dy;
sizeX = dx;
} // end setSize
/**@return the real value of the first number*/
public double getValue()
{
return values[0][0][REAL];
}
/**@return the real value of the first number*/
//public double getValueRe()
//{
// return values[0][0][REAL];
//}
/**@return the real value of the number at position y, x*/
public double getValueRe(int y, int x)
{
return values[y][x][REAL];
}
/**@return the real value of the first number as an integer*/
//public int getIntValue()
//{
// double temp = values[0][0][REAL];
// return (new Double(temp)).intValue();
//}
/**@return the real value of the number at position y, x as an integer*/
public int getIntValue(int y, int x)
{
double temp = values[x][y][REAL];
return (new Double(temp)).intValue();
}
/**@return the imaginary value of the first number*/
public double getValueIm()
{
return values[0][0][IMAGINARY];
}
/**@return the imaginary value of the number at position y, x*/
public double getValueIm(int y, int x)
{
return values[y][x][IMAGINARY];
}
/**@return the absolute value of the number at position y, x*/
public double getValueAbs(int y, int x)
{
double temp = Math.pow(values[y][x][REAL], 2) + Math.pow(values[y][x][IMAGINARY], 2);
return Math.sqrt(temp);
}
/**@return the angle of the number at position y, x in radians*/
public double getValueArg(int y, int x)
{
return Math.atan2(values[y][x][IMAGINARY], values[y][x][REAL]);
}
/**@return the value of the number (old notation)*/
public double[][] getValuesSSSSS()
{
return getReValues();
}
/**@return the real values of the number*/
public double[][] getReValues()
{
double[][] temp = new double[sizeY][sizeX];
if (sizeY==0 && sizeX==0)
return null;
for(int yy = 0; yy < sizeY; yy++)
{
for(int xx = 0; xx < sizeX; xx++)
{
temp[yy][xx] = values[yy][xx][REAL];
}
}
return temp;
}
/**@return the imaginary values of the number*/
public double[][] getImValues()
{
double[][] temp = new double[sizeY][sizeX];
if (sizeY==0 && sizeX==0)
return null;
for(int yy = 0; yy < sizeY; yy++)
{
for(int xx = 0; xx < sizeX; xx++)
{
temp[yy][xx] = values[yy][xx][IMAGINARY];
}
}
return temp;
}
/**@return the imaginary value of the number*/
public double[][] getValuesImg()
{
return getImValues();
}
/*public OperandToken getElement(int y, int x)
{
return new DoubleNumberToken(values[y][x]);
}*/
/*public OperandToken getElement(int n)
{
int x = (int) (n/sizeY); // column to start
int y = n - x*sizeY; // row to start
return new DoubleNumberToken(values[y][x]);
}*/
public void setElement(int y, int x, OperandToken num)
{
double real = ((DoubleNumberToken)num).getValueRe();
double imag = ((DoubleNumberToken)num).getValueIm();
ErrorLogger.debugLine("DoubleNumberToken("+y+","+x+")"+ real+" "+imag);
values[y][x][REAL] = real;
values[y][x][IMAGINARY] = imag;
}
public void setElement(int n, OperandToken num)
{
int x = (int)(n/sizeY); // column to start
int y = n - x*sizeY; // row to start
double real = ((DoubleNumberToken)num).getValueRe();
double imag = ((DoubleNumberToken)num).getValueIm();
ErrorLogger.debugLine("DoubleNumberToken("+y+","+x+")"+ real+" "+imag);
values[y][x][REAL] = real;
values[y][x][IMAGINARY] = imag;
}
/**@return an array of double representing the element at y,x
@param y = y position in matrix
@param x = x position in matrix*/
public double[] getValueComplex(int y, int x)
{
return values[y][x];
}
/**Set value at position y, x
@param y = y position in matrix
@param x = x position in matrix
@param _value = the value to set it to as an array of doubles
@param imag = imaginary value
*/
public void setValueComplex(int y, int x, double[] _value)
{
values[y][x][REAL] = _value[REAL];
values[y][x][IMAGINARY] = _value[IMAGINARY];
}
/**Set value at position y, x
@param y = y position in matrix
@param x = x position in matrix
@param real = real value
@param imag = imaginary value
*/
public void setValue(int y, int x, double _real, double _imag)
{
values[y][x][REAL] = _real;
values[y][x][IMAGINARY] = _imag;
}
/**return the number as a string*/
public String toString()
{
String result = null;
if((sizeY == 0) && (sizeX == 0))
{
result = "[]";
}
else if((sizeY == 1) && (sizeX == 1))
{
result = toString(values[0][0]);
}
else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -