    001/*-
    002 *******************************************************************************
    003 * Copyright (c) 2011, 2016 Diamond Light Source Ltd.
    004 * All rights reserved. This program and the accompanying materials
    005 * are made available under the terms of the Eclipse Public License v1.0
    006 * which accompanies this distribution, and is available at
    007 * http://www.eclipse.org/legal/epl-v10.html
    008 *
    009 * Contributors:
    010 *    Peter Chang - initial API and implementation and/or initial documentation
    011 *******************************************************************************/
    012
    013// This is generated from CompoundDoubleDataset.java by fromcpddouble.py
    014
    015package org.eclipse.january.dataset;
    016
    017import java.util.Arrays;
    018
    019import org.apache.commons.math3.complex.Complex;
    020import org.slf4j.Logger;
    021import org.slf4j.LoggerFactory;
    022
    023/**
    024 * Extend compound dataset for short values // PRIM_TYPE
    025 */
    026public class CompoundShortDataset extends AbstractCompoundDataset {
    027        // pin UID to base class
    028        private static final long serialVersionUID = Dataset.serialVersionUID;
    029
    030        private static final Logger logger = LoggerFactory.getLogger(CompoundShortDataset.class);
    031
    032        protected short[] data; // subclass alias // PRIM_TYPE
    033
    034        @Override
    035        protected void setData() {
    036                data = (short[]) odata; // PRIM_TYPE
    037        }
    038
    039        protected short[] createArray(final int size) { // PRIM_TYPE
    040                short[] array = null; // PRIM_TYPE
    041
    042                try {
    043                        array = new short[isize * size]; // PRIM_TYPE
    044                } catch (OutOfMemoryError e) {
    045                        logger.error("The size of the dataset ({}) that is being created is too large "
    046                                        + "and there is not enough memory to hold it.", size);
    047                        throw new OutOfMemoryError("The dimensions given are too large, and there is "
    048                                        + "not enough memory available in the Java Virtual Machine");
    049                }
    050                return array;
    051        }
    052
    053        /**
    054         * Create a null dataset
    055         */
    056        CompoundShortDataset() {
    057        }
    058
    059        /**
    060         * Create a null dataset
    061         * @param itemSize
    062         */
    063        CompoundShortDataset(final int itemSize) {
    064                isize = itemSize;
    065        }
    066
    067        /**
    068         * Create a zero-filled dataset of given item size and shape
    069         * @param itemSize
    070         * @param shape
    071         */
    072        CompoundShortDataset(final int itemSize, final int[] shape) {
    073                isize = itemSize;
    074                if (shape != null) {
    075                        size = ShapeUtils.calcSize(shape);
    076                        this.shape = shape.clone();
    077
    078                        try {
    079                                odata = data = createArray(size);
    080                        } catch (Throwable t) {
    081                                logger.error("Could not create a dataset of shape {}", Arrays.toString(shape), t);
    082                                throw new IllegalArgumentException(t);
    083                        }
    084                }
    085        }
    086
    087        /**
    088         * Copy a dataset
    089         * @param dataset
    090         */
    091        CompoundShortDataset(final CompoundShortDataset dataset) {
    092                isize = dataset.isize;
    093
    094                copyToView(dataset, this, true, true);
    095                try {
    096                        if (dataset.stride == null) {
    097                                if (dataset.data != null) {
    098                                        odata = data = dataset.data.clone();
    099                                }
    100                        } else {
    101                                offset = 0;
    102                                stride = null;
    103                                base = null;
    104                                odata = data = createArray(size);
    105                                IndexIterator iter = dataset.getIterator();
    106                                for (int j = 0; iter.hasNext();) {
    107                                        for (int i = 0; i < isize; i++) {
    108                                                data[j++] = dataset.data[iter.index + i];
    109                                        }
    110                                }
    111                        }
    112                } catch (Throwable t) {
    113                        logger.error("Could not create a dataset of shape {}", Arrays.toString(shape), t);
    114                        throw new IllegalArgumentException(t);
    115                }
    116        }
    117
    118        /**
    119         * Create a dataset using given dataset
    120         * @param dataset
    121         */
    122        CompoundShortDataset(final CompoundDataset dataset) {
    123                copyToView(dataset, this, true, false);
    124                offset = 0;
    125                stride = null;
    126                base = null;
    127                isize = dataset.getElementsPerItem();
    128                try {
    129                        odata = data = createArray(size);
    130                } catch (Throwable t) {
    131                        logger.error("Could not create a dataset of shape {}", Arrays.toString(shape), t);
    132                        throw new IllegalArgumentException(t);
    133                }
    134
    135                IndexIterator iter = dataset.getIterator();
    136                for (int j = 0; iter.hasNext();) {
    137                        for (int i = 0; i < isize; i++) {
    138                                data[j++] = (short) dataset.getElementLongAbs(iter.index + i); // GET_ELEMENT_WITH_CAST
    139                        }
    140                }
    141        }
    142
    143        /**
    144         * Create a dataset using given data (elements are grouped together)
    145         * @param itemSize
    146         * @param data
    147         * @param shape
    148         *            (can be null to create 1D dataset)
    149         */
    150        CompoundShortDataset(final int itemSize, final short[] data, int... shape) { // PRIM_TYPE
    151                isize = itemSize;
    152                if (data != null) {
    153                        if (shape == null || (shape.length == 0 && data.length > isize)) {
    154                                shape = new int[] { data.length / isize };
    155                        }
    156                        size = ShapeUtils.calcSize(shape);
    157                        if (size * isize != data.length) {
    158                                throw new IllegalArgumentException(String.format("Shape %s is not compatible with size of data array, %d",
    159                                                Arrays.toString(shape), data.length / isize));
    160                        }
    161                        this.shape = size == 0 ? null : shape.clone();
    162
    163                        odata = this.data = data;
    164                }
    165        }
    166
    167        /**
    168         * Create a dataset using given datasets
    169         * @param datasets
    170         */
    171        CompoundShortDataset(final Dataset... datasets) {
    172                if (datasets.length < 1) {
    173                        throw new IllegalArgumentException("Array of datasets must have length greater than zero");
    174                }
    175
    176                for (int i = 1; i < datasets.length; i++) {
    177                        datasets[0].checkCompatibility(datasets[i]);
    178                }
    179
    180                isize = datasets.length;
    181                size = ShapeUtils.calcSize(datasets[0].getShapeRef());
    182                shape = datasets[0].getShape();
    183
    184                try {
    185                        odata = data = createArray(size);
    186                } catch (Throwable t) {
    187                        logger.error("Could not create a dataset of shape {}", Arrays.toString(shape), t);
    188                        throw new IllegalArgumentException(t);
    189                }
    190
    191                IndexIterator[] iters = new IndexIterator[isize];
    192                for (int i = 0; i < datasets.length; i++) {
    193                        iters[i] = datasets[i].getIterator();
    194                }
    195
    196                for (int j = 0; iters[0].hasNext();) {
    197                        data[j++] = (short) datasets[0].getElementLongAbs(iters[0].index); // GET_ELEMENT_WITH_CAST
    198                        for (int i = 1; i < datasets.length; i++) {
    199                                iters[i].hasNext();
    200                                data[j++] = (short) datasets[i].getElementLongAbs(iters[i].index); // GET_ELEMENT_WITH_CAST
    201                        }
    202                }
    203        }
    204
    205        /**
    206         * Cast a dataset to this compound type. If repeat is set, the first element of each item in the given dataset is
    207         * repeated across all elements of an item. Otherwise, each item comprises a truncated or zero-padded copy of
    208         * elements from the given dataset.
    209         * @param itemSize
    210         * @param repeat
    211         *            repeat first element
    212         * @param dataset
    213         */
    214        CompoundShortDataset(final int itemSize, final boolean repeat, final Dataset dataset) {
    215                isize = itemSize;
    216                size = dataset.getSize();
    217                shape = dataset.getShape();
    218                name = new String(dataset.getName());
    219
    220                try {
    221                        odata = data = createArray(size);
    222                } catch (Throwable t) {
    223                        logger.error("Could not create a dataset of shape {}", Arrays.toString(shape), t);
    224                        throw new IllegalArgumentException(t);
    225                }
    226                final int os = dataset.getElementsPerItem();
    227
    228                IndexIterator iter = dataset.getIterator();
    229                if (repeat) {
    230                        int i = 0;
    231                        while (iter.hasNext()) {
    232                                final short v = (short) dataset.getElementLongAbs(iter.index); // PRIM_TYPE // GET_ELEMENT_WITH_CAST
    233                                for (int k = 0; k < isize; k++) {
    234                                        data[i++] = v;
    235                                }
    236                        }
    237                } else {
    238                        final int kmax = Math.min(isize, os);
    239                        int i = 0;
    240                        while (iter.hasNext()) {
    241                                for (int k = 0; k < kmax; k++) {
    242                                        data[i + k] = (short) dataset.getElementLongAbs(iter.index + k); // GET_ELEMENT_WITH_CAST
    243                                }
    244                                i += isize;
    245                        }
    246                }
    247        }
    248
    249        @Override
    250        public boolean equals(Object obj) {
    251                if (this == obj) {
    252                        return true;
    253                }
    254
    255                if (obj == null) {
    256                        return false;
    257                }
    258
    259                if (!getClass().equals(obj.getClass())) {
    260                        if (getRank() == 0) { // for zero-rank datasets
    261                                return obj.equals(getObjectAbs(offset));
    262                        }
    263                        return false;
    264                }
    265
    266                CompoundShortDataset other = (CompoundShortDataset) obj;
    267                if (isize != other.isize) {
    268                        return false;
    269                }
    270                if (size != other.size) {
    271                        return false;
    272                }
    273                if (!Arrays.equals(shape, other.shape)) {
    274                        return false;
    275                }
    276                if (data == other.data && stride == null && other.stride == null) {
    277                        return true;
    278                }
    279
    280                IndexIterator iter = getIterator();
    281                IndexIterator oiter = other.getIterator();
    282                while (iter.hasNext() && oiter.hasNext()) {
    283                        for (int j = 0; j < isize; j++) {
    284                                if (data[iter.index+j] != other.data[oiter.index+j]) {
    285                                        return false;
    286                                }
    287                        }
    288                }
    289                return true;
    290        }
    291
    292        @Override
    293        public int hashCode() {
    294                return super.hashCode();
    295        }
    296
    297        @Override
    298        public CompoundShortDataset clone() {
    299                return new CompoundShortDataset(this);
    300        }
    301
    302        /**
    303         * Create a dataset from an object which could be a Java list, array (of arrays...) or Number. Ragged
    304         * sequences or arrays are padded with zeros. The item size is the last dimension of the corresponding
    305         * elemental dataset
    306         *
    307         * @param obj
    308         * @return dataset with contents given by input
    309         */
    310        static CompoundShortDataset createFromObject(final Object obj) {
    311                ShortDataset result = ShortDataset.createFromObject(obj); // CLASS_TYPE
    312                return createCompoundDatasetWithLastDimension(result, true);
    313        }
    314
    315        /**
    316         * Create a 1D dataset from an object which could be a Java list, array (of arrays...) or Number. Ragged
    317         * sequences or arrays are padded with zeros.
    318         *
    319         * @param itemSize item size
    320         * @param obj object
    321         * @return dataset with contents given by input
    322         */
    323        public static CompoundShortDataset createFromObject(final int itemSize, final Object obj) {
    324                ShortDataset result = ShortDataset.createFromObject(obj); // CLASS_TYPE
    325                boolean zeroRank = result.shape == null ? false : result.shape.length == 0;
    326                if (zeroRank) {
    327                        result.resize(itemSize); // special case of single item
    328                        result.fill(obj);
    329                }
    330                CompoundShortDataset ds = new CompoundShortDataset(itemSize, result.getData(), null);
    331                if (zeroRank) {
    332                        ds.setShape(new int[0]);
    333                }
    334                return ds;
    335        }
    336
    337        /**
    338         * @param stop
    339         * @return a new 1D dataset, filled with values determined by parameters
    340         */
    341        static CompoundShortDataset createRange(final int itemSize, final double stop) {
    342                return createRange(itemSize, 0., stop, 1.);
    343        }
    344
    345        /**
    346         * @param start
    347         * @param stop
    348         * @param step
    349         * @return a new 1D dataset, filled with values determined by parameters
    350         */
    351        static CompoundShortDataset createRange(final int itemSize, final double start, final double stop,
    352                        final double step) {
    353                int size = calcSteps(start, stop, step);
    354                CompoundShortDataset result = new CompoundShortDataset(itemSize, new int[] { size });
    355                for (int i = 0; i < size; i++) {
    356                        result.data[i * result.isize] = (short) (start + i * step); // PRIM_TYPE // ADD_CAST
    357                }
    358                return result;
    359        }
    360
    361        /**
    362         * @param shape
    363         * @return a dataset filled with ones
    364         */
    365        static CompoundShortDataset ones(final int itemSize, final int... shape) {
    366                return new CompoundShortDataset(itemSize, shape).fill(1);
    367        }
    368
    369        /**
    370         * Create a compound dataset using last dimension of given dataset
    371         * @param a dataset
    372         * @param shareData if true, then share data when possible otherwise copy it
    373         * @return compound dataset
    374         */
    375        public static CompoundShortDataset createCompoundDatasetWithLastDimension(final Dataset a, final boolean shareData) {
    376                if (a.getElementsPerItem() != 1) {
    377                        logger.error("Need a single-element dataset");
    378                        throw new IllegalArgumentException("Need a single-element dataset");
    379                }
    380                if (!ShortDataset.class.isAssignableFrom(a.getClass())) { // CLASS_TYPE
    381                        logger.error("Dataset type must be short"); // PRIM_TYPE
    382                        throw new IllegalArgumentException("Dataset type must be short"); // PRIM_TYPE
    383                }
    384
    385                final int[] shape = a.getShapeRef();
    386                if (shape == null) {
    387                        return new CompoundShortDataset(0);
    388                }
    389
    390                final int rank = shape.length - 1;
    391                final int is = rank < 0 ? 1 : shape[rank];
    392
    393                CompoundShortDataset result = new CompoundShortDataset(is);
    394
    395                result.shape = rank > 0 ? Arrays.copyOf(shape, rank) : (rank < 0 ? new int[0] : new int[] {1});
    396                result.size = ShapeUtils.calcSize(result.shape);
    397                result.odata = shareData ? a.flatten().getBuffer() : a.clone().getBuffer();
    398                result.setName(a.getName());
    399                result.setData();
    400                return result;
    401        }
    402
    403        @Override
    404        public ShortDataset asNonCompoundDataset(final boolean shareData) { // CLASS_TYPE
    405                ShortDataset result = new ShortDataset(); // CLASS_TYPE
    406                final int is = getElementsPerItem();
    407                final int rank = is == 1 ? shape.length : shape.length + 1;
    408                final int[] nshape = Arrays.copyOf(shape, rank);
    409                if (is != 1)
    410                        nshape[rank-1] = is;
    411
    412                result.shape = nshape;
    413                result.size = ShapeUtils.calcSize(nshape);
    414                result.odata = shareData && isContiguous() ? data : clone().getBuffer();
    415                result.setName(name);
    416                result.setData();
    417                return result;
    418        }
    419
    420        @Override
    421        public CompoundShortDataset fill(Object obj) {
    422                setDirty();
    423                if (obj instanceof Complex) {
    424                        obj = new Complex(((Complex) obj).getReal(), 0);
    425                }
    426                short[] vr = DTypeUtils.toShortArray(obj, isize); // PRIM_TYPE // CLASS_TYPE
    427                IndexIterator iter = getIterator();
    428
    429                while (iter.hasNext()) {
    430                        for (int i = 0; i < isize; i++) {
    431                                data[iter.index + i] = vr[i]; // PRIM_TYPE
    432                        }
    433                }
    434
    435                return this;
    436        }
    437
    438        /**
    439         * This is a typed version of {@link #getBuffer()}
    440         * @return data buffer as linear array
    441         */
    442        public short[] getData() { // PRIM_TYPE
    443                return data;
    444        }
    445
    446        @Override
    447        protected int getBufferLength() {
    448                if (data == null)
    449                        return 0;
    450                return data.length;
    451        }
    452
    453        @Override
    454        public CompoundShortDataset getView(boolean deepCopyMetadata) {
    455                CompoundShortDataset view = new CompoundShortDataset(isize);
    456                copyToView(this, view, true, deepCopyMetadata);
    457                view.setData();
    458                return view;
    459        }
    460
    461        /**
    462         * Get values at absolute index in the internal array. This is an internal method with no checks so can be
    463         * dangerous. Use with care or ideally with an iterator.
    464         *
    465         * @param index
    466         *            absolute index
    467         * @return values
    468         */
    469        public short[] getAbs(final int index) { // PRIM_TYPE
    470                short[] result = new short[isize]; // PRIM_TYPE
    471                for (int i = 0; i < isize; i++) {
    472                        result[i] = data[index + i];
    473                }
    474                return result;
    475        }
    476
    477        /**
    478         * Get values at absolute index in the internal array. This is an internal method with no checks so can be
    479         * dangerous. Use with care or ideally with an iterator.
    480         *
    481         * @param index
    482         *            absolute index
    483         * @param values output destination
    484         */
    485        public void getAbs(final int index, final short[] values) { // PRIM_TYPE
    486                for (int i = 0; i < isize; i++) {
    487                        values[i] = data[index + i];
    488                }
    489        }
    490
    491        @Override
    492        public boolean getElementBooleanAbs(final int index) {
    493                return data[index] != 0;
    494        }
    495
    496        @Override
    497        public double getElementDoubleAbs(final int index) {
    498                return data[index];
    499        }
    500
    501        @Override
    502        public long getElementLongAbs(final int index) {
    503                return data[index]; // OMIT_TOLONG_INT
    504        }
    505
    506        @Override
    507        protected void setItemDirect(final int dindex, final int sindex, final Object src) {
    508                setDirty();
    509                short[] dsrc = (short[]) src; // PRIM_TYPE
    510                for (int i = 0; i < isize; i++) {
    511                        data[dindex + i] = dsrc[sindex + i];
    512                }
    513        }
    514
    515        /**
    516         * Set values at absolute index in the internal array. This is an internal method with no checks so can be
    517         * dangerous. Use with care or ideally with an iterator.
    518         *
    519         * @param index
    520         *            absolute index
    521         * @param val
    522         *            new values
    523         */
    524        public void setAbs(final int index, final short[] val) { // PRIM_TYPE
    525                setDirty();
    526                for (int i = 0; i < isize; i++) {
    527                        data[index + i] = val[i];
    528                }
    529        }
    530
    531        /**
    532         * Set element value at absolute index in the internal array. This is an internal method with no checks so can be
    533         * dangerous. Use with care or ideally with an iterator.
    534         *
    535         * @param index
    536         *            absolute index
    537         * @param val
    538         *            new value
    539         */
    540        public void setAbs(final int index, final short val) { // PRIM_TYPE
    541                setDirty();
    542                data[index] = val;
    543        }
    544
    545        @Override
    546        public Object getObject() {
    547                return getShortArray(); // PRIM_TYPE
    548        }
    549
    550        @Override
    551        public Object getObject(final int i) {
    552                return getShortArray(i); // PRIM_TYPE
    553        }
    554
    555        @Override
    556        public Object getObject(final int i, final int j) {
    557                return getShortArray(i, j); // PRIM_TYPE
    558        }
    559
    560        @Override
    561        public Object getObject(final int... pos) {
    562                return getShortArray(pos); // PRIM_TYPE
    563        }
    564
    565        @Override
    566        public byte[] getByteArray() {
    567                byte[] result = new byte[isize];
    568                int index = getFirst1DIndex();
    569                for (int k = 0; k < isize; k++) {
    570                        result[k] = (byte) data[index + k]; // OMIT_UPCAST
    571                }
    572                return result;
    573        }
    574
    575        @Override
    576        public byte[] getByteArray(final int i) {
    577                byte[] result = new byte[isize];
    578                int index = get1DIndex(i);
    579                for (int k = 0; k < isize; k++) {
    580                        result[k] = (byte) data[index + k]; // OMIT_UPCAST
    581                }
    582                return result;
    583        }
    584
    585        @Override
    586        public byte[] getByteArray(final int i, final int j) {
    587                byte[] result = new byte[isize];
    588                int index = get1DIndex(i, j);
    589                for (int k = 0; k < isize; k++) {
    590                        result[k] = (byte) data[index + k]; // OMIT_UPCAST
    591                }
    592                return result;
    593        }
    594
    595        @Override
    596        public byte[] getByteArray(final int... pos) {
    597                byte[] result = new byte[isize];
    598                int index = get1DIndex(pos);
    599                for (int k = 0; k < isize; k++) {
    600                        result[k] = (byte) data[index + k]; // OMIT_UPCAST
    601                }
    602                return result;
    603        }
    604
    605        @Override
    606        public short[] getShortArray() {
    607                short[] result = new short[isize];
    608                int index = getFirst1DIndex();
    609                for (int k = 0; k < isize; k++) {
    610                        result[k] = data[index + k]; // OMIT_UPCAST
    611                }
    612                return result;
    613        }
    614
    615        @Override
    616        public short[] getShortArray(final int i) {
    617                short[] result = new short[isize];
    618                int index = get1DIndex(i);
    619                for (int k = 0; k < isize; k++) {
    620                        result[k] = data[index + k]; // OMIT_UPCAST
    621                }
    622                return result;
    623        }
    624
    625        @Override
    626        public short[] getShortArray(final int i, final int j) {
    627                short[] result = new short[isize];
    628                int index = get1DIndex(i, j);
    629                for (int k = 0; k < isize; k++) {
    630                        result[k] = data[index + k]; // OMIT_UPCAST
    631                }
    632                return result;
    633        }
    634
    635        @Override
    636        public short[] getShortArray(final int... pos) {
    637                short[] result = new short[isize];
    638                int index = get1DIndex(pos);
    639                for (int k = 0; k < isize; k++) {
    640                        result[k] = data[index + k]; // OMIT_UPCAST
    641                }
    642                return result;
    643        }
    644
    645        @Override
    646        public int[] getIntArray() {
    647                int[] result = new int[isize];
    648                int index = getFirst1DIndex();
    649                for (int k = 0; k < isize; k++) {
    650                        result[k] = data[index + k]; // OMIT_UPCAST
    651                }
    652                return result;
    653        }
    654
    655        @Override
    656        public int[] getIntArray(final int i) {
    657                int[] result = new int[isize];
    658                int index = get1DIndex(i);
    659                for (int k = 0; k < isize; k++) {
    660                        result[k] = data[index + k]; // OMIT_UPCAST
    661                }
    662                return result;
    663        }
    664
    665        @Override
    666        public int[] getIntArray(final int i, final int j) {
    667                int[] result = new int[isize];
    668                int index = get1DIndex(i, j);
    669                for (int k = 0; k < isize; k++) {
    670                        result[k] = data[index + k]; // OMIT_UPCAST
    671                }
    672                return result;
    673        }
    674
    675        @Override
    676        public int[] getIntArray(final int... pos) {
    677                int[] result = new int[isize];
    678                int index = get1DIndex(pos);
    679                for (int k = 0; k < isize; k++) {
    680                        result[k] = data[index + k]; // OMIT_UPCAST
    681                }
    682                return result;
    683        }
    684
    685        @Override
    686        public long[] getLongArray() {
    687                long[] result = new long[isize];
    688                int index = getFirst1DIndex();
    689                for (int k = 0; k < isize; k++) {
    690                        result[k] = data[index + k]; // OMIT_UPCAST
    691                }
    692                return result;
    693        }
    694
    695        @Override
    696        public long[] getLongArray(final int i) {
    697                long[] result = new long[isize];
    698                int index = get1DIndex(i);
    699                for (int k = 0; k < isize; k++) {
    700                        result[k] = data[index + k]; // OMIT_UPCAST
    701                }
    702                return result;
    703        }
    704
    705        @Override
    706        public long[] getLongArray(final int i, final int j) {
    707                long[] result = new long[isize];
    708                int index = get1DIndex(i, j);
    709                for (int k = 0; k < isize; k++) {
    710                        result[k] = data[index + k]; // OMIT_UPCAST
    711                }
    712                return result;
    713        }
    714
    715        @Override
    716        public long[] getLongArray(final int... pos) {
    717                long[] result = new long[isize];
    718                int index = get1DIndex(pos);
    719                for (int k = 0; k < isize; k++) {
    720                        result[k] = data[index + k]; // OMIT_UPCAST
    721                }
    722                return result;
    723        }
    724
    725        @Override
    726        public float[] getFloatArray() {
    727                float[] result = new float[isize];
    728                int index = getFirst1DIndex();
    729                for (int k = 0; k < isize; k++) {
    730                        result[k] = data[index + k]; // OMIT_REAL_CAST
    731                }
    732                return result;
    733        }
    734
    735        @Override
    736        public float[] getFloatArray(final int i) {
    737                float[] result = new float[isize];
    738                int index = get1DIndex(i);
    739                for (int k = 0; k < isize; k++) {
    740                        result[k] = data[index + k]; // OMIT_REAL_CAST
    741                }
    742                return result;
    743        }
    744
    745        @Override
    746        public float[] getFloatArray(final int i, final int j) {
    747                float[] result = new float[isize];
    748                int index = get1DIndex(i, j);
    749                for (int k = 0; k < isize; k++) {
    750                        result[k] = data[index + k]; // OMIT_REAL_CAST
    751                }
    752                return result;
    753        }
    754
    755        @Override
    756        public float[] getFloatArray(final int... pos) {
    757                float[] result = new float[isize];
    758                int index = get1DIndex(pos);
    759                for (int k = 0; k < isize; k++) {
    760                        result[k] = data[index + k]; // OMIT_REAL_CAST
    761                }
    762                return result;
    763        }
    764
    765        @Override
    766        public double[] getDoubleArray() {
    767                double[] result = new double[isize];
    768                int index = getFirst1DIndex();
    769                for (int k = 0; k < isize; k++) {
    770                        result[k] = data[index + k]; // OMIT_REAL_CAST
    771                }
    772                return result;
    773        }
    774
    775        @Override
    776        public double[] getDoubleArray(final int i) {
    777                double[] result = new double[isize];
    778                int index = get1DIndex(i);
    779                for (int k = 0; k < isize; k++) {
    780                        result[k] = data[index + k]; // OMIT_REAL_CAST
    781                }
    782                return result;
    783        }
    784
    785        @Override
    786        public double[] getDoubleArray(final int i, final int j) {
    787                double[] result = new double[isize];
    788                int index = get1DIndex(i, j);
    789                for (int k = 0; k < isize; k++) {
    790                        result[k] = data[index + k]; // OMIT_REAL_CAST
    791                }
    792                return result;
    793        }
    794
    795        @Override
    796        public double[] getDoubleArray(final int... pos) {
    797                double[] result = new double[isize];
    798                int index = get1DIndex(pos);
    799                for (int k = 0; k < isize; k++) {
    800                        result[k] = data[index + k]; // OMIT_REAL_CAST
    801                }
    802                return result;
    803        }
    804
    805        @Override
    806        public void getDoubleArrayAbs(final int index, final double[] darray) {
    807                for (int i = 0; i < isize; i++) {
    808                        darray[i] = data[index + i];
    809                }
    810        }
    811
    812        @Override
    813        public String getString() {
    814                return getStringAbs(getFirst1DIndex());
    815        }
    816
    817        @Override
    818        public String getString(final int i) {
    819                return getStringAbs(get1DIndex(i));
    820        }
    821
    822        @Override
    823        public String getString(final int i, final int j) {
    824                return getStringAbs(get1DIndex(i, j));
    825        }
    826
    827        @Override
    828        public String getString(final int... pos) {
    829                return getStringAbs(get1DIndex(pos));
    830        }
    831
    832        @Override
    833        protected double getFirstValue() {
    834                return data[getFirst1DIndex()];
    835        }
    836
    837        @Override
    838        protected double getFirstValue(int i) {
    839                return data[get1DIndex(i)];
    840        }
    841
    842        @Override
    843        protected double getFirstValue(int i, int j) {
    844                return data[get1DIndex(i, j)];
    845        }
    846
    847        @Override
    848        protected double getFirstValue(final int... pos) {
    849                return data[get1DIndex(pos)];
    850        }
    851
    852        @Override
    853        public Object getObjectAbs(final int index) {
    854                short[] result = new short[isize]; // PRIM_TYPE
    855                for (int i = 0; i < isize; i++) {
    856                        result[i] = data[index + i];
    857                }
    858                return result;
    859        }
    860
    861        @Override
    862        public String getStringAbs(final int index) {
    863                StringBuilder s = new StringBuilder();
    864                s.append('(');
    865                s.append(stringFormat == null ? String.format("%d", data[index]) : // FORMAT_STRING
    866                        stringFormat.format(data[index]));
    867                for (int i = 1; i < isize; i++) {
    868                        s.append(' ');
    869                        s.append(stringFormat == null ? String.format("%d", data[index + i]) : // FORMAT_STRING
    870                                stringFormat.format(data[index + i]));
    871                }
    872                s.append(')');
    873                return s.toString();
    874        }
    875
    876        @Override
    877        public void setObjectAbs(final int index, Object obj) {
    878                if (obj instanceof Complex) {
    879                        obj = new Complex(((Complex) obj).getReal(), 0);
    880                }
    881                short[] oa = DTypeUtils.toShortArray(obj, isize); // PRIM_TYPE // CLASS_TYPE
    882                setAbs(index, oa);
    883        }
    884
    885        @Override
    886        public void set(final Object obj) {
    887                setItem(DTypeUtils.toShortArray(obj, isize)); // CLASS_TYPE
    888        }
    889
    890        @Override
    891        public void set(final Object obj, final int i) {
    892                setItem(DTypeUtils.toShortArray(obj, isize), i); // CLASS_TYPE
    893        }
    894
    895        @Override
    896        public void set(final Object obj, final int i, final int j) {
    897                setItem(DTypeUtils.toShortArray(obj, isize), i, j); // CLASS_TYPE
    898        }
    899
    900        @Override
    901        public void set(final Object obj, int... pos) {
    902                if (pos == null || (pos.length == 0 && shape.length > 0)) {
    903                        pos = new int[shape.length];
    904                }
    905
    906                setItem(DTypeUtils.toShortArray(obj, isize), pos); // CLASS_TYPE
    907        }
    908
    909        /**
    910         * Set values at first position. The dataset must not be null
    911         *
    912         * @param d input source
    913         * @since 2.0
    914         */
    915        public void setItem(final short[] d) { // PRIM_TYPE
    916                if (d.length > isize) {
    917                        throw new IllegalArgumentException("Array is larger than number of elements in an item");
    918                }
    919                setAbs(getFirst1DIndex(), d);
    920        }
    921
    922        /**
    923         * Set values at given position. The dataset must be 1D
    924         *
    925         * @param d input source
    926         * @param i position in first dimension
    927         */
    928        public void setItem(final short[] d, final int i) { // PRIM_TYPE
    929                if (d.length > isize) {
    930                        throw new IllegalArgumentException("Array is larger than number of elements in an item");
    931                }
    932                setAbs(get1DIndex(i), d);
    933        }
    934
    935        /**
    936         * Set values at given position. The dataset must be 1D
    937         *
    938         * @param d input source
    939         * @param i position in first dimension
    940         * @param j position in second dimension
    941         */
    942        public void setItem(final short[] d, final int i, final int j) { // PRIM_TYPE
    943                if (d.length > isize) {
    944                        throw new IllegalArgumentException("Array is larger than number of elements in an item");
    945                }
    946                setAbs(get1DIndex(i, j), d);
    947        }
    948
    949        /**
    950         * Set values at given position
    951         *
    952         * @param d input source
    953         * @param pos position
    954         */
    955        public void setItem(final short[] d, final int... pos) { // PRIM_TYPE
    956                if (d.length > isize) {
    957                        throw new IllegalArgumentException("Array is larger than number of elements in an item");
    958                }
    959                setAbs(get1DIndex(pos), d);
    960        }
    961
    962        private void setDoubleArrayAbs(final int index, final double[] d) {
    963                for (int i = 0; i < isize; i++)
    964                        data[index + i] = (short) d[i]; // ADD_CAST
    965        }
    966
    967        @Override
    968        public void resize(int... newShape) {
    969                setDirty();
    970                IndexIterator iter = getIterator();
    971                int nsize = ShapeUtils.calcSize(newShape);
    972                short[] ndata; // PRIM_TYPE
    973                try {
    974                        ndata = createArray(nsize);
    975                } catch (Throwable t) {
    976                        logger.error("Could not create a dataset of shape {}", Arrays.toString(shape), t);
    977                        throw new IllegalArgumentException(t);
    978                }
    979
    980                int i = 0;
    981                while (iter.hasNext() && i < nsize) {
    982                        for (int j = 0; j < isize; j++) {
    983                                ndata[i++] = data[iter.index + j];
    984                        }
    985                }
    986
    987                odata = data = ndata;
    988                size = nsize;
    989                shape = newShape;
    990                stride = null;
    991                offset = 0;
    992                base = null;
    993        }
    994
    995        @Override
    996        public CompoundShortDataset getSlice(final SliceIterator siter) {
    997                CompoundShortDataset result = new CompoundShortDataset(isize, siter.getShape());
    998                short[] rdata = result.data; // PRIM_TYPE
    999                IndexIterator riter = result.getIterator();
    1000
    1001                while (siter.hasNext() && riter.hasNext()) {
    1002                        for (int i = 0; i < isize; i++)
    1003                                rdata[riter.index + i] = data[siter.index + i];
    1004                }
    1005
    1006                result.setName(name + BLOCK_OPEN + Slice.createString(siter.shape, siter.start, siter.stop, siter.step) + BLOCK_CLOSE);
    1007                return result;
    1008        }
    1009
    1010        @Override
    1011        public ShortDataset getElementsView(int element) { // CLASS_TYPE
    1012                if (element < 0)
    1013                        element += isize;
    1014                if (element < 0 || element > isize) {
    1015                        throw new IllegalArgumentException(String.format("Invalid choice of element: %d/%d", element, isize));
    1016                }
    1017
    1018                ShortDataset view = new ShortDataset(shape); // CLASS_TYPE
    1019
    1020                copyToView(this, view, true, true);
    1021                view.setData();
    1022                if (view.stride == null) {
    1023                        int[] offset = new int[1];
    1024                        view.stride = createStrides(this, offset);
    1025                        view.offset = offset[0] + element;
    1026                        view.base = base == null ? this : base;
    1027                } else {
    1028                        view.offset += element;
    1029                }
    1030
    1031                return view;
    1032        }
    1033
    1034        @Override
    1035        public ShortDataset getElements(int element) { // CLASS_TYPE
    1036                final ShortDataset elements = new ShortDataset(shape); // CLASS_TYPE
    1037
    1038                copyElements(elements, element);
    1039                return elements;
    1040        }
    1041
    1042        @Override
    1043        public void copyElements(Dataset destination, int element) {
    1044                if (element < 0)
    1045                        element += isize;
    1046                if (element < 0 || element > isize) {
    1047                        throw new IllegalArgumentException(String.format("Invalid choice of element: %d/%d", element, isize));
    1048                }
    1049                if (getElementClass() != destination.getElementClass()) {
    1050                        throw new IllegalArgumentException("Element class of destination does not match this dataset");
    1051                }
    1052
    1053                final IndexIterator it = getIterator(element);
    1054                final short[] elements = ((ShortDataset) destination).data; // CLASS_TYPE // PRIM_TYPE
    1055                destination.setDirty();
    1056
    1057                int n = 0;
    1058                while (it.hasNext()) {
    1059                        elements[n] = data[it.index];
    1060                        n++;
    1061                }
    1062        }
    1063
    1064        @Override
    1065        public void setElements(Dataset source, int element) {
    1066                setDirty();
    1067                if (element < 0)
    1068                        element += isize;
    1069                if (element < 0 || element > isize) {
    1070                        throw new IllegalArgumentException(String.format("Invalid choice of element: %d/%d", element, isize));
    1071                }
    1072                if (getElementClass() != source.getElementClass()) {
    1073                        throw new IllegalArgumentException("Element class of destination does not match this dataset");
    1074                }
    1075
    1076                final IndexIterator it = getIterator(element);
    1077                final short[] elements = ((ShortDataset) source).data; // CLASS_TYPE // PRIM_TYPE
    1078
    1079                int n = 0;
    1080                while (it.hasNext()) {
    1081                        data[it.index] = elements[n];
    1082                        n++;
    1083                }
    1084        }
    1085
    1086        @Override
    1087        public void fillDataset(Dataset result, IndexIterator iter) {
    1088                IndexIterator riter = result.getIterator();
    1089                result.setDirty();
    1090
    1091                short[] rdata = ((CompoundShortDataset) result).data; // PRIM_TYPE
    1092
    1093                while (riter.hasNext() && iter.hasNext()) {
    1094                        for (int i = 0; i < isize; i++) {
    1095                                rdata[riter.index + i] = data[iter.index + i];
    1096                        }
    1097                }
    1098        }
    1099
    1100        @Override
    1101        public CompoundShortDataset setByBoolean(final Object o, Dataset selection) {
    1102                setDirty();
    1103                if (o instanceof Dataset) {
    1104                        Dataset ds = (Dataset) o;
    1105                        final int length = ((Number) selection.sum()).intValue();
    1106                        if (length != ds.getSize()) {
    1107                                throw new IllegalArgumentException(
    1108                                                "Number of true items in selection does not match number of items in dataset");
    1109                        }
    1110
    1111                        IndexIterator iter = ds.getIterator();
    1112                        BooleanIterator biter = getBooleanIterator(selection);
    1113
    1114                        if (ds instanceof AbstractCompoundDataset) {
    1115                                if (isize != ds.getElementsPerItem()) {
    1116                                        throw new IllegalArgumentException("Input dataset is not compatible with slice");
    1117                                }
    1118
    1119                                while (biter.hasNext() && iter.hasNext()) {
    1120                                        for (int i = 0; i < isize; i++) {
    1121                                                data[biter.index + i] = (short) ds.getElementLongAbs(iter.index + i); // GET_ELEMENT_WITH_CAST
    1122                                        }
    1123                                }
    1124                        } else {
    1125                                while (biter.hasNext() && iter.hasNext()) {
    1126                                        data[biter.index] = (short) ds.getElementLongAbs(iter.index); // GET_ELEMENT_WITH_CAST
    1127                                        for (int i = 1; i < isize; i++) {
    1128                                                data[biter.index + i] = 0;
    1129                                        }
    1130                                }
    1131                        }
    1132                } else {
    1133                        try {
    1134                                final short[] vr = DTypeUtils.toShortArray(o, isize); // PRIM_TYPE // CLASS_TYPE
    1135
    1136                                final BooleanIterator biter = getBooleanIterator(selection);
    1137
    1138                                while (biter.hasNext()) {
    1139                                        for (int i = 0; i < isize; i++) {
    1140                                                data[biter.index + i] = vr[i];
    1141                                        }
    1142                                }
    1143                        } catch (IllegalArgumentException e) {
    1144                                throw new IllegalArgumentException("Object for setting is not a dataset or number");
    1145                        }
    1146                }
    1147                return this;
    1148        }
    1149
    1150        @Override
    1151        public CompoundShortDataset setBy1DIndex(final Object o, Dataset index) {
    1152                setDirty();
    1153                if (o instanceof Dataset) {
    1154                        Dataset ds = (Dataset) o;
    1155                        if (index.getSize() != ds.getSize()) {
    1156                                throw new IllegalArgumentException(
    1157                                                "Number of items in selection does not match number of items in dataset");
    1158                        }
    1159
    1160                        IndexIterator oiter = ds.getIterator();
    1161                        final IntegerIterator iter = new IntegerIterator(index, size, isize);
    1162
    1163                        if (ds instanceof AbstractCompoundDataset) {
    1164                                if (isize != ds.getElementsPerItem()) {
    1165                                        throw new IllegalArgumentException("Input dataset is not compatible with slice");
    1166                                }
    1167
    1168                                double[] temp = new double[isize];
    1169                                while (iter.hasNext() && oiter.hasNext()) {
    1170                                        ((AbstractCompoundDataset) ds).getDoubleArrayAbs(oiter.index, temp);
    1171                                        setDoubleArrayAbs(iter.index, temp);
    1172                                }
    1173                                while (iter.hasNext() && oiter.hasNext()) {
    1174                                        for (int i = 0; i < isize; i++) {
    1175                                                data[iter.index + i] = (short) ds.getElementLongAbs(oiter.index + i); // GET_ELEMENT_WITH_CAST
    1176                                        }
    1177                                }
    1178                        } else {
    1179                                while (iter.hasNext() && oiter.hasNext()) {
    1180                                        data[iter.index] = (short) ds.getElementLongAbs(oiter.index); // GET_ELEMENT_WITH_CAST
    1181                                        for (int i = 1; i < isize; i++) {
    1182                                                data[iter.index + i] = 0;
    1183                                        }
    1184                                }
    1185                        }
    1186                } else {
    1187                        try {
    1188                                final short[] vr = DTypeUtils.toShortArray(o, isize); // PRIM_TYPE // CLASS_TYPE
    1189
    1190                                final IntegerIterator iter = new IntegerIterator(index, size, isize);
    1191
    1192                                while (iter.hasNext()) {
    1193                                        setAbs(iter.index, vr);
    1194                                }
    1195                        } catch (IllegalArgumentException e) {
    1196                                throw new IllegalArgumentException("Object for setting is not a dataset or number");
    1197                        }
    1198                }
    1199                return this;
    1200        }
    1201
    1202        @Override
    1203        public CompoundShortDataset setByIndexes(final Object o, final Object... indexes) {
    1204                setDirty();
    1205                final IntegersIterator iter = new IntegersIterator(shape, indexes);
    1206                final int[] pos = iter.getPos();
    1207
    1208                if (o instanceof Dataset) {
    1209                        Dataset ds = (Dataset) o;
    1210                        if (ShapeUtils.calcSize(iter.getShape()) != ds.getSize()) {
    1211                                throw new IllegalArgumentException(
    1212                                                "Number of items in selection does not match number of items in dataset");
    1213                        }
    1214
    1215                        IndexIterator oiter = ds.getIterator();
    1216
    1217                        if (ds instanceof AbstractCompoundDataset) {
    1218                                if (isize != ds.getElementsPerItem()) {
    1219                                        throw new IllegalArgumentException("Input dataset is not compatible with slice");
    1220                                }
    1221
    1222                                double[] temp = new double[isize];
    1223                                while (iter.hasNext() && oiter.hasNext()) {
    1224                                        ((AbstractCompoundDataset) ds).getDoubleArray(temp, pos);
    1225                                        setDoubleArrayAbs(get1DIndex(pos), temp);
    1226                                }
    1227                        } else {
    1228                                while (iter.hasNext() && oiter.hasNext()) {
    1229                                        int n = get1DIndex(pos);
    1230                                        data[n] = (short) ds.getElementLongAbs(oiter.index); // GET_ELEMENT_WITH_CAST
    1231                                        for (int i = 1; i < isize; i++) {
    1232                                                data[n + i] = 0;
    1233                                        }
    1234                                }
    1235                        }
    1236                } else {
    1237                        try {
    1238                                final short[] vr = DTypeUtils.toShortArray(o, isize); // PRIM_TYPE // CLASS_TYPE
    1239
    1240                                while (iter.hasNext()) {
    1241                                        setAbs(get1DIndex(pos), vr);
    1242                                }
    1243                        } catch (IllegalArgumentException e) {
    1244                                throw new IllegalArgumentException("Object for setting is not a dataset or number");
    1245                        }
    1246                }
    1247                return this;
    1248        }
    1249
    1250        @Override
    1251        CompoundShortDataset setSlicedView(Dataset view, Dataset d) {
    1252                setDirty();
    1253                final BroadcastSelfIterator it = BroadcastSelfIterator.createIterator(view, d);
    1254
    1255                final int is = view.getElementsPerItem();
    1256
    1257                if (is > 1) {
    1258                        if (d.getElementsPerItem() == 1) {
    1259                                while (it.hasNext()) {
    1260                                        final short bv = (short) it.bLong; // PRIM_TYPE // BCAST_WITH_CAST d.getElementLongAbs(it.bIndex);
    1261                                        data[it.aIndex] = bv;
    1262                                        for (int j = 1; j < is; j++) {
    1263                                                data[it.aIndex + j] = bv;
    1264                                        }
    1265                                }
    1266                        } else {
    1267                                while (it.hasNext()) {
    1268                                        data[it.aIndex] = (short) it.bLong; // BCAST_WITH_CAST d.getElementLongAbs(it.bIndex);
    1269                                        for (int j = 1; j < is; j++) {
    1270                                                data[it.aIndex + j] = (short) d.getElementLongAbs(it.bIndex + j); // GET_ELEMENT_WITH_CAST
    1271                                        }
    1272                                }
    1273                        }
    1274                } else {
    1275                        while (it.hasNext()) {
    1276                                data[it.aIndex] = (short) it.bLong; // BCAST_WITH_CAST d.getElementLongAbs(it.bIndex);
    1277                        }
    1278                }
    1279                return this;
    1280        }
    1281
    1282        @Override
    1283        public CompoundShortDataset setSlice(final Object o, final IndexIterator siter) {
    1284                setDirty();
    1285                if (o instanceof IDataset) {
    1286                        final IDataset ds = (IDataset) o;
    1287                        final int[] oshape = ds.getShape();
    1288
    1289                        if (!ShapeUtils.areShapesCompatible(siter.getShape(), oshape)) {
    1290                                throw new IllegalArgumentException(String.format(
    1291                                                "Input dataset is not compatible with slice: %s cf %s", Arrays.toString(oshape),
    1292                                                Arrays.toString(siter.getShape())));
    1293                        }
    1294
    1295                        if (ds instanceof Dataset) {
    1296                                final Dataset ads = (Dataset) ds;
    1297                                IndexIterator oiter = ads.getIterator();
    1298
    1299                                if (ds instanceof AbstractCompoundDataset) {
    1300                                        if (isize != ads.getElementsPerItem()) {
    1301                                                throw new IllegalArgumentException("Input dataset is not compatible with slice");
    1302                                        }
    1303
    1304                                        while (siter.hasNext() && oiter.hasNext()) {
    1305                                                for (int i = 0; i < isize; i++) {
    1306                                                        data[siter.index + i] = (short) ads.getElementLongAbs(oiter.index + i); // GET_ELEMENT_WITH_CAST
    1307                                                }
    1308                                        }
    1309                                } else {
    1310                                        while (siter.hasNext() && oiter.hasNext()) {
    1311                                                data[siter.index] = (short) ads.getElementLongAbs(oiter.index); // GET_ELEMENT_WITH_CAST
    1312                                                for (int i = 1; i < isize; i++) {
    1313                                                        data[siter.index + i] = 0;
    1314                                                }
    1315                                        }
    1316                                }
    1317                        } else {
    1318                                final IndexIterator oiter = new PositionIterator(oshape);
    1319                                final int[] pos = oiter.getPos();
    1320
    1321                                if (ds.getElementsPerItem() == 1) {
    1322                                        while (siter.hasNext() && oiter.hasNext()) {
    1323                                                data[siter.index] = ds.getShort(pos); // PRIM_TYPE
    1324                                                for (int i = 1; i < isize; i++) {
    1325                                                        data[siter.index + i] = 0;
    1326                                                }
    1327                                        }
    1328                                } else {
    1329                                        while (siter.hasNext() && oiter.hasNext()) {
    1330                                                final short[] val = DTypeUtils.toShortArray(ds.getObject(pos), isize); // PRIM_TYPE // CLASS_TYPE
    1331                                                for (int i = 0; i < isize; i++) {
    1332                                                        data[siter.index + i] = val[i];
    1333                                                }
    1334                                        }
    1335                                }
    1336                        }
    1337                } else {
    1338                        try {
    1339                                final short[] vr = DTypeUtils.toShortArray(o, isize); // PRIM_TYPE // CLASS_TYPE
    1340
    1341                                while (siter.hasNext()) {
    1342                                        for (int i = 0; i < isize; i++) {
    1343                                                data[siter.index + i] = vr[i];
    1344                                        }
    1345                                }
    1346                        } catch (IllegalArgumentException e) {
    1347                                throw new IllegalArgumentException("Object for setting slice is not a dataset or number");
    1348                        }
    1349                }
    1350                return this;
    1351        }
    1352
    1353        @Override
    1354        public void copyItemsFromAxes(final int[] pos, final boolean[] axes, final Dataset dest) {
    1355                short[] ddata = (short[]) dest.getBuffer(); // PRIM_TYPE
    1356
    1357                if (dest.getElementsPerItem() != isize) {
    1358                        throw new IllegalArgumentException(String.format(
    1359                                        "Destination dataset is incompatible as it has %d elements per item not %d",
    1360                                        dest.getElementsPerItem(), isize));
    1361                }
    1362
    1363                SliceIterator siter = getSliceIteratorFromAxes(pos, axes);
    1364                int[] sshape = ShapeUtils.squeezeShape(siter.getShape(), false);
    1365
    1366                IndexIterator diter = dest.getSliceIterator(null, sshape, null);
    1367
    1368                if (ddata.length < ShapeUtils.calcSize(sshape)) {
    1369                        throw new IllegalArgumentException("destination array is not large enough");
    1370                }
    1371
    1372                dest.setDirty();
    1373                while (siter.hasNext() && diter.hasNext()) {
    1374                        for (int i = 0; i < isize; i++) {
    1375                                ddata[diter.index + i] = data[siter.index + i];
    1376                        }
    1377                }
    1378        }
    1379
    1380        @Override
    1381        public void setItemsOnAxes(final int[] pos, final boolean[] axes, final Object src) {
    1382                setDirty();
    1383                short[] sdata = (short[]) src; // PRIM_TYPE
    1384
    1385                SliceIterator siter = getSliceIteratorFromAxes(pos, axes);
    1386
    1387                if (sdata.length < ShapeUtils.calcSize(siter.getShape())) {
    1388                        throw new IllegalArgumentException("source array is not large enough");
    1389                }
    1390
    1391                for (int i = 0; siter.hasNext(); i++) {
    1392                        for (int j = 0; j < isize; j++) {
    1393                                data[siter.index + j] = sdata[isize * i + j];
    1394                        }
    1395                }
    1396        }
    1397
    1398        @Override
    1399        public boolean containsNans() {
    1400                return false;
    1401        }
    1402
    1403        @Override
    1404        public boolean containsInfs() {
    1405                return false;
    1406        }
    1407
    1408        @Override
    1409        public boolean containsInvalidNumbers() {
    1410                return false;
    1411        }
    1412
    1413        @Override
    1414        public CompoundShortDataset iadd(final Object b) {
    1415                setDirty();
    1416                Dataset bds = b instanceof Dataset ? (Dataset) b : DatasetFactory.createFromObject(b);
    1417                boolean useLong = bds.getElementClass().equals(Long.class);
    1418                int is = bds.getElementsPerItem();
    1419                if (bds.getSize() == 1) {
    1420                        final IndexIterator it = getIterator();
    1421                        final int bOffset = bds.getOffset();
    1422                        if (is == 1) {
    1423                                if (useLong) {
    1424                                        final long lb = bds.getElementLongAbs(bOffset);
    1425                                        while (it.hasNext()) {
    1426                                                for (int i = 0; i < isize; i++) {
    1427                                                        data[it.index + i] += lb;
    1428                                                }
    1429                                        }
    1430                                } else {
    1431                                        final double db = bds.getElementDoubleAbs(bOffset);
    1432                                        while (it.hasNext()) {
    1433                                                for (int i = 0; i < isize; i++) {
    1434                                                        data[it.index + i] += db;
    1435                                                }
    1436                                        }
    1437                                }
    1438                        } else if (is == isize) {
    1439                                if (useLong) {
    1440                                        while (it.hasNext()) {
    1441                                                for (int i = 0; i < isize; i++) {
    1442                                                        data[it.index + i] += bds.getElementLongAbs(i);
    1443                                                }
    1444                                        }
    1445                                } else {
    1446                                        while (it.hasNext()) {
    1447                                                for (int i = 0; i < isize; i++) {
    1448                                                        data[it.index + i] += bds.getElementDoubleAbs(i);
    1449                                                }
    1450                                        }
    1451                                }
    1452                        } else {
    1453                                throw new IllegalArgumentException("Argument does not have same number of elements per item or is not a non-compound dataset");
    1454                        }
    1455                } else {
    1456                        final BroadcastSelfIterator it = BroadcastSelfIterator.createIterator(this, bds);
    1457                        it.setOutputDouble(!useLong);
    1458                        if (is == 1) {
    1459                                if (useLong) {
    1460                                        while (it.hasNext()) {
    1461                                                final long lb = it.bLong;
    1462                                                data[it.aIndex] += lb;
    1463                                                for (int i = 1; i < isize; i++) {
    1464                                                        data[it.aIndex + i] += lb;
    1465                                                }
    1466                                        }
    1467                                } else {
    1468                                        while (it.hasNext()) {
    1469                                                final double db = it.bDouble;
    1470                                                data[it.aIndex] += db;
    1471                                                for (int i = 1; i < isize; i++) {
    1472                                                        data[it.aIndex + i] += db;
    1473                                                }
    1474                                        }
    1475                                }
    1476                        } else if (is == isize) {
    1477                                if (useLong) {
    1478                                        while (it.hasNext()) {
    1479                                                data[it.aIndex] += it.bLong;
    1480                                                for (int i = 1; i < isize; i++) {
    1481                                                        data[it.aIndex + i] += bds.getElementLongAbs(it.bIndex + i);
    1482                                                }
    1483                                        }
    1484                                } else {
    1485                                        while (it.hasNext()) {
    1486                                                data[it.aIndex] += it.bDouble;
    1487                                                for (int i = 1; i < isize; i++) {
    1488                                                        data[it.aIndex + i] += bds.getElementDoubleAbs(it.bIndex + i);
    1489                                                }
    1490                                        }
    1491                                }
    1492                        } else {
    1493                                throw new IllegalArgumentException("Argument does not have same number of elements per item or is not a non-compound dataset");
    1494                        }
    1495                }
    1496                return this;
    1497        }
    1498
    1499        @Override
    1500        public CompoundShortDataset isubtract(final Object b) {
    1501                setDirty();
    1502                Dataset bds = b instanceof Dataset ? (Dataset) b : DatasetFactory.createFromObject(b);
    1503                boolean useLong = bds.getElementClass().equals(Long.class);
    1504                int is = bds.getElementsPerItem();
    1505                if (bds.getSize() == 1) {
    1506                        final IndexIterator it = getIterator();
    1507                        final int bOffset = bds.getOffset();
    1508                        if (is == 1) {
    1509                                if (useLong) {
    1510                                        final long lb = bds.getElementLongAbs(bOffset);
    1511                                        while (it.hasNext()) {
    1512                                                for (int i = 0; i < isize; i++) {
    1513                                                        data[it.index + i] -= lb;
    1514                                                }
    1515                                        }
    1516                                } else {
    1517                                        final double db = bds.getElementDoubleAbs(bOffset);
    1518                                        while (it.hasNext()) {
    1519                                                for (int i = 0; i < isize; i++) {
    1520                                                        data[it.index + i] -= db;
    1521                                                }
    1522                                        }
    1523                                }
    1524                        } else if (is == isize) {
    1525                                if (useLong) {
    1526                                        while (it.hasNext()) {
    1527                                                for (int i = 0; i < isize; i++) {
    1528                                                        data[it.index + i] -= bds.getElementLongAbs(i);
    1529                                                }
    1530                                        }
    1531                                } else {
    1532                                        while (it.hasNext()) {
    1533                                                for (int i = 0; i < isize; i++) {
    1534                                                        data[it.index + i] -= bds.getElementDoubleAbs(i);
    1535                                                }
    1536                                        }
    1537                                }
    1538                        } else {
    1539                                throw new IllegalArgumentException("Argument does not have same number of elements per item or is not a non-compound dataset");
    1540                        }
    1541                } else {
    1542                        final BroadcastSelfIterator it = BroadcastSelfIterator.createIterator(this, bds);
    1543                        it.setOutputDouble(!useLong);
    1544                        if (is == 1) {
    1545                                if (useLong) {
    1546                                        while (it.hasNext()) {
    1547                                                final long lb = it.bLong;
    1548                                                data[it.aIndex] += lb;
    1549                                                for (int i = 1; i < isize; i++) {
    1550                                                        data[it.aIndex + i] -= lb;
    1551                                                }
    1552                                        }
    1553                                } else {
    1554                                        while (it.hasNext()) {
    1555                                                final double db = it.bDouble;
    1556                                                data[it.aIndex] += db;
    1557                                                for (int i = 1; i < isize; i++) {
    1558                                                        data[it.aIndex + i] -= db;
    1559                                                }
    1560                                        }
    1561                                }
    1562                        } else if (is == isize) {
    1563                                if (useLong) {
    1564                                        while (it.hasNext()) {
    1565                                                data[it.aIndex] += it.bLong;
    1566                                                for (int i = 1; i < isize; i++) {
    1567                                                        data[it.aIndex + i] -= bds.getElementLongAbs(it.bIndex + i);
    1568                                                }
    1569                                        }
    1570                                } else {
    1571                                        while (it.hasNext()) {
    1572                                                data[it.aIndex] += it.bDouble;
    1573                                                for (int i = 1; i < isize; i++) {
    1574                                                        data[it.aIndex + i] -= bds.getElementDoubleAbs(it.bIndex + i);
    1575                                                }
    1576                                        }
    1577                                }
    1578                        } else {
    1579                                throw new IllegalArgumentException("Argument does not have same number of elements per item or is not a non-compound dataset");
    1580                        }
    1581                }
    1582                return this;
    1583        }
    1584
    1585        @Override
    1586        public CompoundShortDataset imultiply(final Object b) {
    1587                setDirty();
    1588                Dataset bds = b instanceof Dataset ? (Dataset) b : DatasetFactory.createFromObject(b);
    1589                boolean useLong = bds.getElementClass().equals(Long.class);
    1590                int is = bds.getElementsPerItem();
    1591                if (bds.getSize() == 1) {
    1592                        final IndexIterator it = getIterator();
    1593                        final int bOffset = bds.getOffset();
    1594                        if (useLong) {
    1595                                if (is == 1) {
    1596                                        final long lb = bds.getElementLongAbs(bOffset);
    1597                                        while (it.hasNext()) {
    1598                                                for (int i = 0; i < isize; i++) {
    1599                                                        data[it.index + i] *= lb;
    1600                                                }
    1601                                        }
    1602                                } else if (is == isize) {
    1603                                        while (it.hasNext()) {
    1604                                                for (int i = 0; i < isize; i++) {
    1605                                                        data[it.index + i] *= bds.getElementLongAbs(i);
    1606                                                }
    1607                                        }
    1608                                } else {
    1609                                        throw new IllegalArgumentException("Argument does not have same number of elements per item or is not a non-compound dataset");
    1610                                }
    1611                        } else {
    1612                                if (is == 1) {
    1613                                        final double db = bds.getElementDoubleAbs(bOffset);
    1614                                        while (it.hasNext()) {
    1615                                                for (int i = 0; i < isize; i++) {
    1616                                                        data[it.index + i] *= db;
    1617                                                }
    1618                                        }
    1619                                } else if (is == isize) {
    1620                                        while (it.hasNext()) {
    1621                                                for (int i = 0; i < isize; i++) {
    1622                                                        data[it.index + i] *= bds.getElementDoubleAbs(i);
    1623                                                }
    1624                                        }
    1625                                } else {
    1626                                        throw new IllegalArgumentException("Argument does not have same number of elements per item or is not a non-compound dataset");
    1627                                }
    1628                        }
    1629                } else {
    1630                        final BroadcastSelfIterator it = BroadcastSelfIterator.createIterator(this, bds);
    1631                        it.setOutputDouble(!useLong);
    1632                        if (useLong) {
    1633                                if (is == 1) {
    1634                                        while (it.hasNext()) {
    1635                                                final double lb = it.bLong;
    1636                                                for (int i = 0; i < isize; i++) {
    1637                                                        data[it.aIndex + i] *= lb;
    1638                                                }
    1639                                        }
    1640                                } else if (is == isize) {
    1641                                        while (it.hasNext()) {
    1642                                                data[it.aIndex] *= it.bLong;
    1643                                                for (int i = 1; i < isize; i++) {
    1644                                                        data[it.aIndex + i] *= bds.getElementLongAbs(it.bIndex + i);
    1645                                                }
    1646                                        }
    1647                                } else {
    1648                                        throw new IllegalArgumentException("Argument does not have same number of elements per item or is not a non-compound dataset");
    1649                                }
    1650                        } else {
    1651                                if (is == 1) {
    1652                                        while (it.hasNext()) {
    1653                                                final double db = it.bDouble;
    1654                                                for (int i = 0; i < isize; i++) {
    1655                                                        data[it.aIndex + i] *= db;
    1656                                                }
    1657                                        }
    1658                                } else if (is == isize) {
    1659                                        while (it.hasNext()) {
    1660                                                data[it.aIndex] *= it.bDouble;
    1661                                                for (int i = 1; i < isize; i++) {
    1662                                                        data[it.aIndex + i] *= bds.getElementDoubleAbs(it.bIndex + i);
    1663                                                }
    1664                                        }
    1665                                } else {
    1666                                        throw new IllegalArgumentException("Argument does not have same number of elements per item or is not a non-compound dataset");
    1667                                }
    1668                        }
    1669                }
    1670                return this;
    1671        }
    1672
    1673        @Override
    1674        public CompoundShortDataset idivide(final Object b) {
    1675                setDirty();
    1676                Dataset bds = b instanceof Dataset ? (Dataset) b : DatasetFactory.createFromObject(b);
    1677                boolean useLong = bds.getElementClass().equals(Long.class);
    1678                int is = bds.getElementsPerItem();
    1679                if (bds.getSize() == 1) {
    1680                        final IndexIterator it = getIterator();
    1681                        final int bOffset = bds.getOffset();
    1682                        if (useLong) {
    1683                                if (is == 1) {
    1684                                        final long lb = bds.getElementLongAbs(bOffset);
    1685                                        if (lb == 0) { // INT_USE
    1686                                                fill(0); // INT_USE
    1687                                        } else { // INT_USE
    1688                                        while (it.hasNext()) {
    1689                                                for (int i = 0; i < isize; i++) {
    1690                                                        data[it.index + i] /= lb;
    1691                                                }
    1692                                        }
    1693                                        } // INT_USE
    1694                                } else if (is == isize) {
    1695                                        while (it.hasNext()) {
    1696                                                for (int i = 0; i < isize; i++) {
    1697                                                        final long lb = bds.getElementLongAbs(i);
    1698                                try {
    1699                                                                data[it.index + i] /= lb; // INT_EXCEPTION
    1700                                } catch (ArithmeticException e) {
    1701                                        data[it.index + i] = 0;
    1702                                }
    1703                                                }
    1704                                        }
    1705                                } else {
    1706                                        throw new IllegalArgumentException("Argument does not have same number of elements per item or is not a non-compound dataset");
    1707                                }
    1708                        } else {
    1709                                if (is == 1) {
    1710                                        final double db = bds.getElementDoubleAbs(bOffset);
    1711                                        if (db == 0) { // INT_USE
    1712                                                fill(0); // INT_USE
    1713                                        } else { // INT_USE
    1714                                        while (it.hasNext()) {
    1715                                                for (int i = 0; i < isize; i++) {
    1716                                                        data[it.index + i] /= db;
    1717                                                }
    1718                                        }
    1719                                        } // INT_USE
    1720                                } else if (is == isize) {
    1721                                        while (it.hasNext()) {
    1722                                                for (int i = 0; i < isize; i++) {
    1723                                                        final double db = bds.getElementDoubleAbs(i);
    1724                                try {
    1725                                                                data[it.index + i] /= db; // INT_EXCEPTION
    1726                                } catch (ArithmeticException e) {
    1727                                        data[it.index + i] = 0;
    1728                                }
    1729                                                }
    1730                                        }
    1731                                } else {
    1732                                        throw new IllegalArgumentException("Argument does not have same number of elements per item or is not a non-compound dataset");
    1733                                }
    1734                        }
    1735                } else {
    1736                        final BroadcastSelfIterator it = BroadcastSelfIterator.createIterator(this, bds);
    1737                        it.setOutputDouble(!useLong);
    1738                        if (useLong) {
    1739                                if (is == 1) {
    1740                                        while (it.hasNext()) {
    1741                                                final long lb = it.bLong;
    1742                                                if (lb == 0) { // INT_USE
    1743                                                        for (int i = 0; i < isize; i++) { // INT_USE
    1744                                                                data[it.aIndex + i] = 0; // INT_USE
    1745                                                        }// INT_USE
    1746                                                } else { // INT_USE
    1747                                                for (int i = 0; i < isize; i++) {
    1748                                                        data[it.aIndex + i] /= lb;
    1749                                                }
    1750                                                } // INT_USE
    1751                                        }
    1752                                } else if (is == isize) {
    1753                                        while (it.hasNext()) {
    1754                                                for (int i = 0; i < isize; i++) {
    1755                                                        final long lb = bds.getElementLongAbs(it.bIndex + i);
    1756                                try {
    1757                                                                data[it.aIndex + i] /= lb; // INT_EXCEPTION
    1758                                } catch (ArithmeticException e) {
    1759                                        data[it.aIndex + i] = 0;
    1760                                }
    1761                                                }
    1762                                        }
    1763                                } else {
    1764                                        throw new IllegalArgumentException("Argument does not have same number of elements per item or is not a non-compound dataset");
    1765                                }
    1766                        } else {
    1767                                if (is == 1) {
    1768                                        while (it.hasNext()) {
    1769                                                final double db = it.bDouble;
    1770                                                if (db == 0) { // INT_USE
    1771                                                        for (int i = 0; i < isize; i++) { // INT_USE
    1772                                                                data[it.aIndex + i] = 0; // INT_USE
    1773                                                        }// INT_USE
    1774                                                } else { // INT_USE
    1775                                                for (int i = 0; i < isize; i++) {
    1776                                                        data[it.aIndex + i] /= db;
    1777                                                }
    1778                                                } // INT_USE
    1779                                        }
    1780                                } else if (is == isize) {
    1781                                        while (it.hasNext()) {
    1782                                                for (int i = 0; i < isize; i++) {
    1783                                                        final double db = bds.getElementDoubleAbs(it.bIndex + i);
    1784                                try {
    1785                                                                data[it.aIndex + i] /= db; // INT_EXCEPTION
    1786                                } catch (ArithmeticException e) {
    1787                                        data[it.aIndex + i] = 0;
    1788                                }
    1789                                                }
    1790                                        }
    1791                                } else {
    1792                                        throw new IllegalArgumentException("Argument does not have same number of elements per item or is not a non-compound dataset");
    1793                                }
    1794                        }
    1795                }
    1796                return this;
    1797        }
    1798
    1799        @Override
    1800        public CompoundShortDataset ifloor() {
    1801                return this;
    1802        }
    1803
    1804        @Override
    1805        public CompoundShortDataset iremainder(final Object b) {
    1806                setDirty();
    1807                Dataset bds = b instanceof Dataset ? (Dataset) b : DatasetFactory.createFromObject(b);
    1808                boolean useLong = bds.getElementClass().equals(Long.class);
    1809                int is = bds.getElementsPerItem();
    1810                if (bds.getSize() == 1) {
    1811                        final IndexIterator it = getIterator();
    1812                        final int bOffset = bds.getOffset();
    1813                        if (useLong) {
    1814                                if (is == 1) {
    1815                                        final long lb = bds.getElementLongAbs(bOffset);
    1816                                        if (lb == 0) { // INT_USE
    1817                                                fill(0); // INT_USE
    1818                                        } else { // INT_USE
    1819                                        while (it.hasNext()) {
    1820                                                for (int i = 0; i < isize; i++) {
    1821                                                        data[it.index + i] %= lb;
    1822                                                }
    1823                                        }
    1824                                        } // INT_USE
    1825                                } else if (is == isize) {
    1826                                        while (it.hasNext()) {
    1827                                                for (int i = 0; i < isize; i++) {
    1828                                try {
    1829                                                                data[it.index + i] %= bds.getElementLongAbs(i); // INT_EXCEPTION
    1830                                } catch (ArithmeticException e) {
    1831                                        data[it.index + i] = 0;
    1832                                }
    1833                                                }
    1834                                        }
    1835                                } else {
    1836                                        throw new IllegalArgumentException("Argument does not have same number of elements per item or is not a non-compound dataset");
    1837                                }
    1838                        } else {
    1839                                if (is == 1) {
    1840                                        final double db = bds.getElementDoubleAbs(bOffset);
    1841                                        if (db == 0) { // INT_USE
    1842                                                fill(0); // INT_USE
    1843                                        } else { // INT_USE
    1844                                        while (it.hasNext()) {
    1845                                                for (int i = 0; i < isize; i++) {
    1846                                                        data[it.index + i] %= db;
    1847                                                }
    1848                                        }
    1849                                        } // INT_USE
    1850                                } else if (is == isize) {
    1851                                        while (it.hasNext()) {
    1852                                                for (int i = 0; i < isize; i++) {
    1853                                try {
    1854                                                                data[it.index + i] %= bds.getElementDoubleAbs(i); // INT_EXCEPTION
    1855                                } catch (ArithmeticException e) {
    1856                                        data[it.index + i] = 0;
    1857                                }
    1858                                                }
    1859                                        }
    1860                                } else {
    1861                                        throw new IllegalArgumentException("Argument does not have same number of elements per item or is not a non-compound dataset");
    1862                                }
    1863                        }
    1864                } else {
    1865                        final BroadcastSelfIterator it = BroadcastSelfIterator.createIterator(this, bds);
    1866                        it.setOutputDouble(!useLong);
    1867                        if (useLong) {
    1868                                if (is == 1) {
    1869                                        while (it.hasNext()) {
    1870                                                final long lb = it.bLong;
    1871                                                if (lb == 0) { // INT_USE
    1872                                                        for (int i = 0; i < isize; i++) // INT_USE
    1873                                                                data[it.aIndex + i] = 0; // INT_USE
    1874                                                } else { // INT_USE
    1875                                                for (int i = 0; i < isize; i++)
    1876                                                        data[it.aIndex + i] %= lb;
    1877                                                } // INT_USE
    1878                                        }
    1879                                } else if (is == isize) {
    1880                                        while (it.hasNext()) {
    1881                                                for (int i = 0; i < isize; i++) {
    1882                                                        final long lb = bds.getElementLongAbs(it.bIndex + i);
    1883                                try {
    1884                                                                data[it.aIndex + i] %= lb; // INT_EXCEPTION
    1885                                } catch (ArithmeticException e) {
    1886                                        data[it.aIndex + i] = 0;
    1887                                }
    1888                                                }
    1889                                        }
    1890                                } else {
    1891                                        throw new IllegalArgumentException("Argument does not have same number of elements per item or is not a non-compound dataset");
    1892                                }
    1893                        } else {
    1894                                if (is == 1) {
    1895                                        while (it.hasNext()) {
    1896                                                final double db = it.bDouble;
    1897                                                if (db == 0) { // INT_USE
    1898                                                        for (int i = 0; i < isize; i++) // INT_USE
    1899                                                                data[it.aIndex + i] = 0; // INT_USE
    1900                                                } else { // INT_USE
    1901                                                for (int i = 0; i < isize; i++) {
    1902                                                        data[it.aIndex + i] %= db;
    1903                                                }
    1904                                                } // INT_USE
    1905                                        }
    1906                                } else if (is == isize) {
    1907                                        while (it.hasNext()) {
    1908                                                for (int i = 0; i < isize; i++) {
    1909                                                        final double db = bds.getElementDoubleAbs(it.bIndex + i);
    1910                                try {
    1911                                                                data[it.aIndex + i] %= db; // INT_EXCEPTION
    1912                                } catch (ArithmeticException e) {
    1913                                        data[it.aIndex + i] = 0;
    1914                                }
    1915                                                }
    1916                                        }
    1917                                } else {
    1918                                        throw new IllegalArgumentException("Argument does not have same number of elements per item or is not a non-compound dataset");
    1919                                }
    1920                        }
    1921                }
    1922                return this;
    1923        }
    1924
    1925        @Override
    1926        public CompoundShortDataset ipower(final Object b) {
    1927                setDirty();
    1928                Dataset bds = b instanceof Dataset ? (Dataset) b : DatasetFactory.createFromObject(b);
    1929                final int is = bds.getElementsPerItem();
    1930                if (bds.getSize() == 1) {
    1931                        final int bOffset = bds.getOffset();
    1932                        final double vr = bds.getElementDoubleAbs(bOffset);
    1933                        final IndexIterator it = getIterator();
    1934                        if (bds.isComplex()) {
    1935                                final double vi = bds.getElementDoubleAbs(bOffset + 1);
    1936                                if (vi == 0) {
    1937                                        while (it.hasNext()) {
    1938                                                for (int i = 0; i < isize; i++) {
    1939                                                        final double v = Math.pow(data[it.index + i], vr);
    1940                                                        if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE
    1941                                                                data[it.index + i] = 0; // INT_USE
    1942                                                        } else { // INT_USE
    1943                                                        data[it.index + i] = (short) (long) v; // PRIM_TYPE_LONG // ADD_CAST
    1944                                                        } // INT_USE
    1945                                                }
    1946                                        }
    1947                                } else {
    1948                                        final Complex zv = new Complex(vr, vi);
    1949                                        while (it.hasNext()) {
    1950                                                for (int i = 0; i < isize; i++) {
    1951                                                        Complex zd = new Complex(data[it.index + i], 0);
    1952                                                        final double v = zd.pow(zv).getReal();
    1953                                                        if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE
    1954                                                                data[it.index + i] = 0; // INT_USE
    1955                                                        } else { // INT_USE
    1956                                                        data[it.index + i] = (short) (long) v; // PRIM_TYPE_LONG // ADD_CAST
    1957                                                        } // INT_USE
    1958                                                }
    1959                                        }
    1960                                }
    1961                        } else if (is == 1) {
    1962                                while (it.hasNext()) {
    1963                                        for (int i = 0; i < isize; i++) {
    1964                                                final double v = Math.pow(data[it.index + i], vr);
    1965                                                if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE
    1966                                                        data[it.index + i] = 0; // INT_USE
    1967                                                } else { // INT_USE
    1968                                                data[it.index + i] = (short) (long) v; // PRIM_TYPE_LONG // ADD_CAST
    1969                                                } // INT_USE
    1970                                        }
    1971                                }
    1972                        } else if (is == isize) {
    1973                                while (it.hasNext()) {
    1974                                        for (int i = 0; i < isize; i++) {
    1975                                                final double v = Math.pow(data[it.index + i], bds.getElementDoubleAbs(i));
    1976                                                if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE
    1977                                                        data[it.index + i] = 0; // INT_USE
    1978                                                } else { // INT_USE
    1979                                                data[it.index + i] = (short) (long) v; // PRIM_TYPE_LONG // ADD_CAST
    1980                                                } // INT_USE
    1981                                        }
    1982                                }
    1983                        }
    1984                } else {
    1985                        final BroadcastIterator it = BroadcastIterator.createIterator(this, bds);
    1986                        it.setOutputDouble(true);
    1987                        if (bds.isComplex()) {
    1988                                while (it.hasNext()) {
    1989                                        final Complex zv = new Complex(it.bDouble, bds.getElementDoubleAbs(it.bIndex + 1));
    1990                                        double v = new Complex(it.aDouble, 0).pow(zv).getReal();
    1991                                        if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE
    1992                                                data[it.aIndex] = 0; // INT_USE
    1993                                        } else { // INT_USE
    1994                                        data[it.aIndex] = (short) (long) v; // PRIM_TYPE_LONG // ADD_CAST
    1995                                        } // INT_USE
    1996                                        for (int i = 1; i < isize; i++) {
    1997                                                v = new Complex(data[it.aIndex + i], 0).pow(zv).getReal();
    1998                                                if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE
    1999                                                        data[it.aIndex + i] = 0; // INT_USE
    2000                                                } else { // INT_USE
    2001                                                data[it.aIndex + i] = (short) (long) v; // PRIM_TYPE_LONG // ADD_CAST
    2002                                                } // INT_USE
    2003                                        }
    2004                                }
    2005                        } else {
    2006                                while (it.hasNext()) {
    2007                                        double v = Math.pow(it.aDouble, it.bDouble);
    2008                                        if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE
    2009                                                data[it.aIndex] = 0; // INT_USE
    2010                                        } else { // INT_USE
    2011                                        data[it.aIndex] = (short) (long) v; // PRIM_TYPE_LONG // ADD_CAST
    2012                                        } // INT_USE
    2013                                        for (int i = 1; i < isize; i++) {
    2014                                                v = Math.pow(data[it.aIndex + i], bds.getElementDoubleAbs(it.bIndex + i));
    2015                                                if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE
    2016                                                        data[it.aIndex + i] = 0; // INT_USE
    2017                                                } else { // INT_USE
    2018                                                data[it.aIndex + i] = (short) (long) v; // PRIM_TYPE_LONG // ADD_CAST
    2019                                                } // INT_USE
    2020                                        }
    2021                                }
    2022                        }
    2023                }
    2024                return this;
    2025        }
    2026
    2027        @Override
    2028        public double residual(final Object b, final Dataset w, boolean ignoreNaNs) {
    2029                Dataset bds = b instanceof Dataset ? (Dataset) b : DatasetFactory.createFromObject(b);
    2030                final BroadcastIterator it = BroadcastIterator.createIterator(this, bds);
    2031                it.setOutputDouble(true);
    2032                double sum = 0;
    2033                double comp = 0;
    2034                final int bis = bds.getElementsPerItem();
    2035
    2036                if (bis == 1) {
    2037                        if (w == null) {
    2038                                while (it.hasNext()) {
    2039                                        final double db = it.bDouble;
    2040                                        double diff = it.aDouble - db;
    2041                                        double err = diff * diff - comp;
    2042                                        double temp = sum + err;
    2043                                        comp = (temp - sum) - err;
    2044                                        sum = temp;
    2045                                        for (int i = 1; i < isize; i++) {
    2046                                                diff = data[it.aIndex + i] - db;
    2047                                                err = diff * diff - comp;
    2048                                                temp = sum + err;
    2049                                                comp = (temp - sum) - err;
    2050                                                sum = temp;
    2051                                        }
    2052                                }
    2053                        } else {
    2054                                IndexIterator itw = w.getIterator();
    2055                                while (it.hasNext() && itw.hasNext()) {
    2056                                        final double db = it.bDouble;
    2057                                        double diff = it.aDouble - db;
    2058                                        final double dw = w.getElementDoubleAbs(itw.index);
    2059                                        double err = diff * diff * dw - comp;
    2060                                        double temp = sum + err;
    2061                                        comp = (temp - sum) - err;
    2062                                        sum = temp;
    2063                                        for (int i = 1; i < isize; i++) {
    2064                                                diff = data[it.aIndex + i] - db;
    2065                                                err = diff * diff * dw - comp;
    2066                                                temp = sum + err;
    2067                                                comp = (temp - sum) - err;
    2068                                                sum = temp;
    2069                                        }
    2070                                }
    2071                        }
    2072                } else {
    2073                        if (w == null) {
    2074                                while (it.hasNext()) {
    2075                                        double diff = it.aDouble - it.bDouble;
    2076                                        double err = diff * diff - comp;
    2077                                        double temp = sum + err;
    2078                                        comp = (temp - sum) - err;
    2079                                        sum = temp;
    2080                                        for (int i = 1; i < isize; i++) {
    2081                                                diff = data[it.aIndex + i] - bds.getElementDoubleAbs(it.bIndex + i);
    2082                                                err = diff * diff - comp;
    2083                                                temp = sum + err;
    2084                                                comp = (temp - sum) - err;
    2085                                                sum = temp;
    2086                                        }
    2087                                }
    2088                        } else {
    2089                                IndexIterator itw = w.getIterator();
    2090                                while (it.hasNext() && itw.hasNext()) {
    2091                                        double diff = it.aDouble - it.bDouble;
    2092                                        final double dw = w.getElementDoubleAbs(itw.index);
    2093                                        double err = diff * diff * dw - comp;
    2094                                        double temp = sum + err;
    2095                                        comp = (temp - sum) - err;
    2096                                        sum = temp;
    2097                                        for (int i = 1; i < isize; i++) {
    2098                                                diff = data[it.aIndex + i] - bds.getElementDoubleAbs(it.bIndex + i);
    2099                                                err = diff * diff * dw - comp;
    2100                                                temp = sum + err;
    2101                                                comp = (temp - sum) - err;
    2102                                                sum = temp;
    2103                                        }
    2104                                }
    2105                        }
    2106                }
    2107                return sum;
    2108        }
    2109}