Message Boards Message Boards

1
|
14784 Views
|
4 Replies
|
7 Total Likes
View groups...
Share
Share this post:
GROUPS:

How to get rid of the outermost curly brackets of a Table?

Posted 10 years ago

Hi,

say I have a table

{a,b,c,d}

How can I get rid of the outermost brackets here, so that I have and expression of the form

a,b,c,d

I would need this very often in order to construct a list of arguments, which a function should take.

StringReplace does not do the job in most cases, since the elements a,b,c,d might be lists their selves, i.e. they might contain curly brackets as well.

Thanks for help!

POSTED BY: Gernot H
4 Replies
Posted 10 years ago

Apply ( @@ ) can be used to replace the head List with Sequence, which is the head for a sequence of items as used for arguments.

In[1]:= Range[1, 10, 1]

Out[1]= {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

In[2]:= Range[Sequence @@ {1, 10, 1}]

Out[2]= {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
POSTED BY: David Keith
Posted 10 years ago

Hi Gernot,

In Mathematica, everything is an expression of some kind. The kind is identified by its Head. The Head of {a,b,c} is List. What Apply does is to replace the Head with something else. In this case, List is replaced by Sequence, which is the head of a sequence of items.

However, some functions look at their arguments before evaluation, and will object. This can be remedied by forcing the evaluation.

In[1]:= (* f expects 3 arguments and passes them to g *)
f[a_, b_, c_] := g[a, b, c]

In[2]:= f[a, b, c]

Out[2]= g[a, b, c]

In[3]:= (* this is returned unevaluated because there is no \
definition for f with 1 argument *)
f[{a, b, c}]

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

In[4]:= (* this works fine *)
f[Sequence @@ {a, b, c}]

Out[4]= g[a, b, c]

In[5]:= (* so does this because f is willing to accept lists as the \
individual arguments *)
f[{1, 2}, {3, 4}, {5, 6}]

Out[5]= g[{1, 2}, {3, 4}, {5, 6}]

In[6]:= (* and so does this *)
f[Sequence @@ {{1, 2}, {3, 4}, {5, 6}}]

Out[6]= g[{1, 2}, {3, 4}, {5, 6}]

In[7]:= (* here is a built in function *)
Sum[x^2, {x, 1, 3}]

Out[7]= 14

In[8]:= (* this does not work because Sum looks at its arguments \
before evalauating *)
Sum[x^2, Sequence @@ {{x, 1, 3}}]

During evaluation of In[8]:= Sum::vloc: The variable Sequence@@{{x,1,3}} cannot be localized so that it can be assigned to numerical values. >>

Out[8]= 
\!\(\*UnderscriptBox[\(\[Sum]\), \(Sequence @@ {{x, 1, 3}}\)]\)x^2

In[9]:= (* but if we force the evaluation, Sum is happy *)

In[10]:= Sum[x^2, Sequence @@ {{x, 1, 3}} // Evaluate]

Out[10]= 14
POSTED BY: David Keith
Posted 10 years ago

And more to the point:

In[11]:= ranges = {{x, 1, 7, 2}, {y, 2, 8, 2}};

In[13]:= Table[{x, y}, Sequence @@ ranges // Evaluate]

Out[13]= {{{1, 2}, {1, 4}, {1, 6}, {1, 8}}, {{3, 2}, {3, 4}, {3, 
   6}, {3, 8}}, {{5, 2}, {5, 4}, {5, 6}, {5, 8}}, {{7, 2}, {7, 4}, {7,
    6}, {7, 8}}}

;-)

POSTED BY: David Keith
Posted 10 years ago

But this only works if i want to Apply the whole list as argument, right?

What for example if I have something like

Table[ something , {i1,n1},{i2,n2},{i3,n3}]

I want to generate {i1,n1}},{i2,n2},{i3,n3} as a sequence, and use it as such.

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

Group Abstract Group Abstract