Message Boards Message Boards

Set-builder notation in Wolfram Language?

Posted 8 years ago

Hello, big fan of Wolfram here, but I'm new. I'm using the Wolfram Development Platform. I wanted to inquire about the use of set-builder notation in the wolfram language.

I've been over the documentation but I'm having some trouble.

How would I enter the following simple statement in the Wolfram language?

E = { 2n : enter image description here }

Additionally, how would I have wolfram return a random member of this set for n<1000?

Thank you.

POSTED BY: B M
5 Replies
Posted 8 years ago

Is the output of FindInstance[] an Association object? Or a List of Rules? I guess the two objects are different and I will have to read the documentation about the differences. It seems some of the Documentation for certain functions explicitly state the output type while others do not.

It looks like I can get an Array of just the values using the Values[] function, but it looks like the output is an Array of Arrays rather than the individual components. Is there a way to remove the additional brackets? It's strange because the example in the Values[] documentation shows the output the way I would like it. My goal is to get the output to a place where I can try to manually manipulate the outputs to more formal notation. ExampleCode

Get values from a list of rules: In:= Values[{a -> x, b -> y, c -> z}]

Out= enter image description here

MyCode

In[27]:= DomainAssociation = FindInstance[Mod[x, 2] == 0 &&x^2 ==4, x, Integers, 10]

Out[27]= {{x->-2},{x->2}}

In[28]:= Values[DomainAssociation]

Out[28]= {{-2},{2}}

POSTED BY: B M
Posted 8 years ago

Alright Sean, that helps, and it deals with most of the problems I discussed. It looks like, as far as retrieving output with formal notation is concerned, I might have to look elsewhere, or at the very least it will be slightly more complicated than I thought to make it happen.

"The way you're describing, with symbolic definitions of set objects looks cool and useful, but is really complicated and far from how things are actually done."

Haha... symbolic definition of (arbitrary) set objects.. far from how things are actually (usually) done... really complicated..

You have to admit... that sounds right up Wolfram's alley though.

Thanks again.

POSTED BY: B M

Good math notation and good programming notation are not the same. You cannot do what you're asking any programming language I've ever seen (except maybe for very difficult languages meant for formalized reasoning like Coq). The reason you haven't found what you're looking for in the documentation is that you're looking for something that doesn't make sense with how most programming languages work.

We could cover a number of advanced topics like Infinite list generators, but I don't know if that would satisfy your needs or not. It is possible to define infinite lists. This would allow you to express your code as something that looks elegant, but wouldn't give you any power in abstractly reasoning about the set.

If you want to say that a value x is in that set in some function like FullSimplify, you'd want to say:

(Element[x, Integers] && x>0 && x/2==0)

How would you return a random even number that's less than a thousand?

RandomInteger[{1, Floor[(1000 - 1)/2]}]
POSTED BY: Sean Clarke
Posted 8 years ago

Basically, I want to be able to play around with sets and discrete mathematics in Wolfram, and it's pretty important that at least when it comes to displaying results that the notation is formal. I've learned TraditionalForm[] can be quite useful. I really just want to use Wolfram to do math for class.

E = { 2n : enter image description here }

In a more generalized form I just want to be able to say E = {Expression:Condition} Then I want to be able to do three things.

Either specify a specific variable that I know satisfies the condition and then evaluate the expression for the given condition. Such as n=5 because I know that 5 is a member of Z*, and then get the output of 10. The problem with this is that discovering whether some variables meet a complex condition can require a high amount of computation.

In that case, I want to be able to generate a variable that meets the stated condition. I want to be able to generate a variable, or the set of variables that meets the stated condition. I know it is more complicated in this case because Z* is an infinite set, but I would put additional constraints on variable generation such as <10000 for practical purposes.

Imagine that the statement was E= 2n such that n is a member of the members that satisfy y^2=4

Sorry, my math skills are actually not that great -- that's why I'm working on them -- so I don't even know how to represent this statement formally, but I hope I'm getting my point across. This means that I would like wolfram to be able to discover that 2 and -2 satisfy the condition, and then evaluate E for each case and output for this expression 4 and -4.

I don't expect to be handed a bunch of code, but I do understand many fundamental programming principles. Wolfram is certainly quite different, so I've tried not to approach it in the traditional way, and I have tried to "forget" the ways that most traditional programming languages work; it seems I might have been a bit too hasty in that regard. But hopefully someone can point me in the right direction.

It would also be very helpful to evaluate the expression for say the first 10 variables that meet the condition and then store the results to an array. For example, in the case of the original question, evaluate E for the first 10 integers of the set of all positive integers.The result would be evaluation of 2n for {1, 2, 3, ...,10} and subsequently, I would like to store the results in an array that would be {2, 4, 6, ..., 20}

If you could also point me to a place that described infinite list generation so you do not have to spend time on it yourself that would be great. I don't want to waste your time.

Thanks for your response and taking the time to help!

POSTED BY: B M

What you're describing doesn't exist. At least in the way you are looking for it.

It seems like you're inventing more difficult ways of doing things rather than trying to understand how they're actually done.

The way you're describing, with symbolic definitions of set objects looks cool and useful, but is really complicated and far from how things are actually done.

Imagine that the statement was E= 2n such that n is a member of the members that satisfy y^2=4 ... I would like wolfram to be able to discover that 2 and -2 satisfy the condition, and then evaluate E for each case and output for this expression 4 and -4.

Here's how you do that.

FindInstance[x > 0 && Mod[x, 2] == 0, x, Integers]

{{x -> 2}}

Here's how you find the member of the set x such that x^2 is 16:

FindInstance[x > 0 && Mod[x, 2] == 0 && x^2 == 16, x, Integers]

{{x -> 4}}

Here's ten elements of the set:

 FindInstance[x > 0 && Mod[x, 2] == 0, x, Integers, 10]

{{x -> 1560}, {x -> 1340}, {x -> 1222}, {x -> 176}, {x -> 2}, {x -> 
   1372}, {x -> 138}, {x -> 484}, {x -> 1974}, {x -> 1864}}
POSTED BY: Sean Clarke
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