Re: Programmatically creating functions of many
- To: mathgroup at smc.vnet.net
- Subject: [mg113670] Re: Programmatically creating functions of many
- From: Bob Hanlon <hanlonr at cox.net>
- Date: Sun, 7 Nov 2010 05:10:25 -0500 (EST)
Use SetDelayed rather than Set.
Also, I recommend that instead of creating a list and applying Function to it, just use Function.
graph = GraphData[
{"Grid", {2, 2}}, "AdjacencyMatrix"] //
Normal;
pairs = Position[graph, 1];
f := Function[{x},
And @@ Unequal @@@ Map[x[[#]] &, pairs, {2}]]
f[{x1, x2, x3, x4}]
x1 != x2 && x1 != x4 && x2 != x1 && x2 != x3 && x3 != x2 && x3 != x4 &&
x4 != x1 &&
x4 != x3
Note that this contains redundancies, e.g., both x1 != x2 and x2 != x1
Consequently, redefine pairs as
pairs = Union[Position[graph, 1],
SameTest -> (#1 == Reverse[#2] &)];
f[{x1, x2, x3, x4}]
x1 != x2 && x1 != x4 && x2 != x3 && x3 != x4
Bob Hanlon
---- Yaroslav Bulatov <yaroslavvb at gmail.com> wrote:
=============
Suppose I have a graph, and I'd like to create a function which checks
whether given assignment of nodes is a proper labeling -- ie, no two
adjacent nodes get the same number. One could do something like the
following, but that gives Part warnings. Is there a preferred
approach?
graph = GraphData[{"Grid", {2, 2}}, "AdjacencyMatrix"] // Normal;
f = Function @@ {{x}, And @@ (x[[First[#]]] != x[[Last[#]]] & /@
Position[graph, 1])}
This is actually a problem that I periodically come across. Part
approach causes warnings meanwhile something like Function @@
{Subscript[x,#]&/@Range[n], ...} doesn't work. What are typical ways
of generating multivariate functions automatically?
----
Yaroslav
http://stackoverflow.com/users/419116/yaroslav-bulatov