Group Abstract Group Abstract

Message Boards Message Boards

0
|
5.8K Views
|
3 Replies
|
1 Total Like
View groups...
Share
Share this post:

Using Thread properly?

Posted 9 years ago

I guess I don't understand how threading works. Suppose I define a function

f[v__, c__] := v + c;

Here v is intended to be a vector, and c is a scalar::

vec = {a, b, c, d};

f [vec, x]

then yields

{a + x, b + x, c + x, d + x}

Suppose I want to repeat this for a sequence of c's. I would write:

cs = {e, f, g}

Table[ f [vec, cs[[i]] ], {i, Length[cs]}]

I then get

{{a + e, b + e, c + e, d + e}, {a + f, b + f, c + f, d + f}, {a + g,  b + g, c + g, d + g}}

Now I'd like to do this same thing in parallel:

Thread[ f [vec, cs], List, {2}]

My understanding of this statement is that I want to evaluate f [vec, c] for each c contained in {e, f, g}.

Instead what I get is:

Thread::tdlen: Objects of unequal length in {a,b,c,d}+{e,f,g} cannot be combined. >>

I attach a pdf version of my Mathematica session. If anyone can clarify this for me, I would appreciate it. Thanks for reading this.

Attachments:
POSTED BY: Herbert Swan
3 Replies
    f[vec, #] & /@ cs
(* {{a + e, b + e, c + e, d + e}, {a + f, b + f, c + f, d + f}, {a + g, b + g, c + g, d + g}}
POSTED BY: Murray Eisenberg

Thank you for your help.. That solved my problem.

POSTED BY: Herbert Swan

I don't think Thread is meant as parallelization. (where Thread normally means indeed creating a thread). I think you mean ParallelTable?

http://reference.wolfram.com/language/ref/ParallelTable.html

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