locked
getting subarray on a jagged array - clone the internal range - infer.net RRS feed

  • Question

  • Hi every body

    I have a problem working with jagged array and indexing on them. I would appreciate any help, I have been working on this problem for several days and not find an answer yet.

    I have a network of Genes that has nGenes number of nodes (genes) in it.
    I have defined a jagged array as follows for defining weights between every two genes (there is no weight between a node and itself):
                Range geneRange = new Range(nGenes);

                int[] sizes = new int[nGenes];
                for (int i = 0; i < nGenes; i++) sizes[i] = nGenes - 1; // inner size for internal vectors are nGenes-1 ... for all genes
                Range item = geneRange.Clone().Named("item");
                VariableArray<int> sizesVar = Variable.Constant(sizes, item).Named("sizes");
                Range feature = new Range(sizesVar[item]).Named("feature");
                var w = Variable.Array(Variable.Array<double>(feature), item).Named("w");
    This w matrix contains edge weights between each gene and other genes in the network (there is no edge from one gene to itself).
    for example if we have four genes (G1,G2,G3 and G4), w for gene G1 will include edgeWeight(G2,G1), edgeWeight(G3,G1) and edgeWeight(G4,G1). So for a network of N nodes, w is a N*(N-1) jagged array.
    I have another vector that contains a value for each gene. That vector is called genesT1.
    Now for each gene, I have to compute the weighted sum of other gene values.
    For example : weighted sum for G1 = W(G2,G1) * genesT1(G2) + W(G3,G1) * genesT1(G3) + W(G4,G1) * genesT1(G4)
    So here I need inner product of first row of W, and a subarray of nodes vector (G2,G3,G4).
    I made an indices jagged array to create required indices for each subarray:
    In this case the jagged array for indices to create subarrays is as follows:
    indicesArray[0] = {1,2,3}
    indicesArray[1] = {0,2,3}
    indicesArray[2] = {0,1,3}
    indicesArray[3] = {0,1,2}

    Here is the code to create indices jagged array:
                int[] subsize = new int[nGenes];
                for (int i = 0; i < nGenes; i++) subsize[i] = nGenes - 1;
                Range subRange = geneRange.Clone().Named("subrange");
                VariableArray<int> subsizeVar = Variable.Constant(subsize, subRange).Named("subsizesVar");
                Range innerRange = new Range(subsizeVar[subRange]).Named("innerRange");
                var indicesArray = Variable.Array(Variable.Array<int>(innerRange), geneRange).Named("indicesArray");

                int[][] indices = new int[nGenes][];
                for (int i = 0; i < nGenes; i++) {
                    indices[i] = new int[nGenes-1];
                    int j = 0, ind = 0;
                    while(j<nGenes){
                        if (i == j) { j++; continue; }
                        indices[i][ind] = j;
                        j++;
                        ind++;
                    }                
                }
                indicesArray.ObservedValue = indices;

    Now in a for loop, I wanted to compute these weighted sums:
                Range geneWeightRange = new Range(nGenes - 1).Named("geneWeightRange");
                VariableArray<double> genesubarray = Variable.Array<double>(geneWeightRange);
                using (ForEachBlock firstBlock = Variable.ForEach(geneRange))
                {
                    VariableArray<double> weightSum = Variable.Array<double>(geneWeightRange);
                    genesubarray = Variable.Subarray(genesT1, indicesArray[geneRange]);
                    weightSum[geneWeightRange] = w[geneRange][geneWeightRange] * genesubarray[geneWeightRange];//genesT1[geneRange2]
                    .......
                }

    But I get this error:
    VariableArray<double> w[geneRange] cannot be indexed by geneWeightRange. Perhaps you omitted geneWeightRange as an argument to the constructor?
    Parameter name: index
    My question is that in this case how can I create a connection between a jagged array's inner range and another range? I have no idea how can I define inner range of jagged array as a clone of the desired index.
    Can I introduce geneWeightRange as the inner range for w?
    Or I thought maybe my code is not efficient and I should use another way to do this computation?

    I would appreciate any help in this case.
    Thanks a lot.
    Zahra

    Tuesday, May 27, 2014 3:39 PM

Answers

  • You can use
    Range geneWeightRange = w[geneRange].Range;

    to get the correct inner range.  What you wrote is not correct since there is no guarantee that a range of size nGenes-1 is compatible with sizesVar. Alternatively, you could have used a range of size nGenes-1 for the inner range of w since you know its dimension is fixed.
    Tuesday, May 27, 2014 4:29 PM
    Owner
  • Dear Tom,

    Thanks a lot for your reply. Actually I had tested it and still I got the same error. I thought this is because jagged array elements may have different internal range lengths.

    But I found out I can also use the syntax below:

    var w = Variable.Array(Variable.Array<double>(geneWeightRange), geneRange).Named("w");

    This works since my inner arrays are all of the same length.

    Thanks it is solved now. But I don't know why it was not working using the above syntax.

    • Marked as answer by RazinR Tuesday, May 27, 2014 5:32 PM
    Tuesday, May 27, 2014 5:14 PM

All replies

  • You can use
    Range geneWeightRange = w[geneRange].Range;

    to get the correct inner range.  What you wrote is not correct since there is no guarantee that a range of size nGenes-1 is compatible with sizesVar. Alternatively, you could have used a range of size nGenes-1 for the inner range of w since you know its dimension is fixed.
    Tuesday, May 27, 2014 4:29 PM
    Owner
  • Dear Tom,

    Thanks a lot for your reply. Actually I had tested it and still I got the same error. I thought this is because jagged array elements may have different internal range lengths.

    But I found out I can also use the syntax below:

    var w = Variable.Array(Variable.Array<double>(geneWeightRange), geneRange).Named("w");

    This works since my inner arrays are all of the same length.

    Thanks it is solved now. But I don't know why it was not working using the above syntax.

    • Marked as answer by RazinR Tuesday, May 27, 2014 5:32 PM
    Tuesday, May 27, 2014 5:14 PM