/*
Copyright (C) 1999 CERN - European Organization for Nuclear Research.
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. 
CERN makes no representations about the suitability of this software for any purpose. 
It is provided "as is" without expressed or implied warranty.
*/
package cern.colt.matrix.impl;

import cern.colt.matrix.DoubleMatrix2D;
import cern.colt.matrix.DoubleMatrix3D;
/**
Dense 3-d matrix holding double elements.
First see the package summary and javadoc tree view to get the broad picture.

Implementation:

Internally holds one single contigous one-dimensional array, addressed in (in decreasing order of significance): slice major, row major, column major. Note that this implementation is not synchronized.

Memory requirements:

memory [bytes] = 8*slices()*rows()*columns(). Thus, a 100*100*100 matrix uses 8 MB.

Time complexity:

O(1) (i.e. constant time) for the basic operations get, getQuick, set, setQuick and size,

Applications demanding utmost speed can exploit knowledge about the internal addressing. Setting/getting values in a loop slice-by-slice, row-by-row, column-by-column is quicker than, for example, column-by-column, row-by-row, slice-by-slice. Thus

   for (int slice=0; slice < slices; slice++) {
	  for (int row=0; row < rows; row++) {
	     for (int column=0; column < columns; column++) {
			matrix.setQuick(slice,row,column,someValue);
		 }		    
	  }
   }
is quicker than
   for (int column=0; column < columns; column++) {
	  for (int row=0; row < rows; row++) {
	     for (int slice=0; slice < slices; slice++) {
			matrix.setQuick(slice,row,column,someValue);
		 }
	  }
   }
@author wolfgang.hoschek@cern.ch @version 1.0, 09/24/99 */ public class DenseDoubleMatrix3D extends DoubleMatrix3D { /** * The elements of this matrix. * elements are stored in slice major, then row major, then column major, in order of significance, i.e. * index==slice*sliceStride+ row*rowStride + column*columnStride * i.e. {slice0 row0..m}, {slice1 row0..m}, ..., {sliceN row0..m} * with each row storead as * {row0 column0..m}, {row1 column0..m}, ..., {rown column0..m} */ protected double[] elements; /** * Constructs a matrix with a copy of the given values. * values is required to have the form values[slice][row][column] * and have exactly the same number of rows in in every slice and exactly the same number of columns in in every row. *

* The values are copied. So subsequent changes in values are not reflected in the matrix, and vice-versa. * * @param values The values to be filled into the new matrix. * @throws IllegalArgumentException if for any 1 <= slice < values.length: values[slice].length != values[slice-1].length. * @throws IllegalArgumentException if for any 1 <= row < values[0].length: values[slice][row].length != values[slice][row-1].length. */ public DenseDoubleMatrix3D(double[][][] values) { this(values.length, (values.length==0 ? 0: values[0].length), (values.length==0 ? 0: values[0].length==0 ? 0 : values[0][0].length)); assign(values); } /** * Constructs a matrix with a given number of slices, rows and columns. * All entries are initially 0. * @param slices the number of slices the matrix shall have. * @param rows the number of rows the matrix shall have. * @param columns the number of columns the matrix shall have. * @throws IllegalArgumentException if (double)slices*columns*rows > Integer.MAX_VALUE. * @throws IllegalArgumentException if slices<0 || rows<0 || columns<0. */ public DenseDoubleMatrix3D(int slices, int rows, int columns) { setUp(slices,rows, columns); this.elements = new double[slices*rows*columns]; } /** * Constructs a view with the given parameters. * @param slices the number of slices the matrix shall have. * @param rows the number of rows the matrix shall have. * @param columns the number of columns the matrix shall have. * @param elements the cells. * @param sliceZero the position of the first element. * @param rowZero the position of the first element. * @param columnZero the position of the first element. * @param sliceStride the number of elements between two slices, i.e. index(k+1,i,j)-index(k,i,j). * @param rowStride the number of elements between two rows, i.e. index(k,i+1,j)-index(k,i,j). * @param columnnStride the number of elements between two columns, i.e. index(k,i,j+1)-index(k,i,j). * @throws IllegalArgumentException if (double)slices*columns*rows > Integer.MAX_VALUE. * @throws IllegalArgumentException if slices<0 || rows<0 || columns<0. */ protected DenseDoubleMatrix3D(int slices, int rows, int columns, double[] elements, int sliceZero, int rowZero, int columnZero, int sliceStride, int rowStride, int columnStride) { setUp(slices,rows,columns,sliceZero,rowZero,columnZero,sliceStride,rowStride,columnStride); this.elements = elements; this.isNoView = false; } /** * Sets all cells to the state specified by values. * values is required to have the form values[slice][row][column] * and have exactly the same number of slices, rows and columns as the receiver. *

* The values are copied. So subsequent changes in values are not reflected in the matrix, and vice-versa. * * @param values the values to be filled into the cells. * @return this (for convenience only). * @throws IllegalArgumentException if values.length != slices() || for any 0 <= slice < slices(): values[slice].length != rows(). * @throws IllegalArgumentException if for any 0 <= column < columns(): values[slice][row].length != columns(). */ public DoubleMatrix3D assign(double[][][] values) { if (this.isNoView) { if (values.length != slices) throw new IllegalArgumentException("Must have same number of slices: slices="+values.length+"slices()="+slices()); int i=slices*rows*columns - columns; for (int slice=slices; --slice >= 0;) { double[][] currentSlice = values[slice]; if (currentSlice.length != rows) throw new IllegalArgumentException("Must have same number of rows in every slice: rows="+currentSlice.length+"rows()="+rows()); for (int row=rows; --row >= 0;) { double[] currentRow = currentSlice[row]; if (currentRow.length != columns) throw new IllegalArgumentException("Must have same number of columns in every row: columns="+currentRow.length+"columns()="+columns()); System.arraycopy(currentRow, 0, this.elements, i, columns); i -= columns; } } } else { super.assign(values); } return this; } /** * Replaces all cell values of the receiver with the values of another matrix. * Both matrices must have the same number of slices, rows and columns. * If both matrices share the same cells (as is the case if they are views derived from the same matrix) and intersect in an ambiguous way, then replaces as if using an intermediate auxiliary deep copy of other. * * @param source the source matrix to copy from (may be identical to the receiver). * @return this (for convenience only). * @throws IllegalArgumentException if slices() != source.slices() || rows() != source.rows() || columns() != source.columns() */ public DoubleMatrix3D assign(DoubleMatrix3D source) { // overriden for performance only if (! (source instanceof DenseDoubleMatrix3D)) { return super.assign(source); } DenseDoubleMatrix3D other = (DenseDoubleMatrix3D) source; if (other==this) return this; checkShape(other); if (haveSharedCells(other)) { DoubleMatrix3D c = other.copy(); if (! (c instanceof DenseDoubleMatrix3D)) { // should not happen return super.assign(source); } other = (DenseDoubleMatrix3D) c; } if (this.isNoView && other.isNoView) { // quickest System.arraycopy(other.elements, 0, this.elements, 0, this.elements.length); return this; } return super.assign(other); } /** * Returns the matrix cell value at coordinate [slice,row,column]. * *

Provided with invalid parameters this method may return invalid objects without throwing any exception. * You should only use this method when you are absolutely sure that the coordinate is within bounds. * Precondition (unchecked): slice<0 || slice>=slices() || row<0 || row>=rows() || column<0 || column>=column(). * * @param slice the index of the slice-coordinate. * @param row the index of the row-coordinate. * @param column the index of the column-coordinate. * @return the value at the specified coordinate. */ public double getQuick(int slice, int row, int column) { //if (debug) if (slice<0 || slice>=slices || row<0 || row>=rows || column<0 || column>=columns) throw new IndexOutOfBoundsException("slice:"+slice+", row:"+row+", column:"+column); //return elements[index(slice,row,column)]; //manually inlined: return elements[sliceZero + slice*sliceStride + rowZero + row*rowStride + columnZero + column*columnStride]; } /** * Returns true if both matrices share common cells. * More formally, returns true if other != null and at least one of the following conditions is met *

*/ protected boolean haveSharedCellsRaw(DoubleMatrix3D other) { if (other instanceof SelectedDenseDoubleMatrix3D) { SelectedDenseDoubleMatrix3D otherMatrix = (SelectedDenseDoubleMatrix3D) other; return this.elements==otherMatrix.elements; } else if (other instanceof DenseDoubleMatrix3D) { DenseDoubleMatrix3D otherMatrix = (DenseDoubleMatrix3D) other; return this.elements==otherMatrix.elements; } return false; } /** * Returns the position of the given coordinate within the (virtual or non-virtual) internal 1-dimensional array. * * @param slice the index of the slice-coordinate. * @param row the index of the row-coordinate. * @param column the index of the third-coordinate. */ protected int index(int slice, int row, int column) { //return _sliceOffset(_sliceRank(slice)) + _rowOffset(_rowRank(row)) + _columnOffset(_columnRank(column)); //manually inlined: return sliceZero + slice*sliceStride + rowZero + row*rowStride + columnZero + column*columnStride; } /** * Construct and returns a new empty matrix of the same dynamic type as the receiver, having the specified number of slices, rows and columns. * For example, if the receiver is an instance of type DenseDoubleMatrix3D the new matrix must also be of type DenseDoubleMatrix3D, * if the receiver is an instance of type SparseDoubleMatrix3D the new matrix must also be of type SparseDoubleMatrix3D, etc. * In general, the new matrix should have internal parametrization as similar as possible. * * @param slices the number of slices the matrix shall have. * @param rows the number of rows the matrix shall have. * @param columns the number of columns the matrix shall have. * @return a new empty matrix of the same dynamic type. */ public DoubleMatrix3D like(int slices, int rows, int columns) { return new DenseDoubleMatrix3D(slices,rows,columns); } /** * Construct and returns a new 2-d matrix of the corresponding dynamic type, sharing the same cells. * For example, if the receiver is an instance of type DenseDoubleMatrix3D the new matrix must also be of type DenseDoubleMatrix2D, * if the receiver is an instance of type SparseDoubleMatrix3D the new matrix must also be of type SparseDoubleMatrix2D, etc. * * @param rows the number of rows the matrix shall have. * @param columns the number of columns the matrix shall have. * @param rowZero the position of the first element. * @param columnZero the position of the first element. * @param rowStride the number of elements between two rows, i.e. index(i+1,j)-index(i,j). * @param columnStride the number of elements between two columns, i.e. index(i,j+1)-index(i,j). * @return a new matrix of the corresponding dynamic type. */ protected DoubleMatrix2D like2D(int rows, int columns, int rowZero, int columnZero, int rowStride, int columnStride) { return new DenseDoubleMatrix2D(rows,columns,this.elements,rowZero,columnZero,rowStride,columnStride); } /** * Sets the matrix cell at coordinate [slice,row,column] to the specified value. * *

Provided with invalid parameters this method may access illegal indexes without throwing any exception. * You should only use this method when you are absolutely sure that the coordinate is within bounds. * Precondition (unchecked): slice<0 || slice>=slices() || row<0 || row>=rows() || column<0 || column>=column(). * * @param slice the index of the slice-coordinate. * @param row the index of the row-coordinate. * @param column the index of the column-coordinate. * @param value the value to be filled into the specified cell. */ public void setQuick(int slice, int row, int column, double value) { //if (debug) if (slice<0 || slice>=slices || row<0 || row>=rows || column<0 || column>=columns) throw new IndexOutOfBoundsException("slice:"+slice+", row:"+row+", column:"+column); //elements[index(slice,row,column)] = value; //manually inlined: elements[sliceZero + slice*sliceStride + rowZero + row*rowStride + columnZero + column*columnStride] = value; } /** * Construct and returns a new selection view. * * @param sliceOffsets the offsets of the visible elements. * @param rowOffsets the offsets of the visible elements. * @param columnOffsets the offsets of the visible elements. * @return a new view. */ protected DoubleMatrix3D viewSelectionLike(int[] sliceOffsets, int[] rowOffsets, int[] columnOffsets) { return new SelectedDenseDoubleMatrix3D(this.elements,sliceOffsets,rowOffsets,columnOffsets,0); } /** 27 neighbor stencil transformation. For efficient finite difference operations. Applies a function to a moving 3 x 3 x 3 window. Does nothing if rows() < 3 || columns() < 3 || slices() < 3.

B[k,i,j] = function.apply(
   A[k-1,i-1,j-1], A[k-1,i-1,j], A[k-1,i-1,j+1],
   A[k-1,i,  j-1], A[k-1,i,  j], A[k-1,i,  j+1],
   A[k-1,i+1,j-1], A[k-1,i+1,j], A[k-1,i+1,j+1],

   A[k  ,i-1,j-1], A[k  ,i-1,j], A[k  ,i-1,j+1],
   A[k  ,i,  j-1], A[k  ,i,  j], A[k  ,i,  j+1],
   A[k  ,i+1,j-1], A[k  ,i+1,j], A[k  ,i+1,j+1],

   A[k+1,i-1,j-1], A[k+1,i-1,j], A[k+1,i-1,j+1],
   A[k+1,i,  j-1], A[k+1,i,  j], A[k+1,i,  j+1],
   A[k+1,i+1,j-1], A[k+1,i+1,j], A[k+1,i+1,j+1]
   )

x x x -     - x x x     - - - - 
x o x -     - x o x     - - - - 
x x x -     - x x x ... - x x x 
- - - -     - - - -     - x o x 
- - - -     - - - -     - x x x 
Make sure that cells of this and B do not overlap. In case of overlapping views, behaviour is unspecified.

Example:

final double alpha = 0.25;
final double beta = 0.75;

cern.colt.function.Double27Function f = new cern.colt.function.Double27Function() {
   public final double apply(
      double a000, double a001, double a002,
      double a010, double a011, double a012,
      double a020, double a021, double a022,

      double a100, double a101, double a102,
      double a110, double a111, double a112,
      double a120, double a121, double a122,

      double a200, double a201, double a202,
      double a210, double a211, double a212,
      double a220, double a221, double a222) {
         return beta*a111 + alpha*(a000 + ... + a222);
      }
};
A.zAssign27Neighbors(B,f);
@param B the matrix to hold the results. @param function the function to be applied to the 27 cells. @throws NullPointerException if function==null. @throws IllegalArgumentException if rows() != B.rows() || columns() != B.columns() || slices() != B.slices() . */ public void zAssign27Neighbors(DoubleMatrix3D B, cern.colt.function.Double27Function function) { // overridden for performance only if (!(B instanceof DenseDoubleMatrix3D)) { super.zAssign27Neighbors(B, function); return; } if (function==null) throw new NullPointerException("function must not be null."); checkShape(B); int r = rows-1; int c = columns-1; if (rows<3 || columns<3 || slices<3) return; // nothing to do DenseDoubleMatrix3D BB = (DenseDoubleMatrix3D) B; int A_ss = sliceStride; int A_rs = rowStride; int B_rs = BB.rowStride; int A_cs = columnStride; int B_cs = BB.columnStride; double[] elems = this.elements; double[] B_elems = BB.elements; if (elems == null || B_elems==null) throw new InternalError(); for (int k=1; k

Ads help maintain this website.