evalf(Int) works quite strangely for me - maple

evalf(Int(-(1/9)*exp(-n*(cos(phi)+sin(phi)))*cos(n*sin(phi))*cos(n*cos(phi))*n*(60*n+7*exp(2*n)-7*exp(-2*n))/(Pi^2*(exp(2*n)-exp(-2*n)+4*n)), [phi = 0 .. (1/2)*Pi, n = 0 .. inf]))
is leaving all the integral signs in result. What am I doing wrong?

You need to spell out the word infinity.

Related

How Jump instruction is executed based on value of Out- The Alu Output

Figure from The Elements of Computer System (Nand2Tetris)
Have a look at the scenario where
j1 = 1 (out < 0 )
j2 = 0 (out = 0 )
j3 = 1 (out > 0 )
How this scenario is possible as out < 0 is true as well as out > 0 but out = 0 is false. How out can have both positive and negative values at the same time?
In other words when JNE instruction is going to execute although it theoretically seems possible to me but practically its not?
If out < 0, the jump is executed if j1 = 1.
If out = 0, the jump is executed if j2 = 1.
If out > 0, the jump is executed if j3 = 1.
Hopefully now you can understand the table better. In particular, JNE is executed if out is non-zero, and is skipped if out is zero.
The mnemonic makes sense if those are match-any conditions, not match-all. i.e. jump if the difference is greater or less than zero, but not if it is zero.
Specifically, sub x, y / jne target works the usual way: it jumps if x and y were equal before the subtraction. (So the subtraction result is zero). This is what the if(out!=0) jump in the Effect column is talking about.
IDK the syntax for Nand2Tetris, but hopefully the idea is clear.
BTW, on x86 JNZ is a synonym for JNE, so you can use whichever one is semantically relevant. JNE only really makes sense after something that works as a compare, even though most operations set ZF based on whether the result is zero or not.

Why code displays error?

Can you help me and explain why this code gives a error? I would like to use XOR, but I can not. I'm trying to do this using the following formula:"A XOR B= (A AND ~B)OR(~A AND B). Can you hint what did I do wrong?
public = 'public';
password = 'passwd';
if length(public)== length(password)
public = uint8(public);
password = uint8(password);
negpublic = ~(dec2bin(public));
negpassword = ~(dec2bin(password));
score = bitor(bitand(public,negpassword),bitand(negpublic,password));
public = dec2bin(public);
password = char(password)
else
fprintf('length not ok!\n' );
end
Normally I do not provide answers for homework questions, but it seems as you are almost there. The logic is done and that was the important part I guess.
Regarding the code, there is a few of bugs here. The function dec2bin will deceit you. As far as I know, matlab does not support binary format. The dec2bin actually convert the number to an array of char :(. However, having the text in binary format is not a requirement for doing bitwise operations. I cannot really see the use for a binary format in matlab since the smallest data unit for most computer achitectures normally is one byte.
You can use the function bitcmp (bitwise complement, which is another word for bitwise NOT) to do the negation. Secondly, bitwise operations can also work on vectors. Third, it is possible to define the negation as a variable, but bit operations are among the cheapest for most processors and operating systems so this is frankly not necessary for only two uses. So the content of everything is that you can simplify things a lot.
ab = 'ab'; bb = 'bb';
ab=uint8(ab); bb=uint8(bb);
bitor(bitand(ab,bitcmp(bb)), bitand(bb,bitcmp(ab)))
Why does the code yield error?
Let's first list the error:
Error using bitand Inputs must be signed or unsigned integers of the
same class or scalar doubles.
Error in foo (line 8)
score = bitor(bitand(public,negpassword),bitand(negpublic,password));
Ok, so the following line yields the error:
score = bitor(bitand(public,negpassword),bitand(negpublic,password));
We can break this down and see that both the following expressions yields errors on their own
bitand(public,negpassword)
bitand(negpublic,password)
Why? If we look at the the first of these two a bit closer, we see that public and negpassword and non-compliant for use with bitand:
public =
112 117 98 108 105 99
negpassword =
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
These two must be, at the very least, of the same dimension. See the reference for Bit-wise OR for details.
I'm not really sure what you're trying to achieve here, but not that Matlab has its own bitxor function:
public = 'public';
password = 'passwd';
if length(public)== length(password)
public = uint8(public);
password = uint8(password);
score = bitxor(public,password);
public = dec2bin(public);
password = char(password);
else
fprintf('length not ok!\n' );
end

Bound constraints ignored Matlab

I have the following code working out the efficient frontier for a portfolio of assets:
lb=Bounds(:,1);
ub=Bounds(:,2);
P = Portfolio('AssetList', AssetList,'LowerBound', lb, 'UpperBound', ub, 'Budget', 1);
P = P.estimateAssetMoments(AssetReturns)
[Passetmean, Passetcovar] = P.getAssetMoments
pwgt = P.estimateFrontier(20);
[prsk, pret] = P.estimatePortMoments(pwgt);
It works fine apart from the fact that it ignores the constraints to some extent (results below). How do I set the constraints to be hard constraints- i.e. prevent it from ignoring an upper bound of zero? For example, when I set an upper and lower bound to zero (i.e. I do not want a particular asset to be included in a portfolio) I still get values in the calculated portfolio weights for that asset, albeit very small ones, coming out as part of the optimised portfolio.
Lower bounds (lb), upper bounds (ub), and one of the portfolio weights (pwgt) are set out below:
lb ub pwgt(:,1)
0 0 1.06685493772574e-16
0 0 4.17200995972422e-16
0 0 0
0 0 2.76688394418301e-16
0 0 3.39138439553466e-16
0.192222222222222 0.252222222222222 0.192222222222222
0.0811111111111111 0.141111111111111 0.105624956477606
0.0912121212121212 0.151212121212121 0.0912121212121212
0.0912121212121212 0.151212121212121 0.0912121212121212
0.0306060606060606 0.0906060606060606 0.0306060606060606
0.0306060606060606 0.0906060606060606 0.0306060606060606
0.121515151515152 0.181515151515152 0.181515151515152
0.0508080808080808 0.110808080808081 0.110808080808081
0.00367003367003366 0.0636700336700337 0.0388531580005063
0.00367003367003366 0.0636700336700337 0.0636700336700338
0.00367003367003366 0.0636700336700337 0.0636700336700337
0 0 0
0 0 0
0 0 1.29236898960272e-16
I could use something like: pwgt=floor(pwgt*1000)/1000;, but is there not a more elegant solution than this?
The point is that your bound has not been ignored.
You are calculating with floating point numbers, and hence 0 and 4.17200995972422e-16 are both close enough to 0 to let your program allow them.
My recommendation would indeed be to round your results (or simply display less decimals with format short), however I would do the rounding like this:
pwgt=round(pwgt*100000)/100000;
Note that the other results may also be 'above' the upper bound, however this will not become visible due to the insignificance.
I had issues like this with a laminate/engineering properties code, which was propogating errors all over everything. I fixed it by taking all of the values I had, and systematically converting them from double to sym, and suddenly my 1e-16 values became real zeros, that I could also eval(val) and still see as zeros! This may help, but you may have to go inside of the .m files you're running, and have the numbers convert to sym with val = sym(val).
I can't remember for certain, but I think Matlab functions MIGHT change sym to double once they receive the data for their own internal processing.

Is there a Kronecker symbol in Maple?

The kronecker-symbol for two integers n,m is defined as 1 if n=m and 0 otherwise.
Is there an inbuilt kronecker-symbol in maple or an easy way to implement it?
Sorry, if I should have overlooked something trivial, the maple-homepage is down at the moment and google did not return anything helpful.
Edit: I Just found something useful here:
restart:
delta := table(symmetric,identity);
simplify(delta[1,2]);
simplify(delta[1,1]);
delta := TABLE(symmetric, identity, [])
0
1
However it does not seem to simplify even trivial expressions with symbols:
simplify(delta[n,n]);
simplify(delta[n,n+1]);
delta[n, n]
delta[n, n + 1]
where one would expect to obtain 1 and 0, respectively.
Edit 2: I also tried it this way:
restart:
delta := proc(n,m):
if n=m then 1 else 0 fi;
end;
delta(1,2);
delta(1,1);
delta(n,n);
0
1
1
However, this also works poorly for symbols, as
delta(n,m);
returns 0.
Try this
delta:= (m,n)->
`if`(evalb(m < n)::truefalse, `if`(m=n,1,0), 'procname'(m,n))
;
There is a built-in function called KroneckerDelta in the Physics package. Try this
with(Physics):
KroneckerDelta[i,j];
where i and j take integer values. This gives 1 when i=j and 0 when i =/= j.
I prefer something like this: delta:=(mu,nu)->eval(evalb(mu=nu),[true=1,false=0])

set the values to binary

for s=1:length(C_tem)
for w=1:length(C_tem{s})
if (abs(C_tem{s}{w}) >= 0)
C_tem{s}{w} = 1;
else
C_tem{s}{w} = 0;
end
end
end
I am trying to set the values larger than 0 to 1, and if less or equal to 0, but for some reason this doesn't work.
I'm new in matlab, and I really need the help if possible. Thank you in advance..
i havn't worked on matlab much but this part of code feels suspicious -
if (abs(C_tem{s}{w}) >= 0)
C_tem{s}{w} = 1;
else
C_tem{s}{w} = 0;
end
Why are you doing abs here? I think it will remove sign from number. Code should be something like this-
if (C_tem{s}{w} > 0) //I have removed abs and >= is replaced with >
C_tem{s}{w} = 1;
else
C_tem{s}{w} = 0;
end
abs(x)>=0 is true for all values of x. The simple answer is to remove the abs. The more complete answer follows up on Dan's comment. The cell array is unnecessary at the inner level. If you instead had a cell array of regular arrays, then you could do this for the entire block of code.
for s=1:length(C_tem)
C_tem{s} = (C_tem{s} >= 0);
end
Two things to notice: comparison operators are vectorized, meaning they return a matrix of the same size as the input, thus comparing all values at once. And, the output of the operator is 1 where true and 0 where false.
Also look at the builtin function sign to see if that's closer to what you want to do.