MathGroup Archive 2000

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Modification to Thread or MapThread

  • To: mathgroup at smc.vnet.net
  • Subject: [mg26398] Re: [mg26380] Modification to Thread or MapThread
  • From: BobHanlon at aol.com
  • Date: Wed, 20 Dec 2000 00:21:26 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

V1 = Array[v, {4, 3}];

container2 = {c21, c22, c23, c24};

You left off a set of list brackets in your expression

soln1 = f @@@ Transpose[{V1, Table[container2, { Length[V1] } ]}];

soln2 = f[#, container2] & /@ V1;

soln3 = Thread[f[V1, container2], List, 1];

soln4 = ReleaseHold[Thread[f[V1, Hold[container2]]]];

soln5 = Module[{c}, Thread[f[V1, c]] /. c -> container2];

soln6 = Module[{c}, Thread[f[V1, c @@ container2]] /. c -> List];

Pick a solution and define your function

threadFirst[func_Symbol, v_List, const_] := (func[#, const] & /@ v);

soln1 == soln2 == soln3 == soln4 == soln5 == soln6 == 
  threadFirst[f, V1, container2]

True


Bob Hanlon

In a message dated 12/16/00 3:22:36 AM, cjohnson at shell.faradic.net writes:

>I have a vector V1 of objects of type container1 and a single object of
>container2.
>
>I want to create the vector resulting from applying f over each
>item in V1 and container2, or...
>{ f[ V1[[1]], container2], f[ V1[[2]], container2], ...}
>
>Best case and most intuitive to me is simply try f[V1, container2].  I
>believe this would work if the function was "Listable", but it isn't.
>Unfortunately, the way f is defined, this doesn't work.  The solution so
>far (Thanks to help from this list!) is to use the following syntax...
>
>f @@@ Transpose[V1, Table[container2, { Length[V1] } ]
>
>Is there a better way?  Can I generalize the definitions of functions so
>they can understand this type of syntax?  Or even create my own function
>which automagically threads vectors of objects and single objects?  
>
>The main intent, I suppose, is to avoid generating a table that seems
>unnecessary and feels sloppy.  If avoiding the table creation isn't
>possilble, getting it done behind the scenes would be an improvement to
>me.  A little paint over the duct tape, as it were.
>


  • Prev by Date: Re: physical colors and Mathematica colors
  • Next by Date: Re: Question: numerical solution of nonlinear differential equation
  • Previous by thread: Re: Modification to Thread or MapThread
  • Next by thread: Re:Modification to Thread or MapThread