Message Boards Message Boards

0
|
5739 Views
|
14 Replies
|
5 Total Likes
View groups...
Share
Share this post:

Assigning zero to a set of variables one at the time, two at the time ....?

Posted 8 years ago

Hello

I have a set of variables, say Table[Subscript[\[Theta], i], {i, 0, 16}] and I need to build a list with Subscript[\[Theta], 0] -> 0, then a list with Subscript[\[Theta], 1] -> 0, ... , a list with Subscript[\[Theta], 16] -> 0, then two variables equal zero, three variables equal zero and so on until all of them are set to zero (not needed, of course).

Is there a simple way to do that instead of using For?

many thanks

Ed

POSTED BY: Eduardo Mendes
14 Replies

Hi Ed,

I am not sure what exactly what you want to achieve. This is what I think you want. I will demonstrate it with a list of only 4 elements.

Rule[#, 0] & /@ # & /@ Subsets[Table[Subscript[\[Theta], i], {i, 0, 4}]]

enter image description here

So you first have a list where nothing is set to zero, then four lists where each of the elements is set to zero, then lists where all combinations of 2 elements are set to zero, etc.

If you actually want the lists this might work.

(Table[Subscript[\[Theta], i], {i, 0, 4}] /. #) & /@ (Rule[#, 0] & /@ # & /@ Subsets[Table[Subscript[\[Theta], i], {i, 0, 4}]])

enter image description here

Things like these might also work:

ReplacePart[Table[Subscript[\[Theta], i], {i, 0, 4}], Rule[#, 0] & /@ #] & /@ Subsets[Range[4]]

or

ReplacePart[Evaluate[Table[Subscript[\[Theta], i], {i, 0, 4}]], Flatten[Outer[Rule, #, {0}]]] & /@ Subsets[Range[4]]

This could certainly be made more elegant and faster, but for your 16 elements it is quite fast anyway.

AbsoluteTiming[rules = Rule[#, 0] & /@ # & /@ Subsets[Table[Subscript[\[Theta], i], {i, 0, 16}]];]

This evaluates in under one second.

Cheers,

M.

POSTED BY: Marco Thiel
Posted 8 years ago

Hi there

At first what I wanted was the first solution but all the other solutions will help me too.

Would you mind giving more details on what is going on the first solution? Although I have been using WM for quite some time I don't seem to be getting the hang of it.

Thank you ever so much.

Cheers

Ed

POSTED BY: Eduardo Mendes

Perhaps this version is easier to get the hang of:

Subsets[Table[Subscript[\[Theta], i] -> 0, {i, 5}]]

A Rest will get rid of the the meaningless empty rule set:

Rest@Subsets[Table[Subscript[\[Theta], i] -> 0, {i, 5}]]
POSTED BY: Gianluca Gorni

Hi Ed,

glad that it helped.

Would you mind giving more details on what is going on the first solution? Although I have been using WM for quite some time I don't seem to be getting the hang of it.

I think that the best way is to dissect the code. Let's take the first example of my post.

Rule[#, 0] & /@ # & /@ Subsets[Table[Subscript[\[Theta], i], {i, 0, 4}]]

The innermost bit is

Table[Subscript[\[Theta], i], {i, 0, 4}]

which simply creates a list of the thetas with the indices.

enter image description here

The next step is the Subsets command

Subsets[Table[Subscript[\[Theta], i], {i, 0, 4}]]

This makes all possible subsets of elements of the first list:

enter image description here

Now if you applied the Rule command directly to this you would get the "undesired" solution

Rule[#, 0] & /@ Subsets[Table[Subscript[\[Theta], i], {i, 0, 4}]]

enter image description here

which basically sets all sets to zero. What we really like to do is set each element of each set, one by one, to zero. Picking elements out of a list one by one can be done by

#& /@

Plugging all of this together gives:

Rule[#, 0] & /@ # & /@ Subsets[Table[Subscript[\[Theta], i], {i, 0, 4}]]]

and your result. This is not the most elegant way of doing it, but I hoped it would be quite intuitive.

Cheers,

Marco

POSTED BY: Marco Thiel

Yes, that is much more direct and elegant ....

POSTED BY: Marco Thiel
Posted 8 years ago

Thank you Marco for the nice explanation.

In order to see if I understand some of the usages of # & @ and stuff, I have modified Gianluca's (many thanks) to generate the full quintic polynomial of 5 variables.

Total @ ( 
  Apply[Times, #] & /@ Subsets[Table[Subscript[\[Theta], i], {i, 5}]])

Is that the right rationale? Is there a shorter version?

Many thanks

Ed

POSTED BY: Eduardo Mendes

Dear Ed,

well, yes your program does certainly do that. The Apply function is mapped onto each element of the list, i.e. onto each sublist. Then you total everything. You can get the same effect with this:

Total[Times @@@ Subsets[Table[Subscript[\[Theta], i], {i, 5}]]]

The LeafCount of both solutions is the same; mine's just a shorthand of yours. On the other hand, if that is what you want to achieve, then this might work, too:

Det[DiagonalMatrix[Table[1 + Subscript[\[Theta], i], {i, 6}]]] // Expand

Interestingly, that allows you to get yet another answer to your first question:

List @@ Expand[Det[DiagonalMatrix[Table[1 + Subscript[\[Theta], i], {i, 6}]]]]

This would have been my preferred solution for aesthetic reasons, but I somehow doubt, that this would have made my initial reply clearer.

Consider also this:

enter image description here

Cheers, M.

PS: Interestingly,

Expand[Product[1 + Subscript[\[Theta], i], {i, 16}]]

is not faster; I would have expected this to be faster than the matrix thing.

POSTED BY: Marco Thiel
Posted 8 years ago

Dear Marco

Thank you ever so much. I hope I can get the hang of it after yoru many examples.

One final question if I may. Suppose I want to select (and find the position of) the subsets where the product Subscript[\[Theta], 3] Subscript[\[Theta], 4] is present in the outcome of my first question. I have tried Select and Position, but again I didn't quite figure out the rule to be used.

Cheers

Ed

POSTED BY: Eduardo Mendes

Hi Ed,

this

Select[Subsets[Table[Subscript[\[Theta], i], {i, 5}]], FreeQ[#, Subscript[\[Theta], 3]] \[And] FreeQ[#, Subscript[\[Theta], 4]] &, Infinity]

or

Select[Subsets[Table[Subscript[\[Theta], i], {i, 5}]], ContainsOnly[#, Complement[Table[Subscript[\[Theta], i], {i, 5}], {Subscript[\[Theta], 3], Subscript[\[Theta], 4]}]] &]

might work. Of course if that is what you want you can directly use:

Subsets[Complement[Table[Subscript[\[Theta], i], {i, 5}], {Subscript[\[Theta], 3], Subscript[\[Theta], 4]}]]

Cheers,

Marco

POSTED BY: Marco Thiel
Posted 8 years ago

Hi Marco

Many thanks again.

Cheers

Ed

POSTED BY: Eduardo Mendes
Posted 8 years ago

I forgot to mention that what I needed it is the opposite of the outcome of the commands above but thanks to your answer I have one solution at least

Select[Subsets[Table[Subscript[\[Theta], i], {i, 5}]], 
 MemberQ[#, Subscript[\[Theta], 3]] \[And] 
   MemberQ[#, Subscript[\[Theta], 4]] &, Infinity]

Many thanks

Ed

POSTED BY: Eduardo Mendes

Dear Ed,

is that not simply equivalent to making subsets of other Thetas

Join[{Subscript[\[Theta], 3], Subscript[\[Theta], 4]}, #] & /@ Subsets[Table[Subscript[\[Theta], i], {i, {1, 2, 5}}]]

Cheers,

M.

POSTED BY: Marco Thiel
Posted 8 years ago

Dear Marco

Many thanks. Please correct me if I am wrong but in the solution you have just posted I have to remember the thetas not involved in the first part, {1,2,5}, and that could be difficult when I have, say, 16 instead of 5, right?

I have another question but I think it should be posted in another thread.

Again thank you very much for your help and patience.

regards

Ed

POSTED BY: Updating Name

Dear Ed,

yes, but you can use the Complement function:

Join[{Subscript[\[Theta], 3], Subscript[\[Theta], 4]}, #] & /@ Subsets[Complement[Table[Subscript[\[Theta], i], {i, 5}], {Subscript[\[Theta], 3], Subscript[\[Theta], 4]}]]

Cheers,

M.

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

Group Abstract Group Abstract