What can be the output of this flowchart? Will z turn into zero ever?
After 7 cycles, Z will be zero, and X will be -19200.
Related
I understand the concept of using two's complement to represent a negative value which can then be used in addition to find the resulting value.
ex.
x = 15 and y = 10, in binary x = 1111 and y = 1010
for calculating x - y, we represent y as a negative using twos complement which results y = 0101 + 0001 = 0110 then carrying out the addition x + (-y) = 1111 + 0110 = 0101 which translates to the expected result 5.
All is well and fine until I try to use the same method to calculate y - x which ends up resulting in 1011 translating to 11, not the expected result -5. Is there a way to properly calculate the addition of two binary numbers resulting in a negative value?
Think of it this way; y - x is equivalent to -x + y. For that reason, we can first negate x, which is 15 (1111 in binary), which becomes 0001 after negation. We can then add that value to y (which is 10, 1010 in binary), which results in 1011. Because this value is negative, we then flip the bits and add 1 to make it positive, and we get 0101, which is 5 in base-10. This means that our result was -5, which is correct.
I think you were correct everywhere except where you stated that it translates to 11.
Consider a function F[x;y] that generates a table. I also have two lists; xList:[x1;x2;x3] and yList:[y1;y2;y3]. What is the best way to do a simple comma join of F[x1;y1],F[x1;y2],F[x1;y3],F[x2;y1],..., thereby producing one large table?
You have asked for the cross product of your argument lists, so the correct answer is
raze F ./: xList cross yList
Depending on what you are doing, you might want to look into having your function operate on the entire list of x and the entire list of y and return a table, rather than on each pair and then return a list of tables which has to get razed. The performance impact can be considerable, for example see below
q)g:{x?y} //your core operation
q)//this takes each pair of x,y, performs an operation and returns a table for each
q)//which must then be flattened with raze
q)fm:{flip `x`y`res!(x;y; enlist g[x;y])}
q)//this takes all x, y at once and returns one table
q)f:{flip `x`y`res!(x;y;g'[x;y])}
q)//let's set a seed to compare answers
q)\S 1
q)\ts do[10000;rm:raze fm'[x;y]]
76 2400j
q)\S 1
q)\ts do[10000;r:f[x;y]]
22 2176j
q)rm~r
1b
Setup our example
q)f:{([] total:enlist x+y; x:enlist x; y:enlist y)}
q)x:1 2 3
q)y:4 5 6
Demonstrate F[x1;y1]
q)f[1;4]
total x y
---------
5 1 4
q)f[2;5]
total x y
---------
7 2 5
Use the multi-valent apply operator together with each' to apply to each pair of arguments.
q)raze .'[f;flip (x;y)]
total x y
---------
5 1 4
7 2 5
9 3 6
Another way to achieve it using each-both :
x: 1 2 3
y: 4 5 6
f:{x+y}
f2:{ a:flip x cross y ; f'[a 0;a 1] }
f2[x;y]
5j, 6j, 7j, 6j, 7j, 8j, 7j, 8j, 9j
Using cusparse, I first tried a sparse-dense multiplication with the following sizes:
C1 [8692 x 8692] = A1 [8692 x 7000 sparse] x B1 [7000 x 8692]
It takes only 0.3 seconds. Then I did another one with the following sizes:
C2 [8820 x 8820] = A2 [8820 x 32000 sparse] x B2 [32000 x 8820]
The time it takes varies depending on what's in the sparse matrix, but it ranges from 30 seconds to 90 seconds. Is there anything I can do to accelerate it? I can chop the matrices in different ways if that can reduce the running time, but I'm not sure what the performance issues are here.
The sparse matrices A1 and A2 are stored in CSR formats and they do have a bad sparsity pattern, but they are equally bad. The two figures below show where the non-zero elements are in A1 and A2 respectively. The non-zero elements per column in both cases are controlled to be fixed at 127.
From the sparsity pattern of the matrix, you should split the matrix A1 in 2 parts with a matrix A11 containing about the first 8000 rows and A12 the remaining rows and use the csrmv twice. This way, cusparse will choose a better heuristic for the numbers of threads per row.
You should also consider using the new version in CUSPARSE 6.0 csrmv2 with the transpose case . You would need to transpose B first ( using cublasgeam ) and do :
C = A1 * (B')'
The transpose case is much better because the access to B are all coalesced.
Another option would be to densify A1 ( using cusparsecsr2dense ) and use cublas
I'm stuck and don't know how to proceed further. How do I order my results accordingly?
10 x 2 ml
10 x 10 ml
4 x 20 ml
10 x 2 ml should come first because 2 ml is smaller than 10 ml.
And then order by the number that comes before the multiplication sign.
This is how I solved my own question:
ORDER BY SUBSTR(size, INSTR(size, 'x') + 2) + 0, size + 0
You could try this, but it's really ugly, especially if the tables are big and you need performance:
ORDER BY TRIM(REPLACE(REPLACE(field_name,CONCAT(SUBSTRING_INDEX(field_name,'x',1),'x'),''),'ml',''))
it replaces SUBSTRING_INDEX('ABCx123ml','x',1); //ABC and ml with blanks, triming it, leaving only the value needed for order...
I've built a four bit adder/subtractor utilizing 4, 1 bit full adders and the input and output are twos complement numbers.
If X=0111 and Y=1000 their sum is obviously 1111.
In decimal this is equivalent to 7 + 8 thus 15 which is what the sum results in.
I am confused however if this result needs to be translated back into "regular" binary by flipping the bits and adding one? So that the answer would be 0001 representing 1 in decimal instead. And that Y in decimal before translation was actually 0110 representing 6 thereby yielding the following in binary 7-6 = 1. If anyone could point me in the right direction I would appreciate it!
It appears you've got the conversion for Y wrong. Y = 10002 = -810.
To represent -6 you take 0110, flip the bits to get 1001 and add one, so Y = 1010. (And 0111 + 1010 = 0001 as you expect.)
To go back, flip the bits of 1010 = 0101 and add one giving 0110 = 6.
Edit to answer your follow-up question:
Let:
X = 0111
Y = 1100
X + Y = 0011 (ignoring overflow)
So whatever we're adding, it equals 3. We know that X = 7.
Y = 1100 => 0011 + 1 = (negative)0100 = -4
7 + (-4) = 3
No translation is necessary, just represent the positive and negative numbers correctly. I think your confusion is coming from the fact that we're "negating" the negative numbers to find the absolute value of that number and sticking a negative sign in front of it, as in the conversion of Y above. That's because negative numbers in 2's complement aren't as readable as positive numbers, but 0100 is still +4 and 1100 is still -4.