From: Paul Slevin on
Hello - I was wondering if somebody could help me with this problem. I have the set X = {1,2,3,4}, and have computed the Cartesian Product X^2.

Let R = {(1,2),(2,3),(3,4),(4,1)}
S = {(2,1),(3,2),(4,3),(1,4)}

be relations on X.

I want to compute the product S o R = {(x,y) in X^2: there exists one z in X (for each (x,y) pair) such that (x,z) is in R and (z,y) is in S }.

i.e. I want mathematica to run through each (x,y) pair in X^2 and check if that pair is in S o R.

To clarify, (1,1) is in S o R, because (1,4) is in S and (4,1) is in R. 4 joins 1 to 1, and it is the only element that does so (so it is unique).

Clearly the output I want should be the diagonal on X := {(1,1),(2,2),(3,3),(4,4)}. However I am really struggling here to get mathematica to work.

Could anybody help me with this idea? Thanks very much.

From: Bob Hanlon on

R = {{1, 2}, {2, 3}, {3, 4}, {4, 1}};

S = {{2, 1}, {3, 2}, {4, 3}, {1, 4}};

f[{a_, b_}, {b_, c_}] = {a, c};
f[__] = Sequence[];

Flatten[Outer[f, R, S, 1], 1]

{{1, 1}, {2, 2}, {3, 3}, {4, 4}}


Bob Hanlon

---- Paul Slevin <slevvio(a)hotmail.com> wrote:

=============
Hello - I was wondering if somebody could help me with this problem. I have the set X = {1,2,3,4}, and have computed the Cartesian Product X^2.

Let R = {(1,2),(2,3),(3,4),(4,1)}
S = {(2,1),(3,2),(4,3),(1,4)}

be relations on X.

I want to compute the product S o R = {(x,y) in X^2: there exists one z in X (for each (x,y) pair) such that (x,z) is in R and (z,y) is in S }.

i.e. I want mathematica to run through each (x,y) pair in X^2 and check if that pair is in S o R.

To clarify, (1,1) is in S o R, because (1,4) is in S and (4,1) is in R. 4 joins 1 to 1, and it is the only element that does so (so it is unique).

Clearly the output I want should be the diagonal on X := {(1,1),(2,2),(3,3),(4,4)}. However I am really struggling here to get mathematica to work.

Could anybody help me with this idea? Thanks very much.



From: Peter Pein on
Or define e.g. SmallCircle:

In[1]:= X={1,2,3,4};
R=Transpose[{X,RotateLeft[X]}];
S=Reverse/@R;


SmallCircle[{} | Sequence[], rel: {{_Integer, _} ...}] := rel;

SmallCircle[rel1: ({{_Integer, _} ...} ...), rel2: {{_Integer, _} ...}] :=
SmallCircle[Sequence @@ Most[{rel1}],
rel2 /. {a_Integer, b_} :> {a, b /. Rule @@@ Last[{rel1}]}
]


In[5]:= S \[SmallCircle] R
Out[5]= {{1, 1}, {2, 2}, {3, 3}, {4, 4}}
(* enter the following as SmallCircle[R, S, R, S, R] and press
<Strg><Shift><n> while the cursor is in the input-line *)
In[6]:= R \[SmallCircle] S \[SmallCircle] R \[SmallCircle] S
\[SmallCircle] R
Out[6]= {{1, 2}, {2, 3}, {3, 4}, {4, 1}}

Peter

On 17.03.2010 12:05, Bob Hanlon wrote:
> R = {{1, 2}, {2, 3}, {3, 4}, {4, 1}};
>
> S = {{2, 1}, {3, 2}, {4, 3}, {1, 4}};
>
> f[{a_, b_}, {b_, c_}] = {a, c};
> f[__] = Sequence[];
>
> Flatten[Outer[f, R, S, 1], 1]
>
> {{1, 1}, {2, 2}, {3, 3}, {4, 4}}
>
>
> Bob Hanlon
>
> ---- Paul Slevin<slevvio(a)hotmail.com> wrote:
>
> =============
> Hello - I was wondering if somebody could help me with this problem. I have the set X = {1,2,3,4}, and have computed the Cartesian Product X^2.
>
> Let R = {(1,2),(2,3),(3,4),(4,1)}
> S = {(2,1),(3,2),(4,3),(1,4)}
>
> be relations on X.
>
> I want to compute the product S o R...