📄 logicaltoken.java
字号:
package jmathlib.core.tokens;
import jmathlib.core.interpreter.ErrorLogger;
import jmathlib.core.interpreter.Errors;
import jmathlib.core.tokens.numbertokens.DoubleNumberToken;
public class LogicalToken extends DataToken
{
private boolean values[];
/**
* empty logical token
*
*/
public LogicalToken()
{
dataType = "logical";
sizeY = 0;
sizeX = 0;
sizeA = new int[]{0,0};
noElem = 0;
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 LogicalToken(String _real)
{
this(Boolean.getBoolean(_real));
}
/**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
*/
public LogicalToken(double _real)
{
super(5, "logical");
sizeX = 1;
sizeY = 1;
sizeA = new int[]{1, 1};
noElem = 1;
values = new boolean[1];
if (_real==0)
values[0]= true;
else
values[0]= true;
}
/**
*
* @param _value
*/
public LogicalToken(boolean _value)
{
super(5, "logical");
sizeX = 1;
sizeY = 1;
sizeA = new int[]{1, 1};
noElem = 1;
values = new boolean[1];
values[0] = _value;
}
/**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 LogicalToken(boolean[][] _values)
{
super(5, "logical");
sizeY = _values.length;
sizeX = _values[0].length;
sizeA = new int[]{sizeY, sizeX};
noElem = sizeY * sizeX;
values = new boolean[noElem];
for(int xx = 0; xx < sizeX; xx++)
{
for(int yy = 0; yy < sizeY; yy++)
{
values[xx*sizeY+yy] = _values[yy][xx];
}
}
}
/**
*
* @param _dy
* @param _dx
* @param _reValues
* @param _imValues
*/
public LogicalToken(int _dy, int _dx, boolean[] _values)
{
super(5, "logical");
sizeY = _dy;
sizeX = _dx;
sizeA = new int[]{sizeY, sizeX};
noElem = sizeY * sizeX;
values = new boolean[noElem];
if ((_values != null) &&
(noElem != _values.length) )
Errors.throwMathLibException("LogicalToken: dimension mismatch");
for(int ni = 0; ni< noElem; ni++)
{
if (_values != null)
values[ni] = _values[ni];
}
}
/**
* Constructor for multidimensional array
* @param _sizeA
* @param _reValues
*/
public LogicalToken(int[] _sizeA, boolean[] _values)
{
super(5, "logical");
sizeA = _sizeA;
if (sizeA.length<2)
Errors.throwMathLibException("LogicalToken: dimension too low <2");
sizeY = sizeA[0];
sizeX = sizeA[1];
// compute number of elements over all dimensions
noElem = 1;
for (int i=0; i<sizeA.length; i++)
{
noElem *= sizeA[i];
}
values = new boolean[noElem];
if ((_values != null) &&
(noElem != _values.length) )
Errors.throwMathLibException("LogicalToken: dimension mismatch");
for(int ni = 0; ni< noElem; ni++)
{
if (_values != null)
values[ni] = _values[ni];
}
}
/**
* Convert y,x points to element number n
* @param y
* @param x
* @return n
*/
protected int yx2n(int y, int x)
{
int n = x*sizeY + y;
return n;
}
/*protected int n2y(int n)
{
int x = (int) (n/sizeY); // column to start
int y = n - x*sizeY; // row to start
return y;
}*/
/*protected int n2x(int n)
{
int x = (int) (n/sizeY); // column to start
return x;
}*/
/**
* Convert from index to n (e.g. index={2,3,5} -> n)
* @param
* @return
*/
public int index2n(int[] index)
{
String s="";
for (int i=0; i<index.length; i++)
s += index[i]+" ";
ErrorLogger.debugLine("LogicalToken: index2n: index: "+s);
int dn = noElem;
int n = 0;
for (int i=index.length-1; i>0; i--)
{
dn = dn / sizeA[i];
n += dn * index[i];
}
n+= index[0];
return n;
}
/**
*
*/
public DataToken getElementSized(int y, int x)
{
return new LogicalToken(y, x, new boolean[y*x]);
}
/** 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)
{
boolean[] newValues = new boolean[dy*dx];
ErrorLogger.debugLine("boolean "+dy+" "+dx);
ErrorLogger.debugLine("boolean "+sizeY+" "+sizeX);
// new array must be bigger than original value, otherwise values will be
// lost after copying into the new array
if ((dy<sizeY) || (dx<sizeX))
Errors.throwMathLibException("LogicalToken: setSize: loosing values");
for(int yy = 0; yy < sizeY; yy++)
{
for(int xx = 0; xx < sizeX; xx++)
{
int n = yx2n(yy,xx);
ErrorLogger.debugLine("boolean "+yy+" "+xx);
newValues[xx*dy + yy] = values[n];
}
}
values = newValues;
sizeY = dy;
sizeX = dx;
sizeA = new int[]{sizeY, sizeX};
noElem = sizeY * sizeX;
} // end setSize
/**
*
* @param y
* @param x
* @return
*/
public OperandToken getElement(int y, int x)
{
int n = yx2n(y,x);
return getElement(n);
}
/**
*
* @param n
* @return
*/
public OperandToken getElement(int n)
{
return new LogicalToken(values[n]);
}
/**
*
* @param y
* @param x
* @param num
*/
public void setElement(int y, int x, OperandToken num)
{
int n = yx2n(y,x);
setElement(n, num);
}
/**
*
* @param n
* @param num
*/
public void setElement(int n, OperandToken num)
{
values[n] = ((LogicalToken)num).getValue(n);
}
/**
*
* @param n
* @return
*/
public boolean getValue(int n)
{
return values[n];
}
/**
*
* @return
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -