Group Abstract Group Abstract

Message Boards Message Boards

0
|
7.5K 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 10 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
Posted 10 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
Posted 10 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 10 years ago

Hi Marco

Many thanks again.

Cheers

Ed

POSTED BY: Eduardo Mendes
Posted 10 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 BY: Marco Thiel
Posted 10 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
POSTED BY: Marco Thiel

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

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

POSTED BY: Marco Thiel
Posted 10 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

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
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard