Answered by:
Modulus error
Question

I'm trying to manipulate a Variable<int> (called y in the code below)
using (Variable.If(y % 12 == 0))
using the modulus function but I get the following error on that line:
No operator factor registered for 'Modulus' with argument type System.Int32.
Is there a way to perform modulus arguments on Variable<int> types? If not, how should I approach replicating this function in a manner Infer.net can handle?
Friday, September 26, 2014 10:44 AM
Answers

If y is observed, then you can make another observed variable holding the value of (y % 12). If y is random, then you can make an array holding the value of (y % 12) for all y, and use y to index into this array, so the expression becomes (mod12[y] == 0).
 Marked as answer by MarkG87 Tuesday, September 30, 2014 12:28 PM
Friday, September 26, 2014 2:17 PMOwner 
mod12.ObservedValue = Util.ArrayInit(numYears, i => i % 12);
 Marked as answer by MarkG87 Tuesday, September 30, 2014 12:28 PM
Monday, September 29, 2014 3:30 PMOwner
All replies

If y is observed, then you can make another observed variable holding the value of (y % 12). If y is random, then you can make an array holding the value of (y % 12) for all y, and use y to index into this array, so the expression becomes (mod12[y] == 0).
 Marked as answer by MarkG87 Tuesday, September 30, 2014 12:28 PM
Friday, September 26, 2014 2:17 PMOwner 
Thanks Tom, in this case y is a variable that represents the Index within a ForEach block like so:
using (var yearBlock = Variable.ForEach(year)) { var y = yearBlock.Index;
I've tried:
var ymod = y % 12 using (Variable.If(ymod == 0))
Which gives the same error on the 'var ymod' line. I've also tried setting ymod as being based on the ObservedValue of y (I wasn't sure if y counted as an Observed value within the using loop, as it can only take one value):
var ymod = y.ObservedValue % 12
Which gives a 'The method or operation is not implemented.' error. I've also tried setting up a VariableArray to index into but run in to the original problem again:
using (var yearBlock = Variable.ForEach(year)) { var y = yearBlock.Index; mod12[year] = y % 12; }
As the 'mod12[year]' line gives the original error. How should I structure your solution to this problem?
Monday, September 29, 2014 3:24 PM 
mod12.ObservedValue = Util.ArrayInit(numYears, i => i % 12);
 Marked as answer by MarkG87 Tuesday, September 30, 2014 12:28 PM
Monday, September 29, 2014 3:30 PMOwner