Message Boards Message Boards

0
|
5500 Views
|
8 Replies
|
2 Total Likes
View groups...
Share
Share this post:

How to assign all possible binary values to variables of a function

Posted 5 years ago

Let us have variables v[k], k=1,...,n. Variables v[k] can have only two values: +1 or -1. I want to find a sequence of values of the following double sums: \sum^n{i=1} \sum^n{j=1} v(i) v(j) w(i,j) where w is a weight function, for all possible +/-1 values. So, the 1st term in the sequence would be the value of the sums for v[1]=1, v[2]=1, ... , v[n]=1, the 2nd term in the sequence would be the value of the sums for v[1]=1, v[2]=1, ... , v[n-1]=1, v[n]=-1, ......... The last term in the sequence would be the value of the sums for v[1]=-1, v[2]=-1, ... , v[n]=-1.

How can I obtain such a sequence in Mathematica?

Thanks, Mladen.

POSTED BY: Mladen Pavicic
8 Replies

Seems I could help you. That's fine. Both approaches are equivalent:

In[166]:= sums - ((sum /. #) & /@ rule)

Out[166]= {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
POSTED BY: Hans Dolhaine
Posted 5 years ago

I don't think so, but thanks for the suggestion.

POSTED BY: Mladen Pavicic
Posted 5 years ago

The latter. Thanks. I learned a lot. I'm afraid I wouldn't be able to find "/@ Range[n]" or "& /@ x /. {v[a], b} -> (v[a] -> b)" in the Documentation soon enough.

POSTED BY: Updating Name
Posted 5 years ago

Could you be looking for something like this?

In[1]:= Total /@ Tuples[{1, -1}, 5]
Out[1]= {5, 3, 3, 1, 3, 1, 1, -1, 3, 1, 1, -1, 1, -1, -1, -3, 3, 1, \
1, -1, 1, -1, -1, -3, 1, -1, -1, -3, -1, -3, -3, -5}

enter image description here

POSTED BY: Hans Milton

Or perhaps like this

n = 5
x = Tuples[{-1, 1}, n] // Reverse
var = v /@ Range[n]
rule = Transpose[{var, #}] & /@ x /. {v[a_], b_} -> (v[a] -> b)
m = Table[w[i, j], {i, n}, {j, n}];
m // MatrixForm
sum = var.m.var
(sum /. #) & /@ rule
POSTED BY: Hans Dolhaine

I am afraid I don't quite understand what you mean. This perhaps?

n = 5;
x = Tuples[{-1, 1}, n] // Reverse;
sums = Table[Sum[w[i, j] x[[k, i]] x[[k, j]], {i, n}, {j, n}], {k, Length[x]}];
sums // TableForm

Here you get 2^n = 32 sums with n^2 = 25 terms each.

POSTED BY: Hans Dolhaine
Posted 5 years ago

{{1, 1, 1, 1, 1}, {1, 1, 1, 1, -1},....,{-1, -1, -1, -1, -1} is the premise we start from. The problem is how to call these series from sums. In particular for the first sum we should obtain v[1]=1,v[2]=1,...,v[5]=1 from {1, 1, 1, 1, 1} and obtain some value for the sums. Then the algorithm should call the second set {1, 1, 1, 1, -1} form the next v[1]=1,v[2]=1,...,v[5]=-1, and so on. What would be the Mathematica program for that?

POSTED BY: Mladen Pavicic

What about

n = 5;
x = Tuples[{-1, 1}, n]
x // Reverse

?

POSTED BY: Hans Dolhaine
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract