Automatically create array of named variables

  • Context: Mathematica 
  • Thread starter Thread starter Swamp Thing
  • Start date Start date
  • Tags Tags
    Array Variables
Click For Summary

Discussion Overview

The discussion revolves around the challenge of programmatically creating an array of named variables for use in Mathematica's Manipulate function, particularly in the context of generating multiple locators for a ListPlot. Participants explore various methods to automate this process and share their experiences with different approaches.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant presents an example using Manipulate with two locators and questions how to automate the creation of multiple named variables like p1, p2, etc.
  • Another participant introduces the concept of indexed variables using the Table function to create a list of variables but notes that it does not fit the required input format for Manipulate.
  • A suggestion is made to explore ToExpression and StringRiffle as potential solutions for feeding multiple arguments into a function.
  • Further exploration reveals that ListPlot can iterate over a list of expressions, and a participant shares a code snippet that demonstrates this with randomly generated points.
  • One participant expresses frustration over previous attempts to use Manipulate with a list of locators and acknowledges the subtlety of the syntax involved in the provided solution.

Areas of Agreement / Disagreement

Participants do not reach a consensus on a definitive solution to the problem of creating named variables programmatically. Multiple approaches are discussed, but uncertainty remains regarding the best method to achieve the desired outcome.

Contextual Notes

Participants express limitations in their understanding of the syntax required for Manipulate to accept a list of locators, indicating that there may be unresolved aspects of the implementation.

Swamp Thing
Insights Author
Messages
1,047
Reaction score
798
In the following example...
Code:
Manipulate[
             ListPlot[{p1, p2}, PlotRange -> {{0, 3}, {0, 3}}, Joined -> True],
             {{p1, {0.5, 0.5}}, Locator},
            {{p2, {1, 0.7}},   Locator}
         ]

... we have created variables p1 and p2 that are local to the Manipulate block, and we use them to set up two locators on the ListPlot. Now if we want to create a large number of locators, we could manually type in a large number of p1, p2, p3, p4 ... and so on. But is there a way to build these arrays of named variables programmatically?

The above is just one example, but there are other constructs in Mathematica where an input to a function must contain multiple named variables. It would be nice to automate the creation of large lists like that.
 
Physics news on Phys.org
A bit of googling turned up the concept of indexed variables. For example, we can do :
P = Table[Indexed[p, i], {i, 1, 7}]

which gives us ##P = \{ p_1, p_2, p_3, p_4, p_5, p_6, p_7 \}## , and we can use these as our seven variables.

This takes us one step forwards... But unfortunately, when we look at the locator specification at the end, it doesn't have a true list like format. It's just a series of extra arguments to the Manipulate. So the question turns into, "how can we feed in multiple arguments into a function automatically, when it can accept a series of arguments?"========== Edit: I realized that the indexed variables don't work as I thought ===
 
I'll look them up and give them a try... thanks.
 
ListPlot will iterate over a list of expressions:

Code:
P = Table[Indexed[p, i], {i, 1, 7}]
pointList = Table[{RandomReal[{0, 3}], RandomReal[{0, 3}]}, {i, 1, 7}]
Manipulate[
ListPlot[P, PlotRange -> {{0, 3}, {0, 3}},
  Joined -> True], {{P, pointList}, Locator}]
 
  • Love
Likes   Reactions: Swamp Thing
cabrera said:
ListPlot will iterate over a list of expressions:

Really neat. Thanks!

I had tried unsuccessfully to get Manipulate to accept a list of locators, but I gave up as I couldn't figure out the right syntax... and I wrongly concluded that it just isn't designed to take a bracketed list.

Even now, your code has some subtlety that I can barely wrap my head around.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 178 ·
6
Replies
178
Views
10K
Replies
0
Views
2K
  • · Replies 32 ·
2
Replies
32
Views
5K
  • · Replies 2 ·
Replies
2
Views
13K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
10K
  • · Replies 4 ·
Replies
4
Views
15K