Group Abstract Group Abstract

Message Boards Message Boards

Set-builder notation in Wolfram Language?

Posted 9 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 9 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 9 years ago
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 9 years ago
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