Using the geometric series to work out big o notation for resizing an array in a stack - discrete-mathematics

1+2+4+⋯+2k=1−2^(k+1)/(1−2)
Here, 2^k=N. You get
1+2+4+⋯+N=1−2N−1
2+4+8⋯+N=2N−2
As shown above the array is doubled every 2^k. But I still don't understand all the steps for my working out doesn't match above. Can someone give me a step by step working out and explanation?

Begin with
1 + 2 + 4 + 8 + 16 + ... + 2k = 2k+1 - 1
If 2k = N, then
1 + 2 + 4 + 8 + 16 + ... + 2k = 2k+1 - 1 = 2 · 2k - 1 = 2N - 1
Hope this helps!

Related

Passing integer to kernel function

------ Context ------
i am completely new to swift and metal(kit?).
Problem is: i need to realize a parallel prefixSum(to be specific: the Blelloch scan) calculation in the next 2 weeks.
Due to me not having a NVIDIA GPU but an M1 Macbook, i chose metal, so please be patient with me..
After watching this video (the code is in the video-description), I now try to modify the code so that the prefixSum is calculated instead of just adding two arrays at index i.
------ Question ------
In the first step,I only want to pass an integer myVal and just add up the three values.
Unfortunately, the output for the first 3 values looks like this(note the last digit, which has the expected value but without the additional myVal):
3 + 5 + 10 = 42949672978
2 + 6 + 10 = 42949672978
8 + 3 + 10 = 42949672981
instead of this:
3 + 5 + 10 = 18
2 + 6 + 10 = 18
8 + 3 + 10 = 21
The main.swift code is in this pastebin. The compute.metal can be found here

How do I average elements of a matrix according to an identity matrix?

I have a 333x1 vector of values ('data') and each of the cells in the vector correspond to a range of 1 of 13 subcategories. The identities of each of these subcategories are stored in a separate identity matrix ('id'). I'd like to calculate the sum of the values within the original data matrix that have a similar identity.
e.g. pretending for this example that 'data' and 'id' are 8x1 vectors
data = [1;1;1;0;0;0;1;1]
id = [1;2;1;2;3;3;1;3]
sum of id 1: 1 + 0 + 1 + 0 + 0 + 0 + 1 + 0 = 3
sum of id 2: 0 + 1 + 0 + 0 + 0 + 0 + 0 + 0 = 1
sum of id 3: 0 + 0 + 0 + 0 + 0 + 0 + 0 + 1 = 1
I'm sure that there is a really easy fix for this, however I can't seem to work it out.
Thanks for your time
Mac
A simple solution would be:
numCategories = 13;
totals = zeros(numCategories,1);
for idnum = 1:numCategories
totals(idnum) = sum((id==idnum).*data);
end
EDIT: As knedlsepp pointed out in the comments, the accumarray function accomplishes exactly what the above code does in one line.
accumarray(id,data);

Have a trouble with the function roots

Hey guys I have multiple problems with using function 'roots'.
I Have to find zeros of 's^1000 + 1'.
I made Y = zeros(1,1000) then manually changed the 1000th matrice to '1'. but then 'root' function does not work with it !
Another problem is that I am having trouble with matrix multiplication. The question is finding zeros(roots) of (s^6 + 6*s^5 + 15*s^4 + 20*s^3 + 15*s^2 + 6*s +1)*(s^6 + 6s^5 + 15*s^4 +15*s^2 +6*s +1) so i did:
a = [1 6 15 20 15 6 1]
b = [1 6 15 0 15 6 1]
y = a.*b;
roots(y)
but this gives me
-27.9355 + 0.0000i
-8.2158 + 0.0000i
0.1544 + 0.9880i
0.1544 - 0.9880i
-0.1217 + 0.0000i
-0.0358 + 0.0000i
where I calculate the original equation with wolfram then I have made matrix as :
p = [1 12 66 200 375 492 524 492 375 200 66 12 1]
roots(p)
and this gives me :
-3.1629 + 2.5046i
-3.1629 - 2.5046i
0.3572 + 0.9340i
0.3572 - 0.9340i
-1.0051 + 0.0000i
-1.0025 + 0.0044i
-1.0025 - 0.0044i
-0.9975 + 0.0044i
-0.9975 - 0.0044i
-0.9949 + 0.0000i
-0.1943 + 0.1539i
-0.1943 - 0.1539i
and I think the second solution is right (that is what wolfram alpha gave me)
How would you answer these two questions through matlab guys?
To multiply polynomials, you convolve their coefficients:
>> roots(conv(a,b))
ans =
-3.1629 + 2.5046i
-3.1629 - 2.5046i
0.3572 + 0.9340i
0.3572 - 0.9340i
-1.0051
-1.0025 + 0.0044i
-1.0025 - 0.0044i
-0.9974 + 0.0044i
-0.9974 - 0.0044i
-0.9950
-0.1943 + 0.1539i
-0.1943 - 0.1539i
Q1
Using roots to find the roots of s1000 + 1 is a bit of an overkill. The solution is given by this code snippet (corrected the first version; may be deduced using De Moivre's formula):
n = 1000;
k = 0:n-1
u = (2*k + 1)*pi / n;
s = cos(u) + 1i*sin(u)
Also, this method is approx. 100000 times faster.
Q2
Multiplying two polynomials to find the roots of their product is a bit of an overkill. :-) The union of the two polynomials' root sets is the root set of the product polynomial:
s = [roots(a);roots(b)]
Also, this method is more accurate.
1) The vector describing s^1000 + 1 should end with a 1 as well.
2)
a = [1 6 15 20 15 6 1]
b = [1 6 15 0 15 6 1]
y = a.*b;
This is a DOT product, multiplication of polynomials do not multiply element-wise.
Question 1
You need to include the coefficient of x^0 in the vector of coefficients, so there are 1001 entries with the first and last being 1
coeffs=zeros(1001,1);
coeffs([1,1001])=1;
roots(coeffs)
Question 2
To multiply the coefficients of polynomials you need to use convolution:
roots(conv(a,b))

MATLAB - Summing values after reading their position from another matrix

Suppose I have an array that consists of some elements (A) and I want to take the nth element and sum it with a number of elements that follow it, and I have a matrix that gives me the position of each of the elements from which I should start summing (B).
For example I have
A = [2 3 4 5 6 3 5 7 3 7 3 7 4 7 3 6 2 6 3 8 4 8 4 8 5 8 3]
And I have
B = [8 12 15]
So I would want to sum the value at position B(1,1) which is 8, therefore A(1,8) with the next 4 elements in the matrix
C(1,1) = 7 + 3 + 7 + 3
C(1,2) = 7 + 4 + 7 + 3
C(1,3) = 3 + 6 + 2 + 6
It's always a big fun to use bsxfun:
idx = bsxfun( #plus, B, (0:3)' ); %//' indices of A to be summed together
C = sum( A(idx), 1 );
What I undersdtand from you question is that you want to calculate a matrix C, you only shown an example for calculating C(1,1). C(1,2) will be like,
C(1,2) = 7 + 4 + 7 + 3 + 6 + 2 + 6 + 3 + 8 + 4 + 8 + 4 + 8 + 5 + 8 + 3 = 86
If I'm right my answer is like this
for i = 1:n
C(i) = 0;
for j = B(i):m
C(i) += A(j);
end
end
where
n = B's length (2 in your example)
m = 4 (because you want to add next 4 elements in the matrix)

Problem with numbers

I am given a number N, and i must add some numbers from the array V so that they wil be equal. V is consisting of numbers that are all powers of 3:
N = 17
S = 0
V = 1 3 9 27 81 ..
I should add numbers from V to N and S in order to make them equal. The solution to the example above is :
17 + 1 + 9 = 27, 27, 1 and 9 are taken from V, a number from V can be taken only once, and when taken it's removed from V.
I tried sorting V and then adding the biggest numbers from V to S until S has reached N, but it fails on some tests when it's like:
N = 7
S = 0
V = 1 3 9 27
So the solution will be:
7 + 3 = 9 + 1
In examples like this i need to add numbers both to N and S, and also select them so they become equal.
Any idea of solving this ? Thanks.
Write N in base 3: 17 = 2*1 + 2*3 + 1*9
Find the first power of 3 with coefficient 2, in this case 1.
Add this power of 3: 17 + 1
Repeat until all coefficients are 0 or 1.
17 = 2*1 + 2*3 + 1*9
17 + 1 = 2*9
17 + 1 + 9 = 27
7 = 1*1 + 2*3
7 + 3 = 1*1 + 1*9