How can I generate the set of all numbers that are two greater than the numbers in another SymPy set? - set

If I have a SymPy Set such as:
y=FiniteSet(-3,3)
and I want to generate another set that is the set of all numbers that are 2 more than the numbers in Set y, how can I do this? I tried a ConditionalSet:
ConditionalSet(x,x-2 in y, S.Reals)
But that didn't work.

To do this with ConditionSet you should use Contains:
In [1]: y = {-3, 3}
In [2]: z = ConditionSet(x, Contains(x-2, y), Reals)
In [3]: z
Out[3]: {x │ x ∊ ℝ ∧ (x - 2 ∈ {-3, 3})}
It's better to use ImageSet for this case though:
In [6]: z2 = ImageSet(Lambda(x, x+2), y)
In [7]: z2
Out[7]: {x + 2 │ x ∊ {-3, 3}}
In [8]: z2.doit()
Out[8]: {-1, 5}

AHA! I found the answer:
[x+2 for x in y]
Produces the desired set.

Related

How can we select a random data set from the list of solutions given by Prolog?

i am new to prolog and working my way through the FD solvers in it. I am exploring both SWI-Prolog and Gnu Prolog. I have a case where a constraint like below needs to be solved and one of the possible solutions needs to be picked at random based on a seed. Not sure how much of it is possible in Prolog.
Below is the equation i am trying to solve:
?- X #< 7 , X #> -10, 4*X+2*Y #< 8, Y #< 10, Y #>0, label([X]).
X = -9,
Y in 1..9 ;
X = -8,
Y in 1..9 ;
X = -7,
Y in 1..9 ;
X = -6,
Y in 1..9 ;
X = -5,
Y in 1..9 ;
X = -4,
Y in 1..9 ;
X = -3,
Y in 1..9 ;
X = -2,
Y in 1..7 ;
X = -1,
Y in 1..5 ;
X = 0,
Y in 1..3 ;
X = Y, Y = 1.
As you can see above, it gives me multiple solutions. For my application, I would be interested in choosing one of the data set from above based on a seed.
Is this possible in prolog? It would be great if you can help me with your suggestions.
Thanks,
Venkat
You can specify random choice strategy for labeling variables. That's how to do it in ECLiPSe CLP Prolog:
:- lib(ic).
one_random_solution(X, Y) :-
X #< 7 , X #> -10, 4*X+2*Y #< 8, Y #< 10, Y #>0,
once search([X, Y], 0, input_order, indomain_random, complete, []).
And run with:
[eclipse]: seed(10), one_random_solution(X, Y).
X = -9
Y = 5
Yes (0.00s cpu)
Unfortunately, it looks like SWI-Prolog doesn't support random choice strategy for labeling.

Evaluating Output Function in Mathematica when input does not specify variables

I am evaluating a partial derivative
fx = D[m[x, y], x]
The output gives me an output in terms of x and y. I'm trying to evaluate the function using
fx1 = fx/.{x-> 1.0, y-> 2.0}
but keeps giving me an answer like
0.471328[1.0, 2.0]
But I only want the 0.471328
I can't even begin to guess what code you have written, and not shown, to get where you are, but
In[3]:= Head[0.471328[1.0, 2.0]]
Out[3]= 0.471328
will give you what you are asking for.
Let me show one correct way to do this:
m[x_, y_] := x^2 + y^2 + x y
fx[x_, y_] := D[m[x, y], x]
fx[x, y] /. {x -> 1.0, y -> 2.0}
(*
=> 4.
*)

Using the output of Solve

I had a math problem I solved like this:
In[1]:= Solve[2x(a-x)==0, x]
Out[1]= {{x->0}, {x->a}}
In[2]:= Integrate[2x(a-x), {x,0,a}]
Out[2]= (a^3)/3
In[3]:= Solve[(a^3)/3==a, a]
Out[3]= {{a->0}, {a->-Sqrt[3]}, {a->Sqrt[3]}}
My question is if I could rewrite this to compute it in one step, rather than having to manually input the result from the previous line. I could easily replace the integral used in step three with the Integrate command from step two. But what I can't figure out is how I would use the result from step 1 as the limits of integration in the integral.
You could combine step 1 and 2 by doing something like
Integrate[2 x (a - x), {x, ##}] & ## (x /. Solve[2 x (a - x) == 0, x]);
If you agree to delegate the choice of the (positive oriented) domain to Integrate, by means of using Clip or Boole:
In[77]:= Solve[
Integrate[
Clip[2 x (a - x), {0, Infinity}], {x, -Infinity, Infinity}] == a, a]
Out[77]= {{a -> 0}, {a -> Sqrt[3]}}
or
In[81]:= Solve[
Integrate[
2 x (a - x) Boole[2 x (a - x) > 0], {x, -Infinity, Infinity}] ==
a, a]
Out[81]= {{a -> 0}, {a -> Sqrt[3]}}
The reason only non-negative roots are found, is that Integrate will integrate from the smallest root to the largest root, i.e. from {x,0,a} for positive a and {x,a,0} for negative a.

Using NProbability[] or Probability[] to work out the probability of 3 or more Heads from 4 coin tosses

Is it possible to work out the probability of 3 or more Head from 4 coin tosses using the Probability or NProbability functions.
This is not a question about the trivial answer to this problem, it is more to get an understanding of how to solve this kind of problem with Mathematica using distributions.
So using 4 random variables from Distribution P
I was hoping something like this would do the trick, but it does not work. I get 0.
P = BernoulliDistribution[0.5];
vars = List[Distributed[a,P],Distributed[b,P],Distributed[c,P],Distributed[c,P]];
NProbability[Count[ {a,b,c,d}, 1] >= 3, vars]
Any ideas would be greatly appreciated.
Not an expert using Mma for statistics here, but this seems to work:
l = TransformedDistribution[
x + y + w + z, {x \[Distributed] BernoulliDistribution[0.5],
y \[Distributed] BernoulliDistribution[0.5],
z \[Distributed] BernoulliDistribution[0.5],
w \[Distributed] BernoulliDistribution[0.5]}];
Table[NProbability[x > i, x \[Distributed] l], {i, -1, 4}]
(*
{1, 0.9375, 0.6875, 0.3125, 0.0625, 0.}
*)
In[10]:= Probability[a + b + c + d >= 3, vars]
Out[10]= 0.3125
Coin flipping is easier described with a BinomialDistribution:
In[12]:= Probability[m >= 3, m \[Distributed] BinomialDistribution[4, 0.5]]
Out[12]= 0.3125

How to ask mathematica to compute higher order derivatives evaluated at 0

I have a function, let's say for example,
D[x^2*Exp[x^2], {x, 6}] /. x -> 0
And I want to replace 6 by a general integer n,
Or cases like the following:
Limit[Limit[D[D[x /((-1 + x) (1 - y) (-1 + x + x y)), {x, 3}], {y, 5}], {x -> 0}], {y -> 0}]
And I want to replace 3 and 5 by a general integer m and n respectively.
How to solve these two kinds of problems in general in mma?
Many thanks.
Can use SeriesCoefficient, sometimes.
InputForm[n! * SeriesCoefficient[x^2*Exp[x^2], {x,0,n}]]
Out[21]//InputForm=
n!*Piecewise[{{Gamma[n/2]^(-1), Mod[n, 2] == 0 && n >= 2}}, 0]
InputForm[mncoeff = m!*n! *
SeriesCoefficient[x/((-1+x)*(1-y)*(-1+x+x*y)), {x,0,m}, {y,0,n}]]
Out[22]//InputForm=
m!*n!*Piecewise[{{-1 + Binomial[m, 1 + n]*Hypergeometric2F1[1, -1 - n, m - n,
-1], m >= 1 && n > -1}}, 0]
Good luck extracting limits for m, n integer, in this second case.
Daniel Lichtblau
Wolfram Research
No sure if this is what you want, but you may try:
D[x^2*Exp[x^2], {x, n}] /. n -> 4 /. x -> 0
Another way:
f[x0_, n_] := n! SeriesCoefficient[x^2*Exp[x^2], {x, x0, n}]
f[0,4]
24
And of course, in the same line, for your other question:
f[m_, n_] :=
Limit[Limit[
D[D[x/((-1 + x) (1 - y) (-1 + x + x y)), {x, m}], {y, n}], {x ->
0}], {y -> 0}]
These answers don't give you an explicit form for the derivatives, though.

Resources