Range update in Binary Indexed Tree - range

How can I use Binary Indexed Tree for range update such that each element A[k] in a range say [i..j] is updated to A[k]*c where c is some constant.
And I need to do point queries after such update operations.
I tried with the function below but it wasn't working, here n is size of array,c is the constant I want to multiply each element of range with.
def updateM(x, c, n):
while x <= n:
BIT[x] *= c
x += (x & -x)
and these are my calls to update the range:
updateM(i, c, n)
updateM(j+1, -c, n)
Any kind of help would be appreciated. :)

The multiplicative inverse of c is not -c but 1/c. Also, I don't understand, what you're trying to accomplish by x += (x & -x).

It's simple. You just have to run a loop.
But this method is over kill.
It would be best to use segment tree and update the appropriate range, not just a single value.

Related

Multiply two vectors with dimensions increasing along time

I have two vectors (called A and B) with length N. Then I need to multiply both of them, but as an "integration" process. Which means I have to multiply first A(1)*B(1), then A(1:2)*B(1:2), until A(1:N)*B(1:N). The result of multiplying booth vector is a number, since B is a column vector. I've done it with a for loop:
for k = 1:N
C(k) = A(1:k) * B(1:k).';
end
But I wanted to ask you if this is the best solution or there is any other option more time-efficient, since N is very large (about 110,000)
C = cumsum(A.*B)
does the same thing without for loop. As EBH suggested in the comments if you are not sure whether A and B have same orientation, then use
C = cumsum(A(:).*B(:))

Why do people use hash(k) = c * k with a prime c

Given an integer m, a hash function defined on T is a map T -> {0, 1, 2, ..., m - 1}. If k is an element of T and m is a positive integer, we denote hash(k, m) its hashed value.
For simplicity, most hash functions are of the form hash(k, m) = f(k) % m where f is a map from T to the set of integers.
In the case where m = 2^p (which is often used to the modulo m operation is cheap) and T is a set of integers, I have seen many people using f(k) = c * k with c being a prime number.
I understand if you want to choose a function of the form f(k) = c * k, you need to have gcd(c, m) = 1 for every hash table size m. Even though using a prime number fits the bill, c = 1 is also good.
So my question is the following: why do people still use f(k) = prime * k as their hash function? What kind of nice property does it have?
You don't need it to be prime. One of the most efficient hash functions with provable collision resistance just multiplies with a random number: https://en.wikipedia.org/wiki/Universal_hashing#Avoiding_modular_arithmetic. You do however need it to be odd.

deleting a column in hansl

I have a very simple question. I want to delete a column from a matrix in a loop.
In Matlab I use the following:
for a certain i,
X(:,i)=[]
which deletes the column an reshapes the matrix.
I want to know the equivalent in Hansl (Gretl) program, please.
Thanks!
Sorry it's probably too late for you now, but I just saw your question and maybe it's useful for others.
In hansl (gretl's scripting and matrix language) I could think of several possibilities:
First, if you happen to know the number of columns and the value of i, the solution could use a hard-wired index vector (for i==2 and cols(X)==5 here):
X = X[, {1, 3,4,5}]
Secondly, since the first solution is probably too restrictive, you could concatenate the left and right parts of the matrix, as in:
X = X[, 1: i-1] ~ X[, i+1 :cols(X)]
But the problem here is that i must not index the first or last column, or the indexing will produce an error.
So my final suggestion that should work universally is:
X = selifc( X, ones(1, i-1) ~ 0 ~ ones(1, cols(X) - i) )
The selifc() function discards the column for which the second vector argument has a 0 entry. This also works for i==1 or i==cols(X).
A shorter variation of this final solution might be:
X = selifc(X, seq(1, cols(X)) .!= i)
which does an element-wise not-equal-to-i comparison (.!=) of the column indices constructed with the seq() function. But it's probably not as readable as the previous way.
good luck!

MATLAB function that gives all the positive integers in a column vector

I need to create a function that has the input argument n, a integer , n>1 , and an output argument v, which is a column vector of length n containing all the positive integers smaller than or equal to n, arranged in such a way that no element of the vector equals its own index.
I know how to define the function
This is what I tried so far but it doesn't work
function[v]=int_col(n)
[1,n] = size(n);
k=1:n;
v=n(1:n);
v=k'
end
Let's take a look at what you have:
[1,n] = size(n);
This line doesn't make a lot of sense: n is an integer, which means that size(n) will give you [1,1], you don't need that. (Also an expression like [1,n] can't be on the left hand side of an assignment.) Drop that line. It's useless.
k=1:n;
That line is pretty good, k is now a row vector of size n containing the integers from 1 to n.
v=n(1:n);
Doesn't make sense. n isn't a vector (or you can say it's a 1x1 vector) either way, indexing into it (that's what the parentheses do) doesn't make sense. Drop that line too.
v=k'
That's also a nice line. It makes a column vector v out of your row vector k. The only thing that this doesn't satisfy is the "arranged in such a way that no element of the vector equals its own index" part, since right now every element equals its own index. So now you need to find a way to either shift those elements or shuffle them around in some way that satisfies this condition and you'd be done.
Let's give a working solution. You should really look into it and see how this thing works. It's important to solve the problem in smaller steps and to know what the code is doing.
function [v] = int_col(n)
if n <= 1
error('argument must be >1')
end
v = 1:n; % generate a row-vector of 1 to n
v = v'; % make it a column vector
v = circshift(v,1); % shift all elements by 1
end
This is the result:
>> int_col(5)
ans =
5
1
2
3
4
Instead of using circshift you can do the following as well:
v = [v(end);v(1:end-1)];

How to scan the whole matrix point by point

Taken I have some transition operations where certain numbers of my matrix (M) are being substituted with each other (all ones become 5, all 2s become 6, etc.). Now I know I can write a code like this:
if M(1,1)== 1 M(1,1)=5 end
if M (1,1)== 2 M(1,1)=6 end
if M(1,2)==1 M(1,2)=6 end
Of course if I have (10-by-10-by-10) matrix this is a lot of unnecessary work. Is there a possibility to either define the 3 dimensions (column, row, page) or to tell matlab something like:
% scan from point (1,1,1) to point (10,10,10) and apply mathematical operations when condition is fulfilled.
Thank you
There is a simple command that changes all as to bs in an array using logical indexing. For example,
B = (M == 1);
M(B) = 5;
should change all 1s in M to 5s.
I'm not too familiar with MATLAB, but it seems like you should be able to define 3 scalar variables i, j, and k, and use 3 nested for loops to iterate over every ordered triple (x, y, z) with 0 <= x,y,z < 10... performing the switching logic inside the innermost loop.