       Re: Scalars Instead of Lists with One Element

• To: mathgroup at smc.vnet.net
• Subject: [mg83565] Re: Scalars Instead of Lists with One Element
• From: Szabolcs Horvát <szhorvat at gmail.com>
• Date: Fri, 23 Nov 2007 05:29:10 -0500 (EST)
• References: <fhu75c\$72j\$1@smc.vnet.net> <4742F294.5050606@gmail.com> <fi3jfv\$b34\$1@smc.vnet.net>

```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
>
>
> Gregory
>

--
Szabolcs

```

• Prev by Date: Re: Get list of function variables?
• Next by Date: Re: Dynamic Timeout
• Previous by thread: Re: Scalars Instead of Lists with One Element
• Next by thread: Solving Tanh[x]=Tanh[a]Tanh[b x + c]