# Sum elements of jagged array VariableArray<VariableArray<int>, int[][]> • ### Question

• Hello,

I am looking for some help for the following scenario. I have a 2-D integer array(named "Choices") and every element can have one of two integer values: 0 and 1 and I want to sum the array for every value of dimension-1(i.e G). Below is the relevant portion of my model along with the definitions:

// variable declarations

protected VariableArray<VariableArray<int>, int[][]> Choices;

protected VariableArray<int> History;

protected Variable<int> NumAgents;

//Ranges

G = new Range(NumGames).Named("G");

H = new Range(HistoryLength).Named("H");

S = new Range(NumStrategies).Named("S");

C = new Range(NumAgents).Named("C");

//Model

Choices = Variable.Array(Variable.Array<int>(C), G).Named("Choices");

using (Variable.ForEach(G))
{
History[G] = Variable<int>.Random(HistoryDis);

using (Variable.ForEach(C))
{

Variable<int> Agent = null;
Agent.SetValueRange(S);
using (Variable.Switch(Agent))
{
Choices[G][C] = Variable.Discrete(Phi[History[G], Agent]);
}
}

}

Now, I am looking for a piece of infer.Net code that will allow me to do the following: for each "G" I want to sum "Choices" over all values of "C" and If this sum is less than "NumAgents/2" then I want to define a new "Variable<int> Result" whose value would be 0. i.e.

Using(Variable.ForEach(G))

{

If (sum(Choices[G][*])>NumAgents/2)

Result=0;

else

Result=1;

}

Sorry for the poor pseudo code.I would appreciate any help I can get.Thanks

Thursday, January 3, 2013 8:56 AM

### All replies

• Hi Farhan

Just concentrating on counting the number of times a particular discrete value appears in an array for now, here are two ways of doing this using existing factors. There may be others, and I will post again if I think of more. The first requires variational message passing. Let me know if this helps. Let me know if this gives you enough to go on. If you have further questions (such as the 2-D aspects) let me know.

John

```using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MicrosoftResearch.Infer.Models;
using MicrosoftResearch.Infer.Maths;
using MicrosoftResearch.Infer;

namespace Khawar2
{
class Program
{
static int numChoices = 10; // The size of the array
static int N = 1;  // The value of the discrete we are interested in

static void Method1()
{
var C = new Range(numChoices).Named("C");
var Choices = Variable.Array<int>(C).Named("Choices");
var ChoicesEqualN = Variable.Array<bool>(C).Named("ChoicesEqualN");

Choices[C] = Variable.Discrete(Vector.FromArray(0.1, 0.6, 0.3)).ForEach(C);
ChoicesEqualN[C] = Choices[C] == N;

Vector v = Vector.Constant(numChoices, 1.0);
var sum = Variable.SumWhere(ChoicesEqualN, v);

var engine = new InferenceEngine(new VariationalMessagePassing());
Console.WriteLine(engine.Infer(sum));
}

static void Method2()
{
var C = new Range(numChoices).Named("C");
var Choices = Variable.Array<int>(C).Named("Choices");
var ChoicesEqualN = Variable.Array<double>(C).Named("ChoicesEqualN");

Choices[C] = Variable.Discrete(Vector.FromArray(0.1, 0.6, 0.3)).ForEach(C);

using (Variable.ForEach(C))
{
var c = Choices[C] == N;
using (Variable.If(c))
{
ChoicesEqualN[C] = Variable.GaussianFromMeanAndVariance(1.0, 0.0);
}

using (Variable.IfNot(c))
{
ChoicesEqualN[C] = Variable.GaussianFromMeanAndVariance(0.0, 0.0);
}
}

var sum = Variable.Sum(ChoicesEqualN);

var engine = new InferenceEngine();
Console.WriteLine(engine.Infer(sum));

}

static void Main(string[] args)
{
Method1();
Method2();
}
}
}```

Friday, January 4, 2013 9:20 AM