Why does with scala.math.BigInt(-1).mod(-2) return -3? - scala

I was looking into the difference between x % y and x mod y, which I believe I understood for positive values of y.
During experimentation, it came up that scala.math.BigInt(-1) mod -2 evaluates as -3, which makes absolutely no sense to me. I did not find a formal description of scala's BigInt behavior, but Java's says it should throw an exception if the second argument is negative.
What's going on with this?

Related

Fastest way to check if either one of two numbers is odd using bitwise operations

I came up with the following code (written in Solidity assembly):
let z := xor(and(x, 1), and(y, 1))
Where x and y are of type int256 (they are signed and can have negative values). z is the result, and it's equal to 1 when either of x and y are odd, and 0 when both are either odd or even.
The question is whether there is any algorithm that is faster than the one I wrote above?

Why does the value of angle(0) is chosen 0 in matlab? I expect it as NaN.

angle(z) represents argument of the complex number z. Then what is angle(0)? Again, if it represents the principal argument, then why does the -pi value also appear in some cases, when you are dealing with decimals?
Mathematically, it is indeed undefined as the value of the limit depends on how the two function's arguments approach their zero values. That's all fine and good for analysis.
However, from a numeric, programming perspective, it may be very annoying to have a NaN or exception throw for such a generate case. Here's a nice little blog post going over some short history of defining the behavior. As such, and since Mathworks doesn't give a reference, I'd imagine they are using some implementation of the C99 standard (though SciPy gives a better breakdown) for programmatically defining the mathematically undefined values along certain paths for robustness's sake:
>> x = [0 , 0-eps(0) , 0+eps(0) , 0-eps(0) , 0+eps(0) , 0-eps(0)];
>> y = [0 , 0 , 0+eps(0) , 0-eps(0) , 0-eps(0) , 0+eps(0)];
>> atan2(y,x)
ans =
0 3.1416 0.7854 -2.3562 -0.7854 2.3562
(As far as I know, Matlab doesn't have a concise way to "sign" zeros, so I deferred to the smallest possible floating points, which are still technically finite.)

Boolean Expression Evaluation For ExNor Gate

I know that an xnor expression can be broken up as follows:
X xnor Y = X'Y' + XY
But but I know that sume of the complement of the same combination (x+x') is 1 always therefore shouldn't xnor be always equal to 1?
I got confused in this question when I was trying to solve the following problem:
Z=X*Y where * denotes xnor. Evaluate Z*X.
My answer was 1, which according to the provided solution is incorrect the correct solution was given Y.
How? Thanks for the help.

What is this code doing? Machine Learning

I'm just learning matlab and I have a snippet of code which I don't understand the syntax of. The x is an n x 1 vector.
Code is below
p = (min(x):(max(x)/300):max(x))';
The p vector is used a few lines later to plot the function
plot(p,pp*model,'r');
It generates an arithmetic progression.
An arithmetic progression is a sequence of numbers where the next number is equal to the previous number plus a constant. In an arithmetic progression, this constant must stay the same value.
In your code,
min(x) is the initial value of the sequence
max(x) / 300 is the increment amount
max(x) is the stopping criteria. When the result of incrementation exceeds this stopping criteria, no more items are generated for the sequence.
I cannot comment on this particular choice of initial value and increment amount, without seeing the surrounding code where it was used.
However, from a naive perspective, MATLAB has a linspace command which does something similar, but not exactly the same.
Certainly looks to me like an odd thing to be doing. Basically, it's creating a vector of values p that range from the smallest to the largest values of x, which is fine, but it's using steps between successive values of max(x)/300.
If min(x)=300 and max(x)=300.5 then this would only give 1 point for p.
On the other hand, if min(x)=-1000 and max(x)=0.3 then p would have thousands of elements.
In fact, it's even worse. If max(x) is negative, then you would get an error as p would start from min(x), some negative number below max(x), and then each element would be smaller than the last.
I think p must be used to create pp or model somehow as well so that the plot works, and without knowing how I can't suggest how to fix this, but I can't think of a good reason why it would be done like this. using linspace(min(x),max(x),300) or setting the step to (max(x)-min(x))/299 would make more sense to me.
This code examines an array named x, and finds its minimum value min(x) and its maximum value max(x). It takes the maximum value and divides it by the constant 300.
It doesn't explicitly name any variable, setting it equal to max(x)/300, but for the sake of explanation, I'm naming it "incr", short for increment.
And, it creates a vector named p. p looks something like this:
p = [min(x), min(x) + incr, min(x) + 2*incr, ..., min(x) + 299*incr, max(x)];

Can a SAT solver be used to find all solutions?

I wrote an answer to what I thought was a quite interesting question, but unfortunately the question was deleted by its author before I could post. I'm reposting a summary of the question and my answer here in case it might be of use to anyone else.
Suppose I have a SAT solver that, given a Boolean formula in conjunctive normal form, returns either a solution (a variable assignment that satisfies the formula) or the information that the problem is unsatisfiable.
Can I use this solver to find all the solutions?
There is definitely a way to use the SAT solver you described to find all the solutions of a SAT problem, although it may not be the most efficient way.
Just use the solver to find a solution to your original problem, add a clause that does nothing except rule out the solution you just found, use the solver to find a solution to the new problem, and so forth. Keep going until you get a problem that's unsatisfiable.
For example, suppose you want to satisfy (X or Y) and (X or Z). There are five solutions:
Four with X true, Y and Z arbitrary.
One with X false, Y and Z true.
So you run your solver, and let's say it gives you the solution (X, Y, Z) = (T, F, F). You can rule out this solution---and only this solution---with the constraint
not (X and (not Y) and (not Z))
This constraint can be rewritten as the clause
(not X) or Y or Z
So now you can run your solver on the new problem
(X or Y) and (X or Z) and ((not X) or Y or Z)
and so forth.
Like I said, this is a way to do what you want, but it probably isn't the most efficient way. When your SAT solver is looking for a solution, it learns a lot about the problem, but it doesn't return all that information to you---it just gives you the solution it found. When you run the solver again, it has to re-learn all the information that was thrown away.
Sure it can. When MiniSat[1] finds a solution
s SATISFIABLE
v 1 2 -3 0
(solution 1=True, 2=True, 3=False) then you have to put into the original CNF[2] a clause that bans this solution:
-1 -2 3 0
(which means, either 1 or 2 must be False or 3 must be True). Then you solve again. You do this until the solver returns UNSAT i.e. that there are no more solutions to the problem. You will insert one clause for each iteration, and each clause will have the same format as the solution except that it's all inverted and has a 0 at the end
It's much faster to do this using the C++ interface of MiniSat, as it can then save intermediate data and the iterations will be faster.
[1] http://minisat.se/
[2] http://fairmut3x.wordpress.com/2011/07/29/cnf-conjunctive-normal-form-dimacs-format-explained/