MathGroup Archive 1998

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

Search the Archive

Thread



Hi Groupers:

I've written a program to calculate the forces on charged particles. The
program works well except that parts are less than elegant.  The key
questions I have concern the Thread function:

==========================================

Question 1:
------------

In[1]:=
Thread[f[a,{b,c}]]

Out[1]=
{f[a,b],f[a,c]}

We see that if "a" does not have head "List" it is automatically
replicated so that Thread works.
Now if a, b and c are defined as follows:

In[2]:=
a={q1,{x1,y1,z1}};b={q2,{x2,y2,z2}};c={q3,{x3,y3,z3}};

I would LIKE to have the following output when I apply Thread:

{f[{q1,{x1,y1,z1}},{q2,{x2,y2,z2}}],f[{q1,{x1,y1,z1}},{q3,{x3,y3,z3}]}

However, since "a" now has head "List", Thread now gives me:

In[3]:=
Thread[f[a,{b,c}]]

Out[3]=
{f[q1,{q2,{x2,y2,z2}}],f[{x1,y1,z1},{q3,{x3,y3,z3}}]}

Is there a way to force Thread to give me what I want?

================================================================ I can
make a workaround but its not very elegant:

In[ ]:=
t=Table[a,{2}];
Thread[f[t,{b,c}]]

Out[]=
{f[{q1,{x1,y1,z1}},{q2,{x2,y2,z2}}],f[{q1,{x1,y1,z1}},{q3,{x3,y3,z3}}]}

================================================================

Question 2:
-------------

I would like to be able to Thread g across two lists as follows:

In[1]:=
ccs={a,b}; oc={x,y};
 Thread[g[ccs,oc]]

Out[1]=
{g[a,x],g[b,y]}

But unfortunately, since ccs and oc are lists, and since g is designed
to operate on lists, g executes before the thread takes place.

If g is undefined everything is ok:

In[3]:=
a={a1,a2}; b={b1,b2};  x={x1,x2}; y={y1,y2}; Thread[g[ccs,oc]]

Out[3]=
{g[{a1,a2},{x1,x2}],g[{b1,b2},{y1,y2}]}


If I now define g:

In[4]:=
g[x1_List,x2_List]:=x1+x2^2

I would now LIKE the output of the Thread statement to be:

In[6]:=
{g[{a1,a2},{x1,x2}],g[{b1,b2},{y1,y2}]}

Out[6]=
{{a1 + x1^2, a2 + x2^2}, {b1 + y1^2, b2 + y2^2}}

Instead the Thread statement now gives me:

In[7]:=
Thread[g[ccs,oc]]

Out[7]=
{{a1 + x1^2, b1 + y1^2}, {a2 + x2^2, b2 + y2^2}}

The reason (I believe) is that the g function executes before the Thread
operates.  The function "List" is  threaded over the sublists as in the
following:

In[9]:=
Thread[{{a1 + x1^2, a2 + x2^2}, {b1 + y1^2, b2 + y2^2}}]

Out[9]=
{{a1 + x1^2, b1 + y1^2}, {a2 + x2^2, b2 + y2^2}}

Is there a way of forcing Thread to keep its argument unevaluated until
after it operates?

=============================================== I am currently using the
following workaround, but I don't like it:

In[10]:=
Clear[f]
Thread[f[ccs,oc]] /. f->g

Out[11]=
{{a1 + x1^2, a2 + x2^2}, {b1 + y1^2, b2 + y2^2}}
===============================================

Is there a better way?

Cheers,


Des Penny
Physical Science Dept.
Southern Utah University
Cedar City, UT 84720

Office: (435) 586-7708
FAX:  (435) 865-8051
email: penny@suu.edu

Home: (435) 586-2286
email: dpenny@iname.com




  • Prev by Date: Re: Altering terms of an Expression
  • Next by Date: Re: Display - Funkion
  • Prev by thread: New version of OpenGL viewer for Mathematica
  • Next by thread: Re: Thread