%a = add <6 x i3> ,
I want to write a pass for type legalization.
for (auto &block : function) {
std::vector<BinaryOperator *> binInstsToErase;
for (auto &inst : block) {
BinaryOperator * binaryOpInst = dyn_cast<BinaryOperator>(&inst);
Value * vector1 = inst.getOperand(0);
Value * vector2 = inst.getOperand(1);
Currently, The vector1 and vector2 is 18bits long(numofElem(6)*elementsize(3)). First, I want to widen the vector1 and vector2 from <6 x i3> to <8 x i3>, which means the number of the element is legal. However, I don't know how to do it.
Use ConstantDataVector::get(LLVMContext &Context, ArrayRef< uint8_t > Elts) to create a new vector first. You can use a std::vector as Elts. Then you can push_back origin values into the vector or extract and insert element to the new vector.
See http://llvm.org/doxygen/classllvm_1_1ConstantDataVector.html
Related
I'm plotting a data file with two columns (frequency and voltage) and I need to look for the closest value to a given value val. The thing is that my data behaves like a gaussian, so there are two values that satisfies that, above and below a max value. First I get each column in the data file into a vector array, having that I defined this function for finding those values
#include<iostream>
#include<vector>
#include<cmath>
typedef std::vector <double> vector;
vector posi(vector vec, int ref, double val);
int main(void){
//define a custom volt vector here e.g vector volt{...};
auto auxvmax = std::max_element(volt.begin(), volt.end());
int posvmax = auxvmax - volt.begin();//this is what I take as ref value
double val = 0.7;
vector fpos(2, 0.0);
fpos = posi(volt, posvmax, val);
double auxf_1 = fpos[0];
double auxf_2 = fpos[1];
std::cout << "closest value to " << val << " are " << volt[auxf_1] << " below and " << volt[auxf_2] << " above\n";
return 0;
}
vector posi(vector vec, int ref, double val){
vector posvec(2, 0.0);
auto pos1 = std::lower_bound(vec.begin(), vec.begin() + ref, val);
auto pos2 = std::lower_bound(vec.begin() + ref, vec.end(), val);
double val1a = *(pos1 - 1.0);
double val1b = *pos1;
double val2a = *(pos2 - 1.0);
double val2b = *pos2;
if(fabs(val - val1a) < fabs(val - val1b)){
posvec[0] = pos1 - vec.begin() - 1;
}
if(fabs(val - val1a) > fabs(val1b)){
posvec[0] = pos1 - vec.begin();
}
if(fabs(val - val2a) < fabs(val - val2b)){
posvec[1] = pos2 - vec.begin() - 1;
}
if(fabs(val - val2a) > fabs(val - val2b)){
posvec[1] = pos2 - vec.begin();
}
return posvec;
}
Let me explain how and why I constructed the function like this, so you can tell me where am I wrong.
Basically, I'm trying to use std::lower_bound() in two "regions" of the vector in which the values are, this is so that the program looks for only one closest value in each region, I know where the max value is (in main function, via std::max_element()) so I can easily make the split. ref is the position (iterator) where the max value is allocated in vec vector, so it is supposed to get the closest value to val above and below that ref position.
Next, it only makes sure that the value is the closest, considering the smallest next value to val (default value given by std::lower_bound()) and the previous one, getting the closest in each case (region). Finally, the position in the vector of each value is stored into posvec vector (since I have to get the frequencies where voltage is the closest to val, so I don't need the voltage value but his position, since freq and volt make pairs).
when I compile it gives no errors, no warnings, but the closest values are not the closest. I found (cppreference) that the array that std::lower_bound() gets must be sorted from low to max, and my data is sorted from lowest to max below the max value, and max to lowest above max value, so there should be a problem with the above data, but -and here is my question- why am I not getting the closest value even with the data below? am I not getting something with the std::lower_bound() behavior?, is it maybe something when I use the if statements? the output, printing an example voltage vector as well is below
Here you can see the "closest" values are indeed the furthest.
Thanks for your help in advance.
EDIT: asked in comments, the output is
closest values to 0.7 are 0.485437 below, and 0.320388 above
0.485437
0.500971
0.524272
0.543689
0.563107
0.594175
0.617476
0.648544
0.679612
0.71068
0.741748
0.786408
0.825243
0.864078
0.893204
0.932039
0.961165
0.980583
0.990291
1
0.990291
0.961165
0.941748
0.893204
0.854369
0.805825
0.757282
0.708738
0.669903
0.621359
0.582524
0.547573
0.512621
0.481553
0.454369
0.427184
0.403883
0.384466
0.361165
0.341748
0.320388
Hello i´ve created a simple 2D coordinate class (Point2D) and i want to overload this class with some basic operations ( + - * / ) for some basic types(short,int,double....) so i thought i should use a template. The problem is that i get some strage numbers(i think that these are the max size of the type) as x and y value back so therefore it ignores my e.g addition.
Point2D *p = new Point2D(5, 7) //everything works fine here x=5 y=7
Point2D *p = new Point2D(5, 7) + 10;//x= 1.09 * e^-38 same for y but it want x=15 and y=17
Heres the definition of my operation overloading function :
template<class T>
Point2D Point2D::operator+(initializer_list<T> elementList)
{
for (auto elem : elementList)
{
this->x += elem;
this->y += elem;
}
return Point2D(x,y);
}
The behaviour of
Point2D *p = new Point2D(5, 7) + 10;
is undefined. The right hand side is grouped as (new Point2D(5, 7)) + 10 which is adding 10 lots of sizeof(Point2D) to the Point2D* pointer returned back from new!
You are not allowed to set a pointer to memory you don't own, with the exception of one past the end of an array, or one past the address of a scalar, or nullptr.
You can't add 10 to a pointer like that. One way to fix it would be to simply create Point2D's as a local variable:
Point2D p (5, 7);
Point2D q = p + 10;
q will now equal 15,17
That's assuming you create an operator+ that can take ints - your initializer_list one won't work here (because 10 isn't an initializer_list:
template<class T>
Point2D Point2D::operator+(T e)
{
x += e;
y += e;
return Point2D(x, y);
}
How do you write a vector in OpenCV? that contains 3 values like this v = [p1,p2,p3]? This is what I tried:
int dim [1] = {3};
Mat v(1,dim,CV_32F, Scalar(p1,p2,p3));
But when I do debug in Qt, I see in the local and expression window that the vector v indeed has 1 column and 3 rows but has also 2 dim. I was wondering if this is due to the Mat type in the declaration.
With which type could I replace it to get just a simple vector of 3 values?
Which type could I use to get just a simple vector of 3 values? I want to do some operation on this vector like use the norm and change it's elements.
You can use cv::Vec type.
Assuming you're working on double values (the same applies for other types) you can:
// Create a vector
Vec3d v;
// Assign values / Change elements
v[0] = 1.1;
v[1] = 2.2;
v[2] = 3.3;
// Or initialize in the constructor directly
Vec3d u(1.1, 2.2, 3.3);
// Read values
double d0 = v[0];
// Compute the norm, using cv::norm
double norm_L2 = norm(v, NORM_L2); // or norm(v);
double norm_L1 = norm(v, NORM_L1);
For:
double type use Vec3d
float type use Vec3f
int type use Vec3i
short type use Vec3s
ushort type use Vec3w
uchar type use Vec3b
So, I have a set of points in 3D, and I would like to store them in a 3 dimensional vector. Then I need sort that vector, giving priority first to the X dimention, then Y, then Z. So, for example, if I have this set of points:
P1 = (5, 10 ,9)
P2 = (1, 11, 4)
P3 = (8, 5, 2)
P4 = (5, 10, 3)
P5 = (5, 4, 0)
I would like to get a vector sorted like this:
[1, 11, 4]
[5, 4, 0]
[5, 10, 3]
[5, 10, 9]
[8, 5, 2]
So, how can a sort a multidimentional vector taking all rows into account?
Should I use std::priority_queue instead? If so, how show I use it?
Thanks
You could use an std::tuple<double, double, double> to represent a point. The comparison for std::tuple works lexicographically, the way you want it to. Alternatively, you could provide a custom sort function to your vector of points. Something like this:
sort(pointVector.begin(), pointVector.end(), [](const Point& lhs, const Point& rhs){//Implement your required comparison predicate here});
Also, as this question shows, you can achieve some sort of a named-tuple-with-lexicographic-sorting by using std::tuples lexicographic sort and std::tie.
...giving priority first to the X dimention, then Y, then Z
Use std::sort with std::tie, something like following
#include <algorithm>
#include <tuple>
//....
struct Points // Your 3D Point
{
float x,y,z;
} ;
std::vector<Points> v; // Vector of 3D points
std::sort( v.begin(), v.end(),
[]( const Points& lhs, const Points& rhs )
{
return std::tie(lhs.x,lhs.y,lhs.z)
< std::tie(rhs.x,rhs.y,rhs.z) ;
}
) ;
DEMO
You can use the std::sort() to easily sort according to your specific conditions by making your own comparator function.
Assuming you have stored a single 3D point in a struct point, and the points in a std::vector<points> (A std::tuple might be more useful.), try out this code.
Example:
#include <vector>
#include <algorithm>
using namespace std;
struct point
{
float x, y, z;
}
bool mySort(const point& a, const point& b)
{
//A naive comparison to help you understand better.
//You could always use std::tie for lexicographical comparison.
if (a.x == b.x)
{
if (a.y == b.y)
return a.z < b.z;
else
return a.y < b.y;
}
else
return a.x < b.x;
}
int main()
{
vector<point> graph;
//push_back() all your points into the graph.
//mySort() is a custom comparator function.
sort(graph.begin(),graph.end(),mySort);
}
I have std::set<std::pair<float,float>> which represents points on map ( 2d , x and y value) and I have one point with values x1 and y1. How to sort set in ascending order by distance from point ( x1,y1) ?
std::set is an ordered container, and ordering happens upon insertion, depending on a sorting criteria which can be specified with a second template argument. So use a set with a predicate which returns true or false based on the distance to the reference point.
struct DistanceCompare
{
DistanceCompare(const std::pair<float,float>& point) : point_(point) {}
bool operator()(const std::pair<float,float>& lhs,
const std::pair<float,float>& rhs) const
{
return distance2(lhs) < distance2(rhs);
};
private:
float distance2(const std::pair<float,float>& point) const
{
// calculate distance squared between point and point_
const float x = point.first - point_.first;
const float y = point.second - point_.second;
return x*x + y*y;
}
std::pair<float, float> point_;
};
....
std::pair<float,float> refPoint = ....;
DistanceCompare comp(refPoint);
std::set<std::pair<float, float>, DistanceCompare> pointSet(comp);
It is enough to compare the distance squared, thus avoiding calls to std::sqrt.
The distance between two points can be calculated as follows:
xd = x2-x1;
yd = y2-y1;
Distance = SquareRoot(xd*xd + yd*yd);
And the value of Distance can be used as sorting parameter.