jal.objects
Class Numeric

java.lang.Object
  extended byjal.objects.Numeric

public final class Numeric
extends Object

A class that encapsulates generalized numeric algorithms on one and two arrays. All methods are static and all variables are static and final, so this class has no constructors.

Most methods operate on a range of elements. A range is described by the index of its first element and an index that is one past its last element. So, for example, [n, n+1) is a range that contains one element, [n, n) is a range that contains zero elements, and [n, n-1) is not a valid range.

Unless otherwise specified, the test for equality uses the == operator by default. Any different notion of equality may be represented as a BinaryPredicate. You can use the predefined class Equals, which implements BinaryPredicate, if you want to use the Object.equals() method.

Copyright © 1996 Silicon Graphics, Inc.
Permission to use, copy, modify, distribute and sell this software and its documentation for any purpose is hereby granted without fee, provided that the above copyright notice appear in all copies and that both that copyright notice and this permission notice appear in supporting documentation. Silicon Graphics makes no representations about the suitability of this software for any purpose. It is provided "as is" without express or implied warranty.

Author:
Matthew Austern (austern@mti.sgi.com), Alexander Stepanov (stepanov@mti.sgi.com)
See Also:
Inspection, Modification, Sorting

Method Summary
static Object accumulate(Object[] array, int first, int last, Object init, BinaryOperator op)
          Generalized accumulation.
static int adjacent_difference(Object[] source, Object[] dest, int first, int last, int to, BinaryOperator op)
          Computes a binary operation op for each pair of adjacent elements in the input range and assigns the results to an output range.
static Object inner_product(Object[] array1, Object[] array2, int first1, int last1, int first2, Object init, BinaryOperator op1, BinaryOperator op2)
          Computes the generalized inner product of two ranges.
static int partial_sum(Object[] source, Object[] dest, int first, int last, int to, BinaryOperator op)
          Computes the generalized partial sums of elements in an input range and assigns them to elements in an output range.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

accumulate

public static Object accumulate(Object[] array,
                                int first,
                                int last,
                                Object init,
                                BinaryOperator op)
Generalized accumulation. The result acc is set to an initial value and, for each index i in the range, it is set to op.apply(acc, array[i]).

Parameters:
array - Array containing the range.
first - Beginning of the range.
last - Last element of the range.
init - Initial value of the accumulation.
op - Binary operation.
Returns:
The result of the accumulation.

inner_product

public static Object inner_product(Object[] array1,
                                   Object[] array2,
                                   int first1,
                                   int last1,
                                   int first2,
                                   Object init,
                                   BinaryOperator op1,
                                   BinaryOperator op2)
Computes the generalized inner product of two ranges. This is identical to the ordinary inner product, except that addition and multiplication are replaced, respectively, by binary operations op1 and op2. Specifically, the result acc is set to an initial value, and, for each index i in the range [first, last), acc is set to op1.apply(acc, op2.apply(array1[i] * array2[first2 + (i - first1)])).

Parameters:
array1 - Array containing the first range.
array2 - Array containing the second range.
first1 - Beginning of the first range.
last1 - One past the end of the first range.
first2 - Beginning of the second range.
init - Initial value for the result.
op1 - Binary operation that plays the role of addition.

partial_sum

public static int partial_sum(Object[] source,
                              Object[] dest,
                              int first,
                              int last,
                              int to,
                              BinaryOperator op)
Computes the generalized partial sums of elements in an input range and assigns them to elements in an output range. Generalized partial sums are identical to partial sums except that addition is replaced by an arbitrary binary operation op. dest[to] = source[first], dest[to+1] = op.apply(source[first], source[first+1]), etc. There must be enough space in the destination array, and existing elements will be overwritten.

Parameters:
source - Array containing the input range.
dest - Array containing the output range.
first - Beginning of the input range.
last - One past the end of the input range.
to - Beginning of the output range.
op - Binary operation that plays the role of addition.
Returns:
One past the end of the output range, that is, to + (last - first).

adjacent_difference

public static int adjacent_difference(Object[] source,
                                      Object[] dest,
                                      int first,
                                      int last,
                                      int to,
                                      BinaryOperator op)
Computes a binary operation op for each pair of adjacent elements in the input range and assigns the results to an output range. Assigns dest[to] = source[first], dest[to+1] = op.apply(source[first+1], source[first]),..., dest[to + (last-first)] = op.apply(source[last-1], source[last-2]). There must be enough space in the destination array, and existing elements will be overwritten.

Parameters:
source - Array containing the input range.
dest - Array containing the output range.
first - Beginning of the input range.
last - One past the end of the input range.
to - Beginning of the output range.
op - Binary operation.
Returns:
One past the end of the output range, that is, to + (last - first).