-
-
Notifications
You must be signed in to change notification settings - Fork 213
Working with arrays
apete edited this page May 15, 2016
·
6 revisions
In the org.ojalgo.array package there are classes supporting 1, 2 or “any” dimensional arrays of double, BigDecimal, ComplexNumber, Quaternion and RationalNumber. The arrays can be sparse or dense and arbitrarily large.
This example code demonstrate some of the general features available, but it does so by using an odd member of ojAlgo's array family - memory mapped file based arrays.
import static org.ojalgo.constant.PrimitiveMath.*;
import static org.ojalgo.function.PrimitiveFunction.*;
import java.io.File;
import org.ojalgo.OjAlgoUtils;
import org.ojalgo.array.Array2D;
import org.ojalgo.array.ArrayAnyD;
import org.ojalgo.array.BufferArray;
import org.ojalgo.function.aggregator.AggregatorFunction;
import org.ojalgo.function.aggregator.PrimitiveAggregator;
import org.ojalgo.machine.JavaType;
import org.ojalgo.netio.BasicLogger;
import org.ojalgo.random.Uniform;
/**
* Demonstrate some basic array functionality using memory mapped file based arrays.
*
* @author apete
*/
public class ArrayBasics {
public static void main(final String[] args) {
BasicLogger.debug();
BasicLogger.debug(ArrayBasics.class.getSimpleName());
BasicLogger.debug(OjAlgoUtils.getTitle());
BasicLogger.debug(OjAlgoUtils.getDate());
BasicLogger.debug();
// The file pathname - previously existing or not
final File tmpFile = new File("BasicDemo.array");
final long tmpJvmMemory = OjAlgoUtils.ENVIRONMENT.memory;
BasicLogger.debug("The JVM was started with a max heap size of {}MB", (tmpJvmMemory / 1024L) / 1024L);
final long tmpMaxDimension = (long) Math.sqrt(tmpJvmMemory / JavaType.DOUBLE.memory());
BasicLogger.debug("The maximum number of rows and columns: {}", tmpMaxDimension);
// Disregarding any overhead and all other objects
// A max sized 2-dimensional file based array
final Array2D<Double> tmpArray2D = BufferArray.make(tmpFile, tmpMaxDimension, tmpMaxDimension);
// An equally sized multi/any-dimensional array, based on the same file
final ArrayAnyD<Double> tmpArrayAnyD = BufferArray.make(tmpFile, tmpMaxDimension, tmpMaxDimension, 1L, 1L);
// An any-dimensional array can of course be 1- or 2-dimensional. In this case we instantiated a 4-dimensional
// array, but the size of the 3:d and 4:th dimensions are just 1. Effectively this is just a 2-dimensioanl array.
// Fill the entire array/file with zeros
tmpArrayAnyD.fillAll(ZERO);
BasicLogger.debug("Number of elements in...");
BasicLogger.debug("\t2D: {}", tmpArray2D.count());
BasicLogger.debug("\tAnyD: {}", tmpArrayAnyD.count());
final AggregatorFunction<Double> tmpCardinality = PrimitiveAggregator.getSet().cardinality();
final long tmpRowIndex = Uniform.randomInteger(tmpMaxDimension);
final long tmpColumnIndex = Uniform.randomInteger(tmpMaxDimension);
// Using the arbitrary dimensinal interface/facade we will update an entire row (all columns) of the first matrix of the first cube...
long[] tmpReferenceToFirstElement = new long[] { tmpRowIndex, 0L, 0L, 0L };
int tmpDimension = 1; // That's the column-dimension
tmpArrayAnyD.fillSet(tmpReferenceToFirstElement, tmpDimension, PI);
// Using the arbitrary dimensional interface/facade we will update an entire row (all columns) of the first matrix of the first cube...
tmpReferenceToFirstElement = new long[] { 0L, tmpColumnIndex, 0L, 0L };
tmpDimension = 0; // That's the row-dimension
tmpArrayAnyD.fillSet(tmpReferenceToFirstElement, tmpDimension, PI);
// So far we've been writing to the array-file using ArrayAnyD
// Now we'll switch to using Array2D, but they're both mapped to the same files
tmpCardinality.reset();
tmpArray2D.visitRow(tmpRowIndex, 0L, tmpCardinality);
BasicLogger.debug("Number of nonzero elements in row {}: {}", tmpRowIndex, tmpCardinality.intValue());
tmpCardinality.reset();
tmpArray2D.visitColumn(0L, tmpColumnIndex, tmpCardinality);
BasicLogger.debug("Number of nonzero elements in column {}: {}", tmpColumnIndex, tmpCardinality.intValue());
tmpCardinality.reset();
tmpArray2D.visitAll(tmpCardinality);
BasicLogger.debug("Number of nonzero elements in the 2D array: {}", tmpCardinality.intValue());
BasicLogger.debug("Divide the elements of row {} to create 1.0:s", tmpRowIndex);
tmpArray2D.modifyRow(tmpRowIndex, 0L, DIVIDE.second(PI));
BasicLogger.debug("Subtract from the elements of column {} to create 0.0:s", tmpColumnIndex);
tmpArray2D.modifyColumn(0L, tmpColumnIndex, SUBTRACT.second(PI));
BasicLogger.debug("Explictly set the intersection element to 0.0 using the arbitrary-dimensional array.");
tmpArrayAnyD.set(new long[] { tmpRowIndex, tmpColumnIndex }, ZERO);
final AggregatorFunction<Double> tmpSum = PrimitiveAggregator.getSet().sum();
BasicLogger.debug("Expected sum of all elements: {}", tmpMaxDimension - 1L);
tmpSum.reset();
tmpArray2D.visitAll(tmpSum);
BasicLogger.debug("Actual sum of all elements: {}", tmpSum.intValue());
}
}
ArrayBasics
ojAlgo
2015-10-29
The JVM was started with a max heap size of 2731MB
The maximum number of rows and columns: 18919
Number of elements in...
2D: 357928561
AnyD: 357928561
Number of nonzero elements in row 12259: 18919
Number of nonzero elements in column 13969: 18919
Number of nonzero elements in the 2D array: 37837
Divide the elements of row 12259 to create 1.0:s
Subtract from the elements of column 13969 to create 0.0:s
Explictly set the intersection element to 0.0 using the arbitrary-dimensional array.
Expected sum of all elements: 18918
Actual sum of all elements: 18918