Mathematica Problem

• Mar 15th 2010, 09:32 AM
slevvio
Mathematica Problem
Hello there I was wondering if someone could give me a hand with a problem I'm having on Mathematica. I have the set X := {1,2,3,4}, and I have computed the Cartesian product of this set using Combinatorica.
I have inputted the two subsets
R:= {(1,2),(2,3),(3,4),(4,1)}
S:= {(2,1),(3,2),(4,3),(1,4)}
and I want to find the set

$R \bullet S = \{(x,y) \in X^2: \exists$ a unique z [depending on the pair (x,y)] such that $(x,z) \in S$ and $(z,y) \in R \} \$
So i want mathematica to run through all the elements in X^2 and do a check to see if the condition in $R \bullet S$ is satisified. Is this possible? I have no idea how to define the set $R \bullet S$ or how to get mathematica to even run through all the 16 elements in X^2 to see if they satisfy certain conditions.
To clarify, (1,1) is in $R \bullet S$ because (1,4) is in S and (4,1) is in S. 4 is the unique element that joins x = 1 to y = 1. there is no other element except from 4 that does this.
Thanks for listening to my request and I hope someone can guide me on this. :)
• Mar 15th 2010, 12:12 PM
shawsend
Might have trouble understanding this:

xset = {1, 2, 3, 4}
myclist = Outer[{#1, #2} & , xset, xset]
rlist = {{1, 2}, {2, 3}, {3, 4}, {4, 1}};
slist = {{2, 1}, {3, 2}, {4, 3}, {1, 4}};
mymatch = {};
myset = Table[mytest = myclist[[n,j]];
(* select from slist, those elements for which the first term is equal to the first term in mytest*)
list1 = Select[slist,
#1[[1]] == mytest[[1]] & ];
(* select from rlist, those elements for which the second term is equal to the second term in mylist *)
list2 = Select[rlist,
#1[[2]] == mytest[[2]] & ];
(* now make a list of all the second terms in list 1 *)
common = list1[[All,2]];
(* go through list2 and select all elements for which the first term is one of the common terms *)
mymatch = Append[mymatch,
Table[Select
[list2,
#1[[1]] == common[[n]] & ],
{n, 1, Length[common]}]],
(* do this for the entire cross product of xset x xset *)
{n, 1, 4}, {j, 1, 4}];
(* remove all the NULL cases *)
DeleteDuplicates[Flatten[myset, 4]]
• Mar 15th 2010, 12:21 PM
slevvio
thank you: i will try this tomorrow when I have access to a mathematica machine
• Mar 16th 2010, 03:04 AM
slevvio
hello!

the set I was expecting in the end should be {(1,1),(2,2),(3,3),(4,4)} i.e. the diagonal set X - this one has given me back the R set. What can I do to fix this?
• Mar 16th 2010, 04:10 AM
shawsend
Now it depends if you're planning to use Mathematica in the future or never plan to use it again. If the former, then you've got to tinker with it, play and fiddle with it by changing the parameters and understanding what's going on. Personally I think suggesting this is a much better way of helping you then just figuring it out for you. Try that first before resorting to the experts at

Math Forum Discussions - comp.soft-sys.math.mathematica

I'm 100% confident those guys could write a one, two or three line routine to accomplish the task except it's going to be much more compact and cryptic in the syntax. Just e-mail them a nice, clear explanation of it.
• Mar 16th 2010, 04:31 AM
battingozzy
Pest Control London - Professional Pest Control Services covering all London Boroughs. We specialise in
Bed Bugs London
• Mar 16th 2010, 04:49 AM
slevvio
I hate mathematica with a passion, but I have to use it for this :(

I was wondering what this meant is there not one too few brackets here?
myset=Table[mytest=XX[[n,j]];

im just getting lots of orange lines and plus signs when I try to fix things. Thanks for the advice anyway :)
• Mar 16th 2010, 06:35 AM
shawsend
Mathematica uses double brackets to represent arrays. Single brackets are objects like functions and sub-scripted arrays. Just look at the Select lines one by one. Here, I'll do the first one. I removed that Table construct and just let mytest equal to the first element in myclist by the construct "mytest=myclist[[1,1]]"

xset = {1, 2, 3, 4}
myclist = Outer[{#1, #2} &, xset, xset]
rlist = {{1, 2}, {2, 3}, {3, 4}, {4, 1}};
slist = {{2, 1}, {3, 2}, {4, 3}, {1, 4}};
mymatch = {};
mytest = myclist[[1, 1]];
(*select from slist,those elements for which the first term is equal to the first term in mytest*)
list1 = Select[slist, #1[[1]] == mytest[[1]] &]

That means select from slist, all the {x,y} pairs one by one and check them against mytest[[1]]. The # (or #1 same dif) is the place-holder for each element in slist. Now, #[[1]] means the x-term of the {x,y} pair and mytest[[1]] means the x component of each of the {x,y} pair that's being selected in the mytest=myclist[[n,j]] line. So for each mytest in myclist, look at all the {x,y} pairs in slist, and select only those which have the x terms equal. If I wanted to compare the y terms in slist to the x-terms in mytest, that would be:

list1 = Select[slist, #1[[2]] == mytest[[1]] &]

or the y-terms in slist to the y-terms in mytest:

list1 = Select[slist, #1[[2]] == mytest[[2]] &]

and this would be an error:

list1 = Select[slist, #1[[3]] == mytest[[1]] &]

since the items we're checking are of the form {n,j} and so have only two elements.

Now, note I remove the semicolons in the line I want Mathematica to print the output. Just run that part and see if it's what you want. Get that straight, and then move to the next Select, remove the semicolon and check if it's getting what you want. If not, then it's probably due to the item in the #[[n]] or mytest[[n]] constructs that you need to adjust. Do that all the way down. I may look at it further too.
• Mar 16th 2010, 11:00 AM
slevvio
Had a few discussions with other people - there is a simple way to encode this in mathematica.

If you write the relations as 4x4 matrices, so you put a 1 as the (x,y)th entry for each (x,y) in the relation, and a zero otherwise.

so if you have relations R,S, the matrix $M_R M_S$ will give you information about $S \bullet R$ (where the superscript means the matrix you get from that relation. the (x,y)th entry in the multiplied matrix tells you how many z's join the x and y, so if there is a 1 in the final matrix, that pair is in $S \bullet R$.