Package ucar.ma2
Class MAMath
java.lang.Object
ucar.ma2.MAMath
Element by element algebra on Arrays
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classHolds a minimum and maximum value.static classHolds a scale and offset. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic ArrayAdd elements of two arrays together, allocating the result array.static voidAdd elements of two arrays together as doubles, place sum in the result array.static MAMath.ScaleOffsetcalcScaleOffsetSkipMissingData(Array a, double missingValue, int nbits) Calculate the scale/offset for an array of numbers.static booleanconformable(int[] shapeA, int[] shapeB) Check that two array shapes are conformable.static booleanconformable(Array a, Array b) Check that two arrays are conformable.static ArrayConvert original array to desired typestatic Arrayconvert2packed(Array unpacked, double missingValue, int nbits, DataType packedType) static Arrayconvert2Unpacked(Array packed, MAMath.ScaleOffset scaleOffset) static voidCopy array a to array result, the result array will be in canonical order The operation type is taken from the type of a.static voidcopy(DataType dataType, IndexIterator from, IndexIterator to) Copy using iterators.static voidcopyBoolean(Array result, Array a) copy array a to array result as bytes The array a and result must be type booleanstatic voidcopy array a to array result as bytes The values from the array a are converted to byte (if needed), and then converted to the type of result (if needed).static voidcopy array a to array result as char The values from the array a are converted to char (if needed), and then converted to the type of result (if needed).static voidcopyDouble(Array result, Array a) copy array a to array result as doubles The values from the arrays a are converted to double (if needed), and then converted to the type of result (if needed).static voidcopy array a to array result as floats The values from the arrays a are converted to float (if needed), and then converted to the type of result (if needed).static voidcopy array a to array result as integers The values from the arrays a are converted to integer (if needed), and then converted to the type of result (if needed).static voidcopy array a to array result as longs The values from the array a are converted to long (if needed), and then converted to the type of result (if needed).static voidcopyObject(Array result, Array a) copy array a to array result as an Object The array a and result must be type objectstatic voidcopy array a to array result as shorts The values from the array a are converted to short (if needed), and then converted to the type of result (if needed).static booleanReturns true if the specified arrays have the same data type, shape, and equal corresponding elements.static doublegetMaximum(Array a) static doublegetMaximumSkipMissingData(Array a, double missingValue) static doublegetMinimum(Array a) static doublegetMinimumSkipMissingData(Array a, double missingValue) static MAMath.MinMaxFind min and max value in this array, getting values as doubles.static MAMath.MinMaxgetMinMaxSkipMissingData(Array a, double missingValue) static MAMath.MinMaxstatic intAn implementation ofObject.hashCode()that is consistent withequals(Array, Array).static booleannearlyEquals(Array data1, Array data2) Returns true if the specified arrays have the same size, signedness, and approximately equal corresponding elements.static intreducedRank(int[] shape) Calculate the reduced rank of this shape, by subtracting dimensions with length 1static voidSet all the elements of this array to the given double value.static doublesum all of the elements of array a as doubles.static doublesumDoubleSkipMissingData(Array a, double missingValue) sum all of the elements of array a as doubles.
-
Constructor Details
-
MAMath
public MAMath()
-
-
Method Details
-
add
Add elements of two arrays together, allocating the result array. The result type and the operation type are taken from the type of a.- Parameters:
a- add values from hereb- add values from here- Returns:
- result = a + b
- Throws:
IllegalArgumentException- a and b are not conformableUnsupportedOperationException- dont support this data type yet
-
addDouble
Add elements of two arrays together as doubles, place sum in the result array. The values from the arrays a and b are converted to double (if needed), and the sum is converted to the type of result (if needed).- Parameters:
result- result arraya- operandb- operand- Throws:
IllegalArgumentException- a,b,and result are not conformable
-
conformable
Check that two arrays are conformable.- Parameters:
a- operandb- operand- Returns:
- true if conformable
-
conformable
public static boolean conformable(int[] shapeA, int[] shapeB) Check that two array shapes are conformable. The shapes must match exactly, except that dimensions of length 1 are ignored.- Parameters:
shapeA- shape of array 1shapeB- shape of array 2- Returns:
- true if conformable
-
convert
Convert original array to desired type- Parameters:
org- original arraywantType- desired type- Returns:
- converted data of desired type, or original array if it is already
-
copy
public static void copy(DataType dataType, IndexIterator from, IndexIterator to) throws IllegalArgumentException Copy using iterators. Will copy until !from.hasNext().- Parameters:
dataType- use this operation type (eg DataType.DOUBLE uses getDoubleNext())from- copy from hereto- copy to here- Throws:
IllegalArgumentException- a and b are not conformableUnsupportedOperationException- dont support this data type
-
copy
Copy array a to array result, the result array will be in canonical order The operation type is taken from the type of a.- Parameters:
result- copy to herea- copy from here- Throws:
IllegalArgumentException- a and b are not conformableUnsupportedOperationException- dont support this data type yet
-
copyDouble
copy array a to array result as doubles The values from the arrays a are converted to double (if needed), and then converted to the type of result (if needed).- Parameters:
result- copy to herea- copy from here- Throws:
IllegalArgumentException- a and result are not conformable
-
copyFloat
copy array a to array result as floats The values from the arrays a are converted to float (if needed), and then converted to the type of result (if needed).- Parameters:
result- copy to herea- copy from here- Throws:
IllegalArgumentException- a and result are not conformable
-
copyLong
copy array a to array result as longs The values from the array a are converted to long (if needed), and then converted to the type of result (if needed).- Parameters:
result- copy to herea- copy from here- Throws:
IllegalArgumentException- a and result are not conformable
-
copyInt
copy array a to array result as integers The values from the arrays a are converted to integer (if needed), and then converted to the type of result (if needed).- Parameters:
result- copy to herea- copy from here- Throws:
IllegalArgumentException- a and result are not conformable
-
copyShort
copy array a to array result as shorts The values from the array a are converted to short (if needed), and then converted to the type of result (if needed).- Parameters:
result- copy to herea- copy from here- Throws:
IllegalArgumentException- a and result are not conformable
-
copyChar
copy array a to array result as char The values from the array a are converted to char (if needed), and then converted to the type of result (if needed).- Parameters:
result- copy to herea- copy from here- Throws:
IllegalArgumentException- a and result are not conformable
-
copyByte
copy array a to array result as bytes The values from the array a are converted to byte (if needed), and then converted to the type of result (if needed).- Parameters:
result- copy to herea- copy from here- Throws:
IllegalArgumentException- a and result are not conformable
-
copyBoolean
copy array a to array result as bytes The array a and result must be type boolean- Parameters:
result- copy to herea- copy from here- Throws:
IllegalArgumentException- a and result are not conformable
-
copyObject
copy array a to array result as an Object The array a and result must be type object- Parameters:
result- copy to herea- copy from here- Throws:
IllegalArgumentException- a and result are not conformable
-
reducedRank
public static int reducedRank(int[] shape) Calculate the reduced rank of this shape, by subtracting dimensions with length 1- Parameters:
shape- shape of the array- Returns:
- rank without dimensions of length 1
-
getMinimum
-
getMaximum
-
getMinMax
Find min and max value in this array, getting values as doubles. Skip Double.NaN.- Parameters:
a- the array.- Returns:
- MinMax
-
getMinMaxSkipMissingData
-
getMinimumSkipMissingData
-
getMaximumSkipMissingData
-
getMinMaxSkipMissingData
-
setDouble
Set all the elements of this array to the given double value. The value is converted to the element type of the array, if needed.- Parameters:
result- change this Arrayval- set all elements to this value
-
sumDouble
sum all of the elements of array a as doubles. The values from the array a are converted to double (if needed).- Parameters:
a- read values from this Array- Returns:
- sum of elements
-
sumDoubleSkipMissingData
sum all of the elements of array a as doubles. The values from the array a are converted to double (if needed).- Parameters:
a- read values from this ArraymissingValue- skip values equal to this, or which are NaNs- Returns:
- sum of elements
-
calcScaleOffsetSkipMissingData
public static MAMath.ScaleOffset calcScaleOffsetSkipMissingData(Array a, double missingValue, int nbits) Calculate the scale/offset for an array of numbers.If signed: then max value unpacked = 2^(n-1) - 1 packed min value unpacked = -(2^(n-1) - 1) packed note that -2^(n-1) is unused, and a good place to map missing values by solving 2 eq in 2 unknowns, we get: scale = (max - min) / (2^n - 2) offset = (max + min) / 2 If unsigned then max value unpacked = 2^n - 1 packed min value unpacked = 0 packed and: scale = (max - min) / (2^n - 1) offset = min One could modify this to allow a holder for missing values.- Parameters:
a- array to convert (not changed)missingValue- skip thesenbits- map into this many bits- Returns:
- ScaleOffset, calculated as above.
-
convert2packed
-
convert2Unpacked
-
nearlyEquals
Returns true if the specified arrays have the same size, signedness, and approximately equal corresponding elements.floatelements must be withinMisc.defaultMaxRelativeDiffFloatof each other, as determined byMisc.nearlyEquals(double, double, double). Similarly,doubleelements must be withinMisc.defaultMaxRelativeDiffDoubleof each other.equals(Array, Array)is an alternative to this method that requires that corresponding elements be exactly equal. It is suitable for use inObject.equals(java.lang.Object)implementations, whereas this method isn't.- Parameters:
data1- one array to be tested for equality.data2- the other array to be tested for equality.- Returns:
- true if the specified arrays have the same size, signedness, and approximately equal corresponding elems.
-
equals
Returns true if the specified arrays have the same data type, shape, and equal corresponding elements. This method is suitable for use inObject.equals(java.lang.Object)implementations.Note that floating-point elements must be exactly equal, not merely within some epsilon of each other. This is because it's impossible to write a strictly-conforming
Object.equals(java.lang.Object)implementation when an epsilon is incorporated, due to the transitivity requirement.nearlyEquals(ucar.ma2.Array, ucar.ma2.Array)is an alternative to this method that returns true if the corresponding elements are "approximately" equal to each other.- Parameters:
array1- one array to be tested for equality.array2- the other array to be tested for equality.- Returns:
- true if the specified arrays have the same data type, shape, and equal corresponding elements.
- See Also:
-
hashCode
An implementation ofObject.hashCode()that is consistent withequals(Array, Array).- Parameters:
array- an array to hash.- Returns:
- a hash code value for the array.
-