Can't access elements of an eigen linear system solution - c++

I'm just started of using eigen but for some strange reason I'm struggling with something that should be simple. The code below is a simplified version of some similar computation I would like to perform (Solve x in Ax = b).
Input:
auto N = 10;
auto A = Matrix<Float, Dynamic, Dynamic>::Identity(N, N);
auto b = Matrix<Float, Dynamic, 1>::Constant(N, 1, 1);
std::cout << "A: " << std::endl
<< A << std::endl
<< "b: " << std::endl
<< b << std::endl;
auto x = A.fullPivLu().solve(b);
std::cout << "x(" << x.rows() << ", " << x.cols()
<< "): " << std::endl << x << std::endl;
Output:
A:
1 0 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0 0 1
b:
1
1
1
1
1
1
1
1
1
1
x(10, 1):
mouse: /home/jansen/devel/build/external/eigen/include/eigen3/Eigen/src/Core/Block.h:119: Eigen::Block<Eigen::Matrix<double, -1, -1, 0, -1, -1>, 1, -1, false>::Block(XprType &, Index) [XprType = Eigen::Matrix<double, -1, -1, 0, -1, -1>, BlockRows = 1, BlockCols = -1, InnerPanel = false]: Assertion `(i>=0) && ( ((BlockRows==1) && (BlockCols==XprType::ColsAtCompileTime) && i<xpr.rows()) ||((BlockRows==XprType::RowsAtCompileTime) && (BlockCols==1) && i<xpr.cols()))' failed.
[1] 21192 abort (core dumped) ./src/mouse
A and b is well formed and the solution x even have the right dimensions but whenever I try to access an element of x I get an assertion failure. From the assertion I deduce that some sort of out of bounds error happens but I can't figure out why?

Please don't abuse of auto with expression template libraries, see this page. Typically, in your case, x is not a Matrix<> object but an abstract object saying that A\b as to be computed... The solution is thus:
Matrix<Float, Dynamic, 1> x = A.fullPivLu().solve(b);

Related

How to compute right kernel of a matrix with Eigen library?

I've started to implementation of an algorithm with Eigen library. I needed to calculate null space(kernel) of a matrix. I have tried with a cube's matrix that,
0, 0, 1,
0, 1, 0,
1, 0, 0,
-1, 0, 0,
0, 0, -1,
0, -1, 0
Then, I call, its source
A.transposeInPlace();
std::cout << "and after being transposed:\n" << A << std::endl;
FullPivLU<MatrixXf> lu(A);
MatrixXf A_null_space = lu.kernel();
std::cout << "Null space:\n" << A_null_space << std::endl;
A_null_space.transposeInPlace();
std::cout << "Null space Transposed_A:\n" << A_null_space;
I obtain,
0.5 0 -1 1 0 0 0 0 0 0.5
-0.5 0 -0 0 1 0 0 0 0 -0.5
0.5 0 -0 0 0 1 0 0 0 -0.5
0.5 0 -0 0 0 0 1 0 0 0.5
-1 0 1 0 0 0 0 1 0 -1
-0.5 0 1 0 0 0 0 0 1 -0.5
-0.5 1 -0 0 0 0 0 0 0 0.5
But, I realized later on that its right kernel and left kernel is same and seemingly the code snippet calculates left kernel. The code is getting crazy output on the other test case. So, how can be the right kernel be calculated? The link is also to show the difference btw right and left kernels with examples. However, if I remove first line, the output is 0 0 0
Clearly problem of the case is,
MatrixXf A{10, 3};
A <<
1, 0, 1 ,
1, 0, 0 ,
0, 1, 1 ,
0, 1, 0 ,
0, 0, 1 ,
-1, 0, 0 ,
0, 0, -1 ,
0, -1, 1 ,
0, -1, 0 ,
-1, 0, 1;
Its output is expected as,
1 0 0 0 0 0 0 -2 2 1
0 1 0 0 0 0 0 -1 1 1
0 0 1 0 0 0 0 -1 2 0
0 0 0 1 0 0 0 0 1 0
0 0 0 0 1 0 0 -1 1 0
0 0 0 0 0 1 0 1 -1 -1
0 0 0 0 0 0 1 1 -1 0
QR factorization,
HouseholderQR<MatrixXf> qr(A);
cout << "\nQR matrix to compare \n" << qr.matrixQR().transpose();
Then I get,
-1.41421 0 0.414214
-0.707107 -0.707107 -1
-0.707107 0.707107 1
0 0 1
-0.707107 0.707107 0
0.707107 0.707107 0
0.707107 -0.707107 0
-0.707107 0.707107 -1
0 0 -1
1.19209e-07 1.41421 5.96046e-08
#Edit 2, Does Eigen calculate wrongly?
Source
#Edit 3,
I'm really but really confused because both matrix seem right! How come?
As you observed, both matrices are valid right kernels. This is because they correspond to two different basis of the same subspace. To check it, you can reduce the two matrices to the reduced row echelon form (rref function in matlab, or see this online calculator). This transformation is unique and does not change the span defined by the matrix. Your reference kernel basis is already in this form. So all you have to do is to reduce the one returned by Eigen and see that it gives you the same matrix as your reference one.

Recursive problems in 2D array C++

I have a problem that from a certain number 1 in a 2D matrix with (x, y) coordinates. From this number, it will start spreading out its 4-neighbor which their values will be assigned by (start point + 1)
We start from a coordinate of (3, 3) = 1. Its neighbor's value will be 2. Next step, 4 neighbors of the point having value of 2 will be 3. And so on, until, all 1 numbers in the matrix are infected!
I have resolved this problem by using some loops. However, I'd like to resolve it by another way that is recursion. But I haven't done with it.
Before
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 1 1 1 0 0 0 0 0
0 0 1 1 1 1 1 1 0 0
0 0 1 1 1 1 0 0 0 0
0 0 0 0 1 1 0 0 0 0
0 0 0 0 1 1 0 0 0 0
0 0 1 1 1 0 0 0 0 0
0 0 1 1 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
After spreading out
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 3 2 3 0 0 0 0 0
0 0 2 1 2 3 4 5 0 0
0 0 3 2 3 4 0 0 0 0
0 0 0 0 4 5 0 0 0 0
0 0 0 0 5 6 0 0 0 0
0 0 8 7 6 0 0 0 0 0
0 0 9 8 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
Below is my code but I just can spread all 1 numbers with another value but not as I want. So please help me resolve this problem.
#include <iostream>
#define MAX 10
using namespace std;
int data[MAX][MAX] = {
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 1, 1, 1, 0, 0, 0, 0, 0},
{0, 0, 1, 1, 1, 1, 1, 1, 0, 0},
{0, 0, 1, 1, 1, 1, 0, 0, 0, 0},
{0, 0, 0, 0, 1, 1, 0, 0, 0, 0},
{0, 0, 0, 0, 1, 1, 0, 0, 0, 0},
{0, 0, 1, 1, 1, 0, 0, 0, 0, 0},
{0, 0, 1, 1, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
};
int mark[MAX][MAX];
void spreading(int x, int y, int v){
if (x < 0 || x == MAX) return;
if (y < 0 || y == MAX) return;
if(data[x][y] == 0 || mark[x][y] != 0)
return;
data[x][y] = v;
mark[x][y] = v;
spreading(x + 1, y, v);
spreading(x, y + 1, v);
spreading(x - 1, y, v);
spreading(x, y - 1, v);
}
void printArr(int a[MAX][MAX]){
for (int i = 0; i < MAX; ++i) {
cout << endl;
for (int j = 0; j < MAX; ++j) {
cout << a[i][j] << " ";
}
}
}
int main(){
spreading(3, 3, 1);
printArr(data);
system("pause");
return 0;
}
Following may solve your issue: (https://ideone.com/VQmBhU)
void spreading(int x, int y, int v){
// Test if x, y is inside the propagation area
if (x < 0 || x == MAX) return;
if (y < 0 || y == MAX) return;
if (data[x][y] == 0) return;
// if already visited with a better path, cancel.
// if not visited, or the previous visit was worst than this try, continue
if (mark[x][y] != 0 && mark[x][y] <= v) return;
data[x][y] = v;
mark[x][y] = v;
spreading(x + 1, y, v + 1);
spreading(x, y + 1, v + 1);
spreading(x - 1, y, v + 1);
spreading(x, y - 1, v + 1);
}
Some example of 're' visit (with the mark array content):
(1) 0 -> 1 (2) -> 1 2 -> 1 2
0 0 0 0 0 (3) (4) 3
1 <= 5, 3 <= 5 : (4) finished
2 <= 4 : (3) finished
1 <= 3 : (2) finished
4 > 2 : we continue propagation from (1)
(1) 2 -> 1 2
4 3 (2) 3
...

Extract the adjacency matrix from a BGL graph

Using the Boost Graph Library I am looking for a way to extract the adjacency matrix from an underlying graph represented by either boost::adjacency_list or boost::adjacency_matrix. I'd like to use this matrix in conjunction with boost::numeric::ublas to solve a system of simultaneous linear equations.
Here is a minimal example to get you going:
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/adjacency_matrix.hpp>
using namespace boost;
typedef boost::adjacency_list< listS, vecS, directedS > ListGraph;
typedef boost::adjacency_matrix< directedS > MatrixGraph;
int main(){
ListGraph lg;
add_edge (0, 1, lg);
add_edge (0, 3, lg);
add_edge (1, 2, lg);
add_edge (2, 3, lg);
//How do I get the adjacency matrix underlying lg?
MatrixGraph mg(3);
add_edge (0, 1, mg);
add_edge (0, 3, mg);
add_edge (1, 2, mg);
add_edge (2, 3, mg);
//How do I get the adjacency matrix underlying mg?
}
If anyone could come up with an efficient way to obtain the adjacency matrix I would be much obliged. Ideally the solution is compatible with uBLAS. I wonder if there is a way to avoid iteration through the entire graph.
The easiest way to convert adjacency_list into adjacency_matrix is to use boost::copy_graph
Your code for MatrixGraph mg should be modified as follows
#include <boost/graph/copy.hpp>
#include <cassert>
using namespace boost;
typedef boost::adjacency_list< listS, vecS, directedS > ListGraph;
typedef boost::adjacency_matrix< directedS > MatrixGraph;
int main(){
ListGraph lg;
add_edge(0, 1, lg);
add_edge(0, 3, lg);
add_edge(1, 2, lg);
add_edge(2, 3, lg);
//How do I get the adjacency matrix underlying lg?
//How do I get the adjacency matrix underlying mg?
MatrixGraph mg( num_vertices(lg));
boost::copy_graph(lg, mg);
}
Now, to use adjacency matrix with ublas or similar, you can write a simple "access" class to make syntax more compliant with ublas. Continuing previous snippet we get:
template <class Graph>
class MatrixAccessor
{
public:
typedef typename Graph::Matrix Matrix; //actually a vector<
typedef typename Matrix::const_reference const_reference;
MatrixAccessor(const Graph* g)
: m_g(g)
{
static_assert(boost::is_same<size_t, typename Graph::vertex_descriptor>::value, "Vertex descriptor should be of integer type");
}
const_reference operator()(size_t u, size_t v) const
{
return m_g->get_edge(u, v);
}
const Graph* m_g;
};
void use_matrix(const MatrixGraph & mg)
{
MatrixAccessor<MatrixGraph> matr(&mg);
assert(matr(0, 1) == 1);
assert(matr(0, 2) == 0);
}
In case your adjacency_matrix has some edge-bundled properties, you might need to modify the operator() in MatrixAccessor.
Depending on how much uBLAS you use, you can refine MatrixAccessor further. For example, out_edge_iterator for a given vertex of a MatrixGraph is actually an iterator over matrix column; vertex_iterator can be treated as iterator over matrix rows, etc.
Of course, graph matrix is immutable and as such should be used with care.
just as an easy way and I don't know how much it is efficient.
This is what I came up with:
I have used a small world graph and printed the adjacency matrix.
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/small_world_generator.hpp>
#include <boost/random/linear_congruential.hpp>
using namespace std;
using namespace boost;
typedef adjacency_list<vecS, vecS, undirectedS> Graph;
typedef small_world_iterator<boost::minstd_rand, Graph> SWGen;
int main()
{
boost::minstd_rand gen;
int N = 20;
int degree = 4;
double rewiring = 0.;
Graph g(SWGen(gen, N, degree, rewiring), SWGen(), 20);
cout << num_edges(g)<< '\n';
typedef graph_traits<Graph>::edge_iterator edge_iterator;
pair<edge_iterator, edge_iterator> ei = edges(g);
for(edge_iterator edge_iter = ei.first; edge_iter != ei.second; ++edge_iter) {
cout << "(" << source(*edge_iter, g) << ", " << target(*edge_iter, g) << ")\n";
}
vector<vector<int> > mat(N,vector<int>(N));
for (edge_iterator edge_iter = ei.first; edge_iter != ei.second; ++edge_iter){
int a = source(*edge_iter, g);
int b = target(*edge_iter, g);
mat[a][b] = 1;
mat[b][a] = 1;
}
for (int i=0; i<N; i++){
for (int j=0; j<N; j++){
cout << mat[i][j]<<" ";
}
cout <<endl;
}
return 0;
}
Output:
0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1
1 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 1 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 1 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 1 1 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 1 1 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 1 1 0 1 1 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 1 1 0 1 1 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 1 1 0 1 1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 1 0 1 1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 1 0 1 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 1 1 0 1 1 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 1 1 0 1 1 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 1 1 0 1 1 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1 1 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1 1 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1 1 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1
1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0
The current revision of the adjacency_matrix has an undocumented public member m_matrix (see line 640). However, it is a flat vector of tuples <bool, bundled_properties> (line 512). Since the underlying storage looks so different from a ublas matrix, it is most likely not possible to convert a graph to a matrix besides iterating over edges.

Writing a 2D vector to a file? c++

I'm wondering how I can output a 2D vector to a file with spaces in between the values. It's to write a map to a file at a specified size that the user chooses. I am already dynamically loading the map from there. I have a basis for the function but I'm kind of lost on the next bit.
void Map::SetMapSize(int sizeX, int sizeY, const char *filename)
{
std::ofstream out(filename);
out << "[Map]" << std::endl;
MapSizeVector[sizeX][sizeY];
for(int i = 0; i <= sizeX; i++)
{
for(int j = 0; j <= sizeY; j++)
{
std::ostream_iterator<std::string> output_iterator(out, " ");
}
}
}
The Map.txt looks like this:
[Map]
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 1 1 0 0 0 1 1 0 0 1 1 1 1 1 1 0 0
0 0 1 1 0 0 0 1 1 0 0 1 1 1 1 1 1 0 0
0 0 1 1 0 0 0 1 1 0 0 0 0 1 1 0 0 0 0
0 0 1 1 0 0 0 1 1 0 0 0 0 1 1 0 0 0 0
0 0 1 1 1 1 1 1 1 0 0 0 0 1 1 0 0 0 0
0 0 1 1 1 1 1 1 1 0 0 0 0 1 1 0 0 0 0
0 0 1 1 0 0 0 1 1 0 0 0 0 1 1 0 0 0 0
0 0 1 1 0 0 0 1 1 0 0 0 0 1 1 0 0 0 0
0 0 1 1 0 0 0 1 1 0 0 1 1 1 1 1 1 0 0
0 0 1 1 0 0 0 1 1 0 0 1 1 1 1 1 1 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
It also has a details bit underneath it. Basically, I want to rewrite that [Map] part to whatever size x and y the user requests above the [Details] and replacing the existing [Map] part. The numbers are fine with being 0. Thanks!
Declaration of vector in Map.h
std::vector <std::vector <int> > MapSizeVector;
Your function should look like this:
void Map::SetMapSize(int sizeX, int sizeY, const char *filename)
{
std::ofstream out(filename);
out << "[Map]" << std::endl;
MapSizeVector.resize(sizeX);
for(int i = 0; i < sizeX; i++)
{
MapSizeVector[i].resize(sizeY);
for(int j = 0; j < sizeY; j++)
{
char str[20];
sprintf(str, "%d ", MapSizeVector[i][j]);
out << str;
}
out << '\n';
}
}

3d maze recursion method - c++

I am making a 3D maze in c++. I am having trouble with a recursive method to find a valid path between the two endpoints (starting point is m[0][0][0]; endpoint is m[7][7][7];). It checks positions in the array. If its contents are a 1, then it is a valid part of the path; if 0, it is not a valid part of the path. Here is my method:
bool Maze::findPath(int row, int column, int level,string path){
cout << "findPath " << row << ", " << column << ", " << level << " value " << m[row][column][level] << endl;
if(row < 0 || row > 7 || column < 0 || column > 7 || level < 0 || level > 7 ){
cout << "Out of bounds" << endl;
//system("PAUSE");
return false;
}
else if(m[row][column][level] == 0){
cout << "spot is zero" << endl;
//system("PAUSE");
return false;
}
else if(visited[row][column][level] == 1){
cout << "visited" << endl;
return false;
}
else if(row == 7 && column == 7 && level == 7 && m[row][column][level] == 1){
cout << "Found!" << endl;
//system("PAUSE");
return true;
}
else{
visited[row][column][level] = 1;
//cout << "searching..." << endl;
if(row < 7 && findPath(row + 1,column,level,path))
return true;
if(column < 7 && findPath(row,column + 1,level,path))
return true;
if(level < 7 && findPath(row,column,level + 1,path))
return true;
if(row > 7 && findPath(row - 1,column,level,path))
return true;
if(column > 7 && findPath(row,column - 1,level,path))
return true;
if(level > 7 && findPath(row,column,level - 1,path))
return true;
}
return false;
}
So the method checks for "Out of bounds", an invalid spot on the path (zero), a visited location. I'm not sure what exactly I'm missing, but the method returns true to mazes that are unsolvable. Can anybody see some blatant mistake that I may be missing with my recursive call? Thanks
EDIT: Fixed a few code mistakes, but it still seems to be "solving" unsolvable mazes.
Here's an example of a solvable maze that it is saying is not possible to solve:
1 0 0 0 0 0 0 1
0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 0
0 0 0 1 0 0 0 1
0 0 0 1 0 0 0 0
1 0 0 1 0 1 0 0
0 0 0 1 0 0 0 0
1 0 0 1 0 0 0 1
1 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0
1 1 1 1 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0
0 1 1 0 0 0 0 0
0 0 0 1 0 1 1 1
0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0
0 0 0 1 0 0 0 1
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0
0 0 0 0 0 0 0 1
0 0 0 1 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1
0 0 0 1 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
1 0 0 0 0 1 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1
1 1 1 1 0 0 0 0
0 0 0 1 0 0 0 0
0 0 0 1 0 0 0 1
0 0 0 0 0 0 1 0
0 0 0 0 0 0 1 0
1 0 0 0 0 1 0 0
0 1 0 0 0 0 0 0
1 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0
0 0 0 0 0 0 1 0
0 0 0 0 0 0 1 0
1 1 1 1 0 0 0 0
0 0 0 1 0 0 0 0
0 0 0 1 0 0 0 0
1 1 1 1 0 0 0 1
1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0
0 0 1 0 0 0 0 0
0 0 1 0 0 0 0 0
0 0 1 0 0 0 0 0
0 0 1 0 0 0 0 1
0 0 1 0 0 0 0 1
0 0 1 0 0 0 0 1
0 0 1 0 0 0 0 1
0 0 1 1 0 0 0 1
0 0 0 1 0 0 0 1
0 0 0 1 0 0 0 1
0 0 0 1 1 1 0 1
There's a problem in the findPath(++row,column,level,path) (and similar recursive calls): you don't want the variable increments to carry over to the other recursive calls. (For example, the variable row in findPath(row,++column,level,path) would be affected by the first recursive call.)
Use findPath(row + 1,column,level,path) (and similar) instead.
Also, in the last three recursive calls, you're not making the right tests:
//instead of level < 7
if(level < 7 && findPath(--row,column,level,path)) //should be row > 0
return true;
if(level < 7 && findPath(row,--column,level,path)) //should be column > 0
return true;
if(level < 7 && findPath(row,column,--level,path)) //should be level > 0
return true;
EDIT
However, you don't actually need these tests since you filter out out of bounds errors at the top of your recursive function. Therefore, these calls can be simplified to:
return findPath(row + 1,column,level,path) || findPath(row,column + 1,level,path)
|| findPath(row,column,level + 1,path) || findPath(row - 1,column,level,path)
|| findPath(row,column - 1,level,path) || findPath(row,column,level - 1,path);
Additionally, the test && m[row][column][level] == 1 is redundant since the else if(m[row][column][level] == 0) takes care of that. (I'd check m[7][7][7] before even calling this function the first time, by the way.)
I just Finished this algorithm as an assignment for a class, ours only used a 5x5 block as the maze, but I found that it will go very slowly testing all possibilities each time it reaches the block from any angle, I found that the program can be sped up significantly by setting values in your array to 0 as you determine that they're not useful. I did it at the return false at the end of the function.