<commit_before>/*
 *
 *  * Copyright 2015 Skymind,Inc.
 *  *
 *  *    Licensed under the Apache License, Version 2.0 (the "License");
 *  *    you may not use this file except in compliance with the License.
 *  *    You may obtain a copy of the License at
 *  *
 *  *        http://www.apache.org/licenses/LICENSE-2.0
 *  *
 *  *    Unless required by applicable law or agreed to in writing, software
 *  *    distributed under the License is distributed on an "AS IS" BASIS,
 *  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  *    See the License for the specific language governing permissions and
 *  *    limitations under the License.
 *
 *
 */

package org.nd4j.linalg.api.shape;

import com.google.common.primitives.Ints;
import org.nd4j.bytebuddy.shape.IndexMapper;
import org.nd4j.bytebuddy.shape.ShapeMapper;
import org.nd4j.linalg.api.buffer.DataBuffer;
import org.nd4j.linalg.api.complex.IComplexNDArray;
import org.nd4j.linalg.api.ndarray.INDArray;
import org.nd4j.linalg.factory.Nd4j;
import org.nd4j.linalg.indexing.INDArrayIndex;
import org.nd4j.linalg.indexing.NDArrayIndex;
import org.nd4j.linalg.indexing.ShapeOffsetResolution;
import org.nd4j.linalg.util.ArrayUtil;

import java.util.*;

/**
 * Encapsulates all shape related logic (vector of 0 dimension is a scalar is equivalent to
 * a vector of length 1...)
 *
 * @author <NAME>
 */
public class Shape {

    private static IndexMapper[] indexMappers = new IndexMapper[255];
    private static IndexMapper[] indexMappersC = new IndexMapper[255];

    static {
        for(int i = 0; i < 255; i++) {
            indexMappersC[i] = ShapeMapper.getInd2SubInstance('c',i);
            indexMappers[i] = ShapeMapper.getInd2SubInstance('f',i);

        }
    }


    /**
     * Create a copy of the matrix
     * where the new offset is zero
     * @param arr the array to copy to offset 0
     * @return the same array if offset is zero
     * otherwise a copy of the array with
     * elements set to zero
     */
    public static INDArray toOffsetZero(INDArray arr) {
        if(arr.offset() < 1 && arr.data().length() == arr.length() || arr instanceof  IComplexNDArray && arr.length() * 2 == arr.data().length())
            if(arr.ordering() == 'f' && arr.stride(-1) != arr.elementStride() ||
                    arr.ordering() == 'c' && arr.stride(0) != arr.elementStride())
                return arr;

        if(arr.isRowVector()) {
            if(arr instanceof IComplexNDArray) {
                IComplexNDArray ret = Nd4j.createComplex(arr.shape());
                for(int i = 0; i < ret.length(); i++)
                    ret.putScalar(i, ((IComplexNDArray) arr).getComplex(i));
                return ret;
            }
            else {
                INDArray ret = Nd4j.create(arr.shape());
                for(int i = 0; i < ret.length(); i++)
                    ret.putScalar(i,arr.getDouble(i));
                return ret;
            }
        }


        if(arr instanceof IComplexNDArray) {
            IComplexNDArray ret = Nd4j.createComplex(arr.shape());
            for(int i = 0; i < ret.slices(); i++)
                ret.putSlice(i,arr.slice(i));
            return ret;
        }
        else {
            INDArray ret = Nd4j.create(arr.shape());
            for(int i = 0; i < ret.slices(); i++)
                ret.putSlice(i,arr.slice(i));
            return ret;
        }
    }




    /**
     * Create a copy of the matrix
     * where the new offset is zero
     * @param arr the array to copy to offset 0
     * @return the same array if offset is zero
     * otherwise a copy of the array with
     * elements set to zero
     */
    public static INDArray toOffsetZeroCopy(INDArray arr) {
        if(arr.isRowVector()) {
            if(arr instanceof IComplexNDArray) {
                IComplexNDArray ret = Nd4j.createComplex(arr.shape());
                for(int i = 0; i < ret.length(); i++)
                    ret.putScalar(i, ((IComplexNDArray) arr).getComplex(i));
                return ret;
            }
            else {
                INDArray ret = Nd4j.create(arr.shape());
                for(int i = 0; i < ret.length(); i++)
                    ret.putScalar(i,arr.getDouble(i));
                return ret;
            }
        }


        if(arr instanceof IComplexNDArray) {
            IComplexNDArray ret = Nd4j.createComplex(arr.shape());
            for(int i = 0; i < ret.slices(); i++)
                ret.putSlice(i,arr.slice(i));
            return ret;
        }
        else {
            INDArray ret = Nd4j.create(arr.shape());
            for(int i = 0; i < arr.vectorsAlongDimension(0); i++) {
                ret.vectorAlongDimension(i,0).assign(arr.vectorAlongDimension(i,0));
            }


            return ret;
        }
    }

    public static double getDouble(INDArray arr,int...indices) {
         if(indices.length != arr.rank())
             throw new IllegalStateException("Indexes must be of same length as array");
        int offset = 0;
        DataBuffer data = arr.data();
        for(int i = 0; i < indices.length; i++) {
            /**
             * See:
             * http://docs.scipy.org/doc/numpy/reference/arrays.ndarray.html
             * Basically if the size(i) is 1, the stride shouldn't be counted.
             */
            if(arr.size(i) == 1)
                continue;
            offset += indices[i] * arr.stride(i);
        }


        return data.getDouble(offset + arr.offset());
    }


    /**
     * Output an int array for a particular dimension
     * @param axes the axes
     * @param shape the current shape
     * @return
     */
    public static int[] sizeForAxes(int[] axes, int[] shape) {
        int[] ret = new int[shape.length];
        for (int i = 0; i < axes.length; i++) {
            ret[i] = shape[axes[i]];
        }
        return ret;
    }
<commit_msg>begin refactor for bytecode based getDouble()<commit_after>/*
 *
 *  * Copyright 2015 Skymind,Inc.
 *  *
 *  *    Licensed under the Apache License, Version 2.0 (the "License");
 *  *    you may not use this file except in compliance with the License.
 *  *    You may obtain a copy of the License at
 *  *
 *  *        http://www.apache.org/licenses/LICENSE-2.0
 *  *
 *  *    Unless required by applicable law or agreed to in writing, software
 *  *    distributed under the License is distributed on an "AS IS" BASIS,
 *  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  *    See the License for the specific language governing permissions and
 *  *    limitations under the License.
 *
 *
 */

package org.nd4j.linalg.api.shape;

import com.google.common.primitives.Ints;
import org.nd4j.bytebuddy.shape.IndexMapper;
import org.nd4j.bytebuddy.shape.ShapeMapper;
import org.nd4j.linalg.api.buffer.DataBuffer;
import org.nd4j.linalg.api.complex.IComplexNDArray;
import org.nd4j.linalg.api.ndarray.INDArray;
import org.nd4j.linalg.factory.Nd4j;
import org.nd4j.linalg.indexing.INDArrayIndex;
import org.nd4j.linalg.indexing.NDArrayIndex;
import org.nd4j.linalg.indexing.ShapeOffsetResolution;
import org.nd4j.linalg.util.ArrayUtil;

import java.util.*;

/**
 * Encapsulates all shape related logic (vector of 0 dimension is a scalar is equivalent to
 * a vector of length 1...)
 *
 * @author Adam Gibson
 */
public class Shape {

    private static IndexMapper[] indexMappers = new IndexMapper[255];
    private static IndexMapper[] indexMappersC = new IndexMapper[255];

    static {
        for(int i = 0; i < 255; i++) {
            indexMappersC[i] = ShapeMapper.getInd2SubInstance('c',i);
            indexMappers[i] = ShapeMapper.getInd2SubInstance('f',i);

        }
    }


    /**
     * Create a copy of the matrix
     * where the new offset is zero
     * @param arr the array to copy to offset 0
     * @return the same array if offset is zero
     * otherwise a copy of the array with
     * elements set to zero
     */
    public static INDArray toOffsetZero(INDArray arr) {
        if(arr.offset() < 1 && arr.data().length() == arr.length() || arr instanceof  IComplexNDArray && arr.length() * 2 == arr.data().length())
            if(arr.ordering() == 'f' && arr.stride(-1) != arr.elementStride() ||
                    arr.ordering() == 'c' && arr.stride(0) != arr.elementStride())
                return arr;

        if(arr.isRowVector()) {
            if(arr instanceof IComplexNDArray) {
                IComplexNDArray ret = Nd4j.createComplex(arr.shape());
                for(int i = 0; i < ret.length(); i++)
                    ret.putScalar(i, ((IComplexNDArray) arr).getComplex(i));
                return ret;
            }
            else {
                INDArray ret = Nd4j.create(arr.shape());
                for(int i = 0; i < ret.length(); i++)
                    ret.putScalar(i,arr.getDouble(i));
                return ret;
            }
        }


        if(arr instanceof IComplexNDArray) {
            IComplexNDArray ret = Nd4j.createComplex(arr.shape());
            for(int i = 0; i < ret.slices(); i++)
                ret.putSlice(i,arr.slice(i));
            return ret;
        }
        else {
            INDArray ret = Nd4j.create(arr.shape());
            for(int i = 0; i < ret.slices(); i++)
                ret.putSlice(i,arr.slice(i));
            return ret;
        }
    }




    /**
     * Create a copy of the matrix
     * where the new offset is zero
     * @param arr the array to copy to offset 0
     * @return the same array if offset is zero
     * otherwise a copy of the array with
     * elements set to zero
     */
    public static INDArray toOffsetZeroCopy(INDArray arr) {
        if(arr.isRowVector()) {
            if(arr instanceof IComplexNDArray) {
                IComplexNDArray ret = Nd4j.createComplex(arr.shape());
                for(int i = 0; i < ret.length(); i++)
                    ret.putScalar(i, ((IComplexNDArray) arr).getComplex(i));
                return ret;
            }
            else {
                INDArray ret = Nd4j.create(arr.shape());
                for(int i = 0; i < ret.length(); i++)
                    ret.putScalar(i,arr.getDouble(i));
                return ret;
            }
        }


        if(arr instanceof IComplexNDArray) {
            IComplexNDArray ret = Nd4j.createComplex(arr.shape());
            for(int i = 0; i < ret.slices(); i++)
                ret.putSlice(i,arr.slice(i));
            return ret;
        }
        else {
            INDArray ret = Nd4j.create(arr.shape());
            for(int i = 0; i < arr.vectorsAlongDimension(0); i++) {
                ret.vectorAlongDimension(i,0).assign(arr.vectorAlongDimension(i,0));
            }


            return ret;
        }
    }

    /**
     * Get a double based on the array and given indices
     * @param arr the array to retrieve the double from
     * @param indices the indices to iterate over
     * @return the double at the specified index
     */
    public static double getDouble(INDArray arr,int...indices) {
        return arr.data().getDouble(getOffset(arr.offset(),arr.shape(),arr.stride(),indices));
    }


    /**
     * Get an offset for retrieval from a data buffer based on the given
     * shape stride and given indices
     * @param baseOffset the array to retrieve the double from
     * @param shape the shape of the array
     * @param stride the stride of the array
     * @param indices the indices to iterate over
     * @return the double at the specified index
     */
    public static int getOffset(int baseOffset,int[] shape,int[] stride,int...indices) {
        if(indices.length != shape.length)
            throw new IllegalStateException("Indexes must be of same length as array");
        int offset = 0;
        for(int i = 0; i < indices.length; i++) {
            /**
             * See:
             * http://docs.scipy.org/doc/numpy/reference/arrays.ndarray.html
             * Basically if the size(i) is 1, the stride shouldn't be counted.
             */
            if(shape[i] == 1)
                continue;
            offset += indices[i] * stride[i];
        }


        return offset + baseOffset;
    }


    /**
     * Output an int array for a particular dimension
     * @param axes the axes
     * @param shape the current shape
     * @return
     */
    public static int[] sizeForAxes(int[] axes, int[] shape) {
        int[] ret = new int[shape.length];
        for (int i = 0; i < axes.length; i++) {
            ret[i] = shape[axes[i]];
        }
        return ret;
    }

{'fraction_non_alphanumeric': 0.11737163717165304, 'fraction_numerical': 0.010157923974287756, 'mean_word_length': 1.9478362573099415, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3313142', 'n_tokens_mistral': 3888, 'n_tokens_neox': 3711, 'n_words': 1050}