| Interface | Description |
|---|---|
| Tuple |
Interface describing an N-dimensional tuple
|
| Class | Description |
|---|---|
| Utils |
Internal utility methods for the tuples package.
|
| Enum | Description |
|---|---|
| Order |
An enumeration of the possible orders of tuples.
|
de.javagl.nd.tuples.d: Tuples consisting
of double values
de.javagl.nd.tuples.i: Tuples consisting
of int values
de.javagl.nd.tuples.j: Tuples consisting
of long values
DoubleTuple
interface represents a read-only tuple of double values.
The MutableDoubleTuple
represents a tuple of double values that may be modified.
For both interfaces, abstract base implementations exist. For example,
AbstractDoubleTuple and
AbstractMutableDoubleTuple. These classes offer canonical implementations
of the equals, hashCode and toString
methods, and thus allow implementing the corresponding interfaces with
minimal effort:
MutableDoubleTuple t = new AbstractMutableDoubleTuple()
{
@Override
public int getSize()
{
return myData.getNumberOfElements();
}
@Override
public double get(int index)
{
return myData.getElement(index);
}
@Override
public void set(int index, double value)
{
myData.setElement(index, value);
}
});
stream() method for stream handling,
and the subTuple(int,int) method that creates a view on
parts of the tuple. Sensible default implementations
for these methods exist in the interface, but they may be overridden
by implementors of the interface. These methods allow to flexibly
operate on tuples (and sub-tuples) using stream operations. Examples
of using the subTuple or stream method of the DoubleTuple class are shown here:
DoubleTuple tuple =
DoubleTuples.of(5.0, 4.0, 3.0, 2.0, 1.0, 0.0);
// Create a sub-tuple that is a view on a range of the tuple:
DoubleTuple subTuple = tuple.subTuple(1, 5);
System.out.println(subTuple); // Prints (4.0, 3.0, 2.0, 1.0)
// Create a stream from the sub-tuple, derive a sorted stream,
// and then convert the sorted stream into an array
double sortedArray[] = subTuple.stream().sorted().toArray();
System.out.println(
Arrays.toString(sortedArray)); // Prints [1.0, 2.0, 3.0, 4.0]
DoubleTuples class contains
methods that allow the creation and manipulation of DoubleTuple
instances:
// Create two tuples
MutableDoubleTuple t0 = DoubleTuples.of(0.0, 1.0, 2.0, 3.0);
MutableDoubleTuple t1 = DoubleTuples.of(1.0, 2.0, 3.0, 4.0);
// Compute the sum of the tuples, creating a new tuple:
MutableDoubleTuple sum = DoubleTuples.add(t0, t1, null);
System.out.println(sum); // Prints (1.0, 3.0, 5.0, 7.0)
// Multiply all elements if the sum with 2.0, storing the
// result in a predefined result tuple
MutableDoubleTuple result = DoubleTuples.create(sum.getSize());
DoubleTuples.multiply(sum, 2.0, result);
System.out.println(result); // Prints (2.0, 6.0, 10.0, 14.0)
// Multiply the result with 2.0, in-place
DoubleTuples.multiply(result, 2.0, result);
System.out.println(result); // Prints (4.0, 12.0, 20.0, 28.0)
// Do some comparisons
System.out.println(
DoubleTuples.areElementsLessThan(t1, 28.0)); // Prints false
System.out.println(
DoubleTuples.areElementsLessThanOrEqual(t1, 28.0)); // Prints true
DoubleTupleCollections
class contains methods that allow computing the sum of a collection of
DoubleTuple instances:
// Create a list of tuples
List<DoubleTuple> tuples = Arrays.asList (
DoubleTuples.of(0.0, 1.0, 2.0),
DoubleTuples.of(0.0, 1.0, 2.0),
DoubleTuples.of(0.0, 1.0, 2.0)
);
// Compute the sum of all tuples
MutableDoubleTuple sum =
DoubleTupleCollections.add(tuples, null);
System.out.println(sum); // Prints (0.0, 3.0, 6.0)
DoubleTupleFunctions class contains these utility methods for operating
on DoubleTuple instances.
MutableDoubleTuple tuple =
DoubleTuples.of(2.0, 3.0, 1.0, 4.0);
// Compute the sum of all elements of the tuple,
// using a (0,+) reduction
double sum = DoubleTupleFunctions.reduce(
tuple, 0.0, (a,b) -> a + b);
System.out.println(sum); // Prints 10.0
// Compute the product of all elements of the tuple,
// using a (1,*) reduction
double product = DoubleTupleFunctions.reduce(
tuple, 1.0, (a,b) > a * b);
System.out.println(product); // Prints 24.0
// Compute the minimum of all elements of the tuple,
// using a (inf,min) reduction
double min = DoubleTupleFunctions.reduce(
tuple, Double.POSITIVE_INFINITY, Math::min);
System.out.println(min); // Prints 1.0
Copyright © 2015. All Rights Reserved.