I am trying to write a backtracking algorithm that keeps state using mutable BitSets, it works fine but I want it to go faster!
The crux is given two mutable.BitSet alpha and beta I need to calculate if any of the bits of alpha are set in beta, i.e. bitwise AND. I do not need the resulting set just need to know if the intersection isNonEmpty
(alpha intersect beta).nonEmpty
or
(alpha & beta).nonEmpty
but both of these construct a set which is then tested for size... I really just need a boolean and would like to avoid the cost of constructing the intermediate set.
Is there a better way?
TIA
Nivag
Referring to the API docs, you may use find and contains method.
alpha find (beta.contains) isDefined
OR
Even better, use exists method.
alpha exists (beta.contains)
OR
Even shorter and better, use apply method of BitSet which is equivalent to its contains method.
alpha exists beta
Related
Given a matrix's LDLT decomposition, I would like to modify the diagonal - for example floor all the values. Is there a way to do this with eigen?
To be clear, I can do:
auto ldlt_ = matrix.ldlt();
and I would like to follow up with:
ldlt_.vectorD().cwiseMax(Vector::Constant(n,epsilon))
before solving a problem:
ldlt_.solve(a)
I don't see any non const accessors to the vectorD member - what am I missing?
No, you cannot do that, and I don't think that's a good idea to increase small (or negative) diagonal entries this way. If there are too small entries, the usual approach is either to ignore them (default behavior of LDLT::solve), or to redo the factorization with matrix+eps*I. Anyway, if you really want to tweak D yourself, then you have to implement your own solve function.
The title is perhaps misleading but I don't know how this is called. I do want to know how to type cast for example an int variable to create a string image_number, where number is the value of that variable.
But the thing that bothers me the most is how to call for example an imageSprite with a string constructed in similar way.
I have 16 sprites in a list and some for loops, and have trouble calling sprites. I want to for example check whether that particular sprite has an image set. I know I can use the block that tells me whether that's the case or not, but I want to say:
if the number you are increasing is n, I want you to check whether imageSprite_n has an image set or not (without using if number == n, use imageSprite_n).
I hope my question is clear. Is there any way to do that or should this be done without a loop?
Learn how to work with lists
How to work with Lists by Saj
How to work with Lists and Lists of
lists (pdf) by appinventor.org
and learn how to use the advanced blocks
Then you can do it like this
The example uses 4 image sprites and checks, if image sprite 3 has an image set
Is there a good practice to check if my result Eigen::Matrix4f is almost identity? Since due to floating point errors I don't get some times exactly only zeros and ones.
One brute force method would be, to check each value in the matrix if it is between certain EPSILON and if just one of them fails, then it is not an identity matrix. Is there a better solution?
First, you have to define in what sense they shall be "close". There can be many different definitions of closeness, depending on your specific task. One of the most used is:
norm( A - I ) < eps
where norm is some matrix norm. Most common are 2-norm, 1-norm, inf-norm and Frobenius norm.
Your method is also possible. It is equivalent to the method above with max-norm (where norm(A) = max abs Aij). It can be implemented in Eigen using:
(A - Matrix4f::Identity()).cwiseAbs().max() < eps;
Update:
Actually, in Eigen there is a special method to check that: isIdentity. You give it the threshold value:
A.isIdentity(eps)
Suppose that I have these Three variables in matlab Variables
I want to extract diverse values in NewGrayLevels and sum rows of OldHistogram that are in the same rows as one diverse value is.
For example you see in NewGrayLevels that the six first rows are equal to zero. It means that 0 in the NewGrayLevels has taken its value from (0 1 2 3 4 5) of OldGrayLevels. So the corresponding rows in OldHistogram should be summed.
So 0+2+12+38+113+163=328 would be the frequency of the gray level 0 in the equalized histogram and so on.
Those who are familiar with image processing know that it's part of the histogram equalization algorithm.
Note that I don't want to use built-in function "histeq" available in image processing toolbox and I want to implement it myself.
I know how to write the algorithm with for loops. I'm seeking if there is a faster way without using for loops.
The code using for loops:
for k=0:255
Condition = NewGrayLevels==k;
ConditionMultiplied = Condition.*OldHistogram;
NewHistogram(k+1,1) = sum(ConditionMultiplied);
end
I'm afraid if this code gets slow for high resolution big images.Because the variables that I have uploaded are for a small image downloaded from the internet but my code may be used for sattellite images.
I know you say you don't want to use histeq, but it might be worth your time to look at the MATLAB source file to see how the developers wrote it and copy the parts of their code that you would like to implement. Just do edit('histeq') or edit('histeq.m'), I forget which.
Usually the MATLAB code is vectorized where possible and runs pretty quick. This could save you from having to reinvent the entire wheel, just the parts you want to change.
I can't think a way to implement this without a for loop somewhere, but one optimisation you could make would be using indexing instead of multiplication:
for k=0:255
Condition = NewGrayLevels==k; % These act as logical indices to OldHistogram
NewHistogram(k+1,1) = sum(OldHistogram(Condition)); % Removes a vector multiplication, some additions, and an index-to-double conversion
end
Edit:
On rereading your initial post, I think that the way to do this without a for loop is to use accumarray (I find this a difficult function to understand, so read the documentation and search online and on here for examples to do so):
NewHistogram = accumarray(1+NewGrayLevels,OldHistogram);
This should work so long as your maximum value in NewGrayLevels (+1 because you are starting at zero) is equal to the length of OldHistogram.
Well I understood that there's no need to write the code that #Hugh Nolan suggested. See the explanation here:
%The green lines are because after writing the code, I understood that
%there's no need to calculate the equalized histogram in
%"HistogramEqualization" function and after gaining the equalized image
%matrix you can pass it to the "ExtractHistogram" function
% (which there's no loops in it) to acquire the
%equalized histogram.
%But I didn't delete those lines of code because I had tried a lot to
%understand the algorithm and write them.
For more information and studying the code, please see my next question.
I need to speed-up some calculation and result of calculation then used to draw OpenGL model.
Major speed-up archived when I changed std::vector to Concurrency::concurrent_vector and used parallel_for instead of just for loops.
This vector (or concurrent_vector) calculated in for (or parallel_for) loop and contains vertices for OpenGL to visualize.
It is fine using std::vector because OpenGL rendering procedure relies on the fact that std::vector keeps it's items in sequence which is not a case with concurrent_vector. Code runs something like this:
glVertexPointer(3, GL_FLOAT, 0, &vectorWithVerticesData[0]);
To generate concurrent_vector and copy it to std::vector is too expensive since there are lot of items.
So, the question is: I'd like to use OpenGL arrays, but also like to use concurrent_vector which is incompatible with OpenGL output.
Any suggestions?
You're trying to use a data structure that doesn't store its elements contiguously in an API that requires contiguous storage. Well, one of those has to give, and it's not going to be OpenGL. GL isn't going to walk concurrent_vector's data structure (not if you like performance).
So your option is to not use non-sequential objects.
I can only guess at what you're doing (since you didn't provide example code for the generator), so that limits what I can advise. If your parallel_for iterates for a fixed number of times (by "fixed", I mean a value that is known immediately before parallel_for executes. It doesn't change based on how many times you've iterated), then you can just use a regular vector.
Simply size the vector with vector::size. This will value-initialize the elements, which means that every element exists. You can now perform your parallel_for loop, but instead of using push_back or whatever, you simply copy the element directly into its location in the output. I think parallel_for can iterate over the actual vector iterators, but I'm not positive. Either way, it doesn't matter; you won't get any race conditions unless you try to set the same element from different threads.