# Scalars Instead of Lists with One Element

112 views

### Gregory Lypny

Nov 20, 2007, 3:50:52 AM11/20/07
to
Hello everyone,

Say I have the list x={2, 7}. Is there a command I can use to get 2
rather than {2} when I ask for x[], and 7 rather than {7} when I
ask for x[]? Doing so would solve my problem with my tables being
too deep to be able to cut and paste directly into the table objects
of other applications like word processors and spread sheets.

Regards,

Gregory

### Thomas E Burton

Nov 21, 2007, 2:41:48 AM11/21/07
to
[==2 && x[]==7. I recall an earlier post of yours, however, in
which you wanted lists of one element to be replaced by that element.
The following simple rule does this

r1={e_}:>e

For example,

In:= {{3,4},{2},{1,2},{3}}/.r1
Out= {{3,4},2,{1,2},3}

This simple rule and application can be tweaked to avoid failures in

### Sseziwa Mukasa

Nov 21, 2007, 2:42:50 AM11/21/07
to

On Nov 20, 2007, at 3:46 AM, Gregory Lypny wrote:

> Hello everyone,

>
> Say I have the list x={2, 7}. Is there a command I can use to get 2
> rather than {2} when I ask for x[], and 7 rather than {7} when I
> ask for x[]? Doing so would solve my problem with my tables being
> too deep to be able to cut and paste directly into the table objects
> of other applications like word processors and spread sheets.

x[] is 2 and x[] is 7. x[[{1}]] is {2} and x[[{2}]] is {7}.

Regards,

Ssezi

### Gregory Lypny

Nov 21, 2007, 2:46:54 AM11/21/07
to
Yes, I'm trying to reproduce the {.} problem with some regularity.
Be that as it may, it's not clear why, when I iterate over {i,
1,...,8} and {j ,1,...,8}, I get tables with dimensions {8, 8, 1}
rather than {8, 8}. That's the cut-and-paste problem. Sigh.

Gregory

### Jean-Marc Gulliet

Nov 21, 2007, 2:47:56 AM11/21/07
to
Gregory Lypny wrote:

> Say I have the list x={2, 7}. Is there a command I can use to get 2
> rather than {2} when I ask for x[], and 7 rather than {7} when I

Here, I do not follow (or understand) you. You have a list of scalars
and taking any one of its elements with *Part* will produce a scalar and
not a list of one element. Or you may have something else in mind... For
instance,

In:= x = {2, 7}

Out= {2, 7}

In:= x[]

Out= 2

In:= x[]

Out= 7

As you can see above, no list is returned in this case.

> ask for x[]? Doing so would solve my problem with my tables being
> too deep to be able to cut and paste directly into the table objects
> of other applications like word processors and spread sheets.

You could use the system variable *\$Post* and set up your own post
processing function. (You might be interested in \$PrePrint too.) Here is
an example of such a function that should do what you are looking for,
though you may want to add some additional tests (or remove few of them).

In:= \$Post =
If[Head[#] === List && Length[#] == 1 &&
Depth[#] < 3, #[], #] &;

prob = {.4, {.6}, {.4, .6}, {{.4, .6}}};

prob[] (* Scalar: not a list, depth one *)
prob[] (* List of length one and depth two *)
prob[] (* List of length two and depth two *)
prob[] (* List of length one and depth three *)

\$Post =. (* Reset \$Post *)

Out= 0.4

Out= 0.6

Out= {0.4, 0.6}

Out= {{0.4, 0.6}}

Best regards,
--
Jean-Marc

Nov 21, 2007, 2:50:00 AM11/21/07
to
Gregory Lypny wrote:
> Hello everyone,
>
> Say I have the list x={2, 7}. Is there a command I can use to get 2
> rather than {2} when I ask for x[], and 7 rather than {7} when I

x[] *does* return 2 and not {2}, and x[] *does* return 7.

--
University of Vermont

### Szabolcs Horvát

Nov 21, 2007, 2:54:04 AM11/21/07
to

I am not sure what you mean, since

In:= x={2,7}
Out= {2,7}

In:= x[]
Out= 2

--
Szabolcs

### Matthew Fairtlough

Nov 21, 2007, 3:00:10 AM11/21/07
to
I don't get {2} when I calculate {2,7}[], I get just 2, and this is
the documented behaviour. Could it be that the tables you have
constructed are not quite as you think? Try FullForm if you are not sure.

Matthew.

In:= x = {2, 7}
x[]

Out= {2, 7}

Out= 2

### Bill Rowe

Nov 21, 2007, 3:18:29 AM11/21/07
to
On 11/20/07 at 3:46 AM, gregor...@videotron.ca (Gregory Lypny)
wrote:

>Say I have the list x={2, 7}. Is there a command I can use to get 2
>rather than {2} when I ask for x[], and 7 rather than {7} when I
>ask for x[]? Doing so would solve my problem with my tables
>being too deep to be able to cut and paste directly into the table
>objects of other applications like word processors and spread sheets.

Your wording is very confusing or perhaps you haven't stated
your problem quite right. When you say you "ask for x[]" what
does that mean if not entering x[] somewhere? And if that is
what you mean, then since

In:= x = {2, 7};

In:= x[]

Out= 2

then there is something wrong with what you wrote above.

Generally, it is better to post some code to eliminate this kind
of confusion.
--
To reply via email subtract one hundred and four

### Jens-Peer Kuska

Nov 21, 2007, 3:31:46 AM11/21/07
to
Hi,

with:

x={2,7}

for x[] 2 and for x[] 7 and *not*
{1} or {7} ...

Regards
Jens

### Szabolcs Horvát

Nov 21, 2007, 6:05:20 AM11/21/07
to
Gregory Lypny wrote:
> Yes, I'm trying to reproduce the {.} problem with some regularity.
> Be that as it may, it's not clear why, when I iterate over {i,
> 1,...,8} and {j ,1,...,8}, I get tables with dimensions {8, 8, 1}
> rather than {8, 8}. That's the cut-and-paste problem. Sigh.
>

Gregory,

Table[i*j, {i, 8}, {j, 8}] returns a list with dimensions {8, 8}. But
if the expression inside Table returns a single-element list, then the
table will have dimensions {8, 8, 1}. Example: Table[{i*j}, {i, 8}, {j,
8}]. So you could try using

Table[
First[(complicated expression that returns a single element list)],
{i, 8}, {j, 8}
]

Szabolcs

### Gregory Lypny

Nov 22, 2007, 4:51:59 AM11/22/07
to

I've found the problem. Say you want to sum a list whose elements are
13 and 9. Mathematica will return a list with one element, {22},
rather than 22 if the original list is specified as a 2x1 column vector.

x = {{13}, {9}}; y = Total@x >>> returns {22}

This also happens if you write x as I have above, or you use
Mathematica's Insert menu to create a more visually appealing column
vector, but it does not happen if you define x as a 2x1 array using
the Array command.

I think it will happen with any matrix calculation whose result should
otherwise be a scalar. If we now let y be the row vector {1, 1} then

y.x >>> returns {22}

The upshot of this is that any table that is created from calculations
that make use of column vectors or matrix math will likely have a
depth greater than 3, and you won't be able to cut and paste directly
into a word processor or spreadsheet.

I'm going to have a look at some of the work-arounds that have been

Gregory

### Szabolcs Horvát

Nov 23, 2007, 5:30:15 AM11/23/07
to
Gregory Lypny wrote:
> Thanks everyone for your insights,
>
> I've found the problem. Say you want to sum a list whose elements are
> 13 and 9. Mathematica will return a list with one element, {22},
> rather than 22 if the original list is specified as a 2x1 column vector.
>
> x = {{13}, {9}}; y = Total@x >>> returns {22}
>
> This also happens if you write x as I have above, or you use
> Mathematica's Insert menu to create a more visually appealing column
> vector,

> but it does not happen if you define x as a 2x1 array using
> the Array command.

Yes, it does:

In:= Array[# &, {2, 1}]
Out= {{1}, {2}}

In:= Total[%]
Out= {3}

>
> I think it will happen with any matrix calculation whose result should
> otherwise be a scalar. If we now let y be the row vector {1, 1} then
>
> y.x >>> returns {22}
>

This is because Mathematica works with tensors of arbitrary dimensions,
not just matrices or vectors. So in Mathematica it does not make sense
to speak about column or row vectors---all vectors (1D arrays) are
treated in the same way. {{1},{2}} is a 2 by 1 matrix and {{1, 2}} is a
1 by 2 matrix, not vectors. (Of course all this is just a question of
naming conventions. I am just trying to explain why Mathematica works
this way.)

> The upshot of this is that any table that is created from calculations
> that make use of column vectors or matrix math will likely have a
> depth greater than 3, and you won't be able to cut and paste directly
> into a word processor or spreadsheet.

When you multiply a vector with a matrix, you get a vector. If you
multiply a vector with a vector, you get a scalar:

In:= {x, y}.{{a, b}, {c, d}}
Out= {a x + c y, b x + d y}

In:= {{a, b}, {c, d}}.{x, y}
Out= {a x + b y, c x + d y}

In:= {x, y}.{a, b}
Out= a x + b y

If the distinction between column and row vectors is important, just
work with matrices, and you'll always get matrices as the result (no
scalars):

In:= {{x, y}}.{{a}, {b}}
Out= {{a x + b y}}

In:= {{a}, {b}}.{{x, y}}
Out= {{a x, a y}, {b x, b y}}

If it is not important to differentiate between row and column vectors,
then use 1D arrays to represent vectors. The Outer product can be
calculated like this:

In:= Outer[Times, {a, b}, {x, y}]
Out= {{a x, a y}, {b x, b y}}

>
> I'm going to have a look at some of the work-arounds that have been
> suggested in this thread and my related thread "Copy and Pasting