I'm trying to figure out if this is a bug in Eigen or something I'm doing wrong. I simply want the dot product of two complex vectors [ 1 , i] and [1 , -i]. The answer is 1*1 + i*(-i) = 2. But Eigen gives zero as answer. See code below:
#include <iostream>
#include <sstream>
#include <vector>
#include <fstream>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <map>
#include <sys/sysinfo.h>
#include <algorithm>
#include <Eigen/Dense>
using namespace Eigen;
using namespace std;
int main( void ){
VectorXcd xd0(2);
complex<double> c_i( 0.0 , 1.0 );
xd0[0] = 1.0 ;
xd0[1] = c_i;
VectorXcd xd0conj = xd0.conjugate();
cout <<" vec is \n" << xd0 <<endl;
cout <<" conj vec is \n" << xd0conj <<endl;
cout << "eigen dot = " << (xd0conj).dot(xd0 ) <<endl;
complex<double> outVal = 0.0;
for( int a=0; a<xd0.size(); a++){
outVal += xd0[a]*xd0conj[a];
}
cout << "naive dot = " << outVal<<endl;
};
From the documentation:
"
If the scalar type is complex numbers, then this function returns the hermitian (sesquilinear) dot product, conjugate-linear in the first variable and linear in the second variable.
"
This product is often used, for example in quantum mechanics.
It computes conj(x)*y which is what you are seeing. I guess you want the simple / ordinary dot:
cout << "eigen dot = " << xd0conj.transpose()*xd0 <<endl;
This outputs
eigen dot = (2,0)
Related
I am trying to create a sine, cosine, tangent, and cotangent table. I want to printf/cout an "INF" instead of huge complicated numbers or interesting symbols when I calculate the cotangent of 0. But it doesn't allow me to do this. I tried every way I can think of, but I couldn't do it.
Can you help me about that?
Code is below:
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <iostream>
#define PI 3.14159265
#include <math.h>
#include <string>
#include <sstream>
using namespace std;
int main()
{
setlocale(LC_ALL, "Turkish");
string diziBaslik[1][5] = {{"AÇI","SİN","COS","TAN","COTAN"}};
string diziBaslikCizgi[1][5] = {{"------","------","------","------","------"}};
float dizi[10][5] = {
{0 ,sin(0*PI/180) ,cos(0*PI/180) ,tan(0*PI/180) ,1/tan(0*PI/180) },
{10,sin(10*PI/180),cos(10*PI/180),tan(10*PI/180),1/tan(10*PI/180)},
{20,sin(20*PI/180),cos(20*PI/180),tan(20*PI/180),1/tan(20*PI/180)},
{30,sin(30*PI/180),cos(30*PI/180),tan(30*PI/180),1/tan(30*PI/180)},
{40,sin(40*PI/180),cos(40*PI/180),tan(40*PI/180),1/tan(40*PI/180)},
{50,sin(50*PI/180),cos(50*PI/180),tan(50*PI/180),1/tan(50*PI/180)},
{60,sin(60*PI/180),cos(60*PI/180),tan(60*PI/180),1/tan(60*PI/180)},
{70,sin(70*PI/180),cos(70*PI/180),tan(70*PI/180),1/tan(70*PI/180)},
{80,sin(80*PI/180),cos(80*PI/180),tan(80*PI/180),1/tan(80*PI/180)},
{90,sin(90*PI/180),cos(90*PI/180),tan(90*PI/180),1/tan(90*PI/180)}
};
cout << diziBaslik[0][0] << "\t";
for(int j=1;j<5;j++){
cout << diziBaslik[0][j] << "\t\t";
}
cout<< endl;
cout << diziBaslikCizgi[0][0] << "\t";
for(int j=1;j<5;j++){
cout << diziBaslikCizgi[0][j] << "\t\t";
}
cout << endl;
for(int i=0;i<10;i++){
for(int j=0;j<5;j++){
if(j==0){
cout << dizi[i][j] << "\xB0\t";
}
else{
printf("%.6f\t", dizi[i][j]);
}
}
cout<<endl;
}
}
There are 2 issues:
Due to low PI precision, tan(90*PI/180) returns a large number and not infinity.
Your system is printing infinity as 1,#INF00. I don't think there's a way to change that.
To work around both issues don't let large values go to printf and instead print your own string if the value is larger than some tolerance value:
if (dizi[i][j] < 1e5) {
printf("%.6f\t", dizi[i][j]);
} else {
printf("INFINITY\t");
}
Also note that setlocale() won't affect cout because it's constructed before. For cout you need to add something like
cout.imbue(std::locale("Turkish"));
I have a code which will convert the float value to string, i have written like below
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main() {
float myFloat= 10.80;
std::ostringstream ss;
ss << myFloat;
cout<<"value = " << ss.str();
std::string s(ss.str());
cout<<"value = " << s;
return 0;
}
But the problem is when my value is 10.66 its coming 10.66 but when its 10.80 its coming like 10.8 or when its 10.00 its coming 10 only .
How can i print the complete value
Try this code .
Use the setprecision function with '2' .
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
using namespace std;
int main() {
float myFloat= 10.80;
stringstream stream;
stream << fixed << setprecision(2) << myFloat;
string s = stream.str();
cout<<"value = " << s;
return 0;
}
The trailing zeros are only kept if you set either fixed or scientific mode.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double x = 4.2;
cout << fixed << setprecision(2);
cout << x << endl;
return 0;
}
It seems you want something like below.
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
using namespace std;
int main() {
float myFloat= 10.80;
std::ostringstream ss;
ss << fixed << setprecision(2) << myFloat;
cout<<"value = " << ss.str();
std::string s(ss.str());
cout<<"value = " << s;
}
Probably the least complicated way would be to use printf instead of std::cout. There you can specifically specify how many digits are to be displayed.
#include "stdio.h"
printf("%3.2f",myfloat);
where 3 is the # of digits before and 2 the # of digits after the dot, either can be left out. Append '\n' to the string if you want a new line.
EDIT: Ok, I did not know about setprecision(2).
I'm using viennacl to solve a linear system of equations (AX = B) with the graphic card. Also, the code uses armadillo.
My system of equations have complex numbers. So the question is: Can I solve a system of equations (with complex numbers) using viennacl?
Above is an example of a working code with real numbers.
// System headers
#include <iostream>
// Armadillo headers (disable BLAS and LAPACK to avoid linking issues)
#define ARMA_DONT_USE_BLAS
#define ARMA_DONT_USE_LAPACK
#include <armadillo>
#include <complex>
#define VIENNACL_WITH_ARMADILLO 1
// ViennaCL headers
#include "viennacl/linalg/cg.hpp"
#include "viennacl/linalg/bicgstab.hpp"
#include "viennacl/linalg/gmres.hpp"
#include "viennacl/io/matrix_market.hpp"
#include "vector-io.hpp"
//using namespace arma;
using namespace viennacl::linalg;
using namespace std;
typedef arma::mat armat;
typedef arma::vec arvec;
typedef complex<double> dcmplx;
int main(void)
{
int N = 500;
armat A(N,N);
A.randu();
arvec B(N);
B.randu();
arvec X(N);
arvec residual(N);
viennacl::matrix<double> vcl_A(N, N);
viennacl::vector<double> vcl_B(N);
viennacl::vector<double> vcl_X(N);
viennacl::vector<double> vcl_result(N);
viennacl::copy(A, vcl_A);
viennacl::copy(B, vcl_B);
viennacl::copy(X, vcl_X);
std::cout << "----- Running GMRES -----" << std::endl;
vcl_X = viennacl::linalg::solve(vcl_A, vcl_B, viennacl::linalg::gmres_tag());
viennacl::copy(vcl_A, A);
viennacl::copy(vcl_B, B);
viennacl::copy(vcl_X, X);
residual = A * X - B;
cout << "Relative residual: " << norm(residual) / norm(B) << endl;
}
Complex version of the code:
#include <iostream>
// Armadillo headers (disable BLAS and LAPACK to avoid linking issues)
#define ARMA_DONT_USE_BLAS
#define ARMA_DONT_USE_LAPACK
#include <armadillo>
#include <complex>
#define VIENNACL_WITH_ARMADILLO 1
// ViennaCL headers
#include "viennacl/linalg/cg.hpp"
#include "viennacl/linalg/bicgstab.hpp"
#include "viennacl/linalg/gmres.hpp"
#include "viennacl/io/matrix_market.hpp"
#include "vector-io.hpp"
//using namespace arma;
using namespace viennacl::linalg;
using namespace std;
typedef arma::cx_mat armat;
typedef arma::cx_vec arvec;
typedef complex<double> dcmplx;
int main(void)
{
int N = 500;
armat A(N,N);
A.randu();
arvec B(N);
B.randu();
arvec X(N);
arvec residual(N);
viennacl::matrix<dcmplx> vcl_A(N, N);
viennacl::vector<dcmplx> vcl_B(N);
viennacl::vector<dcmplx> vcl_X(N);
viennacl::vector<dcmplx> vcl_result(N);
viennacl::copy(A, vcl_A);
viennacl::copy(B, vcl_B);
viennacl::copy(X, vcl_X);
std::cout << "----- Running GMRES -----" << std::endl;
vcl_X = viennacl::linalg::solve(vcl_A, vcl_B, viennacl::linalg::gmres_tag());
viennacl::copy(vcl_A, A);
viennacl::copy(vcl_B, B);
viennacl::copy(vcl_X, X);
residual = A * X - B;
cout << "Relative residual: " << norm(residual) / norm(B) << endl;
std::cout << "----- Running BiCGStab -----" << std::endl;
vcl_X = viennacl::linalg::solve(vcl_A, vcl_B, viennacl::linalg::bicgstab_tag());
viennacl::copy(vcl_A, A);
viennacl::copy(vcl_B, B);
viennacl::copy(vcl_X, X);
residual = A * X - B;
cout << "Relative residual: " << norm(residual) / norm(B) << endl;
std::cout << "----- Running CG -----" << std::endl;
vcl_X = viennacl::linalg::solve(vcl_A, vcl_B, viennacl::linalg::cg_tag());
viennacl::copy(vcl_A, A);
viennacl::copy(vcl_B, B);
viennacl::copy(vcl_X, X);
residual = A * X - B;
cout << "Relative residual: " << norm(residual) / norm(B) << endl;
}
ViennaCL currently does not support complex numbers. The primary technical reason is that OpenCL does not natively provide support for complex numbers. While emulating complex arithmetic via real arithmetic is certainly possible, we were reluctant to go down this path and (wrongly?) hoped that a standardization for complex will come soon.
I am trying to implement a cube root after Herons method.
Square root is working fine , but I am strugling with cubes root.
My formula for cubes root is: x[i+1]= (3*x[i] + N/x[i]*x[i])/4
#include <cstdlib>
#include <iostream>
#include <vector>
#include <sstream>
using namespace std;
double Heron(int Nummer)
{
double x = Nummer/2;
for(int NumAppr = 0;NumAppr<100;NumAppr++)
{
x = (3*x+Nummer/x*x)/4;
}
//double y = x/3;
cout << "root is: " << x << endl;
return x;
}
int main()
{
Heron(27);
system("PAUSE");
return EXIT_SUCCESS;
}
Hmmm. You're missing parentheses around x*x:
x = (3*x+Nummer/(x*x))/4;
To get the cube root, simply call std::cbrt:
#include <cmath>
#include <iostream>
int main()
{
std::cout << "Root is: " << std::cbrt(27) << std::endl;
}
// Output: "Root is: 3"
(live demo)
I have a problem using atof,
here is the code:
#include <stdio.h>
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
int main(){
std::string num ("1.0");
//std::string num ("1.1");
cout<< atof(num.c_str());
return 0;
}
If the num string is "1.1" , it can correctly cout 1.1. But if I want to keep the zero when the num string is "1.0" (want it to be 1.0 but not 1), what should I do?
You need to use std::fixed and std::setprecision, like so:
std::cout<< std::fixed << std::setprecision(1) << atof(num.c_str());
This will require that you include the iomanip header.
A possible solution is
#include <cstdio>
#include <iostream>
#include <string>
#include <iomanip>
int main() {
std::cout.precision(3);
std::cout.setf(std::ios::fixed);
std::string s("1.0");
float f = 0.0f;
sscanf(s.c_str(), "%f", &f);
// alternative way of setting this flags
// std::cout << std::fixed << std::setprecision(3) << f << "\n";
std::cout << f << "\n";
return (0);
}
notice that there are at least 2 ways of accomplishing the same format for the output, I left one of them commented out .