Package net.fec.openrq.util.linearalgebra.io

Examples of net.fec.openrq.util.linearalgebra.io.ByteVectorIterator


    public void serializeToChannel(WritableByteChannel ch) throws IOException {

        Serialization.writeType(ch, Serialization.Type.SPARSE_VECTOR);
        Serialization.writeVectorLength(ch, length());
        Serialization.writeVectorCardinality(ch, cardinality());
        ByteVectorIterator it = nonZeroIterator();
        while (it.hasNext()) {
            it.next();
            Serialization.writeVectorIndex(ch, it.index());
            Serialization.writeVectorValue(ch, it.get());
        }
    }
View Full Code Here


    }

    @Override
    public Void apply(DenseByteVector a, SparseByteVector b) {

        ByteVectorIterator it = b.nonZeroIterator();
        while (it.hasNext()) {
            it.next();
            a.set(it.index(), aMinusB(a.get(it.index()), it.get()));
        }
        return null;
    }
View Full Code Here

    }

    @Override
    public Void apply(SparseByteVector a, SparseByteVector b) {

        ByteVectorIterator it = indexer.nonZeroIterator(b);
        // TODO: these.andAlsoAdd(those)
        // these.andAlsoSubtract(those);
        while (it.hasNext()) {
            it.next();
            a.addInPlace(it.index(), multB(it.get()));
        }
        return null;
    }
View Full Code Here

    }

    @Override
    public Void apply(SparseByteVector a, DenseByteVector b) {

        ByteVectorIterator it = indexer.iterator(a);
        while (it.hasNext()) {
            it.set(aPlusB(it.get(), multB(b.get(it.index()))));
        }
        return null;
    }
View Full Code Here

    }

    @Override
    public Void apply(DenseByteVector a, SparseByteVector b) {

        ByteVectorIterator it = indexer.nonZeroIterator(b);
        while (it.hasNext()) {
            it.next();
            a.set(it.index(), aPlusB(a.get(it.index()), multB(it.get())));
        }
        return null;
    }
View Full Code Here

            // this is an optimization
            if (nonZeros == 2 && !isHDPC) {
                int originalDegree = 0;
                final Set<Integer> nodes = new HashSet<>(2 + 1, 1.0f); // we already know there are only 2 non zeros

                ByteVectorIterator it = A.nonZeroRowIterator(row, 0, L - u);
                while (it.hasNext()) {
                    it.next();
                    originalDegree += OctetOps.UNSIGN(it.get()); // add to the degree of this row
                    nodes.add(it.index()); // add the column index to the nodes
                }

                rows.put(row, new Row(row, nonZeros, originalDegree, isHDPC, nodes));
            }
            else {
                int originalDegree = 0;

                ByteVectorIterator it = A.nonZeroRowIterator(row, 0, L - u);
                while (it.hasNext()) {
                    it.next();
                    originalDegree += OctetOps.UNSIGN(it.get()); // add to the degree of this row
                }

                rows.put(row, new Row(row, nonZeros, originalDegree, isHDPC));
            }
        }

        TimerUtils.markTimestamp(); // DEBUG
        initNanos += TimerUtils.getEllapsedTimeLong(TimeUnit.NANOSECONDS);

        // at most L steps
        while (i + u != L)
        {
            // the degree of the 'currently chosen' row
            int minDegree = 256 * L;

            // number of non-zeros in the 'currently chosen' row
            int r = L + 1;

            // currently chosen row
            Row chosenRow = null;

            // decoding failure?
            boolean allZeros = true;

            // there is a row with exactly two ones
            boolean two1s = false;

            /*
             * find r
             */

            TimerUtils.beginTimer(); // DEBUG

            for (Row row : rows.values()) {
                if (row.nonZeros != 0) allZeros = false;
                if (row.isHDPC && chosenRowsCounter < nonHDPCRows) continue;

                // if it's an edge, then it must have exactly two 1's
                if (row.nodes != null) two1s = true;

                if (row.nonZeros < r && row.nonZeros > 0) {
                    chosenRow = row;
                    r = chosenRow.nonZeros;
                    minDegree = chosenRow.originalDegree;
                }
                else if (row.nonZeros == r && row.originalDegree < minDegree) {
                    chosenRow = row;
                    minDegree = chosenRow.originalDegree;
                }
            }

            TimerUtils.markTimestamp(); // DEBUG
            findRNanos += TimerUtils.getEllapsedTimeLong(TimeUnit.NANOSECONDS);

            if (allZeros) {// DECODING FAILURE
                throw new SingularMatrixException(
                    "Decoding Failure - PI Decoding @ Phase 1: All entries in V are zero.");
            }

            /*
             * choose the row
             */

            TimerUtils.beginTimer(); // DEBUG

            if (r == 2 && two1s) {

                /*
                 * create graph
                 */

                // allocate memory
                Map<Integer, Set<Integer>> graph = new HashMap<>(L - u - i + 1, 1.0f);

                // lets go through all the rows... (yet again!)
                for (Row row : rows.values())
                {
                    // is this row an edge?
                    if (row.nodes != null)
                    {
                        // get the nodes connected through this edge
                        Integer[] edge = row.nodes.toArray(new Integer[2]);
                        int node1 = edge[0];
                        int node2 = edge[1];

                        // node1 already in graph?
                        if (graph.keySet().contains(node1))
                        { // it is

                            // then lets add node 2 to its neighbours
                            graph.get(node1).add(node2);
                        }
                        else
                        { // it isn't

                            // allocate memory for its neighbours
                            Set<Integer> edges = new HashSet<>(L - u - i + 1, 1.0f);

                            // add node 2 to its neighbours
                            edges.add(node2);

                            // finally, add node 1 to the graph along with its neighbours
                            graph.put(node1, edges);
                        }

                        // node2 already in graph?
                        if (graph.keySet().contains(node2))
                        { // it is

                            // then lets add node 1 to its neighbours
                            graph.get(node2).add(node1);
                        }
                        else
                        { // it isn't

                            // allocate memory for its neighbours
                            Set<Integer> edges = new HashSet<>(L - u - i + 1, 1.0f);

                            // add node 1 to its neighbours
                            edges.add(node1);

                            // finally, add node 2 to the graph along with its neighbours
                            graph.put(node2, edges);
                        }
                    }
                    else continue;
                }

                /*
                 * the graph is complete, now we must
                 * find the maximum size component
                 */

                // set of visited nodes
                Set<Integer> visited = null;

                /*
                 * TODO Optimization: I already searched, and there are optimized algorithms to find connected
                 * components. Then we just find and use the best one available...
                 */

                // what is the size of the largest component we've already found
                int maximumSize = 0;

                // the maximum size component
                Set<Integer> greatestComponent = null;

                // which nodes have already been used (either in visited or in toVisit)
                Set<Integer> used = new HashSet<>(L - u - i + 1, 1.0f);

                // iterates the nodes in the graph
                Iterator<Map.Entry<Integer, Set<Integer>>> it = graph.entrySet().iterator();

                // let's iterate through the nodes in the graph, looking for the maximum
                // size component. we will be doing a breadth first search // TODO optimize this with a better
                // algorithm?
                while (it.hasNext())
                {
                    // get our initial node
                    Map.Entry<Integer, Set<Integer>> node = it.next();
                    int initialNode = node.getKey();

                    // we can't have used it before!
                    if (used.contains(initialNode)) continue;

                    // what are the edges of our initial node?
                    Integer[] edges = node.getValue().toArray(new Integer[node.getValue().size()]);

                    // allocate memory for the set of visited nodes
                    visited = new HashSet<>(L - u - i + 1, 1.0f);

                    // the set of nodes we must still visit
                    List<Integer> toVisit = new LinkedList<>();

                    // add the initial node to the set of used and visited nodes
                    visited.add(initialNode);
                    used.add(initialNode);

                    // add my edges to the set of nodes we must visit
                    // and also put them in the used set
                    for (Integer edge : edges)
                    {
                        toVisit.add(edge);
                        used.add(edge);
                    }

                    // start the search!
                    while (toVisit.size() != 0)
                    {
                        // the node we are visiting
                        int no = toVisit.remove(0);

                        // add node to visited set
                        visited.add(no);

                        // queue edges to be visited (if they haven't been already
                        for (Integer edge : graph.get(no))
                            if (!visited.contains(edge)) toVisit.add(edge);
                    }

                    // is the number of visited nodes, greater than the 'currently' largest component?
                    if (visited.size() > maximumSize)
                    { // it is! we've found a greater component then...

                        // update the maximum size
                        maximumSize = visited.size();

                        // update our greatest component
                        greatestComponent = visited;
                    }
                    else continue;
                }

                /*
                 * we've found the maximum size connected component -- 'greatestComponent'
                 */

                // let's choose the row
                for (Row row : rows.values())
                {
                    // is it a node in the graph?
                    if (row.nodes != null)
                    { // it is

                        // get the nodes connected through this edge
                        Integer[] edge = row.nodes.toArray(new Integer[2]);
                        int node1 = edge[0];
                        int node2 = edge[1];

                        // is this row an edge in the maximum size component?
                        if (greatestComponent.contains(node1) && greatestComponent.contains(node2))
                        {
                            chosenRow = row;
                            break;
                        }
                        else continue;
                    }
                    else continue;
                }

                chosenRowsCounter++;

                TimerUtils.markTimestamp(); // DEBUG
                chooseRowNanos += TimerUtils.getEllapsedTimeLong(TimeUnit.NANOSECONDS);
            }
            else {

                // already chosen (in 'find r')
                chosenRowsCounter++;
            }

            /*
             * a row has been chosen! -- 'chosenRow'
             */

            /*
             * "After the row is chosen in this step, the first row of A that intersects V is exchanged
             * with the chosen row so that the chosen row is the first row that intersects V."
             */

            final int chosenRowPos = chosenRow.position;

            // if the chosen row is not 'i' already
            if (chosenRowPos != i) {
                TimerUtils.beginTimer(); // DEBUG

                // swap in A
                A.swapRows(i, chosenRowPos);

                // swap in X
                X.swapRows(i, chosenRowPos);

                // decoding process - swap in d
                ArrayUtils.swapInts(d, i, chosenRowPos);

                // update values in 'rows' map
                Row other = rows.remove(i);
                rows.put(chosenRowPos, other);
                other.position = chosenRowPos;
                chosenRow.position = i;

                TimerUtils.markTimestamp(); // DEBUG
                swapRowsNanos += TimerUtils.getEllapsedTimeLong(TimeUnit.NANOSECONDS);
            }

            /*
             * "The columns of A among those that intersect V are reordered so that one of the r nonzeros
             * in the chosen row appears in the first column of V and so that the remaining r-1 nonzeros
             * appear in the last columns of V."
             */

            TimerUtils.beginTimer(); // DEBUG

            // an array with the positions (column indices) of the non-zeros
            final int[] nonZeroPos = A.nonZeroPositionsInRow(i, i, L - u);

            /*
             * lets start swapping columns!
             */

            // is the first column in V already the place of a non-zero?
            final int firstNZpos = nonZeroPos[0]; // the chosen row always has at least one non-zero
            if (i != firstNZpos) {
                // no, so swap the first column in V (i) with the first non-zero column

                // swap columns
                A.swapColumns(i, firstNZpos);
                X.swapColumns(i, firstNZpos);

                // decoding process - swap in c
                ArrayUtils.swapInts(c, i, firstNZpos);
            }

            // swap the remaining non-zeros' columns so that they're the last columns in V
            for (int nzp = nonZeroPos.length - 1, currCol = L - u - 1; nzp > 0; nzp--, currCol--) {
                // is the current column already the place of a non-zero?
                final int currNZpos = nonZeroPos[nzp];
                if (currCol != currNZpos) {
                    // no, so swap the current column in V with the current non-zero column

                    // swap columns
                    A.swapColumns(currCol, currNZpos);
                    X.swapColumns(currCol, currNZpos);

                    // decoding process - swap in c
                    ArrayUtils.swapInts(c, currCol, currNZpos);
                }
            }

            TimerUtils.markTimestamp(); // DEBUG
            swapColumnsNanos += TimerUtils.getEllapsedTimeLong(TimeUnit.NANOSECONDS);

            /*
             * "... if a row below the chosen row has entry beta in the first column of V, and the chosen
             * row has entry alpha in the first column of V, then beta/alpha multiplied by the chosen
             * row is added to this row to leave a zero value in the first column of V."
             */

            TimerUtils.beginTimer(); // DEBUG

            // "the chosen row has entry alpha in the first column of V"
            final byte alpha = A.get(i, i);

            // let's look at all rows below the chosen one
            for (int row = i + 1; row < M; row++)
            // Page35@RFC6330 1st Par.
            {
                // "if a row below the chosen row has entry beta in the first column of V"
                final byte beta = A.get(row, i);

                // if it's already 0, no problem
                if (beta == 0) {
                    continue;
                }
                // if it's a non-zero we've got to "zerofy" it
                else
                {
                    /*
                     * "then beta/alpha multiplied by the chosen row is added to this row"
                     */

                    // division
                    byte betaOverAlpha = OctetOps.aDividedByB(beta, alpha);

                    // multiplication and addition
                    A.addRowsInPlace(betaOverAlpha, i, row);

                    // decoding process - D[d[row]] + (betaOverAlpha * D[d[i]])
                    OctetOps.vectorVectorAddition(betaOverAlpha, D[d[i]], D[d[row]], D[d[row]]);

                    // ISDCodeWriter.instance().writePhase1Code(betaOverAlpha, d[i], d[row]); // DEBUG
                }
            }

            TimerUtils.markTimestamp(); // DEBUG
            addMultiplyNanos += TimerUtils.getEllapsedTimeLong(TimeUnit.NANOSECONDS);

            /*
             * "Finally, i is incremented by 1 and u is incremented by r-1, which completes the step."
             */
            i++;
            u += r - 1;

            TimerUtils.beginTimer(); // DEBUG

            // update nonZeros
            for (Row row : rows.values()) {
                // update the non zero count
                row.nonZeros = A.nonZerosInRow(row.position, i, L - u);

                if (row.nonZeros != 2 || row.isHDPC) {
                    row.nodes = null;
                }
                else {
                    final Set<Integer> nodes = new HashSet<>(2 + 1, 1.0f); // we know there will only be two non zeros
                    ByteVectorIterator it = A.nonZeroRowIterator(row.position, i, L - u);
                    while (it.hasNext()) {
                        it.next();
                        nodes.add(it.index()); // add node to this edge (column index)
                    }

                    row.nodes = nodes;
                }
            }
View Full Code Here

         * and if the value of that nonzero entry is b, then add to this row b times row j of I_u."
         */

        // "For each of the first i rows of U_upper"
        for (int row = 0; row < i; row++) {
            ByteVectorIterator it = A.nonZeroRowIterator(row, i, L);
            while (it.hasNext()) {
                it.next();

                // "if the row has a nonzero entry at position j"
                final int j = it.index();
                // "if the value of that nonzero entry is b"
                final byte b = it.get();

                // "add to this row b times row j of I_u" -- this would "zerofy"
                // that position, thus we can save the complexity
                // (no need to actually "zerofy" it, since this part of the matrix will not be used again)
                // it.set((byte)0);
View Full Code Here

                // decoding process - D[d[j]] / beta
                OctetOps.valueVectorDivision(beta, D[d[j]], D[d[j]]); // in place division
            }

            // "For eL from 1 to j-1"
            ByteVectorIterator it = A.nonZeroRowIterator(j, 0, j);
            while (it.hasNext()) {
                it.next();

                // "then add A[j,eL] multiplied with row eL of A to row j of A."
                final int eL = it.index();
                beta = it.get();

                // We do not actually have to perform this operation on the matrix A
                // because it will not be used again.
                // A.addRowsInPlace(beta, eL, j);
View Full Code Here

    @Test
    public void testColumnIterator_1_InRangeOf_0_to_0() {

        ByteMatrix a = iteratorMatrix_1();
        ByteVectorIterator it = a.columnIterator(1, 0, 0);

        assertFalse(it.hasNext());
    }
View Full Code Here

    @Test
    public void testColumnIterator_1_InRangeOf_0_to_3() {

        ByteMatrix a = iteratorMatrix_1();
        ByteVectorIterator it = a.columnIterator(1, 0, 3);

        assertTrue("hasNext 0", it.hasNext());
        it.next();
        assertEquals("get 0", 0, it.get());
        assertEquals("index 0", 0, it.index());

        assertTrue("hasNext 1", it.hasNext());
        it.next();
        assertEquals("get 1", 1, it.get());
        assertEquals("index 1", 1, it.index());

        assertTrue("hasNext 2", it.hasNext());
        it.next();
        assertEquals("get 2", 0, it.get());
        assertEquals("index 2", 2, it.index());

        assertFalse(it.hasNext());
    }
View Full Code Here

TOP

Related Classes of net.fec.openrq.util.linearalgebra.io.ByteVectorIterator

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.