Package net.fec.openrq.util.linearalgebra.matrix

Examples of net.fec.openrq.util.linearalgebra.matrix.ByteMatrix


        final int B = W - S;

        TimerUtils.beginTimer(); // DEBUG

        // allocate memory for the constraint matrix
        ByteMatrix A = getMatrixAfactory(L, overheadRows).createMatrix(L + overheadRows, L);

        /*
         * upper half
         */

        // initialize G_LPDC2
        initializeG_LPDC2(A, S, P, W);

        // initialize G_LPDC1
        initializeG_LPDC1(A, B, S);

        // initialize I_s
        initializeIs(A, S, B);

        /*
         * bottom half
         */

        // initialize I_h
        initializeIh(A, W, U, H, S);

        // initialize G_HDPC

        // MT
        ByteMatrix MT = generateMT(H, Kprime, S);

        // GAMMA
        ByteMatrix GAMMA = generateGAMMA(Kprime, S);

        // G_HDPC = MT * GAMMA
        ByteMatrix G_HDPC = MT.multiply(GAMMA);

        // initialize G_HDPC
        for (int row = S; row < S + H; row++) {
            for (int col = 0; col < W + U; col++) {
                A.set(row, col, G_HDPC.get(row - S, col));
            }
        }

        // initialize G_ENC
        initializeG_ENC(A, S, H, L, Kprime);
View Full Code Here


        for (int i = L; i < M; i++) {
            d[i] = i;
        }

        final ByteMatrix X = A.copy();

        // initialize i and u parameters, for the submatrices sizes
        int i = 0, u = P;

        // counts how many rows have been chosen already
        int chosenRowsCounter = 0;

        // the number of rows that are not HDPC
        // (these should be chosen first)
        int nonHDPCRows = S + Kprime;

        // maps the index of a row to an object Row (which stores that row's characteristics)
        final Map<Integer, Row> rows = new HashMap<>(M + 1, 1.0f);
        for (int row = 0; row < M; row++) {
            // retrieve the number of non-zeros in the row
            final int nonZeros = A.nonZerosInRow(row, 0, L - u); // exclude last u columns
            // is this a HDPC row?
            final boolean isHDPC = (row >= S && row < S + H);

            // 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);
                }
            }
View Full Code Here

        // decoding process
        final int Drows = Xrows;
        final int Dcols = (D.length == 0) ? 0 : D[0].length;
        final byte[][] DShallowCopy = Arrays.copyOf(D, D.length);
        final ByteMatrix DM = new RowIndirected2DByteMatrix(Drows, Dcols, DShallowCopy, d);

        for (int row = 0; row < Xrows; row++) {
            // multiply X[row] by D
            BasicByteVector prod = (BasicByteVector)X.multiplyRow(row, DM, 0, Xcols, LinearAlgebra.BASIC2D_FACTORY);
            D[d[row]] = prod.getInternalArray();
View Full Code Here

    private static final class ReduceMatrixToRowEchelon implements ISDOperation {

        static ReduceMatrixToRowEchelon deserializeFromChannel(ReadableByteChannel ch) throws IOException {

            final ByteMatrix A = readMatrix(ch);
            final int fromRow = ExtraChannels.readInt(ch);
            final int toRow = ExtraChannels.readInt(ch);
            final int fromCol = ExtraChannels.readInt(ch);
            final int toCol = ExtraChannels.readInt(ch);
            final int[] d = readIntArray(ch);
View Full Code Here

    private static final class MatrixVectorMultiplication implements ISDOperation {

        static MatrixVectorMultiplication deserializeFromChannel(ReadableByteChannel ch) throws IOException {

            final ByteMatrix X = readMatrix(ch);
            final int Xrows = ExtraChannels.readInt(ch);
            final int Xcols = ExtraChannels.readInt(ch);
            final int[] d = readIntArray(ch);
            return new MatrixVectorMultiplication(X, Xrows, Xcols, d);
        }
View Full Code Here

        }

        @Override
        public byte[][] apply(byte[][] D) {

            ByteMatrix DM = new RowIndirected2DByteMatrix(Xrows, Dcols(D), DShallowCopy(D), d);
            for (int row = 0; row < Xrows; row++) {
                D[d[row]] = getInnerArray(X.multiplyRow(row, DM, 0, Xcols, LinearAlgebra.BASIC1D_FACTORY));
            }

            return D;
View Full Code Here

            return isd.decode(D);
        }
        else { // if no optimized decoder is available, fall back to the standard decoding process

            // generate LxL Constraint Matrix
            ByteMatrix constraint_matrix = LinearSystem.generateConstraintMatrix(Kprime); // A

            // solve system of equations
            try {
                return LinearSystem.PInactivationDecoding(constraint_matrix, D, Kprime);
                // return Utilities.gaussElimination(constraint_matrix, D);
View Full Code Here

    @Override
    public ByteMatrix createRandomMatrix(int rows, int columns, Random random) {

        int cardinality = (rows * columns) / DENSITY;

        ByteMatrix matrix = new CCSByteMatrix(rows, columns);
        for (; cardinality > 0; cardinality--) {
            final int i = random.nextInt(rows);
            final int j = random.nextInt(columns);
            matrix.set(i, j, (byte)random.nextInt());
        }

        return matrix;
    }
View Full Code Here

    @Override
    public ByteMatrix createRandomSymmetricMatrix(int size, Random random) {

        int cardinality = (size * size) / DENSITY;

        ByteMatrix matrix = new CCSByteMatrix(size, size);
        for (int k = 0; k < cardinality / 2; k++) {
            final int i = random.nextInt(size);
            final int j = random.nextInt(size);
            final byte value = (byte)random.nextInt();

            matrix.set(i, j, value);
            matrix.set(j, i, value);
        }

        return matrix;
    }
View Full Code Here

            throw new IllegalArgumentException("Sides of blocks are incompatible!");
        }

        final int rows = a.rows() + c.rows();
        final int cols = a.columns() + b.columns();
        ByteMatrix matrix = new CCSByteMatrix(rows, cols);
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                if ((i < a.rows()) && (j < a.columns())) {
                    matrix.set(i, j, a.get(i, j));
                }
                if ((i < a.rows()) && (j > a.columns())) {
                    matrix.set(i, j, b.get(i, j));
                }
                if ((i > a.rows()) && (j < a.columns())) {
                    matrix.set(i, j, c.get(i, j));
                }
                if ((i > a.rows()) && (j > a.columns())) {
                    matrix.set(i, j, d.get(i, j));
                }
            }
        }

        return matrix;
View Full Code Here

TOP

Related Classes of net.fec.openrq.util.linearalgebra.matrix.ByteMatrix

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.