Group Abstract Group Abstract

Message Boards Message Boards

0
|
302 Views
|
11 Replies
|
7 Total Likes
View groups...
Share
Share this post:

Assigning values to indexed lists

Posted 17 days ago

Hi there,

I have got a problem that seems quite trivial but I cannot seem to solve it so I would be grateful for any help.

I am trying to assign values to an indexed list a[ i ], (0 < i < 9) but when I ask Mathematica

Do[a[ i ] [[ i ]] = 2, {i , 1 ,8 }] 

It gives me an error and says that a[ i ] is not a symbol.

Can someone tell me what I am doing wrong?

Many thanks

POSTED BY: Richard Stedman
11 Replies

That's great Eric. I think I should be able to do what I want now. I will post again if I run into more trouble. I think I need to get out of the 'looping mindset'.

POSTED BY: Richard Stedman
Posted 4 days ago

Once you have the structure, you can create whatever labels you want. In my example, the symbol result was assigned a value that was a list of all of the desired vectors. So, result[[1]] could be the label for the first vector, etc. That means that for the example you just gave, you could use this:

quad = Sum[ZZ . result[[i]], {i, 1, 1080}]

Also, you don't always need explicit names for things if you use more functional approaches rather than looping constructs. I'm not entirely sure what you're after, and again I'm not sure my result is correct, but you might be able to do something like this:

quad = Total[result . ZZ]

And another option is to use a different iterator specification form:

quad = Sum[ZZ . a, {a, result}]
POSTED BY: Eric Rimbey

POSTED BY: Michael Rogers

Thanks Eric. That is very helpful but your solution does not give a label to each vector. I need to do operations with them. For instance, if I define a vector

ZZ = {z[1], z[2], z[3], z[4], z[5], z[6], z[7], z[8]}

I can define the object

quad = Sum[ ZZ . a[ i ], {i, 1, 1080} ]

(where the a[ i ] are the vectors I am talking about).

Have you got another suggestion on how I could give each vector a label?

Thanks.

(Sorry I have not replied for a while, I am not online very much)

POSTED BY: Richard Stedman

You can create the list of vectors first, and then assign them to a[i]:

Clear[a];
vectors = Flatten[
  Table[UnitVector[8, i] -
    UnitVector[8, j], {i, 7}, {j, i + 1, 8}],
  1]
Evaluate[Array[a, Length[vectors]]] = vectors
a[3]
POSTED BY: Gianluca Gorni
Posted 15 days ago

Okay, got it. I'd suggest that when using Mathematica don't use looping constructs as your first strategy. There are usually built-in functions that already exist to do all of the "standard" things that are done with loops in other languages. I'll walk you through my strategy. Caveat: I don't know what a "norm-4 vector in E8" is, so I'm just guessing based on what I can infer from your comments so far. So, the result here is probably wrong, but hopefully it's close enough for you to modify it to give you what you want.

I'd start with the basic constraint for "norm-4 vector", which I'm guessing means something like "the sum of the squares of the terms is 4". So, I can just list out examples (excluding zero terms):

init = {{2}, {1, 1, 1, 1}, {1, 1, 1, -1}, {1, 1, -1, -1}, {1, -1, -1, -1}, {-1, -1, -1, -1}}

I could have just included the zero terms, but I know there is a PadRight function, which I'll use next:

vectors = PadRight[init, {Automatic, 8}]
(* {{2, 0, 0, 0, 0, 0, 0, 0}, 
    {1, 1, 1, 1, 0, 0, 0, 0}, 
    {1, 1, 1, -1, 0, 0, 0, 0}, 
    {1, 1, -1, -1, 0, 0, 0, 0}, 
    {1, -1, -1, -1, 0, 0, 0, 0}, 
    {-1, -1, -1, -1, 0, 0, 0, 0}} *)

Now we need all permutations for each of those, and we can do that with Permutations:

Permutations /@ vectors

This will give a set for each original vector, but we want just the list of vectors without that extra layer of structure, and Catenate will "join" together the sets, so I'd apply Catenate:

result = Catenate[Permutations /@ PadRight[init, {Automatic, 8}]]

This seems to be the form you're looking for--here's a sample of every 100th vector in the result:

result[[1 ;;  ;; 100]]
(* {{2, 0, 0, 0, 0, 0, 0, 0}, {1, 1, 0, 0, -1, 0, 1, 0}, 
    {-1, 0, 1, 1, 0, 0, 1, 0}, {0, 0, 1, 1, 1, 0, 0, -1}, 
    {1, -1, 0, 0, 0, -1, 0, 1}, {-1, -1, 0, 1, 0, 0, 1, 0}, 
    {0, 1, 0, 1, -1, 0, 0, -1}, {0, 0, 1, -1, 0, 1, -1, 0}, 
    {1, 0, -1, 0, 0, -1, -1, 0}, {-1, 0, 0, -1, 0, 1, -1, 0}, 
    {0, 0, 1, -1, -1, 0, 0, -1}, {0, -1, -1, 0, 0, -1, -1, 0}} *)

At this point, I know I've misunderstood the semantics, because

Length[result]
(* 1128 *)

instead of 1080. So either this gets you close enough to fix on your own, or else you can explain the actual semantics to me more specifically. Or maybe someone else here that has the mathematical knowledge can give you the correct derivation.

POSTED BY: Eric Rimbey

It's the coding, not the math, that needs explaining. However, here's a stab at a solution that might work, depending on how a[i] is defined:

Do[a[ i ]  = ReplacePart[a[i], i -> 2], {i , 1 ,8 }] 
POSTED BY: Michael Rogers

No, I am not trying to create a diagonal matrix, my simple example was just to illustrate my problem. I am actually trying to generate the 1080 norm-4 vectors of the E_8 matrix (the Lie algebra).

The next step, after the (2,0,0,0,0,0,0,0) (and permuations) would be vectors of the form (1,-1,0,0,0,0,0,0), (1,0,-1,0,0,0,0,0) etc until (0,0,0,0,0,0,1,-1). I know these are not norm-4 but if I could do these then I would be on the way to doing the norm-4 ones.

POSTED BY: Richard Stedman

You are triggering an error described in https://reference.wolfram.com/language/ref/message/General/setps.html You can assign a whole vector value to a[i]:

Do[a[i] = 2 UnitVector[8, i], {i, 8}]
POSTED BY: Gianluca Gorni
Posted 17 days ago

If you just want a diagonal matrix:

a = DiagonalMatrix[ConstantArray[2, 8]]

If you're trying to define a set of arrays and you want those arrays assigned to a[i], then you could do something like the following (after clearing the previous definition of a).

a[i_Integer /; 1 <= i <= 8] := a[i] = ReplacePart[{0, 0, 0, 0, 0, 0, 0, 0}, i -> 2]

But regardless, you can't do a raw assignment like a[2][[3]] = 2. If you had previously defined a to be an 8x8 matrix, then you could do something like a[[2,3]]=2. In a loop it would be

a = ConstantArray[0, {8, 8}];
Do[a[[i,i]] = 2, {i, 1, 8}]
POSTED BY: Eric Rimbey
Posted 17 days ago

Are you trying to create a diagonal matrix?

Is this your entire code (relevant to your question), or did you previously define a?

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