Non-trival solution for AX = 0 using eigen c++ [closed] - c++

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
How can I get non-trival solution of M*x=0 using Eigen linear algebra library.
I tried this but solution is all zeros.
Matrix<float,2,3> m ;
Matrix<float,2,1> y ;
m << 2 , 3 ,5 , -4 , 2, 3;
y(0,0) = 0;
y(1,0) = 0;
cout << "Here is matrix m:" << endl << m << endl;
cout << "Here is matrix y:" << endl << y << endl;
cout<<"solution: \n"<<m.fullPivLu().solve(y);

You are trying to solve M * x = 0, so it makes sense to return x=0. If you want to avoid this trivial solution, then you have to add additional constraints. For instance you might say that you want to minimize |M * x|^2 subject to |x|=1 in which case you end up with an eigenvalue problem (through Lagrange multipliers). Your solution is the eigenvector corresponding to the minimal eigenvalue. Using Eigen:
Matrix3f A = m.adjoint() * m;
Vector3f x = SelfAdjointEigenSolver<Matrix3f>(A).eigenvectors().col(0);
Here I get:
x = 0.032739 0.851202 -0.523816
and m * x is in the order of 1e-16.

You want to find a nonzero element in the kernel of your matrix (the kernel is the set of x such that Mx = 0), look in Eigen for a decompositon offering a kernel() method, for instance fullPivLu does:
http://eigen.tuxfamily.org/dox/classEigen_1_1FullPivLU.html#a6e8f1d2fcbd86d3dc5a8a013b6e7200a

Related

Understanding the Syntax of Gps_traits_2::Polygons_with_holes_2 in CGAL and Additional Related Questions

Good day,
I am currently in the learning process of CGAL while being relatively new to C++. For my current project I need to use Minkowski sums and then do additional operations on the boundary of it.
However, before I do these additional operations I need to get a better understanding of the output of offset_polygon_2(), the exact Minkowski offset computation.
Question 1: What is the Syntax of the output for .outer_boundary?
From what I understand so far, it outputs a list of a conic circles defined here. I would also imagine you would need some kind of arc-angle range for each of these concic circles and origin point, correct? An example of the output goes something like this:
89 {-1*x^2 + -1*y^2 + 0*xy + 1400*x + 0*y + -489975} : (705,0) --ccw--> (700,5) {0*x^2 + 0*y^2 + 0*xy + 0*x + 0*y + 0} : (700,5) --l--> (699.97,5)...
Question 2: How do you use CGAL::draw() for the above?
I have the following code, but I am unsure of what else needs to be done before it can be drawn.
Offset_polygon_with_holes_2 offset = CGAL::offset_polygon_2(P, 5, traits);
double secs = timer.time();
std::cout << "The offset polygon has " << offset.outer_boundary().size()
<< " vertices, " << offset.number_of_holes() << " holes."
<< std::endl;
std::cout << "Offset computation took " << secs << " seconds." << std::endl;
Question 3: What other operations can be done on the "offset"?
So in the example code for Minkowski sums (also see above) offset.outer_boundary() is done, is there a list of other operations that can be done? Note: I do not think "operations" is the correct term here, please correct me.
I think that is all I have for now, thanks!

Unexpected result of C++ program [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I have created a C++ which compiles successfully, but when I run it I get c=0.
Can anybody explain why?
int main()
{
double U0, U, C, A, B, D;
U = 0.2;
A = U/U0;
B = 1+1/(16*pow(A, 2));
D = pow(B, 2)-(1/4)*A;
for (U0=0.2; U0<=1; U0=U0+0.2)
{
if (U <= (4*U0))
{
C= (1/2)*(B+sqrt(D));
cout <<" | U0 | "<< U0 <<" | U | "<< U <<" | C^2 | "<< C << endl;
U = U + 0.2;
}
}
return 0;
}
Because of these kind of statements:
C= (1/2)*(B+sqrt(D));
C++ interprets 1/2 as an integer operation (not a floating one), hence 1/2 = 0 (for integers)
This is an error everybody has done once in his life!
After you will always write something like 1/2. with the dot to force a division using the double type.

Operation on vector [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I want my program to create and output 4 sets, each having 13 numeric elements.
Expected output is 1.1, 1.2, ..., 1.13, 2.13, ..., 4.13 (set and element is represented as set.element):
Set Element
1 1
1 2
...
1 13
2 1
2 2
...
2 13
...
4 13
I also want to store this data in a std::vector so that I can access and reuse it by using functions at or operator[].
My current output is 0. I want to display the output at a particular index, say output at index 30.
Code:
vector<int> storein(52);
int sortn;
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 13; j++)
{
storein.push_back(j);
cout << i + 1 << "\t" << j << endl;
}
}
// cout << storein.size();
cout << storein[30] << endl;
Live example: http://ideone.com/XcGAyX
vector<int> storein(52);
The vector now has 52 elements.
The calls to push_back add more elements to the end of the vector. When you refer to storein[30], you find one of the original 52 elements.
Try this:
vector<int> storein;
In general, when you start to use a new tool, you should try the simplest things you can do, test the results, and build up to more complex operations. This is a vital skill.

How to calculate the exponent/power value (how much of the power) i.e. 'n' in c++? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
How can we calculate the value/degree of power exponent of a certain number?
I mean if it's like a^n = b, then how can we calculate n?
For example assume that a = 2 and b = 8, then how can we calculate that n = 3? Is there any special function?
Use std::log. Example from the reference page:
#include <cmath>
#include <iostream>
int main()
{
double base = 2.0;
double arg = 8.0;
double result = std::log(arg) / std::log(base);
std::cout << result << '\n'; // prints 3
}
More to learn at wikipedia.
What you are looking for is the logarithm of b to the base a (at least we call it that in german).
C++ example:
#include <cmath> /* log */
int main ()
{
int a = 2;
int b = 8;
float n = log(b) / log(a); // 3
}
Well, you can use the same logarithmic functions here too. Include cmath.
In-code
.
.
cout << log(8) / log(3) << endl;
.
.
Output
.
.
.
...3...
.
.
.

Using a counter-controlled while loop [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
The program should be print the numbers 0 through 10, along with their values doubled and tripled. The data file contains the necessary variable declarations and output statements.
Example the output:
single 1 double 2 triple 3
single 2 double 4 triple 6
single 3 double 6 triple 9
single 4 double 8 triple 12
here my code tell me if correct
#include <iostream>
#include <cstdlib>
using namespace std;
int main ()
{
int x, double, triple;
int x = 0
while (x<=10)
{
cout << "x" << double = (x*2) << triple = (x*3) << endl;
x++;
}
return EXIT_SUCCESS
I'll attempt to put a guiding answer so I'm not going to give you a straight code that you can just copy and paste into your homework, but if you read and follow it, it should be the answer. (And next time, do go find your lecturer, or your tutor).
Some issues:
You are not printing the "Single" and "double" and "Triple" text which you should (based on your expected answer) So add that in.
You did your calculation to get the number for double, and triple - good. But again, you did not print them out.
Also C++ allows you to stack multiple cout all on one line, so for example:
cout << "My name is " << myname << endl;
Will print out:
My name is (content of variable myname)
And then print an end of line (endl). You can use that same technique to do part of your assignment to print out the results to meet the expected output.
Credit to Synetech
you miss a lot of code in there. You didn't print nothing what you want
try again with this inside while loop:
cout << “single “ << x << double << x*x
<<“ triple “<< x*x*x << endl;
x++;