I am trying to convert my c++ vector to a torch tensor. However, my code is returning incorrect conversions.
cout << history << endl;
auto options1 = torch::TensorOptions().dtype(torch::kFloat32);
input = torch::from_blob(history.data(), {size, 1, 6}, options1).to(torch::kFloat32);
cout << input << endl;
The above code returns the following output:
-9 -3 -3 -12 -0 -0 -12 -2 -3 -12 -0 -0
(1,.,.) =
-9.8681e-32 4.5793e-41 -9.8682e-32 4.5793e-41 -9.8682e-32 4.5793e-41
(2,.,.) =
-9.8682e-32 4.5793e-41 -9.8683e-32 4.5793e-41 -9.8683e-32 4.5793e-41
[ CPUFloatType{2,1,6} ]
You can not convert a 2D vector or more dimension to Tensor with the from_blob() method, but you can use this method to overcome the problem:
vector<float> linearize(const vector<vector<float>>& vec_vec) {
vector<float> vec;
for (const auto& v : vec_vec) {
for (auto d : v) {
vec.push_back(d);
}
}
return vec;
}
And, in this way, you can convert it to Tensor (m,n width and height of vector):
vector<float> vec = linearize(your2Dvector);
torch::Tensor t = torch::from_blob(vec.data(), {n,m});
Related
I found a strange behavior of C ternary operator (?:).
In the following code, the expected values of both b and c should be 0, but b is -2.
I checked the C operator precedence, and made sure minus(-) is higher than greater than or equal to (>=), which is higher than the conditional operator (?:). Could anyone kindly explain why the values of b and c are different?
#include <iostream>
#include <vector>
using std::vector;
using std::cout;
using std::endl;
int main() {
int i;
vector<int> a;
for (i = 0; i < 29; ++i)
a.push_back(i);
int b = 27 - a.size() >= 0 ? 27 - a.size() : 0;
int c = 27 - 29 >=0 ? 27 - 29 : 0;
cout << b << endl;
cout << c << endl;
return 0;
}
After looking on the document page of vector, the return type of method size() is size_t which is equal to an unsigned long long.
So, when you do 27 - a.size() this will cause overflow, making the result of the 27 - a.size() >= 0 operator be True. It got nothing to do with C operator precedence.
To prove that, you can do:
#include <iostream>
using std::cout;
using std::endl;
int main() {
unsigned long long tmp = 29;
cout << 27 - tmp << endl; //(this will be a super large integer.)
return 0;
}
Solution:
The solution is simple, you can simply add a typecasting (int) before a.size() in the condition of the ternary operator.
It can be looks like this:
int b = 27 - (int) a.size() >= 0 ? 27 - a.size() : 0;
I have an Eigen Vector. I would want to cat it recursively. For example
Eigen::Vector3d vec;
vec << 5, 6, 7;
Eigen::VectorXd vecCat;
for(int i=0;i<3;i++)
vecCat << vec(i),0,0;
cout<<vecCat<<endl;
so that the final output would be
vecCat= 5 0 0 6 0 0 7 0 0
I am getting an error if I do the above way. Can anyone help me?
As I said in the comment, I will not explain how one could use the CommaInitializer iteratively. But here is a solution using Eigen::Map:
Eigen::Vector3d vec;
vec << 5,6,7;
Eigen::VectorXd vecCat = Eigen::VectorXd::Zero(9); // result vector
{
// map vector to 3x3 matrix:
Eigen::Map<Eigen::MatrixXd> map(vecCat.data(), 3,3);
map.row(0) = vec.transpose(); // set top elements to elements of vec
}
std::cout << vecCat.transpose() << '\n';
If row(0) is everything you need to modify in map you can alternatively (instead of the { } block) write:
Eigen::MatrixXd::Map(vecCat.data(), 3, 3).row(0) = vec.transpose();
I am looking for a good way to round an int in Qt to the nearest 5.
e.g:
8 -> 10
12 -> 10
13 -> 15
15 -> 15
17 -> 15
and so on
Rounding in C++ to the nearest integer number usually is done via:
static_cast<int>(number + 0.5);
Now, to round it to the next 5, I would bring it into the system where we can apply this rounding rule (i.e. 5 -> 1, 6 -> 1.2) and then bring it back into the system where 5 really is 5:
int roundToNearestFive(int number)
{
return static_cast<int>(number / 5. + .5) * 5;
}
I find this formulation easiest.
Here a possible solution:
#include<iostream>
int toNearest5(int i) {
int r = i%5, o = 0;
if(r) {
o = r/5. >= .5 ? 5 : 0;
}
return (i-r+o);
}
int main() {
using namespace std;
cout << toNearest5(8) << endl;
cout << toNearest5(12) << endl;
cout << toNearest5(13) << endl;
cout << toNearest5(15) << endl;
cout << toNearest5(17) << endl;
}
The idea is to get the number and round it to the lowest multiple of 5 (you can do that by removing the remainder), that is:
int remainder = i%5;
int rounded = i - remainder;
Now, you can check the remainder and add 5 to the rounded number if the remainder is greater than 2.5, otherwise add 0.
In order to check it, I've divided the remainder by 5 (its upper bound), so that to get a number in [0,1[ and check it with 0.5 to know how to round the original number.
It follows a more compact version of the same function:
int toNearest5(int i) {
int j = i%5;
return (i - j + (j/5. >= .5 ? 5 : 0));
}
I don't know if the Qt framework offers something similar out of the box, but it's a matter of an one line function, you can easily write it for yourself.
i'm having trouble getting the correct output of the function. The function output should show the expression. For example, if the input is "1234", then the output should be 1 + 2 + 3 + 4 = 10.
i can get the function to output the first part of the expression, but i'm not sure how to get it to output the sum as well.
heres what i have so far:
void sumDigits(int num, int &sum){
sum += num % 10;
if(num < 10)
cout << num;
else {
sumDigits(num/10, sum);
cout << " + " << num % 10;
}
}
why don't you do it in caller?
you could pass the remaining digits number to process to a function and output sum when it's 0, but I doing it in the caller is better..
<< can be chained. Try
cout << " + " << num % 10 << " = " << sum << endl;
Please see the following Question recently posted on HackerRank
Adam is standing at point (a,b) in an infinite 2D grid. He wants to know if he can reach point (x,y) or not. The only operation he can do is to move to point (a+b,b), (a,a+b), (a-b,b), or (a,a-b) from some point (a,b). It is given that he can move to any point on this 2D grid,i.e., the points having positive or negative X(or Y) co-ordinates.Tell Adam whether he can reach (x,y) or not.
https://www.hackerrank.com/contests/infinitum-jun14/challenges/possible-path
I realized that both x and y must be a sum of some multiple of a and b...
So x%(a+b) OR x%(a-b) should be divisible by either a or b
and similarly for y...
But the following does not work ...
long long int xb,yb,xa,ya;
xb = x % b;
xa = x % a;
yb = y % b;
ya = y % a;
// for x
bool cxbaplusb = a+b==0 ? xb == 0: (xb%(a+b))==0;
bool cxbaminb = a-b==0 ? xb == 0: (xb%(a-b))==0;
// for y
bool cybaplusb = a+b==0 ? yb == 0: (yb%(a+b))==0;
bool cybaminb = a-b==0 ? yb == 0: (yb%(a-b))==0;
// for x
bool cxaaplusb = a+b==0 ? xa == 0: (xa%(a+b))==0;
bool cxaaminb = a-b==0 ? xa == 0: (xa%(a-b))==0;
// for y
bool cyaaplusb = a+b==0 ? ya == 0: (ya%(a+b))==0;
bool cyaaminb = a-b==0 ? ya == 0: (ya%(a-b))==0;
if ( (cxbaplusb || cxbaminb || cxaaplusb || cxaaminb) && (cybaplusb || cybaminb || cyaaplusb || cyaaminb) )
std::cout << "YES" << std::endl;
else
std::cout << "NO" << std::endl;
But this is not working ... Am I missing any conditions ? Any suggestions ??
The following mathematical explanation may help you achieve your goal.
Source: https://hr-filepicker.s3.amazonaws.com/infinitum-jun14/editorials/2372-possible-path.pdf
Please check the input size
1 ≤ a,b,x,y ≤ 10^18
https://www.hackerrank.com/challenges/possible-path
CPP won't support this much size, it will throw garbage value resulting in wrong answer
def gcd(a, b):
if(b == 0):
return a
return gcd(b, a%b)
t=input()
for i in range(t):
a = map(int, raw_input().split())
if(gcd(a[0],a[1]) == gcd(a[2],a[3])):
print "YES"
else:
print "NO"
#include<iostream>
using namespace std;
int gcd(int a, int b){
return b ? gcd(b, a%b) : a;
}
int main(){
int t;
cin >> t;
while (t--){
int a, b, x, y;
cin >> a >> b >> x >> y;
if (gcd(abs(a), abs(b)) == gcd(abs(x), abs(y)))
cout << "YES" << endl;
else
cout << "NO" << endl;
}
return 0;
}
Initially I had same doubt as you , but its not " x and y must be a sum of some multiple of a and b " because we can move from (a,b) to any point in (a+b,b), (a-b,b),(a,b+a),(a,b-a) in case if you move (a+b,b) now a=a+b,b=b so for this value of a,b ( not the given one here a is updated to a+b) only you can do the above operation so its not that x and y always must be sum of some multiple of a,b . so that you have to go with gcd method
public class Solution {
public static void main(String[] args) {
int a,b,x,y;
if(gcd(x,y)=gcd(a,b))
System.out.println("Adam can reach")
else
System.out.println("Adam cannot reach")
}
}