View Javadoc
1   /*
2    * The baseCode project
3    * 
4    * Copyright (c) 2008-2019 University of British Columbia
5    * 
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *       http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   *
18   */
19  package ubic.basecode.dataStructure.matrix;
20  
21  import java.util.List;
22  
23  import cern.colt.matrix.DoubleMatrix2D;
24  
25  /**
26   * @author Xwan
27   * 
28   */
29  public class DenseDouble3dMatrix<R, C, S> extends DoubleMatrix3D<R, C, S> {
30  
31      private cern.colt.matrix.impl.DenseDoubleMatrix3D matrix;
32  
33      public DenseDouble3dMatrix( double[][][] data ) {
34          super();
35          matrix = new cern.colt.matrix.impl.DenseDoubleMatrix3D( data );
36      }
37  
38      public DenseDouble3dMatrix( double[][][] data, List<S> sliceNames, List<R> rowNames, List<C> colNames ) {
39          super();
40          matrix = new cern.colt.matrix.impl.DenseDoubleMatrix3D( data );
41          setRowNames( rowNames );
42          setColumnNames( colNames );
43          setSliceNames( sliceNames );
44      }
45  
46      public DenseDouble3dMatrix( int slices, int rows, int columns ) {
47          super();
48          matrix = new cern.colt.matrix.impl.DenseDoubleMatrix3D( slices, rows, columns );
49      }
50  
51      public DenseDouble3dMatrix( List<S> sliceNames, List<R> rowNames, List<C> colNames ) {
52          super();
53          setRowNames( rowNames );
54          setColumnNames( colNames );
55          setSliceNames( sliceNames );
56          matrix = new cern.colt.matrix.impl.DenseDoubleMatrix3D( sliceNames.size(), rowNames.size(), colNames.size() );
57      }
58  
59      @Override
60      public int columns() {
61          return matrix.columns();
62      }
63  
64      @Override
65      public double get( int i, int j, int k ) {
66          return matrix.get( i, j, k );
67      }
68  
69      @Override
70      public Double[][] getColObj( int col ) {
71          Double[][] colObj = new Double[slices()][rows()];
72          for ( int i = 0; i < slices(); i++ ) {
73              for ( int j = 0; j < rows(); j++ ) {
74                  colObj[i][j] = matrix.get( i, j, col );
75              }
76          }
77          return colObj;
78      }
79  
80      @Override
81      public double[][] getColumn( int col ) {
82          double[][] columnArray = new double[slices()][rows()];
83          for ( int i = 0; i < slices(); i++ )
84              for ( int j = 0; j < rows(); j++ )
85                  columnArray[i][j] = matrix.get( i, j, col );
86          return columnArray;
87      }
88  
89      @Override
90      public Double getObject( int slice, int row, int col ) {
91          return new Double( get( slice, row, col ) );
92      }
93  
94      @Override
95      public double[][] getRow( int row ) {
96          double[][] rowArray = new double[slices()][columns()];
97          for ( int i = 0; i < slices(); i++ )
98              for ( int j = 0; j < columns(); j++ )
99                  rowArray[i][j] = matrix.get( i, row, j );
100         return rowArray;
101     }
102 
103     @Override
104     public Double[][] getRowObj( int row ) {
105         Double[][] rowObj = new Double[slices()][columns()];
106         for ( int i = 0; i < slices(); i++ ) {
107             for ( int j = 0; j < columns(); j++ ) {
108                 rowObj[i][j] = new Double( matrix.get( i, row, j ) );
109             }
110         }
111         return rowObj;
112     }
113 
114     @Override
115     public double[][] getSlice( int slice ) {
116         double[][] sliceArray = new double[rows()][columns()];
117         for ( int i = 0; i < rows(); i++ )
118             for ( int j = 0; j < columns(); j++ )
119                 sliceArray[i][j] = matrix.get( slice, i, j );
120         return sliceArray;
121     }
122 
123     @Override
124     public Double[][] getSliceObj( int slice ) {
125         Double[][] sliceObj = new Double[slices()][columns()];
126         for ( int i = 0; i < rows(); i++ ) {
127             for ( int j = 0; j < columns(); j++ ) {
128                 sliceObj[i][j] = new Double( matrix.get( slice, i, j ) );
129             }
130         }
131         return sliceObj;
132     }
133 
134     @Override
135     public boolean isMissing( int slice, int row, int col ) {
136         return slice < slices() || row < rows() || col < columns();
137     }
138 
139     @Override
140     public int numMissing() {
141         int num = 0;
142         for ( int i = 0; i < slices(); i++ )
143             for ( int j = 0; j < rows(); j++ )
144                 for ( int k = 0; k < columns(); k++ )
145                     if ( isMissing( i, j, k ) ) num++;
146         return num;
147     }
148 
149     @Override
150     public int rows() {
151         return matrix.rows();
152     }
153 
154     @Override
155     public void set( int slice, int row, int col, double val ) {
156         matrix.set( slice, row, col, val );
157     }
158 
159     @Override
160     public void setQuick( int slice, int row, int column, double c ) {
161         matrix.setQuick( slice, row, column, c );
162     }
163 
164     @Override
165     public int slices() {
166         return matrix.slices();
167     }
168 
169     @Override
170     public DoubleMatrix2D viewColumn( int column ) {
171         return matrix.viewColumn( column );
172     }
173 
174     @Override
175     public DoubleMatrix2D viewRow( int row ) {
176         return matrix.viewRow( row );
177     }
178 
179     @Override
180     public DoubleMatrix2D viewSlice( int slice ) {
181         return matrix.viewSlice( slice );
182     }
183 
184 }