mirror of
https://github.com/apache/commons-math.git
synced 2025-02-08 02:59:36 +00:00
Formatting and javadoc.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1522083 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
9a50472883
commit
4c7b801625
@ -1272,159 +1272,158 @@ public class MathArrays {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Normalizes an array to make it sum to a specified value.
|
* Normalizes an array to make it sum to a specified value.
|
||||||
* Returns the result of the transformation <pre>
|
* Returns the result of the transformation
|
||||||
* x |-> x * normalizedSum / sum
|
* <pre>
|
||||||
* </pre>
|
* x |-> x * normalizedSum / sum
|
||||||
* applied to each non-NaN element x of the input array, where sum is the
|
* </pre>
|
||||||
* sum of the non-NaN entries in the input array.</p>
|
* applied to each non-NaN element x of the input array, where sum is the
|
||||||
*
|
* sum of the non-NaN entries in the input array.
|
||||||
* <p>Throws IllegalArgumentException if {@code normalizedSum} is infinite
|
* <p>
|
||||||
* or NaN and ArithmeticException if the input array contains any infinite elements
|
* Throws IllegalArgumentException if {@code normalizedSum} is infinite
|
||||||
* or sums to 0.</p>
|
* or NaN and ArithmeticException if the input array contains any infinite elements
|
||||||
*
|
* or sums to 0.
|
||||||
* <p>Ignores (i.e., copies unchanged to the output array) NaNs in the input array.</p>
|
* <p>
|
||||||
*
|
* Ignores (i.e., copies unchanged to the output array) NaNs in the input array.
|
||||||
* @param values Input array to be normalized
|
*
|
||||||
* @param normalizedSum Target sum for the normalized array
|
* @param values Input array to be normalized
|
||||||
* @return the normalized array.
|
* @param normalizedSum Target sum for the normalized array
|
||||||
* @throws MathArithmeticException if the input array contains infinite
|
* @return the normalized array.
|
||||||
* elements or sums to zero.
|
* @throws MathArithmeticException if the input array contains infinite
|
||||||
* @throws MathIllegalArgumentException if the target sum is infinite or {@code NaN}.
|
* elements or sums to zero.
|
||||||
* @since 2.1
|
* @throws MathIllegalArgumentException if the target sum is infinite or {@code NaN}.
|
||||||
*/
|
* @since 2.1
|
||||||
public static double[] normalizeArray(double[] values, double normalizedSum)
|
*/
|
||||||
throws MathIllegalArgumentException, MathArithmeticException {
|
public static double[] normalizeArray(double[] values, double normalizedSum)
|
||||||
if (Double.isInfinite(normalizedSum)) {
|
throws MathIllegalArgumentException, MathArithmeticException {
|
||||||
throw new MathIllegalArgumentException(LocalizedFormats.NORMALIZE_INFINITE);
|
if (Double.isInfinite(normalizedSum)) {
|
||||||
}
|
throw new MathIllegalArgumentException(LocalizedFormats.NORMALIZE_INFINITE);
|
||||||
if (Double.isNaN(normalizedSum)) {
|
}
|
||||||
throw new MathIllegalArgumentException(LocalizedFormats.NORMALIZE_NAN);
|
if (Double.isNaN(normalizedSum)) {
|
||||||
}
|
throw new MathIllegalArgumentException(LocalizedFormats.NORMALIZE_NAN);
|
||||||
double sum = 0d;
|
}
|
||||||
final int len = values.length;
|
double sum = 0d;
|
||||||
double[] out = new double[len];
|
final int len = values.length;
|
||||||
for (int i = 0; i < len; i++) {
|
double[] out = new double[len];
|
||||||
if (Double.isInfinite(values[i])) {
|
for (int i = 0; i < len; i++) {
|
||||||
throw new MathIllegalArgumentException(LocalizedFormats.INFINITE_ARRAY_ELEMENT, values[i], i);
|
if (Double.isInfinite(values[i])) {
|
||||||
}
|
throw new MathIllegalArgumentException(LocalizedFormats.INFINITE_ARRAY_ELEMENT, values[i], i);
|
||||||
if (!Double.isNaN(values[i])) {
|
}
|
||||||
sum += values[i];
|
if (!Double.isNaN(values[i])) {
|
||||||
}
|
sum += values[i];
|
||||||
}
|
}
|
||||||
if (sum == 0) {
|
}
|
||||||
throw new MathArithmeticException(LocalizedFormats.ARRAY_SUMS_TO_ZERO);
|
if (sum == 0) {
|
||||||
}
|
throw new MathArithmeticException(LocalizedFormats.ARRAY_SUMS_TO_ZERO);
|
||||||
for (int i = 0; i < len; i++) {
|
}
|
||||||
if (Double.isNaN(values[i])) {
|
for (int i = 0; i < len; i++) {
|
||||||
out[i] = Double.NaN;
|
if (Double.isNaN(values[i])) {
|
||||||
} else {
|
out[i] = Double.NaN;
|
||||||
out[i] = values[i] * normalizedSum / sum;
|
} else {
|
||||||
}
|
out[i] = values[i] * normalizedSum / sum;
|
||||||
}
|
}
|
||||||
return out;
|
}
|
||||||
}
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
/** Build an array of elements.
|
/** Build an array of elements.
|
||||||
* <p>
|
* <p>
|
||||||
* Arrays are filled with field.getZero()
|
* Arrays are filled with field.getZero()
|
||||||
* </p>
|
*
|
||||||
* @param <T> the type of the field elements
|
* @param <T> the type of the field elements
|
||||||
* @param field field to which array elements belong
|
* @param field field to which array elements belong
|
||||||
* @param length of the array
|
* @param length of the array
|
||||||
* @return a new array
|
* @return a new array
|
||||||
* @since 3.2
|
* @since 3.2
|
||||||
*/
|
*/
|
||||||
public static <T> T[] buildArray(final Field<T> field, final int length) {
|
public static <T> T[] buildArray(final Field<T> field, final int length) {
|
||||||
@SuppressWarnings("unchecked") // OK because field must be correct class
|
@SuppressWarnings("unchecked") // OK because field must be correct class
|
||||||
T[] array = (T[]) Array.newInstance(field.getRuntimeClass(), length);
|
T[] array = (T[]) Array.newInstance(field.getRuntimeClass(), length);
|
||||||
Arrays.fill(array, field.getZero());
|
Arrays.fill(array, field.getZero());
|
||||||
return array;
|
return array;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Build a double dimension array of elements.
|
/** Build a double dimension array of elements.
|
||||||
* <p>
|
* <p>
|
||||||
* Arrays are filled with field.getZero()
|
* Arrays are filled with field.getZero()
|
||||||
* </p>
|
*
|
||||||
* @param <T> the type of the field elements
|
* @param <T> the type of the field elements
|
||||||
* @param field field to which array elements belong
|
* @param field field to which array elements belong
|
||||||
* @param rows number of rows in the array
|
* @param rows number of rows in the array
|
||||||
* @param columns number of columns (may be negative to build partial
|
* @param columns number of columns (may be negative to build partial
|
||||||
* arrays in the same way <code>new Field[rows][]</code> works)
|
* arrays in the same way <code>new Field[rows][]</code> works)
|
||||||
* @return a new array
|
* @return a new array
|
||||||
* @since 3.2
|
* @since 3.2
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public static <T> T[][] buildArray(final Field<T> field, final int rows, final int columns) {
|
public static <T> T[][] buildArray(final Field<T> field, final int rows, final int columns) {
|
||||||
final T[][] array;
|
final T[][] array;
|
||||||
if (columns < 0) {
|
if (columns < 0) {
|
||||||
T[] dummyRow = buildArray(field, 0);
|
T[] dummyRow = buildArray(field, 0);
|
||||||
array = (T[][]) Array.newInstance(dummyRow.getClass(), rows);
|
array = (T[][]) Array.newInstance(dummyRow.getClass(), rows);
|
||||||
} else {
|
} else {
|
||||||
array = (T[][]) Array.newInstance(field.getRuntimeClass(),
|
array = (T[][]) Array.newInstance(field.getRuntimeClass(),
|
||||||
new int[] {
|
new int[] {
|
||||||
rows, columns
|
rows, columns
|
||||||
});
|
});
|
||||||
for (int i = 0; i < rows; ++i) {
|
for (int i = 0; i < rows; ++i) {
|
||||||
Arrays.fill(array[i], field.getZero());
|
Arrays.fill(array[i], field.getZero());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return array;
|
return array;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Calculates the <a href="http://en.wikipedia.org/wiki/Convolution">
|
* Calculates the <a href="http://en.wikipedia.org/wiki/Convolution">
|
||||||
* convolution</a> between two sequences.
|
* convolution</a> between two sequences.
|
||||||
* The solution is obtained via straightforward computation of the
|
* <p>
|
||||||
* convolution sum (and not via FFT).
|
* The solution is obtained via straightforward computation of the
|
||||||
* Whenever the computation needs an element that would be located
|
* convolution sum (and not via FFT). Whenever the computation needs
|
||||||
* at an index outside the input arrays, the value is assumed to be
|
* an element that would be located at an index outside the input arrays,
|
||||||
* zero.
|
* the value is assumed to be zero.
|
||||||
*
|
*
|
||||||
* @param x First sequence.
|
* @param x First sequence.
|
||||||
* Typically, this sequence will represent an input signal to a system.
|
* Typically, this sequence will represent an input signal to a system.
|
||||||
* @param h Second sequence.
|
* @param h Second sequence.
|
||||||
* Typically, this sequence will represent the impulse response of the
|
* Typically, this sequence will represent the impulse response of the system.
|
||||||
* system.
|
* @return the convolution of {@code x} and {@code h}.
|
||||||
* @return the convolution of {@code x} and {@code h}.
|
* This array's length will be {@code x.length + h.length - 1}.
|
||||||
* This array's length will be {@code x.length + h.length - 1}.
|
* @throws NullArgumentException if either {@code x} or {@code h} is {@code null}.
|
||||||
* @throws NullArgumentException if either {@code x} or {@code h} is
|
* @throws NoDataException if either {@code x} or {@code h} is empty.
|
||||||
* {@code null}.
|
*
|
||||||
* @throws NoDataException if either {@code x} or {@code h} is empty.
|
* @since 3.3
|
||||||
*
|
*/
|
||||||
* @since 3.3
|
public static double[] convolve(double[] x, double[] h)
|
||||||
*/
|
throws NullArgumentException,
|
||||||
public static double[] convolve(double[] x, double[] h)
|
NoDataException {
|
||||||
throws NullArgumentException,
|
MathUtils.checkNotNull(x);
|
||||||
NoDataException {
|
MathUtils.checkNotNull(h);
|
||||||
MathUtils.checkNotNull(x);
|
|
||||||
MathUtils.checkNotNull(h);
|
|
||||||
|
|
||||||
final int xLen = x.length;
|
final int xLen = x.length;
|
||||||
final int hLen = h.length;
|
final int hLen = h.length;
|
||||||
|
|
||||||
if (xLen == 0 || hLen == 0) {
|
if (xLen == 0 || hLen == 0) {
|
||||||
throw new NoDataException();
|
throw new NoDataException();
|
||||||
}
|
}
|
||||||
|
|
||||||
// initialize the output array
|
// initialize the output array
|
||||||
final int totalLength = xLen + hLen - 1;
|
final int totalLength = xLen + hLen - 1;
|
||||||
final double[] y = new double[totalLength];
|
final double[] y = new double[totalLength];
|
||||||
|
|
||||||
// straightforward implementation of the convolution sum
|
// straightforward implementation of the convolution sum
|
||||||
for (int n = 0; n < totalLength; n++) {
|
for (int n = 0; n < totalLength; n++) {
|
||||||
double yn = 0;
|
double yn = 0;
|
||||||
int k = FastMath.max(0, n + 1 - xLen);
|
int k = FastMath.max(0, n + 1 - xLen);
|
||||||
int j = n - k;
|
int j = n - k;
|
||||||
while (k < hLen && j >= 0) {
|
while (k < hLen && j >= 0) {
|
||||||
yn += x[j--] * h[k++];
|
yn += x[j--] * h[k++];
|
||||||
}
|
}
|
||||||
y[n] = yn;
|
y[n] = yn;
|
||||||
}
|
}
|
||||||
|
|
||||||
return y;
|
return y;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Specification for indicating that some operation applies
|
* Specification for indicating that some operation applies
|
||||||
|
Loading…
x
Reference in New Issue
Block a user