public static IComplexNDArray center ( IComplexNDArray arr , long [ ] shape ) { if ( arr . length ( ) < ArrayUtil . prod ( shape ) ) return arr ; for ( int i = 0 ; i < shape . length ; i ++ ) if ( shape [ i ] < 1 ) shape [ i ] = 1 ; INDArray shapeMatrix = NDArrayUtil . toNDArray ( shape ) ; INDArray currShape = NDArrayUtil . toNDArray ( arr . shape ( ) ) ; INDArray startIndex = Transforms . floor ( currShape . sub ( shapeMatrix ) . divi ( Nd4j . scalar ( 2 ) ) ) ; INDArray endIndex = startIndex . add ( shapeMatrix ) ; INDArrayIndex [ ] indexes = Indices . createFromStartAndEnd ( startIndex , endIndex ) ; if ( shapeMatrix . length ( ) > 1 ) return arr . get ( indexes ) ; else { IComplexNDArray ret = Nd4j . createComplex ( new int [ ] { ( int ) shapeMatrix . getDouble ( 0 ) } ) ; int start = ( int ) startIndex . getDouble ( 0 ) ; int end = ( int ) endIndex . getDouble ( 0 ) ; int count = 0 ; for ( int i = start ; i < end ; i ++ ) { ret . putScalar ( count ++ , arr . getComplex ( i ) ) ; } return ret ; } }
Center an array