Class RealOps
java.lang.Object
org.flag4j.linalg.ops.common.real.RealOps
This class provides low level methods for computing ops on real tensors. These methods can be applied to
either sparse or dense real tensors.
-
Method Summary
Modifier and TypeMethodDescriptionstatic double[]
abs
(double[] src) Computes the element-wise absolute value of a tensor.static double[]
round
(double[] src) Rounds the values of a tensor to the nearest integer.static double[]
round
(double[] src, int precision) Rounds the values of a tensor with specified precision.static double[]
roundToZero
(double[] src, double threshold) Rounds values which are close to zero in absolute value to zero.static double[]
scalDiv
(double[] src, double divisor, double[] dest) Computes the scalar division of a tensor.static double[]
scalMult
(double[] src, double factor, double[] dest) Computes the scalar multiplication of a tensor.static double[]
scalMult
(double[] src, double factor, int start, int stop, double[] dest) Computes the scalar multiplication of a tensor.static double[]
scalMult
(double[] src, double factor, int start, int n, int stride, double[] dest) Scales entries by the specifiedfactor
withinsrc
starting at indexstart
and scaling a total ofn
elements spaced bystride
.static double[]
sqrt
(double[] src) Computes the element-wise square root of a tensor.
-
Method Details
-
scalMult
public static double[] scalMult(double[] src, double factor, double[] dest) Computes the scalar multiplication of a tensor.- Parameters:
src
- Entries of the tensor.factor
- Scalar value to multiply.dest
- Array to store result in. May be null.- Returns:
- A reference to the
dest
array if it was not null. Otherwise, a new array will be formed. - Throws:
ArrayIndexOutOfBoundsException
- Ifdest
is not at least the size ofsrc
.
-
scalMult
public static double[] scalMult(double[] src, double factor, int start, int stop, double[] dest) Computes the scalar multiplication of a tensor.- Parameters:
src
- Entries of the tensor.factor
- Scalar value to multiply.start
- Starting index of scalar multiplication.stop
- Stopping index of scalar multiplication.dest
- Array to store result in. May be null.- Returns:
- A reference to the
dest
array if it was not null. Otherwise, a new array will be formed. - Throws:
ArrayIndexOutOfBoundsException
- Ifdest
is not the size ofsrc
.
-
scalMult
public static double[] scalMult(double[] src, double factor, int start, int n, int stride, double[] dest) Scales entries by the specified
factor
withinsrc
starting at indexstart
and scaling a total ofn
elements spaced bystride
.More formally, this method scales elements by the specified
factor
at indices:start
,start + stride
,start + 2*stride
, ...,start + (n-1)*stride
.This method may be used to scale a row or column of a
matrix
a
as follows:- Maximum absolute value within row
i
:scale(a.data, i*a.numCols, a.numCols, 1, dest);
- Maximum absolute value within column
j
:scale(a.data, j, a.numRows, a.numRows, dest);
- Parameters:
src
- The array containing values to scale.factor
- Factor by which to scale elements.start
- The starting index insrc
to begin scaling.n
- The number of elements to scale withinsrc1
.stride
- The gap (in indices) between consecutive elements to scale withinsrc
.dest
- The array to store the result in. May benull
or the same array assrc
to perform the operation in-place. Assumed to be at least as large assrc
but this is not explicitly enforced.- Returns:
- If
dest == null
a new array containing all elements ofsrc
with the appropriate values scaled. Otherwise, A reference to thedest
array.
- Maximum absolute value within row
-
scalDiv
public static double[] scalDiv(double[] src, double divisor, double[] dest) Computes the scalar division of a tensor.- Parameters:
src
- Entries of the tensor.divisor
- Scalar value to divide.dest
- Array to store result in. May benull
.- Returns:
- A reference to the
dest
array if it was notnull
. Otherwise, a new array will be formed.
-
sqrt
public static double[] sqrt(double[] src) Computes the element-wise square root of a tensor.- Parameters:
src
- Elements of the tensor.- Returns:
- The element-wise square root of the tensor.
-
abs
public static double[] abs(double[] src) Computes the element-wise absolute value of a tensor.- Parameters:
src
- Elements of the tensor.- Returns:
- The element-wise absolute value of the tensor.
-
round
public static double[] round(double[] src) Rounds the values of a tensor to the nearest integer. Also seeround(double[], int)
.- Parameters:
src
- Entries of the tensor to round.- Returns:
- The result of rounding all data of the source tensor to the nearest integer.
- Throws:
IllegalArgumentException
- Ifprecision
is negative.
-
round
public static double[] round(double[] src, int precision) Rounds the values of a tensor with specified precision. Note, if precision is zero,round(double[])
is preferred.- Parameters:
src
- Entries of the tensor to round.precision
- Precision to round to (i.e. the number of decimal places).- Returns:
- The result of rounding all data of the source tensor with the specified precision.
- Throws:
IllegalArgumentException
- Ifprecision
is negative.
-
roundToZero
public static double[] roundToZero(double[] src, double threshold) Rounds values which are close to zero in absolute value to zero.- Parameters:
threshold
- Threshold for rounding values to zero. That is, if a value in this tensor is less than the threshold in absolute value then it will be rounded to zero. This value must be non-negative.- Returns:
- A copy of this matrix with rounded values.
- Throws:
IllegalArgumentException
- Ifthreshold
is negative.
-