Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2006
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2006

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

Search the Archive

Re: Beginner--Using subscripts in vectors

  • To: mathgroup at smc.vnet.net
  • Subject: [mg64507] Re: Beginner--Using subscripts in vectors
  • From: Paul Abbott <paul at physics.uwa.edu.au>
  • Date: Sun, 19 Feb 2006 05:36:25 -0500 (EST)
  • Organization: The University of Western Australia
  • References: <dt6kup$nj8$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

In article <dt6kup$nj8$1 at smc.vnet.net>, kt at squeakydolphin.com wrote:

> I'm taking a class in Evolutionary Computation and I would like to provide 
> the other students with some plots of around 20 2D functions that are defined 
> over any length vector (of real numbers).  I've defined a few of them already 
> and it is more laborous that I would think it need be.  

To define functions over vectors (lists) you should read section 2.2 
(especially 2.2.3) of the Mathematica book. In particular, look up Map, 
Apply, NestList and FoldList.
 
> What I'm currently doing is describing the function using standard 
> mathematical notation in a text cell and then defining it again in a way that 
> Mathematica can understand it in the next cell. The real problem is doing 
> summations over vector components.  I would like "Subscript[x, i]" to be 
> interpretted as "Part[x, i]" and n to be interpretted as "Length[x]" when I 
> do the plot.  I would then like to be able to tell mathematica to plot it 
> over all two dimensional vectors where "Subscript[x, i]" ranges from -10.0 to 
> 10.0 for each i.
> 
> Is there anyway I can do this?

Yes -- there are many ways that you can do this. For example, defining

  Subscript[x_, i_] := Part[x, i]

then if 

  x = {a, b, c};

you will see that

  Subscript[x, 3]

returns c. 

Note that if you subscript a list variable with [[i]] it will extract 
the i-th component of the list -- so the notation that you are after is 
"almost" built-in.

> An example:
> 
> Schwefel's Problem 1.2 is defined as:
> 
> \!\(f \((x)\)\  = \ \[Sum]\+\(i = 1\)\%n\((\[Sum]\+\(j = 1\)\%i x\_j)\)\^2\)

Essentially, this function sums the squares of the cumulative sums of 
the elements of the list x. You could do a direct translation:

  f[x_List] := With[{n = Length[x]},
    Sum[Sum[Subscript[x, j], {j, 1, i}]^2, {i, 1, n}]]

But direct code translation of such expressions can be inefficient.

A functional approach is to note that cumulative operations are achieved 
using FoldList, and to compute the sum over a list one can use Tr 
(trace) or Total -- so Schwefel's function can be efficiently coded as

  f[x_List] := Total[FoldList[Plus, 0, x]^2]

Although it takes some getting used to, this code is short and elegant. 
No need to worry about subscripted variables of the length of the sums.
 
> I would like to be able to pass this in to Mathematica and get a 3D plot out 
> of this function for i ranging from 1 to 2 and Subscript[x, i] ranging from 
> -10 to 10 without having to translate the formula again..
> 
> Oh, I don't mind changing f(x) to be f[x].  I know that's how mathematica 
> wants it.

In TraditionalForm you can use f(x) if you like.

Cheers,
Paul

> Link to the forum page for this post:
> http://www.mathematica-users.org/webMathematica/wiki/wiki.jsp?pageName=Special
> :Forum_ViewTopic&pid=8365#p8365
> Posted through http://www.mathematica-users.org [[postId=8365]]

_______________________________________________________________________
Paul Abbott                                      Phone:  61 8 6488 2734
School of Physics, M013                            Fax: +61 8 6488 1014
The University of Western Australia         (CRICOS Provider No 00126G)    
AUSTRALIA                               http://physics.uwa.edu.au/~paul


  • Prev by Date: Re: Using a text editor like interface for Mathematica?
  • Next by Date: Re: SetOptions with Column
  • Previous by thread: Beginner--Using subscripts in vectors
  • Next by thread: Greek-Letter Bug? Replacing Print