I'm having trouble converting an index number into its respective column/row. The table goes like this
The graph scales in each dimension. Each square is surrounded by one blank space. I need to turn the number of the square into the x/y coordinates
I've figured out the column, but the row is still evading me.
This is what i have now:
#define IDtoX(n, w) ((2*(n%w))+1)
#define IDtoY(n, h) ((2*(n/h))+1)
IDtoX works as intended. IDtoY does not.
outputs should be as following.:
grid of width 7 and height 5:
n y
0 3
1 3
2 3
3 1
4 1
5 1
grid of width 9 and height 7:
0 5
1 5
2 5
3 5
4 3
5 3
6 3
7 3
8 1
9 1
10 1
11 1
The main reason why you are failing with your function IDtoY(n, h) is that the result also depends on the value of w. Therefore you must change your signature to something like IDtoY(n, w, h). To see this, try drawing more arrays with the same hs but varying ws and you will see that the ids will also change for each n. You were fooled by your successful function for IDtoX which does indeed not depend on h but only on n and w. Now, if your ids began at zero at the top, they would not depend on w, but as you drew the array, they do.
I found multiple formulae that work, but none of them are pretty. Here are two--if you do not like them, you could find some equivalent formulae.
#define IDtoY(n, w, h) (h - 2 - 2 * n // (w - 1) * 2)
or perhaps
#define IDtoY(n, w, h) (h - 2 - n // (w // 2) * 2)
where the // operator is integer division. You do not state which computing environment you are using--the simple / operator may work for you.
Related
I already found this one
Brute force is possible of course, but are there any other ways? Is there a way to find all multisets? Is there a way to find out how many combinations exist under a certain limit?
Perhaps this question is too mathy for SO, if that is the case I'll move it.
I created my own version in javascript by generating all possible combinations of a list of numbers, then checking for integer RMS. These are sets though, not multisets.
Edit: I used N for sum value and K for the number of squares.
Number of multi-sets grows fast, so N should have reasonable value. So this problem is equivalent to changing sum N by K coins with nominals 1,4,9,25... (and the number of variants might be calculated using dynamic programming).
The simplest implementation is recursive - we just generate all possible addends. In my Delphi implementation I collect summands in string (instead of list) for simplicity.
Note that such implementation might generate the same sequences again and again - note {5,7} end-sequence in my example. To improve performance (important for rather large values of N and K), it is worth to store generated sequences in table or map (with {N;K;Min} key). In that case generation from large summands to smaller ones would be better, because small summands give a lot of repeating patterns.
procedure FSP(N, K, Minn: Integer; Reslt: string);
var
i: Integer;
begin
if (K = 0) then begin
if (N = 0) then
Memo1.Lines.Add(Reslt); //yield result
Exit;
end;
i := Minn;
while (i * i <= N) do begin
FSP(N - i * i, K - 1, i, Reslt + Format('%d ', [i]));
i := i + 1;
end;
end;
procedure FindSquarePartitions(N, K: integer);
begin
FSP(N, K, 1, '');
end;
FindSquarePartitions(101, 5);
1 1 1 7 7
1 1 3 3 9
1 1 5 5 7
1 2 4 4 8
1 5 5 5 5
2 2 2 5 8
2 3 4 6 6
2 4 4 4 7
3 3 3 5 7
So i've been looking into modulo recently. I'm trying to improve my math skills, which are not the best if i'm honest. But something i am trying to improve. I understand how this works i think. I am also quite competent with long division. However something is bugging me and i can't seem to find an answer for it online.
I know that 7 % 5 = 2 (5 goes into 7 once, with a remainder of 2).
What i don't understand is this;
1 % 3 = 1
How can this be, 3 goes into 1, 0 times, with a remainder of 3? Surely the answer to 1 % 3 = 3?
Can anyone explain this in its most simplest terms please?
Am i correct in thinking that if the dividend (1) is less than the devisor (3) which we know will equal 0 remainder x, it just uses the dividend as the result?
Thanks for your help.
The remainder in 1%3 refers to what remains of 1 (not 3) after you divide by 3. As you have already said, 3 goes into 1 zero times. So -- when you remove 0 multiples of 3 from 1, all of 1 remains. Thus 1 % 3 = 1.
The result of a modulo operation n % m is just that number r for which q * m + r = n (q may be anything). The only requirement we have is that 0 <= r < m.
So for instance:
7 % 5 --> 1 * 5 + 2 == 7 --> r = 2
1 % 3 --> 0 * 3 + 1 == 1 --> r = 1
I wasn't starting to understand linear recursion and then I thought I practice up on sorting algorithms and then quick sort was where I had trouble with recursion. So I decided to work with a simpler eg, a binary sum that I found online. I understand that recursion, like all function calls, are executed one # a time and not at the same time (which is what multi-threading does but is not of my concern when tracing). So I need to execute all of recursive call A BEFORE recursive call B, but I get lost in the mix. Does anyone mind tracing it completely. The e.g. I have used of size, n = 9 where elems are all 1's to keep it simple.
/**
* Sums an integer array using binary recursion.
* #param arr, an integer array
* #param i starting index
* #param n size of the array
* floor(x) is largest integer <= x
* ceil(x) is smallest integer >= x
*/
public int binarySum(int arr[], int i, int n) {
if (n == 1)
return arr[i];
return binarySum(arr, i, ceil(n/2)) + binarySum(arr,i + ceil(n/2), floor(n/2));
}
What I personally do is start with an array of size 2. There are two elements.
return binarySum(arr, i, ceil(n/2)) + binarySum(arr,i + ceil(n/2), floor(n/2)) will do nothing but split the array into 2 and add the two elements. - case 1
now, this trivial starting point will be the lowest level of the recursion for the higher cases.
now increase n = 4. the array is split into 2 : indices from 0-2 and 2-4.
now the 2 elements inside indices 0 to 2 are added in case 1 and so are the 2 elements added in indices 2-4.
Now these two results are added in this case.
Now we are able to make more sense of the recursion technique, some times understanding bottom up is easier as in this case!
Now to your question consider an array of 9 elements : 1 2 3 4 5 6 7 8 9
n = 9 => ceil(9/2) = 5, floor(9/2) = 4
Now first call (top call) of binarySum(array, 0, 9)
now n = size is not 1
hence the recursive call....
return binarySum(array, 0, 5) + binarySum(array, 5, 4)
now the first binarySum(array, 0 ,5) operates on the first 5 elements of the array and the second binarySum(array,5,4) operates on the last 4 elements of the array
hence the array division can be seen like this: 1 2 3 4 5 | 6 7 8 9
The first function finds the sum of the elements: 1 2 3 4 5
and the second function finds the sum of the elements 6 7 8 9
and these two are added together and returned as the answer to the top call!
now how does this 1+2+3+4+5 and 6+7+8+9 work? we recurse again....
so the tracing will look like
1 2 3 4 5 | 6 7 8 9
1 2 3 | 4 5 6 7 | 8 9
1 2 | 3 4 | 5 6 | 7 8 | 9
[1 | 2]___[3]___[4 5]___[6 7]___[8 9]
Till this we are fine..we are just calling the functions recursively.
But now, we hit the base case!
if (n == 1)
return arr[i];
[1 + 2]____[3]____[4 + 5]____[6 + 7]____[8 + 9]
[3 + 3] ____ [9] ____[13 + 17]
[6 + 9] [30]
[15 + 30]
[45]
which is the sum.
So for understanding see what is done to the major instance of the problem and you can be sure that the same thing is going to happen to the minor instance of the problem.
This example explains binary sum with trace in java
the trace is based on index of array , where 0 - is yours starting index and 8 is length of the array
int sum(int* arr, int p, int k) {
if (p == k)
return arr[k];
int s = (p + k) / 2;
return sum(arr, p, s) + sum(arr, s + 1, k);
}
I'm trying to understand the binary operators in C# or in general, in particular ^ - exclusive or.
For example:
Given an array of positive integers. All numbers occur even number of times except one number which occurs odd number of times. Find the number in O(n) time and constant space.
This can be done with ^ as follows: Do bitwise XOR of all the elements. Finally we get the number which has odd occurrences.
How does it work?
When I do:
int res = 2 ^ 3;
res = 1;
int res = 2 ^ 5;
res = 7;
int res = 2 ^ 10;
res = 8;
What's actually happening? What are the other bit magics? Any reference I can look up and learn more about them?
I know this is a rather old post but I wanted simplify the answer since I stumbled upon it while looking for something else.
XOR (eXclusive OR/either or), can be translated simply as toggle on/off.
Which will either exclude (if exists) or include (if nonexistent) the specified bits.
Using 4 bits (1111) we get 16 possible results from 0-15:
decimal | binary | bits (expanded)
0 | 0000 | 0
1 | 0001 | 1
2 | 0010 | 2
3 | 0011 | (1+2)
4 | 0100 | 4
5 | 0101 | (1+4)
6 | 0110 | (2+4)
7 | 0111 | (1+2+4)
8 | 1000 | 8
9 | 1001 | (1+8)
10 | 1010 | (2+8)
11 | 1011 | (1+2+8)
12 | 1100 | (4+8)
13 | 1101 | (1+4+8)
14 | 1110 | (2+4+8)
15 | 1111 | (1+2+4+8)
The decimal value to the left of the binary value, is the numeric value used in XOR and other bitwise operations, that represents the total value of associated bits. See Computer Number Format and Binary Number - Decimal for more details.
For example: 0011 are bits 1 and 2 as on, leaving bits 4 and 8 as off. Which is represented as the decimal value of 3 to signify the bits that are on, and displayed in an expanded form as 1+2.
As for what's going on with the logic behind XOR here are some examples
From the original post
2^3 = 1
2 is a member of 1+2 (3) remove 2 = 1
2^5 = 7
2 is not a member of 1+4 (5) add 2 = 1+2+4 (7)
2^10 = 8
2 is a member of 2+8 (10) remove 2 = 8
Further examples
1^3 = 2
1 is a member of 1+2 (3) remove 1 = 2
4^5 = 1
4 is a member of 1+4 (5) remove 4 = 1
4^4 = 0
4 is a member of itself remove 4 = 0
1^2^3 = 0Logic: ((1^2)^(1+2))
(1^2) 1 is not a member of 2 add 2 = 1+2 (3)
(3^3) 1 and 2 are members of 1+2 (3) remove 1+2 (3) = 0
1^1^0^1 = 1 Logic: (((1^1)^0)^1)
(1^1) 1 is a member of 1 remove 1 = 0
(0^0) 0 is a member of 0 remove 0 = 0
(0^1) 0 is not a member of 1 add 1 = 1
1^8^4 = 13 Logic: ((1^8)^4)
(1^8) 1 is not a member of 8 add 1 = 1+8 (9)
(9^4) 1 and 8 are not members of 4 add 1+8 = 1+4+8 (13)
4^13^10 = 3 Logic: ((4^(1+4+8))^(2+8))
(4^13) 4 is a member of 1+4+8 (13) remove 4 = 1+8 (9)
(9^10) 8 is a member of 2+8 (10) remove 8 = 2
1 is not a member of 2+8 (10) add 1 = 1+2 (3)
4^10^13 = 3 Logic: ((4^(2+8))^(1+4+8))
(4^10) 4 is not a member of 2+8 (10) add 4 = 2+4+8 (14)
(14^13) 4 and 8 are members of 1+4+8 (13) remove 4+8 = 1
2 is not a member of 1+4+8 (13) add 2 = 1+2 (3)
To see how it works, first you need to write both operands in binary, because bitwise operations work on individual bits.
Then you can apply the truth table for your particular operator. It acts on each pair of bits having the same position in the two operands (the same place value). So the leftmost bit (MSB) of A is combined with the MSB of B to produce the MSB of the result.
Example: 2^10:
0010 2
XOR 1010 8 + 2
----
1 xor(0, 1)
0 xor(0, 0)
0 xor(1, 1)
0 xor(0, 0)
----
= 1000 8
And the result is 8.
The other way to show this is to use the algebra of XOR; you do not need to know anything about individual bits.
For any numbers x, y, z:
XOR is commutative: x ^ y == y ^ x
XOR is associative: x ^ (y ^ z) == (x ^ y) ^ z
The identity is 0: x ^ 0 == x
Every element is its own inverse: x ^ x == 0
Given this, it is easy to prove the result stated. Consider a sequence:
a ^ b ^ c ^ d ...
Since XOR is commutative and associative, the order does not matter. So sort the elements.
Now any adjacent identical elements x ^ x can be replaced with 0 (self-inverse property). And any 0 can be removed (because it is the identity).
Repeat as long as possible. Any number that appears an even number of times has an integral number of pairs, so they all become 0 and disappear.
Eventually you are left with just one element, which is the one appearing an odd number of times. Every time it appears twice, those two disappear. Eventually you are left with one occurrence.
[update]
Note that this proof only requires certain assumptions about the operation. Specifically, suppose a set S with an operator . has the following properties:
Assocativity: x . (y . z) = (x . y) . z for any x, y, and z in S.
Identity: There exists a single element e such that e . x = x . e = x for all x in S.
Closure: For any x and y in S, x . y is also in S.
Self-inverse: For any x in S, x . x = e
As it turns out, we need not assume commutativity; we can prove it:
(x . y) . (x . y) = e (by self-inverse)
x . (y . x) . y = e (by associativity)
x . x . (y . x) . y . y = x . e . y (multiply both sides by x on the left and y on the right)
y . x = x . y (because x . x = y . y = e and the e's go away)
Now, I said that "you do not need to know anything about individual bits". I was thinking that any group satisfying these properties would be enough, and that such a group need not necessarily be isomorphic to the integers under XOR.
But #Steve Jessup proved me wrong in the comments. If you define scalar multiplication by {0,1} as:
0 * x = 0
1 * x = x
...then this structure satisfies all of the axioms of a vector space over the integers mod 2.
Thus any such structure is isomorphic to a set of vectors of bits under component-wise XOR.
This is based on the simple fact that XOR of a number with itself results Zero.
and XOR of a number with 0 results the number itself.
So, if we have an array = {5,8,12,5,12}.
5 is occurring 2 times.
8 is occurring 1 times.
12 is occurring 2 times.
We have to find the number occurring odd number of times. Clearly, 8 is the number.
We start with res=0 and XOR with all the elements of the array.
int res=0;
for(int i:array)
res = res ^ i;
1st Iteration: res = 0^5 = 5
2nd Iteration: res = 5^8
3rd Iteration: res = 5^8^12
4th Iteration: res = 5^8^12^5 = 0^8^12 = 8^12
5th Iteration: res = 8^12^12 = 8^0 = 8
The bitwise operators treat the bits inside an integer value as a tiny array of bits. Each of those bits is like a tiny bool value. When you use the bitwise exclusive or operator, one interpretation of what the operator does is:
for each bit in the first value, toggle the bit if the corresponding bit in the second value is set
The net effect is that a single bit starts out false and if the total number of "toggles" is even, it will still be false at the end. If the total number of "toggles" is odd, it will be true at the end.
Just think "tiny array of boolean values" and it will start to make sense.
The definition of the XOR (exclusive OR) operator, over bits, is that:
0 XOR 0 = 0
0 XOR 1 = 1
1 XOR 0 = 1
1 XOR 1 = 0
One of the ways to imagine it, is to say that the "1" on the right side changes the bit from the left side, and 0 on the right side doesn't change the bit on the left side. However, XOR is commutative, so the same is true if the sides are reversed.
As any number can be represented in binary form, any two numbers can be XOR-ed together.
To prove it being commutative, you can simply look at its definition, and see that for every combination of bits on either side, the result is the same if the sides are changed. To prove it being associative, you can simply run through all possible combinations of having 3 bits being XOR-ed to each other, and the result will stay the same no matter what the order is.
Now, as we proved the above, let's see what happens if we XOR the same number at itself. Since the operation works on individual bits, we can test it on just two numbers: 0 and 1.
0 XOR 0 = 0
1 XOR 1 = 0
So, if you XOR a number onto itself, you always get 0 (believe it or not, but that property of XOR has been used by compilers, when a 0 needs to be loaded into a CPU register. It's faster to perform a bit operation than to explicitly push 0 into a register. The compiler will just produce assembly code to XOR a register onto itself).
Now, if X XOR X is 0, and XOR is associative, and you need to find out what number hasn't repeated in a sequence of numbers where all other numbers have been repeated two (or any other odd number of times). If we had the repeating numbers together, they will XOR to 0. Anything that is XOR-ed with 0 will remain itself. So, out of XOR-ing such a sequence, you will end up being left with a number that doesn't repeat (or repeats an even number of times).
This has a lot of samples of various functionalities done by bit fiddling. Some of can be quite complex so beware.
What you need to do to understand the bit operations is, at least, this:
the input data, in binary form
a truth table that tells you how to "mix" the inputs to form the result
For XOR, the truth table is simple:
1^1 = 0
1^0 = 1
0^1 = 1
0^0 = 0
To obtain bit n in the result you apply the rule to bits n in the first and second inputs.
If you try to calculate 1^1^0^1 or any other combination, you will discover that the result is 1 if there is an odd number of 1's and 0 otherwise. You will also discover that any number XOR'ed with itself is 0 and that is doesn't matter in what order you do the calculations, e.g. 1^1^(0^1) = 1^(1^0)^1.
This means that when you XOR all the numbers in your list, the ones which are duplicates (or present an even number of times) will XOR to 0 and you will be left with just the one which is present an odd number of times.
As it is obvious from the name(bitwise), it operates between bits.
Let's see how it works,
for example, we have two numbers a=3 and b=4,
the binary representation of 3 is 011 and of 4 is 100, so basically xor of the same bits is 0 and for opposite bits, it is 1.
In the given example 3^4, where "^" is a xor symbol, will give us 111 whose decimal value will be 7.
for another example, if you've given an array in which every element occurs twice except one element & you've to find that element.
How can you do that? simple xor of the same numbers will always be 0 and the number which occur exactly once will be your output. because the output of any one number with 0 will be the same name number because the number will have set bits which zero don't have.
This requires a little knowledge about Matlab and I have none. I was just wondering if someone could point me in the right direction and give me some pointer :)
I have to write a matlab code for finding the Null spaces of matices
A and B, where B = A^T x A. And then nd the general solutions to AX = b1
and BX = b2, where b1= the column [1 2 3 4 5] and b2= the column [ 1 2 3 4 5 6 7 8].
My concern is that I dont really know how to go about this code.
This is what I have so far and I do not think i am in the right track. I have a specific matrix as below.
The rows are divided by semi-colon.
A = [ 1 2 3 4 5 6 7 8;
1 2^2 3^2 4^2 5^2 6^2 7^2 8^2;
1 2^3 3^3 4^3 5^3 6^3 7^3 8^3;
1 2^4 3^4 4^4 5^4 6^4 7^4 8^4;
6 8 1 1 7 9 0 7 ]
B = A’A (this is how transpose is written)
C = null(A)
D = null(B)
I feel like there should be a rref somewhere - I'm just not getting anywhere. Please point me in the right direction.
Ok so I updated it to the this now....My username changed from jona and I dont know why
A = [ 1 2 3 4 5 6 7 8;
1 2^2 3^2 4^2 5^2 6^2 7^2 8^2;
1 2^3 3^3 4^3 5^3 6^3 7^3 8^3;
1 2^4 3^4 4^4 5^4 6^4 7^4 8^4;
6 8 1 1 7 9 0 7 ]
B = A’*A (this is how transpose is written)
null(A)
null(B)
b1=[1; 2; 3; 4; 5 ];
b2=[1; 2; 3; 4; 5; 6; 7; 8 ];
end
rref(A,b1)
rref(B,b2)
end
However I still don't feel this is right :(
#Chris A. I know the null space is the solution to Ax=0. However I'm confused on how to use it to find general solution using the b1 and b2 I have. Is it possible for you to explain to me the connection? I don't undertand the book as much.
In MATLAB, arithmetic operations need to be explicit, i.e. a(b+c) should be written as a*(b+c)
Have you tried writing B as
B=A'*A;
Also, you seem to be using a different character for the transpose... You're using ’, the unicode character for single right quotation when you should be using ' or the unicode character for apostrophe.
Ok, so the bottom line is that the null space is the set of all vectors x such that A * x = 0. You got that right. And C is an orthonormal basis for the vectors in the null space. So that means if you have a particular solution (let's call it v) such that A * v = b1 then the space of solutions is the vector v plus any combination of vectors in the null space.
For the case of A, the size (second dimension) of your C will tell you the dimension of the null space. Each one of the vectors in C will be a vector in the null space.
To get v you can do v = A \ b1. You can write arbitrary combinations of vectors in C by C * c where little c is a column vector that is the size of the null space.
The general solution is thus v + C * c where c is any vector that is the dimension of the null space. To see that this solve the system, just plug it back in
A * (v + C * c) =
A * v + A * C * c =
b1 + 0 * c =
b1
Edit: It's the exact same idea for finding the solution to A'*A * x = b2, just anywhere you see A in the above discussion, replace it with A'*A and anywhere you see b1 replace it with b2. The solutions to A * x = b1 and A'*A * x = b2 are separate problems.