Package net.algart.math

Examples of net.algart.math.IPoint


                }
            }
            Matrix<DoubleArray> cyclic = matrix.subMatr(submatrixPos, submatrixDim,
                Matrix.ContinuationMode.CYCLIC);

            IPoint from = IPoint.valueOf(cyclic.subMatrixFrom());
            IPoint to = IPoint.valueOf(cyclic.subMatrixTo());
            Matrix<DoubleArray> pseudoCyclic = matrix.subMatrix(from.coordinates(), to.coordinates(),
                Matrix.ContinuationMode.PSEUDO_CYCLIC);
            System.out.println(testCount + ": testing " + from + ".." + to
                + " inside " + JArrays.toString(matrix.dimensions(), "x", 100));
            for (int shiftIndex = 0; shiftIndex < 1000; shiftIndex++) {
                boolean showResults = shiftIndex < 10;
                long[] shift = new long[cyclic.dimCount()];
                for (int k = 0; k < shift.length; k++) {
                    shift[k] = cyclic.size() == 0 ? rnd.nextInt(10) :
                        showResults && k == shift.length - 1 ?
                            (cyclic.dim(k) + shiftIndex - 1) % cyclic.dim(k) :
                            Math.round(rnd.nextDouble() * (cyclic.dim(k) - 1));
                }
                IPoint p = IPoint.valueOf(shift);
                IPoint shifted = from.add(p);
                long cyclicIndex = matrix.cyclicIndex(shifted.coordinates());
                long pseudoCyclicIndex = matrix.pseudoCyclicIndex(shifted.coordinates());
                long oneDimensional = shifted.toOneDimensional(matrix.dimensions(), true);
                if (pseudoCyclicIndex != oneDimensional)
                    throw new AssertionError("Bug A detected: pseudo-cyclic index calculated differently "
                        + "in Matrix and IPoint: " + pseudoCyclicIndex + " and " + oneDimensional);
                long rPseudoCyclicIndex = hugePseudoCyclicIndex(matrix.dimensions(), shifted.coordinates());
                if (pseudoCyclicIndex != rPseudoCyclicIndex)
                    throw new AssertionError("Bug B detected: pseudo-cyclic index calculated incorrectly: "
                        + pseudoCyclicIndex + " instead of " + rPseudoCyclicIndex);
                if (cyclic.size() == 0) {
                    continue; // cannot test internal indexes
View Full Code Here


            //        do something with element #i;
            //     }
            // and if the checks "i < 0" lead to the same stable result (almost always true or, little better,
            // almost always false), then CPU can optimize this branching.
//            System.out.println("QUICK");
            final IPoint min = pattern.coordMin().toRoundedPoint();
            final Pattern shiftedPattern = pattern.shift(min.symmetric().toPoint()); // "normalized" coordinates >=0
            final Matrix<?> shiftedSrc = Matrices.asShifted(src, min.coordinates());
            Matrix<? extends UpdatablePArray> clone = Arrays.SMM.newMatrix(UpdatablePArray.class, src);
            Matrices.copy(arrayContext == null ? null : arrayContext.part(0.0, 0.05),
                clone, shiftedSrc);
            Matrix<? extends PArray> lazy = asProcessed(requiredType, clone, additionalMatrices, shiftedPattern);
            new Arrays.Copier(arrayContext == null ? null : arrayContext.part(0.05, 1.0),
                dest.array(), lazy.array(), 0, 1).process();
            // "1" allows to split into minimal number of ranges
            return;
        }
        boolean direct = sa instanceof DirectAccessible && ((DirectAccessible) sa).hasJavaArray();
        if (direct || !ENABLE_STREAMING) {
//            System.out.println("SIMPLE");
            Matrix<? extends PArray> lazy = asProcessed(requiredType, src, additionalMatrices, pattern);
            new Arrays.Copier(arrayContext, dest.array(), lazy.array(), 0, 1).process();
            // "1" allows to split into minimal number of ranges
            return;
        }
        assert bufSize < size;

        final IPoint min = pattern.coordMin().toRoundedPoint();
        final Pattern shiftedPattern = pattern.shift(min.symmetric().toPoint()); // "normalized" coordinates >=0
        final Matrix<? extends PArray> shiftedSrc = Matrices.asShifted(src, min.coordinates()).cast(PArray.class);

        final long[] dimensions = src.dimensions();
        final long lSize = Arrays.longMul(dimensions, 0, dimensions.length - 1);
        final long lastDim = dimensions[dimensions.length - 1];
        assert lastDim * lSize == size;
View Full Code Here

            int n = dim(0);
            TinyBitArrays.unpackBits(temp, 0, array, pseudoCyclicIndex(coordinates), n);
            for (int i = 0; i < n; i++) {
                if (temp[i]) {
                    coordinates[0] = i;
                    IPoint p = IPoint.valueOf(coordinates);
                    if (shift != null) {
                        p = p.add(shift);
                    }
                    points.add(p);
                }
            }
        } else {
View Full Code Here

        if (multipliers.length != dimCount())
            throw new IllegalArgumentException("Illegal number of multipliers: "
                + multipliers.length + " instead of " + dimCount());
        int n = dimCount();
        Pattern newPattern = parent.scale(multipliers);
        IPoint newMin = weightCoordMin.roundedScale(multipliers);
        IPoint newMax = weightCoordMin.roundedScale(multipliers);
        long count = 1;
        for (int k = 0; k < n; k++) {
            IRange range = IRange.valueOf(newMin.coord(k), newMax.coord(k));
            if (range.size() >= Integer.MAX_VALUE || (count *= range.size()) >= Integer.MAX_VALUE)
                throw new IllegalArgumentException("Too large desired weight matrix after resizing: "
                    + "more than 2^31-1 elements");
        }
        double[] newWeights = new double[(int) count]; // zero-filled
        double newOutsideWeight = outsideWeight * newPattern.largePointCount() / this.largePointCount();
        BasicWeightedPattern result = new BasicWeightedPattern(newPattern,
            newMin, newMax, newWeights, newOutsideWeight, false);
        if (Math.abs(multipliers[0]) < 1.0) { //TODO!!
            Set<IPoint> points = roundedPoints();
            for (IPoint point : points) {
                IPoint newPoint = point.roundedScale(multipliers);
                int newIndex = result.weightIndex(newPoint);
                if (newIndex != -1) {
                    newWeights[newIndex] += weight(point);
                }
            }
        } else {
            double multInv = 1.0 / multipliers[0]; //TODO!!
            int[] duplicationCounts = new int[weights.length]; // zero-filled
            Set<IPoint> newPoints = result.roundedPoints();
            for (IPoint newPoint : newPoints) {
                IPoint point = newPoint.multiply(multInv);
                int index = weightIndex(point);
                if (index != -1) {
                    duplicationCounts[index]++;
                }
            }
            for (IPoint newPoint : newPoints) {
                int newIndex = result.weightIndex(newPoint);
                if (newIndex != -1) {
                    IPoint point = newPoint.multiply(multInv);
                    int index = weightIndex(point);
                    if (index != -1) {
                        newWeights[newIndex] = weights[index] / duplicationCounts[index];
                    } else {
                        newWeights[newIndex] = newOutsideWeight;
View Full Code Here

        Pattern lastNormalized = null;
        Set<IPoint> shiftsOfEqualSegments = new HashSet<IPoint>();
        for (Pattern ptn : patterns) {
            Pattern normalized;
            List<Pattern> minkowskiIncrement;
            IPoint rectEndOrStart;
            if (!isRectangularOrVerySmall(ptn)) {
                // surely not a segment: avoiding slow calls of coordMin, coordMax, shift methods
                // and providing a correct, non-shifted pattern for the quick algorithm
                rectEndOrStart = IPoint.origin(ptn.dimCount());
                normalized = ptn;
                minkowskiIncrement = null;
            } else {
                Point preciseRectEndOrStart = negativeSegments ? ptn.coordMax() : ptn.coordMin();
                assert preciseRectEndOrStart.isInteger();
                // - this method is called only in union-decomposition mode,
                // which is not used for non-integer or (N+1)-dimensional patterns
                rectEndOrStart = preciseRectEndOrStart.toRoundedPoint();
                normalized = ptn.shift(rectEndOrStart.symmetric().toPoint());
                // normalized segment has origin at the left or right end:
                // so minkowskiIncrement will consists of little positive / negative points
                // that provide good (small) leftward / rightward shifts
                minkowskiIncrement = lastNormalized == null ?
                    null :
                    minkowskiSubtractSegment(normalized, lastNormalized);
            }
            boolean equalSegments = minkowskiIncrement != null && minkowskiIncrement.size() == 1
                && minkowskiIncrement.get(0).isSurelyOriginPoint();
            if (minkowskiIncrement == null || !equalSegments) {
                // If minkowskiIncrement!=null, "normalized" and "lastNormalized" are segments along the same axis
                // (equal segments if minkowskiIncrement.size()==1 && minkowskiIncrement.get(0).isOriginPoint().
                // If minkowskiIncrement==null, then
                // either "lastNormalized" is not a segment and should be saved in result (if !=null),
                // or we should save the accumulated set of shifted copies of "lastNormalized" segment.
                if (lastNormalized != null) {
                    result.add(new MinkowskiPair(
                        lastNormalized, // - main pattern, probably segment
                        shiftsOfEqualSegments,
                        minkowskiIncrement));
                    shiftsOfEqualSegments.clear();
                }
            }
            shiftsOfEqualSegments.add(rectEndOrStart);
            lastNormalized = normalized;
        }
        if (lastNormalized != null) {
            result.add(new MinkowskiPair(
                lastNormalized,
                shiftsOfEqualSegments,
                null));
        }
        for (int k = 0, n = result.size(); k < n; k++) {
            MinkowskiPair pair = result.get(k);
            List<Pattern> incrementFromPrevious = k == 0 ? null : result.get(k - 1).incrementToNext;
            if (incrementFromPrevious == null && pair.incrementToNext == null) {
                // isolated pattern, in particular, not a segment
                if (pair.shifts.pointCount() == 1) {
                    // so, there is no sense to store the shift and the basic pattern separately
                    IPoint shift = pair.shifts.roundedPoints().iterator().next();
                    if (!shift.isOrigin()) {
                        pair = new MinkowskiPair(
                            pair.main.shift(shift.toPoint()),
                            Collections.singleton(IPoint.origin(shift.coordCount())),
                            null);
                    }
                }
            }
            pair.incrementFromPrevious = incrementFromPrevious;
View Full Code Here

            Matrix<? extends PArray> m = singleFrame.matrix();
            FramePosition p = singleFrame.position();
            if (integerShiftPositions) {
//                System.out.println("1 frame branch: integer coordinates");
                Point o = p.area().min();
                IPoint localOffset = o.toRoundedPoint();
                assert o.equals(localOffset.toPoint());
                Matrix<T> casted = Matrices.asFuncMatrix(Func.IDENTITY, requiredType, m);
                // - must be before subMatr, if requiredType allows outsideValue, but the source matrix does not
                return casted.subMatr(localOffset.symmetric().coordinates(), dimensions,
                    Matrix.ContinuationMode.getConstantMode(outsideValue(actualFrames)));
            }
            if (p instanceof UniversalFramePosition
                && ((UniversalFramePosition)p).inverseTransform() instanceof LinearOperator)
            {
                LinearOperator inverseTransform = (LinearOperator)((UniversalFramePosition)p).inverseTransform();
//                System.out.println("1 frame branch: linear operator " + inverseTransform);
                LinearOperator shift = LinearOperator.getShiftInstance(area.min().coordinates());
                LinearOperator lo = shift.superposition(inverseTransform);
                Func f = Matrices.asInterpolationFunc(m, Matrices.InterpolationMethod.POLYLINEAR_FUNCTION,
                    outsideValue(actualFrames));
                f = lo.apply(f);
                return Matrices.asCoordFuncMatrix(f, requiredType, dimensions);
            }
        }
        if (integerShiftPositions && stitchingMethod instanceof CoordinateFreeStitchingMethod<?>) {
//            System.out.println("coordinate free branch: combining submatrices");
            // optimization: here we can just create corresponded submatrices
            List<Matrix<? extends PArray>> expandedMatrices =
                new ArrayList<Matrix<? extends PArray>>(actualFrames.size());
            for (Frame<P> localFrame : actualFrames) {
                Point o = localFrame.position().area().min();
                IPoint localOffset = o.toRoundedPoint();
                assert o.equals(localOffset.toPoint());
                Matrix<? extends PArray> m = localFrame.matrix();
                m = Matrices.asFuncMatrix(Func.IDENTITY, DoubleArray.class, m);
                // - we need Double virtual matrix to provide correct NaN values to the combining function
                m = m.subMatr(localOffset.symmetric().coordinates(), dimensions, Matrix.ContinuationMode.NAN_CONSTANT);
                expandedMatrices.add(m);
            }
            return Matrices.asFuncMatrix(
                ((CoordinateFreeStitchingMethod<?>) stitchingMethod).combiningFunc(),
                requiredType, expandedMatrices);
View Full Code Here

        boolean negativeSegments = sameRightEnd;
        if (!negativeSegments && !sameLeftEnd) {
            result.add(Patterns.newIntegerPattern(IPoint.valueOf(leftShift)));
        }
        // Building Minkowski decomposition of left..0 segment (inclusive), without pairs less than smallLength
        IPoint origin = IPoint.origin(dimCount);
        long len = largerLength - smallerLength;
        while (len > 0) {
            // We need to add 0..len segment
            if (len <= smallerLength) {
                result.add(Patterns.newIntegerPattern(origin, origin.shiftAlongAxis(axis, negativeSegments ? -len : len)));
                break;
            }
            long newLen = len >> 1;
            result.add(Patterns.newIntegerPattern(origin, origin.shiftAlongAxis(axis,
                negativeSegments ? newLen - len : len - newLen)));
            len = newLen;
        }
        return result;
    }
View Full Code Here

TOP

Related Classes of net.algart.math.IPoint

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.