In this problem, I am asked to find the expected value of locality. I understand how the answer to the problem is
(n - 1) * (expected value of |π(i) − π(i − 1)|).
The problem solutions states that the expected value of
|π(i) − π(i − 1)| is (n + 1) / 3.
I do not understand this part of the solution. Could someone please explain this reasoning?
Thank you.
Related
We are giving students some exercises, where their solutions are evaluated with maxima.
The answer involves the unit step function. The evaluation in maxima seems to go okay, except that there seems to be missing some algebraic rules on the unit_step functions.
For example is(unit_step(x)*unit_step(x) = unit_step(x)) evaluates to false. It is quite unlikely that the student gives the answer in such a form, but still we don't want to have the possibility that the student gives a good answer, that is evaluated as incorrect.
Below is a screenshot of an answer we try to evaluate with maxima involving the unit_step function (that we defined as u):
Maxima doesn't know much about unit_step at present (circa Maxima 5.41). This is just a shortcoming, there's no reason for it, except that nobody has gotten around to doing the work. That said, it's not too hard to make some progress.
The simplifier for multiplication merges identical terms into powers:
(%i3) unit_step(x)*unit_step(x);
2
(%o3) unit_step (x)
So let's define a simplifier rule which reduces positive powers of unit_step. (I was going to say positive integer powers, but a moment's thought shows that the same identity holds for noninteger positive powers as well.)
(%i4) matchdeclare (aa, lambda ([e], e > 0)) $
(%i5) matchdeclare (xx, all) $
(%i6) tellsimpafter (unit_step(xx)^aa, unit_step(xx));
(%o6) [^rule1, simpexpt]
Let's try it.
(%i7) unit_step(x)*unit_step(x);
(%o7) unit_step(x)
(%i8) is (unit_step(x)*unit_step(x) = unit_step(x));
(%o8) true
(%i9) unit_step(t - 5)^(1/4);
(%o9) unit_step(t - 5)
(%i10) assume (m > 0);
(%o10) [m > 0]
(%i11) unit_step(2*u + 1)^m;
(%o11) unit_step(2 u + 1)
So far, so good. Of course this is just one identity and there are others that could be useful. Since this rule is not built-in, one would have to load that definition in order to make use of it; that would be bothersome if you intend for others to use this.
For the record, the only simplification for unit_step which I found in the Maxima source code is in share/contrib/integration/abs_integrate.mac, which contains a function unit_step_mult_simp, which applies the identity unit_step(a)*unit_step(b) --> unit_step(min(a, b)).
simple multiplication is failling in my script-fu.
the folowing code
(print "hello")
(print (/ 4 3))
(print (* 3 4))
(print "world")
Gives :
"hello"
1,333333333.0
Error: ( : 1) not enough arguments
Any idea ?
Thanks
I ran into a similar problem when trying to add new functionality to someone else's script. I wanted to provide my solution in case anyone else runs into a similar issue.
In this case, there was a '(let* (...))' statement that was being used to initialize some variables. The original author of the script wrote '(let * (...))' - with a space between let and star - which means every vector in the 'let' statement becomes the expected arguments for the '*' statement.
More info: http://docs.racket-lang.org/reference/let.html
Please excuse (and correct if necessary) any incorrect nomenclature regarding Scheme. I have barely just been exposed to it.
Formula which I wanna solve looks like this in C:
#define foo(w,x,y) ((((w)*(x)*(y)+31) & ~31) / 8)
WORD w,x,y,z;
y = 24;
if( (foo(w,x,y) * z) == -1 )
printf("yeah!");
I rewrite it to z3py in the following way:
from z3 import *
w= BitVec('w',16)
x= BitVec('x',16)
z= BitVec('z',16)
y= BitVecVal(24,16)
solve( (UDiv( (w*x*y+31) & ~31, 8 )) * z == 0xffffffff)
Any suggestions ?
PS: Please notice that trying to solve formula in that form:
solve( (UDiv( (w*x*y+31), 8 )) * z == 0xffffffff)
is possible, so I can't belive that bit-wise operation causes this formula Unsatisfiable.
I don't see anything wrong in Z3 behavior. Why do you think the formula should be satisfiable?
A = (w*x*y+31) & ~31 -- imply that the 5 rightmost bits will be always zero
B = UDiv( A & ~31, 8 ) (equal to a logical shift right by 3) -- imply that the 2 rightmost bits will be always zero.
C = B * z -- this will always have the 2 rightmost bits zero
c == 0xffffffff -- that's impossible
If you change the constant to 0xfffffffc, then you'll get a solution.
The C program doesn't print anything for me. And Z3 says "no solution". So, it's consistent.
Does your C program prints "yeah!"? If so, aren't you on a big-endian machine? I've tried your example in an x86 machine.
I'm new to Z3 and I was checking the online python tutorial.
Then I thought I could check overflow behavior in BitVecs.
I wrote this code:
x = BitVec('x', 3)
y = Int('y')
solve(BV2Int(x) == y, Not(BV2Int(x + 1) == (y + 1)))
and I was expecting [y = 7, x = 7] (i.e. when values are equal but successors are not because x + 1 will be 0 and y + 1 will be 8)
But Z3 answers [y = 0, x = 0].
What am I doing wrong?
I don't think you're doing anything wrong, looks like BV2Int is buggy:
x = BitVec('x', 3)
prove(x <= 3)
prove(BV2Int(x) <= 3)
Z3py proves the first one, but gives the counter-example x=0 for the second. That doesn't sound right. (The only explanation might be some weird Python thing, but I don't see how.)
Also note that the model you get will depend on whether + treats the bit-vector as a signed number in the Python bindings, which I believe is the case. However, BV2Int might not do so, treating it as an unsigned value. This would further complicate the matters.
In any case, looks like BV2Int is not quite kosher; I'd stay away from it until there's an official answer from the Z3 folks.
For others who are concerned by this, this appears to have been solved at some point. I just re-ran this example with the latest version of z3 (a few years after initial post), and it does return 7,7.
I have been recently using Maxima (instead of Maple) and I obtained a weird symbol for which I didn't manage to get information.
Here is the code :
gx(t):=p0x*(1-t)^3+p1x*t*(1-t)^2+p2x*(1-t)*t^2+p3x*t^3;
dgx:diff(gx(t),t);
gy(t):=p0y*(1-t)^3+p1y*t*(1-t)^2+p2y*(1-t)*t^2+p3y*t^3;
dgy:diff(gy(t),t);
det(ax,ay,bx,by):=ax*by-ay*bx;
v(x,y):=-1/(R-sqrt(x**2+y**2))*[x,y];
f(t) := det(subst(N*t-j+1,t,dgx),subst(N*t-j+1,t,dgy),v(gx(N*t-j+1),gy(N*t-j+1))[1],v(gx(N*t-j+1),gy(N*t-j+1))[2])^2;
Now for the output corresponding to f(t), the "expt(...)" symbol appears.
If anyone has got any idea, I would greatly appreciate. Thank you.
From the manual:
If an exponential expression is too wide to be displayed as a^b it appears as expt (a, b)