I have a 3*3 matrix, and want to translate each column.
#include <Eigen/geometry>
using namespace Eigen;
int main()
{
Translation3d tr(1,2,3);
Matrix3d m; m<<1,2,3,4,5,6,7,8,9;
// m = tr * m; //will not work
}
Don't have a clue from the Eigen manual...
The manual says Translation is not to be used directly.
For transforms, you will likely want Affine3d, but note that your matrix m is not a homogeneus matrix, so you either change it to be 4x4 or use another Affine3d object.
Related
std::vector<int> v(6);
how to declare a two dimensional vector with limits like above code for one dimension
I'm a noob in c++. I tried like this:
`
std::vector<int> v(6)(2);
`
I expected a two dimensional vector with 6 rows and 2 columns to take input in.
I know how to declare 2d vector. I just wanted it with limit.
In C++, there's no direct type which exactly represents a 2D "vector"/"matrix"/"tensor".
What you can create, is a vector of vectors. You'd write that as std::vector<std::vector<int>> - each element of the outer vector is itself a vector. But there's an important difference here: each of the inner vectors has its own length, and those could be different.
vector has a constructor taking a second argument, the initial value. You can use that here to initialize the inner vectors:
std::vector<std::vector<int>> v(6, std::vector<int>(2));
I'm working on a problem out of Cracking The Coding Interview that asks: Given a 2-D graph with points on it, find a line which passes the most number of points.
The solution is to: Draw an infinite line between every two points and, using a hash table, track which line is most common. To find the most common line, we iterate through all line segments using a hash table to count the number of times we've seen each line.
The author goes on to say there's a complication: "we're definining two lines to be equal if the lines have the same slope and y-intercept. We are then, furthermore, hashing the lines based on these values (specifically based on the slope). The problem with floating point numbers cannot always be represented accurately in binary. We resolve this by checking if two floating point numbers are within an epsilon value of each other."
Here's where I'm confused. Even if the slope is a floating point, we can't use that as a hash key? If so, why not just hash the slope as a string instead? Why do we need to introduce into our code hashing based upon keys that are within epsilon of each other?
Have a look at the following example written in c++.
#include <stdio.h>
#include <stdlib.h>
int main() {
double a=10.0;
double b=a/3;
double c=(b-3)*3;
printf("a: %20.50lf\n", a);
printf("b: %20.50lf\n", b);
printf("c: %20.50lf\n", c);
return 0;
}
'c' should be equal to 1 but due to floating point rounding the above code produces the following.
a: 10.00000000000000000000000000000000000000000000000000
b: 3.33333333333333348136306995002087205648422241210938
c: 1.00000000000000044408920985006261616945266723632812
The algorithm you are describing does not need any hash table.
Use histogram instead. This Answer is exact example of this task in C++
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line
If you still want to use floats as keys
Then you need to truncate them so they can be compared as binary. For example let assume you got (assuming C++ syntax):
const float da=1.5*M_PI/180.0; // [rad] comparison precision
float a;
a=atan2(dy,dx); // [rad] your line angle from deltas
a=floor(a/da); // [da] truncated angle
Where dx,dy is your line delta and da is your comparison precision angle. Now to access float a as binary for hashing purposes you can simply do this:
union { float f32; DWORD u32; } b;
b.f32=a;
// here b.u32 is your hash key
I'm only new to processing and it's confusing me far more than Java or Javascript ever did!
I have to solve simultaneous equations for a college assignment. (It's a class where they don't really explain to us what they're doing in the code.)
I know how they figure out the code with two equations in the code below, however they now want us to do it with 3 equations. Does anyone know how I would do this? I imagined I would just have to add the extra bits into each matrix but it is obviously more complicated than that.
The 3 equations I have are:
x+y+z=9
x+2y+3z=23
x+5y-3z=-7
The code for two equations is the following:
// import Jama.*;
// Solve 3x+2y=3
// -2x-y=-1
// AX=B
// X=InvA B
import java.io.StringWriter;
void setup()
{
size(150,110);
fill(0,0,0);
double [][] Aline12={{ 3, 2}, // Create a 2D array to store A
{-2,-1}};
Matrix A = new Matrix(Aline12); // Copy array to A Matrix data structure
double [][] B1ine12 = {{3}, // Create a 2D array to store B
{-1}};
Matrix B = new Matrix(B1ine12); // Copy array to B Matrix data structure
Matrix X=(A.inverse()).times(B); // Solve for X
text("A",10,12);
app_print(A,0,16);
text("B",110,12);
app_print(B,100,16);
text("X",10,65);
app_print(X,0,70);
}
// Method added to allow printing on applet screen at (x,y)
void app_print(Matrix P, int x, int y)
{
StringWriter stringWriter = new StringWriter();
PrintWriter writer = new PrintWriter(stringWriter);
P.print(writer,5,2);
text(stringWriter.toString(),x,y);
}
You will solve it the same way you solve a system of 2 equations, just add the third variable. Also in practice you almost never want to take the inverse of a matrix, there are better methods like LU decomposition to solve Ax=B. Since you are using Jama, you can try the following snippet
double [][] Aline12={{1.0, 1.0, 1.0}, // Create a 2D array to store A
{1.0, 2.0. 3.0},
{1.0, 5.0, -3.0}};
Matrix A = new Matrix(Aline12); // Copy array to A Matrix data structure
double [][] B1ine12 = {{9}, // Create a 2D array to store B
{23},
{-7}};
Matrix B = new Matrix(B1ine12); // Copy array to B Matrix data structure
Matrix X = A.solve(B) // Solve for X. See Jama documentation on solve
Jama docs
http://math.nist.gov/javanumerics/jama/doc/
Given a 2D matrix that consists of numbers 0-9, how do I find the largest square that is constructed from a single number?
For example,
12039487067
81111012389
01111111769
71181231987
11111891167
86171231222
17130471282
37111111222
47061902547
There are 3 squares:
starts at (1, 1) and ends at (4, 4)
starts at (2, 2) and ends at (7, 7)
starts at (5, 8) and ends at (7, 10)
The biggest square is the second one. And how do I do it if I have to find a rectangle?
I know the logic for finding solid square, but not when its hollow. Any ideas?
I'd go with something like: For every position in the array call a recursive subrutine that matches (X'+1, Y'') and (X'',Y'+1) with the first value. If some of those dont match start recursion with (X', Y''+1) and (X''+1,Y') until they meet again on the same spot where you can say you've got an square…
without backtracking that would also deal with the "weird" case in your example of (2,2)->(4,4)
It may help to first scan the matrix to compute auxiliary matrices left,up that measure the number of consecutive entries in each direction.
So, for example, left[2,5] would be equal to 3 because there are 3 consecutive elements to the left of element 2,5 with the same value.
For a matrix of size n*n these matrices can be computed in O(n^2) using straightforward dynamic programming.
Once you have these matrices you can check in O(1) time whether any particular candidate rectangle is valid by checking the corners.
This then gives an immediate O(n^3) algorithm for finding the best square (simply test each of the O(n^3) possible square locations), an an O(n^4) method for rectangles.
You can also get an O(n^3) method for finding the best rectangle
by fixing the top and bottom coordinates of the rectangle (O(n^2) choices) and then iterating across the matrix. You then need to consider at each x coordinate the following cases:
Can we continue the previous rectangle (possible if the colour at the top and bottom matches the current colour)
Can we complete the previous rectangle (possible if there is a connection from top to bottom with the current colour)
Otherwise, can we start a new rectangle (possible if there is a connection from top to bottom with a new colour)
All of these take O(1) by using the top matrix, and there are O(n) x coordinates, so altogether this takes O(n^3) to find the best rectangle outline.
This is simple solution for finding max square.
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include<cstring>
#include <math.h>
#include<cstdio>
#include<string>
#include <queue>
#include <list>
using namespace std;
bool flag = false;
char ch[100][100];
int findMxSq(int leftX, int upY, int rightX, int downY){
int mx = (rightX-leftX+1)*(downY-upY+1);
bool flag = true;
char c = ch[leftX][upY];
for(int i=leftX; i<=rightX; i++){
flag &= (c==ch[i][downY+1]);
}
if(flag) mx = max(mx, findMxSq(leftX,upY,rightX,downY+1));
flag = true;
for(int i=upY; i<=downY; i++){
flag &= (c==ch[rightX+1][i]);
}
if(flag) mx = max(mx, findMxSq(leftX,upY,rightX+1,downY));
return mx;
}
int main(){
memset(ch,'*',sizeof(ch));
for(int i=1; i<=9; i++)
for(int j=1; j<=11; j++)
cin>>ch[i][j];
int ans = 0;
for(int i=1; i<=9; i++)
for(int j=1; j<=11; j++)
ans = max(ans,findMxSq(i,j,i,j));
cout<<ans<<endl;
return 0;
}
I have set P of point (3D) which are vertices of convex hull (every one). I looking for method for checking if given point p0 is NOT outside of this convex hull.
I'll have to repeat the checking it several times (for different p0). So if is possible to reuse part of computation it would be great.
On stackoverflow pages i found this:
Find if a point is inside a convex hull for a set of points without computing the hull itself
There are 2 aproche:
First based on convex hull property - set of linear equations.
Secound based on observation: "The point lies outside of the convex hull of the other points if and only if the direction of all the vectors from it to those other points are on less than one half of a circle/sphere/hypersphere around it."
Unfortunately I don't know how exactly can do it.
First give me insoluble system of equations - 3 equation with n unknown (n>3). How can I sovle it? Did I do some mistake?
In second approach I don't know how check this assumption.
CGAL can easily do this test. Not only you could build the convex hull with CGAL, but you can quickly and easily determine whether a specific point is inside, on the surface or outside of the polyhedron.
A code snippet is shown below:
#include <CGAL/Simple_cartesian.h>
#include <CGAL/AABB_tree.h>
#include <CGAL/AABB_traits.h>
#include <CGAL/Polyhedron_3.h>
#include <CGAL/boost/graph/graph_traits_Polyhedron_3.h>
#include <CGAL/AABB_face_graph_triangle_primitive.h>
#include <CGAL/algorithm.h>
#include <CGAL/Side_of_triangle_mesh.h>
typedef CGAL::Simple_cartesian<double> K;
typedef K::Point_3 Point;
typedef CGAL::Polyhedron_3<K> Polyhedron;
typedef CGAL::AABB_face_graph_triangle_primitive<Polyhedron> Primitive;
typedef CGAL::AABB_traits<K, Primitive> Traits;
typedef CGAL::AABB_tree<Traits> Tree;
typedef CGAL::Side_of_triangle_mesh<Polyhedron, K> Point_inside;
bool pointInside(Polyhedron &polyhedron, Point &query) {
// Construct AABB tree with a KdTree
Tree tree(faces(polyhedron).first, faces(polyhedron).second, polyhedron);
tree.accelerate_distance_queries();
// Initialize the point-in-polyhedron tester
Point_inside inside_tester(tree);
// Determine the side and return true if inside!
return inside_tester(query) == CGAL::ON_BOUNDED_SIDE;
}
Assuming P is large and there are many p0, you should compute a 3D triangulation in which to do point location. This is a CGAL demo.
You can write down the points in the hull as columns in a matrix and then have a vector which tells you what combination of points to take:
(X1 X2) (a) (X1a + X2b)
(Y1 Y2) (b) = (Y1a + Y2b)
(Z1 Z2) (Z1a + Z2b)
To generate your target point you want to find a vector that solves this, subject to the constraints that the elements of the vector are between 0 and 1, and that the elements of the vector add up to 1. You can solve this sort of problem - if there is a solution - via linear programming, which might involve using the http://en.wikipedia.org/wiki/Simplex_algorithm.
There are a variety of tricks to get this into the strict form. Adding another row to the matrix will allow you to say a + b = 1. To force b <= 1 you could have b + q = 1 and q >= 0, although as pointed out by Ted Hopp below, in this case b <= 1 because a + b = 1, and both a and b are >= 0.