What is the tf.maximum equivalent in C++ - c++

I am trying to find tf.maximum(X, Y) function equivalence in C++. Basically, what tf.maximum() does is "Returns the max of x and y (i.e. x > y ? x : y) element-wise."
If there is a library or a built-in function, I would like to use it.
I really want to find the fastest option for that except going through all the elements in a matrix or vector.
The main reason why I want to use a max function, I would like to replace any negative value item with 0 in a matrix or a vector in C++.
Any suggestions? Thank you in advance.

The normal C++ way of replacing values with other values is std::transform and a function object
std::transform(thing.begin(), thing.end(), thing.begin(), [](float value) { return std::max(0, value); });

Related

Using binary search tree to store multidimensional vectors

I have an array of vector-3 values:
struct Vector3 {
int x, y, z;
...
};
I would like to put these values into a binary search tree for quick search and finding duplicates. In a binary search tree we need to compare values to set them in a correct order. But would there be a way to compare vectors that makes sense?
I thought about comparing them by length but this will not find duplicates correctly, as two vectors can point in different directions but have the same length.
Comparing each element doesn't make sense either since it will give different results of comparison based on order in which you compare two vectors.
So does utilizing a binary search tree for multidimensional vectors makes sense? Or should I look into hash tables?
Edit: The suggested answer implements comparison between two two-dimensional vectors (points) like this (x < pt.x) || ((!(pt.x < x)) && (y < pt.y));. But there's no explanation as to how/why this comparison works.
When dealing with performance work, I do like John Carmack does in "Masters of Doom." Do the simplest possible thing first. You could start with a binary tree with a sort function sorting by x, then y, and then z. I'd measure and if that wasn't fast enough, depending on your ranges for X, Y, and Z, I might try a hash map (maybe hashed using bitshifting based on the possible ranges for X, Y and Z), or look into storing them in an octtree.

How to do Y = aX + Y in C++

I am trying to do matrix operations using C++ STL containers.
There are two vectors of sizes say Y and X of sizes m,n(m>n). I want to multiply X with a scalar and add it from a given index in Y. In this process I don't want to transform X (don't want to use std::transform). In fact the X's are columns in a matrix DPtr. One version I tried is given below.
std::vector<double> D(&DPtr[index1],&DPtr[index1]+size_C);
std::transform(D.begin(), D.end(), D.begin(),std::bind2nd(std::multiplies<double>(), val1*val2));
std::transform (D.begin(), D.end(), YPtr.begin()+index2, YPtr.begin()+index2, std::plus<double>());
I am trying to copy the column in to a temporary vector and do operations on it.
Can some one help me in rewriting the code in a much simpler manner where I need not copy columns into another vector?
I am guessing I have to use std::for_each and lamda expression or a function call? But I am new to C++?
Just to give a lead, I want to write it as
std::for_each(YPtr.begin()+index2,YPtr.begin()+index2+(size_c-1),scalarAdd);
using a function scalarAdd or any lamda expression where I can access DPtr directly.
Also can I write
YPtr.begin()+index2+(size_c-1)
.
as the second argument.Is it valid?
Also Imagine I made the matrix as a C++-vector where all columns of DPtr matrix are stored in one single C++ vector D.
Visual Representation of my question
May I suggest you use a dedicated linear-algebra library like Eigen? With that you could simply write Y += X * a, and the library+compiler will figure out the best implementation for you.
Since you are using C++11, you could use std::transform together with a lambda (std::for_each is not recommended since you need to transform your Y).
std::transform(DPtr.begin() + index1, DPtr.begin() + index1 + size_c, // X
YPtr.begin() + index2, // Y_in
YPtr.begin() + index2, // Y_out
[](double x, double y_in) { return a*x + y_in; });
// y_out = a*x + y_in, for every entry.

Function of a letter in C++

I have the following expression:
A = cos(5x),
where x is a letter indicating a generic parameter.
In my program I have to work on A, and after some calculations I must have a result that must still be a function of x , explicitly.
In order to do that, what kind of variable should A (and I guess all the other variables that I use for my calculations) be?
Many thanks to whom will answer
I'm guessing you need precision. In which case, double is probably what you want.
You can also use float if you need to operate on a lot of floating-point numbers (think in the order of thousands or more) and analysis of the algorithm has shown that the reduced range and accuracy don't pose a problem.
If you need more range or accuracy than double, long double can also be used.
To define function A(x) = cos(5 * x)
You may do:
Regular function:
double A(double x) { return std::cos(5 * x); }
Lambda:
auto A = [](double x) { return std::cos(5 * x); };
And then just call it as any callable object.
A(4.); // cos(20.)
It sounds like you're trying to do a symbolic calculation, ie
A = magic(cos(5 x))
B = acos(A)
print B
> 5 x
If so, there isn't a simple datatype that will do this for you, unless you're programming in Mathematica.
The most general answer is "A will be an Expression in some AST representation for which you have a general algebraic solver."
However, if you really want to end up with a C++ function you can call (instead of a symbolic representation you can print as well as evaluating), you can just use function composition. In that case, A would be a
std::function<double (double )>
or something similar.

Fastest way to zero out a vector<vector<bool> >

I've tried something like this:
vector<bool> x(10, 0);
vector<vector<bool> > hello(10, x);
/*some modifications on hello*/
memset(&hello, 0, sizeof(hello));
And my program compiles, but it breaks. Any idea how I can do this operation as quickly as possible? I know that the memset probably isn't working because of the nested vector, but I'm not sure how to accomplish this task.
I would use this, which reuses the x variable you declared in the question.
std::fill(hello.begin(), hello.end(), x);
for(auto& bv : hello)
std::fill(begin(bv), end(bv), false);
Or if you want to use x as the prototype
std::fill(begin(hello), end(hello), x);
With the code you have, I'd write …
for( auto& v : hello ) { v = x; }
assuming x has remained as all-zeros. It's clear enough and avoids dragging in <algorithm>.
Hewever, it will probably be faster to change the representation of your bit matrix, from a vector of vectors to a single vector, or if it's fixed size, to a single bitset.

Dividing each element in a container between a given number C++

I was multiplying each container against another number so I did the following:
local_it begin = magnitudesBegin;
std::advance(begin , 2);
local_it end = magnitudesBegin;
std::advance(end, 14);
std::transform(begin, end, firstHalf.begin(),
std::bind1st(std::multiplies<double>(),100));
It worked wonders, problem is when doing the same to divide between another container. Here is a working example of my problem:
const std::size_t stabilitySize = 13;
boost::array<double,stabilitySize> secondHalf;
double fundamental = 707;
boost::array<double, stabilitySize> indexes = {{3,4,5,6,7,8,9,10,11,12,13,14,15}};
std::transform(indexes.begin(), indexes.end(), secondHalf.begin(),
std::bind1st(std::divides<double>(),fundamental));
It does divide the container. But instead of dividing each element in the array against 707 it divides 707 between each element in the array.
std::bind1st(std::divides<double>(),fundamental)
The code above takes a functor std::divides<double> that takes two arguments and fixes the value of the first argument to be fundamental. That is it fixes the numerator of the operation and you get the expected result. If you want to bind fundamental to be the denominator, use std::bind2nd.
you can try the following , divide has a completely different operation than multiply, it just divides a constant number by all your elements
std::bind1st(std::multiplies<double>(),1.0/707.0));
If the number 707.0 is something like a fundamental constant, and a division can be seen as a "conversion", let's call it "x to y" (I don't know what your numbers are representing, so replace this by meaningful words). It would be nice to wrap this "x to y" conversion in a free-standing function for re-usability. Then, use this function on std::transform.
double x_to_y(double x) {
return x / 707.0;
}
...
std::transform(..., x_to_y);
If you had C++11 available, or want to use another lambda-library, another option is to write this in-line where being used. You might find this syntax more readable like parameter binding using bind2nd:
std::transform(..., _1 / 707.0); // when using boost::lambda