Class JSCNArray
- java.lang.Object
-
- com.jstatcom.model.AbstractJSCData
-
- com.jstatcom.model.JSCNArray
-
- All Implemented Interfaces:
- JSCData
public final class JSCNArray extends AbstractJSCData
A wrapper for 2-dimensional number arrays that conforms to theJSCDatainterface. Number arrays are stored asdouble[][]. It provides some convenience methods for matrix transformation, but it is not intended to serve as a general purpose matrix class. Instead it provides an adapter to set and get instances of theJama.Matrixclass, which is probably the de facto standard matrix class for JAVA .A
JSCNArrayis empty, if the stored array isnullor has either zero rows or columns. All rows of array values must have the same length.All array arguments are copied before they are set as values. The original arrays are untouched. In a similar way all
getmethods return either a copy of a mutable object (likedouble[][]) or a reference to an immutable object.Whenever the value of this data object changes, registered
JSCDataListenersare notified via aJSCDataEvent. If not otherwise stated, the event objects always contain the values before the change and after the change.This class is thread-safe. All access to mutable data is synchronized. All events are fired outside the synchronized blocks.
-
-
Field Summary
Fields Modifier and Type Field and Description static XmlFormat<JSCNArray>JSCNArray_XMLXML format field for (de)serialization.
-
Constructor Summary
Constructors Constructor and Description JSCNArray(JSCNArray narray)Copy-Constructor.JSCNArray(java.lang.String name)Creates an emptyJSCNArray.JSCNArray(java.lang.String name, double... value)Creates aJSCNArrayfromvalue.JSCNArray(java.lang.String name, double[][] value)Creates aJSCNArrayfromvalue.JSCNArray(java.lang.String name, int[] value)Creates aJSCNArrayfromvalue.JSCNArray(java.lang.String name, int[][] value)Creates aJSCNArrayfromvalue.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method and Description voidappendCols(JSCNArray newCols)AppendsnewColsto the right end of the value array.voidappendRows(JSCNArray newRows)AppendsnewRowsto the bottom the value array.voidclear()Clearsthisdata object.intcols()Gets the number of columns in the stored array.JSCNArraycopy()Gets a copy of the current object.voiddelCol(int i)Deletes columnifrom the value array.voiddelColsIf(int... index)Deletes all columns ofthisthat have a corresponding nonzero element inindex.voiddelRow(int i)Deletes rowifrom the value array.voiddelRowsIf(int... index)Deletes all rows ofthisthat have a corresponding nonzero element inindex.java.lang.Stringdisplay()Gets a string representing thedouble[][]stored.double[][]doubleArray()Gets a copy of the underlying value as adouble[][].doubledoubleAt(int i, int j)Gets thedoubleelement stored at index i, j.java.lang.DoubledoubleValAt(int i, int j)Gets theDoubleelement stored at index i, j.double[]getCol(int colIndex)Gets the column of the underlying double array specified withcolIndex.double[][]getCols(int startCol, int endCol)Gets all columns of the underlying double array defined bystartCol:endCol.double[][]getPow(double pow)Gets a new double array with the elements raised to the power ofpow.double[]getRow(int rowIndex)Gets the row of the underlying double array specified withrowIndex.double[][]getRows(int startRow, int endRow)Gets all rows of the underlying double array defined bystartRow:endRow.double[][]getTransposed()Gets a new double array with elements transposed.int[][]intArray()Gets a copy of the underlying value as aint[][].intintAt(int i, int j)Gets theintelement stored at index i, j.booleanisEmpty()Gets whetherthisis empty.booleanisEqual(JSCData o)Gets whether the values of this data object andotherare logically equal.booleanisNaN(int i, int j)Gets whether the element stored at index i, j is of typeDouble.NaN.MatrixjamaMatrix()Gets the values stored in thisJSCNArrayas an instance ofJama.Matrix.java.lang.Stringname()Gets the name of the data object.intnanCount()Gets the number ofdoubleelements that are equal toDouble.NaN.intrank()Gets the rank of the stored value.introws()Gets the number of rows in the stored array.double[][]selColsIf(int... colIndex)Gets all columns of the underlying double array that have a nonzero corresponding element incolIndex.double[][]selRowsIf(int... rowIndex)Gets all rows of the underlying double array that have a nonzero corresponding element inrowIndex.voidsetFormatString(java.lang.String newFormatString)Sets a new format string.voidsetVal(double[] newValue)Sets the value of this tonewValue, seesetVal(double[][] newValue).voidsetVal(double[][] newValue)Sets the value of this tonewValue.voidsetVal(int[] newValue)Sets the value of this tonewValue, seesetVal(double[][] newValue).voidsetVal(int[][] newValue)Sets the value of this tonewValue, seesetVal(double[][] newValue).voidsetVal(JSCNArray val)Sets the values stored invalto thisJSCNArray.voidsetVal(Matrix mat)Sets the values stored inmatto thisJSCNArray.voidsetValAt(double val, int i, int j)Sets thedoubleelement stored at index i, j.java.lang.StringtoString()Gets a string representation of thisJSCNArrayinstance.voidtranspose()Transposes the underlying array.JSCTypestype()Gets the type of thisJSCDataobject.java.lang.Objectvalue()Gets a copy of the underlying value, which is stored as adouble[][].static JSCNArrayvalueOf(java.io.File file, java.lang.String name)Creates an instance of this class from a data file.double[]vec()Gets a vector stacked with the columns of the underlying data array.intzeroCount()Gets the number ofdoubleelements that are equal to zero.-
Methods inherited from class com.jstatcom.model.AbstractJSCData
addJSCDataListener, getJSCProperty, removeJSCDataListener, removeJSCProperty, setJSCProperty
-
-
-
-
Constructor Detail
-
JSCNArray
public JSCNArray(java.lang.String name)
Creates an emptyJSCNArray.- Parameters:
name- the identifier of that object- Throws:
java.lang.IllegalArgumentException- ifnameis not valid orif (name == null)- See Also:
JSCConstants.isValidName(java.lang.String)
-
JSCNArray
public JSCNArray(java.lang.String name, double[][] value)Creates aJSCNArrayfromvalue. The elements ofvalueare copied before.- Parameters:
name- the identifier of that objectvalue- thedouble[][]to be copied and used as value- Throws:
java.lang.IllegalArgumentException- ifnameis not valid
or if rows ofvaluehave different lengths orif (name == null)- See Also:
JSCConstants.isValidName(java.lang.String)
-
JSCNArray
public JSCNArray(JSCNArray narray)
Copy-Constructor.- Parameters:
narray- the number array to copy- Throws:
java.lang.IllegalArgumentException-if (narray == null)- See Also:
JSCConstants.isValidName(java.lang.String)
-
JSCNArray
public JSCNArray(java.lang.String name, double... value)Creates aJSCNArrayfromvalue. The elements ofvalueare copied before.- Parameters:
name- the identifier of that objectvalue- thedouble[]to be copied and used as value- Throws:
java.lang.IllegalArgumentException- ifnameis not valid orif (name == null)- See Also:
JSCConstants.isValidName(java.lang.String)
-
JSCNArray
public JSCNArray(java.lang.String name, int[] value)Creates aJSCNArrayfromvalue. The elements ofvalueare copied before.- Parameters:
name- the identifier of that objectvalue- theint[]to be copied and used as value- Throws:
java.lang.IllegalArgumentException- ifnameis not valid orif (name == null)- See Also:
JSCConstants.isValidName(java.lang.String)
-
JSCNArray
public JSCNArray(java.lang.String name, int[][] value)Creates aJSCNArrayfromvalue. The elements ofvalueare copied before.- Parameters:
name- the identifier of that objectvalue- theint[][]to be copied and used as value- Throws:
java.lang.IllegalArgumentException- ifnameis not valid
or if rows ofvaluehave different lengths orif (name == null)- See Also:
JSCConstants.isValidName(java.lang.String)
-
-
Method Detail
-
appendCols
public void appendCols(JSCNArray newCols)
AppendsnewColsto the right end of the value array. Fires aJSCDataEventof type:JSCDataEventTypes.VALUE_CHANGEDifnewColsis not emptyJSCDataEventTypes.EMPTY_STATEifnewColsis not empty and this was empty before
newColsis empty, nothing happens.- Parameters:
newCols- theJSCNArrayto be appended- Throws:
java.lang.IllegalArgumentException-if (newCols == null)orif (rows() > 0 && newCols.rows() > 0 && rows() != newCols.rows())
-
appendRows
public void appendRows(JSCNArray newRows)
AppendsnewRowsto the bottom the value array. Fires aJSCDataEventof type:JSCDataEventTypes.VALUE_CHANGEDifnewRowsis not emptyJSCDataEventTypes.EMPTY_STATEifnewRowsis not empty and this was empty before
newRowsis empty, nothing happens.- Parameters:
newRows- theJSCNArrayto be appended- Throws:
java.lang.IllegalArgumentException-if (newRows == null)orif (cols() > 0 && newRows.cols() > 0 && cols() != newRows.cols())
-
clear
public void clear()
Clearsthisdata object. Fires:JSCDataEventTypes.EMPTY_STATEeventJSCDataEventTypes.VALUE_CHANGEDevent
isEmptyreturnstrue.
-
cols
public int cols()
Gets the number of columns in the stored array.- Returns:
- the number of columns or 0
if (isEmpty())
-
copy
public JSCNArray copy()
Description copied from interface:JSCDataGets a copy of the current object. Implementations of this method must fulfil the two conditions:a.isEqual(a.copy())- compares valuesa.getName().equals(a.copy().getName())- compares name
JSCDataListenersattached to the original object should not be copied.- Returns:
- a clonelike copy of the original object
- See Also:
JSCData
-
delCol
public void delCol(int i)
Deletes columnifrom the value array. Fires aJSCDataEventof type:JSCDataEventTypes.VALUE_CHANGEDin any caseJSCDataEventTypes.EMPTY_STATEifthisis emtpy afterwards
- Parameters:
i- the index of the column to remove- Throws:
java.lang.IllegalStateException- ifthisis emptyjava.lang.IllegalArgumentException- if column index is invalid
-
delColsIf
public void delColsIf(int... index)
Deletes all columns ofthisthat have a corresponding nonzero element inindex. Ifindexcontains only zeros, nothing happens. Ifindexselects all columns,thisis cleared.This method fires a
JSCDataEventof type:JSCDataEventTypes.VALUE_CHANGEDif some column was deletedJSCDataEventTypes.EMPTY_STATEifthisis emtpy afterwards
- Parameters:
index- thecols() x 1 intarray, nonzero entries select columns- Throws:
java.lang.IllegalStateException- ifthisis emptyjava.lang.IllegalArgumentException-if (index.length != cols())if (index == null)
-
delRow
public void delRow(int i)
Deletes rowifrom the value array. Fires aJSCDataEventof type:JSCDataEventTypes.VALUE_CHANGEDin any caseJSCDataEventTypes.EMPTY_STATEifthisis emtpy afterwards
- Parameters:
i- the index of the row to remove- Throws:
java.lang.IllegalStateException- ifthisis emptyjava.lang.IllegalArgumentException- if row index is invalid
-
delRowsIf
public void delRowsIf(int... index)
Deletes all rows ofthisthat have a corresponding nonzero element inindex. Ifindexcontains only zeros, nothing happens. Ifindexselects all rows,thisis cleared.This method fires a
JSCDataEventof type:JSCDataEventTypes.VALUE_CHANGEDif some row was deletedJSCDataEventTypes.EMPTY_STATEifthisis emtpy afterwards
- Parameters:
index- therows() x 1 intarray, nonzero entries select rows- Throws:
java.lang.IllegalStateException- ifthisis emptyjava.lang.IllegalArgumentException-if (index.length != rows()orif (index == null)
-
display
public java.lang.String display()
Gets a string representing thedouble[][]stored.- Specified by:
displayin interfaceJSCData- Overrides:
displayin classAbstractJSCData- Returns:
- "{}" if
isEmpty == true, otherwise a formatted string with all elements in table form, the format parameters can be changed viasetFormatString
-
doubleArray
public double[][] doubleArray()
Gets a copy of the underlying value as adouble[][].- Returns:
- the double array
ifthisis emptynew double[0][0]is returned
-
doubleAt
public double doubleAt(int i, int j)Gets thedoubleelement stored at index i, j.- Parameters:
i- the row index, zero basedj- the column index, zero based- Returns:
- the
doublestored at this array position - Throws:
java.lang.IllegalArgumentException- if indices out of range
-
doubleValAt
public java.lang.Double doubleValAt(int i, int j)Gets theDoubleelement stored at index i, j.- Parameters:
i- the row index, zero basedj- the column index, zero based- Returns:
- the
Doublestored at this array position - Throws:
java.lang.IllegalArgumentException- if indices out of range
-
intArray
public int[][] intArray()
Gets a copy of the underlying value as aint[][]. The following specialintvalues for elementi, jare returned:- 0 if
value[i][j] == Double.NaN -
Integer.MAX_VALUE if value[i][j] == Double.POSITIVE_INFINITY -
Integer.MIN_VALUE if value[i][j] == Double.NEGATIVE_INFINITY
- Returns:
- the double array
ifthisis empty, then eithernullornew int[0][0]is returned, depending on what has been set before
- 0 if
-
intAt
public int intAt(int i, int j)Gets theintelement stored at index i, j. Note that during this operation information is potentially lost. Only the integer part is preserved, which is different from rounding. The following specialintvalues are returned:- 0 if
value == Double.NaN Integer.MAX_VALUE if value == Double.POSITIVE_INFINITYInteger.MIN_VALUE if value == Double.NEGATIVE_INFINITY
- Parameters:
i- the row index, zero basedj- the column index, zero based- Returns:
- the
intstored at this array position - Throws:
java.lang.IllegalArgumentException- if indices out of range
- 0 if
-
isEmpty
public boolean isEmpty()
Gets whetherthisis empty. This method returnstrueif one of the following conditions is met:value() == nullrows() == 0cols() == 0
- Returns:
trueif empty
-
isEqual
public boolean isEqual(JSCData o)
Description copied from interface:JSCDataGets whether the values of this data object andotherare logically equal.- Returns:
trueif considered equal according to the logic of the representation- See Also:
JSCData
-
isNaN
public boolean isNaN(int i, int j)Gets whether the element stored at index i, j is of typeDouble.NaN.- Parameters:
i- the row indexj- the column index- Returns:
trueif the array element is a missing value- Throws:
java.lang.IllegalArgumentException- if indices out of range
-
jamaMatrix
public Matrix jamaMatrix()
Gets the values stored in thisJSCNArrayas an instance ofJama.Matrix. The underlying array is copied before.- Returns:
- instance of
Jama.Matrixinitialized with the stored values - Throws:
java.lang.IllegalStateException- ifthisis empty- See Also:
Matrix
-
name
public java.lang.String name()
Description copied from interface:JSCDataGets the name of the data object. It must fulfil the requirements for variable names defined inJSCConstants.- Returns:
- the name
- See Also:
JSCData
-
nanCount
public int nanCount()
Gets the number ofdoubleelements that are equal toDouble.NaN.- Returns:
- the number of missing values
-
rank
public int rank()
Gets the rank of the stored value. The computation is delegated to aJama.Matrixinstance.- Returns:
- the rank of the
double[][]value - Throws:
java.lang.IllegalStateException- ifthisis empty
-
rows
public int rows()
Gets the number of rows in the stored array.- Returns:
- the number of rows or 0
if (isEmpty())
-
setFormatString
public void setFormatString(java.lang.String newFormatString)
Sets a new format string. This string is used by thedisplaymethod to format the double array. The default format string is"%- 8.4f ".For a complete description of all legal arguments, see the
PrintfFormatdocumentation. The string must contain exactly one conversion character.- Parameters:
newFormatString- the format string to be used fordisplay- Throws:
java.lang.IllegalArgumentException- ifnewFormatStringis not valid- See Also:
PrintfFormat
-
setVal
public void setVal(double[][] newValue)
Sets the value of this tonewValue. Fires aJSCDataEventof type:JSCDataEventTypes.VALUE_CHANGEDifnewValueis different from current valueJSCDataEventTypes.EMPTY_STATEifnewValueis not empty and this was empty beforeJSCDataEventTypes.EMPTY_STATEifnewValueis empty and this was not empty before
nullor at least one dimension is zero. ThenewValueis only compared to the existing value if a listener toJSCDataEventTypes.VALUE_CHANGEDis registered.- Parameters:
newValue- the new value- Throws:
java.lang.IllegalArgumentException- if rows ofnewValuehave different lengths
-
setVal
public void setVal(int[][] newValue)
Sets the value of this tonewValue, seesetVal(double[][] newValue).- Parameters:
newValue- the new value- Throws:
java.lang.IllegalArgumentException- if rows ofnewValuehave different lengths
-
setVal
public void setVal(double[] newValue)
Sets the value of this tonewValue, seesetVal(double[][] newValue).- Parameters:
newValue- the new value
-
setVal
public void setVal(int[] newValue)
Sets the value of this tonewValue, seesetVal(double[][] newValue).- Parameters:
newValue- the new value
-
setVal
public void setVal(JSCNArray val)
Sets the values stored invalto thisJSCNArray. Future changes invalhave no effect onthis.- Parameters:
val- instance ofJSCNArray- Throws:
java.lang.IllegalArgumentException-if (val == null)
-
setVal
public void setVal(Matrix mat)
Sets the values stored inmatto thisJSCNArray. The underlying array is copied before.- Parameters:
mat- instance ofJama.Matrix- See Also:
Matrix
-
setValAt
public void setValAt(double val, int i, int j)Sets thedoubleelement stored at index i, j. Fires aJSCDataEventof typeJSCDataEventTypes.VALUE_CHANGEDif the new value is different from the one currently stored at that position. The event object contains the following values:oldValue-new double[] { oldNumber, i, j }newValue-new double[] { newNumber, i, j }
- Parameters:
val- thedoubleto store at the given positioni- the row index, zero basedj- the column index, zero based- Throws:
java.lang.IllegalArgumentException- if indices out of range
-
toString
public java.lang.String toString()
Gets a string representation of thisJSCNArrayinstance. The value is only represented with a summary string, because the underlying array might be large. To print the complete value, usedisplayinstead.- Overrides:
toStringin classAbstractJSCData- Returns:
- string with name, type and value information
-
transpose
public void transpose()
Transposes the underlying array. Nothing happens ifthisis empty. Fires aJSCDataEventof typeJSCDataEventTypes.VALUE_CHANGEDif the stored array is not symmetric.
-
type
public JSCTypes type()
Gets the type of thisJSCDataobject.- Returns:
JSCTypes.NARRAY- See Also:
JSCTypes
-
value
public java.lang.Object value()
Gets a copy of the underlying value, which is stored as adouble[][].- Returns:
- the value object
ifthisis emptynew double[0][0]is returned
-
zeroCount
public int zeroCount()
Gets the number ofdoubleelements that are equal to zero.- Returns:
- the number of zero elements
-
valueOf
public static JSCNArray valueOf(java.io.File file, java.lang.String name)
Creates an instance of this class from a data file. The method parses the elementsfileinto a double array according to the following conventions:- the number of rows of the array is the number of nonempty rows in
file - the number of cols of the array is the number of tokens of the row with the most tokens
- entries are parsed to numbers, exponential notation is allowed, all
entries that cannot be parsed to a number are set to
Double.NaN
fileis empty, an emptyJSCNArraywill be returned.- Parameters:
file- the file to parse the double array fromname- the name of the newJSCNArrayto create- Returns:
- an instance of
JSCNArray - Throws:
java.lang.IllegalArgumentException-if (file == null || name == null), or ifnameis not legal orif (!file.isFile())
- the number of rows of the array is the number of nonempty rows in
-
getCol
public double[] getCol(int colIndex)
Gets the column of the underlying double array specified withcolIndex. The index starts with 0 as usual.- Parameters:
colIndex- the index of the column to extract (starting from 0)- Returns:
- a one dimensional double array with the column data
- Throws:
java.lang.IllegalStateException- ifthisis emptyjava.lang.IllegalArgumentException-colIndexis outside of valid array bounds
-
vec
public double[] vec()
Gets a vector stacked with the columns of the underlying data array.- Returns:
- a one dimensional double array with all values starting with the first column
-
getRow
public double[] getRow(int rowIndex)
Gets the row of the underlying double array specified withrowIndex. The index starts with 0 as usual.- Parameters:
rowIndex- the index of the row to extract (starting from 0)- Returns:
- a one dimensional double array with the row data
- Throws:
java.lang.IllegalStateException- ifthisis emptyjava.lang.IllegalArgumentException-rowIndexis outside of valid array bounds
-
getCols
public double[][] getCols(int startCol, int endCol)Gets all columns of the underlying double array defined bystartCol:endCol. The indices start with 0 as usual.- Parameters:
startCol- the index of the first column to extract (starting from 0)endCol- the index of the last column to extract (starting from 0)- Returns:
- a two dimensional double array with the column data
- Throws:
java.lang.IllegalStateException- ifthisis emptyjava.lang.IllegalArgumentException-if (startCol > endCol)or if the indices are outside the valid array bounds
-
getRows
public double[][] getRows(int startRow, int endRow)Gets all rows of the underlying double array defined bystartRow:endRow. The indices start with 0 as usual.- Parameters:
startRow- the index of the first row to extract (starting from 0)endRow- the index of the last row to extract (starting from 0)- Returns:
- a two dimensional double array with the row data
- Throws:
java.lang.IllegalStateException- ifthisis emptyjava.lang.IllegalArgumentException-if (startRow > endRow)or if the indices are outside the valid array bounds
-
selColsIf
public double[][] selColsIf(int... colIndex)
Gets all columns of the underlying double array that have a nonzero corresponding element incolIndex.- Parameters:
colIndex-this.cols() x 1vector selecting columns- Returns:
- new array with the extracted columns,
new double[0][0]if index contains only 0's, a copy of the underlying values ifcolIndexcontains only nonzero elements - Throws:
java.lang.IllegalArgumentException-if (colIndex == null)orif (this.isEmpty() && colIndex > 0)orif (this.cols() != colIndex)
-
selRowsIf
public double[][] selRowsIf(int... rowIndex)
Gets all rows of the underlying double array that have a nonzero corresponding element inrowIndex.- Parameters:
rowIndex-this.rows() x 1vector selecting rows- Returns:
- new array with the extracted rows,
new double[0][0]if index contains only 0's, a copy of the underlying values ifrowIndexcontains only nonzero elements - Throws:
java.lang.IllegalArgumentException-if (rowIndex == null)orif (this.isEmpty() && rowIndex > 0)orif (this.rows() != rowIndex)
-
getTransposed
public double[][] getTransposed()
Gets a new double array with elements transposed.- Returns:
- transposed data
-
getPow
public double[][] getPow(double pow)
Gets a new double array with the elements raised to the power ofpow.- Parameters:
pow- the power to raise each element of the data array to- Returns:
- double array with changed elements
-
-
DMelt 3.0 © DataMelt by jWork.ORG